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. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 4 | * |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 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 | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 9 | #include <json.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 10 | #include <libcper/base64.h> |
| 11 | #include <libcper/Cper.h> |
| 12 | #include <libcper/cper-utils.h> |
| 13 | #include <libcper/sections/cper-section-ccix-per.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 14 | #include <libcper/log.h> |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 15 | #include <string.h> |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 16 | |
| 17 | //Converts a single CCIX PER log CPER section into JSON IR. |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 18 | json_object *cper_section_ccix_per_to_ir(const UINT8 *section, UINT32 size, |
| 19 | char **desc_string) |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 20 | { |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 21 | int outstr_len = 0; |
| 22 | *desc_string = malloc(SECTION_DESC_STRING_SIZE); |
| 23 | outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE, |
| 24 | "A CCIX PER Log Error occurred"); |
| 25 | if (outstr_len < 0) { |
| 26 | cper_print_log( |
| 27 | "Error: Could not write to CCIX PER Log description string\n"); |
| 28 | } else if (outstr_len > SECTION_DESC_STRING_SIZE) { |
| 29 | cper_print_log( |
| 30 | "Error: CCIX PER Log description string truncated\n"); |
| 31 | } |
| 32 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 33 | if (size < sizeof(EFI_CCIX_PER_LOG_DATA)) { |
| 34 | return NULL; |
| 35 | } |
| 36 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 37 | EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section; |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 38 | |
| 39 | if (size < ccix_error->Length) { |
| 40 | return NULL; |
| 41 | } |
| 42 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 43 | json_object *section_ir = json_object_new_object(); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 44 | ValidationTypes ui64Type = { UINT_64T, |
| 45 | .value.ui64 = ccix_error->ValidBits }; |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 46 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 47 | //Length (bytes) for the entire structure. |
| 48 | json_object_object_add(section_ir, "length", |
| 49 | json_object_new_uint64(ccix_error->Length)); |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 50 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 51 | //CCIX source/port IDs. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 52 | if (isvalid_prop_to_ir(&ui64Type, 0)) { |
| 53 | json_object_object_add( |
| 54 | section_ir, "ccixSourceID", |
| 55 | json_object_new_int(ccix_error->CcixSourceId)); |
| 56 | } |
| 57 | if (isvalid_prop_to_ir(&ui64Type, 1)) { |
| 58 | json_object_object_add( |
| 59 | section_ir, "ccixPortID", |
| 60 | json_object_new_int(ccix_error->CcixPortId)); |
| 61 | } |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 62 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 63 | //CCIX PER Log. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 64 | if (isvalid_prop_to_ir(&ui64Type, 2)) { |
| 65 | //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0). |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 66 | const UINT8 *cur_pos = (const UINT8 *)(ccix_error + 1); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 67 | int remaining_length = |
| 68 | ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA); |
| 69 | if (remaining_length > 0) { |
| 70 | int32_t encoded_len = 0; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 71 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 72 | char *encoded = base64_encode((UINT8 *)cur_pos, |
| 73 | remaining_length, |
| 74 | &encoded_len); |
| 75 | if (encoded == NULL) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 76 | cper_print_log( |
| 77 | "Failed to allocate encode output buffer. \n"); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 78 | } else { |
| 79 | json_object_object_add( |
| 80 | section_ir, "ccixPERLog", |
| 81 | json_object_new_string_len( |
| 82 | encoded, encoded_len)); |
| 83 | free(encoded); |
| 84 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 85 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | //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] | 92 | void ir_section_ccix_per_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 93 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 94 | EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc( |
| 95 | 1, sizeof(EFI_CCIX_PER_LOG_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 96 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 97 | ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 }; |
| 98 | struct json_object *obj = NULL; |
| 99 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 100 | //Length. |
| 101 | section_cper->Length = json_object_get_uint64( |
| 102 | json_object_object_get(section, "length")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 103 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 104 | //Validation bits. |
| 105 | section_cper->ValidBits = ir_to_bitfield( |
| 106 | json_object_object_get(section, "validationBits"), 3, |
| 107 | CCIX_PER_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 108 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 109 | //CCIX source/port IDs. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 110 | if (json_object_object_get_ex(section, "ccixSourceID", &obj)) { |
| 111 | section_cper->CcixSourceId = (UINT8)json_object_get_int(obj); |
| 112 | add_to_valid_bitfield(&ui64Type, 0); |
| 113 | } |
| 114 | if (json_object_object_get_ex(section, "ccixPortID", &obj)) { |
| 115 | section_cper->CcixPortId = (UINT8)json_object_get_int(obj); |
| 116 | add_to_valid_bitfield(&ui64Type, 1); |
| 117 | } |
| 118 | |
| 119 | bool perlog_exists = false; |
| 120 | if (json_object_object_get_ex(section, "ccixPERLog", &obj)) { |
| 121 | perlog_exists = true; |
| 122 | add_to_valid_bitfield(&ui64Type, 2); |
| 123 | } |
| 124 | section_cper->ValidBits = ui64Type.value.ui64; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 125 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 126 | //Write header out to stream. |
| 127 | fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out); |
| 128 | fflush(out); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 129 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 130 | //Write CCIX PER log itself to stream. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 131 | if (perlog_exists) { |
| 132 | json_object *encoded = obj; |
| 133 | int32_t decoded_len = 0; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 134 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 135 | UINT8 *decoded = base64_decode( |
| 136 | json_object_get_string(encoded), |
| 137 | json_object_get_string_len(encoded), &decoded_len); |
| 138 | if (decoded == NULL) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 139 | cper_print_log( |
| 140 | "Failed to allocate decode output buffer. \n"); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 141 | } else { |
| 142 | fwrite(decoded, decoded_len, 1, out); |
| 143 | fflush(out); |
| 144 | free(decoded); |
| 145 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 146 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 147 | //Free resources. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 148 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 149 | } |