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