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. |
| 22 | char* name = default_value; |
| 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 | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 33 | //Converts a single UINT16 revision number into JSON IR representation. |
| 34 | json_object* revision_to_ir(UINT16 revision) |
| 35 | { |
| 36 | json_object* revision_info = json_object_new_object(); |
| 37 | json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8)); |
| 38 | json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF)); |
| 39 | return revision_info; |
| 40 | } |
| 41 | |
| 42 | //Returns the appropriate string for the given integer severity. |
| 43 | const char* severity_to_string(UINT8 severity) |
| 44 | { |
| 45 | return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown"; |
| 46 | } |
| 47 | |
| 48 | //Helper function to convert an EDK EFI GUID into a string for intermediate use. |
| 49 | void guid_to_string(char* out, EFI_GUID* guid) |
| 50 | { |
| 51 | sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x", |
| 52 | guid->Data1, |
| 53 | guid->Data2, |
| 54 | guid->Data3, |
| 55 | guid->Data4[0], |
| 56 | guid->Data4[1], |
| 57 | guid->Data4[2], |
| 58 | guid->Data4[3], |
| 59 | guid->Data4[4], |
| 60 | guid->Data4[5], |
| 61 | guid->Data4[6], |
| 62 | guid->Data4[7]); |
| 63 | } |
| 64 | |
| 65 | //Returns one if two EFI GUIDs are equal, zero otherwise. |
| 66 | int guid_equal(EFI_GUID* a, EFI_GUID* b) |
| 67 | { |
| 68 | //Check top base 3 components. |
| 69 | if (a->Data1 != b->Data1 |
| 70 | || a->Data2 != b->Data2 |
| 71 | || a->Data3 != b->Data3) |
| 72 | { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | //Check Data4 array for equality. |
| 77 | for (int i=0; i<8; i++) |
| 78 | { |
| 79 | if (a->Data4[i] != b->Data4[i]) |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | return 1; |
| 84 | } |