blob: c8819f616d6c264c35aa63fec203c04594fea0ab [file] [log] [blame]
Lawrence Tang02c801a2022-07-18 14:43:52 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * Describes functions for generating pseudo-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 Tang8f977452022-08-24 14:55:07 +010012#include "sections/gen-section.h"
Lawrence Tangd34f2b12022-07-19 15:36:31 +010013#include "cper-generate.h"
Lawrence Tang02c801a2022-07-18 14:43:52 +010014
Lawrence Tange407b4c2022-07-21 13:54:01 +010015EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
16 size_t *lengths,
17 int index,
18 int num_sections);
19size_t generate_section(void **location, char *type);
Lawrence Tang02c801a2022-07-18 14:43:52 +010020
Lawrence Tangd34f2b12022-07-19 15:36:31 +010021//Generates a CPER record with the given section types, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010022void generate_cper_record(char **types, UINT16 num_sections, FILE *out)
Lawrence Tang02c801a2022-07-18 14:43:52 +010023{
Lawrence Tange407b4c2022-07-21 13:54:01 +010024 //Initialise randomiser.
25 init_random();
Lawrence Tang02c801a2022-07-18 14:43:52 +010026
Lawrence Tange407b4c2022-07-21 13:54:01 +010027 //Generate the sections.
28 void *sections[num_sections];
29 size_t section_lengths[num_sections];
30 for (int i = 0; i < num_sections; i++) {
31 section_lengths[i] = generate_section(sections + i, types[i]);
32 if (section_lengths[i] == 0) {
33 //Error encountered, exit.
34 printf("Error encountered generating section %d of type '%s', length returned zero.\n",
35 i + 1, types[i]);
36 return;
37 }
38 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010039
Lawrence Tange407b4c2022-07-21 13:54:01 +010040 //Generate the header given the number of sections.
41 EFI_COMMON_ERROR_RECORD_HEADER *header =
42 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc(
43 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
44 header->SignatureStart = 0x52455043; //CPER
45 header->SectionCount = num_sections;
46 header->SignatureEnd = 0xFFFFFFFF;
47 header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
48 header->RecordID = (UINT64)rand();
49 header->ErrorSeverity = rand() % 4;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010050
Lawrence Tange407b4c2022-07-21 13:54:01 +010051 //Generate a valid timestamp.
52 header->TimeStamp.Century = int_to_bcd(rand() % 100);
53 header->TimeStamp.Year = int_to_bcd(rand() % 100);
54 header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1);
55 header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1);
56 header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1);
57 header->TimeStamp.Seconds = int_to_bcd(rand() % 60);
Lawrence Tangaacf0e22022-07-20 13:28:52 +010058
Lawrence Tange407b4c2022-07-21 13:54:01 +010059 //Turn all validation bits on.
60 header->ValidationBits = 0b11;
Lawrence Tang02c801a2022-07-18 14:43:52 +010061
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 //Generate the section descriptors given the number of sections.
63 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections];
64 for (int i = 0; i < num_sections; i++)
65 section_descriptors[i] = generate_section_descriptor(
66 types[i], section_lengths, i, num_sections);
Lawrence Tang02c801a2022-07-18 14:43:52 +010067
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 //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;
Lawrence Tang02c801a2022-07-18 14:43:52 +010074
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 //Write to stream in order, free all resources.
76 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
77 fflush(out);
78 free(header);
79 for (int i = 0; i < num_sections; i++) {
80 fwrite(section_descriptors[i],
81 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
82 fflush(out);
83 free(section_descriptors[i]);
84 }
85 for (int i = 0; i < num_sections; i++) {
86 fwrite(sections[i], section_lengths[i], 1, out);
87 fflush(out);
88 free(sections[i]);
89 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010090}
91
Lawrence Tang617949e2022-08-08 14:21:42 +010092//Generates a single section record for the given section, and outputs to file.
93void generate_single_section_record(char *type, FILE *out)
94{
95 //Generate a section.
96 void *section;
97 size_t section_len = generate_section(&section, type);
98
99 //Generate a descriptor, correct the offset.
100 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor =
101 generate_section_descriptor(type, &section_len, 0, 1);
102 section_descriptor->SectionOffset =
103 sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
104
105 //Write all to file.
106 fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
107 out);
108 fwrite(section, section_len, 1, out);
109 fflush(out);
110
111 //Free remaining resources.
112 free(section_descriptor);
113}
114
Lawrence Tang02c801a2022-07-18 14:43:52 +0100115//Generates a single section descriptor for a section with the given properties.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
117 size_t *lengths,
118 int index,
119 int num_sections)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100120{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 EFI_ERROR_SECTION_DESCRIPTOR *descriptor =
122 (EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes(
123 sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
Lawrence Tang02c801a2022-07-18 14:43:52 +0100124
Lawrence Tange407b4c2022-07-21 13:54:01 +0100125 //Set reserved bits to zero.
126 descriptor->Resv1 = 0;
127 descriptor->SectionFlags &= 0xFF;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100128
Lawrence Tange407b4c2022-07-21 13:54:01 +0100129 //Validation bits all set to 'on'.
130 descriptor->SecValidMask = 0b11;
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100131
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 //Set severity.
133 descriptor->Severity = rand() % 4;
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100134
Lawrence Tange407b4c2022-07-21 13:54:01 +0100135 //Set length, offset from base record.
136 descriptor->SectionLength = (UINT32)lengths[index];
137 descriptor->SectionOffset =
138 sizeof(EFI_COMMON_ERROR_RECORD_HEADER) +
139 (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
140 for (int i = 0; i < index; i++)
141 descriptor->SectionOffset += lengths[i];
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Ensure the FRU text is not null terminated early.
144 for (int i = 0; i < 20; i++) {
145 if (descriptor->FruString[i] == 0x0)
146 descriptor->FruString[i] = rand() % 127 + 1;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100147
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 //Null terminate last byte.
149 if (i == 19)
150 descriptor->FruString[i] = 0x0;
151 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100152
Lawrence Tang8f977452022-08-24 14:55:07 +0100153 //If section type is not "unknown", set section type GUID based on type name.
154 int section_guid_found = 0;
155 if (strcmp(type, "unknown") == 0) {
156 section_guid_found = 1;
157 } else {
158 //Find the appropriate GUID for this section name.
159 for (int i = 0; i < generator_definitions_len; i++) {
160 if (strcmp(type, generator_definitions[i].ShortName) ==
161 0) {
162 memcpy(&descriptor->SectionType,
163 generator_definitions[i].Guid,
164 sizeof(EFI_GUID));
165 section_guid_found = 1;
166 break;
167 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100168 }
Lawrence Tang8f977452022-08-24 14:55:07 +0100169 }
170
171 //Undefined section, show error.
172 if (!section_guid_found) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100173 //Undefined section, show error.
174 printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n",
175 type);
176 return 0;
177 }
178
179 return descriptor;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100180}
181
182//Generates a single CPER section given the string type.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100183size_t generate_section(void **location, char *type)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100184{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100185 //The length of the section.
186 size_t length = 0;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100187
Lawrence Tang8f977452022-08-24 14:55:07 +0100188 //If the section name is "unknown", simply generate a random bytes section.
189 int section_generated = 0;
190 if (strcmp(type, "unknown") == 0) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100191 length = generate_random_section(location, rand() % 256);
Lawrence Tang8f977452022-08-24 14:55:07 +0100192 section_generated = 1;
193 } else {
194 //Function defined section, switch on the type, generate accordingly.
195 for (int i = 0; i < generator_definitions_len; i++) {
196 if (strcmp(type, generator_definitions[i].ShortName) ==
197 0) {
198 length = generator_definitions[i].Generate(
199 location);
200 section_generated = 1;
201 break;
202 }
203 }
204 }
205
206 //If we didn't find a section generator for the given name, error out.
207 if (!section_generated) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100208 printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n",
209 type);
210 return 0;
211 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100212
Lawrence Tange407b4c2022-07-21 13:54:01 +0100213 return length;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100214}