xref: /openbmc/libcper/sections/cper-section-firmware.c (revision 50b966f7afa31fe39fda70e10eb9139cce39e025)
1 /**
2  * Describes functions for converting firmware CPER sections from binary and JSON format
3  * into an intermediate format.
4  *
5  * Author: Lawrence.Tang@arm.com
6  **/
7 #include <stdio.h>
8 #include <json.h>
9 #include <libcper/Cper.h>
10 #include <libcper/cper-utils.h>
11 #include <libcper/sections/cper-section-firmware.h>
12 #include <libcper/log.h>
13 
14 //Converts a single firmware CPER section into JSON IR.
cper_section_firmware_to_ir(const UINT8 * section,UINT32 size)15 json_object *cper_section_firmware_to_ir(const UINT8 *section, UINT32 size)
16 {
17 	if (size < sizeof(EFI_FIRMWARE_ERROR_DATA)) {
18 		return NULL;
19 	}
20 
21 	EFI_FIRMWARE_ERROR_DATA *firmware_error =
22 		(EFI_FIRMWARE_ERROR_DATA *)section;
23 	json_object *section_ir = json_object_new_object();
24 
25 	//Record type.
26 	json_object *record_type = integer_to_readable_pair(
27 		firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS,
28 		FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)");
29 	json_object_object_add(section_ir, "errorRecordType", record_type);
30 
31 	//Revision, record identifier.
32 	json_object_object_add(section_ir, "revision",
33 			       json_object_new_int(firmware_error->Revision));
34 	json_object_object_add(
35 		section_ir, "recordID",
36 		json_object_new_uint64(firmware_error->RecordId));
37 
38 	//Record GUID.
39 	add_guid(section_ir, "recordIDGUID", &firmware_error->RecordIdGuid);
40 
41 	return section_ir;
42 }
43 
44 //Converts a single firmware CPER-JSON section into CPER binary, outputting to the given stream.
ir_section_firmware_to_cper(json_object * section,FILE * out)45 void ir_section_firmware_to_cper(json_object *section, FILE *out)
46 {
47 	EFI_FIRMWARE_ERROR_DATA *section_cper =
48 		(EFI_FIRMWARE_ERROR_DATA *)calloc(
49 			1, sizeof(EFI_FIRMWARE_ERROR_DATA));
50 
51 	//Record fields.
52 	section_cper->ErrorType = readable_pair_to_integer(
53 		json_object_object_get(section, "errorRecordType"));
54 	section_cper->Revision = json_object_get_int(
55 		json_object_object_get(section, "revision"));
56 	section_cper->RecordId = json_object_get_uint64(
57 		json_object_object_get(section, "recordID"));
58 	string_to_guid(&section_cper->RecordIdGuid,
59 		       json_object_get_string(json_object_object_get(
60 			       section, "recordIDGUID")));
61 
62 	//Write to stream, free resources.
63 	fwrite(section_cper, sizeof(EFI_FIRMWARE_ERROR_DATA), 1, out);
64 	fflush(out);
65 	free(section_cper);
66 }
67