Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes utility functions for parsing CPER into JSON IR. |
| 3 | * |
| 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include "json.h" |
| 9 | #include "edk/Cper.h" |
| 10 | #include "cper-utils.h" |
| 11 | |
| 12 | //The available severity types for CPER. |
| 13 | const char* CPER_SEVERITY_TYPES[4] = {"Recoverable", "Fatal", "Corrected", "Informational"}; |
| 14 | |
Lawrence Tang | 3c43f74 | 2022-07-05 11:37:17 +0100 | [diff] [blame] | 15 | //Converts a single integer value to an object containing a value, and a readable name if possible. |
| 16 | json_object* integer_to_readable_pair(int value, int len, int keys[], const char* values[], const char* default_value) |
| 17 | { |
| 18 | json_object* result = json_object_new_object(); |
| 19 | json_object_object_add(result, "value", json_object_new_int(value)); |
| 20 | |
| 21 | //Search for human readable name, add. |
Lawrence Tang | 794312c | 2022-07-05 14:46:10 +0100 | [diff] [blame] | 22 | const char* name = default_value; |
Lawrence Tang | 3c43f74 | 2022-07-05 11:37:17 +0100 | [diff] [blame] | 23 | for (int i=0; i<len; i++) |
| 24 | { |
| 25 | if (keys[i] == value) |
| 26 | name = values[i]; |
| 27 | } |
| 28 | |
| 29 | json_object_object_add(result, "name", json_object_new_string(name)); |
| 30 | return result; |
| 31 | } |
| 32 | |
Lawrence Tang | 794312c | 2022-07-05 14:46:10 +0100 | [diff] [blame] | 33 | //Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left. |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 34 | json_object* bitfield_to_ir(UINT64 bitfield, int num_fields, const char* names[]) |
Lawrence Tang | 794312c | 2022-07-05 14:46:10 +0100 | [diff] [blame] | 35 | { |
| 36 | json_object* result = json_object_new_object(); |
| 37 | for (int i=0; i<num_fields; i++) |
| 38 | { |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 39 | json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1)); |
Lawrence Tang | 794312c | 2022-07-05 14:46:10 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 46 | //Converts a single UINT16 revision number into JSON IR representation. |
| 47 | json_object* revision_to_ir(UINT16 revision) |
| 48 | { |
| 49 | json_object* revision_info = json_object_new_object(); |
| 50 | json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8)); |
| 51 | json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF)); |
| 52 | return revision_info; |
| 53 | } |
| 54 | |
| 55 | //Returns the appropriate string for the given integer severity. |
| 56 | const char* severity_to_string(UINT8 severity) |
| 57 | { |
| 58 | return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown"; |
| 59 | } |
| 60 | |
| 61 | //Helper function to convert an EDK EFI GUID into a string for intermediate use. |
| 62 | void guid_to_string(char* out, EFI_GUID* guid) |
| 63 | { |
| 64 | sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x", |
| 65 | guid->Data1, |
| 66 | guid->Data2, |
| 67 | guid->Data3, |
| 68 | guid->Data4[0], |
| 69 | guid->Data4[1], |
| 70 | guid->Data4[2], |
| 71 | guid->Data4[3], |
| 72 | guid->Data4[4], |
| 73 | guid->Data4[5], |
| 74 | guid->Data4[6], |
| 75 | guid->Data4[7]); |
| 76 | } |
| 77 | |
| 78 | //Returns one if two EFI GUIDs are equal, zero otherwise. |
| 79 | int guid_equal(EFI_GUID* a, EFI_GUID* b) |
| 80 | { |
| 81 | //Check top base 3 components. |
| 82 | if (a->Data1 != b->Data1 |
| 83 | || a->Data2 != b->Data2 |
| 84 | || a->Data3 != b->Data3) |
| 85 | { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | //Check Data4 array for equality. |
| 90 | for (int i=0; i<8; i++) |
| 91 | { |
| 92 | if (a->Data4[i] != b->Data4[i]) |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | return 1; |
| 97 | } |