Add initial version with header/secdesc parsing.
diff --git a/cper-parse.c b/cper-parse.c
new file mode 100644
index 0000000..86fa80d
--- /dev/null
+++ b/cper-parse.c
@@ -0,0 +1,349 @@
+/**
+ * Describes functions for parsing CPER headers and section descriptions
+ * into an intermediate JSON format.
+ *
+ * Author: Lawrence.Tang@arm.com
+ **/
+
+#include <stdio.h>
+#include "json.h"
+#include "edk/Cper.h"
+#include "cper-parse.h"
+#include "cper-utils.h"
+#include "sections/cper-section-generic.h"
+
+//Private pre-definitions.
+json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header);
+json_object* cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR* section_descriptor);
+json_object* cper_section_to_ir(FILE* handle, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
+
+//Reads a CPER log file at the given file location, and returns an intermediate
+//JSON representation of this CPER record.
+json_object* cper_to_ir(const char* filename)
+{
+ //Get a handle for the log file.
+ FILE* cper_file = fopen(filename, "r");
+ if (cper_file == NULL) {
+ printf("Could not open CPER record, file handle returned null.");
+ return NULL;
+ }
+
+ //Ensure this is really a CPER log.
+ EFI_COMMON_ERROR_RECORD_HEADER header;
+ fseek(cper_file, 0, SEEK_SET);
+ if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, cper_file) != 1)
+ {
+ printf("Invalid CPER file: Invalid length (log too short).");
+ return NULL;
+ }
+
+ //Check if the header contains the magic bytes ("CPER").
+ if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
+ printf("Invalid CPER file: Invalid header (incorrect signature).");
+ return NULL;
+ }
+
+ // //Print struct contents (debug).
+ // fpos_t file_pos;
+ // fgetpos(cper_file, &file_pos);
+ // printf("Stream is at position %d.\n", file_pos);
+ // printf("SignatureStart: %s\n", (char*)&header.SignatureStart);
+ // printf("Revision: %u\n", header.Revision);
+ // printf("SectionCount: %u\n", header.SectionCount);
+ // printf("Severity: %d\n", header.ErrorSeverity);
+ // printf("RecordLength: %d\n", header.RecordLength);
+
+ //Create the header JSON object from the read bytes.
+ json_object* header_ir = cper_header_to_ir(&header);
+
+ //Read the appropriate number of section descriptors & sections, and convert them into IR format.
+ json_object* section_descriptors_ir = json_object_new_array();
+ json_object* sections_ir = json_object_new_array();
+ for (int i=0; i<header.SectionCount; i++)
+ {
+ //Create the section descriptor.
+ EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
+ if (fread(§ion_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, cper_file) != 1)
+ {
+ printf("Invalid number of section headers: Header states %d sections, could not read section %d.", header.SectionCount, i+1);
+ return NULL;
+ }
+ json_object_array_add(section_descriptors_ir, cper_section_descriptor_to_ir(§ion_descriptor));
+
+ //Read the section itself.
+ json_object_array_add(sections_ir, cper_section_to_ir(cper_file, §ion_descriptor));
+ }
+
+ //Add the header, section descriptors, and sections to a parent object.
+ json_object* parent = json_object_new_object();
+ json_object_object_add(parent, "header", header_ir);
+ json_object_object_add(parent, "sectionDescriptors", section_descriptors_ir);
+
+ //...
+ return parent;
+}
+
+//Converts a parsed CPER record header into intermediate JSON object format.
+json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header)
+{
+ json_object* header_ir = json_object_new_object();
+
+ //Revision/version information.
+ json_object_object_add(header_ir, "revision", revision_to_ir(header->Revision));
+
+ //Section count.
+ json_object_object_add(header_ir, "sectionCount", json_object_new_int(header->SectionCount));
+
+ //Error severity (with interpreted string version).
+ json_object* error_severity = json_object_new_object();
+ json_object_object_add(error_severity, "code", json_object_new_int(header->ErrorSeverity));
+ json_object_object_add(error_severity, "name", json_object_new_string(severity_to_string(header->ErrorSeverity)));
+ json_object_object_add(header_ir, "severity", error_severity);
+
+ //The validation bits for each section.
+ json_object* validation_bits = json_object_new_object();
+ json_object_object_add(validation_bits, "platformID", json_object_new_boolean(header->ValidationBits & 0b1));
+ json_object_object_add(validation_bits, "timestamp", json_object_new_boolean(header->ValidationBits & 0b10 >> 1));
+ json_object_object_add(validation_bits, "partitionID", json_object_new_boolean(header->ValidationBits & 0b100 >> 2));
+ json_object_object_add(header_ir, "validationBits", validation_bits);
+
+ //Total length of the record (including headers) in bytes.
+ json_object_object_add(header_ir, "recordLength", json_object_new_int(header->RecordLength));
+
+ //If a timestamp exists according to validation bits, then add it.
+ if (header->ValidationBits & 0b10)
+ {
+ char timestamp_string[TIMESTAMP_LENGTH];
+ sprintf(timestamp_string, "%02d%02d-%02d-%02dT%02d:%02d:%02d.000",
+ header->TimeStamp.Century,
+ header->TimeStamp.Year,
+ header->TimeStamp.Month,
+ header->TimeStamp.Day,
+ header->TimeStamp.Hours,
+ header->TimeStamp.Minutes,
+ header->TimeStamp.Seconds);
+
+ json_object_object_add(header_ir, "timestamp", json_object_new_string(timestamp_string));
+ json_object_object_add(header_ir, "timestampIsPrecise", json_object_new_boolean(header->TimeStamp.Flag));
+ }
+
+ //If a platform ID exists according to the validation bits, then add it.
+ if (header->ValidationBits & 0b1)
+ {
+ char platform_string[GUID_STRING_LENGTH];
+ guid_to_string(platform_string, &header->PlatformID);
+ json_object_object_add(header_ir, "platformID", json_object_new_string(platform_string));
+ }
+
+ //If a partition ID exists according to the validation bits, then add it.
+ if (header->ValidationBits & 0b100)
+ {
+ char partition_string[GUID_STRING_LENGTH];
+ guid_to_string(partition_string, &header->PartitionID);
+ json_object_object_add(header_ir, "partitionID", json_object_new_string(partition_string));
+ }
+
+ //Creator ID of the header.
+ char creator_string[GUID_STRING_LENGTH];
+ guid_to_string(creator_string, &header->CreatorID);
+ json_object_object_add(header_ir, "creatorID", json_object_new_string(creator_string));
+
+ //Notification type for the header. Some defined types are available.
+ json_object* notification_type = json_object_new_object();
+ char notification_type_string[GUID_STRING_LENGTH];
+ guid_to_string(notification_type_string, &header->NotificationType);
+ json_object_object_add(notification_type, "guid", json_object_new_string(notification_type_string));
+
+ //Add the human readable notification type if possible.
+ char* notification_type_readable = "Unknown";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCmcGuid))
+ notification_type_readable = "CMC";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCpeGuid))
+ notification_type_readable = "CPE";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeMceGuid))
+ notification_type_readable = "MCE";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypePcieGuid))
+ notification_type_readable = "PCIe";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeInitGuid))
+ notification_type_readable = "INIT";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeNmiGuid))
+ notification_type_readable = "NMI";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeBootGuid))
+ notification_type_readable = "Boot";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeDmarGuid))
+ notification_type_readable = "DMAr";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeSeaGuid))
+ notification_type_readable = "SEA";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeSeiGuid))
+ notification_type_readable = "SEI";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypePeiGuid))
+ notification_type_readable = "PEI";
+ if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCxlGuid))
+ notification_type_readable = "CXL Component";
+ json_object_object_add(notification_type, "type", json_object_new_string(notification_type_readable));
+ json_object_object_add(header_ir, "notificationType", notification_type);
+
+ //The record ID for this record, unique on a given system.
+ json_object_object_add(header_ir, "recordID", json_object_new_uint64(header->RecordID));
+
+ //Flag for the record, and a human readable form.
+ json_object* flags = json_object_new_object();
+ json_object_object_add(flags, "value", json_object_new_int(header->Flags));
+ char* flags_readable = "Unknown";
+ switch (header->Flags)
+ {
+ case 1:
+ flags_readable = "HW_ERROR_FLAGS_RECOVERED";
+ break;
+ case 2:
+ flags_readable = "HW_ERROR_FLAGS_PREVERR";
+ break;
+ case 3:
+ flags_readable = "HW_ERROR_FLAGS_SIMULATED";
+ break;
+
+ }
+ json_object_object_add(flags, "type", json_object_new_string(flags_readable));
+ json_object_object_add(header_ir, "flags", flags);
+
+ //Persistence information. Outside the scope of specification, so just a uint32 here.
+ json_object_object_add(header_ir, "persistenceInformation", json_object_new_uint64(header->PersistenceInfo));
+ return header_ir;
+}
+
+//Converts the given EFI section descriptor into JSON IR format.
+json_object* cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR* section_descriptor)
+{
+ json_object* section_descriptor_ir = json_object_new_object();
+
+ //The offset of the section from the base of the record header, length.
+ json_object_object_add(section_descriptor_ir, "sectionOffset", json_object_new_int(section_descriptor->SectionOffset));
+ json_object_object_add(section_descriptor_ir, "sectionLength", json_object_new_int(section_descriptor->SectionLength));
+
+ //Revision.
+ json_object_object_add(section_descriptor_ir, "revision", revision_to_ir(section_descriptor->Revision));
+
+ //Validation bits.
+ json_object* validation_bits = json_object_new_object();
+ json_object_object_add(validation_bits, "fruID", json_object_new_boolean(section_descriptor->SecValidMask & 0b1));
+ json_object_object_add(validation_bits, "fruString", json_object_new_boolean((section_descriptor->SecValidMask & 0b10) >> 1));
+ json_object_object_add(section_descriptor_ir, "validationBits", validation_bits);
+
+ //Flag bits.
+ json_object* flags = json_object_new_object();
+ json_object_object_add(flags, "primary", json_object_new_boolean(section_descriptor->SectionFlags >> 31));
+ json_object_object_add(flags, "containmentWarning", json_object_new_boolean((section_descriptor->SectionFlags >> 30) & 0b1));
+ json_object_object_add(flags, "reset", json_object_new_boolean((section_descriptor->SectionFlags >> 29) & 0b1));
+ json_object_object_add(flags, "errorThresholdExceeded", json_object_new_boolean((section_descriptor->SectionFlags >> 28) & 0b1));
+ json_object_object_add(flags, "resourceNotAccessible", json_object_new_boolean((section_descriptor->SectionFlags >> 27) & 0b1));
+ json_object_object_add(flags, "latentError", json_object_new_boolean((section_descriptor->SectionFlags >> 26) & 0b1));
+ json_object_object_add(flags, "propagated", json_object_new_boolean((section_descriptor->SectionFlags >> 25) & 0b1));
+ json_object_object_add(flags, "overflow", json_object_new_boolean((section_descriptor->SectionFlags >> 24) & 0b1));
+ json_object_object_add(section_descriptor_ir, "flags", flags);
+
+ //Section type (GUID).
+ json_object* section_type = json_object_new_object();
+ char section_type_string[GUID_STRING_LENGTH];
+ guid_to_string(section_type_string, §ion_descriptor->SectionType);
+ json_object_object_add(section_type, "data", json_object_new_string(section_type_string));
+
+ //Readable section type, if possible.
+ char* section_type_readable = "Unknown";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
+ section_type_readable = "Processor Generic";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
+ section_type_readable = "IA32/X64";
+ //todo: Why does IPF have an overly long GUID?
+ // if (guid_equal(§ion_descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
+ // section_type_readable = "IPF";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
+ section_type_readable = "ARM";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
+ section_type_readable = "Platform Memory";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiPcieErrorSectionGuid))
+ section_type_readable = "PCIe";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
+ section_type_readable = "Firmware Error Record Reference";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
+ section_type_readable = "PCI/PCI-X Bus";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
+ section_type_readable = "PCI Component/Device";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
+ section_type_readable = "DMAr Generic";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
+ section_type_readable = "Intel VT for Directed I/O specific DMAr section";
+ if (guid_equal(§ion_descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
+ section_type_readable = "IOMMU specific DMAr section";
+
+ //todo: How do you determine if this is a CXL component event?
+ // if (guid_equal(§ion_descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
+ // section_type_readable = "CXL Component Event";
+
+ json_object_object_add(section_type, "type", json_object_new_string(section_type_readable));
+ json_object_object_add(section_descriptor_ir, "sectionType", section_type);
+
+ //If validation bits indicate it exists, add FRU ID.
+ if (section_descriptor->SecValidMask & 0b1)
+ {
+ char fru_id_string[GUID_STRING_LENGTH];
+ guid_to_string(fru_id_string, §ion_descriptor->FruId);
+ json_object_object_add(section_descriptor_ir, "fruID", json_object_new_string(fru_id_string));
+ }
+
+ //If validation bits indicate it exists, add FRU text.
+ if ((section_descriptor->SecValidMask & 0b10) >> 1)
+ json_object_object_add(section_descriptor_ir, "fruText", json_object_new_string(section_descriptor->FruString));
+
+ //Section severity.
+ json_object* section_severity = json_object_new_object();
+ json_object_object_add(section_severity, "code", json_object_new_int(section_descriptor->Severity));
+ json_object_object_add(section_severity, "name", json_object_new_string(severity_to_string(section_descriptor->Severity)));
+ json_object_object_add(section_descriptor_ir, "severity", section_severity);
+
+ return section_descriptor_ir;
+}
+
+
+//Converts the section described by a single given section descriptor.
+json_object* cper_section_to_ir(FILE* handle, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
+{
+ //Read section as described by the section descriptor.
+ fseek(handle, descriptor->SectionOffset, SEEK_SET);
+ void* section = malloc(descriptor->SectionLength);
+ if (fread(section, descriptor->SectionLength, 1, handle) != 1)
+ {
+ printf("Section read failed: Could not read %d bytes from global offset %d.",
+ descriptor->SectionLength,
+ descriptor->SectionOffset);
+ }
+
+ json_object* result = NULL;
+ if (guid_equal(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
+ result = cper_section_generic_to_ir(handle, descriptor);
+ // if (guid_equal(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
+ // result = cper_section_ia32x64_to_ir(handle);
+ // //todo: Why does IPF have an overly long GUID?
+ // // if (guid_equal(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
+ // if (guid_equal(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
+ // result = cper_section_arm_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
+ // result = cper_section_platform_memory_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiPcieErrorSectionGuid))
+ // result = cper_section_pcie_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
+ // result = cper_section_firmware_error_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
+ // result = cper_section_pci_bus_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
+ // result = cper_section_pci_dev_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
+ // result = cper_section_dmar_generic_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
+ // result = cper_section_intel_io_dma_to_ir(handle);
+ // if (guid_equal(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
+ // result = cper_section_iommu_dma_to_ir(handle);
+
+ //Free section memory, return result.
+ free(section);
+ return result;
+}
\ No newline at end of file