Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting PCIe CPER sections from binary and JSON format |
| 3 | * into an intermediate format. |
| 4 | * |
| 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 8 | #include <string.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 9 | #include <json.h> |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 10 | #include "libbase64.h" |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 11 | #include "../edk/Cper.h" |
| 12 | #include "../cper-utils.h" |
| 13 | #include "cper-section-pcie.h" |
| 14 | |
| 15 | //Converts a single PCIe CPER section into JSON IR. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 16 | json_object *cper_section_pcie_to_ir(void *section) |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 17 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 18 | EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section; |
| 19 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | //Validation bits. |
| 22 | json_object *validation = bitfield_to_ir( |
| 23 | pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES); |
| 24 | json_object_object_add(section_ir, "validationBits", validation); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 25 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 26 | //Port type. |
| 27 | json_object *port_type = integer_to_readable_pair( |
| 28 | pcie_error->PortType, 9, PCIE_ERROR_PORT_TYPES_KEYS, |
| 29 | PCIE_ERROR_PORT_TYPES_VALUES, "Unknown"); |
| 30 | json_object_object_add(section_ir, "portType", port_type); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 31 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 32 | //Version, provided each half in BCD. |
| 33 | json_object *version = json_object_new_object(); |
| 34 | json_object_object_add( |
| 35 | version, "minor", |
| 36 | json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF))); |
| 37 | json_object_object_add( |
| 38 | version, "major", |
| 39 | json_object_new_int(bcd_to_int(pcie_error->Version >> 8))); |
| 40 | json_object_object_add(section_ir, "version", version); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 41 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 42 | //Command & status. |
| 43 | json_object *command_status = json_object_new_object(); |
| 44 | json_object_object_add( |
| 45 | command_status, "commandRegister", |
| 46 | json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF)); |
| 47 | json_object_object_add( |
| 48 | command_status, "statusRegister", |
| 49 | json_object_new_uint64(pcie_error->CommandStatus >> 16)); |
| 50 | json_object_object_add(section_ir, "commandStatus", command_status); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 51 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 52 | //PCIe Device ID. |
| 53 | json_object *device_id = json_object_new_object(); |
| 54 | UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) + |
| 55 | (pcie_error->DevBridge.ClassCode[1] << 8) + |
| 56 | pcie_error->DevBridge.ClassCode[2]; |
| 57 | json_object_object_add( |
| 58 | device_id, "vendorID", |
| 59 | json_object_new_uint64(pcie_error->DevBridge.VendorId)); |
| 60 | json_object_object_add( |
| 61 | device_id, "deviceID", |
| 62 | json_object_new_uint64(pcie_error->DevBridge.DeviceId)); |
| 63 | json_object_object_add(device_id, "classCode", |
| 64 | json_object_new_uint64(class_id)); |
| 65 | json_object_object_add( |
| 66 | device_id, "functionNumber", |
| 67 | json_object_new_uint64(pcie_error->DevBridge.Function)); |
| 68 | json_object_object_add( |
| 69 | device_id, "deviceNumber", |
| 70 | json_object_new_uint64(pcie_error->DevBridge.Device)); |
| 71 | json_object_object_add( |
| 72 | device_id, "segmentNumber", |
| 73 | json_object_new_uint64(pcie_error->DevBridge.Segment)); |
| 74 | json_object_object_add( |
| 75 | device_id, "primaryOrDeviceBusNumber", |
| 76 | json_object_new_uint64( |
| 77 | pcie_error->DevBridge.PrimaryOrDeviceBus)); |
| 78 | json_object_object_add( |
| 79 | device_id, "secondaryBusNumber", |
| 80 | json_object_new_uint64(pcie_error->DevBridge.SecondaryBus)); |
| 81 | json_object_object_add( |
| 82 | device_id, "slotNumber", |
| 83 | json_object_new_uint64(pcie_error->DevBridge.Slot.Number)); |
| 84 | json_object_object_add(section_ir, "deviceID", device_id); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 85 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 86 | //Device serial number. |
| 87 | json_object_object_add(section_ir, "deviceSerialNumber", |
| 88 | json_object_new_uint64(pcie_error->SerialNo)); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 89 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 90 | //Bridge control status. |
| 91 | json_object *bridge_control_status = json_object_new_object(); |
| 92 | json_object_object_add( |
| 93 | bridge_control_status, "secondaryStatusRegister", |
| 94 | json_object_new_uint64(pcie_error->BridgeControlStatus & |
| 95 | 0xFFFF)); |
| 96 | json_object_object_add( |
| 97 | bridge_control_status, "controlRegister", |
| 98 | json_object_new_uint64(pcie_error->BridgeControlStatus >> 16)); |
| 99 | json_object_object_add(section_ir, "bridgeControlStatus", |
| 100 | bridge_control_status); |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 101 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 102 | //Capability structure. |
| 103 | //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure |
| 104 | //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem |
| 105 | //to be a way to differentiate these, so this is left as a b64 dump. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 106 | char *encoded = malloc(2 * 60); |
| 107 | size_t encoded_len = 0; |
| 108 | if (!encoded) { |
| 109 | printf("Failed to allocate encode output buffer. \n"); |
| 110 | } else { |
| 111 | base64_encode((const char *)pcie_error->Capability.PcieCap, 60, |
| 112 | encoded, &encoded_len, 0); |
| 113 | json_object *capability = json_object_new_object(); |
| 114 | json_object_object_add(capability, "data", |
| 115 | json_object_new_string_len(encoded, |
| 116 | encoded_len)); |
| 117 | free(encoded); |
| 118 | json_object_object_add(section_ir, "capabilityStructure", |
| 119 | capability); |
| 120 | } |
Lawrence Tang | 4dbe3d7 | 2022-07-06 13:51:01 +0100 | [diff] [blame] | 121 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 122 | //AER information. |
| 123 | json_object *aer_capability_ir = json_object_new_object(); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 124 | encoded = malloc(2 * 96); |
| 125 | encoded_len = 0; |
| 126 | if (!encoded) { |
| 127 | printf("Failed to allocate encode output buffer. \n"); |
| 128 | } else { |
| 129 | base64_encode((const char *)pcie_error->AerInfo.PcieAer, 96, |
| 130 | encoded, &encoded_len, 0); |
| 131 | json_object_object_add(aer_capability_ir, "data", |
| 132 | json_object_new_string_len(encoded, |
| 133 | encoded_len)); |
| 134 | free(encoded); |
| 135 | json_object_object_add(section_ir, "aerInfo", |
| 136 | aer_capability_ir); |
| 137 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 138 | return section_ir; |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 142 | void ir_section_pcie_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 143 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 144 | EFI_PCIE_ERROR_DATA *section_cper = |
| 145 | (EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA)); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 146 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 147 | //Validation bits. |
| 148 | section_cper->ValidFields = ir_to_bitfield( |
| 149 | json_object_object_get(section, "validationBits"), 8, |
| 150 | PCIE_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 151 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 152 | //Version. |
| 153 | json_object *version = json_object_object_get(section, "version"); |
| 154 | UINT32 minor = int_to_bcd( |
| 155 | json_object_get_int(json_object_object_get(version, "minor"))); |
| 156 | UINT32 major = int_to_bcd( |
| 157 | json_object_get_int(json_object_object_get(version, "major"))); |
| 158 | section_cper->Version = minor + (major << 8); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 159 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 160 | //Command/status registers. |
| 161 | json_object *command_status = |
| 162 | json_object_object_get(section, "commandStatus"); |
| 163 | UINT32 command = (UINT16)json_object_get_uint64( |
| 164 | json_object_object_get(command_status, "commandRegister")); |
| 165 | UINT32 status = (UINT16)json_object_get_uint64( |
| 166 | json_object_object_get(command_status, "statusRegister")); |
| 167 | section_cper->CommandStatus = command + (status << 16); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 168 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 169 | //Device ID. |
| 170 | json_object *device_id = json_object_object_get(section, "deviceID"); |
| 171 | UINT64 class_id = json_object_get_uint64( |
| 172 | json_object_object_get(device_id, "classCode")); |
| 173 | section_cper->DevBridge.VendorId = (UINT16)json_object_get_uint64( |
| 174 | json_object_object_get(device_id, "vendorID")); |
| 175 | section_cper->DevBridge.DeviceId = (UINT16)json_object_get_uint64( |
| 176 | json_object_object_get(device_id, "deviceID")); |
| 177 | section_cper->DevBridge.ClassCode[0] = class_id >> 16; |
| 178 | section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF; |
| 179 | section_cper->DevBridge.ClassCode[2] = class_id & 0xFF; |
| 180 | section_cper->DevBridge.Function = (UINT8)json_object_get_uint64( |
| 181 | json_object_object_get(device_id, "functionNumber")); |
| 182 | section_cper->DevBridge.Device = (UINT8)json_object_get_uint64( |
| 183 | json_object_object_get(device_id, "deviceNumber")); |
| 184 | section_cper->DevBridge.Segment = (UINT16)json_object_get_uint64( |
| 185 | json_object_object_get(device_id, "segmentNumber")); |
| 186 | section_cper->DevBridge.PrimaryOrDeviceBus = |
| 187 | (UINT8)json_object_get_uint64(json_object_object_get( |
| 188 | device_id, "primaryOrDeviceBusNumber")); |
| 189 | section_cper->DevBridge.SecondaryBus = (UINT8)json_object_get_uint64( |
| 190 | json_object_object_get(device_id, "secondaryBusNumber")); |
| 191 | section_cper->DevBridge.Slot.Number = (UINT16)json_object_get_uint64( |
| 192 | json_object_object_get(device_id, "slotNumber")); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 193 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 194 | //Bridge/control status. |
| 195 | json_object *bridge_control = |
| 196 | json_object_object_get(section, "bridgeControlStatus"); |
| 197 | UINT32 bridge_status = (UINT16)json_object_get_uint64( |
| 198 | json_object_object_get(bridge_control, |
| 199 | "secondaryStatusRegister")); |
| 200 | UINT32 control_status = (UINT16)json_object_get_uint64( |
| 201 | json_object_object_get(bridge_control, "controlRegister")); |
| 202 | section_cper->BridgeControlStatus = |
| 203 | bridge_status + (control_status << 16); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 204 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 205 | //Capability structure. |
| 206 | json_object *capability = |
| 207 | json_object_object_get(section, "capabilityStructure"); |
| 208 | json_object *encoded = json_object_object_get(capability, "data"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 209 | char *decoded = malloc(json_object_get_string_len(encoded)); |
| 210 | size_t decoded_len = 0; |
| 211 | if (!decoded) { |
| 212 | printf("Failed to allocate decode output buffer. \n"); |
| 213 | } else { |
| 214 | base64_decode(json_object_get_string(encoded), |
| 215 | json_object_get_string_len(encoded), decoded, |
| 216 | &decoded_len, 0); |
| 217 | memcpy(section_cper->Capability.PcieCap, decoded, decoded_len); |
| 218 | free(decoded); |
| 219 | } |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 220 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 221 | //AER capability structure. |
| 222 | json_object *aer_info = json_object_object_get(section, "aerInfo"); |
| 223 | encoded = json_object_object_get(aer_info, "data"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 224 | decoded = malloc(json_object_get_string_len(encoded)); |
| 225 | decoded_len = 0; |
| 226 | if (!decoded) { |
| 227 | printf("Failed to allocate decode output buffer. \n"); |
| 228 | } else { |
| 229 | base64_decode(json_object_get_string(encoded), |
| 230 | json_object_get_string_len(encoded), decoded, |
| 231 | &decoded_len, 0); |
| 232 | memcpy(section_cper->AerInfo.PcieAer, decoded, decoded_len); |
| 233 | free(decoded); |
| 234 | } |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 235 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 236 | //Miscellaneous value fields. |
| 237 | section_cper->PortType = (UINT32)readable_pair_to_integer( |
| 238 | json_object_object_get(section, "portType")); |
| 239 | section_cper->SerialNo = json_object_get_uint64( |
| 240 | json_object_object_get(section, "deviceSerialNumber")); |
Lawrence Tang | 3b7f45b | 2022-07-14 14:14:30 +0100 | [diff] [blame] | 241 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 242 | //Write out to stream, free resources. |
| 243 | fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out); |
| 244 | fflush(out); |
| 245 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 246 | } |