1 /**
2  * Describes functions for converting generic DMAr 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-dmar-generic.h"
12 
13 //Converts a single generic DMAr CPER section into JSON IR.
14 json_object* cper_section_dmar_generic_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
15 {
16     EFI_DMAR_GENERIC_ERROR_DATA* firmware_error = (EFI_DMAR_GENERIC_ERROR_DATA*)section;
17     json_object* section_ir = json_object_new_object();
18 
19     //Requester ID, segment.
20     json_object_object_add(section_ir, "requesterID", json_object_new_int(firmware_error->RequesterId));
21     json_object_object_add(section_ir, "segmentNumber", json_object_new_int(firmware_error->SegmentNumber));
22 
23     //Fault reason.
24     json_object* fault_reason = integer_to_readable_pair_with_desc(firmware_error->FaultReason, 11,
25         DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_KEYS,
26         DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_VALUES,
27         DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_DESCRIPTIONS,
28         "Unknown (Reserved)");
29     json_object_object_add(section_ir, "faultReason", fault_reason);
30 
31     //Access type.
32     json_object* access_type = integer_to_readable_pair(firmware_error->AccessType, 2,
33         DMAR_GENERIC_ERROR_ACCESS_TYPES_KEYS,
34         DMAR_GENERIC_ERROR_ACCESS_TYPES_VALUES,
35         "Unknown (Reserved)");
36     json_object_object_add(section_ir, "accessType", access_type);
37 
38     //Address type.
39     json_object* address_type = integer_to_readable_pair(firmware_error->AddressType, 2,
40         DMAR_GENERIC_ERROR_ADDRESS_TYPES_KEYS,
41         DMAR_GENERIC_ERROR_ADDRESS_TYPES_VALUES,
42         "Unknown (Reserved)");
43     json_object_object_add(section_ir, "addressType", address_type);
44 
45     //Architecture type.
46     json_object* arch_type = integer_to_readable_pair(firmware_error->ArchType, 2,
47         DMAR_GENERIC_ERROR_ARCH_TYPES_KEYS,
48         DMAR_GENERIC_ERROR_ARCH_TYPES_VALUES,
49         "Unknown (Reserved)");
50     json_object_object_add(section_ir, "architectureType", arch_type);
51 
52     //Device address.
53     json_object_object_add(section_ir, "deviceAddress", json_object_new_uint64(firmware_error->DeviceAddr));
54 
55     return section_ir;
56 }
57 
58 //Converts a single generic DMAR CPER-JSON section into CPER binary, outputting to the given stream.
59 void ir_section_dmar_generic_to_cper(json_object* section, FILE* out)
60 {
61     EFI_DMAR_GENERIC_ERROR_DATA* section_cper =
62         (EFI_DMAR_GENERIC_ERROR_DATA*)calloc(1, sizeof(EFI_DMAR_GENERIC_ERROR_DATA));
63 
64     //Record fields.
65     section_cper->RequesterId = (UINT16)json_object_get_int(json_object_object_get(section, "requesterID"));
66     section_cper->SegmentNumber = (UINT16)json_object_get_int(json_object_object_get(section, "segmentNumber"));
67     section_cper->FaultReason = (UINT8)readable_pair_to_integer(json_object_object_get(section, "faultReason"));
68     section_cper->AccessType = (UINT8)readable_pair_to_integer(json_object_object_get(section, "accessType"));
69     section_cper->AddressType = (UINT8)readable_pair_to_integer(json_object_object_get(section, "addressType"));
70     section_cper->ArchType = (UINT8)readable_pair_to_integer(json_object_object_get(section, "architectureType"));
71     section_cper->DeviceAddr = json_object_get_uint64(json_object_object_get(section, "deviceAddress"));
72 
73     //Write to stream, free resources.
74     fwrite(section_cper, sizeof(EFI_DMAR_GENERIC_ERROR_DATA), 1, out);
75     fflush(out);
76     free(section_cper);
77 }