Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 2 | * Describes functions for generating psuedo-random specification compliant CPER records. |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 3 | * |
| 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include "../edk/Cper.h" |
| 11 | #include "gen-utils.h" |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 12 | #include "sections/gen-sections.h" |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 13 | #include "cper-generate.h" |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 14 | |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 15 | EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 16 | size_t generate_section(void** location, char* type); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 17 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 18 | //Generates a CPER record with the given section types, outputting to the given stream. |
| 19 | void generate_cper_record(char** types, UINT16 num_sections, FILE* out) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 20 | { |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 21 | //Initialise randomiser. |
| 22 | init_random(); |
| 23 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 24 | //Generate the sections. |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 25 | void* sections[num_sections]; |
| 26 | size_t section_lengths[num_sections]; |
| 27 | for (int i=0; i<num_sections; i++) |
| 28 | { |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 29 | section_lengths[i] = generate_section(sections + i, types[i]); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 30 | if (section_lengths[i] == 0) |
| 31 | { |
| 32 | //Error encountered, exit. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 33 | printf("Error encountered generating section %d of type '%s', length returned zero.\n", i+1, types[i]); |
| 34 | return; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | //Generate the header given the number of sections. |
| 39 | EFI_COMMON_ERROR_RECORD_HEADER* header = |
| 40 | (EFI_COMMON_ERROR_RECORD_HEADER*)calloc(1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER)); |
| 41 | header->SignatureStart = 0x52455043; //CPER |
| 42 | header->SectionCount = num_sections; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 43 | header->SignatureEnd = 0xFFFFFFFF; |
| 44 | header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 45 | header->RecordID = (UINT64)rand(); |
| 46 | header->ErrorSeverity = rand() % 4; |
| 47 | *((UINT64*)&header->TimeStamp) = (UINT64)rand(); |
| 48 | header->ValidationBits = rand() % 0b1000; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 49 | |
| 50 | //Generate the section descriptors given the number of sections. |
| 51 | EFI_ERROR_SECTION_DESCRIPTOR* section_descriptors[num_sections]; |
| 52 | for (int i=0; i<num_sections; i++) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 53 | section_descriptors[i] = generate_section_descriptor(types[i], section_lengths, i, num_sections); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 54 | |
| 55 | //Calculate total length of structure, set in header. |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 56 | size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 57 | for (int i=0; i<num_sections; i++) |
| 58 | total_len += section_lengths[i]; |
| 59 | total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR); |
| 60 | header->RecordLength = (UINT32)total_len; |
| 61 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 62 | //Write to stream in order, free all resources. |
| 63 | fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out); |
| 64 | fflush(out); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 65 | free(header); |
| 66 | for (int i=0; i<num_sections; i++) |
| 67 | { |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 68 | fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out); |
| 69 | fflush(out); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 70 | free(section_descriptors[i]); |
| 71 | } |
| 72 | for (int i=0; i<num_sections; i++) |
| 73 | { |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 74 | fwrite(sections[i], section_lengths[i], 1, out); |
| 75 | fflush(out); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 76 | free(sections[i]); |
| 77 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | //Generates a single section descriptor for a section with the given properties. |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 81 | EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 82 | { |
| 83 | EFI_ERROR_SECTION_DESCRIPTOR* descriptor = |
| 84 | (EFI_ERROR_SECTION_DESCRIPTOR*)generate_random_bytes(sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
| 85 | |
| 86 | //Set reserved bits to zero. |
| 87 | descriptor->SecValidMask &= 0b11; |
| 88 | descriptor->Resv1 = 0; |
| 89 | descriptor->SectionFlags &= 0xFF; |
| 90 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame^] | 91 | //Set severity. |
| 92 | descriptor->Severity = rand() % 4; |
| 93 | |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 94 | //Set length, offset from base record. |
| 95 | descriptor->SectionLength = (UINT32)lengths[index]; |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 96 | descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER) |
| 97 | + (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 98 | for (int i=0; i<index; i++) |
| 99 | descriptor->SectionOffset += lengths[i]; |
| 100 | |
| 101 | //Set section type GUID based on type name. |
| 102 | if (strcmp(type, "generic") == 0) |
| 103 | memcpy(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid, sizeof(EFI_GUID)); |
| 104 | else if (strcmp(type, "ia32x64") == 0) |
| 105 | memcpy(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid, sizeof(EFI_GUID)); |
| 106 | else if (strcmp(type, "ipf") == 0) |
| 107 | memcpy(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid, sizeof(EFI_GUID)); |
| 108 | else if (strcmp(type, "arm") == 0) |
| 109 | memcpy(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid, sizeof(EFI_GUID)); |
| 110 | else if (strcmp(type, "memory") == 0) |
| 111 | memcpy(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid, sizeof(EFI_GUID)); |
| 112 | else if (strcmp(type, "memory2") == 0) |
| 113 | memcpy(&descriptor->SectionType, &gEfiPlatformMemoryError2SectionGuid, sizeof(EFI_GUID)); |
| 114 | else if (strcmp(type, "pcie") == 0) |
| 115 | memcpy(&descriptor->SectionType, &gEfiPcieErrorSectionGuid, sizeof(EFI_GUID)); |
| 116 | else if (strcmp(type, "firmware") == 0) |
| 117 | memcpy(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid, sizeof(EFI_GUID)); |
| 118 | else if (strcmp(type, "pcibus") == 0) |
| 119 | memcpy(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid, sizeof(EFI_GUID)); |
| 120 | else if (strcmp(type, "pcidev") == 0) |
| 121 | memcpy(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid, sizeof(EFI_GUID)); |
| 122 | else if (strcmp(type, "dmargeneric") == 0) |
| 123 | memcpy(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid, sizeof(EFI_GUID)); |
| 124 | else if (strcmp(type, "dmarvtd") == 0) |
| 125 | memcpy(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid, sizeof(EFI_GUID)); |
| 126 | else if (strcmp(type, "dmariommu") == 0) |
| 127 | memcpy(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid, sizeof(EFI_GUID)); |
| 128 | else if (strcmp(type, "ccixper") == 0) |
| 129 | memcpy(&descriptor->SectionType, &gEfiCcixPerLogErrorSectionGuid, sizeof(EFI_GUID)); |
| 130 | else if (strcmp(type, "cxlprotocol") == 0) |
| 131 | memcpy(&descriptor->SectionType, &gEfiCxlProtocolErrorSectionGuid, sizeof(EFI_GUID)); |
| 132 | else if (strcmp(type, "cxlcomponent") == 0) |
| 133 | { |
| 134 | //Choose between the different CXL component type GUIDs. |
| 135 | int componentType = rand() % 5; |
| 136 | switch (componentType) |
| 137 | { |
| 138 | case 0: |
| 139 | memcpy(&descriptor->SectionType, &gEfiCxlGeneralMediaErrorSectionGuid, sizeof(EFI_GUID)); |
| 140 | break; |
| 141 | case 1: |
| 142 | memcpy(&descriptor->SectionType, &gEfiCxlDramEventErrorSectionGuid, sizeof(EFI_GUID)); |
| 143 | break; |
| 144 | case 2: |
| 145 | memcpy(&descriptor->SectionType, &gEfiCxlPhysicalSwitchErrorSectionGuid, sizeof(EFI_GUID)); |
| 146 | break; |
| 147 | case 3: |
| 148 | memcpy(&descriptor->SectionType, &gEfiCxlVirtualSwitchErrorSectionGuid, sizeof(EFI_GUID)); |
| 149 | break; |
| 150 | default: |
| 151 | memcpy(&descriptor->SectionType, &gEfiCxlMldPortErrorSectionGuid, sizeof(EFI_GUID)); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | else if (strcmp(type, "unknown") != 0) |
| 156 | { |
| 157 | //Undefined section, show error. |
| 158 | printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n", type); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | return descriptor; |
| 163 | } |
| 164 | |
| 165 | //Generates a single CPER section given the string type. |
| 166 | size_t generate_section(void** location, char* type) |
| 167 | { |
| 168 | //The length of the section. |
| 169 | size_t length = 0; |
| 170 | |
| 171 | //Switch on the type, generate accordingly. |
| 172 | if (strcmp(type, "generic") == 0) |
| 173 | length = generate_section_generic(location); |
| 174 | else if (strcmp(type, "ia32x64") == 0) |
| 175 | length = generate_section_ia32x64(location); |
| 176 | // else if (strcmp(type, "ipf") == 0) |
| 177 | // length = generate_section_ipf(location); |
Lawrence Tang | 67cbed6 | 2022-07-18 16:44:51 +0100 | [diff] [blame] | 178 | else if (strcmp(type, "arm") == 0) |
| 179 | length = generate_section_arm(location); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 180 | else if (strcmp(type, "memory") == 0) |
| 181 | length = generate_section_memory(location); |
| 182 | else if (strcmp(type, "memory2") == 0) |
| 183 | length = generate_section_memory2(location); |
| 184 | else if (strcmp(type, "pcie") == 0) |
| 185 | length = generate_section_pcie(location); |
| 186 | else if (strcmp(type, "firmware") == 0) |
| 187 | length = generate_section_firmware(location); |
| 188 | else if (strcmp(type, "pcibus") == 0) |
| 189 | length = generate_section_pci_bus(location); |
| 190 | else if (strcmp(type, "pcidev") == 0) |
| 191 | length = generate_section_pci_dev(location); |
| 192 | else if (strcmp(type, "dmargeneric") == 0) |
| 193 | length = generate_section_dmar_generic(location); |
| 194 | else if (strcmp(type, "dmarvtd") == 0) |
| 195 | length = generate_section_dmar_vtd(location); |
| 196 | else if (strcmp(type, "dmariommu") == 0) |
| 197 | length = generate_section_dmar_iommu(location); |
| 198 | else if (strcmp(type, "ccixper") == 0) |
| 199 | length = generate_section_ccix_per(location); |
| 200 | else if (strcmp(type, "cxlprotocol") == 0) |
| 201 | length = generate_section_cxl_protocol(location); |
| 202 | else if (strcmp(type, "cxlcomponent") == 0) |
| 203 | length = generate_section_cxl_component(location); |
| 204 | else if (strcmp(type, "unknown") == 0) |
| 205 | length = generate_random_section(location, rand() % 256); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 206 | else |
| 207 | { |
| 208 | //Undefined section, show error. |
| 209 | printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n", type); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | return length; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 214 | } |