14dbe3d72SLawrence Tang /** 24dbe3d72SLawrence Tang * Describes functions for converting PCIe CPER sections from binary and JSON format 34dbe3d72SLawrence Tang * into an intermediate format. 44dbe3d72SLawrence Tang * 54dbe3d72SLawrence Tang * Author: Lawrence.Tang@arm.com 64dbe3d72SLawrence Tang **/ 74dbe3d72SLawrence Tang #include <stdio.h> 83b7f45b5SLawrence Tang #include <string.h> 95202bbb4SLawrence Tang #include <json.h> 10*f8fc7052SJohn Chung #include "libbase64.h" 114dbe3d72SLawrence Tang #include "../edk/Cper.h" 124dbe3d72SLawrence Tang #include "../cper-utils.h" 134dbe3d72SLawrence Tang #include "cper-section-pcie.h" 144dbe3d72SLawrence Tang 154dbe3d72SLawrence Tang //Converts a single PCIe CPER section into JSON IR. 16*f8fc7052SJohn Chung json_object *cper_section_pcie_to_ir(void *section) 174dbe3d72SLawrence Tang { 184dbe3d72SLawrence Tang EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section; 194dbe3d72SLawrence Tang json_object *section_ir = json_object_new_object(); 204dbe3d72SLawrence Tang 214dbe3d72SLawrence Tang //Validation bits. 22e407b4c8SLawrence Tang json_object *validation = bitfield_to_ir( 23e407b4c8SLawrence Tang pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES); 244dbe3d72SLawrence Tang json_object_object_add(section_ir, "validationBits", validation); 254dbe3d72SLawrence Tang 264dbe3d72SLawrence Tang //Port type. 27e407b4c8SLawrence Tang json_object *port_type = integer_to_readable_pair( 28e407b4c8SLawrence Tang pcie_error->PortType, 9, PCIE_ERROR_PORT_TYPES_KEYS, 29e407b4c8SLawrence Tang PCIE_ERROR_PORT_TYPES_VALUES, "Unknown"); 304dbe3d72SLawrence Tang json_object_object_add(section_ir, "portType", port_type); 314dbe3d72SLawrence Tang 324dbe3d72SLawrence Tang //Version, provided each half in BCD. 334dbe3d72SLawrence Tang json_object *version = json_object_new_object(); 34e407b4c8SLawrence Tang json_object_object_add( 35e407b4c8SLawrence Tang version, "minor", 36e407b4c8SLawrence Tang json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF))); 37e407b4c8SLawrence Tang json_object_object_add( 38e407b4c8SLawrence Tang version, "major", 39e407b4c8SLawrence Tang json_object_new_int(bcd_to_int(pcie_error->Version >> 8))); 404dbe3d72SLawrence Tang json_object_object_add(section_ir, "version", version); 414dbe3d72SLawrence Tang 424dbe3d72SLawrence Tang //Command & status. 434dbe3d72SLawrence Tang json_object *command_status = json_object_new_object(); 44e407b4c8SLawrence Tang json_object_object_add( 45e407b4c8SLawrence Tang command_status, "commandRegister", 46e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF)); 47e407b4c8SLawrence Tang json_object_object_add( 48e407b4c8SLawrence Tang command_status, "statusRegister", 49e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->CommandStatus >> 16)); 504dbe3d72SLawrence Tang json_object_object_add(section_ir, "commandStatus", command_status); 514dbe3d72SLawrence Tang 524dbe3d72SLawrence Tang //PCIe Device ID. 534dbe3d72SLawrence Tang json_object *device_id = json_object_new_object(); 54db1b7ce2SLawrence Tang UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) + 554dbe3d72SLawrence Tang (pcie_error->DevBridge.ClassCode[1] << 8) + 56db1b7ce2SLawrence Tang pcie_error->DevBridge.ClassCode[2]; 57e407b4c8SLawrence Tang json_object_object_add( 58e407b4c8SLawrence Tang device_id, "vendorID", 59e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.VendorId)); 60e407b4c8SLawrence Tang json_object_object_add( 61e407b4c8SLawrence Tang device_id, "deviceID", 62e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.DeviceId)); 63e407b4c8SLawrence Tang json_object_object_add(device_id, "classCode", 64e407b4c8SLawrence Tang json_object_new_uint64(class_id)); 65e407b4c8SLawrence Tang json_object_object_add( 66e407b4c8SLawrence Tang device_id, "functionNumber", 67e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.Function)); 68e407b4c8SLawrence Tang json_object_object_add( 69e407b4c8SLawrence Tang device_id, "deviceNumber", 70e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.Device)); 71e407b4c8SLawrence Tang json_object_object_add( 72e407b4c8SLawrence Tang device_id, "segmentNumber", 73e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.Segment)); 74e407b4c8SLawrence Tang json_object_object_add( 75e407b4c8SLawrence Tang device_id, "primaryOrDeviceBusNumber", 76e407b4c8SLawrence Tang json_object_new_uint64( 77e407b4c8SLawrence Tang pcie_error->DevBridge.PrimaryOrDeviceBus)); 78e407b4c8SLawrence Tang json_object_object_add( 79e407b4c8SLawrence Tang device_id, "secondaryBusNumber", 80e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.SecondaryBus)); 81e407b4c8SLawrence Tang json_object_object_add( 82e407b4c8SLawrence Tang device_id, "slotNumber", 83e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->DevBridge.Slot.Number)); 844dbe3d72SLawrence Tang json_object_object_add(section_ir, "deviceID", device_id); 854dbe3d72SLawrence Tang 864dbe3d72SLawrence Tang //Device serial number. 87e407b4c8SLawrence Tang json_object_object_add(section_ir, "deviceSerialNumber", 88e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->SerialNo)); 894dbe3d72SLawrence Tang 904dbe3d72SLawrence Tang //Bridge control status. 914dbe3d72SLawrence Tang json_object *bridge_control_status = json_object_new_object(); 92e407b4c8SLawrence Tang json_object_object_add( 93e407b4c8SLawrence Tang bridge_control_status, "secondaryStatusRegister", 94e407b4c8SLawrence Tang json_object_new_uint64(pcie_error->BridgeControlStatus & 95e407b4c8SLawrence Tang 0xFFFF)); 96e407b4c8SLawrence Tang json_object_object_add( 97e407b4c8SLawrence Tang bridge_control_status, "controlRegister", 984dbe3d72SLawrence Tang json_object_new_uint64(pcie_error->BridgeControlStatus >> 16)); 99e407b4c8SLawrence Tang json_object_object_add(section_ir, "bridgeControlStatus", 100e407b4c8SLawrence Tang bridge_control_status); 1014dbe3d72SLawrence Tang 1024dbe3d72SLawrence Tang //Capability structure. 1036c461e90SLawrence Tang //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure 1046c461e90SLawrence Tang //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem 1056c461e90SLawrence Tang //to be a way to differentiate these, so this is left as a b64 dump. 106*f8fc7052SJohn Chung char *encoded = malloc(2 * 60); 107*f8fc7052SJohn Chung size_t encoded_len = 0; 108*f8fc7052SJohn Chung if (!encoded) { 109*f8fc7052SJohn Chung printf("Failed to allocate encode output buffer. \n"); 110*f8fc7052SJohn Chung } else { 111*f8fc7052SJohn Chung base64_encode((const char *)pcie_error->Capability.PcieCap, 60, 112*f8fc7052SJohn Chung encoded, &encoded_len, 0); 1136c461e90SLawrence Tang json_object *capability = json_object_new_object(); 114e407b4c8SLawrence Tang json_object_object_add(capability, "data", 115*f8fc7052SJohn Chung json_object_new_string_len(encoded, 116*f8fc7052SJohn Chung encoded_len)); 1176c461e90SLawrence Tang free(encoded); 118*f8fc7052SJohn Chung json_object_object_add(section_ir, "capabilityStructure", 119*f8fc7052SJohn Chung capability); 120*f8fc7052SJohn Chung } 1214dbe3d72SLawrence Tang 1224dbe3d72SLawrence Tang //AER information. 1236c461e90SLawrence Tang json_object *aer_capability_ir = json_object_new_object(); 124*f8fc7052SJohn Chung encoded = malloc(2 * 96); 125*f8fc7052SJohn Chung encoded_len = 0; 126*f8fc7052SJohn Chung if (!encoded) { 127*f8fc7052SJohn Chung printf("Failed to allocate encode output buffer. \n"); 128*f8fc7052SJohn Chung } else { 129*f8fc7052SJohn Chung base64_encode((const char *)pcie_error->AerInfo.PcieAer, 96, 130*f8fc7052SJohn Chung encoded, &encoded_len, 0); 131e407b4c8SLawrence Tang json_object_object_add(aer_capability_ir, "data", 132*f8fc7052SJohn Chung json_object_new_string_len(encoded, 133*f8fc7052SJohn Chung encoded_len)); 1346c461e90SLawrence Tang free(encoded); 135*f8fc7052SJohn Chung json_object_object_add(section_ir, "aerInfo", 136*f8fc7052SJohn Chung aer_capability_ir); 137*f8fc7052SJohn Chung } 1384dbe3d72SLawrence Tang return section_ir; 1394dbe3d72SLawrence Tang } 1403b7f45b5SLawrence Tang 1413b7f45b5SLawrence Tang //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream. 1423b7f45b5SLawrence Tang void ir_section_pcie_to_cper(json_object *section, FILE *out) 1433b7f45b5SLawrence Tang { 144e407b4c8SLawrence Tang EFI_PCIE_ERROR_DATA *section_cper = 145e407b4c8SLawrence Tang (EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA)); 1463b7f45b5SLawrence Tang 1473b7f45b5SLawrence Tang //Validation bits. 148e407b4c8SLawrence Tang section_cper->ValidFields = ir_to_bitfield( 149e407b4c8SLawrence Tang json_object_object_get(section, "validationBits"), 8, 150e407b4c8SLawrence Tang PCIE_ERROR_VALID_BITFIELD_NAMES); 1513b7f45b5SLawrence Tang 1523b7f45b5SLawrence Tang //Version. 1533b7f45b5SLawrence Tang json_object *version = json_object_object_get(section, "version"); 154e407b4c8SLawrence Tang UINT32 minor = int_to_bcd( 155e407b4c8SLawrence Tang json_object_get_int(json_object_object_get(version, "minor"))); 156e407b4c8SLawrence Tang UINT32 major = int_to_bcd( 157e407b4c8SLawrence Tang json_object_get_int(json_object_object_get(version, "major"))); 1583ab351feSLawrence Tang section_cper->Version = minor + (major << 8); 1593b7f45b5SLawrence Tang 1603b7f45b5SLawrence Tang //Command/status registers. 161e407b4c8SLawrence Tang json_object *command_status = 162e407b4c8SLawrence Tang json_object_object_get(section, "commandStatus"); 163e407b4c8SLawrence Tang UINT32 command = (UINT16)json_object_get_uint64( 164e407b4c8SLawrence Tang json_object_object_get(command_status, "commandRegister")); 165e407b4c8SLawrence Tang UINT32 status = (UINT16)json_object_get_uint64( 166e407b4c8SLawrence Tang json_object_object_get(command_status, "statusRegister")); 1673b7f45b5SLawrence Tang section_cper->CommandStatus = command + (status << 16); 1683b7f45b5SLawrence Tang 1693b7f45b5SLawrence Tang //Device ID. 1703b7f45b5SLawrence Tang json_object *device_id = json_object_object_get(section, "deviceID"); 171e407b4c8SLawrence Tang UINT64 class_id = json_object_get_uint64( 172e407b4c8SLawrence Tang json_object_object_get(device_id, "classCode")); 173e407b4c8SLawrence Tang section_cper->DevBridge.VendorId = (UINT16)json_object_get_uint64( 174e407b4c8SLawrence Tang json_object_object_get(device_id, "vendorID")); 175e407b4c8SLawrence Tang section_cper->DevBridge.DeviceId = (UINT16)json_object_get_uint64( 176e407b4c8SLawrence Tang json_object_object_get(device_id, "deviceID")); 1773b7f45b5SLawrence Tang section_cper->DevBridge.ClassCode[0] = class_id >> 16; 1783b7f45b5SLawrence Tang section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF; 1793ab351feSLawrence Tang section_cper->DevBridge.ClassCode[2] = class_id & 0xFF; 180e407b4c8SLawrence Tang section_cper->DevBridge.Function = (UINT8)json_object_get_uint64( 181e407b4c8SLawrence Tang json_object_object_get(device_id, "functionNumber")); 182e407b4c8SLawrence Tang section_cper->DevBridge.Device = (UINT8)json_object_get_uint64( 183e407b4c8SLawrence Tang json_object_object_get(device_id, "deviceNumber")); 184e407b4c8SLawrence Tang section_cper->DevBridge.Segment = (UINT16)json_object_get_uint64( 185e407b4c8SLawrence Tang json_object_object_get(device_id, "segmentNumber")); 1863b7f45b5SLawrence Tang section_cper->DevBridge.PrimaryOrDeviceBus = 187e407b4c8SLawrence Tang (UINT8)json_object_get_uint64(json_object_object_get( 188e407b4c8SLawrence Tang device_id, "primaryOrDeviceBusNumber")); 189e407b4c8SLawrence Tang section_cper->DevBridge.SecondaryBus = (UINT8)json_object_get_uint64( 190e407b4c8SLawrence Tang json_object_object_get(device_id, "secondaryBusNumber")); 191e407b4c8SLawrence Tang section_cper->DevBridge.Slot.Number = (UINT16)json_object_get_uint64( 192e407b4c8SLawrence Tang json_object_object_get(device_id, "slotNumber")); 1933b7f45b5SLawrence Tang 1943b7f45b5SLawrence Tang //Bridge/control status. 195e407b4c8SLawrence Tang json_object *bridge_control = 196e407b4c8SLawrence Tang json_object_object_get(section, "bridgeControlStatus"); 197e407b4c8SLawrence Tang UINT32 bridge_status = (UINT16)json_object_get_uint64( 198e407b4c8SLawrence Tang json_object_object_get(bridge_control, 199e407b4c8SLawrence Tang "secondaryStatusRegister")); 200e407b4c8SLawrence Tang UINT32 control_status = (UINT16)json_object_get_uint64( 201e407b4c8SLawrence Tang json_object_object_get(bridge_control, "controlRegister")); 202e407b4c8SLawrence Tang section_cper->BridgeControlStatus = 203e407b4c8SLawrence Tang bridge_status + (control_status << 16); 2043b7f45b5SLawrence Tang 2053b7f45b5SLawrence Tang //Capability structure. 206e407b4c8SLawrence Tang json_object *capability = 207e407b4c8SLawrence Tang json_object_object_get(section, "capabilityStructure"); 2083b7f45b5SLawrence Tang json_object *encoded = json_object_object_get(capability, "data"); 209*f8fc7052SJohn Chung char *decoded = malloc(json_object_get_string_len(encoded)); 210*f8fc7052SJohn Chung size_t decoded_len = 0; 211*f8fc7052SJohn Chung if (!decoded) { 212*f8fc7052SJohn Chung printf("Failed to allocate decode output buffer. \n"); 213*f8fc7052SJohn Chung } else { 214*f8fc7052SJohn Chung base64_decode(json_object_get_string(encoded), 215*f8fc7052SJohn Chung json_object_get_string_len(encoded), decoded, 216*f8fc7052SJohn Chung &decoded_len, 0); 217*f8fc7052SJohn Chung memcpy(section_cper->Capability.PcieCap, decoded, decoded_len); 2183b7f45b5SLawrence Tang free(decoded); 219*f8fc7052SJohn Chung } 2203b7f45b5SLawrence Tang 2213b7f45b5SLawrence Tang //AER capability structure. 2223b7f45b5SLawrence Tang json_object *aer_info = json_object_object_get(section, "aerInfo"); 2233ab351feSLawrence Tang encoded = json_object_object_get(aer_info, "data"); 224*f8fc7052SJohn Chung decoded = malloc(json_object_get_string_len(encoded)); 225*f8fc7052SJohn Chung decoded_len = 0; 226*f8fc7052SJohn Chung if (!decoded) { 227*f8fc7052SJohn Chung printf("Failed to allocate decode output buffer. \n"); 228*f8fc7052SJohn Chung } else { 229*f8fc7052SJohn Chung base64_decode(json_object_get_string(encoded), 230*f8fc7052SJohn Chung json_object_get_string_len(encoded), decoded, 231*f8fc7052SJohn Chung &decoded_len, 0); 232*f8fc7052SJohn Chung memcpy(section_cper->AerInfo.PcieAer, decoded, decoded_len); 2333b7f45b5SLawrence Tang free(decoded); 234*f8fc7052SJohn Chung } 2353b7f45b5SLawrence Tang 2363b7f45b5SLawrence Tang //Miscellaneous value fields. 237e407b4c8SLawrence Tang section_cper->PortType = (UINT32)readable_pair_to_integer( 238e407b4c8SLawrence Tang json_object_object_get(section, "portType")); 239e407b4c8SLawrence Tang section_cper->SerialNo = json_object_get_uint64( 240e407b4c8SLawrence Tang json_object_object_get(section, "deviceSerialNumber")); 2413b7f45b5SLawrence Tang 2423b7f45b5SLawrence Tang //Write out to stream, free resources. 2433ab351feSLawrence Tang fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out); 2443b7f45b5SLawrence Tang fflush(out); 2453b7f45b5SLawrence Tang free(section_cper); 2463b7f45b5SLawrence Tang } 247