| /** |
| * Describes functions for converting PCIe CPER sections from binary and JSON format |
| * into an intermediate format. |
| * |
| * Author: Lawrence.Tang@arm.com |
| **/ |
| #include <stdio.h> |
| #include <string.h> |
| #include <json.h> |
| #include <libcper/base64.h> |
| #include <libcper/Cper.h> |
| #include <libcper/cper-utils.h> |
| #include <libcper/sections/cper-section-pcie.h> |
| |
| struct aer_info_registers { |
| UINT32 pcie_capability_header; |
| UINT32 uncorrectable_error_status; |
| UINT32 uncorrectable_error_mask; |
| UINT32 uncorrectable_error_severity; |
| UINT32 correctable_error_status; |
| UINT32 correctable_error_mask; |
| UINT32 aer_capabilites_control; |
| UINT32 tlp_header_log[4]; |
| }; |
| |
| //Converts a single PCIe CPER section into JSON IR. |
| json_object *cper_section_pcie_to_ir(void *section) |
| { |
| EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section; |
| json_object *section_ir = json_object_new_object(); |
| |
| //Validation bits. |
| json_object *validation = bitfield_to_ir( |
| pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES); |
| json_object_object_add(section_ir, "validationBits", validation); |
| |
| //Port type. |
| json_object *port_type = integer_to_readable_pair( |
| pcie_error->PortType, 9, PCIE_ERROR_PORT_TYPES_KEYS, |
| PCIE_ERROR_PORT_TYPES_VALUES, "Unknown"); |
| json_object_object_add(section_ir, "portType", port_type); |
| |
| //Version, provided each half in BCD. |
| json_object *version = json_object_new_object(); |
| json_object_object_add( |
| version, "minor", |
| json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF))); |
| json_object_object_add( |
| version, "major", |
| json_object_new_int(bcd_to_int(pcie_error->Version >> 8))); |
| json_object_object_add(section_ir, "version", version); |
| |
| //Command & status. |
| json_object *command_status = json_object_new_object(); |
| json_object_object_add( |
| command_status, "commandRegister", |
| json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF)); |
| json_object_object_add( |
| command_status, "statusRegister", |
| json_object_new_uint64(pcie_error->CommandStatus >> 16)); |
| json_object_object_add(section_ir, "commandStatus", command_status); |
| |
| //PCIe Device ID. |
| json_object *device_id = json_object_new_object(); |
| UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) + |
| (pcie_error->DevBridge.ClassCode[1] << 8) + |
| pcie_error->DevBridge.ClassCode[2]; |
| json_object_object_add( |
| device_id, "vendorID", |
| json_object_new_uint64(pcie_error->DevBridge.VendorId)); |
| json_object_object_add( |
| device_id, "deviceID", |
| json_object_new_uint64(pcie_error->DevBridge.DeviceId)); |
| json_object_object_add(device_id, "classCode", |
| json_object_new_uint64(class_id)); |
| json_object_object_add( |
| device_id, "functionNumber", |
| json_object_new_uint64(pcie_error->DevBridge.Function)); |
| json_object_object_add( |
| device_id, "deviceNumber", |
| json_object_new_uint64(pcie_error->DevBridge.Device)); |
| json_object_object_add( |
| device_id, "segmentNumber", |
| json_object_new_uint64(pcie_error->DevBridge.Segment)); |
| json_object_object_add( |
| device_id, "primaryOrDeviceBusNumber", |
| json_object_new_uint64( |
| pcie_error->DevBridge.PrimaryOrDeviceBus)); |
| json_object_object_add( |
| device_id, "secondaryBusNumber", |
| json_object_new_uint64(pcie_error->DevBridge.SecondaryBus)); |
| json_object_object_add( |
| device_id, "slotNumber", |
| json_object_new_uint64(pcie_error->DevBridge.Slot.Number)); |
| json_object_object_add(section_ir, "deviceID", device_id); |
| |
| //Device serial number. |
| json_object_object_add(section_ir, "deviceSerialNumber", |
| json_object_new_uint64(pcie_error->SerialNo)); |
| |
| //Bridge control status. |
| json_object *bridge_control_status = json_object_new_object(); |
| json_object_object_add( |
| bridge_control_status, "secondaryStatusRegister", |
| json_object_new_uint64(pcie_error->BridgeControlStatus & |
| 0xFFFF)); |
| json_object_object_add( |
| bridge_control_status, "controlRegister", |
| json_object_new_uint64(pcie_error->BridgeControlStatus >> 16)); |
| json_object_object_add(section_ir, "bridgeControlStatus", |
| bridge_control_status); |
| |
| //Capability structure. |
| //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure |
| //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem |
| //to be a way to differentiate these, so this is left as a b64 dump. |
| int32_t encoded_len = 0; |
| |
| char *encoded = base64_encode((UINT8 *)pcie_error->Capability.PcieCap, |
| 60, &encoded_len); |
| if (encoded == NULL) { |
| printf("Failed to allocate encode output buffer. \n"); |
| } else { |
| json_object *capability = json_object_new_object(); |
| json_object_object_add(capability, "data", |
| json_object_new_string_len(encoded, |
| encoded_len)); |
| free(encoded); |
| |
| json_object_object_add(section_ir, "capabilityStructure", |
| capability); |
| } |
| |
| //AER information. |
| json_object *aer_capability_ir = json_object_new_object(); |
| encoded_len = 0; |
| |
| encoded = base64_encode((UINT8 *)pcie_error->AerInfo.PcieAer, 96, |
| &encoded_len); |
| if (encoded == NULL) { |
| printf("Failed to allocate encode output buffer. \n"); |
| } else { |
| json_object_object_add(aer_capability_ir, "data", |
| json_object_new_string_len(encoded, |
| encoded_len)); |
| free(encoded); |
| } |
| |
| struct aer_info_registers *aer_decode; |
| aer_decode = (struct aer_info_registers *)&pcie_error->AerInfo.PcieAer; |
| json_object_object_add( |
| aer_capability_ir, "capability_header", |
| json_object_new_uint64(aer_decode->pcie_capability_header)); |
| json_object_object_add( |
| aer_capability_ir, "uncorrectable_error_status", |
| json_object_new_uint64(aer_decode->uncorrectable_error_status)); |
| json_object_object_add( |
| aer_capability_ir, "uncorrectable_error_mask", |
| json_object_new_uint64(aer_decode->uncorrectable_error_mask)); |
| json_object_object_add( |
| aer_capability_ir, "uncorrectable_error_severity", |
| json_object_new_uint64( |
| aer_decode->uncorrectable_error_severity)); |
| json_object_object_add( |
| aer_capability_ir, "correctable_error_status", |
| json_object_new_uint64(aer_decode->correctable_error_status)); |
| json_object_object_add( |
| aer_capability_ir, "correctable_error_mask", |
| json_object_new_uint64(aer_decode->correctable_error_mask)); |
| json_object_object_add( |
| aer_capability_ir, "capabilites_control", |
| json_object_new_uint64(aer_decode->aer_capabilites_control)); |
| json_object_object_add( |
| aer_capability_ir, "tlp_header_0", |
| json_object_new_uint64(aer_decode->tlp_header_log[0])); |
| json_object_object_add( |
| aer_capability_ir, "tlp_header_1", |
| json_object_new_uint64(aer_decode->tlp_header_log[1])); |
| json_object_object_add( |
| aer_capability_ir, "tlp_header_2", |
| json_object_new_uint64(aer_decode->tlp_header_log[2])); |
| json_object_object_add( |
| aer_capability_ir, "tlp_header_3", |
| json_object_new_uint64(aer_decode->tlp_header_log[3])); |
| json_object_object_add(section_ir, "aerInfo", aer_capability_ir); |
| |
| return section_ir; |
| } |
| |
| //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream. |
| void ir_section_pcie_to_cper(json_object *section, FILE *out) |
| { |
| EFI_PCIE_ERROR_DATA *section_cper = |
| (EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA)); |
| |
| //Validation bits. |
| section_cper->ValidFields = ir_to_bitfield( |
| json_object_object_get(section, "validationBits"), 8, |
| PCIE_ERROR_VALID_BITFIELD_NAMES); |
| |
| //Version. |
| json_object *version = json_object_object_get(section, "version"); |
| UINT32 minor = int_to_bcd( |
| json_object_get_int(json_object_object_get(version, "minor"))); |
| UINT32 major = int_to_bcd( |
| json_object_get_int(json_object_object_get(version, "major"))); |
| section_cper->Version = minor + (major << 8); |
| |
| //Command/status registers. |
| json_object *command_status = |
| json_object_object_get(section, "commandStatus"); |
| UINT32 command = (UINT16)json_object_get_uint64( |
| json_object_object_get(command_status, "commandRegister")); |
| UINT32 status = (UINT16)json_object_get_uint64( |
| json_object_object_get(command_status, "statusRegister")); |
| section_cper->CommandStatus = command + (status << 16); |
| |
| //Device ID. |
| json_object *device_id = json_object_object_get(section, "deviceID"); |
| UINT64 class_id = json_object_get_uint64( |
| json_object_object_get(device_id, "classCode")); |
| section_cper->DevBridge.VendorId = (UINT16)json_object_get_uint64( |
| json_object_object_get(device_id, "vendorID")); |
| section_cper->DevBridge.DeviceId = (UINT16)json_object_get_uint64( |
| json_object_object_get(device_id, "deviceID")); |
| section_cper->DevBridge.ClassCode[0] = class_id >> 16; |
| section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF; |
| section_cper->DevBridge.ClassCode[2] = class_id & 0xFF; |
| section_cper->DevBridge.Function = (UINT8)json_object_get_uint64( |
| json_object_object_get(device_id, "functionNumber")); |
| section_cper->DevBridge.Device = (UINT8)json_object_get_uint64( |
| json_object_object_get(device_id, "deviceNumber")); |
| section_cper->DevBridge.Segment = (UINT16)json_object_get_uint64( |
| json_object_object_get(device_id, "segmentNumber")); |
| section_cper->DevBridge.PrimaryOrDeviceBus = |
| (UINT8)json_object_get_uint64(json_object_object_get( |
| device_id, "primaryOrDeviceBusNumber")); |
| section_cper->DevBridge.SecondaryBus = (UINT8)json_object_get_uint64( |
| json_object_object_get(device_id, "secondaryBusNumber")); |
| section_cper->DevBridge.Slot.Number = (UINT16)json_object_get_uint64( |
| json_object_object_get(device_id, "slotNumber")); |
| |
| //Bridge/control status. |
| json_object *bridge_control = |
| json_object_object_get(section, "bridgeControlStatus"); |
| UINT32 bridge_status = (UINT16)json_object_get_uint64( |
| json_object_object_get(bridge_control, |
| "secondaryStatusRegister")); |
| UINT32 control_status = (UINT16)json_object_get_uint64( |
| json_object_object_get(bridge_control, "controlRegister")); |
| section_cper->BridgeControlStatus = |
| bridge_status + (control_status << 16); |
| |
| //Capability structure. |
| json_object *capability = |
| json_object_object_get(section, "capabilityStructure"); |
| json_object *encoded = json_object_object_get(capability, "data"); |
| |
| int32_t decoded_len = 0; |
| |
| UINT8 *decoded = base64_decode(json_object_get_string(encoded), |
| json_object_get_string_len(encoded), |
| &decoded_len); |
| if (decoded == NULL) { |
| printf("Failed to allocate decode output buffer. \n"); |
| } else { |
| memcpy(section_cper->Capability.PcieCap, decoded, decoded_len); |
| free(decoded); |
| } |
| |
| //AER capability structure. |
| json_object *aer_info = json_object_object_get(section, "aerInfo"); |
| encoded = json_object_object_get(aer_info, "data"); |
| decoded_len = 0; |
| |
| decoded = base64_decode(json_object_get_string(encoded), |
| json_object_get_string_len(encoded), |
| &decoded_len); |
| |
| if (decoded == NULL) { |
| printf("Failed to allocate decode output buffer. \n"); |
| } else { |
| memcpy(section_cper->AerInfo.PcieAer, decoded, decoded_len); |
| free(decoded); |
| } |
| |
| //Miscellaneous value fields. |
| section_cper->PortType = (UINT32)readable_pair_to_integer( |
| json_object_object_get(section, "portType")); |
| section_cper->SerialNo = json_object_get_uint64( |
| json_object_object_get(section, "deviceSerialNumber")); |
| |
| //Write out to stream, free resources. |
| fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out); |
| fflush(out); |
| free(section_cper); |
| } |