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