Add "cper-generate", update README, fix minor bugs.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c9a80a2..bf5ddc4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,8 +26,10 @@
# Add library and test executable.
file(GLOB SectionSources sections/*.c)
file(GLOB EDKSources edk/*.c)
+file(GLOB GeneratorSectionSources generator/sections/*.c)
add_library(cper-parse STATIC cper-parse.c ir-parse.c cper-utils.c json-schema.c json-schema.h ${SectionSources} ${EDKSources})
-add_executable(cper-convert testing/cper-convert.c)
+add_executable(cper-convert cli-app/cper-convert.c)
+add_executable(cper-generate generator/cper-generate.c generator/gen-utils.c ${GeneratorSectionSources} ${EDKSources})
# Link library.
target_link_libraries(cper-parse json-c b64c)
diff --git a/README.md b/README.md
index ebebb1c..a29c3e9 100644
--- a/README.md
+++ b/README.md
@@ -9,6 +9,28 @@
```
A static library file for the parsing library will be written to `lib/`, and test executables will be written to `bin/`.
+## Usage
+This project comes with several binaries and libraries to help you deal with CPER binary and CPER-JSON. The first of these is `cper-convert`, which is a command line tool that can be found in `bin/`. With this, you can convert to and from CPER and CPER-JSON through the command line. An example usage scenario is below:
+```
+cper-convert to-cper samples/cper-json-test-arm.json --out cper.dump
+cper-convert to-json cper.generated.dump
+```
+Another tool bundled with this repository is `cper-generate`, which is another command line tool found in `bin/`. This allows you to generate psuedo-random valid CPER records with sections of specified types for testing purposes. An example use of the program is below:
+```
+cper-generate --out cper.generated.dump --sections generic ia32x64
+```
+Help for both of these tools can be accessed through using the `--help` flag in isolation.
+
+Finally, a static library containing symbols for converting CPER and CPER-JSON between an intermediate JSON format can be found generated in `lib/`. This contains the following useful library symbols:
+```
+json_object* cper_to_ir(FILE* cper_file);
+void ir_to_cper(json_object* ir, FILE* out);
+```
+
## Specification
The specification for this project can be found in `specification/`.
-Specification for the CPER binary format can be found in [UEFI Specification Appendix N](https://uefi.org/sites/default/files/resources/UEFI_Spec_2_9_2021_03_18.pdf) (2021/03/18).
\ No newline at end of file
+Specification for the CPER binary format can be found in [UEFI Specification Appendix N](https://uefi.org/sites/default/files/resources/UEFI_Spec_2_9_2021_03_18.pdf) (2021/03/18).
+
+### Remaining Task List
+- Add readable versions of the IA32/x64 processor info type GUIDs, updating specification & both conversions.
+- Add remaining `cper-generate` sections for testing preparation.esting purposes.
\ No newline at end of file
diff --git a/testing/cper-convert.c b/cli-app/cper-convert.c
similarity index 100%
rename from testing/cper-convert.c
rename to cli-app/cper-convert.c
diff --git a/cper-parse.c b/cper-parse.c
index ee670e1..0bc801e 100644
--- a/cper-parse.c
+++ b/cper-parse.c
@@ -41,13 +41,13 @@
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).");
+ printf("Invalid CPER file: Invalid length (log too short).\n");
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).");
+ printf("Invalid CPER file: Invalid header (incorrect signature).\n");
return NULL;
}
@@ -63,7 +63,7 @@
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);
+ printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n", header.SectionCount, i+1);
return NULL;
}
json_object_array_add(section_descriptors_ir, cper_section_descriptor_to_ir(§ion_descriptor));
@@ -230,7 +230,8 @@
section_type_readable = "IPF";
else if (guid_equal(§ion_descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
section_type_readable = "ARM";
- else if (guid_equal(§ion_descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
+ else if (guid_equal(§ion_descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid)
+ || guid_equal(§ion_descriptor->SectionType, &gEfiPlatformMemoryError2SectionGuid))
section_type_readable = "Platform Memory";
else if (guid_equal(§ion_descriptor->SectionType, &gEfiPcieErrorSectionGuid))
section_type_readable = "PCIe";
@@ -294,7 +295,7 @@
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.",
+ printf("Section read failed: Could not read %d bytes from global offset %d.\n",
descriptor->SectionLength,
descriptor->SectionOffset);
free(section);
diff --git a/cper-parse.h b/cper-parse.h
index 439f731..f9c6466 100644
--- a/cper-parse.h
+++ b/cper-parse.h
@@ -7,7 +7,7 @@
#define CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES (const char*[]) \
{"primary", "containmentWarning", "reset", "errorThresholdExceeded", "resourceNotAccessible", "latentError", \
"propagated", "overflow"}
-#define CPER_HEADER_FLAG_TYPES_KEYS (int []){1, 2, 3}
+#define CPER_HEADER_FLAG_TYPES_KEYS (int []){1, 2, 4}
#define CPER_HEADER_FLAG_TYPES_VALUES (const char*[]){"HW_ERROR_FLAGS_RECOVERED", "HW_ERROR_FLAGS_PREVERR", "HW_ERROR_FLAGS_SIMULATED"}
json_object* cper_to_ir(FILE* cper_file);
diff --git a/cper-utils.c b/cper-utils.c
index 28d94d4..c8206c0 100644
--- a/cper-utils.c
+++ b/cper-utils.c
@@ -192,7 +192,7 @@
}
//Returns the appropriate string for the given integer severity.
-const char* severity_to_string(UINT8 severity)
+const char* severity_to_string(UINT32 severity)
{
return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown";
}
diff --git a/cper-utils.h b/cper-utils.h
index 6b34b1a..cea6e0a 100644
--- a/cper-utils.h
+++ b/cper-utils.h
@@ -17,7 +17,7 @@
UINT64 ir_to_bitfield(json_object* ir, int num_fields, const char* names[]);
json_object* uint64_array_to_ir_array(UINT64* array, int len);
json_object* revision_to_ir(UINT16 revision);
-const char* severity_to_string(UINT8 severity);
+const char* severity_to_string(UINT32 severity);
void timestamp_to_string(char* out, EFI_ERROR_TIME_STAMP* timestamp);
void string_to_timestamp(EFI_ERROR_TIME_STAMP* out, const char* timestamp);
void guid_to_string(char* out, EFI_GUID* guid);
diff --git a/generator/cper-generate.c b/generator/cper-generate.c
new file mode 100644
index 0000000..dd336fe
--- /dev/null
+++ b/generator/cper-generate.c
@@ -0,0 +1,257 @@
+/**
+ * A user-space application for generating psuedo-random specification compliant CPER records.
+ *
+ * Author: Lawrence.Tang@arm.com
+ **/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../edk/Cper.h"
+#include "gen-utils.h"
+#include "sections/gen-section-generic.h"
+#include "sections/gen-section-ia32x64.h"
+
+EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index);
+size_t generate_section(void** location, char* type);
+void print_help();
+
+int main(int argc, char* argv[])
+{
+ //If help requested, print help.
+ if (argc == 2 && strcmp(argv[1], "--help") == 0)
+ {
+ print_help();
+ return 0;
+ }
+
+ //Ensure the minimum number of arguments.
+ if (argc < 5)
+ {
+ printf("Insufficient number of arguments. See 'cper-generate --help' for command information.\n");
+ return -1;
+ }
+
+ //Initialise randomiser.
+ init_random();
+
+ //Generate the sections. Type names start from argv[4].
+ UINT16 num_sections = argc - 4;
+ void* sections[num_sections];
+ size_t section_lengths[num_sections];
+ for (int i=0; i<num_sections; i++)
+ {
+ section_lengths[i] = generate_section(sections + i, argv[4 + i]);
+ if (section_lengths[i] == 0)
+ {
+ //Error encountered, exit.
+ return -1;
+ }
+ }
+
+ //Generate the header given the number of sections.
+ EFI_COMMON_ERROR_RECORD_HEADER* header =
+ (EFI_COMMON_ERROR_RECORD_HEADER*)calloc(1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
+ header->SignatureStart = 0x52455043; //CPER
+ header->SectionCount = num_sections;
+ printf("%d sections\n", num_sections);
+ header->SignatureEnd = 0xFFFFFFFF;
+ header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
+
+ //Generate the section descriptors given the number of sections.
+ EFI_ERROR_SECTION_DESCRIPTOR* section_descriptors[num_sections];
+ for (int i=0; i<num_sections; i++)
+ section_descriptors[i] = generate_section_descriptor(argv[4 + i], section_lengths, i);
+
+ //Calculate total length of structure, set in header.
+ size_t total_len = sizeof(header);
+ for (int i=0; i<num_sections; i++)
+ total_len += section_lengths[i];
+ total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
+ header->RecordLength = (UINT32)total_len;
+
+ //Open a file handle to write output.
+ FILE* cper_file = fopen(argv[2], "w");
+ if (cper_file == NULL)
+ {
+ printf("Could not get a handle for output file '%s', file handle returned null.\n", argv[2]);
+ return -1;
+ }
+
+ //Write to file in order, free all resources.
+ fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, cper_file);
+ fflush(cper_file);
+ free(header);
+ for (int i=0; i<num_sections; i++)
+ {
+ fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, cper_file);
+ fflush(cper_file);
+ free(section_descriptors[i]);
+ }
+ for (int i=0; i<num_sections; i++)
+ {
+ fwrite(sections[i], section_lengths[i], 1, cper_file);
+ fflush(cper_file);
+ free(sections[i]);
+ }
+ fclose(cper_file);
+}
+
+//Generates a single section descriptor for a section with the given properties.
+EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index)
+{
+ EFI_ERROR_SECTION_DESCRIPTOR* descriptor =
+ (EFI_ERROR_SECTION_DESCRIPTOR*)generate_random_bytes(sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
+
+ //Set reserved bits to zero.
+ descriptor->SecValidMask &= 0b11;
+ descriptor->Resv1 = 0;
+ descriptor->SectionFlags &= 0xFF;
+
+ //Set length, offset from base record.
+ descriptor->SectionLength = (UINT32)lengths[index];
+ descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
+ for (int i=0; i<index; i++)
+ descriptor->SectionOffset += lengths[i];
+
+ //Set section type GUID based on type name.
+ if (strcmp(type, "generic") == 0)
+ memcpy(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "ia32x64") == 0)
+ memcpy(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "ipf") == 0)
+ memcpy(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "arm") == 0)
+ memcpy(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "memory") == 0)
+ memcpy(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "memory2") == 0)
+ memcpy(&descriptor->SectionType, &gEfiPlatformMemoryError2SectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "pcie") == 0)
+ memcpy(&descriptor->SectionType, &gEfiPcieErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "firmware") == 0)
+ memcpy(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "pcibus") == 0)
+ memcpy(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "pcidev") == 0)
+ memcpy(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "dmargeneric") == 0)
+ memcpy(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "dmarvtd") == 0)
+ memcpy(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "dmariommu") == 0)
+ memcpy(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "ccixper") == 0)
+ memcpy(&descriptor->SectionType, &gEfiCcixPerLogErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "cxlprotocol") == 0)
+ memcpy(&descriptor->SectionType, &gEfiCxlProtocolErrorSectionGuid, sizeof(EFI_GUID));
+ else if (strcmp(type, "cxlcomponent") == 0)
+ {
+ //Choose between the different CXL component type GUIDs.
+ int componentType = rand() % 5;
+ switch (componentType)
+ {
+ case 0:
+ memcpy(&descriptor->SectionType, &gEfiCxlGeneralMediaErrorSectionGuid, sizeof(EFI_GUID));
+ break;
+ case 1:
+ memcpy(&descriptor->SectionType, &gEfiCxlDramEventErrorSectionGuid, sizeof(EFI_GUID));
+ break;
+ case 2:
+ memcpy(&descriptor->SectionType, &gEfiCxlPhysicalSwitchErrorSectionGuid, sizeof(EFI_GUID));
+ break;
+ case 3:
+ memcpy(&descriptor->SectionType, &gEfiCxlVirtualSwitchErrorSectionGuid, sizeof(EFI_GUID));
+ break;
+ default:
+ memcpy(&descriptor->SectionType, &gEfiCxlMldPortErrorSectionGuid, sizeof(EFI_GUID));
+ break;
+ }
+ }
+ else if (strcmp(type, "unknown") != 0)
+ {
+ //Undefined section, show error.
+ printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n", type);
+ return 0;
+ }
+
+ return descriptor;
+}
+
+//Generates a single CPER section given the string type.
+size_t generate_section(void** location, char* type)
+{
+ //The length of the section.
+ size_t length = 0;
+
+ //Switch on the type, generate accordingly.
+ if (strcmp(type, "generic") == 0)
+ length = generate_section_generic(location);
+ else if (strcmp(type, "ia32x64") == 0)
+ length = generate_section_ia32x64(location);
+ // else if (strcmp(type, "ipf") == 0)
+ // length = generate_section_ipf(location);
+ // else if (strcmp(type, "arm") == 0)
+ // length = generate_section_arm(location);
+ // else if (strcmp(type, "memory") == 0)
+ // length = generate_section_memory(location);
+ // else if (strcmp(type, "memory2") == 0)
+ // length = generate_section_memory2(location);
+ // else if (strcmp(type, "pcie") == 0)
+ // length = generate_section_pcie(location);
+ // else if (strcmp(type, "firmware") == 0)
+ // length = generate_section_firmware(location);
+ // else if (strcmp(type, "pcibus") == 0)
+ // length = generate_section_pci_bus(location);
+ // else if (strcmp(type, "pcidev") == 0)
+ // length = generate_section_pci_dev(location);
+ // else if (strcmp(type, "dmargeneric") == 0)
+ // length = generate_section_dmar_generic(location);
+ // else if (strcmp(type, "dmarvtd") == 0)
+ // length = generate_section_dmar_vtd(location);
+ // else if (strcmp(type, "dmariommu") == 0)
+ // length = generate_section_dmar_iommu(location);
+ // else if (strcmp(type, "ccixper") == 0)
+ // length = generate_section_ccix_per(location);
+ // else if (strcmp(type, "cxlprotocol") == 0)
+ // length = generate_section_cxl_protocol(location);
+ // else if (strcmp(type, "cxlcomponent") == 0)
+ // length = generate_section_cxl_component(location);
+ // else if (strcmp(type, "unknown") == 0)
+ // length = generate_section_unknown(location);
+ else
+ {
+ //Undefined section, show error.
+ printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n", type);
+ return 0;
+ }
+
+ return length;
+}
+
+//Prints command help for this CPER generator.
+void print_help()
+{
+ printf(":: --out cper.file --sections section1 [section2 section3 ...]\n");
+ printf("\tGenerates a psuedo-random CPER file with the provided section types and outputs to the given file name.\n");
+ printf("\tValid section type names are the following:\n");
+ printf("\t\t- generic\n");
+ printf("\t\t- ia32x64\n");
+ printf("\t\t- ipf\n");
+ printf("\t\t- arm\n");
+ printf("\t\t- memory\n");
+ printf("\t\t- memory2\n");
+ printf("\t\t- pcie\n");
+ printf("\t\t- firmware\n");
+ printf("\t\t- pcibus\n");
+ printf("\t\t- pcidev\n");
+ printf("\t\t- dmargeneric\n");
+ printf("\t\t- dmarvtd\n");
+ printf("\t\t- dmariommu\n");
+ printf("\t\t- ccixper\n");
+ printf("\t\t- cxlprotocol\n");
+ printf("\t\t- cxlcomponent\n");
+ printf("\t\t- unknown\n");
+ printf("\n:: --help\n");
+ printf("\tDisplays help information to the console.\n");
+}
\ No newline at end of file
diff --git a/generator/gen-utils.c b/generator/gen-utils.c
new file mode 100644
index 0000000..a0f6e3c
--- /dev/null
+++ b/generator/gen-utils.c
@@ -0,0 +1,32 @@
+/**
+ * Utility functions to assist in generating psuedo-random CPER sections.
+ *
+ * Author: Lawrence.Tang@arm.com
+ **/
+#include <stdlib.h>
+#include <time.h>
+#include "../edk/BaseTypes.h"
+#include "gen-utils.h"
+
+//Generates a random section of the given byte size, saving the result to the given location.
+//Returns the length of the section as passed in.
+size_t generate_random_section(void** location, size_t size)
+{
+ *location = generate_random_bytes(size);
+ return size;
+}
+
+//Generates a random byte allocation of the given size.
+UINT8* generate_random_bytes(size_t size)
+{
+ UINT8* bytes = malloc(size);
+ for (size_t i = 0; i < size; i++)
+ bytes[i] = rand();
+ return bytes;
+}
+
+//Initializes the random seed for rand() using the current time.
+void init_random()
+{
+ srand((unsigned int)time(NULL));
+}
\ No newline at end of file
diff --git a/generator/gen-utils.h b/generator/gen-utils.h
new file mode 100644
index 0000000..4a966db
--- /dev/null
+++ b/generator/gen-utils.h
@@ -0,0 +1,11 @@
+#ifndef GEN_UTILS_H
+#define GEN_UTILS_H
+
+#include <stdlib.h>
+#include "../edk/BaseTypes.h"
+
+size_t generate_random_section(void** location, size_t size);
+UINT8* generate_random_bytes(size_t size);
+void init_random();
+
+#endif
\ No newline at end of file
diff --git a/generator/sections/gen-section-generic.c b/generator/sections/gen-section-generic.c
new file mode 100644
index 0000000..205fea1
--- /dev/null
+++ b/generator/sections/gen-section-generic.c
@@ -0,0 +1,26 @@
+/**
+ * Functions for generating psuedo-random CPER generic processor sections.
+ *
+ * Author: Lawrence.Tang@arm.com
+ **/
+
+#include <stdlib.h>
+#include "../../edk/BaseTypes.h"
+#include "../gen-utils.h"
+#include "gen-section-generic.h"
+
+//Generates a single psuedo-random generic processor section, saving the resulting address to the given
+//location. Returns the size of the newly created section.
+size_t generate_section_generic(void** location)
+{
+ //Create random bytes.
+ size_t size = generate_random_section(location, 192);
+
+ //Set reserved locations to zero.
+ UINT8* start_byte = (UINT8*)*location;
+ *((UINT64*)start_byte) &= 0xFFF;
+ *(start_byte + 12) &= 0b111;
+ *((UINT16*)(start_byte + 14)) = 0x0;
+
+ return size;
+}
\ No newline at end of file
diff --git a/generator/sections/gen-section-generic.h b/generator/sections/gen-section-generic.h
new file mode 100644
index 0000000..49ff655
--- /dev/null
+++ b/generator/sections/gen-section-generic.h
@@ -0,0 +1,8 @@
+#ifndef GEN_SECTION_GENERIC_H
+#define GEN_SECTION_GENERIC_H
+
+#include <stdlib.h>
+
+size_t generate_section_generic(void** location);
+
+#endif
\ No newline at end of file
diff --git a/generator/sections/gen-section-ia32x64.c b/generator/sections/gen-section-ia32x64.c
new file mode 100644
index 0000000..b06a237
--- /dev/null
+++ b/generator/sections/gen-section-ia32x64.c
@@ -0,0 +1,129 @@
+/**
+ * Functions for generating psuedo-random CPER IA32/x64 sections.
+ *
+ * Author: Lawrence.Tang@arm.com
+ **/
+
+#include <stdlib.h>
+#include <string.h>
+#include "../../edk/Cper.h"
+#include "../gen-utils.h"
+#include "gen-section-ia32x64.h"
+
+void* generate_ia32x64_error_structure();
+size_t generate_ia32x64_context_structure(void** location);
+
+//Generates a single psuedo-random IA32/x64 section, saving the resulting address to the given
+//location. Returns the size of the newly created section.
+size_t generate_section_ia32x64(void** location)
+{
+ //Set up for generation of error/context structures.
+ UINT16 error_structure_num = rand() % 5;
+ UINT16 context_structure_num = rand() % 5;
+ void* error_structures[error_structure_num];
+ void* context_structures[context_structure_num];
+ size_t context_structure_lengths[context_structure_num];
+
+ //Generate the structures.
+ for (int i=0; i<error_structure_num; i++)
+ error_structures[i] = generate_ia32x64_error_structure();
+ for (int i=0; i<context_structure_num; i++)
+ context_structure_lengths[i] = generate_ia32x64_context_structure(context_structures + i);
+
+ //Create a valid IA32/x64 section.
+ size_t total_len = 64 + (IA32X64_ERROR_STRUCTURE_SIZE * error_structure_num);
+ for (int i=0; i<context_structure_num; i++)
+ total_len += context_structure_lengths[i];
+ UINT8* section = generate_random_bytes(total_len);
+
+ //Set header information.
+ UINT64* validation = (UINT64*)section;
+ *validation &= 0b11;
+ *validation |= error_structure_num << 2;
+ *validation |= context_structure_num << 8;
+
+ //Copy in structures, free resources.
+ UINT8* cur_pos = section + 64;
+ for (int i=0; i<error_structure_num; i++)
+ {
+ memcpy(cur_pos, error_structures[i], IA32X64_ERROR_STRUCTURE_SIZE);
+ free(error_structures[i]);
+ cur_pos += IA32X64_ERROR_STRUCTURE_SIZE;
+ }
+ for (int i=0; i<context_structure_num; i++)
+ {
+ memcpy(cur_pos, context_structures[i], context_structure_lengths[i]);
+ free(context_structures[i]);
+ cur_pos += context_structure_lengths[i];
+ }
+
+ //Set return values, exist.
+ *location = section;
+ return total_len;
+}
+
+//Generates a single IA32/x64 error structure. Must later be freed.
+void* generate_ia32x64_error_structure()
+{
+ UINT8* error_structure = generate_random_bytes(IA32X64_ERROR_STRUCTURE_SIZE);
+
+ //Create a random type of error structure.
+ EFI_GUID* guid = (EFI_GUID*)error_structure;
+ int error_structure_type = rand() % 4;
+ switch (error_structure_type)
+ {
+ //Cache
+ case 0:
+ memcpy(guid, &gEfiIa32x64ErrorTypeCacheCheckGuid, sizeof(guid));
+ memset(error_structure + 30, 0, 34);
+ break;
+
+ //TLB
+ case 1:
+ memcpy(guid, &gEfiIa32x64ErrorTypeTlbCheckGuid, sizeof(guid));
+ memset(error_structure + 30, 0, 34);
+ break;
+
+ //Bus
+ case 2:
+ memcpy(guid, &gEfiIa32x64ErrorTypeBusCheckGuid, sizeof(guid));
+ memset(error_structure + 35, 0, 29);
+ break;
+
+ //MS
+ case 3:
+ memcpy(guid, &gEfiIa32x64ErrorTypeMsCheckGuid, sizeof(guid));
+ memset(error_structure + 24, 0, 38);
+ break;
+ }
+
+ return error_structure;
+}
+
+//Generates a single IA32/x64 context structure. Must later be freed.
+size_t generate_ia32x64_context_structure(void** location)
+{
+ //Initial length is 16 bytes. Add extra based on type.
+ int reg_type = rand() % 8;
+ int reg_size = 0;
+
+ //Set register size.
+ if (reg_type == 2)
+ reg_size = 92; //IA32 registers.
+ if (reg_type == 3)
+ reg_size = 244; //x64 registers.
+ else
+ reg_size = rand() % 64; //Not table defined.
+
+ //Create structure randomly.
+ int total_size = 16 + reg_size;
+ UINT16* context_structure = (UINT16*)generate_random_bytes(total_size);
+
+ //Set header information.
+ *(context_structure) = reg_type;
+ *(context_structure + 1) = reg_size;
+
+ //Set return values and exit.
+ *location = context_structure;
+ return total_size;
+}
\ No newline at end of file
diff --git a/generator/sections/gen-section-ia32x64.h b/generator/sections/gen-section-ia32x64.h
new file mode 100644
index 0000000..78f5052
--- /dev/null
+++ b/generator/sections/gen-section-ia32x64.h
@@ -0,0 +1,10 @@
+#ifndef GEN_SECTION_IA32X64_H
+#define GEN_SECTION_IA32X64_H
+
+#include <stdlib.h>
+
+#define IA32X64_ERROR_STRUCTURE_SIZE 64
+
+size_t generate_section_ia32x64(void** location);
+
+#endif
\ No newline at end of file