blob: 7e7cc40dd8dab923b2d8e2e11d413f3e16c64bc7 [file] [log] [blame]
Lawrence Tang02c801a2022-07-18 14:43:52 +01001/**
Lawrence Tangd34f2b12022-07-19 15:36:31 +01002 * Describes functions for generating psuedo-random specification compliant CPER records.
Lawrence Tang02c801a2022-07-18 14:43:52 +01003 *
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 Tang67cbed62022-07-18 16:44:51 +010012#include "sections/gen-sections.h"
Lawrence Tangd34f2b12022-07-19 15:36:31 +010013#include "cper-generate.h"
Lawrence Tang02c801a2022-07-18 14:43:52 +010014
Lawrence Tang67cbed62022-07-18 16:44:51 +010015EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections);
Lawrence Tang02c801a2022-07-18 14:43:52 +010016size_t generate_section(void** location, char* type);
Lawrence Tang02c801a2022-07-18 14:43:52 +010017
Lawrence Tangd34f2b12022-07-19 15:36:31 +010018//Generates a CPER record with the given section types, outputting to the given stream.
19void generate_cper_record(char** types, UINT16 num_sections, FILE* out)
Lawrence Tang02c801a2022-07-18 14:43:52 +010020{
Lawrence Tang02c801a2022-07-18 14:43:52 +010021 //Initialise randomiser.
22 init_random();
23
Lawrence Tangd34f2b12022-07-19 15:36:31 +010024 //Generate the sections.
Lawrence Tang02c801a2022-07-18 14:43:52 +010025 void* sections[num_sections];
26 size_t section_lengths[num_sections];
27 for (int i=0; i<num_sections; i++)
28 {
Lawrence Tangd34f2b12022-07-19 15:36:31 +010029 section_lengths[i] = generate_section(sections + i, types[i]);
Lawrence Tang02c801a2022-07-18 14:43:52 +010030 if (section_lengths[i] == 0)
31 {
32 //Error encountered, exit.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010033 printf("Error encountered generating section %d of type '%s', length returned zero.\n", i+1, types[i]);
34 return;
Lawrence Tang02c801a2022-07-18 14:43:52 +010035 }
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 Tang02c801a2022-07-18 14:43:52 +010043 header->SignatureEnd = 0xFFFFFFFF;
44 header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
Lawrence Tang67cbed62022-07-18 16:44:51 +010045 header->RecordID = (UINT64)rand();
46 header->ErrorSeverity = rand() % 4;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010047
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;
Lawrence Tang02c801a2022-07-18 14:43:52 +010058
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++)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010062 section_descriptors[i] = generate_section_descriptor(types[i], section_lengths, i, num_sections);
Lawrence Tang02c801a2022-07-18 14:43:52 +010063
64 //Calculate total length of structure, set in header.
Lawrence Tang67cbed62022-07-18 16:44:51 +010065 size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
Lawrence Tang02c801a2022-07-18 14:43:52 +010066 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
Lawrence Tangd34f2b12022-07-19 15:36:31 +010071 //Write to stream in order, free all resources.
72 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
73 fflush(out);
Lawrence Tang02c801a2022-07-18 14:43:52 +010074 free(header);
75 for (int i=0; i<num_sections; i++)
76 {
Lawrence Tangd34f2b12022-07-19 15:36:31 +010077 fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
78 fflush(out);
Lawrence Tang02c801a2022-07-18 14:43:52 +010079 free(section_descriptors[i]);
80 }
81 for (int i=0; i<num_sections; i++)
82 {
Lawrence Tangd34f2b12022-07-19 15:36:31 +010083 fwrite(sections[i], section_lengths[i], 1, out);
84 fflush(out);
Lawrence Tang02c801a2022-07-18 14:43:52 +010085 free(sections[i]);
86 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010087}
88
89//Generates a single section descriptor for a section with the given properties.
Lawrence Tang67cbed62022-07-18 16:44:51 +010090EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections)
Lawrence Tang02c801a2022-07-18 14:43:52 +010091{
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.
Lawrence Tang02c801a2022-07-18 14:43:52 +010096 descriptor->Resv1 = 0;
97 descriptor->SectionFlags &= 0xFF;
98
Lawrence Tangaacf0e22022-07-20 13:28:52 +010099 //Validation bits all set to 'on'.
100 descriptor->SecValidMask = 0b11;
101
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100102 //Set severity.
103 descriptor->Severity = rand() % 4;
104
Lawrence Tang02c801a2022-07-18 14:43:52 +0100105 //Set length, offset from base record.
106 descriptor->SectionLength = (UINT32)lengths[index];
Lawrence Tang67cbed62022-07-18 16:44:51 +0100107 descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER)
108 + (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
Lawrence Tang02c801a2022-07-18 14:43:52 +0100109 for (int i=0; i<index; i++)
110 descriptor->SectionOffset += lengths[i];
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100111
112 //Ensure the FRU text is not null terminated early.
113 for (int i=0; i<20; i++)
114 {
Lawrence Tang01e3a442022-07-20 15:14:50 +0100115 if (descriptor->FruString[i] == 0x0)
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100116 descriptor->FruString[i] = rand() % 127 + 1;
117
118 //Null terminate last byte.
119 if (i == 19)
120 descriptor->FruString[i] = 0x0;
121 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100122
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.
188size_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);
Lawrence Tang67cbed62022-07-18 16:44:51 +0100200 else if (strcmp(type, "arm") == 0)
201 length = generate_section_arm(location);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100202 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);
Lawrence Tang02c801a2022-07-18 14:43:52 +0100228 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;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100236}