blob: b7f95967f3d5385ae985c4268508b57a62b51c58 [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 //Generate the sections.
28 void *sections[num_sections];
29 size_t section_lengths[num_sections];
30 for (int i = 0; i < num_sections; i++) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080031 section_lengths[i] =
32 generate_section(sections + i, types[i], validBitsType);
Lawrence Tange407b4c2022-07-21 13:54:01 +010033 if (section_lengths[i] == 0) {
34 //Error encountered, exit.
35 printf("Error encountered generating section %d of type '%s', length returned zero.\n",
36 i + 1, types[i]);
37 return;
38 }
39 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010040
Lawrence Tange407b4c2022-07-21 13:54:01 +010041 //Generate the header given the number of sections.
42 EFI_COMMON_ERROR_RECORD_HEADER *header =
43 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc(
44 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
45 header->SignatureStart = 0x52455043; //CPER
46 header->SectionCount = num_sections;
47 header->SignatureEnd = 0xFFFFFFFF;
48 header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
49 header->RecordID = (UINT64)rand();
50 header->ErrorSeverity = rand() % 4;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010051
Lawrence Tange407b4c2022-07-21 13:54:01 +010052 //Generate a valid timestamp.
53 header->TimeStamp.Century = int_to_bcd(rand() % 100);
54 header->TimeStamp.Year = int_to_bcd(rand() % 100);
55 header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1);
56 header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1);
57 header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1);
58 header->TimeStamp.Seconds = int_to_bcd(rand() % 60);
Lawrence Tangaacf0e22022-07-20 13:28:52 +010059
Lawrence Tange407b4c2022-07-21 13:54:01 +010060 //Turn all validation bits on.
John Chungf8fc7052024-05-03 20:05:29 +080061 header->ValidationBits = 0x3;
Lawrence Tang02c801a2022-07-18 14:43:52 +010062
Lawrence Tange407b4c2022-07-21 13:54:01 +010063 //Generate the section descriptors given the number of sections.
64 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections];
John Chungf8fc7052024-05-03 20:05:29 +080065 for (int i = 0; i < num_sections; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010066 section_descriptors[i] = generate_section_descriptor(
67 types[i], section_lengths, i, num_sections);
John Chungf8fc7052024-05-03 20:05:29 +080068 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010069
Lawrence Tange407b4c2022-07-21 13:54:01 +010070 //Calculate total length of structure, set in header.
71 size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
John Chungf8fc7052024-05-03 20:05:29 +080072 for (int i = 0; i < num_sections; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010073 total_len += section_lengths[i];
John Chungf8fc7052024-05-03 20:05:29 +080074 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
76 header->RecordLength = (UINT32)total_len;
Lawrence Tang02c801a2022-07-18 14:43:52 +010077
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 //Write to stream in order, free all resources.
79 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
80 fflush(out);
81 free(header);
82 for (int i = 0; i < num_sections; i++) {
83 fwrite(section_descriptors[i],
84 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
85 fflush(out);
86 free(section_descriptors[i]);
87 }
88 for (int i = 0; i < num_sections; i++) {
89 fwrite(sections[i], section_lengths[i], 1, out);
90 fflush(out);
91 free(sections[i]);
92 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010093}
94
Lawrence Tang617949e2022-08-08 14:21:42 +010095//Generates a single section record for the given section, and outputs to file.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080096void generate_single_section_record(char *type, FILE *out,
97 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang617949e2022-08-08 14:21:42 +010098{
99 //Generate a section.
John Chungf8fc7052024-05-03 20:05:29 +0800100 void *section = NULL;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800101 size_t section_len = generate_section(&section, type, validBitsType);
Lawrence Tang617949e2022-08-08 14:21:42 +0100102
103 //Generate a descriptor, correct the offset.
104 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor =
105 generate_section_descriptor(type, &section_len, 0, 1);
106 section_descriptor->SectionOffset =
107 sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
108
109 //Write all to file.
110 fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
111 out);
112 fwrite(section, section_len, 1, out);
113 fflush(out);
114
115 //Free remaining resources.
116 free(section_descriptor);
John Chungf8fc7052024-05-03 20:05:29 +0800117 free(section);
Lawrence Tang617949e2022-08-08 14:21:42 +0100118}
119
Lawrence Tang02c801a2022-07-18 14:43:52 +0100120//Generates a single section descriptor for a section with the given properties.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
Ed Tanousb35d9572024-06-18 13:17:22 -0700122 const size_t *lengths,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100123 int index,
124 int num_sections)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100125{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 EFI_ERROR_SECTION_DESCRIPTOR *descriptor =
127 (EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes(
128 sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
Lawrence Tang02c801a2022-07-18 14:43:52 +0100129
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 //Set reserved bits to zero.
131 descriptor->Resv1 = 0;
132 descriptor->SectionFlags &= 0xFF;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100133
Lawrence Tange407b4c2022-07-21 13:54:01 +0100134 //Validation bits all set to 'on'.
John Chungf8fc7052024-05-03 20:05:29 +0800135 descriptor->SecValidMask = 0x3;
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100136
Lawrence Tange407b4c2022-07-21 13:54:01 +0100137 //Set severity.
138 descriptor->Severity = rand() % 4;
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100139
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 //Set length, offset from base record.
141 descriptor->SectionLength = (UINT32)lengths[index];
142 descriptor->SectionOffset =
143 sizeof(EFI_COMMON_ERROR_RECORD_HEADER) +
144 (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
John Chungf8fc7052024-05-03 20:05:29 +0800145 for (int i = 0; i < index; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100146 descriptor->SectionOffset += lengths[i];
John Chungf8fc7052024-05-03 20:05:29 +0800147 }
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100148
Lawrence Tange407b4c2022-07-21 13:54:01 +0100149 //Ensure the FRU text is not null terminated early.
150 for (int i = 0; i < 20; i++) {
Khang D Nguyenbd1814d2025-03-31 13:07:49 +0700151 // FRU string can only be printable ASCII
152 descriptor->FruString[i] = rand() % (0x7f - 0x20) + 0x20;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100153
Lawrence Tange407b4c2022-07-21 13:54:01 +0100154 //Null terminate last byte.
John Chungf8fc7052024-05-03 20:05:29 +0800155 if (i == 19) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 descriptor->FruString[i] = 0x0;
John Chungf8fc7052024-05-03 20:05:29 +0800157 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100158 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100159
Lawrence Tang8f977452022-08-24 14:55:07 +0100160 //If section type is not "unknown", set section type GUID based on type name.
161 int section_guid_found = 0;
162 if (strcmp(type, "unknown") == 0) {
163 section_guid_found = 1;
164 } else {
165 //Find the appropriate GUID for this section name.
John Chungf8fc7052024-05-03 20:05:29 +0800166 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang8f977452022-08-24 14:55:07 +0100167 if (strcmp(type, generator_definitions[i].ShortName) ==
168 0) {
169 memcpy(&descriptor->SectionType,
170 generator_definitions[i].Guid,
171 sizeof(EFI_GUID));
172 section_guid_found = 1;
173 break;
174 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100175 }
Lawrence Tang8f977452022-08-24 14:55:07 +0100176 }
177
178 //Undefined section, show error.
179 if (!section_guid_found) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100180 //Undefined section, show error.
181 printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n",
182 type);
183 return 0;
184 }
185
186 return descriptor;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100187}
188
189//Generates a single CPER section given the string type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800190size_t generate_section(void **location, char *type,
191 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100192{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100193 //The length of the section.
194 size_t length = 0;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100195
Lawrence Tang8f977452022-08-24 14:55:07 +0100196 //If the section name is "unknown", simply generate a random bytes section.
197 int section_generated = 0;
198 if (strcmp(type, "unknown") == 0) {
Ed Tanous2d4d3b62025-03-11 10:34:29 -0700199 length = generate_random_section(location, ALL_VALID);
Lawrence Tang8f977452022-08-24 14:55:07 +0100200 section_generated = 1;
201 } else {
202 //Function defined section, switch on the type, generate accordingly.
John Chungf8fc7052024-05-03 20:05:29 +0800203 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang8f977452022-08-24 14:55:07 +0100204 if (strcmp(type, generator_definitions[i].ShortName) ==
205 0) {
206 length = generator_definitions[i].Generate(
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800207 location, validBitsType);
Lawrence Tang8f977452022-08-24 14:55:07 +0100208 section_generated = 1;
209 break;
210 }
211 }
212 }
213
214 //If we didn't find a section generator for the given name, error out.
215 if (!section_generated) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100216 printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n",
217 type);
218 return 0;
219 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100220
Lawrence Tange407b4c2022-07-21 13:54:01 +0100221 return length;
John Chungf8fc7052024-05-03 20:05:29 +0800222}