/** * Describes utility functions for parsing CPER into JSON IR. * * Author: Lawrence.Tang@arm.com **/ #include #include #include "edk/Cper.h" #include "cper-utils.h" //The available severity types for CPER. const char *CPER_SEVERITY_TYPES[4] = { "Recoverable", "Fatal", "Corrected", "Informational" }; //Converts the given generic CPER error status to JSON IR. json_object * cper_generic_error_status_to_ir(EFI_GENERIC_ERROR_STATUS *error_status) { json_object *error_status_ir = json_object_new_object(); //Error type. json_object_object_add(error_status_ir, "errorType", integer_to_readable_pair_with_desc( error_status->Type, 18, CPER_GENERIC_ERROR_TYPES_KEYS, CPER_GENERIC_ERROR_TYPES_VALUES, CPER_GENERIC_ERROR_TYPES_DESCRIPTIONS, "Unknown (Reserved)")); //Boolean bit fields. json_object_object_add( error_status_ir, "addressSignal", json_object_new_boolean(error_status->AddressSignal)); json_object_object_add( error_status_ir, "controlSignal", json_object_new_boolean(error_status->ControlSignal)); json_object_object_add( error_status_ir, "dataSignal", json_object_new_boolean(error_status->DataSignal)); json_object_object_add( error_status_ir, "detectedByResponder", json_object_new_boolean(error_status->DetectedByResponder)); json_object_object_add( error_status_ir, "detectedByRequester", json_object_new_boolean(error_status->DetectedByRequester)); json_object_object_add( error_status_ir, "firstError", json_object_new_boolean(error_status->FirstError)); json_object_object_add( error_status_ir, "overflowDroppedLogs", json_object_new_boolean(error_status->OverflowNotLogged)); return error_status_ir; } //Converts the given CPER-JSON generic error status into a CPER structure. void ir_generic_error_status_to_cper( json_object *error_status, EFI_GENERIC_ERROR_STATUS *error_status_cper) { error_status_cper->Type = readable_pair_to_integer( json_object_object_get(error_status, "errorType")); error_status_cper->AddressSignal = json_object_get_boolean( json_object_object_get(error_status, "addressSignal")); error_status_cper->ControlSignal = json_object_get_boolean( json_object_object_get(error_status, "controlSignal")); error_status_cper->DataSignal = json_object_get_boolean( json_object_object_get(error_status, "dataSignal")); error_status_cper->DetectedByResponder = json_object_get_boolean( json_object_object_get(error_status, "detectedByResponder")); error_status_cper->DetectedByRequester = json_object_get_boolean( json_object_object_get(error_status, "detectedByRequester")); error_status_cper->FirstError = json_object_get_boolean( json_object_object_get(error_status, "firstError")); error_status_cper->OverflowNotLogged = json_object_get_boolean( json_object_object_get(error_status, "overflowDroppedLogs")); } //Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order. json_object *uniform_struct64_to_ir(UINT64 *start, int len, const char *names[]) { json_object *result = json_object_new_object(); UINT64 *cur = start; for (int i = 0; i < len; i++) { json_object_object_add(result, names[i], json_object_new_uint64(*cur)); cur++; } return result; } //Converts a single uniform struct of UINT32s into intermediate JSON IR format, given names for each field in byte order. json_object *uniform_struct_to_ir(UINT32 *start, int len, const char *names[]) { json_object *result = json_object_new_object(); UINT32 *cur = start; for (int i = 0; i < len; i++) { json_object_object_add(result, names[i], json_object_new_uint64(*cur)); cur++; } return result; } //Converts a single object containing UINT32s into a uniform struct. void ir_to_uniform_struct64(json_object *ir, UINT64 *start, int len, const char *names[]) { UINT64 *cur = start; for (int i = 0; i < len; i++) { *cur = json_object_get_uint64( json_object_object_get(ir, names[i])); cur++; } } //Converts a single object containing UINT32s into a uniform struct. void ir_to_uniform_struct(json_object *ir, UINT32 *start, int len, const char *names[]) { UINT32 *cur = start; for (int i = 0; i < len; i++) { *cur = (UINT32)json_object_get_uint64( json_object_object_get(ir, names[i])); cur++; } } //Converts a single integer value to an object containing a value, and a readable name if possible. json_object *integer_to_readable_pair(UINT64 value, int len, int keys[], const char *values[], const char *default_value) { json_object *result = json_object_new_object(); json_object_object_add(result, "value", json_object_new_uint64(value)); //Search for human readable name, add. const char *name = default_value; for (int i = 0; i < len; i++) { if (keys[i] == value) name = values[i]; } json_object_object_add(result, "name", json_object_new_string(name)); return result; } //Converts a single integer value to an object containing a value, readable name and description if possible. json_object *integer_to_readable_pair_with_desc(int value, int len, int keys[], const char *values[], const char *descriptions[], const char *default_value) { json_object *result = json_object_new_object(); json_object_object_add(result, "value", json_object_new_int(value)); //Search for human readable name, add. const char *name = default_value; for (int i = 0; i < len; i++) { if (keys[i] == value) { name = values[i]; json_object_object_add( result, "description", json_object_new_string(descriptions[i])); } } json_object_object_add(result, "name", json_object_new_string(name)); return result; } //Returns a single UINT64 value from the given readable pair object. //Assumes the integer value is held in the "value" field. UINT64 readable_pair_to_integer(json_object *pair) { return json_object_get_uint64(json_object_object_get(pair, "value")); } //Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left. json_object *bitfield_to_ir(UINT64 bitfield, int num_fields, const char *names[]) { json_object *result = json_object_new_object(); for (int i = 0; i < num_fields; i++) { json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1)); } return result; } //Converts the given IR bitfield into a standard UINT64 bitfield, with fields beginning from bit 0. UINT64 ir_to_bitfield(json_object *ir, int num_fields, const char *names[]) { UINT64 result = 0x0; for (int i = 0; i < num_fields; i++) { if (json_object_get_boolean( json_object_object_get(ir, names[i]))) result |= (0x1 << i); } return result; } //Converts the given UINT64 array into a JSON IR array, given the length. json_object *uint64_array_to_ir_array(UINT64 *array, int len) { json_object *array_ir = json_object_new_array(); for (int i = 0; i < len; i++) json_object_array_add(array_ir, json_object_new_uint64(array[i])); return array_ir; } //Converts a single UINT16 revision number into JSON IR representation. json_object *revision_to_ir(UINT16 revision) { json_object *revision_info = json_object_new_object(); json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8)); json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF)); return revision_info; } //Returns the appropriate string for the given integer severity. const char *severity_to_string(UINT32 severity) { return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown"; } //Converts a single EFI timestamp to string, at the given output. //Output must be at least TIMESTAMP_LENGTH bytes long. void timestamp_to_string(char *out, EFI_ERROR_TIME_STAMP *timestamp) { sprintf(out, "%02hhu%02hhu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu.000", bcd_to_int(timestamp->Century) % 100, //Cannot go to three digits. bcd_to_int(timestamp->Year) % 100, //Cannot go to three digits. bcd_to_int(timestamp->Month), bcd_to_int(timestamp->Day), bcd_to_int(timestamp->Hours), bcd_to_int(timestamp->Minutes), bcd_to_int(timestamp->Seconds)); } //Converts a single timestamp string to an EFI timestamp. void string_to_timestamp(EFI_ERROR_TIME_STAMP *out, const char *timestamp) { //Ignore invalid timestamps. if (timestamp == NULL) return; sscanf(timestamp, "%2hhu%2hhu-%hhu-%hhuT%hhu:%hhu:%hhu.000", &out->Century, &out->Year, &out->Month, &out->Day, &out->Hours, &out->Minutes, &out->Seconds); //Convert back to BCD. out->Century = int_to_bcd(out->Century); out->Year = int_to_bcd(out->Year); out->Month = int_to_bcd(out->Month); out->Day = int_to_bcd(out->Day); out->Hours = int_to_bcd(out->Hours); out->Minutes = int_to_bcd(out->Minutes); out->Seconds = int_to_bcd(out->Seconds); } //Helper function to convert an EDK EFI GUID into a string for intermediate use. void guid_to_string(char *out, EFI_GUID *guid) { sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x", guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]); } //Helper function to convert a string into an EDK EFI GUID. void string_to_guid(EFI_GUID *out, const char *guid) { //Ignore invalid GUIDs. if (guid == NULL) return; sscanf(guid, "%08x-%04hx-%04hx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", &out->Data1, &out->Data2, &out->Data3, out->Data4, out->Data4 + 1, out->Data4 + 2, out->Data4 + 3, out->Data4 + 4, out->Data4 + 5, out->Data4 + 6, out->Data4 + 7); } //Returns one if two EFI GUIDs are equal, zero otherwise. int guid_equal(EFI_GUID *a, EFI_GUID *b) { //Check top base 3 components. if (a->Data1 != b->Data1 || a->Data2 != b->Data2 || a->Data3 != b->Data3) { return 0; } //Check Data4 array for equality. for (int i = 0; i < 8; i++) { if (a->Data4[i] != b->Data4[i]) return 0; } return 1; }