xref: /openbmc/libcper/generator/cper-generate.c (revision 01e3a44d)
102c801a5SLawrence Tang /**
2d34f2b11SLawrence Tang  * Describes functions for generating psuedo-random specification compliant CPER records.
302c801a5SLawrence Tang  *
402c801a5SLawrence Tang  * Author: Lawrence.Tang@arm.com
502c801a5SLawrence Tang  **/
602c801a5SLawrence Tang 
702c801a5SLawrence Tang #include <stdio.h>
802c801a5SLawrence Tang #include <stdlib.h>
902c801a5SLawrence Tang #include <string.h>
1002c801a5SLawrence Tang #include "../edk/Cper.h"
1102c801a5SLawrence Tang #include "gen-utils.h"
1267cbed6bSLawrence Tang #include "sections/gen-sections.h"
13d34f2b11SLawrence Tang #include "cper-generate.h"
1402c801a5SLawrence Tang 
1567cbed6bSLawrence Tang EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections);
1602c801a5SLawrence Tang size_t generate_section(void** location, char* type);
1702c801a5SLawrence Tang 
18d34f2b11SLawrence Tang //Generates a CPER record with the given section types, outputting to the given stream.
19d34f2b11SLawrence Tang void generate_cper_record(char** types, UINT16 num_sections, FILE* out)
2002c801a5SLawrence Tang {
2102c801a5SLawrence Tang     //Initialise randomiser.
2202c801a5SLawrence Tang     init_random();
2302c801a5SLawrence Tang 
24d34f2b11SLawrence Tang     //Generate the sections.
2502c801a5SLawrence Tang     void* sections[num_sections];
2602c801a5SLawrence Tang     size_t section_lengths[num_sections];
2702c801a5SLawrence Tang     for (int i=0; i<num_sections; i++)
2802c801a5SLawrence Tang     {
29d34f2b11SLawrence Tang         section_lengths[i] = generate_section(sections + i, types[i]);
3002c801a5SLawrence Tang         if (section_lengths[i] == 0)
3102c801a5SLawrence Tang         {
3202c801a5SLawrence Tang             //Error encountered, exit.
33d34f2b11SLawrence Tang             printf("Error encountered generating section %d of type '%s', length returned zero.\n", i+1, types[i]);
34d34f2b11SLawrence Tang             return;
3502c801a5SLawrence Tang         }
3602c801a5SLawrence Tang     }
3702c801a5SLawrence Tang 
3802c801a5SLawrence Tang     //Generate the header given the number of sections.
3902c801a5SLawrence Tang     EFI_COMMON_ERROR_RECORD_HEADER* header =
4002c801a5SLawrence Tang         (EFI_COMMON_ERROR_RECORD_HEADER*)calloc(1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
4102c801a5SLawrence Tang     header->SignatureStart = 0x52455043; //CPER
4202c801a5SLawrence Tang     header->SectionCount = num_sections;
4302c801a5SLawrence Tang     header->SignatureEnd = 0xFFFFFFFF;
4402c801a5SLawrence Tang     header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
4567cbed6bSLawrence Tang     header->RecordID = (UINT64)rand();
4667cbed6bSLawrence Tang     header->ErrorSeverity = rand() % 4;
47aacf0e26SLawrence Tang 
48aacf0e26SLawrence Tang     //Generate a valid timestamp.
49aacf0e26SLawrence Tang     header->TimeStamp.Century = int_to_bcd(rand() % 100);
50aacf0e26SLawrence Tang     header->TimeStamp.Year = int_to_bcd(rand() % 100);
51aacf0e26SLawrence Tang     header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1);
52aacf0e26SLawrence Tang     header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1);
53aacf0e26SLawrence Tang     header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1);
54aacf0e26SLawrence Tang     header->TimeStamp.Seconds = int_to_bcd(rand() % 60);
55aacf0e26SLawrence Tang 
56aacf0e26SLawrence Tang     //Turn all validation bits on.
57aacf0e26SLawrence Tang     header->ValidationBits = 0b11;
5802c801a5SLawrence Tang 
5902c801a5SLawrence Tang     //Generate the section descriptors given the number of sections.
6002c801a5SLawrence Tang     EFI_ERROR_SECTION_DESCRIPTOR* section_descriptors[num_sections];
6102c801a5SLawrence Tang     for (int i=0; i<num_sections; i++)
62d34f2b11SLawrence Tang         section_descriptors[i] = generate_section_descriptor(types[i], section_lengths, i, num_sections);
6302c801a5SLawrence Tang 
6402c801a5SLawrence Tang     //Calculate total length of structure, set in header.
6567cbed6bSLawrence Tang     size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
6602c801a5SLawrence Tang     for (int i=0; i<num_sections; i++)
6702c801a5SLawrence Tang         total_len += section_lengths[i];
6802c801a5SLawrence Tang     total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
6902c801a5SLawrence Tang     header->RecordLength = (UINT32)total_len;
7002c801a5SLawrence Tang 
71d34f2b11SLawrence Tang     //Write to stream in order, free all resources.
72d34f2b11SLawrence Tang     fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
73d34f2b11SLawrence Tang     fflush(out);
7402c801a5SLawrence Tang     free(header);
7502c801a5SLawrence Tang     for (int i=0; i<num_sections; i++)
7602c801a5SLawrence Tang     {
77d34f2b11SLawrence Tang         fwrite(section_descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
78d34f2b11SLawrence Tang         fflush(out);
7902c801a5SLawrence Tang         free(section_descriptors[i]);
8002c801a5SLawrence Tang     }
8102c801a5SLawrence Tang     for (int i=0; i<num_sections; i++)
8202c801a5SLawrence Tang     {
83d34f2b11SLawrence Tang         fwrite(sections[i], section_lengths[i], 1, out);
84d34f2b11SLawrence Tang         fflush(out);
8502c801a5SLawrence Tang         free(sections[i]);
8602c801a5SLawrence Tang     }
8702c801a5SLawrence Tang }
8802c801a5SLawrence Tang 
8902c801a5SLawrence Tang //Generates a single section descriptor for a section with the given properties.
9067cbed6bSLawrence Tang EFI_ERROR_SECTION_DESCRIPTOR* generate_section_descriptor(char* type, size_t* lengths, int index, int num_sections)
9102c801a5SLawrence Tang {
9202c801a5SLawrence Tang     EFI_ERROR_SECTION_DESCRIPTOR* descriptor =
9302c801a5SLawrence Tang         (EFI_ERROR_SECTION_DESCRIPTOR*)generate_random_bytes(sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
9402c801a5SLawrence Tang 
9502c801a5SLawrence Tang     //Set reserved bits to zero.
9602c801a5SLawrence Tang     descriptor->Resv1 = 0;
9702c801a5SLawrence Tang     descriptor->SectionFlags &= 0xFF;
9802c801a5SLawrence Tang 
99aacf0e26SLawrence Tang     //Validation bits all set to 'on'.
100aacf0e26SLawrence Tang     descriptor->SecValidMask = 0b11;
101aacf0e26SLawrence Tang 
102d34f2b11SLawrence Tang     //Set severity.
103d34f2b11SLawrence Tang     descriptor->Severity = rand() % 4;
104d34f2b11SLawrence Tang 
10502c801a5SLawrence Tang     //Set length, offset from base record.
10602c801a5SLawrence Tang     descriptor->SectionLength = (UINT32)lengths[index];
10767cbed6bSLawrence Tang     descriptor->SectionOffset = sizeof(EFI_COMMON_ERROR_RECORD_HEADER)
10867cbed6bSLawrence Tang         + (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
10902c801a5SLawrence Tang     for (int i=0; i<index; i++)
11002c801a5SLawrence Tang         descriptor->SectionOffset += lengths[i];
11102c801a5SLawrence Tang 
112aacf0e26SLawrence Tang     //Ensure the FRU text is not null terminated early.
113aacf0e26SLawrence Tang     for (int i=0; i<20; i++)
114aacf0e26SLawrence Tang     {
115*01e3a44dSLawrence Tang         if (descriptor->FruString[i] == 0x0)
116aacf0e26SLawrence Tang             descriptor->FruString[i] = rand() % 127 + 1;
117aacf0e26SLawrence Tang 
118aacf0e26SLawrence Tang         //Null terminate last byte.
119aacf0e26SLawrence Tang         if (i == 19)
120aacf0e26SLawrence Tang             descriptor->FruString[i] = 0x0;
121aacf0e26SLawrence Tang     }
122aacf0e26SLawrence Tang 
12302c801a5SLawrence Tang     //Set section type GUID based on type name.
12402c801a5SLawrence Tang     if (strcmp(type, "generic") == 0)
12502c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid, sizeof(EFI_GUID));
12602c801a5SLawrence Tang     else if (strcmp(type, "ia32x64") == 0)
12702c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid, sizeof(EFI_GUID));
12802c801a5SLawrence Tang     else if (strcmp(type, "ipf") == 0)
12902c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid, sizeof(EFI_GUID));
13002c801a5SLawrence Tang     else if (strcmp(type, "arm") == 0)
13102c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid, sizeof(EFI_GUID));
13202c801a5SLawrence Tang     else if (strcmp(type, "memory") == 0)
13302c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid, sizeof(EFI_GUID));
13402c801a5SLawrence Tang     else if (strcmp(type, "memory2") == 0)
13502c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiPlatformMemoryError2SectionGuid, sizeof(EFI_GUID));
13602c801a5SLawrence Tang     else if (strcmp(type, "pcie") == 0)
13702c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiPcieErrorSectionGuid, sizeof(EFI_GUID));
13802c801a5SLawrence Tang     else if (strcmp(type, "firmware") == 0)
13902c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid, sizeof(EFI_GUID));
14002c801a5SLawrence Tang     else if (strcmp(type, "pcibus") == 0)
14102c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid, sizeof(EFI_GUID));
14202c801a5SLawrence Tang     else if (strcmp(type, "pcidev") == 0)
14302c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid, sizeof(EFI_GUID));
14402c801a5SLawrence Tang     else if (strcmp(type, "dmargeneric") == 0)
14502c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid, sizeof(EFI_GUID));
14602c801a5SLawrence Tang     else if (strcmp(type, "dmarvtd") == 0)
14702c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid, sizeof(EFI_GUID));
14802c801a5SLawrence Tang     else if (strcmp(type, "dmariommu") == 0)
14902c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid, sizeof(EFI_GUID));
15002c801a5SLawrence Tang     else if (strcmp(type, "ccixper") == 0)
15102c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiCcixPerLogErrorSectionGuid, sizeof(EFI_GUID));
15202c801a5SLawrence Tang     else if (strcmp(type, "cxlprotocol") == 0)
15302c801a5SLawrence Tang         memcpy(&descriptor->SectionType, &gEfiCxlProtocolErrorSectionGuid, sizeof(EFI_GUID));
15402c801a5SLawrence Tang     else if (strcmp(type, "cxlcomponent") == 0)
15502c801a5SLawrence Tang     {
15602c801a5SLawrence Tang         //Choose between the different CXL component type GUIDs.
15702c801a5SLawrence Tang         int componentType = rand() % 5;
15802c801a5SLawrence Tang         switch (componentType)
15902c801a5SLawrence Tang         {
16002c801a5SLawrence Tang             case 0:
16102c801a5SLawrence Tang                 memcpy(&descriptor->SectionType, &gEfiCxlGeneralMediaErrorSectionGuid, sizeof(EFI_GUID));
16202c801a5SLawrence Tang                 break;
16302c801a5SLawrence Tang             case 1:
16402c801a5SLawrence Tang                 memcpy(&descriptor->SectionType, &gEfiCxlDramEventErrorSectionGuid, sizeof(EFI_GUID));
16502c801a5SLawrence Tang                 break;
16602c801a5SLawrence Tang             case 2:
16702c801a5SLawrence Tang                 memcpy(&descriptor->SectionType, &gEfiCxlPhysicalSwitchErrorSectionGuid, sizeof(EFI_GUID));
16802c801a5SLawrence Tang                 break;
16902c801a5SLawrence Tang             case 3:
17002c801a5SLawrence Tang                 memcpy(&descriptor->SectionType, &gEfiCxlVirtualSwitchErrorSectionGuid, sizeof(EFI_GUID));
17102c801a5SLawrence Tang                 break;
17202c801a5SLawrence Tang             default:
17302c801a5SLawrence Tang                 memcpy(&descriptor->SectionType, &gEfiCxlMldPortErrorSectionGuid, sizeof(EFI_GUID));
17402c801a5SLawrence Tang                 break;
17502c801a5SLawrence Tang         }
17602c801a5SLawrence Tang     }
17702c801a5SLawrence Tang     else if (strcmp(type, "unknown") != 0)
17802c801a5SLawrence Tang     {
17902c801a5SLawrence Tang         //Undefined section, show error.
18002c801a5SLawrence Tang         printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n", type);
18102c801a5SLawrence Tang         return 0;
18202c801a5SLawrence Tang     }
18302c801a5SLawrence Tang 
18402c801a5SLawrence Tang     return descriptor;
18502c801a5SLawrence Tang }
18602c801a5SLawrence Tang 
18702c801a5SLawrence Tang //Generates a single CPER section given the string type.
18802c801a5SLawrence Tang size_t generate_section(void** location, char* type)
18902c801a5SLawrence Tang {
19002c801a5SLawrence Tang     //The length of the section.
19102c801a5SLawrence Tang     size_t length = 0;
19202c801a5SLawrence Tang 
19302c801a5SLawrence Tang     //Switch on the type, generate accordingly.
19402c801a5SLawrence Tang     if (strcmp(type, "generic") == 0)
19502c801a5SLawrence Tang         length = generate_section_generic(location);
19602c801a5SLawrence Tang     else if (strcmp(type, "ia32x64") == 0)
19702c801a5SLawrence Tang         length = generate_section_ia32x64(location);
19802c801a5SLawrence Tang     // else if (strcmp(type, "ipf") == 0)
19902c801a5SLawrence Tang     //     length = generate_section_ipf(location);
20067cbed6bSLawrence Tang     else if (strcmp(type, "arm") == 0)
20167cbed6bSLawrence Tang         length = generate_section_arm(location);
202de9707f9SLawrence Tang     else if (strcmp(type, "memory") == 0)
203de9707f9SLawrence Tang         length = generate_section_memory(location);
204de9707f9SLawrence Tang     else if (strcmp(type, "memory2") == 0)
205de9707f9SLawrence Tang         length = generate_section_memory2(location);
206de9707f9SLawrence Tang     else if (strcmp(type, "pcie") == 0)
207de9707f9SLawrence Tang         length = generate_section_pcie(location);
208de9707f9SLawrence Tang     else if (strcmp(type, "firmware") == 0)
209de9707f9SLawrence Tang         length = generate_section_firmware(location);
210de9707f9SLawrence Tang     else if (strcmp(type, "pcibus") == 0)
211de9707f9SLawrence Tang         length = generate_section_pci_bus(location);
212de9707f9SLawrence Tang     else if (strcmp(type, "pcidev") == 0)
213de9707f9SLawrence Tang         length = generate_section_pci_dev(location);
214de9707f9SLawrence Tang     else if (strcmp(type, "dmargeneric") == 0)
215de9707f9SLawrence Tang         length = generate_section_dmar_generic(location);
216de9707f9SLawrence Tang     else if (strcmp(type, "dmarvtd") == 0)
217de9707f9SLawrence Tang         length = generate_section_dmar_vtd(location);
218de9707f9SLawrence Tang     else if (strcmp(type, "dmariommu") == 0)
219de9707f9SLawrence Tang         length = generate_section_dmar_iommu(location);
220de9707f9SLawrence Tang     else if (strcmp(type, "ccixper") == 0)
221de9707f9SLawrence Tang         length = generate_section_ccix_per(location);
222de9707f9SLawrence Tang     else if (strcmp(type, "cxlprotocol") == 0)
223de9707f9SLawrence Tang         length = generate_section_cxl_protocol(location);
224de9707f9SLawrence Tang     else if (strcmp(type, "cxlcomponent") == 0)
225de9707f9SLawrence Tang         length = generate_section_cxl_component(location);
226de9707f9SLawrence Tang     else if (strcmp(type, "unknown") == 0)
227de9707f9SLawrence Tang         length = generate_random_section(location, rand() % 256);
22802c801a5SLawrence Tang     else
22902c801a5SLawrence Tang     {
23002c801a5SLawrence Tang         //Undefined section, show error.
23102c801a5SLawrence Tang         printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n", type);
23202c801a5SLawrence Tang         return 0;
23302c801a5SLawrence Tang     }
23402c801a5SLawrence Tang 
23502c801a5SLawrence Tang     return length;
23602c801a5SLawrence Tang }