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