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 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 const 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 33 //Converts the given uint8 bitfield to IR, assuming bit 0 starts on the left. 34 json_object* bitfield8_to_ir(UINT8 bitfield, int num_fields, const char* names[]) 35 { 36 json_object* result = json_object_new_object(); 37 for (int i=0; i<num_fields; i++) 38 { 39 json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> (7 - i)) & 0b1)); 40 } 41 42 return result; 43 } 44 45 //Converts the given bitfield to IR, assuming bit 0 starts on the left. 46 json_object* bitfield_to_ir(UINT32 bitfield, int num_fields, const char* names[]) 47 { 48 json_object* result = json_object_new_object(); 49 for (int i=0; i<num_fields; i++) 50 { 51 json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> (31 - i)) & 0b1)); 52 } 53 54 return result; 55 } 56 57 //Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left. 58 json_object* bitfield64_to_ir(UINT64 bitfield, int num_fields, const char* names[]) 59 { 60 json_object* result = json_object_new_object(); 61 for (int i=0; i<num_fields; i++) 62 { 63 json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> (63 - i)) & 0b1)); 64 } 65 66 return result; 67 } 68 69 70 //Converts a single UINT16 revision number into JSON IR representation. 71 json_object* revision_to_ir(UINT16 revision) 72 { 73 json_object* revision_info = json_object_new_object(); 74 json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8)); 75 json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF)); 76 return revision_info; 77 } 78 79 //Returns the appropriate string for the given integer severity. 80 const char* severity_to_string(UINT8 severity) 81 { 82 return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown"; 83 } 84 85 //Helper function to convert an EDK EFI GUID into a string for intermediate use. 86 void guid_to_string(char* out, EFI_GUID* guid) 87 { 88 sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x", 89 guid->Data1, 90 guid->Data2, 91 guid->Data3, 92 guid->Data4[0], 93 guid->Data4[1], 94 guid->Data4[2], 95 guid->Data4[3], 96 guid->Data4[4], 97 guid->Data4[5], 98 guid->Data4[6], 99 guid->Data4[7]); 100 } 101 102 //Returns one if two EFI GUIDs are equal, zero otherwise. 103 int guid_equal(EFI_GUID* a, EFI_GUID* b) 104 { 105 //Check top base 3 components. 106 if (a->Data1 != b->Data1 107 || a->Data2 != b->Data2 108 || a->Data3 != b->Data3) 109 { 110 return 0; 111 } 112 113 //Check Data4 array for equality. 114 for (int i=0; i<8; i++) 115 { 116 if (a->Data4[i] != b->Data4[i]) 117 return 0; 118 } 119 120 return 1; 121 }