xref: /openbmc/libcper/generator/cper-generate.c (revision ae8f6d9aaeaf37332e8924dd2c0b6f320335548c)
1 /**
2  * Describes functions for generating pseudo-random specification compliant CPER records.
3  *
4  * Author: Lawrence.Tang@arm.com
5  **/
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #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>
14 
15 EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
16 							  const size_t *lengths,
17 							  int index,
18 							  int num_sections);
19 size_t generate_section(void **location, char *type,
20 			GEN_VALID_BITS_TEST_TYPE validBitsType);
21 
22 //Generates a CPER record with the given section types, outputting to the given stream.
generate_cper_record(char ** types,UINT16 num_sections,FILE * out,GEN_VALID_BITS_TEST_TYPE validBitsType)23 void generate_cper_record(char **types, UINT16 num_sections, FILE *out,
24 			  GEN_VALID_BITS_TEST_TYPE validBitsType)
25 {
26 	//Initialise randomiser.
27 	init_random(0);
28 
29 	//Generate the sections.
30 	void *sections[num_sections];
31 	size_t section_lengths[num_sections];
32 	for (int i = 0; i < num_sections; i++) {
33 		section_lengths[i] =
34 			generate_section(sections + i, types[i], validBitsType);
35 		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 	}
42 
43 	//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;
53 
54 	//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);
61 
62 	//Turn all validation bits on.
63 	header->ValidationBits = 0x3;
64 
65 	//Generate the section descriptors given the number of sections.
66 	EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections];
67 	for (int i = 0; i < num_sections; i++) {
68 		section_descriptors[i] = generate_section_descriptor(
69 			types[i], section_lengths, i, num_sections);
70 	}
71 
72 	//Calculate total length of structure, set in header.
73 	size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
74 	for (int i = 0; i < num_sections; i++) {
75 		total_len += section_lengths[i];
76 	}
77 	total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
78 	header->RecordLength = (UINT32)total_len;
79 
80 	//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 	}
95 }
96 
97 //Generates a single section record for the given section, and outputs to file.
generate_single_section_record(char * type,FILE * out,GEN_VALID_BITS_TEST_TYPE validBitsType)98 void generate_single_section_record(char *type, FILE *out,
99 				    GEN_VALID_BITS_TEST_TYPE validBitsType)
100 {
101 	//Generate a section.
102 	void *section = NULL;
103 	size_t section_len = generate_section(&section, type, validBitsType);
104 
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);
119 	free(section);
120 }
121 
122 //Generates a single section descriptor for a section with the given properties.
generate_section_descriptor(char * type,const size_t * lengths,int index,int num_sections)123 EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
124 							  const size_t *lengths,
125 							  int index,
126 							  int num_sections)
127 {
128 	EFI_ERROR_SECTION_DESCRIPTOR *descriptor =
129 		(EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes(
130 			sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
131 
132 	//Set reserved bits to zero.
133 	descriptor->Resv1 = 0;
134 	descriptor->SectionFlags &= 0xFF;
135 
136 	//Validation bits all set to 'on'.
137 	descriptor->SecValidMask = 0x3;
138 
139 	//Set severity.
140 	descriptor->Severity = rand() % 4;
141 
142 	//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));
147 	for (int i = 0; i < index; i++) {
148 		descriptor->SectionOffset += lengths[i];
149 	}
150 
151 	//Ensure the FRU text is not null terminated early.
152 	for (int i = 0; i < 20; i++) {
153 		// FRU string can only be ASCII
154 		descriptor->FruString[i] = rand() % 127 + 1;
155 
156 		//Null terminate last byte.
157 		if (i == 19) {
158 			descriptor->FruString[i] = 0x0;
159 		}
160 	}
161 
162 	//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.
168 		for (size_t i = 0; i < generator_definitions_len; i++) {
169 			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 			}
177 		}
178 	}
179 
180 	//Undefined section, show error.
181 	if (!section_guid_found) {
182 		//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;
189 }
190 
191 //Generates a single CPER section given the string type.
generate_section(void ** location,char * type,GEN_VALID_BITS_TEST_TYPE validBitsType)192 size_t generate_section(void **location, char *type,
193 			GEN_VALID_BITS_TEST_TYPE validBitsType)
194 {
195 	//The length of the section.
196 	size_t length = 0;
197 
198 	//If the section name is "unknown", simply generate a random bytes section.
199 	int section_generated = 0;
200 	if (strcmp(type, "unknown") == 0) {
201 		length = generate_random_section(location, rand() % 256);
202 		section_generated = 1;
203 	} else {
204 		//Function defined section, switch on the type, generate accordingly.
205 		for (size_t i = 0; i < generator_definitions_len; i++) {
206 			if (strcmp(type, generator_definitions[i].ShortName) ==
207 			    0) {
208 				length = generator_definitions[i].Generate(
209 					location, validBitsType);
210 				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) {
218 		printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n",
219 		       type);
220 		return 0;
221 	}
222 
223 	return length;
224 }
225