xref: /openbmc/libcper/cper-parse.c (revision 580423fe)
1 /**
2  * Describes high level functions for converting an entire CPER log, and functions for parsing
3  * CPER headers and section descriptions into an intermediate JSON format.
4  *
5  * Author: Lawrence.Tang@arm.com
6  **/
7 
8 #include <stdio.h>
9 #include <json.h>
10 #include "b64.h"
11 #include "edk/Cper.h"
12 #include "cper-parse.h"
13 #include "cper-utils.h"
14 #include "sections/cper-section.h"
15 
16 //Private pre-definitions.
17 json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header);
18 json_object *
19 cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor);
20 json_object *cper_section_to_ir(FILE *handle,
21 				EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
22 
23 //Reads a CPER log file at the given file location, and returns an intermediate
24 //JSON representation of this CPER record.
25 json_object *cper_to_ir(FILE *cper_file)
26 {
27 	//Ensure this is really a CPER log.
28 	EFI_COMMON_ERROR_RECORD_HEADER header;
29 	fseek(cper_file, 0, SEEK_SET);
30 	if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1,
31 		  cper_file) != 1) {
32 		printf("Invalid CPER file: Invalid length (log too short).\n");
33 		return NULL;
34 	}
35 
36 	//Check if the header contains the magic bytes ("CPER").
37 	if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
38 		printf("Invalid CPER file: Invalid header (incorrect signature).\n");
39 		return NULL;
40 	}
41 
42 	//Create the header JSON object from the read bytes.
43 	json_object *header_ir = cper_header_to_ir(&header);
44 
45 	//Read the appropriate number of section descriptors & sections, and convert them into IR format.
46 	json_object *section_descriptors_ir = json_object_new_array();
47 	json_object *sections_ir = json_object_new_array();
48 	for (int i = 0; i < header.SectionCount; i++) {
49 		//Create the section descriptor.
50 		EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
51 		if (fread(&section_descriptor,
52 			  sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
53 			  cper_file) != 1) {
54 			printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n",
55 			       header.SectionCount, i + 1);
56 			return NULL;
57 		}
58 		json_object_array_add(
59 			section_descriptors_ir,
60 			cper_section_descriptor_to_ir(&section_descriptor));
61 
62 		//Read the section itself.
63 		json_object_array_add(sections_ir,
64 				      cper_section_to_ir(cper_file,
65 							 &section_descriptor));
66 	}
67 
68 	//Add the header, section descriptors, and sections to a parent object.
69 	json_object *parent = json_object_new_object();
70 	json_object_object_add(parent, "header", header_ir);
71 	json_object_object_add(parent, "sectionDescriptors",
72 			       section_descriptors_ir);
73 	json_object_object_add(parent, "sections", sections_ir);
74 
75 	return parent;
76 }
77 
78 //Converts a parsed CPER record header into intermediate JSON object format.
79 json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
80 {
81 	json_object *header_ir = json_object_new_object();
82 
83 	//Revision/version information.
84 	json_object_object_add(header_ir, "revision",
85 			       revision_to_ir(header->Revision));
86 
87 	//Section count.
88 	json_object_object_add(header_ir, "sectionCount",
89 			       json_object_new_int(header->SectionCount));
90 
91 	//Error severity (with interpreted string version).
92 	json_object *error_severity = json_object_new_object();
93 	json_object_object_add(error_severity, "code",
94 			       json_object_new_uint64(header->ErrorSeverity));
95 	json_object_object_add(error_severity, "name",
96 			       json_object_new_string(severity_to_string(
97 				       header->ErrorSeverity)));
98 	json_object_object_add(header_ir, "severity", error_severity);
99 
100 	//The validation bits for each section.
101 	json_object *validation_bits = bitfield_to_ir(
102 		header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
103 	json_object_object_add(header_ir, "validationBits", validation_bits);
104 
105 	//Total length of the record (including headers) in bytes.
106 	json_object_object_add(header_ir, "recordLength",
107 			       json_object_new_uint64(header->RecordLength));
108 
109 	//If a timestamp exists according to validation bits, then add it.
110 	if (header->ValidationBits & 0b10) {
111 		char timestamp_string[TIMESTAMP_LENGTH];
112 		timestamp_to_string(timestamp_string, &header->TimeStamp);
113 
114 		json_object_object_add(
115 			header_ir, "timestamp",
116 			json_object_new_string(timestamp_string));
117 		json_object_object_add(
118 			header_ir, "timestampIsPrecise",
119 			json_object_new_boolean(header->TimeStamp.Flag));
120 	}
121 
122 	//If a platform ID exists according to the validation bits, then add it.
123 	if (header->ValidationBits & 0b1) {
124 		char platform_string[GUID_STRING_LENGTH];
125 		guid_to_string(platform_string, &header->PlatformID);
126 		json_object_object_add(header_ir, "platformID",
127 				       json_object_new_string(platform_string));
128 	}
129 
130 	//If a partition ID exists according to the validation bits, then add it.
131 	if (header->ValidationBits & 0b100) {
132 		char partition_string[GUID_STRING_LENGTH];
133 		guid_to_string(partition_string, &header->PartitionID);
134 		json_object_object_add(
135 			header_ir, "partitionID",
136 			json_object_new_string(partition_string));
137 	}
138 
139 	//Creator ID of the header.
140 	char creator_string[GUID_STRING_LENGTH];
141 	guid_to_string(creator_string, &header->CreatorID);
142 	json_object_object_add(header_ir, "creatorID",
143 			       json_object_new_string(creator_string));
144 
145 	//Notification type for the header. Some defined types are available.
146 	json_object *notification_type = json_object_new_object();
147 	char notification_type_string[GUID_STRING_LENGTH];
148 	guid_to_string(notification_type_string, &header->NotificationType);
149 	json_object_object_add(
150 		notification_type, "guid",
151 		json_object_new_string(notification_type_string));
152 
153 	//Add the human readable notification type if possible.
154 	char *notification_type_readable = "Unknown";
155 	if (guid_equal(&header->NotificationType,
156 		       &gEfiEventNotificationTypeCmcGuid))
157 		notification_type_readable = "CMC";
158 	else if (guid_equal(&header->NotificationType,
159 			    &gEfiEventNotificationTypeCpeGuid))
160 		notification_type_readable = "CPE";
161 	else if (guid_equal(&header->NotificationType,
162 			    &gEfiEventNotificationTypeMceGuid))
163 		notification_type_readable = "MCE";
164 	else if (guid_equal(&header->NotificationType,
165 			    &gEfiEventNotificationTypePcieGuid))
166 		notification_type_readable = "PCIe";
167 	else if (guid_equal(&header->NotificationType,
168 			    &gEfiEventNotificationTypeInitGuid))
169 		notification_type_readable = "INIT";
170 	else if (guid_equal(&header->NotificationType,
171 			    &gEfiEventNotificationTypeNmiGuid))
172 		notification_type_readable = "NMI";
173 	else if (guid_equal(&header->NotificationType,
174 			    &gEfiEventNotificationTypeBootGuid))
175 		notification_type_readable = "Boot";
176 	else if (guid_equal(&header->NotificationType,
177 			    &gEfiEventNotificationTypeDmarGuid))
178 		notification_type_readable = "DMAr";
179 	else if (guid_equal(&header->NotificationType,
180 			    &gEfiEventNotificationTypeSeaGuid))
181 		notification_type_readable = "SEA";
182 	else if (guid_equal(&header->NotificationType,
183 			    &gEfiEventNotificationTypeSeiGuid))
184 		notification_type_readable = "SEI";
185 	else if (guid_equal(&header->NotificationType,
186 			    &gEfiEventNotificationTypePeiGuid))
187 		notification_type_readable = "PEI";
188 	else if (guid_equal(&header->NotificationType,
189 			    &gEfiEventNotificationTypeCxlGuid))
190 		notification_type_readable = "CXL Component";
191 	json_object_object_add(
192 		notification_type, "type",
193 		json_object_new_string(notification_type_readable));
194 	json_object_object_add(header_ir, "notificationType",
195 			       notification_type);
196 
197 	//The record ID for this record, unique on a given system.
198 	json_object_object_add(header_ir, "recordID",
199 			       json_object_new_uint64(header->RecordID));
200 
201 	//Flag for the record, and a human readable form.
202 	json_object *flags = integer_to_readable_pair(
203 		header->Flags,
204 		sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
205 		CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES,
206 		"Unknown");
207 	json_object_object_add(header_ir, "flags", flags);
208 
209 	//Persistence information. Outside the scope of specification, so just a uint32 here.
210 	json_object_object_add(header_ir, "persistenceInfo",
211 			       json_object_new_uint64(header->PersistenceInfo));
212 	return header_ir;
213 }
214 
215 //Converts the given EFI section descriptor into JSON IR format.
216 json_object *
217 cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor)
218 {
219 	json_object *section_descriptor_ir = json_object_new_object();
220 
221 	//The offset of the section from the base of the record header, length.
222 	json_object_object_add(
223 		section_descriptor_ir, "sectionOffset",
224 		json_object_new_uint64(section_descriptor->SectionOffset));
225 	json_object_object_add(
226 		section_descriptor_ir, "sectionLength",
227 		json_object_new_uint64(section_descriptor->SectionLength));
228 
229 	//Revision.
230 	json_object_object_add(section_descriptor_ir, "revision",
231 			       revision_to_ir(section_descriptor->Revision));
232 
233 	//Validation bits.
234 	json_object *validation_bits =
235 		bitfield_to_ir(section_descriptor->SecValidMask, 2,
236 			       CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
237 	json_object_object_add(section_descriptor_ir, "validationBits",
238 			       validation_bits);
239 
240 	//Flag bits.
241 	json_object *flags =
242 		bitfield_to_ir(section_descriptor->SectionFlags, 8,
243 			       CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
244 	json_object_object_add(section_descriptor_ir, "flags", flags);
245 
246 	//Section type (GUID).
247 	json_object *section_type = json_object_new_object();
248 	char section_type_string[GUID_STRING_LENGTH];
249 	guid_to_string(section_type_string, &section_descriptor->SectionType);
250 	json_object_object_add(section_type, "data",
251 			       json_object_new_string(section_type_string));
252 
253 	//Readable section type, if possible.
254 	const char *section_type_readable = "Unknown";
255 	for (int i=0; i<section_definitions_len; i++)
256 	{
257 		if (guid_equal(section_definitions[i].Guid, &section_descriptor->SectionType)) {
258 			section_type_readable = section_definitions[i].ReadableName;
259 			break;
260 		}
261 	}
262 
263 	json_object_object_add(section_type, "type",
264 			       json_object_new_string(section_type_readable));
265 	json_object_object_add(section_descriptor_ir, "sectionType",
266 			       section_type);
267 
268 	//If validation bits indicate it exists, add FRU ID.
269 	if (section_descriptor->SecValidMask & 0b1) {
270 		char fru_id_string[GUID_STRING_LENGTH];
271 		guid_to_string(fru_id_string, &section_descriptor->FruId);
272 		json_object_object_add(section_descriptor_ir, "fruID",
273 				       json_object_new_string(fru_id_string));
274 	}
275 
276 	//If validation bits indicate it exists, add FRU text.
277 	if ((section_descriptor->SecValidMask & 0b10) >> 1)
278 		json_object_object_add(
279 			section_descriptor_ir, "fruText",
280 			json_object_new_string(section_descriptor->FruString));
281 
282 	//Section severity.
283 	json_object *section_severity = json_object_new_object();
284 	json_object_object_add(
285 		section_severity, "code",
286 		json_object_new_uint64(section_descriptor->Severity));
287 	json_object_object_add(section_severity, "name",
288 			       json_object_new_string(severity_to_string(
289 				       section_descriptor->Severity)));
290 	json_object_object_add(section_descriptor_ir, "severity",
291 			       section_severity);
292 
293 	return section_descriptor_ir;
294 }
295 
296 //Converts the section described by a single given section descriptor.
297 json_object *cper_section_to_ir(FILE *handle,
298 				EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
299 {
300 	//Save our current position in the stream.
301 	long position = ftell(handle);
302 
303 	//Read section as described by the section descriptor.
304 	fseek(handle, descriptor->SectionOffset, SEEK_SET);
305 	void *section = malloc(descriptor->SectionLength);
306 	if (fread(section, descriptor->SectionLength, 1, handle) != 1) {
307 		printf("Section read failed: Could not read %d bytes from global offset %d.\n",
308 		       descriptor->SectionLength, descriptor->SectionOffset);
309 		free(section);
310 		return NULL;
311 	}
312 
313 	//Seek back to our original position.
314 	fseek(handle, position, SEEK_SET);
315 
316 	//Parse section to IR based on GUID.
317 	json_object *result = NULL;
318 	int section_converted = 0;
319 	for (int i=0; i<section_definitions_len; i++)
320 	{
321 		if (guid_equal(section_definitions[i].Guid, &descriptor->SectionType) && section_definitions[i].ToIR != NULL) {
322 			result = section_definitions[i].ToIR(section, descriptor);
323 			section_converted = 1;
324 			break;
325 		}
326 	}
327 
328 	//Was it an unknown GUID/failed read?
329 	if (!section_converted) {
330 		//Output the data as formatted base64.
331 		result = json_object_new_object();
332 		char *encoded = b64_encode((unsigned char *)section,
333 					   descriptor->SectionLength);
334 		json_object_object_add(result, "data",
335 				       json_object_new_string(encoded));
336 		free(encoded);
337 	}
338 
339 	//Free section memory, return result.
340 	free(section);
341 	return result;
342 }
343 
344 //Converts a single CPER section, without a header but with a section descriptor, to JSON.
345 json_object *cper_single_section_to_ir(FILE *cper_section_file)
346 {
347 	json_object *ir = json_object_new_object();
348 
349 	//Read the section descriptor out.
350 	EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
351 	if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
352 		  cper_section_file) != 1) {
353 		printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
354 		return NULL;
355 	}
356 
357 	//Convert the section descriptor to IR.
358 	json_object *section_descriptor_ir =
359 		cper_section_descriptor_to_ir(&section_descriptor);
360 	json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
361 
362 	//Parse the single section.
363 	json_object *section_ir =
364 		cper_section_to_ir(cper_section_file, &section_descriptor);
365 	json_object_object_add(ir, "section", section_ir);
366 
367 	return ir;
368 }