xref: /openbmc/libcper/generator/cper-generate.c (revision bd1814de02a5eb0e4cd4e4bd4f1210d460f9b6f5)
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/log.h>
11 #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>
15 
16 EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
17 							  const size_t *lengths,
18 							  int index,
19 							  int num_sections);
20 size_t generate_section(void **location, char *type,
21 			GEN_VALID_BITS_TEST_TYPE validBitsType);
22 
23 //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)24 void generate_cper_record(char **types, UINT16 num_sections, FILE *out,
25 			  GEN_VALID_BITS_TEST_TYPE validBitsType)
26 {
27 	//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] =
32 			generate_section(sections + i, types[i], validBitsType);
33 		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 	}
40 
41 	//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;
51 
52 	//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);
59 
60 	//Turn all validation bits on.
61 	header->ValidationBits = 0x3;
62 
63 	//Generate the section descriptors given the number of sections.
64 	EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections];
65 	for (int i = 0; i < num_sections; i++) {
66 		section_descriptors[i] = generate_section_descriptor(
67 			types[i], section_lengths, i, num_sections);
68 	}
69 
70 	//Calculate total length of structure, set in header.
71 	size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
72 	for (int i = 0; i < num_sections; i++) {
73 		total_len += section_lengths[i];
74 	}
75 	total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
76 	header->RecordLength = (UINT32)total_len;
77 
78 	//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 	}
93 }
94 
95 //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)96 void generate_single_section_record(char *type, FILE *out,
97 				    GEN_VALID_BITS_TEST_TYPE validBitsType)
98 {
99 	//Generate a section.
100 	void *section = NULL;
101 	size_t section_len = generate_section(&section, type, validBitsType);
102 
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);
117 	free(section);
118 }
119 
120 //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)121 EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type,
122 							  const size_t *lengths,
123 							  int index,
124 							  int num_sections)
125 {
126 	EFI_ERROR_SECTION_DESCRIPTOR *descriptor =
127 		(EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes(
128 			sizeof(EFI_ERROR_SECTION_DESCRIPTOR));
129 
130 	//Set reserved bits to zero.
131 	descriptor->Resv1 = 0;
132 	descriptor->SectionFlags &= 0xFF;
133 
134 	//Validation bits all set to 'on'.
135 	descriptor->SecValidMask = 0x3;
136 
137 	//Set severity.
138 	descriptor->Severity = rand() % 4;
139 
140 	//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));
145 	for (int i = 0; i < index; i++) {
146 		descriptor->SectionOffset += lengths[i];
147 	}
148 
149 	//Ensure the FRU text is not null terminated early.
150 	for (int i = 0; i < 20; i++) {
151 		// FRU string can only be printable ASCII
152 		descriptor->FruString[i] = rand() % (0x7f - 0x20) + 0x20;
153 
154 		//Null terminate last byte.
155 		if (i == 19) {
156 			descriptor->FruString[i] = 0x0;
157 		}
158 	}
159 
160 	//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.
166 		for (size_t i = 0; i < generator_definitions_len; i++) {
167 			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 			}
175 		}
176 	}
177 
178 	//Undefined section, show error.
179 	if (!section_guid_found) {
180 		//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;
187 }
188 
189 //Generates a single CPER section given the string type.
generate_section(void ** location,char * type,GEN_VALID_BITS_TEST_TYPE validBitsType)190 size_t generate_section(void **location, char *type,
191 			GEN_VALID_BITS_TEST_TYPE validBitsType)
192 {
193 	//The length of the section.
194 	size_t length = 0;
195 
196 	//If the section name is "unknown", simply generate a random bytes section.
197 	int section_generated = 0;
198 	if (strcmp(type, "unknown") == 0) {
199 		length = generate_random_section(location, ALL_VALID);
200 		section_generated = 1;
201 	} else {
202 		//Function defined section, switch on the type, generate accordingly.
203 		for (size_t i = 0; i < generator_definitions_len; i++) {
204 			if (strcmp(type, generator_definitions[i].ShortName) ==
205 			    0) {
206 				length = generator_definitions[i].Generate(
207 					location, validBitsType);
208 				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) {
216 		printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n",
217 		       type);
218 		return 0;
219 	}
220 
221 	return length;
222 }
223