Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting CCIX PER log 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 | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 8 | #include <string.h> |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 9 | #include "json.h" |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 10 | #include "b64.h" |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 11 | #include "../edk/Cper.h" |
| 12 | #include "../cper-utils.h" |
| 13 | #include "cper-section-ccix-per.h" |
| 14 | |
| 15 | //Converts a single CCIX PER log CPER section into JSON IR. |
| 16 | json_object* cper_section_ccix_per_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) |
| 17 | { |
| 18 | EFI_CCIX_PER_LOG_DATA* ccix_error = (EFI_CCIX_PER_LOG_DATA*)section; |
| 19 | json_object* section_ir = json_object_new_object(); |
| 20 | |
| 21 | //Length (bytes) for the entire structure. |
| 22 | json_object_object_add(section_ir, "length", json_object_new_uint64(ccix_error->Length)); |
| 23 | |
| 24 | //Validation bits. |
| 25 | json_object* validation = bitfield_to_ir(ccix_error->ValidBits, 3, CCIX_PER_ERROR_VALID_BITFIELD_NAMES); |
| 26 | json_object_object_add(section_ir, "validationBits", validation); |
| 27 | |
| 28 | //CCIX source/port IDs. |
| 29 | json_object_object_add(section_ir, "ccixSourceID", json_object_new_int(ccix_error->CcixSourceId)); |
| 30 | json_object_object_add(section_ir, "ccixPortID", json_object_new_int(ccix_error->CcixPortId)); |
| 31 | |
| 32 | //CCIX PER Log. |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 33 | //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0). |
| 34 | unsigned char* cur_pos = (unsigned char*)(ccix_error + 1); |
Lawrence Tang | 0a4b3f2 | 2022-07-21 10:40:10 +0100 | [diff] [blame] | 35 | int remaining_length = ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 36 | if (remaining_length > 0) |
| 37 | { |
| 38 | char* encoded = b64_encode(cur_pos, remaining_length); |
| 39 | json_object_object_add(section_ir, "ccixPERLog", json_object_new_string(encoded)); |
| 40 | free(encoded); |
| 41 | } |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 42 | |
| 43 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | //Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream. |
| 47 | void ir_section_ccix_per_to_cper(json_object* section, FILE* out) |
| 48 | { |
| 49 | EFI_CCIX_PER_LOG_DATA* section_cper = |
| 50 | (EFI_CCIX_PER_LOG_DATA*)calloc(1, sizeof(EFI_CCIX_PER_LOG_DATA)); |
| 51 | |
| 52 | //Length. |
| 53 | section_cper->Length = json_object_get_uint64(json_object_object_get(section, "length")); |
| 54 | |
| 55 | //Validation bits. |
| 56 | section_cper->ValidBits = ir_to_bitfield(json_object_object_get(section, "validationBits"), |
| 57 | 3, CCIX_PER_ERROR_VALID_BITFIELD_NAMES); |
| 58 | |
| 59 | //CCIX source/port IDs. |
| 60 | section_cper->CcixSourceId = (UINT8)json_object_get_int(json_object_object_get(section, "ccixSourceID")); |
| 61 | section_cper->CcixPortId = (UINT8)json_object_get_int(json_object_object_get(section, "ccixPortID")); |
| 62 | |
| 63 | //Write header out to stream. |
Lawrence Tang | 3ab351f | 2022-07-20 16:09:34 +0100 | [diff] [blame] | 64 | fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 65 | fflush(out); |
| 66 | |
| 67 | //Write CCIX PER log itself to stream. |
| 68 | json_object* encoded = json_object_object_get(section, "ccixPERLog"); |
| 69 | UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); |
Lawrence Tang | 0a4b3f2 | 2022-07-21 10:40:10 +0100 | [diff] [blame] | 70 | fwrite(decoded, section_cper->Length - sizeof(EFI_CCIX_PER_LOG_DATA), 1, out); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 71 | fflush(out); |
| 72 | |
| 73 | //Free resources. |
| 74 | free(decoded); |
| 75 | free(section_cper); |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 76 | } |