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 "../edk/Cper.h"
10 #include "../cper-utils.h"
11 #include "cper-section-firmware.h"
12 
13 //Converts a single firmware CPER section into JSON IR.
14 json_object *
15 cper_section_firmware_to_ir(void *section,
16 			    EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
17 {
18 	EFI_FIRMWARE_ERROR_DATA *firmware_error =
19 		(EFI_FIRMWARE_ERROR_DATA *)section;
20 	json_object *section_ir = json_object_new_object();
21 
22 	//Record type.
23 	json_object *record_type = integer_to_readable_pair(
24 		firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS,
25 		FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)");
26 	json_object_object_add(section_ir, "errorRecordType", record_type);
27 
28 	//Revision, record identifier.
29 	json_object_object_add(section_ir, "revision",
30 			       json_object_new_int(firmware_error->Revision));
31 	json_object_object_add(
32 		section_ir, "recordID",
33 		json_object_new_uint64(firmware_error->RecordId));
34 
35 	//Record GUID.
36 	char record_id_guid[GUID_STRING_LENGTH];
37 	guid_to_string(record_id_guid, &firmware_error->RecordIdGuid);
38 	json_object_object_add(section_ir, "recordIDGUID",
39 			       json_object_new_string(record_id_guid));
40 
41 	return section_ir;
42 }
43 
44 //Converts a single firmware CPER-JSON section into CPER binary, outputting to the given stream.
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 }