xref: /openbmc/libcper/generator/cper-generate.c (revision de9707f9)
1 /**
2  * A user-space application 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 
14 EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections);
15 size_t generate_section(void** location, char* type);
16 void print_help();
17 
18 int main(int argc, char* argv[])
19 {
20     //If help requested, print help.
21     if (argc == 2 && strcmp(argv[1], "--help") == 0)
22     {
23         print_help();
24         return 0;
25     }
26 
27     //Ensure the minimum number of arguments.
28     if (argc < 5)
29     {
30         printf("Insufficient number of arguments. See 'cper-generate --help' for command information.\n");
31         return -1;
32     }
33 
34     //Initialise randomiser.
35     init_random();
36 
37     //Generate the sections. Type names start from argv[4].
38     UINT16 num_sections = argc - 4;
39     void* sections[num_sections];
40     size_t section_lengths[num_sections];
41     for (int i=0; i<num_sections; i++)
42     {
43         section_lengths[i] = generate_section(sections + i, argv[4 + i]);
44         if (section_lengths[i] == 0)
45         {
46             //Error encountered, exit.
47             return -1;
48         }
49     }
50 
51     //Generate the header given the number of sections.
52     EFI_COMMON_ERROR_RECORD_HEADER* header =
53         (EFI_COMMON_ERROR_RECORD_HEADER*)calloc(1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
54     header->SignatureStart = 0x52455043; //CPER
55     header->SectionCount = num_sections;
56     header->SignatureEnd = 0xFFFFFFFF;
57     header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
58     header->RecordID = (UINT64)rand();
59     header->ErrorSeverity = rand() % 4;
60     *((UINT64*)&header->TimeStamp) = (UINT64)rand();
61     header->ValidationBits = rand() % 0b1000;
62 
63     //Generate the section descriptors given the number of sections.
64     EFI_ERROR_SECTION_DESCRIPTOR* section_descriptors[num_sections];
65     for (int i=0; i<num_sections; i++)
66         section_descriptors[i] = generate_section_descriptor(argv[4 + 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     //Open a file handle to write output.
76     FILE* cper_file = fopen(argv[2], "w");
77     if (cper_file == NULL)
78     {
79         printf("Could not get a handle for output file '%s', file handle returned null.\n", argv[2]);
80         return -1;
81     }
82 
83     //Write to file in order, free all resources.
84     fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, cper_file);
85     fflush(cper_file);
86     free(header);
87     for (int i=0; i<num_sections; i++)
88     {
89         fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, cper_file);
90         fflush(cper_file);
91         free(section_descriptors[i]);
92     }
93     for (int i=0; i<num_sections; i++)
94     {
95         fwrite(sections[i], section_lengths[i], 1, cper_file);
96         fflush(cper_file);
97         free(sections[i]);
98     }
99     fclose(cper_file);
100 }
101 
102 //Generates a single section descriptor for a section with the given properties.
103 EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections)
104 {
105     EFI_ERROR_SECTION_DESCRIPTOR* descriptor =
106         (EFI_ERROR_SECTION_DESCRIPTOR*)generate_random_bytes(sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
107 
108     //Set reserved bits to zero.
109     descriptor->SecValidMask &= 0b11;
110     descriptor->Resv1 = 0;
111     descriptor->SectionFlags &= 0xFF;
112 
113     //Set length, offset from base record.
114     descriptor->SectionLength = (UINT32)lengths[index];
115     descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER)
116         + (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
117     for (int i=0; i<index; i++)
118         descriptor->SectionOffset += lengths[i];
119 
120     //Set section type GUID based on type name.
121     if (strcmp(type, "generic") == 0)
122         memcpy(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid, sizeof(EFI_GUID));
123     else if (strcmp(type, "ia32x64") == 0)
124         memcpy(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid, sizeof(EFI_GUID));
125     else if (strcmp(type, "ipf") == 0)
126         memcpy(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid, sizeof(EFI_GUID));
127     else if (strcmp(type, "arm") == 0)
128         memcpy(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid, sizeof(EFI_GUID));
129     else if (strcmp(type, "memory") == 0)
130         memcpy(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid, sizeof(EFI_GUID));
131     else if (strcmp(type, "memory2") == 0)
132         memcpy(&descriptor->SectionType, &gEfiPlatformMemoryError2SectionGuid, sizeof(EFI_GUID));
133     else if (strcmp(type, "pcie") == 0)
134         memcpy(&descriptor->SectionType, &gEfiPcieErrorSectionGuid, sizeof(EFI_GUID));
135     else if (strcmp(type, "firmware") == 0)
136         memcpy(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid, sizeof(EFI_GUID));
137     else if (strcmp(type, "pcibus") == 0)
138         memcpy(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid, sizeof(EFI_GUID));
139     else if (strcmp(type, "pcidev") == 0)
140         memcpy(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid, sizeof(EFI_GUID));
141     else if (strcmp(type, "dmargeneric") == 0)
142         memcpy(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid, sizeof(EFI_GUID));
143     else if (strcmp(type, "dmarvtd") == 0)
144         memcpy(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid, sizeof(EFI_GUID));
145     else if (strcmp(type, "dmariommu") == 0)
146         memcpy(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid, sizeof(EFI_GUID));
147     else if (strcmp(type, "ccixper") == 0)
148         memcpy(&descriptor->SectionType, &gEfiCcixPerLogErrorSectionGuid, sizeof(EFI_GUID));
149     else if (strcmp(type, "cxlprotocol") == 0)
150         memcpy(&descriptor->SectionType, &gEfiCxlProtocolErrorSectionGuid, sizeof(EFI_GUID));
151     else if (strcmp(type, "cxlcomponent") == 0)
152     {
153         //Choose between the different CXL component type GUIDs.
154         int componentType = rand() % 5;
155         switch (componentType)
156         {
157             case 0:
158                 memcpy(&descriptor->SectionType, &gEfiCxlGeneralMediaErrorSectionGuid, sizeof(EFI_GUID));
159                 break;
160             case 1:
161                 memcpy(&descriptor->SectionType, &gEfiCxlDramEventErrorSectionGuid, sizeof(EFI_GUID));
162                 break;
163             case 2:
164                 memcpy(&descriptor->SectionType, &gEfiCxlPhysicalSwitchErrorSectionGuid, sizeof(EFI_GUID));
165                 break;
166             case 3:
167                 memcpy(&descriptor->SectionType, &gEfiCxlVirtualSwitchErrorSectionGuid, sizeof(EFI_GUID));
168                 break;
169             default:
170                 memcpy(&descriptor->SectionType, &gEfiCxlMldPortErrorSectionGuid, sizeof(EFI_GUID));
171                 break;
172         }
173     }
174     else if (strcmp(type, "unknown") != 0)
175     {
176         //Undefined section, show error.
177         printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n", type);
178         return 0;
179     }
180 
181     return descriptor;
182 }
183 
184 //Generates a single CPER section given the string type.
185 size_t generate_section(void** location, char* type)
186 {
187     //The length of the section.
188     size_t length = 0;
189 
190     //Switch on the type, generate accordingly.
191     if (strcmp(type, "generic") == 0)
192         length = generate_section_generic(location);
193     else if (strcmp(type, "ia32x64") == 0)
194         length = generate_section_ia32x64(location);
195     // else if (strcmp(type, "ipf") == 0)
196     //     length = generate_section_ipf(location);
197     else if (strcmp(type, "arm") == 0)
198         length = generate_section_arm(location);
199     else if (strcmp(type, "memory") == 0)
200         length = generate_section_memory(location);
201     else if (strcmp(type, "memory2") == 0)
202         length = generate_section_memory2(location);
203     else if (strcmp(type, "pcie") == 0)
204         length = generate_section_pcie(location);
205     else if (strcmp(type, "firmware") == 0)
206         length = generate_section_firmware(location);
207     else if (strcmp(type, "pcibus") == 0)
208         length = generate_section_pci_bus(location);
209     else if (strcmp(type, "pcidev") == 0)
210         length = generate_section_pci_dev(location);
211     else if (strcmp(type, "dmargeneric") == 0)
212         length = generate_section_dmar_generic(location);
213     else if (strcmp(type, "dmarvtd") == 0)
214         length = generate_section_dmar_vtd(location);
215     else if (strcmp(type, "dmariommu") == 0)
216         length = generate_section_dmar_iommu(location);
217     else if (strcmp(type, "ccixper") == 0)
218         length = generate_section_ccix_per(location);
219     else if (strcmp(type, "cxlprotocol") == 0)
220         length = generate_section_cxl_protocol(location);
221     else if (strcmp(type, "cxlcomponent") == 0)
222         length = generate_section_cxl_component(location);
223     else if (strcmp(type, "unknown") == 0)
224         length = generate_random_section(location, rand() % 256);
225     else
226     {
227         //Undefined section, show error.
228         printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n", type);
229         return 0;
230     }
231 
232     return length;
233 }
234 
235 //Prints command help for this CPER generator.
236 void print_help()
237 {
238     printf(":: --out cper.file --sections section1 [section2 section3 ...]\n");
239     printf("\tGenerates a psuedo-random CPER file with the provided section types and outputs to the given file name.\n");
240     printf("\tValid section type names are the following:\n");
241     printf("\t\t- generic\n");
242     printf("\t\t- ia32x64\n");
243     printf("\t\t- ipf\n");
244     printf("\t\t- arm\n");
245     printf("\t\t- memory\n");
246     printf("\t\t- memory2\n");
247     printf("\t\t- pcie\n");
248     printf("\t\t- firmware\n");
249     printf("\t\t- pcibus\n");
250     printf("\t\t- pcidev\n");
251     printf("\t\t- dmargeneric\n");
252     printf("\t\t- dmarvtd\n");
253     printf("\t\t- dmariommu\n");
254     printf("\t\t- ccixper\n");
255     printf("\t\t- cxlprotocol\n");
256     printf("\t\t- cxlcomponent\n");
257     printf("\t\t- unknown\n");
258     printf("\n:: --help\n");
259     printf("\tDisplays help information to the console.\n");
260 }