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* cper_section_firmware_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
15 {
16     EFI_FIRMWARE_ERROR_DATA* firmware_error = (EFI_FIRMWARE_ERROR_DATA*)section;
17     json_object* section_ir = json_object_new_object();
18 
19     //Record type.
20     json_object* record_type = integer_to_readable_pair(firmware_error->ErrorType, 3,
21         FIRMWARE_ERROR_RECORD_TYPES_KEYS,
22         FIRMWARE_ERROR_RECORD_TYPES_VALUES,
23         "Unknown (Reserved)");
24     json_object_object_add(section_ir, "errorRecordType", record_type);
25 
26     //Revision, record identifier.
27     json_object_object_add(section_ir, "revision", json_object_new_int(firmware_error->Revision));
28     json_object_object_add(section_ir, "recordID", json_object_new_uint64(firmware_error->RecordId));
29 
30     //Record GUID.
31     char record_id_guid[GUID_STRING_LENGTH];
32     guid_to_string(record_id_guid, &firmware_error->RecordIdGuid);
33     json_object_object_add(section_ir, "recordIDGUID", json_object_new_string(record_id_guid));
34 
35     return section_ir;
36 }
37 
38 //Converts a single firmware CPER-JSON section into CPER binary, outputting to the given stream.
39 void ir_section_firmware_to_cper(json_object* section, FILE* out)
40 {
41     EFI_FIRMWARE_ERROR_DATA* section_cper =
42         (EFI_FIRMWARE_ERROR_DATA*)calloc(1, sizeof(EFI_FIRMWARE_ERROR_DATA));
43 
44     //Record fields.
45     section_cper->ErrorType = readable_pair_to_integer(json_object_object_get(section, "errorRecordType"));
46     section_cper->Revision = json_object_get_int(json_object_object_get(section, "revision"));
47     section_cper->RecordId = json_object_get_uint64(json_object_object_get(section, "revision"));
48     string_to_guid(&section_cper->RecordIdGuid,
49         json_object_get_string(json_object_object_get(section, "recordIDGUID")));
50 
51     //Write to stream, free resources.
52     fwrite(&section_cper, sizeof(section_cper), 1, out);
53     fflush(out);
54     free(section_cper);
55 }