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 | |
| 15 | //Converts a single UINT16 revision number into JSON IR representation. |
| 16 | json_object* revision_to_ir(UINT16 revision) |
| 17 | { |
| 18 | json_object* revision_info = json_object_new_object(); |
| 19 | json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8)); |
| 20 | json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF)); |
| 21 | return revision_info; |
| 22 | } |
| 23 | |
| 24 | //Returns the appropriate string for the given integer severity. |
| 25 | const char* severity_to_string(UINT8 severity) |
| 26 | { |
| 27 | return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown"; |
| 28 | } |
| 29 | |
| 30 | //Helper function to convert an EDK EFI GUID into a string for intermediate use. |
| 31 | void guid_to_string(char* out, EFI_GUID* guid) |
| 32 | { |
| 33 | sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x", |
| 34 | guid->Data1, |
| 35 | guid->Data2, |
| 36 | guid->Data3, |
| 37 | guid->Data4[0], |
| 38 | guid->Data4[1], |
| 39 | guid->Data4[2], |
| 40 | guid->Data4[3], |
| 41 | guid->Data4[4], |
| 42 | guid->Data4[5], |
| 43 | guid->Data4[6], |
| 44 | guid->Data4[7]); |
| 45 | } |
| 46 | |
| 47 | //Returns one if two EFI GUIDs are equal, zero otherwise. |
| 48 | int guid_equal(EFI_GUID* a, EFI_GUID* b) |
| 49 | { |
| 50 | //Check top base 3 components. |
| 51 | if (a->Data1 != b->Data1 |
| 52 | || a->Data2 != b->Data2 |
| 53 | || a->Data3 != b->Data3) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | //Check Data4 array for equality. |
| 59 | for (int i=0; i<8; i++) |
| 60 | { |
| 61 | if (a->Data4[i] != b->Data4[i]) |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | return 1; |
| 66 | } |