Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting CXL protocol 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 | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 8 | #include <string.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-protocol.h> |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 13 | |
| 14 | //Converts a single CXL protocol error CPER section into JSON IR. |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 15 | json_object *cper_section_cxl_protocol_to_ir(const UINT8 *section, UINT32 size) |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 16 | { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 17 | if (size < sizeof(EFI_CXL_PROTOCOL_ERROR_DATA)) { |
| 18 | return NULL; |
| 19 | } |
| 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | EFI_CXL_PROTOCOL_ERROR_DATA *cxl_protocol_error = |
| 22 | (EFI_CXL_PROTOCOL_ERROR_DATA *)section; |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 23 | |
| 24 | if (size < sizeof(EFI_CXL_PROTOCOL_ERROR_DATA) + |
| 25 | cxl_protocol_error->CxlDvsecLength + |
| 26 | cxl_protocol_error->CxlErrorLogLength) { |
| 27 | return NULL; |
| 28 | } |
| 29 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 30 | json_object *section_ir = json_object_new_object(); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 31 | ValidationTypes ui64Type = { |
| 32 | UINT_64T, .value.ui64 = cxl_protocol_error->ValidBits |
| 33 | }; |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 34 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 35 | //Type of detecting agent. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 36 | if (isvalid_prop_to_ir(&ui64Type, 0)) { |
| 37 | json_object *agent_type = integer_to_readable_pair( |
| 38 | cxl_protocol_error->CxlAgentType, 2, |
| 39 | CXL_PROTOCOL_ERROR_AGENT_TYPES_KEYS, |
| 40 | CXL_PROTOCOL_ERROR_AGENT_TYPES_VALUES, |
| 41 | "Unknown (Reserved)"); |
| 42 | json_object_object_add(section_ir, "agentType", agent_type); |
| 43 | } |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 44 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 45 | //CXL agent address, depending on the agent type. |
| 46 | json_object *agent_address = json_object_new_object(); |
| 47 | if (cxl_protocol_error->CxlAgentType == |
| 48 | CXL_PROTOCOL_ERROR_DEVICE_AGENT) { |
| 49 | //Address is a CXL1.1 device agent. |
| 50 | json_object_object_add( |
| 51 | agent_address, "functionNumber", |
| 52 | json_object_new_uint64( |
| 53 | cxl_protocol_error->CxlAgentAddress |
| 54 | .DeviceAddress.FunctionNumber)); |
| 55 | json_object_object_add( |
| 56 | agent_address, "deviceNumber", |
| 57 | json_object_new_uint64( |
| 58 | cxl_protocol_error->CxlAgentAddress |
| 59 | .DeviceAddress.DeviceNumber)); |
| 60 | json_object_object_add( |
| 61 | agent_address, "busNumber", |
| 62 | json_object_new_uint64( |
| 63 | cxl_protocol_error->CxlAgentAddress |
| 64 | .DeviceAddress.BusNumber)); |
| 65 | json_object_object_add( |
| 66 | agent_address, "segmentNumber", |
| 67 | json_object_new_uint64( |
| 68 | cxl_protocol_error->CxlAgentAddress |
| 69 | .DeviceAddress.SegmentNumber)); |
| 70 | } else if (cxl_protocol_error->CxlAgentType == |
| 71 | CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) { |
| 72 | //Address is a CXL port RCRB base address. |
| 73 | json_object_object_add( |
| 74 | agent_address, "value", |
| 75 | json_object_new_uint64( |
| 76 | cxl_protocol_error->CxlAgentAddress |
| 77 | .PortRcrbBaseAddress)); |
| 78 | } |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 79 | if (isvalid_prop_to_ir(&ui64Type, 1)) { |
| 80 | json_object_object_add(section_ir, "cxlAgentAddress", |
| 81 | agent_address); |
| 82 | } else { |
| 83 | json_object_put(agent_address); |
| 84 | } |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 85 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 86 | json_object *device_id = json_object_new_object(); |
| 87 | json_object_object_add( |
| 88 | device_id, "vendorID", |
| 89 | json_object_new_uint64(cxl_protocol_error->DeviceId.VendorId)); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 90 | |
| 91 | //Device ID. |
| 92 | if (isvalid_prop_to_ir(&ui64Type, 2)) { |
| 93 | json_object_object_add( |
| 94 | device_id, "deviceID", |
| 95 | json_object_new_uint64( |
| 96 | cxl_protocol_error->DeviceId.DeviceId)); |
| 97 | json_object_object_add( |
| 98 | device_id, "subsystemVendorID", |
| 99 | json_object_new_uint64( |
| 100 | cxl_protocol_error->DeviceId.SubsystemVendorId)); |
| 101 | json_object_object_add( |
| 102 | device_id, "subsystemDeviceID", |
| 103 | json_object_new_uint64( |
| 104 | cxl_protocol_error->DeviceId.SubsystemDeviceId)); |
| 105 | json_object_object_add( |
| 106 | device_id, "classCode", |
| 107 | json_object_new_uint64( |
| 108 | cxl_protocol_error->DeviceId.ClassCode)); |
| 109 | json_object_object_add( |
| 110 | device_id, "slotNumber", |
| 111 | json_object_new_uint64( |
| 112 | cxl_protocol_error->DeviceId.SlotNumber)); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 113 | } |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 114 | json_object_object_add(section_ir, "deviceID", device_id); |
Lawrence Tang | 368e0b4 | 2022-07-07 14:31:06 +0100 | [diff] [blame] | 115 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 116 | if (isvalid_prop_to_ir(&ui64Type, 3)) { |
| 117 | //Device serial & capability structure (if CXL 1.1 device). |
| 118 | if (cxl_protocol_error->CxlAgentType == |
| 119 | CXL_PROTOCOL_ERROR_DEVICE_AGENT) { |
| 120 | json_object_object_add( |
| 121 | section_ir, "deviceSerial", |
| 122 | json_object_new_uint64( |
| 123 | cxl_protocol_error->DeviceSerial)); |
| 124 | } |
| 125 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 126 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 127 | char *encoded; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 128 | int32_t encoded_len = 0; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 129 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 130 | //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure |
| 131 | //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem |
| 132 | //to be a way to differentiate these, so this is left as a b64 dump. |
| 133 | if (isvalid_prop_to_ir(&ui64Type, 4)) { |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 134 | encoded = base64_encode( |
| 135 | (UINT8 *)cxl_protocol_error->CapabilityStructure.PcieCap, |
| 136 | 60, &encoded_len); |
| 137 | if (encoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 138 | printf("Failed to allocate encode output buffer. \n"); |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 139 | json_object_put(section_ir); |
| 140 | |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 141 | return NULL; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 142 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 143 | json_object_object_add(section_ir, "capabilityStructure", |
| 144 | json_object_new_string_len(encoded, |
| 145 | encoded_len)); |
| 146 | free(encoded); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 147 | } |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 148 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 149 | const UINT8 *cur_pos = (const UINT8 *)(cxl_protocol_error + 1); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 150 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 151 | if (isvalid_prop_to_ir(&ui64Type, 5)) { |
| 152 | //CXL DVSEC & error log length. |
| 153 | json_object_object_add( |
| 154 | section_ir, "dvsecLength", |
| 155 | json_object_new_int( |
| 156 | cxl_protocol_error->CxlDvsecLength)); |
| 157 | //CXL DVSEC |
| 158 | //For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec. |
| 159 | //For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec. |
| 160 | int32_t encoded_len = 0; |
| 161 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 162 | encoded = base64_encode(cur_pos, |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 163 | cxl_protocol_error->CxlDvsecLength, |
| 164 | &encoded_len); |
| 165 | if (encoded == NULL) { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 166 | json_object_put(section_ir); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 167 | return NULL; |
| 168 | } |
| 169 | json_object_object_add(section_ir, "cxlDVSEC", |
| 170 | json_object_new_string_len(encoded, |
| 171 | encoded_len)); |
| 172 | |
| 173 | free(encoded); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 174 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 175 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 176 | cur_pos += cxl_protocol_error->CxlDvsecLength; |
Lawrence Tang | 368e0b4 | 2022-07-07 14:31:06 +0100 | [diff] [blame] | 177 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 178 | if (isvalid_prop_to_ir(&ui64Type, 6)) { |
| 179 | json_object_object_add( |
| 180 | section_ir, "errorLogLength", |
| 181 | json_object_new_int( |
| 182 | cxl_protocol_error->CxlErrorLogLength)); |
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 | //CXL Error Log |
| 185 | //This is the "CXL RAS Capability Structure" as in CXL 1.1 spec. |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 186 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 187 | encoded_len = 0; |
| 188 | encoded = base64_encode((UINT8 *)cur_pos, |
| 189 | cxl_protocol_error->CxlErrorLogLength, |
| 190 | &encoded_len); |
| 191 | |
| 192 | if (encoded == NULL) { |
| 193 | printf("Failed to allocate encode output buffer. \n"); |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame^] | 194 | json_object_put(section_ir); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 195 | return NULL; |
| 196 | } |
| 197 | json_object_object_add(section_ir, "cxlErrorLog", |
| 198 | json_object_new_string_len(encoded, |
| 199 | encoded_len)); |
| 200 | free(encoded); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 201 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 202 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 203 | return section_ir; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | //Converts a single CXL protocol CPER-JSON section into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 207 | void ir_section_cxl_protocol_to_cper(json_object *section, FILE *out) |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 208 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 209 | EFI_CXL_PROTOCOL_ERROR_DATA *section_cper = |
| 210 | (EFI_CXL_PROTOCOL_ERROR_DATA *)calloc( |
| 211 | 1, sizeof(EFI_CXL_PROTOCOL_ERROR_DATA)); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 212 | struct json_object *obj = NULL; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 213 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 214 | //Validation bits. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 215 | ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 }; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 216 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 217 | //Detecting agent type. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 218 | if (json_object_object_get_ex(section, "agentType", &obj)) { |
| 219 | section_cper->CxlAgentType = readable_pair_to_integer(obj); |
| 220 | add_to_valid_bitfield(&ui64Type, 0); |
| 221 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 222 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 223 | //Based on the agent type, set the address. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 224 | if (json_object_object_get_ex(section, "cxlAgentAddress", &obj)) { |
| 225 | json_object *address = obj; |
| 226 | if (section_cper->CxlAgentType == |
| 227 | CXL_PROTOCOL_ERROR_DEVICE_AGENT) { |
| 228 | //Address is split by function, device, bus & segment. |
| 229 | UINT64 function = json_object_get_uint64( |
| 230 | json_object_object_get(address, |
| 231 | "functionNumber")); |
| 232 | UINT64 device = json_object_get_uint64( |
| 233 | json_object_object_get(address, |
| 234 | "deviceNumber")); |
| 235 | UINT64 bus = json_object_get_uint64( |
| 236 | json_object_object_get(address, "busNumber")); |
| 237 | UINT64 segment = json_object_get_uint64( |
| 238 | json_object_object_get(address, |
| 239 | "segmentNumber")); |
| 240 | section_cper->CxlAgentAddress.DeviceAddress |
| 241 | .FunctionNumber = function; |
| 242 | section_cper->CxlAgentAddress.DeviceAddress |
| 243 | .DeviceNumber = device; |
| 244 | section_cper->CxlAgentAddress.DeviceAddress.BusNumber = |
| 245 | bus; |
| 246 | section_cper->CxlAgentAddress.DeviceAddress |
| 247 | .SegmentNumber = segment; |
| 248 | } else if (section_cper->CxlAgentType == |
| 249 | CXL_PROTOCOL_ERROR_HOST_DOWNSTREAM_PORT_AGENT) { |
| 250 | //Plain RCRB base address. |
| 251 | section_cper->CxlAgentAddress.PortRcrbBaseAddress = |
| 252 | json_object_get_uint64(json_object_object_get( |
| 253 | address, "value")); |
| 254 | } |
| 255 | add_to_valid_bitfield(&ui64Type, 1); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 256 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 257 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 258 | //Device ID information. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 259 | if (json_object_object_get_ex(section, "deviceID", &obj)) { |
| 260 | json_object *device_id = obj; |
| 261 | section_cper->DeviceId.VendorId = json_object_get_uint64( |
| 262 | json_object_object_get(device_id, "vendorID")); |
| 263 | section_cper->DeviceId.DeviceId = json_object_get_uint64( |
| 264 | json_object_object_get(device_id, "deviceID")); |
| 265 | section_cper->DeviceId.SubsystemVendorId = |
| 266 | json_object_get_uint64(json_object_object_get( |
| 267 | device_id, "subsystemVendorID")); |
| 268 | section_cper->DeviceId.SubsystemDeviceId = |
| 269 | json_object_get_uint64(json_object_object_get( |
| 270 | device_id, "subsystemDeviceID")); |
| 271 | section_cper->DeviceId.ClassCode = json_object_get_uint64( |
| 272 | json_object_object_get(device_id, "classCode")); |
| 273 | section_cper->DeviceId.SlotNumber = json_object_get_uint64( |
| 274 | json_object_object_get(device_id, "slotNumber")); |
| 275 | add_to_valid_bitfield(&ui64Type, 2); |
| 276 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 277 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 278 | //If CXL 1.1 device, the serial number & PCI capability structure. |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 279 | UINT8 *decoded; |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 280 | if (section_cper->CxlAgentType == CXL_PROTOCOL_ERROR_DEVICE_AGENT) { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 281 | if (json_object_object_get_ex(section, "deviceSerial", &obj)) { |
| 282 | section_cper->DeviceSerial = |
| 283 | json_object_get_uint64(obj); |
| 284 | add_to_valid_bitfield(&ui64Type, 3); |
| 285 | } |
| 286 | if (json_object_object_get_ex(section, "capabilityStructure", |
| 287 | &obj)) { |
| 288 | json_object *encoded = obj; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 289 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 290 | int32_t decoded_len = 0; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 291 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 292 | decoded = base64_decode( |
| 293 | json_object_get_string(encoded), |
| 294 | json_object_get_string_len(encoded), |
| 295 | &decoded_len); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 296 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 297 | if (decoded == NULL) { |
| 298 | printf("Failed to allocate decode output buffer. \n"); |
| 299 | } else { |
| 300 | memcpy(section_cper->CapabilityStructure.PcieCap, |
| 301 | decoded, decoded_len); |
| 302 | free(decoded); |
| 303 | add_to_valid_bitfield(&ui64Type, 4); |
| 304 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 305 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 306 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 307 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 308 | //DVSEC length & error log length. |
| 309 | section_cper->CxlDvsecLength = (UINT16)json_object_get_int( |
| 310 | json_object_object_get(section, "dvsecLength")); |
| 311 | section_cper->CxlErrorLogLength = (UINT16)json_object_get_int( |
| 312 | json_object_object_get(section, "errorLogLength")); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 313 | |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 314 | json_object *encodedsrc = NULL; |
| 315 | json_object *encodederr = NULL; |
| 316 | |
| 317 | //DVSEC out: write valid bits |
| 318 | if (json_object_object_get_ex(section, "cxlDVSEC", &obj)) { |
| 319 | add_to_valid_bitfield(&ui64Type, 5); |
| 320 | encodedsrc = obj; |
| 321 | } |
| 322 | |
| 323 | //Error log: write valid bits |
| 324 | if (json_object_object_get_ex(section, "cxlErrorLog", &obj)) { |
| 325 | add_to_valid_bitfield(&ui64Type, 6); |
| 326 | encodederr = obj; |
| 327 | } |
| 328 | section_cper->ValidBits = ui64Type.value.ui64; |
| 329 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 330 | //Write header to stream. |
| 331 | fwrite(section_cper, sizeof(EFI_CXL_PROTOCOL_ERROR_DATA), 1, out); |
| 332 | fflush(out); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 333 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 334 | //DVSEC out to stream. |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 335 | int32_t decoded_len = 0; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 336 | if (encodedsrc != NULL) { |
| 337 | decoded = base64_decode(json_object_get_string(encodedsrc), |
| 338 | json_object_get_string_len(encodedsrc), |
| 339 | &decoded_len); |
| 340 | if (decoded == NULL) { |
| 341 | printf("Failed to allocate decode output buffer. \n"); |
| 342 | } else { |
| 343 | fwrite(decoded, decoded_len, 1, out); |
| 344 | fflush(out); |
| 345 | free(decoded); |
| 346 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 347 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 348 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 349 | //Error log out to stream. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 350 | decoded_len = 0; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 351 | if (encodederr != NULL) { |
| 352 | decoded = base64_decode(json_object_get_string(encodederr), |
| 353 | json_object_get_string_len(encodederr), |
| 354 | &decoded_len); |
| 355 | if (decoded == NULL) { |
| 356 | printf("Failed to allocate decode output buffer. \n"); |
| 357 | } else { |
| 358 | fwrite(decoded, decoded_len, 1, out); |
| 359 | fflush(out); |
| 360 | free(decoded); |
| 361 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 362 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 363 | |
| 364 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 365 | } |