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