Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting CXL component error 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 | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 8 | #include <json.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 9 | #include <libcper/base64.h> |
| 10 | #include <libcper/Cper.h> |
| 11 | #include <libcper/cper-utils.h> |
| 12 | #include <libcper/sections/cper-section-cxl-component.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 13 | #include <libcper/log.h> |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 14 | #include <string.h> |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 15 | |
| 16 | //Converts a single CXL component error CPER section into JSON IR. |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 17 | json_object *cper_section_cxl_component_to_ir(const UINT8 *section, UINT32 size, |
| 18 | char **desc_string) |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 19 | { |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 20 | int outstr_len = 0; |
| 21 | *desc_string = malloc(SECTION_DESC_STRING_SIZE); |
| 22 | outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE, |
| 23 | "A CXL Component Error occurred"); |
| 24 | if (outstr_len < 0) { |
| 25 | cper_print_log( |
| 26 | "Error: Could not write to CXL Component description string\n"); |
| 27 | } else if (outstr_len > SECTION_DESC_STRING_SIZE) { |
| 28 | cper_print_log( |
| 29 | "Error: CXL Component description string truncated\n"); |
| 30 | } |
| 31 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 32 | if (size < sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)) { |
| 33 | return NULL; |
| 34 | } |
| 35 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 36 | EFI_CXL_COMPONENT_EVENT_HEADER *cxl_error = |
| 37 | (EFI_CXL_COMPONENT_EVENT_HEADER *)section; |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 38 | if (cxl_error->Length < sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)) { |
| 39 | return NULL; |
| 40 | } |
| 41 | if (size < cxl_error->Length) { |
| 42 | return NULL; |
| 43 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 44 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 45 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 46 | //Length (bytes) for the entire structure. |
| 47 | json_object_object_add(section_ir, "length", |
| 48 | json_object_new_uint64(cxl_error->Length)); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 49 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 50 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 51 | ValidationTypes ui64Type = { UINT_64T, |
| 52 | .value.ui64 = cxl_error->ValidBits }; |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 53 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 54 | //Device ID. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 55 | if (isvalid_prop_to_ir(&ui64Type, 0)) { |
| 56 | json_object *device_id = json_object_new_object(); |
| 57 | json_object_object_add( |
| 58 | device_id, "vendorID", |
| 59 | json_object_new_int(cxl_error->DeviceId.VendorId)); |
| 60 | json_object_object_add( |
| 61 | device_id, "deviceID", |
| 62 | json_object_new_int(cxl_error->DeviceId.DeviceId)); |
| 63 | json_object_object_add( |
| 64 | device_id, "functionNumber", |
| 65 | json_object_new_int( |
| 66 | cxl_error->DeviceId.FunctionNumber)); |
| 67 | json_object_object_add( |
| 68 | device_id, "deviceNumber", |
| 69 | json_object_new_int(cxl_error->DeviceId.DeviceNumber)); |
| 70 | json_object_object_add( |
| 71 | device_id, "busNumber", |
| 72 | json_object_new_int(cxl_error->DeviceId.BusNumber)); |
| 73 | json_object_object_add( |
| 74 | device_id, "segmentNumber", |
| 75 | json_object_new_int(cxl_error->DeviceId.SegmentNumber)); |
| 76 | json_object_object_add( |
| 77 | device_id, "slotNumber", |
| 78 | json_object_new_int(cxl_error->DeviceId.SlotNumber)); |
| 79 | json_object_object_add(section_ir, "deviceID", device_id); |
| 80 | } |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 81 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 82 | //Device serial. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 83 | if (isvalid_prop_to_ir(&ui64Type, 1)) { |
| 84 | json_object_object_add( |
| 85 | section_ir, "deviceSerial", |
| 86 | json_object_new_uint64(cxl_error->DeviceSerial)); |
| 87 | } |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 88 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 89 | //The specification for this is defined within the CXL Specification Section 8.2.9.1. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 90 | if (isvalid_prop_to_ir(&ui64Type, 2)) { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 91 | const UINT8 *cur_pos = (const UINT8 *)(cxl_error + 1); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 92 | int remaining_len = cxl_error->Length - |
| 93 | sizeof(EFI_CXL_COMPONENT_EVENT_HEADER); |
| 94 | if (remaining_len > 0) { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 95 | int32_t encoded_len = 0; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 96 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 97 | char *encoded = base64_encode(cur_pos, remaining_len, |
| 98 | &encoded_len); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 99 | if (encoded == NULL) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 100 | cper_print_log( |
| 101 | "Failed to allocate encode output buffer. \n"); |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 102 | json_object_put(section_ir); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 105 | json_object *event_log = json_object_new_object(); |
| 106 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 107 | json_object_object_add(event_log, "data", |
| 108 | json_object_new_string_len( |
| 109 | encoded, encoded_len)); |
| 110 | |
| 111 | free(encoded); |
| 112 | json_object_object_add( |
| 113 | section_ir, "cxlComponentEventLog", event_log); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 114 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return section_ir; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | //Converts a single given CXL Component CPER-JSON section into CPER binary, outputting to the |
| 121 | //given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 122 | void ir_section_cxl_component_to_cper(json_object *section, FILE *out) |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 123 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 124 | EFI_CXL_COMPONENT_EVENT_HEADER *section_cper = |
| 125 | (EFI_CXL_COMPONENT_EVENT_HEADER *)calloc( |
| 126 | 1, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 127 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 128 | //Length of the structure. |
| 129 | section_cper->Length = json_object_get_uint64( |
| 130 | json_object_object_get(section, "length")); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 131 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 132 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 133 | ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 }; |
| 134 | struct json_object *obj = NULL; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 135 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 136 | //Device ID information. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 137 | if (json_object_object_get_ex(section, "deviceID", &obj)) { |
| 138 | json_object *device_id = obj; |
| 139 | section_cper->DeviceId.VendorId = json_object_get_uint64( |
| 140 | json_object_object_get(device_id, "vendorID")); |
| 141 | section_cper->DeviceId.DeviceId = json_object_get_uint64( |
| 142 | json_object_object_get(device_id, "deviceID")); |
| 143 | section_cper->DeviceId.FunctionNumber = json_object_get_uint64( |
| 144 | json_object_object_get(device_id, "functionNumber")); |
| 145 | section_cper->DeviceId.DeviceNumber = json_object_get_uint64( |
| 146 | json_object_object_get(device_id, "deviceNumber")); |
| 147 | section_cper->DeviceId.BusNumber = json_object_get_uint64( |
| 148 | json_object_object_get(device_id, "busNumber")); |
| 149 | section_cper->DeviceId.SegmentNumber = json_object_get_uint64( |
| 150 | json_object_object_get(device_id, "segmentNumber")); |
| 151 | section_cper->DeviceId.SlotNumber = json_object_get_uint64( |
| 152 | json_object_object_get(device_id, "slotNumber")); |
| 153 | add_to_valid_bitfield(&ui64Type, 0); |
| 154 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 155 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 156 | //Device serial number. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 157 | if (json_object_object_get_ex(section, "deviceSerial", &obj)) { |
| 158 | section_cper->DeviceSerial = json_object_get_uint64(obj); |
| 159 | add_to_valid_bitfield(&ui64Type, 1); |
| 160 | } |
| 161 | |
| 162 | //CXL component event log, decoded from base64. |
| 163 | json_object *event_log = NULL; |
| 164 | if (json_object_object_get_ex(section, "cxlComponentEventLog", &obj)) { |
| 165 | event_log = obj; |
| 166 | add_to_valid_bitfield(&ui64Type, 2); |
| 167 | } |
| 168 | section_cper->ValidBits = ui64Type.value.ui64; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 169 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 170 | //Write header out to stream. |
| 171 | fwrite(section_cper, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER), 1, out); |
| 172 | fflush(out); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 173 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 174 | if (event_log != NULL) { |
| 175 | json_object *encoded = |
| 176 | json_object_object_get(event_log, "data"); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 177 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 178 | int32_t decoded_len = 0; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 179 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 180 | UINT8 *decoded = base64_decode( |
| 181 | json_object_get_string(encoded), |
| 182 | json_object_get_string_len(encoded), &decoded_len); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 183 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 184 | if (decoded == NULL) { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 185 | cper_print_log( |
| 186 | "Failed to allocate decode output buffer. \n"); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 187 | } else { |
| 188 | fwrite(decoded, decoded_len, 1, out); |
| 189 | fflush(out); |
| 190 | free(decoded); |
| 191 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 192 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 193 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 194 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 195 | } |