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 <libcper/Cper.h>
10 #include <libcper/cper-utils.h>
11 #include <libcper/sections/cper-section-dmar-generic.h>
12 #include <libcper/log.h>
13 #include <string.h>
14
15 //Converts a single generic DMAr CPER section into JSON IR.
cper_section_dmar_generic_to_ir(const UINT8 * section,UINT32 size,char ** desc_string)16 json_object *cper_section_dmar_generic_to_ir(const UINT8 *section, UINT32 size,
17 char **desc_string)
18 {
19 int outstr_len = 0;
20
21 *desc_string = NULL;
22 if (size < sizeof(EFI_DMAR_GENERIC_ERROR_DATA)) {
23 cper_print_log("Error: DMAr Generic section too small\n");
24 return NULL;
25 }
26
27 *desc_string = calloc(1, SECTION_DESC_STRING_SIZE);
28 if (*desc_string == NULL) {
29 cper_print_log(
30 "Error: Failed to allocate DMAr Generic desc string\n");
31 return NULL;
32 }
33 outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE,
34 "A DMAr Generic Error occurred");
35 if (outstr_len < 0) {
36 cper_print_log(
37 "Error: Could not write to DMAr Generic description string\n");
38 } else if (outstr_len > SECTION_DESC_STRING_SIZE) {
39 cper_print_log(
40 "Error: DMAr Generic description string truncated\n");
41 }
42
43 EFI_DMAR_GENERIC_ERROR_DATA *firmware_error =
44 (EFI_DMAR_GENERIC_ERROR_DATA *)section;
45 json_object *section_ir = json_object_new_object();
46
47 //Requester ID, segment.
48 add_int(section_ir, "requesterID", firmware_error->RequesterId);
49 add_int(section_ir, "segmentNumber", firmware_error->SegmentNumber);
50
51 //Fault reason.
52 json_object *fault_reason = integer_to_readable_pair_with_desc(
53 firmware_error->FaultReason, 11,
54 DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_KEYS,
55 DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_VALUES,
56 DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_DESCRIPTIONS,
57 "Unknown (Reserved)");
58 json_object_object_add(section_ir, "faultReason", fault_reason);
59
60 //Access type.
61 json_object *access_type = integer_to_readable_pair(
62 firmware_error->AccessType, 2,
63 DMAR_GENERIC_ERROR_ACCESS_TYPES_KEYS,
64 DMAR_GENERIC_ERROR_ACCESS_TYPES_VALUES, "Unknown (Reserved)");
65 json_object_object_add(section_ir, "accessType", access_type);
66
67 //Address type.
68 json_object *address_type = integer_to_readable_pair(
69 firmware_error->AddressType, 2,
70 DMAR_GENERIC_ERROR_ADDRESS_TYPES_KEYS,
71 DMAR_GENERIC_ERROR_ADDRESS_TYPES_VALUES, "Unknown (Reserved)");
72 json_object_object_add(section_ir, "addressType", address_type);
73
74 //Architecture type.
75 json_object *arch_type = integer_to_readable_pair(
76 firmware_error->ArchType, 2, DMAR_GENERIC_ERROR_ARCH_TYPES_KEYS,
77 DMAR_GENERIC_ERROR_ARCH_TYPES_VALUES, "Unknown (Reserved)");
78 json_object_object_add(section_ir, "architectureType", arch_type);
79
80 //Device address.
81 add_uint(section_ir, "deviceAddress", firmware_error->DeviceAddr);
82
83 return section_ir;
84 }
85
86 //Converts a single generic DMAR CPER-JSON section into CPER binary, outputting to the given stream.
ir_section_dmar_generic_to_cper(json_object * section,FILE * out)87 void ir_section_dmar_generic_to_cper(json_object *section, FILE *out)
88 {
89 EFI_DMAR_GENERIC_ERROR_DATA *section_cper =
90 (EFI_DMAR_GENERIC_ERROR_DATA *)calloc(
91 1, sizeof(EFI_DMAR_GENERIC_ERROR_DATA));
92
93 //Record fields.
94 section_cper->RequesterId = (UINT16)json_object_get_int(
95 json_object_object_get(section, "requesterID"));
96 section_cper->SegmentNumber = (UINT16)json_object_get_int(
97 json_object_object_get(section, "segmentNumber"));
98 section_cper->FaultReason = (UINT8)readable_pair_to_integer(
99 json_object_object_get(section, "faultReason"));
100 section_cper->AccessType = (UINT8)readable_pair_to_integer(
101 json_object_object_get(section, "accessType"));
102 section_cper->AddressType = (UINT8)readable_pair_to_integer(
103 json_object_object_get(section, "addressType"));
104 section_cper->ArchType = (UINT8)readable_pair_to_integer(
105 json_object_object_get(section, "architectureType"));
106 section_cper->DeviceAddr = json_object_get_uint64(
107 json_object_object_get(section, "deviceAddress"));
108
109 //Write to stream, free resources.
110 fwrite(section_cper, sizeof(EFI_DMAR_GENERIC_ERROR_DATA), 1, out);
111 fflush(out);
112 free(section_cper);
113 }
114