xref: /openbmc/libcper/generator/cper-generate.c (revision d34f2b11)
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     *((UINT64*)&header->TimeStamp) = (UINT64)rand();
48     header->ValidationBits = rand() % 0b1000;
49 
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++)
53         section_descriptors[i] = generate_section_descriptor(types[i], section_lengths, i, num_sections);
54 
55     //Calculate total length of structure, set in header.
56     size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
57     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 
62     //Write to stream in order, free all resources.
63     fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
64     fflush(out);
65     free(header);
66     for (int i=0; i<num_sections; i++)
67     {
68         fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
69         fflush(out);
70         free(section_descriptors[i]);
71     }
72     for (int i=0; i<num_sections; i++)
73     {
74         fwrite(sections[i], section_lengths[i], 1, out);
75         fflush(out);
76         free(sections[i]);
77     }
78 }
79 
80 //Generates a single section descriptor for a section with the given properties.
81 EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections)
82 {
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 
91     //Set severity.
92     descriptor->Severity = rand() % 4;
93 
94     //Set length, offset from base record.
95     descriptor->SectionLength = (UINT32)lengths[index];
96     descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER)
97         + (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
98     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.
166 size_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);
178     else if (strcmp(type, "arm") == 0)
179         length = generate_section_arm(location);
180     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);
206     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;
214 }