blob: 8675a7880f2c1ea14b146cc3035092b8c48ff60c [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>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/Cper.h>
11#include <libcper/generator/gen-utils.h>
12#include <libcper/generator/sections/gen-section.h>
13#include <libcper/generator/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,
Ed Tanousb35d9572024-06-18 13:17:22 -070016 const size_t *lengths,
Lawrence Tange407b4c2022-07-21 13:54:01 +010017 int index,
18 int num_sections);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080019size_t generate_section(void **location, char *type,
20 GEN_VALID_BITS_TEST_TYPE validBitsType);
Lawrence Tang02c801a2022-07-18 14:43:52 +010021
Lawrence Tangd34f2b12022-07-19 15:36:31 +010022//Generates a CPER record with the given section types, outputting to the given stream.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080023void generate_cper_record(char **types, UINT16 num_sections, FILE *out,
24 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang02c801a2022-07-18 14:43:52 +010025{
Lawrence Tange407b4c2022-07-21 13:54:01 +010026 //Initialise randomiser.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080027 init_random(0);
Lawrence Tang02c801a2022-07-18 14:43:52 +010028
Lawrence Tange407b4c2022-07-21 13:54:01 +010029 //Generate the sections.
30 void *sections[num_sections];
31 size_t section_lengths[num_sections];
32 for (int i = 0; i < num_sections; i++) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080033 section_lengths[i] =
34 generate_section(sections + i, types[i], validBitsType);
Lawrence Tange407b4c2022-07-21 13:54:01 +010035 if (section_lengths[i] == 0) {
36 //Error encountered, exit.
37 printf("Error encountered generating section %d of type '%s', length returned zero.\n",
38 i + 1, types[i]);
39 return;
40 }
41 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010042
Lawrence Tange407b4c2022-07-21 13:54:01 +010043 //Generate the header given the number of sections.
44 EFI_COMMON_ERROR_RECORD_HEADER *header =
45 (EFI_COMMON_ERROR_RECORD_HEADER *)calloc(
46 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER));
47 header->SignatureStart = 0x52455043; //CPER
48 header->SectionCount = num_sections;
49 header->SignatureEnd = 0xFFFFFFFF;
50 header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED
51 header->RecordID = (UINT64)rand();
52 header->ErrorSeverity = rand() % 4;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010053
Lawrence Tange407b4c2022-07-21 13:54:01 +010054 //Generate a valid timestamp.
55 header->TimeStamp.Century = int_to_bcd(rand() % 100);
56 header->TimeStamp.Year = int_to_bcd(rand() % 100);
57 header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1);
58 header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1);
59 header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1);
60 header->TimeStamp.Seconds = int_to_bcd(rand() % 60);
Lawrence Tangaacf0e22022-07-20 13:28:52 +010061
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 //Turn all validation bits on.
John Chungf8fc7052024-05-03 20:05:29 +080063 header->ValidationBits = 0x3;
Lawrence Tang02c801a2022-07-18 14:43:52 +010064
Lawrence Tange407b4c2022-07-21 13:54:01 +010065 //Generate the section descriptors given the number of sections.
66 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections];
John Chungf8fc7052024-05-03 20:05:29 +080067 for (int i = 0; i < num_sections; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 section_descriptors[i] = generate_section_descriptor(
69 types[i], section_lengths, i, num_sections);
John Chungf8fc7052024-05-03 20:05:29 +080070 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010071
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 //Calculate total length of structure, set in header.
73 size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
John Chungf8fc7052024-05-03 20:05:29 +080074 for (int i = 0; i < num_sections; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 total_len += section_lengths[i];
John Chungf8fc7052024-05-03 20:05:29 +080076 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010077 total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
78 header->RecordLength = (UINT32)total_len;
Lawrence Tang02c801a2022-07-18 14:43:52 +010079
Lawrence Tange407b4c2022-07-21 13:54:01 +010080 //Write to stream in order, free all resources.
81 fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out);
82 fflush(out);
83 free(header);
84 for (int i = 0; i < num_sections; i++) {
85 fwrite(section_descriptors[i],
86 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out);
87 fflush(out);
88 free(section_descriptors[i]);
89 }
90 for (int i = 0; i < num_sections; i++) {
91 fwrite(sections[i], section_lengths[i], 1, out);
92 fflush(out);
93 free(sections[i]);
94 }
Lawrence Tang02c801a2022-07-18 14:43:52 +010095}
96
Lawrence Tang617949e2022-08-08 14:21:42 +010097//Generates a single section record for the given section, and outputs to file.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080098void generate_single_section_record(char *type, FILE *out,
99 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang617949e2022-08-08 14:21:42 +0100100{
101 //Generate a section.
John Chungf8fc7052024-05-03 20:05:29 +0800102 void *section = NULL;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800103 size_t section_len = generate_section(&section, type, validBitsType);
Lawrence Tang617949e2022-08-08 14:21:42 +0100104
105 //Generate a descriptor, correct the offset.
106 EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor =
107 generate_section_descriptor(type, &section_len, 0, 1);
108 section_descriptor->SectionOffset =
109 sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
110
111 //Write all to file.
112 fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
113 out);
114 fwrite(section, section_len, 1, out);
115 fflush(out);
116
117 //Free remaining resources.
118 free(section_descriptor);
John Chungf8fc7052024-05-03 20:05:29 +0800119 free(section);
Lawrence Tang617949e2022-08-08 14:21:42 +0100120}
121
Lawrence Tang02c801a2022-07-18 14:43:52 +0100122//Generates a single section descriptor for a section with the given properties.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100123EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
Ed Tanousb35d9572024-06-18 13:17:22 -0700124 const size_t *lengths,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100125 int index,
126 int num_sections)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100127{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100128 EFI_ERROR_SECTION_DESCRIPTOR *descriptor =
129 (EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes(
130 sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
Lawrence Tang02c801a2022-07-18 14:43:52 +0100131
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 //Set reserved bits to zero.
133 descriptor->Resv1 = 0;
134 descriptor->SectionFlags &= 0xFF;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100135
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 //Validation bits all set to 'on'.
John Chungf8fc7052024-05-03 20:05:29 +0800137 descriptor->SecValidMask = 0x3;
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100138
Lawrence Tange407b4c2022-07-21 13:54:01 +0100139 //Set severity.
140 descriptor->Severity = rand() % 4;
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100141
Lawrence Tange407b4c2022-07-21 13:54:01 +0100142 //Set length, offset from base record.
143 descriptor->SectionLength = (UINT32)lengths[index];
144 descriptor->SectionOffset =
145 sizeof(EFI_COMMON_ERROR_RECORD_HEADER) +
146 (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
John Chungf8fc7052024-05-03 20:05:29 +0800147 for (int i = 0; i < index; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 descriptor->SectionOffset += lengths[i];
John Chungf8fc7052024-05-03 20:05:29 +0800149 }
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100150
Lawrence Tange407b4c2022-07-21 13:54:01 +0100151 //Ensure the FRU text is not null terminated early.
152 for (int i = 0; i < 20; i++) {
Aushim Nagarkattia6f070b2025-02-04 11:06:43 -0800153 // FRU string can only be ASCII
154 descriptor->FruString[i] = rand() % 127 + 1;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100155
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 //Null terminate last byte.
John Chungf8fc7052024-05-03 20:05:29 +0800157 if (i == 19) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100158 descriptor->FruString[i] = 0x0;
John Chungf8fc7052024-05-03 20:05:29 +0800159 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100161
Lawrence Tang8f977452022-08-24 14:55:07 +0100162 //If section type is not "unknown", set section type GUID based on type name.
163 int section_guid_found = 0;
164 if (strcmp(type, "unknown") == 0) {
165 section_guid_found = 1;
166 } else {
167 //Find the appropriate GUID for this section name.
John Chungf8fc7052024-05-03 20:05:29 +0800168 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang8f977452022-08-24 14:55:07 +0100169 if (strcmp(type, generator_definitions[i].ShortName) ==
170 0) {
171 memcpy(&descriptor->SectionType,
172 generator_definitions[i].Guid,
173 sizeof(EFI_GUID));
174 section_guid_found = 1;
175 break;
176 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100177 }
Lawrence Tang8f977452022-08-24 14:55:07 +0100178 }
179
180 //Undefined section, show error.
181 if (!section_guid_found) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100182 //Undefined section, show error.
183 printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n",
184 type);
185 return 0;
186 }
187
188 return descriptor;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100189}
190
191//Generates a single CPER section given the string type.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800192size_t generate_section(void **location, char *type,
193 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tang02c801a2022-07-18 14:43:52 +0100194{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100195 //The length of the section.
196 size_t length = 0;
Lawrence Tang02c801a2022-07-18 14:43:52 +0100197
Lawrence Tang8f977452022-08-24 14:55:07 +0100198 //If the section name is "unknown", simply generate a random bytes section.
199 int section_generated = 0;
200 if (strcmp(type, "unknown") == 0) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100201 length = generate_random_section(location, rand() % 256);
Lawrence Tang8f977452022-08-24 14:55:07 +0100202 section_generated = 1;
203 } else {
204 //Function defined section, switch on the type, generate accordingly.
John Chungf8fc7052024-05-03 20:05:29 +0800205 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tang8f977452022-08-24 14:55:07 +0100206 if (strcmp(type, generator_definitions[i].ShortName) ==
207 0) {
208 length = generator_definitions[i].Generate(
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800209 location, validBitsType);
Lawrence Tang8f977452022-08-24 14:55:07 +0100210 section_generated = 1;
211 break;
212 }
213 }
214 }
215
216 //If we didn't find a section generator for the given name, error out.
217 if (!section_generated) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100218 printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n",
219 type);
220 return 0;
221 }
Lawrence Tang02c801a2022-07-18 14:43:52 +0100222
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 return length;
John Chungf8fc7052024-05-03 20:05:29 +0800224}