blob: 93df4fe1431de5d6a89985747893732554d6ce0d [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;
47 *((UINT64*)&header->TimeStamp) = (UINT64)rand();
48 header->ValidationBits = rand() % 0b1000;
Lawrence Tang02c801a2022-07-18 14:43:52 +010049
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 Tangd34f2b12022-07-19 15:36:31 +010053 section_descriptors[i] = generate_section_descriptor(types[i], section_lengths, i, num_sections);
Lawrence Tang02c801a2022-07-18 14:43:52 +010054
55 //Calculate total length of structure, set in header.
Lawrence Tang67cbed62022-07-18 16:44:51 +010056 size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
Lawrence Tang02c801a2022-07-18 14:43:52 +010057 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 Tangd34f2b12022-07-19 15:36:31 +010062 //Write to stream in order, free all resources.
63 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
64 fflush(out);
Lawrence Tang02c801a2022-07-18 14:43:52 +010065 free(header);
66 for (int i=0; i<num_sections; i++)
67 {
Lawrence Tangd34f2b12022-07-19 15:36:31 +010068 fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
69 fflush(out);
Lawrence Tang02c801a2022-07-18 14:43:52 +010070 free(section_descriptors[i]);
71 }
72 for (int i=0; i<num_sections; i++)
73 {
Lawrence Tangd34f2b12022-07-19 15:36:31 +010074 fwrite(sections[i], section_lengths[i], 1, out);
75 fflush(out);
Lawrence Tang02c801a2022-07-18 14:43:52 +010076 free(sections[i]);
77 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010078}
79
80//Generates a single section descriptor for a section with the given properties.
Lawrence Tang67cbed62022-07-18 16:44:51 +010081EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections)
Lawrence Tang02c801a2022-07-18 14:43:52 +010082{
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 Tangd34f2b12022-07-19 15:36:31 +010091 //Set severity.
92 descriptor->Severity = rand() % 4;
93
Lawrence Tang02c801a2022-07-18 14:43:52 +010094 //Set length, offset from base record.
95 descriptor->SectionLength = (UINT32)lengths[index];
Lawrence Tang67cbed62022-07-18 16:44:51 +010096 descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER)
97 + (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
Lawrence Tang02c801a2022-07-18 14:43:52 +010098 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.
166size_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 Tang67cbed62022-07-18 16:44:51 +0100178 else if (strcmp(type, "arm") == 0)
179 length = generate_section_arm(location);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100180 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 Tang02c801a2022-07-18 14:43:52 +0100206 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 Tang02c801a2022-07-18 14:43:52 +0100214}