11b0b00e3SLawrence Tang /** 21b0b00e3SLawrence Tang * Describes utility functions for parsing CPER into JSON IR. 31b0b00e3SLawrence Tang * 41b0b00e3SLawrence Tang * Author: Lawrence.Tang@arm.com 51b0b00e3SLawrence Tang **/ 61b0b00e3SLawrence Tang 71b0b00e3SLawrence Tang #include <stdio.h> 81b0b00e3SLawrence Tang #include "json.h" 91b0b00e3SLawrence Tang #include "edk/Cper.h" 101b0b00e3SLawrence Tang #include "cper-utils.h" 111b0b00e3SLawrence Tang 121b0b00e3SLawrence Tang //The available severity types for CPER. 131b0b00e3SLawrence Tang const char* CPER_SEVERITY_TYPES[4] = {"Recoverable", "Fatal", "Corrected", "Informational"}; 141b0b00e3SLawrence Tang 15*7f21db6cSLawrence Tang //Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order. 16*7f21db6cSLawrence Tang json_object* uniform_struct64_to_ir(UINT64* start, int len, const char* names[]) 17*7f21db6cSLawrence Tang { 18*7f21db6cSLawrence Tang json_object* result = json_object_new_object(); 19*7f21db6cSLawrence Tang 20*7f21db6cSLawrence Tang UINT64* cur = start; 21*7f21db6cSLawrence Tang for (int i=0; i<len; i++) 22*7f21db6cSLawrence Tang { 23*7f21db6cSLawrence Tang json_object_object_add(result, names[i], json_object_new_uint64(*cur)); 24*7f21db6cSLawrence Tang cur++; 25*7f21db6cSLawrence Tang } 26*7f21db6cSLawrence Tang 27*7f21db6cSLawrence Tang return result; 28*7f21db6cSLawrence Tang } 29*7f21db6cSLawrence Tang 30*7f21db6cSLawrence Tang //Converts a single uniform struct of UINT32s into intermediate JSON IR format, given names for each field in byte order. 31*7f21db6cSLawrence Tang json_object* uniform_struct_to_ir(UINT32* start, int len, const char* names[]) 32*7f21db6cSLawrence Tang { 33*7f21db6cSLawrence Tang json_object* result = json_object_new_object(); 34*7f21db6cSLawrence Tang 35*7f21db6cSLawrence Tang UINT32* cur = start; 36*7f21db6cSLawrence Tang for (int i=0; i<len; i++) 37*7f21db6cSLawrence Tang { 38*7f21db6cSLawrence Tang json_object_object_add(result, names[i], json_object_new_uint64(*cur)); 39*7f21db6cSLawrence Tang cur++; 40*7f21db6cSLawrence Tang } 41*7f21db6cSLawrence Tang 42*7f21db6cSLawrence Tang return result; 43*7f21db6cSLawrence Tang } 44*7f21db6cSLawrence Tang 453c43f743SLawrence Tang //Converts a single integer value to an object containing a value, and a readable name if possible. 463c43f743SLawrence Tang json_object* integer_to_readable_pair(int value, int len, int keys[], const char* values[], const char* default_value) 473c43f743SLawrence Tang { 483c43f743SLawrence Tang json_object* result = json_object_new_object(); 493c43f743SLawrence Tang json_object_object_add(result, "value", json_object_new_int(value)); 503c43f743SLawrence Tang 513c43f743SLawrence Tang //Search for human readable name, add. 52794312c8SLawrence Tang const char* name = default_value; 533c43f743SLawrence Tang for (int i=0; i<len; i++) 543c43f743SLawrence Tang { 553c43f743SLawrence Tang if (keys[i] == value) 563c43f743SLawrence Tang name = values[i]; 573c43f743SLawrence Tang } 583c43f743SLawrence Tang 593c43f743SLawrence Tang json_object_object_add(result, "name", json_object_new_string(name)); 603c43f743SLawrence Tang return result; 613c43f743SLawrence Tang } 623c43f743SLawrence Tang 63*7f21db6cSLawrence Tang //Converts a single integer value to an object containing a value, readable name and description if possible. 64*7f21db6cSLawrence Tang json_object* integer_to_readable_pair_with_desc(int value, int len, int keys[], const char* values[], 65*7f21db6cSLawrence Tang const char* descriptions[], const char* default_value) 66*7f21db6cSLawrence Tang { 67*7f21db6cSLawrence Tang json_object* result = json_object_new_object(); 68*7f21db6cSLawrence Tang json_object_object_add(result, "value", json_object_new_int(value)); 69*7f21db6cSLawrence Tang 70*7f21db6cSLawrence Tang //Search for human readable name, add. 71*7f21db6cSLawrence Tang const char* name = default_value; 72*7f21db6cSLawrence Tang for (int i=0; i<len; i++) 73*7f21db6cSLawrence Tang { 74*7f21db6cSLawrence Tang if (keys[i] == value) 75*7f21db6cSLawrence Tang { 76*7f21db6cSLawrence Tang name = values[i]; 77*7f21db6cSLawrence Tang json_object_object_add(result, "description", json_object_new_string(descriptions[i])); 78*7f21db6cSLawrence Tang } 79*7f21db6cSLawrence Tang } 80*7f21db6cSLawrence Tang 81*7f21db6cSLawrence Tang json_object_object_add(result, "name", json_object_new_string(name)); 82*7f21db6cSLawrence Tang return result; 83*7f21db6cSLawrence Tang } 84*7f21db6cSLawrence Tang 85794312c8SLawrence Tang //Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left. 863d0e4f24SLawrence Tang json_object* bitfield_to_ir(UINT64 bitfield, int num_fields, const char* names[]) 87794312c8SLawrence Tang { 88794312c8SLawrence Tang json_object* result = json_object_new_object(); 89794312c8SLawrence Tang for (int i=0; i<num_fields; i++) 90794312c8SLawrence Tang { 912800cd8eSLawrence Tang json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1)); 92794312c8SLawrence Tang } 93794312c8SLawrence Tang 94794312c8SLawrence Tang return result; 95794312c8SLawrence Tang } 96794312c8SLawrence Tang 97794312c8SLawrence Tang 981b0b00e3SLawrence Tang //Converts a single UINT16 revision number into JSON IR representation. 991b0b00e3SLawrence Tang json_object* revision_to_ir(UINT16 revision) 1001b0b00e3SLawrence Tang { 1011b0b00e3SLawrence Tang json_object* revision_info = json_object_new_object(); 1021b0b00e3SLawrence Tang json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8)); 1031b0b00e3SLawrence Tang json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF)); 1041b0b00e3SLawrence Tang return revision_info; 1051b0b00e3SLawrence Tang } 1061b0b00e3SLawrence Tang 1071b0b00e3SLawrence Tang //Returns the appropriate string for the given integer severity. 1081b0b00e3SLawrence Tang const char* severity_to_string(UINT8 severity) 1091b0b00e3SLawrence Tang { 1101b0b00e3SLawrence Tang return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown"; 1111b0b00e3SLawrence Tang } 1121b0b00e3SLawrence Tang 1131b0b00e3SLawrence Tang //Helper function to convert an EDK EFI GUID into a string for intermediate use. 1141b0b00e3SLawrence Tang void guid_to_string(char* out, EFI_GUID* guid) 1151b0b00e3SLawrence Tang { 1161b0b00e3SLawrence Tang sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x", 1171b0b00e3SLawrence Tang guid->Data1, 1181b0b00e3SLawrence Tang guid->Data2, 1191b0b00e3SLawrence Tang guid->Data3, 1201b0b00e3SLawrence Tang guid->Data4[0], 1211b0b00e3SLawrence Tang guid->Data4[1], 1221b0b00e3SLawrence Tang guid->Data4[2], 1231b0b00e3SLawrence Tang guid->Data4[3], 1241b0b00e3SLawrence Tang guid->Data4[4], 1251b0b00e3SLawrence Tang guid->Data4[5], 1261b0b00e3SLawrence Tang guid->Data4[6], 1271b0b00e3SLawrence Tang guid->Data4[7]); 1281b0b00e3SLawrence Tang } 1291b0b00e3SLawrence Tang 1301b0b00e3SLawrence Tang //Returns one if two EFI GUIDs are equal, zero otherwise. 1311b0b00e3SLawrence Tang int guid_equal(EFI_GUID* a, EFI_GUID* b) 1321b0b00e3SLawrence Tang { 1331b0b00e3SLawrence Tang //Check top base 3 components. 1341b0b00e3SLawrence Tang if (a->Data1 != b->Data1 1351b0b00e3SLawrence Tang || a->Data2 != b->Data2 1361b0b00e3SLawrence Tang || a->Data3 != b->Data3) 1371b0b00e3SLawrence Tang { 1381b0b00e3SLawrence Tang return 0; 1391b0b00e3SLawrence Tang } 1401b0b00e3SLawrence Tang 1411b0b00e3SLawrence Tang //Check Data4 array for equality. 1421b0b00e3SLawrence Tang for (int i=0; i<8; i++) 1431b0b00e3SLawrence Tang { 1441b0b00e3SLawrence Tang if (a->Data4[i] != b->Data4[i]) 1451b0b00e3SLawrence Tang return 0; 1461b0b00e3SLawrence Tang } 1471b0b00e3SLawrence Tang 1481b0b00e3SLawrence Tang return 1; 1491b0b00e3SLawrence Tang }