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