xref: /openbmc/libcper/sections/cper-section-firmware.c (revision 50b966f7afa31fe39fda70e10eb9139cce39e025)
1c60a243eSLawrence Tang /**
2c60a243eSLawrence Tang  * Describes functions for converting firmware CPER sections from binary and JSON format
3c60a243eSLawrence Tang  * into an intermediate format.
4c60a243eSLawrence Tang  *
5c60a243eSLawrence Tang  * Author: Lawrence.Tang@arm.com
6c60a243eSLawrence Tang  **/
7c60a243eSLawrence Tang #include <stdio.h>
85202bbb4SLawrence Tang #include <json.h>
9e42fb487SThu Nguyen #include <libcper/Cper.h>
10e42fb487SThu Nguyen #include <libcper/cper-utils.h>
11e42fb487SThu Nguyen #include <libcper/sections/cper-section-firmware.h>
12*50b966f7SEd Tanous #include <libcper/log.h>
13c60a243eSLawrence Tang 
cper_section_firmware_to_ir(void * section)14c60a243eSLawrence Tang //Converts a single firmware CPER section into JSON IR.
1512dbd4fdSEd Tanous json_object *cper_section_firmware_to_ir(const UINT8 *section, UINT32 size)
16c60a243eSLawrence Tang {
1712dbd4fdSEd Tanous 	if (size < sizeof(EFI_FIRMWARE_ERROR_DATA)) {
1812dbd4fdSEd Tanous 		return NULL;
1912dbd4fdSEd Tanous 	}
2012dbd4fdSEd Tanous 
21e407b4c8SLawrence Tang 	EFI_FIRMWARE_ERROR_DATA *firmware_error =
22e407b4c8SLawrence Tang 		(EFI_FIRMWARE_ERROR_DATA *)section;
23c60a243eSLawrence Tang 	json_object *section_ir = json_object_new_object();
24c60a243eSLawrence Tang 
25c60a243eSLawrence Tang 	//Record type.
26e407b4c8SLawrence Tang 	json_object *record_type = integer_to_readable_pair(
27e407b4c8SLawrence Tang 		firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS,
28e407b4c8SLawrence Tang 		FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)");
29c60a243eSLawrence Tang 	json_object_object_add(section_ir, "errorRecordType", record_type);
30c60a243eSLawrence Tang 
31c60a243eSLawrence Tang 	//Revision, record identifier.
32e407b4c8SLawrence Tang 	json_object_object_add(section_ir, "revision",
33e407b4c8SLawrence Tang 			       json_object_new_int(firmware_error->Revision));
34e407b4c8SLawrence Tang 	json_object_object_add(
35e407b4c8SLawrence Tang 		section_ir, "recordID",
36e407b4c8SLawrence Tang 		json_object_new_uint64(firmware_error->RecordId));
37c60a243eSLawrence Tang 
38c60a243eSLawrence Tang 	//Record GUID.
39c2ebdddbSEd Tanous 	add_guid(section_ir, "recordIDGUID", &firmware_error->RecordIdGuid);
40c60a243eSLawrence Tang 
41c60a243eSLawrence Tang 	return section_ir;
42c60a243eSLawrence Tang }
ir_section_firmware_to_cper(json_object * section,FILE * out)43205dd1d7SLawrence Tang 
44205dd1d7SLawrence Tang //Converts a single firmware CPER-JSON section into CPER binary, outputting to the given stream.
45205dd1d7SLawrence Tang void ir_section_firmware_to_cper(json_object *section, FILE *out)
46205dd1d7SLawrence Tang {
47205dd1d7SLawrence Tang 	EFI_FIRMWARE_ERROR_DATA *section_cper =
48e407b4c8SLawrence Tang 		(EFI_FIRMWARE_ERROR_DATA *)calloc(
49e407b4c8SLawrence Tang 			1, sizeof(EFI_FIRMWARE_ERROR_DATA));
50205dd1d7SLawrence Tang 
51205dd1d7SLawrence Tang 	//Record fields.
52e407b4c8SLawrence Tang 	section_cper->ErrorType = readable_pair_to_integer(
53e407b4c8SLawrence Tang 		json_object_object_get(section, "errorRecordType"));
54e407b4c8SLawrence Tang 	section_cper->Revision = json_object_get_int(
55e407b4c8SLawrence Tang 		json_object_object_get(section, "revision"));
56e407b4c8SLawrence Tang 	section_cper->RecordId = json_object_get_uint64(
57e407b4c8SLawrence Tang 		json_object_object_get(section, "recordID"));
58205dd1d7SLawrence Tang 	string_to_guid(&section_cper->RecordIdGuid,
59e407b4c8SLawrence Tang 		       json_object_get_string(json_object_object_get(
60e407b4c8SLawrence Tang 			       section, "recordIDGUID")));
61205dd1d7SLawrence Tang 
62205dd1d7SLawrence Tang 	//Write to stream, free resources.
633ab351feSLawrence Tang 	fwrite(section_cper, sizeof(EFI_FIRMWARE_ERROR_DATA), 1, out);
64205dd1d7SLawrence Tang 	fflush(out);
65205dd1d7SLawrence Tang 	free(section_cper);
66205dd1d7SLawrence Tang }
67