Add PCIe/memory CPER-JSON parsing.
diff --git a/cper-utils.c b/cper-utils.c
index ba8cd63..28d94d4 100644
--- a/cper-utils.c
+++ b/cper-utils.c
@@ -36,6 +36,19 @@
return error_status_ir;
}
+//Converts the given CPER-JSON generic error status into a CPER structure.
+void ir_generic_error_status_to_cper(json_object* error_status, EFI_GENERIC_ERROR_STATUS* error_status_cper)
+{
+ error_status_cper->Type = readable_pair_to_integer(json_object_object_get(error_status, "errorType"));
+ error_status_cper->AddressSignal = json_object_get_boolean(json_object_object_get(error_status, "addressSignal"));
+ error_status_cper->ControlSignal = json_object_get_boolean(json_object_object_get(error_status, "controlSignal"));
+ error_status_cper->DataSignal = json_object_get_boolean(json_object_object_get(error_status, "dataSignal"));
+ error_status_cper->DetectedByResponder = json_object_get_boolean(json_object_object_get(error_status, "detectedByResponder"));
+ error_status_cper->DetectedByRequester = json_object_get_boolean(json_object_object_get(error_status, "detectedByRequester"));
+ error_status_cper->FirstError = json_object_get_boolean(json_object_object_get(error_status, "firstError"));
+ error_status_cper->OverflowNotLogged = json_object_get_boolean(json_object_object_get(error_status, "overflowDroppedLogs"));
+}
+
//Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order.
json_object* uniform_struct64_to_ir(UINT64* start, int len, const char* names[])
{
@@ -278,4 +291,17 @@
int bcd_to_int(UINT8 bcd)
{
return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F);
+}
+
+//Converts the given integer to a single byte BCD.
+UINT8 int_to_bcd(int value)
+{
+ UINT8 result = 0;
+ int shift = 0;
+ while (value > 0) {
+ result |= (value % 10) << (shift++ << 2);
+ value /= 10;
+ }
+
+ return result;
}
\ No newline at end of file
diff --git a/cper-utils.h b/cper-utils.h
index 1fb17cc..6b34b1a 100644
--- a/cper-utils.h
+++ b/cper-utils.h
@@ -5,6 +5,7 @@
#define TIMESTAMP_LENGTH 24
json_object* cper_generic_error_status_to_ir(EFI_GENERIC_ERROR_STATUS* error_status);
+void ir_generic_error_status_to_cper(json_object* error_status, EFI_GENERIC_ERROR_STATUS* error_status_cper);
json_object* uniform_struct_to_ir(UINT32* start, int len, const char* names[]);
json_object* uniform_struct64_to_ir(UINT64* start, int len, const char* names[]);
void ir_to_uniform_struct(json_object* ir, UINT32* start, int len, const char* names[]);
@@ -23,6 +24,7 @@
void string_to_guid(EFI_GUID* out, const char* guid);
int guid_equal(EFI_GUID* a, EFI_GUID* b);
int bcd_to_int(UINT8 bcd);
+UINT8 int_to_bcd(int value);
//The available severity types for CPER.
extern const char* CPER_SEVERITY_TYPES[4];
diff --git a/ir-parse.c b/ir-parse.c
index 80b5986..3f4f250 100644
--- a/ir-parse.c
+++ b/ir-parse.c
@@ -71,12 +71,12 @@
// ir_section_ipf_to_cper(section, out);
else if (guid_equal(&descriptors[i]->SectionType, &gEfiArmProcessorErrorSectionGuid))
ir_section_arm_to_cper(section, out);
- // else if (guid_equal(&descriptors[i]->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
- // ir_section_platform_memory_to_cper(section, out);
- // else if (guid_equal(&descriptors[i]->SectionType, &gEfiPlatformMemoryError2SectionGuid))
- // ir_section_platform_memory2_to_cper(section, out);
- // else if (guid_equal(&descriptors[i]->SectionType, &gEfiPcieErrorSectionGuid))
- // ir_section_pcie_to_cper(section, out);
+ else if (guid_equal(&descriptors[i]->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
+ ir_section_memory_to_cper(section, out);
+ else if (guid_equal(&descriptors[i]->SectionType, &gEfiPlatformMemoryError2SectionGuid))
+ ir_section_memory2_to_cper(section, out);
+ else if (guid_equal(&descriptors[i]->SectionType, &gEfiPcieErrorSectionGuid))
+ ir_section_pcie_to_cper(section, out);
// else if (guid_equal(&descriptors[i]->SectionType, &gEfiFirmwareErrorSectionGuid))
// ir_section_firmware_to_cper(section, out);
// else if (guid_equal(&descriptors[i]->SectionType, &gEfiPciBusErrorSectionGuid))
diff --git a/sections/cper-section-memory.c b/sections/cper-section-memory.c
index 296425c..089a7bb 100644
--- a/sections/cper-section-memory.c
+++ b/sections/cper-section-memory.c
@@ -134,4 +134,117 @@
json_object_object_add(section_ir, "moduleSmbiosHandle", json_object_new_uint64(memory_error->ModuleHandle));
return section_ir;
+}
+
+//Converts a single Memory Error IR section into CPER binary, outputting to the provided stream.
+void ir_section_memory_to_cper(json_object* section, FILE* out)
+{
+ EFI_PLATFORM_MEMORY_ERROR_DATA* section_cper =
+ (EFI_PLATFORM_MEMORY_ERROR_DATA*)calloc(1, sizeof(EFI_PLATFORM_MEMORY_ERROR_DATA));
+
+ //Validation bits.
+ section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"),
+ 22, MEMORY_ERROR_VALID_BITFIELD_NAMES);
+
+ //Error status.
+ ir_generic_error_status_to_cper(json_object_object_get(section, "errorStatus"), §ion_cper->ErrorStatus);
+
+ //Bank.
+ json_object* bank = json_object_object_get(section, "bank");
+ if ((section_cper->ValidFields >> 5) & 0x1)
+ {
+ //Bank just uses simple address.
+ section_cper->Bank = (UINT16)json_object_get_uint64(json_object_object_get(bank, "value"));
+ }
+ else
+ {
+ //Bank uses address/group style address.
+ UINT16 address = (UINT8)json_object_get_uint64(json_object_object_get(bank, "address"));
+ UINT16 group = (UINT8)json_object_get_uint64(json_object_object_get(bank, "group"));
+ section_cper->Bank = address + (group << 8);
+ }
+
+ //"Extended" field.
+ json_object* extended = json_object_object_get(section, "extended");
+ section_cper->Extended = 0;
+ section_cper->Extended |= json_object_get_boolean(json_object_object_get(extended, "rowBit16"));
+ section_cper->Extended |= json_object_get_boolean(json_object_object_get(extended, "rowBit17")) << 1;
+ section_cper->Extended |= json_object_get_int(json_object_object_get(extended, "chipIdentification")) << 5;
+
+ //Miscellaneous value fields.
+ section_cper->ErrorType = readable_pair_to_integer(json_object_object_get(section, "memoryErrorType"));
+ section_cper->PhysicalAddress = json_object_get_uint64(json_object_object_get(section, "physicalAddress"));
+ section_cper->PhysicalAddressMask = json_object_get_uint64(json_object_object_get(section, "physicalAddressMask"));
+ section_cper->Node = (UINT16)json_object_get_uint64(json_object_object_get(section, "node"));
+ section_cper->Card = (UINT16)json_object_get_uint64(json_object_object_get(section, "card"));
+ section_cper->ModuleRank = (UINT16)json_object_get_uint64(json_object_object_get(section, "moduleRank"));
+ section_cper->Device = (UINT16)json_object_get_uint64(json_object_object_get(section, "device"));
+ section_cper->Row = (UINT16)json_object_get_uint64(json_object_object_get(section, "row"));
+ section_cper->Column = (UINT16)json_object_get_uint64(json_object_object_get(section, "column"));
+ section_cper->BitPosition = (UINT16)json_object_get_uint64(json_object_object_get(section, "bitPosition"));
+ section_cper->RequestorId = json_object_get_uint64(json_object_object_get(section, "requestorID"));
+ section_cper->ResponderId = json_object_get_uint64(json_object_object_get(section, "responderID"));
+ section_cper->TargetId = json_object_get_uint64(json_object_object_get(section, "targetID"));
+ section_cper->RankNum = (UINT16)json_object_get_uint64(json_object_object_get(section, "rankNumber"));
+ section_cper->CardHandle = (UINT16)json_object_get_uint64(json_object_object_get(section, "cardSmbiosHandle"));
+ section_cper->ModuleHandle = (UINT16)json_object_get_uint64(json_object_object_get(section, "moduleSmbiosHandle"));
+
+ //Write to stream, free up resources.
+ fwrite(§ion_cper, sizeof(section_cper), 1, out);
+ fflush(out);
+ free(section_cper);
+}
+
+//Converts a single Memory Error 2 IR section into CPER binary, outputting to the provided stream.
+void ir_section_memory2_to_cper(json_object* section, FILE* out)
+{
+ EFI_PLATFORM_MEMORY2_ERROR_DATA* section_cper =
+ (EFI_PLATFORM_MEMORY2_ERROR_DATA*)calloc(1, sizeof(EFI_PLATFORM_MEMORY2_ERROR_DATA));
+
+ //Validation bits.
+ section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"),
+ 22, MEMORY_ERROR_2_VALID_BITFIELD_NAMES);
+
+ //Error status.
+ ir_generic_error_status_to_cper(json_object_object_get(section, "errorStatus"), §ion_cper->ErrorStatus);
+
+ //Bank.
+ json_object* bank = json_object_object_get(section, "bank");
+ if ((section_cper->ValidFields >> 5) & 0x1)
+ {
+ //Bank just uses simple address.
+ section_cper->Bank = (UINT16)json_object_get_uint64(json_object_object_get(bank, "value"));
+ }
+ else
+ {
+ //Bank uses address/group style address.
+ UINT16 address = (UINT8)json_object_get_uint64(json_object_object_get(bank, "address"));
+ UINT16 group = (UINT8)json_object_get_uint64(json_object_object_get(bank, "group"));
+ section_cper->Bank = address + (group << 8);
+ }
+
+ //Miscellaneous value fields.
+ section_cper->MemErrorType = readable_pair_to_integer(json_object_object_get(section, "memoryErrorType"));
+ section_cper->Status = (UINT8)readable_pair_to_integer(json_object_object_get(section, "status"));
+ section_cper->PhysicalAddress = json_object_get_uint64(json_object_object_get(section, "physicalAddress"));
+ section_cper->PhysicalAddressMask = json_object_get_uint64(json_object_object_get(section, "physicalAddressMask"));
+ section_cper->Node = (UINT16)json_object_get_uint64(json_object_object_get(section, "node"));
+ section_cper->Card = (UINT16)json_object_get_uint64(json_object_object_get(section, "card"));
+ section_cper->Module = (UINT32)json_object_get_uint64(json_object_object_get(section, "module"));
+ section_cper->Device = (UINT32)json_object_get_uint64(json_object_object_get(section, "device"));
+ section_cper->Row = (UINT32)json_object_get_uint64(json_object_object_get(section, "row"));
+ section_cper->Column = (UINT32)json_object_get_uint64(json_object_object_get(section, "column"));
+ section_cper->Rank = (UINT32)json_object_get_uint64(json_object_object_get(section, "rank"));
+ section_cper->BitPosition = (UINT32)json_object_get_uint64(json_object_object_get(section, "bitPosition"));
+ section_cper->ChipId = (UINT8)json_object_get_uint64(json_object_object_get(section, "chipID"));
+ section_cper->RequestorId = json_object_get_uint64(json_object_object_get(section, "requestorID"));
+ section_cper->ResponderId = json_object_get_uint64(json_object_object_get(section, "responderID"));
+ section_cper->TargetId = json_object_get_uint64(json_object_object_get(section, "targetID"));
+ section_cper->CardHandle = (UINT32)json_object_get_uint64(json_object_object_get(section, "cardSmbiosHandle"));
+ section_cper->ModuleHandle = (UINT32)json_object_get_uint64(json_object_object_get(section, "moduleSmbiosHandle"));
+
+ //Write to stream, free up resources.
+ fwrite(§ion_cper, sizeof(section_cper), 1, out);
+ fflush(out);
+ free(section_cper);
}
\ No newline at end of file
diff --git a/sections/cper-section-memory.h b/sections/cper-section-memory.h
index 541619c..e0d470c 100644
--- a/sections/cper-section-memory.h
+++ b/sections/cper-section-memory.h
@@ -23,5 +23,8 @@
json_object* cper_section_platform_memory_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
json_object* cper_section_platform_memory2_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
+void ir_section_memory_to_cper(json_object* section, FILE* out);
+void ir_section_memory2_to_cper(json_object* section, FILE* out);
+
#endif
\ No newline at end of file
diff --git a/sections/cper-section-pcie.c b/sections/cper-section-pcie.c
index 3e3e502..8c70734 100644
--- a/sections/cper-section-pcie.c
+++ b/sections/cper-section-pcie.c
@@ -5,6 +5,7 @@
* Author: Lawrence.Tang@arm.com
**/
#include <stdio.h>
+#include <string.h>
#include "json.h"
#include "b64.h"
#include "../edk/Cper.h"
@@ -113,4 +114,109 @@
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");
+ int minor = json_object_get_int(json_object_object_get(version, "minor"));
+ int major = json_object_get_int(json_object_object_get(version, "major"));
+ section_cper->Version = int_to_bcd(minor) + ((UINT16)(int_to_bcd(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[1] = 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");
+ UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
+ memcpy(section_cper->Capability.PcieCap, decoded, 60);
+ free(decoded);
+
+ //AER capability structure.
+ json_object* aer_info = json_object_object_get(section, "aerInfo");
+ EFI_PCIE_ADV_ERROR_EXT_CAPABILITY* aer_capability =
+ (EFI_PCIE_ADV_ERROR_EXT_CAPABILITY*)section_cper->AerInfo.PcieAer;
+ aer_capability->Header.PcieExtendedCapabilityId =
+ json_object_get_uint64(json_object_object_get(aer_info, "capabilityID"));
+ aer_capability->Header.CapabilityVersion =
+ json_object_get_uint64(json_object_object_get(aer_info, "capabilityVersion"));
+ aer_capability->UncorrectableErrorStatusReg =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "uncorrectableErrorStatusRegister"));
+ aer_capability->UncorrectableErrorMaskReg =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "uncorrectableErrorMaskRegister"));
+ aer_capability->UncorrectableErrorSeverityReg =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "uncorrectableErrorSeverityRegister"));
+ aer_capability->CorrectableErrorStatusReg =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "correctableErrorStatusRegister"));
+ aer_capability->CorrectableErrorMaskReg =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "correctableErrorMaskRegister"));
+ aer_capability->AeccReg =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "aeccReg"));
+
+ //AER header log register.
+ encoded = json_object_object_get(aer_info, "headerLogRegister");
+ decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
+ memcpy(aer_capability->HeaderLogReg, decoded, 16);
+ free(decoded);
+
+ //Remaining AER fields.
+ aer_capability->RootErrorCommand =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "rootErrorCommand"));
+ aer_capability->RootErrorStatus =
+ (UINT32)json_object_get_uint64(json_object_object_get(aer_info, "rootErrorStatus"));
+ aer_capability->ErrorSourceIdReg =
+ (UINT16)json_object_get_uint64(json_object_object_get(aer_info, "errorSourceIDRegister"));
+ aer_capability->CorrectableSourceIdReg =
+ (UINT16)json_object_get_uint64(json_object_object_get(aer_info, "correctableErrorSourceIDRegister"));
+
+
+ //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(§ion_cper, sizeof(section_cper), 1, out);
+ fflush(out);
+ free(section_cper);
}
\ No newline at end of file
diff --git a/sections/cper-section-pcie.h b/sections/cper-section-pcie.h
index 987fa6c..c264960 100644
--- a/sections/cper-section-pcie.h
+++ b/sections/cper-section-pcie.h
@@ -34,5 +34,6 @@
} EFI_PCIE_ADV_ERROR_EXT_CAPABILITY;
json_object* cper_section_pcie_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
+void ir_section_pcie_to_cper(json_object* section, FILE* out);
#endif
\ No newline at end of file