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. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 4 | * |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 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> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 9 | #include <libcper/Cper.h> |
| 10 | #include <libcper/cper-utils.h> |
| 11 | #include <libcper/sections/cper-section-firmware.h> |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 12 | |
| 13 | //Converts a single firmware CPER section into JSON IR. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 14 | json_object *cper_section_firmware_to_ir(void *section) |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 15 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 16 | EFI_FIRMWARE_ERROR_DATA *firmware_error = |
| 17 | (EFI_FIRMWARE_ERROR_DATA *)section; |
| 18 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 19 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 20 | //Record type. |
| 21 | json_object *record_type = integer_to_readable_pair( |
| 22 | firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS, |
| 23 | FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)"); |
| 24 | json_object_object_add(section_ir, "errorRecordType", record_type); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 25 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 26 | //Revision, record identifier. |
| 27 | json_object_object_add(section_ir, "revision", |
| 28 | json_object_new_int(firmware_error->Revision)); |
| 29 | json_object_object_add( |
| 30 | section_ir, "recordID", |
| 31 | json_object_new_uint64(firmware_error->RecordId)); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 32 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 33 | //Record GUID. |
| 34 | char record_id_guid[GUID_STRING_LENGTH]; |
| 35 | guid_to_string(record_id_guid, &firmware_error->RecordIdGuid); |
| 36 | json_object_object_add(section_ir, "recordIDGUID", |
| 37 | json_object_new_string(record_id_guid)); |
| 38 | |
| 39 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | //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] | 43 | void ir_section_firmware_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 44 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 45 | EFI_FIRMWARE_ERROR_DATA *section_cper = |
| 46 | (EFI_FIRMWARE_ERROR_DATA *)calloc( |
| 47 | 1, sizeof(EFI_FIRMWARE_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 48 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 49 | //Record fields. |
| 50 | section_cper->ErrorType = readable_pair_to_integer( |
| 51 | json_object_object_get(section, "errorRecordType")); |
| 52 | section_cper->Revision = json_object_get_int( |
| 53 | json_object_object_get(section, "revision")); |
| 54 | section_cper->RecordId = json_object_get_uint64( |
| 55 | json_object_object_get(section, "recordID")); |
| 56 | string_to_guid(§ion_cper->RecordIdGuid, |
| 57 | json_object_get_string(json_object_object_get( |
| 58 | section, "recordIDGUID"))); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 59 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 60 | //Write to stream, free resources. |
| 61 | fwrite(section_cper, sizeof(EFI_FIRMWARE_ERROR_DATA), 1, out); |
| 62 | fflush(out); |
| 63 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 64 | } |