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> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 12 | #include <libcper/log.h> |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 13 | |
| 14 | //Converts a single firmware CPER section into JSON IR. |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 15 | json_object *cper_section_firmware_to_ir(const UINT8 *section, UINT32 size) |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 16 | { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 17 | if (size < sizeof(EFI_FIRMWARE_ERROR_DATA)) { |
| 18 | return NULL; |
| 19 | } |
| 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | EFI_FIRMWARE_ERROR_DATA *firmware_error = |
| 22 | (EFI_FIRMWARE_ERROR_DATA *)section; |
| 23 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 24 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 25 | //Record type. |
| 26 | json_object *record_type = integer_to_readable_pair( |
| 27 | firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS, |
| 28 | FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)"); |
| 29 | json_object_object_add(section_ir, "errorRecordType", record_type); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 30 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 31 | //Revision, record identifier. |
| 32 | json_object_object_add(section_ir, "revision", |
| 33 | json_object_new_int(firmware_error->Revision)); |
| 34 | json_object_object_add( |
| 35 | section_ir, "recordID", |
| 36 | json_object_new_uint64(firmware_error->RecordId)); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 37 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 38 | //Record GUID. |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 39 | add_guid(section_ir, "recordIDGUID", &firmware_error->RecordIdGuid); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 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); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 66 | } |