Add section descriptor parsing, generic processor error.
diff --git a/cper-parse.c b/cper-parse.c
index 39faff6..ee670e1 100644
--- a/cper-parse.c
+++ b/cper-parse.c
@@ -207,9 +207,7 @@
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, "fruIDValid", json_object_new_boolean(section_descriptor->SecValidMask & 0b1));
- json_object_object_add(validation_bits, "fruStringValid", json_object_new_boolean((section_descriptor->SecValidMask & 0b10) >> 1));
+ json_object* validation_bits = bitfield_to_ir(section_descriptor->SecValidMask, 2, CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
json_object_object_add(section_descriptor_ir, "validationBits", validation_bits);
//Flag bits.
diff --git a/cper-parse.h b/cper-parse.h
index 77c5ea9..439f731 100644
--- a/cper-parse.h
+++ b/cper-parse.h
@@ -3,6 +3,7 @@
#include "json.h"
#define CPER_HEADER_VALID_BITFIELD_NAMES (const char*[]) {"platformIDValid", "timestampValid", "partitionIDValid"}
+#define CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES (const char*[]) {"fruIDValid", "fruStringValid"}
#define CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES (const char*[]) \
{"primary", "containmentWarning", "reset", "errorThresholdExceeded", "resourceNotAccessible", "latentError", \
"propagated", "overflow"}
diff --git a/cper-utils.c b/cper-utils.c
index 454fc5b..db41381 100644
--- a/cper-utils.c
+++ b/cper-utils.c
@@ -179,7 +179,11 @@
//Converts a single timestamp string to an EFI timestamp.
void string_to_timestamp(EFI_ERROR_TIME_STAMP* out, const char* timestamp)
{
- sscanf(timestamp, "%02d%02d-%02d-%02dT%02d:%02d:%02d.000",
+ //Ignore invalid timestamps.
+ if (timestamp == NULL)
+ return;
+
+ sscanf(timestamp, "%02hhd%02hhd-%02hhd-%02hhdT%02hhd:%02hhd:%02hhd.000",
&out->Century,
&out->Year,
&out->Month,
@@ -209,7 +213,11 @@
//Helper function to convert a string into an EDK EFI GUID.
void string_to_guid(EFI_GUID* out, const char* guid)
{
- sscanf(guid, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
+ //Ignore invalid GUIDs.
+ if (guid == NULL)
+ return;
+
+ sscanf(guid, "%08x-%04hx-%04hx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
&out->Data1,
&out->Data2,
&out->Data3,
diff --git a/ir-parse.c b/ir-parse.c
index 8eb75db..5eaa1ac 100644
--- a/ir-parse.c
+++ b/ir-parse.c
@@ -5,36 +5,123 @@
**/
#include <stdio.h>
+#include <string.h>
#include "json.h"
+#include "b64.h"
#include "edk/Cper.h"
#include "cper-parse.h"
#include "cper-utils.h"
+#include "sections/cper-section-generic.h"
+#include "sections/cper-section-ia32x64.h"
+#include "sections/cper-section-arm.h"
+#include "sections/cper-section-memory.h"
+#include "sections/cper-section-pcie.h"
+#include "sections/cper-section-pci-bus.h"
+#include "sections/cper-section-pci-dev.h"
+#include "sections/cper-section-firmware.h"
+#include "sections/cper-section-dmar-generic.h"
+#include "sections/cper-section-dmar-vtd.h"
+#include "sections/cper-section-dmar-iommu.h"
+#include "sections/cper-section-ccix-per.h"
+#include "sections/cper-section-cxl-protocol.h"
+#include "sections/cper-section-ipf.h"
+#include "sections/cper-section-cxl-component.h"
//Private pre-declarations.
void ir_header_to_cper(json_object* header_ir, EFI_COMMON_ERROR_RECORD_HEADER* header);
+void ir_section_descriptor_to_cper(json_object* section_descriptor_ir, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
//Converts the given JSON IR CPER representation into CPER binary format, piped to the provided file stream.
-//This function performs no validation of the IR against the CPER-JSON specification. For this, call
-//validate_schema() from json-schema.h before attempting to call this function.
+//This function performs no validation of the IR against the CPER-JSON specification. To ensure a safe call,
+//use validate_schema() from json-schema.h before attempting to call this function.
void ir_to_cper(json_object* ir, FILE* out)
{
//Create the CPER header.
EFI_COMMON_ERROR_RECORD_HEADER* header = (EFI_COMMON_ERROR_RECORD_HEADER*)calloc(1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
ir_header_to_cper(json_object_object_get(ir, "header"), header);
fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
-
- //...
-
- //Free all resources.
fflush(out);
+
+ //Create the CPER section descriptors.
+ json_object* section_descriptors = json_object_object_get(ir, "sectionDescriptors");
+ int amt_descriptors = json_object_array_length(section_descriptors);
+ EFI_ERROR_SECTION_DESCRIPTOR* descriptors[amt_descriptors];
+ for (int i=0; i<amt_descriptors; i++)
+ {
+ descriptors[i] = (EFI_ERROR_SECTION_DESCRIPTOR*)calloc(1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
+ ir_section_descriptor_to_cper(json_object_array_get_idx(section_descriptors, i), descriptors[i]);
+ fwrite(descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
+ fflush(out);
+ }
+
+ //Run through each section in turn.
+ json_object* sections = json_object_object_get(ir, "sections");
+ int amt_sections = json_object_array_length(sections);
+ for (int i=0; i<amt_sections; i++)
+ {
+ //Get the section itself from the IR.
+ json_object* section = json_object_array_get_idx(sections, i);
+
+ //Find the correct section type, and parse.
+ if (guid_equal(&descriptors[i]->SectionType, &gEfiProcessorGenericErrorSectionGuid))
+ ir_section_generic_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
+ // ir_section_ia32x64_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiIpfProcessorErrorSectionGuid))
+ // 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, &gEfiFirmwareErrorSectionGuid))
+ // ir_section_firmware_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiPciBusErrorSectionGuid))
+ // ir_section_pci_bus_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiPciDevErrorSectionGuid))
+ // ir_section_pci_dev_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiDMArGenericErrorSectionGuid))
+ // ir_section_dmar_generic_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
+ // ir_section_dmar_vtd_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiIommuDMArErrorSectionGuid))
+ // ir_section_dmar_iommu_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiCcixPerLogErrorSectionGuid))
+ // ir_section_ccix_per_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiCxlProtocolErrorSectionGuid))
+ // ir_section_cxl_protocol_to_cper(section, out);
+ // else if (guid_equal(&descriptors[i]->SectionType, &gEfiCxlGeneralMediaErrorSectionGuid)
+ // || guid_equal(&descriptors[i]->SectionType, &gEfiCxlDramEventErrorSectionGuid)
+ // || guid_equal(&descriptors[i]->SectionType, &gEfiCxlPhysicalSwitchErrorSectionGuid)
+ // || guid_equal(&descriptors[i]->SectionType, &gEfiCxlVirtualSwitchErrorSectionGuid)
+ // || guid_equal(&descriptors[i]->SectionType, &gEfiCxlMldPortErrorSectionGuid))
+ // {
+ // ir_section_cxl_component_to_cper(section, out);
+ // }
+ else
+ {
+ //Unknown GUID, so read as a base64 unknown section.
+ json_object* encoded = json_object_object_get(section, "data");
+ UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
+ fwrite(decoded, descriptors[i]->SectionLength, 1, out);
+ fflush(out);
+ free(decoded);
+ }
+ }
+
+ //Free all remaining resources.
free(header);
+ for (int i=0; i<amt_descriptors; i++)
+ free(descriptors[i]);
}
//Converts a CPER-JSON IR header to a CPER header structure.
void ir_header_to_cper(json_object* header_ir, EFI_COMMON_ERROR_RECORD_HEADER* header)
{
header->SignatureStart = 0x52455043; //CPER
- printf("beginning write.\n");
//Revision.
json_object* revision = json_object_object_get(header_ir, "revision");
@@ -60,7 +147,6 @@
header->RecordLength = (UINT32)json_object_get_uint64(json_object_object_get(header_ir, "recordLength"));
//Timestamp, if present.
- printf("timestamp write.\n");
json_object* timestamp = json_object_object_get(header_ir, "timestamp");
if (timestamp != NULL)
{
@@ -69,7 +155,6 @@
}
//Various GUIDs.
- printf("guid write.\n");
json_object* platform_id = json_object_object_get(header_ir, "platformID");
json_object* partition_id = json_object_object_get(header_ir, "partitionID");
if (platform_id != NULL)
@@ -79,7 +164,6 @@
string_to_guid(&header->CreatorID, json_object_get_string(json_object_object_get(header_ir, "creatorID")));
//Notification type.
- printf("notif type write.\n");
json_object* notification_type = json_object_object_get(header_ir, "notificationType");
string_to_guid(&header->NotificationType, json_object_get_string(json_object_object_get(notification_type, "guid")));
@@ -88,7 +172,44 @@
header->PersistenceInfo = json_object_get_uint64(json_object_object_get(header_ir, "persistenceInfo"));
//Flags.
- printf("flag write.\n");
json_object* flags = json_object_object_get(header_ir, "flags");
header->Flags = (UINT32)json_object_get_uint64(json_object_object_get(flags, "value"));
+}
+
+//Converts a single CPER-JSON IR section descriptor into a CPER structure.
+void ir_section_descriptor_to_cper(json_object* section_descriptor_ir, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
+{
+ //Section offset, length.
+ descriptor->SectionOffset = (UINT32)json_object_get_uint64(json_object_object_get(section_descriptor_ir, "sectionOffset"));
+ descriptor->SectionLength = (UINT32)json_object_get_uint64(json_object_object_get(section_descriptor_ir, "sectionLength"));
+
+ //Revision.
+ json_object* revision = json_object_object_get(section_descriptor_ir, "revision");
+ int minor = json_object_get_int(json_object_object_get(revision, "minor"));
+ int major = json_object_get_int(json_object_object_get(revision, "major"));
+ descriptor->Revision = minor + (major << 8);
+
+ //Validation bits, flags.
+ descriptor->SecValidMask = ir_to_bitfield(json_object_object_get(section_descriptor_ir, "validationBits"),
+ 2, CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
+ descriptor->SectionFlags = ir_to_bitfield(json_object_object_get(section_descriptor_ir, "flags"),
+ 8, CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
+
+ //Section type.
+ json_object* section_type = json_object_object_get(section_descriptor_ir, "sectionType");
+ string_to_guid(&descriptor->SectionType, json_object_get_string(json_object_object_get(section_type, "data")));
+
+ //FRU ID, if present.
+ json_object* fru_id = json_object_object_get(section_descriptor_ir, "fruID");
+ if (fru_id != NULL)
+ string_to_guid(&descriptor->FruId, json_object_get_string(fru_id));
+
+ //Severity code.
+ json_object* severity = json_object_object_get(section_descriptor_ir, "severity");
+ descriptor->Severity = (UINT32)json_object_get_uint64(json_object_object_get(severity, "code"));
+
+ //FRU text, if present.
+ json_object* fru_text = json_object_object_get(section_descriptor_ir, "fruText");
+ if (fru_text != NULL)
+ strncpy(descriptor->FruString, json_object_get_string(fru_text), 20);
}
\ No newline at end of file
diff --git a/sections/cper-section-generic.c b/sections/cper-section-generic.c
index 20f14c2..5b77c87 100644
--- a/sections/cper-section-generic.c
+++ b/sections/cper-section-generic.c
@@ -6,6 +6,7 @@
**/
#include <stdio.h>
+#include <string.h>
#include "json.h"
#include "../edk/Cper.h"
#include "../cper-utils.h"
@@ -74,4 +75,44 @@
json_object_object_add(section_ir, "instructionIP", json_object_new_uint64(section_generic->InstructionIP));
return section_ir;
+}
+
+//Converts the given CPER-JSON processor-generic error section into CPER binary,
+//outputting to the provided stream.
+void ir_section_generic_to_cper(json_object* section, FILE* out)
+{
+ EFI_PROCESSOR_GENERIC_ERROR_DATA* section_cper =
+ (EFI_PROCESSOR_GENERIC_ERROR_DATA*)calloc(1, sizeof(EFI_PROCESSOR_GENERIC_ERROR_DATA));
+
+ //Validation bits.
+ section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"),
+ 11, GENERIC_VALIDATION_BITFIELD_NAMES);
+
+ //Various name/value pair fields.
+ section_cper->Type = (UINT8)readable_pair_to_integer(json_object_object_get(section, "processorType"));
+ section_cper->Isa = (UINT8)readable_pair_to_integer(json_object_object_get(section, "processorISA"));
+ section_cper->ErrorType = (UINT8)readable_pair_to_integer(json_object_object_get(section, "errorType"));
+ section_cper->Operation = (UINT8)readable_pair_to_integer(json_object_object_get(section, "operation"));
+
+ //Flags.
+ section_cper->Flags = (UINT8)ir_to_bitfield(json_object_object_get(section, "flags"), 4, GENERIC_FLAGS_BITFIELD_NAMES);
+
+ //Various numeric/string fields.
+ section_cper->Level = (UINT8)json_object_get_int(json_object_object_get(section, "level"));
+ section_cper->VersionInfo = json_object_get_uint64(json_object_object_get(section, "cpuVersionInfo"));
+ section_cper->ApicId = json_object_get_uint64(json_object_object_get(section, "processorID"));
+ section_cper->TargetAddr = json_object_get_uint64(json_object_object_get(section, "targetAddress"));
+ 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->InstructionIP = json_object_get_uint64(json_object_object_get(section, "instructionIP"));
+
+ //CPU brand string.
+ const char* brand_string = json_object_get_string(json_object_object_get(section, "cpuBrandString"));
+ if (brand_string != NULL)
+ strncpy(section_cper->BrandString, brand_string, 127);
+
+ //Write & flush out to file, free memory.
+ fwrite(section_cper, sizeof(EFI_PROCESSOR_GENERIC_ERROR_DATA), 1, out);
+ fflush(out);
+ free(section_cper);
}
\ No newline at end of file
diff --git a/sections/cper-section-generic.h b/sections/cper-section-generic.h
index 78c5705..dd1931d 100644
--- a/sections/cper-section-generic.h
+++ b/sections/cper-section-generic.h
@@ -20,5 +20,6 @@
{"restartable", "preciseIP", "overflow", "corrected"}
json_object* cper_section_generic_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
+void ir_section_generic_to_cper(json_object* section, FILE* out);
#endif
\ No newline at end of file
diff --git a/testing/cper-test.c b/testing/cper-test.c
index 15b77be..114374c 100644
--- a/testing/cper-test.c
+++ b/testing/cper-test.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
#include "../cper-parse.h"
#include "json.h"
#include "../json-schema.h"
@@ -8,14 +9,28 @@
int main(int argc, char* argv[])
{
- test_ir_to_cper(argc, argv);
+ if (!strcmp(argv[1], "convert-to-json"))
+ test_cper_to_ir(argc, argv);
+ else if (!strcmp(argv[1], "convert-to-cper"))
+ test_ir_to_cper(argc, argv);
+ else
+ {
+ printf("Invalid command provided. Must be one of 'convert-to-json' or 'convert-to-cper'.\n");
+ }
+
return 0;
}
void test_ir_to_cper(int argc, char* argv[])
{
+ if (argc < 4)
+ {
+ printf("Insufficient number of arguments.\n");
+ return;
+ }
+
//Read JSON IR from file.
- json_object* ir = json_object_from_file(argv[1]);
+ json_object* ir = json_object_from_file(argv[2]);
if (ir == NULL)
{
printf("Could not read IR JSON, import returned null.");
@@ -23,7 +38,7 @@
}
//Open a read for the output file.
- FILE* cper_file = fopen(argv[2], "w");
+ FILE* cper_file = fopen(argv[3], "w");
if (cper_file == NULL) {
printf("Could not open output file, file handle returned null.");
return;
@@ -38,7 +53,7 @@
void test_cper_to_ir(int argc, char* argv[])
{
//Get a handle for the log file.
- FILE* cper_file = fopen(argv[1], "r");
+ FILE* cper_file = fopen(argv[2], "r");
if (cper_file == NULL) {
printf("Could not open CPER record, file handle returned null.");
return;
@@ -51,12 +66,12 @@
printf("\n%s\n", json_output);
//Test JSON validator.
- if (argc >= 3)
+ if (argc >= 4)
{
- printf("Validating output with specification %s...\n", argv[2]);
+ printf("Validating output with specification %s...\n", argv[3]);
validate_schema_debug_enable();
char* error_message = malloc(JSON_ERROR_MSG_MAX_LEN);
- if (!validate_schema_from_file(argv[2], ir, error_message))
+ if (!validate_schema_from_file(argv[3], ir, error_message))
{
printf("Validation failed: %s\n", error_message);
}