1 /** 2 * Describes functions for generating pseudo-random specification compliant CPER records. 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" 12 #include "sections/gen-sections.h" 13 #include "cper-generate.h" 14 15 EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type, 16 size_t *lengths, 17 int index, 18 int num_sections); 19 size_t generate_section(void **location, char *type); 20 21 //Generates a CPER record with the given section types, outputting to the given stream. 22 void generate_cper_record(char **types, UINT16 num_sections, FILE *out) 23 { 24 //Initialise randomiser. 25 init_random(); 26 27 //Generate the sections. 28 void *sections[num_sections]; 29 size_t section_lengths[num_sections]; 30 for (int i = 0; i < num_sections; i++) { 31 section_lengths[i] = generate_section(sections + i, types[i]); 32 if (section_lengths[i] == 0) { 33 //Error encountered, exit. 34 printf("Error encountered generating section %d of type '%s', length returned zero.\n", 35 i + 1, types[i]); 36 return; 37 } 38 } 39 40 //Generate the header given the number of sections. 41 EFI_COMMON_ERROR_RECORD_HEADER *header = 42 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc( 43 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER)); 44 header->SignatureStart = 0x52455043; //CPER 45 header->SectionCount = num_sections; 46 header->SignatureEnd = 0xFFFFFFFF; 47 header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED 48 header->RecordID = (UINT64)rand(); 49 header->ErrorSeverity = rand() % 4; 50 51 //Generate a valid timestamp. 52 header->TimeStamp.Century = int_to_bcd(rand() % 100); 53 header->TimeStamp.Year = int_to_bcd(rand() % 100); 54 header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1); 55 header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1); 56 header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1); 57 header->TimeStamp.Seconds = int_to_bcd(rand() % 60); 58 59 //Turn all validation bits on. 60 header->ValidationBits = 0b11; 61 62 //Generate the section descriptors given the number of sections. 63 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections]; 64 for (int i = 0; i < num_sections; i++) 65 section_descriptors[i] = generate_section_descriptor( 66 types[i], section_lengths, i, num_sections); 67 68 //Calculate total length of structure, set in header. 69 size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER); 70 for (int i = 0; i < num_sections; i++) 71 total_len += section_lengths[i]; 72 total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR); 73 header->RecordLength = (UINT32)total_len; 74 75 //Write to stream in order, free all resources. 76 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out); 77 fflush(out); 78 free(header); 79 for (int i = 0; i < num_sections; i++) { 80 fwrite(section_descriptors[i], 81 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out); 82 fflush(out); 83 free(section_descriptors[i]); 84 } 85 for (int i = 0; i < num_sections; i++) { 86 fwrite(sections[i], section_lengths[i], 1, out); 87 fflush(out); 88 free(sections[i]); 89 } 90 } 91 92 //Generates a single section record for the given section, and outputs to file. 93 void generate_single_section_record(char *type, FILE *out) 94 { 95 //Generate a section. 96 void *section; 97 size_t section_len = generate_section(§ion, type); 98 99 //Generate a descriptor, correct the offset. 100 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor = 101 generate_section_descriptor(type, §ion_len, 0, 1); 102 section_descriptor->SectionOffset = 103 sizeof(EFI_ERROR_SECTION_DESCRIPTOR); 104 105 //Write all to file. 106 fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, 107 out); 108 fwrite(section, section_len, 1, out); 109 fflush(out); 110 111 //Free remaining resources. 112 free(section_descriptor); 113 } 114 115 //Generates a single section descriptor for a section with the given properties. 116 EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type, 117 size_t *lengths, 118 int index, 119 int num_sections) 120 { 121 EFI_ERROR_SECTION_DESCRIPTOR *descriptor = 122 (EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes( 123 sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); 124 125 //Set reserved bits to zero. 126 descriptor->Resv1 = 0; 127 descriptor->SectionFlags &= 0xFF; 128 129 //Validation bits all set to 'on'. 130 descriptor->SecValidMask = 0b11; 131 132 //Set severity. 133 descriptor->Severity = rand() % 4; 134 135 //Set length, offset from base record. 136 descriptor->SectionLength = (UINT32)lengths[index]; 137 descriptor->SectionOffset = 138 sizeof(EFI_COMMON_ERROR_RECORD_HEADER) + 139 (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); 140 for (int i = 0; i < index; i++) 141 descriptor->SectionOffset += lengths[i]; 142 143 //Ensure the FRU text is not null terminated early. 144 for (int i = 0; i < 20; i++) { 145 if (descriptor->FruString[i] == 0x0) 146 descriptor->FruString[i] = rand() % 127 + 1; 147 148 //Null terminate last byte. 149 if (i == 19) 150 descriptor->FruString[i] = 0x0; 151 } 152 153 //Set section type GUID based on type name. 154 if (strcmp(type, "generic") == 0) 155 memcpy(&descriptor->SectionType, 156 &gEfiProcessorGenericErrorSectionGuid, sizeof(EFI_GUID)); 157 else if (strcmp(type, "ia32x64") == 0) 158 memcpy(&descriptor->SectionType, 159 &gEfiIa32X64ProcessorErrorSectionGuid, sizeof(EFI_GUID)); 160 else if (strcmp(type, "ipf") == 0) 161 memcpy(&descriptor->SectionType, 162 &gEfiIpfProcessorErrorSectionGuid, sizeof(EFI_GUID)); 163 else if (strcmp(type, "arm") == 0) 164 memcpy(&descriptor->SectionType, 165 &gEfiArmProcessorErrorSectionGuid, sizeof(EFI_GUID)); 166 else if (strcmp(type, "memory") == 0) 167 memcpy(&descriptor->SectionType, 168 &gEfiPlatformMemoryErrorSectionGuid, sizeof(EFI_GUID)); 169 else if (strcmp(type, "memory2") == 0) 170 memcpy(&descriptor->SectionType, 171 &gEfiPlatformMemoryError2SectionGuid, sizeof(EFI_GUID)); 172 else if (strcmp(type, "pcie") == 0) 173 memcpy(&descriptor->SectionType, &gEfiPcieErrorSectionGuid, 174 sizeof(EFI_GUID)); 175 else if (strcmp(type, "firmware") == 0) 176 memcpy(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid, 177 sizeof(EFI_GUID)); 178 else if (strcmp(type, "pcibus") == 0) 179 memcpy(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid, 180 sizeof(EFI_GUID)); 181 else if (strcmp(type, "pcidev") == 0) 182 memcpy(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid, 183 sizeof(EFI_GUID)); 184 else if (strcmp(type, "dmargeneric") == 0) 185 memcpy(&descriptor->SectionType, 186 &gEfiDMArGenericErrorSectionGuid, sizeof(EFI_GUID)); 187 else if (strcmp(type, "dmarvtd") == 0) 188 memcpy(&descriptor->SectionType, 189 &gEfiDirectedIoDMArErrorSectionGuid, sizeof(EFI_GUID)); 190 else if (strcmp(type, "dmariommu") == 0) 191 memcpy(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid, 192 sizeof(EFI_GUID)); 193 else if (strcmp(type, "ccixper") == 0) 194 memcpy(&descriptor->SectionType, 195 &gEfiCcixPerLogErrorSectionGuid, sizeof(EFI_GUID)); 196 else if (strcmp(type, "cxlprotocol") == 0) 197 memcpy(&descriptor->SectionType, 198 &gEfiCxlProtocolErrorSectionGuid, sizeof(EFI_GUID)); 199 else if (strcmp(type, "cxlcomponent") == 0) { 200 //Choose between the different CXL component type GUIDs. 201 int componentType = rand() % 5; 202 switch (componentType) { 203 case 0: 204 memcpy(&descriptor->SectionType, 205 &gEfiCxlGeneralMediaErrorSectionGuid, 206 sizeof(EFI_GUID)); 207 break; 208 case 1: 209 memcpy(&descriptor->SectionType, 210 &gEfiCxlDramEventErrorSectionGuid, 211 sizeof(EFI_GUID)); 212 break; 213 case 2: 214 memcpy(&descriptor->SectionType, 215 &gEfiCxlPhysicalSwitchErrorSectionGuid, 216 sizeof(EFI_GUID)); 217 break; 218 case 3: 219 memcpy(&descriptor->SectionType, 220 &gEfiCxlVirtualSwitchErrorSectionGuid, 221 sizeof(EFI_GUID)); 222 break; 223 default: 224 memcpy(&descriptor->SectionType, 225 &gEfiCxlMldPortErrorSectionGuid, 226 sizeof(EFI_GUID)); 227 break; 228 } 229 } else if (strcmp(type, "unknown") != 0) { 230 //Undefined section, show error. 231 printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n", 232 type); 233 return 0; 234 } 235 236 return descriptor; 237 } 238 239 //Generates a single CPER section given the string type. 240 size_t generate_section(void **location, char *type) 241 { 242 //The length of the section. 243 size_t length = 0; 244 245 //Switch on the type, generate accordingly. 246 if (strcmp(type, "generic") == 0) 247 length = generate_section_generic(location); 248 else if (strcmp(type, "ia32x64") == 0) 249 length = generate_section_ia32x64(location); 250 // else if (strcmp(type, "ipf") == 0) 251 // length = generate_section_ipf(location); 252 else if (strcmp(type, "arm") == 0) 253 length = generate_section_arm(location); 254 else if (strcmp(type, "memory") == 0) 255 length = generate_section_memory(location); 256 else if (strcmp(type, "memory2") == 0) 257 length = generate_section_memory2(location); 258 else if (strcmp(type, "pcie") == 0) 259 length = generate_section_pcie(location); 260 else if (strcmp(type, "firmware") == 0) 261 length = generate_section_firmware(location); 262 else if (strcmp(type, "pcibus") == 0) 263 length = generate_section_pci_bus(location); 264 else if (strcmp(type, "pcidev") == 0) 265 length = generate_section_pci_dev(location); 266 else if (strcmp(type, "dmargeneric") == 0) 267 length = generate_section_dmar_generic(location); 268 else if (strcmp(type, "dmarvtd") == 0) 269 length = generate_section_dmar_vtd(location); 270 else if (strcmp(type, "dmariommu") == 0) 271 length = generate_section_dmar_iommu(location); 272 else if (strcmp(type, "ccixper") == 0) 273 length = generate_section_ccix_per(location); 274 else if (strcmp(type, "cxlprotocol") == 0) 275 length = generate_section_cxl_protocol(location); 276 else if (strcmp(type, "cxlcomponent") == 0) 277 length = generate_section_cxl_component(location); 278 else if (strcmp(type, "unknown") == 0) 279 length = generate_random_section(location, rand() % 256); 280 else { 281 //Undefined section, show error. 282 printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n", 283 type); 284 return 0; 285 } 286 287 return length; 288 }