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> |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 13 | #include <string.h> |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 14 | |
| 15 | //Converts a single firmware CPER section into JSON IR. |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 16 | json_object *cper_section_firmware_to_ir(const UINT8 *section, UINT32 size, |
| 17 | char **desc_string) |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 18 | { |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 19 | int outstr_len = 0; |
| 20 | *desc_string = malloc(SECTION_DESC_STRING_SIZE); |
| 21 | outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE, |
| 22 | "A Firmware Error occurred"); |
| 23 | if (outstr_len < 0) { |
| 24 | cper_print_log( |
| 25 | "Error: Could not write to Firmware description string\n"); |
| 26 | } else if (outstr_len > SECTION_DESC_STRING_SIZE) { |
| 27 | cper_print_log( |
| 28 | "Error: Firmware description string truncated\n"); |
| 29 | } |
| 30 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 31 | if (size < sizeof(EFI_FIRMWARE_ERROR_DATA)) { |
| 32 | return NULL; |
| 33 | } |
| 34 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 35 | EFI_FIRMWARE_ERROR_DATA *firmware_error = |
| 36 | (EFI_FIRMWARE_ERROR_DATA *)section; |
| 37 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 38 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 39 | //Record type. |
| 40 | json_object *record_type = integer_to_readable_pair( |
| 41 | firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS, |
| 42 | FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)"); |
| 43 | json_object_object_add(section_ir, "errorRecordType", record_type); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 44 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 45 | //Revision, record identifier. |
| 46 | json_object_object_add(section_ir, "revision", |
| 47 | json_object_new_int(firmware_error->Revision)); |
| 48 | json_object_object_add( |
| 49 | section_ir, "recordID", |
| 50 | json_object_new_uint64(firmware_error->RecordId)); |
Lawrence Tang | c60a243 | 2022-07-06 14:58:33 +0100 | [diff] [blame] | 51 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 52 | //Record GUID. |
Ed Tanous | c2ebddd | 2025-03-09 10:07:01 -0700 | [diff] [blame] | 53 | add_guid(section_ir, "recordIDGUID", &firmware_error->RecordIdGuid); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 54 | |
| 55 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | //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] | 59 | void ir_section_firmware_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 60 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 61 | EFI_FIRMWARE_ERROR_DATA *section_cper = |
| 62 | (EFI_FIRMWARE_ERROR_DATA *)calloc( |
| 63 | 1, sizeof(EFI_FIRMWARE_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 64 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | //Record fields. |
| 66 | section_cper->ErrorType = readable_pair_to_integer( |
| 67 | json_object_object_get(section, "errorRecordType")); |
| 68 | section_cper->Revision = json_object_get_int( |
| 69 | json_object_object_get(section, "revision")); |
| 70 | section_cper->RecordId = json_object_get_uint64( |
| 71 | json_object_object_get(section, "recordID")); |
| 72 | string_to_guid(§ion_cper->RecordIdGuid, |
| 73 | json_object_get_string(json_object_object_get( |
| 74 | section, "recordIDGUID"))); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 75 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 76 | //Write to stream, free resources. |
| 77 | fwrite(section_cper, sizeof(EFI_FIRMWARE_ERROR_DATA), 1, out); |
| 78 | fflush(out); |
| 79 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 80 | } |