blob: b1b105bd77d7074c9b4595a1d277e00cdcac9be9 [file] [log] [blame]
Lawrence Tang02c801a2022-07-18 14:43:52 +01001/**
Ed Tanousfedd4572024-07-12 13:56:00 -07002 * Describes functions for generating pseudo-random specification compliant CPER records.
3 *
Lawrence Tang02c801a2022-07-18 14:43:52 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
Ed Tanous50b966f2025-03-11 09:06:19 -070010#include <libcper/log.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000011#include <libcper/Cper.h>
12#include <libcper/generator/gen-utils.h>
13#include <libcper/generator/sections/gen-section.h>
14#include <libcper/generator/cper-generate.h>
Lawrence Tang02c801a2022-07-18 14:43:52 +010015
Lawrence Tange407b4c2022-07-21 13:54:01 +010016EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
Ed Tanousb35d9572024-06-18 13:17:22 -070017 const size_t *lengths,
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 int index,
19 int num_sections);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080020size_t generate_section(void **location, char *type,
21 GEN_VALID_BITS_TEST_TYPE validBitsType);
Lawrence Tang02c801a2022-07-18 14:43:52 +010022
Lawrence Tangd34f2b12022-07-19 15:36:31 +010023//Generates a CPER record with the given section types, outputting to the given stream.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080024void generate_cper_record(char **types, UINT16 num_sections, FILE *out,
25 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang02c801a2022-07-18 14:43:52 +010026{
Lawrence Tange407b4c2022-07-21 13:54:01 +010027 //Initialise randomiser.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080028 init_random(0);
Lawrence Tang02c801a2022-07-18 14:43:52 +010029
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 //Generate the sections.
31 void *sections[num_sections];
32 size_t section_lengths[num_sections];
33 for (int i = 0; i < num_sections; i++) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080034 section_lengths[i] =
35 generate_section(sections + i, types[i], validBitsType);
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 if (section_lengths[i] == 0) {
37 //Error encountered, exit.
38 printf("Error encountered generating section %d of type '%s', length returned zero.\n",
39 i + 1, types[i]);
40 return;
41 }
42 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010043
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 //Generate the header given the number of sections.
45 EFI_COMMON_ERROR_RECORD_HEADER *header =
46 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc(
47 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
48 header->SignatureStart = 0x52455043; //CPER
49 header->SectionCount = num_sections;
50 header->SignatureEnd = 0xFFFFFFFF;
51 header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
52 header->RecordID = (UINT64)rand();
53 header->ErrorSeverity = rand() % 4;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010054
Lawrence Tange407b4c2022-07-21 13:54:01 +010055 //Generate a valid timestamp.
56 header->TimeStamp.Century = int_to_bcd(rand() % 100);
57 header->TimeStamp.Year = int_to_bcd(rand() % 100);
58 header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1);
59 header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1);
60 header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1);
61 header->TimeStamp.Seconds = int_to_bcd(rand() % 60);
Lawrence Tangaacf0e22022-07-20 13:28:52 +010062
Lawrence Tange407b4c2022-07-21 13:54:01 +010063 //Turn all validation bits on.
John Chungf8fc7052024-05-03 20:05:29 +080064 header->ValidationBits = 0x3;
Lawrence Tang02c801a2022-07-18 14:43:52 +010065
Lawrence Tange407b4c2022-07-21 13:54:01 +010066 //Generate the section descriptors given the number of sections.
67 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections];
John Chungf8fc7052024-05-03 20:05:29 +080068 for (int i = 0; i < num_sections; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010069 section_descriptors[i] = generate_section_descriptor(
70 types[i], section_lengths, i, num_sections);
John Chungf8fc7052024-05-03 20:05:29 +080071 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010072
Lawrence Tange407b4c2022-07-21 13:54:01 +010073 //Calculate total length of structure, set in header.
74 size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
John Chungf8fc7052024-05-03 20:05:29 +080075 for (int i = 0; i < num_sections; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010076 total_len += section_lengths[i];
John Chungf8fc7052024-05-03 20:05:29 +080077 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
79 header->RecordLength = (UINT32)total_len;
Lawrence Tang02c801a2022-07-18 14:43:52 +010080
Lawrence Tange407b4c2022-07-21 13:54:01 +010081 //Write to stream in order, free all resources.
82 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
83 fflush(out);
84 free(header);
85 for (int i = 0; i < num_sections; i++) {
86 fwrite(section_descriptors[i],
87 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
88 fflush(out);
89 free(section_descriptors[i]);
90 }
91 for (int i = 0; i < num_sections; i++) {
92 fwrite(sections[i], section_lengths[i], 1, out);
93 fflush(out);
94 free(sections[i]);
95 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010096}
97
Lawrence Tang617949e2022-08-08 14:21:42 +010098//Generates a single section record for the given section, and outputs to file.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080099void generate_single_section_record(char *type, FILE *out,
100 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang617949e2022-08-08 14:21:42 +0100101{
102 //Generate a section.
John Chungf8fc7052024-05-03 20:05:29 +0800103 void *section = NULL;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800104 size_t section_len = generate_section(&section, type, validBitsType);
Lawrence Tang617949e2022-08-08 14:21:42 +0100105
106 //Generate a descriptor, correct the offset.
107 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor =
108 generate_section_descriptor(type, &section_len, 0, 1);
109 section_descriptor->SectionOffset =
110 sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
111
112 //Write all to file.
113 fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
114 out);
115 fwrite(section, section_len, 1, out);
116 fflush(out);
117
118 //Free remaining resources.
119 free(section_descriptor);
John Chungf8fc7052024-05-03 20:05:29 +0800120 free(section);
Lawrence Tang617949e2022-08-08 14:21:42 +0100121}
122
Lawrence Tang02c801a2022-07-18 14:43:52 +0100123//Generates a single section descriptor for a section with the given properties.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100124EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
Ed Tanousb35d9572024-06-18 13:17:22 -0700125 const size_t *lengths,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 int index,
127 int num_sections)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100128{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100129 EFI_ERROR_SECTION_DESCRIPTOR *descriptor =
130 (EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes(
131 sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
Lawrence Tang02c801a2022-07-18 14:43:52 +0100132
Lawrence Tange407b4c2022-07-21 13:54:01 +0100133 //Set reserved bits to zero.
134 descriptor->Resv1 = 0;
135 descriptor->SectionFlags &= 0xFF;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100136
Lawrence Tange407b4c2022-07-21 13:54:01 +0100137 //Validation bits all set to 'on'.
John Chungf8fc7052024-05-03 20:05:29 +0800138 descriptor->SecValidMask = 0x3;
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100139
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 //Set severity.
141 descriptor->Severity = rand() % 4;
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Set length, offset from base record.
144 descriptor->SectionLength = (UINT32)lengths[index];
145 descriptor->SectionOffset =
146 sizeof(EFI_COMMON_ERROR_RECORD_HEADER) +
147 (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
John Chungf8fc7052024-05-03 20:05:29 +0800148 for (int i = 0; i < index; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100149 descriptor->SectionOffset += lengths[i];
John Chungf8fc7052024-05-03 20:05:29 +0800150 }
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100151
Lawrence Tange407b4c2022-07-21 13:54:01 +0100152 //Ensure the FRU text is not null terminated early.
153 for (int i = 0; i < 20; i++) {
Aushim Nagarkattia6f070b2025-02-04 11:06:43 -0800154 // FRU string can only be ASCII
155 descriptor->FruString[i] = rand() % 127 + 1;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100156
Lawrence Tange407b4c2022-07-21 13:54:01 +0100157 //Null terminate last byte.
John Chungf8fc7052024-05-03 20:05:29 +0800158 if (i == 19) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100159 descriptor->FruString[i] = 0x0;
John Chungf8fc7052024-05-03 20:05:29 +0800160 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100161 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100162
Lawrence Tang8f977452022-08-24 14:55:07 +0100163 //If section type is not "unknown", set section type GUID based on type name.
164 int section_guid_found = 0;
165 if (strcmp(type, "unknown") == 0) {
166 section_guid_found = 1;
167 } else {
168 //Find the appropriate GUID for this section name.
John Chungf8fc7052024-05-03 20:05:29 +0800169 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang8f977452022-08-24 14:55:07 +0100170 if (strcmp(type, generator_definitions[i].ShortName) ==
171 0) {
172 memcpy(&descriptor->SectionType,
173 generator_definitions[i].Guid,
174 sizeof(EFI_GUID));
175 section_guid_found = 1;
176 break;
177 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100178 }
Lawrence Tang8f977452022-08-24 14:55:07 +0100179 }
180
181 //Undefined section, show error.
182 if (!section_guid_found) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100183 //Undefined section, show error.
184 printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n",
185 type);
186 return 0;
187 }
188
189 return descriptor;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100190}
191
192//Generates a single CPER section given the string type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800193size_t generate_section(void **location, char *type,
194 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100195{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 //The length of the section.
197 size_t length = 0;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100198
Lawrence Tang8f977452022-08-24 14:55:07 +0100199 //If the section name is "unknown", simply generate a random bytes section.
200 int section_generated = 0;
201 if (strcmp(type, "unknown") == 0) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100202 length = generate_random_section(location, rand() % 256);
Lawrence Tang8f977452022-08-24 14:55:07 +0100203 section_generated = 1;
204 } else {
205 //Function defined section, switch on the type, generate accordingly.
John Chungf8fc7052024-05-03 20:05:29 +0800206 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang8f977452022-08-24 14:55:07 +0100207 if (strcmp(type, generator_definitions[i].ShortName) ==
208 0) {
209 length = generator_definitions[i].Generate(
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800210 location, validBitsType);
Lawrence Tang8f977452022-08-24 14:55:07 +0100211 section_generated = 1;
212 break;
213 }
214 }
215 }
216
217 //If we didn't find a section generator for the given name, error out.
218 if (!section_generated) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100219 printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n",
220 type);
221 return 0;
222 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100223
Lawrence Tange407b4c2022-07-21 13:54:01 +0100224 return length;
John Chungf8fc7052024-05-03 20:05:29 +0800225}