Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting firmware CPER sections from binary and JSON format |
| 3 | * into an intermediate format. |
| 4 | * |
| 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame^] | 8 | #include <json.h> |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 9 | #include "../edk/Cper.h" |
| 10 | #include "../cper-utils.h" |
| 11 | #include "cper-section-firmware.h" |
| 12 | |
| 13 | //Converts a single firmware CPER section into JSON IR. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 14 | json_object * |
| 15 | cper_section_firmware_to_ir(void *section, |
| 16 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor) |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 17 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 18 | EFI_FIRMWARE_ERROR_DATA *firmware_error = |
| 19 | (EFI_FIRMWARE_ERROR_DATA *)section; |
| 20 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 21 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | //Record type. |
| 23 | json_object *record_type = integer_to_readable_pair( |
| 24 | firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS, |
| 25 | FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)"); |
| 26 | json_object_object_add(section_ir, "errorRecordType", record_type); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 27 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 28 | //Revision, record identifier. |
| 29 | json_object_object_add(section_ir, "revision", |
| 30 | json_object_new_int(firmware_error->Revision)); |
| 31 | json_object_object_add( |
| 32 | section_ir, "recordID", |
| 33 | json_object_new_uint64(firmware_error->RecordId)); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 34 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 35 | //Record GUID. |
| 36 | char record_id_guid[GUID_STRING_LENGTH]; |
| 37 | guid_to_string(record_id_guid, &firmware_error->RecordIdGuid); |
| 38 | json_object_object_add(section_ir, "recordIDGUID", |
| 39 | json_object_new_string(record_id_guid)); |
| 40 | |
| 41 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | //Converts a single firmware CPER-JSON section into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 45 | void ir_section_firmware_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 46 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 47 | EFI_FIRMWARE_ERROR_DATA *section_cper = |
| 48 | (EFI_FIRMWARE_ERROR_DATA *)calloc( |
| 49 | 1, sizeof(EFI_FIRMWARE_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 50 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 51 | //Record fields. |
| 52 | section_cper->ErrorType = readable_pair_to_integer( |
| 53 | json_object_object_get(section, "errorRecordType")); |
| 54 | section_cper->Revision = json_object_get_int( |
| 55 | json_object_object_get(section, "revision")); |
| 56 | section_cper->RecordId = json_object_get_uint64( |
| 57 | json_object_object_get(section, "recordID")); |
| 58 | string_to_guid(§ion_cper->RecordIdGuid, |
| 59 | json_object_get_string(json_object_object_get( |
| 60 | section, "recordIDGUID"))); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 61 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 62 | //Write to stream, free resources. |
| 63 | fwrite(section_cper, sizeof(EFI_FIRMWARE_ERROR_DATA), 1, out); |
| 64 | fflush(out); |
| 65 | free(section_cper); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 66 | } |