blob: fcbce1828da41a506e62d9accae6e63e5981a7ef [file] [log] [blame]
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +01001/**
2 * Describes functions for converting VT-d specific DMAr CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang205dd1d2022-07-14 16:23:38 +01008#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/base64.h>
11#include <libcper/Cper.h>
12#include <libcper/cper-utils.h>
13#include <libcper/sections/cper-section-dmar-vtd.h>
Ed Tanous50b966f2025-03-11 09:06:19 -070014#include <libcper/log.h>
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010015
16//Converts a single VT-d specific DMAr CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080017json_object *cper_section_dmar_vtd_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010018{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080019 if (size < sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA)) {
20 return NULL;
21 }
22
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 EFI_DIRECTED_IO_DMAR_ERROR_DATA *vtd_error =
24 (EFI_DIRECTED_IO_DMAR_ERROR_DATA *)section;
25 json_object *section_ir = json_object_new_object();
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010026
Lawrence Tange407b4c2022-07-21 13:54:01 +010027 //Version, revision and OEM ID, as defined in the VT-d architecture.
28 UINT64 oem_id = 0;
John Chungf8fc7052024-05-03 20:05:29 +080029 for (int i = 0; i < 6; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 oem_id |= (UINT64)vtd_error->OemId[i] << (i * 8);
John Chungf8fc7052024-05-03 20:05:29 +080031 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 json_object_object_add(section_ir, "version",
33 json_object_new_int(vtd_error->Version));
34 json_object_object_add(section_ir, "revision",
35 json_object_new_int(vtd_error->Revision));
36 json_object_object_add(section_ir, "oemID",
37 json_object_new_uint64(oem_id));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010038
Lawrence Tange407b4c2022-07-21 13:54:01 +010039 //Registers.
40 json_object_object_add(section_ir, "capabilityRegister",
41 json_object_new_uint64(vtd_error->Capability));
42 json_object_object_add(section_ir, "extendedCapabilityRegister",
43 json_object_new_uint64(vtd_error->CapabilityEx));
44 json_object_object_add(
45 section_ir, "globalCommandRegister",
46 json_object_new_uint64(vtd_error->GlobalCommand));
47 json_object_object_add(section_ir, "globalStatusRegister",
48 json_object_new_uint64(vtd_error->GlobalStatus));
49 json_object_object_add(section_ir, "faultStatusRegister",
50 json_object_new_uint64(vtd_error->FaultStatus));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010051
Lawrence Tange407b4c2022-07-21 13:54:01 +010052 //Fault record basic fields.
53 json_object *fault_record_ir = json_object_new_object();
54 EFI_VTD_FAULT_RECORD *fault_record =
55 (EFI_VTD_FAULT_RECORD *)vtd_error->FaultRecord;
56 json_object_object_add(
57 fault_record_ir, "faultInformation",
58 json_object_new_uint64(fault_record->FaultInformation));
59 json_object_object_add(
60 fault_record_ir, "sourceIdentifier",
61 json_object_new_uint64(fault_record->SourceIdentifier));
62 json_object_object_add(
63 fault_record_ir, "privelegeModeRequested",
64 json_object_new_boolean(fault_record->PrivelegeModeRequested));
65 json_object_object_add(
66 fault_record_ir, "executePermissionRequested",
67 json_object_new_boolean(
68 fault_record->ExecutePermissionRequested));
69 json_object_object_add(
70 fault_record_ir, "pasidPresent",
71 json_object_new_boolean(fault_record->PasidPresent));
72 json_object_object_add(
73 fault_record_ir, "faultReason",
74 json_object_new_uint64(fault_record->FaultReason));
75 json_object_object_add(
76 fault_record_ir, "pasidValue",
77 json_object_new_uint64(fault_record->PasidValue));
78 json_object_object_add(
79 fault_record_ir, "addressType",
80 json_object_new_uint64(fault_record->AddressType));
Lawrence Tangce0f82b2022-07-07 16:14:28 +010081
Lawrence Tange407b4c2022-07-21 13:54:01 +010082 //Fault record type.
83 json_object *fault_record_type = integer_to_readable_pair(
84 fault_record->Type, 2, VTD_FAULT_RECORD_TYPES_KEYS,
85 VTD_FAULT_RECORD_TYPES_VALUES, "Unknown");
86 json_object_object_add(fault_record_ir, "type", fault_record_type);
87 json_object_object_add(section_ir, "faultRecord", fault_record_ir);
Lawrence Tangce0f82b2022-07-07 16:14:28 +010088
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 //Root entry.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070090 int32_t encoded_len = 0;
91
92 char *encoded =
93 base64_encode((UINT8 *)vtd_error->RootEntry, 16, &encoded_len);
94 json_object_object_add(section_ir, "rootEntry",
95 json_object_new_string_len(encoded,
96 encoded_len));
97 free(encoded);
Lawrence Tangce0f82b2022-07-07 16:14:28 +010098
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 //Context entry.
John Chungf8fc7052024-05-03 20:05:29 +0800100 encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700101 encoded = base64_encode((UINT8 *)vtd_error->ContextEntry, 16,
102 &encoded_len);
103 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700104 cper_print_log("Failed to allocate encode output buffer. \n");
John Chungf8fc7052024-05-03 20:05:29 +0800105 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800106 json_object_object_add(section_ir, "contextEntry",
107 json_object_new_string_len(encoded,
108 encoded_len));
109 free(encoded);
110 }
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +0100111
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 //PTE entry for all page levels.
113 json_object_object_add(section_ir, "pageTableEntry_Level6",
114 json_object_new_uint64(vtd_error->PteL6));
115 json_object_object_add(section_ir, "pageTableEntry_Level5",
116 json_object_new_uint64(vtd_error->PteL5));
117 json_object_object_add(section_ir, "pageTableEntry_Level4",
118 json_object_new_uint64(vtd_error->PteL4));
119 json_object_object_add(section_ir, "pageTableEntry_Level3",
120 json_object_new_uint64(vtd_error->PteL3));
121 json_object_object_add(section_ir, "pageTableEntry_Level2",
122 json_object_new_uint64(vtd_error->PteL2));
123 json_object_object_add(section_ir, "pageTableEntry_Level1",
124 json_object_new_uint64(vtd_error->PteL1));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +0100125
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100127}
128
129//Converts a single VT-d DMAR CPER-JSON segment into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130void ir_section_dmar_vtd_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100131{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 EFI_DIRECTED_IO_DMAR_ERROR_DATA *section_cper =
133 (EFI_DIRECTED_IO_DMAR_ERROR_DATA *)calloc(
134 1, sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100135
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 //OEM ID.
137 UINT64 oem_id = json_object_get_uint64(
138 json_object_object_get(section, "oemID"));
John Chungf8fc7052024-05-03 20:05:29 +0800139 for (int i = 0; i < 6; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 section_cper->OemId[i] = (oem_id >> (i * 8)) & 0xFF;
John Chungf8fc7052024-05-03 20:05:29 +0800141 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Registers & basic numeric fields.
144 section_cper->Version = (UINT8)json_object_get_int(
145 json_object_object_get(section, "version"));
146 section_cper->Revision = (UINT8)json_object_get_int(
147 json_object_object_get(section, "revision"));
148 section_cper->Capability = json_object_get_uint64(
149 json_object_object_get(section, "capabilityRegister"));
150 section_cper->CapabilityEx = json_object_get_uint64(
151 json_object_object_get(section, "extendedCapabilityRegister"));
152 section_cper->GlobalCommand = json_object_get_uint64(
153 json_object_object_get(section, "globalCommandRegister"));
154 section_cper->GlobalStatus = json_object_get_uint64(
155 json_object_object_get(section, "globalStatusRegister"));
156 section_cper->FaultStatus = json_object_get_uint64(
157 json_object_object_get(section, "faultStatusRegister"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100158
Lawrence Tange407b4c2022-07-21 13:54:01 +0100159 //Fault record.
160 json_object *fault_record =
161 json_object_object_get(section, "faultRecord");
162 EFI_VTD_FAULT_RECORD *fault_record_cper =
163 (EFI_VTD_FAULT_RECORD *)section_cper->FaultRecord;
164 fault_record_cper->FaultInformation = json_object_get_uint64(
165 json_object_object_get(fault_record, "faultInformation"));
166 fault_record_cper->SourceIdentifier = json_object_get_uint64(
167 json_object_object_get(fault_record, "sourceIdentifier"));
168 fault_record_cper->PrivelegeModeRequested = json_object_get_boolean(
169 json_object_object_get(fault_record, "privelegeModeRequested"));
170 fault_record_cper->ExecutePermissionRequested = json_object_get_boolean(
171 json_object_object_get(fault_record,
172 "executePermissionRequested"));
173 fault_record_cper->PasidPresent = json_object_get_boolean(
174 json_object_object_get(fault_record, "pasidPresent"));
175 fault_record_cper->FaultReason = json_object_get_uint64(
176 json_object_object_get(fault_record, "faultReason"));
177 fault_record_cper->PasidValue = json_object_get_uint64(
178 json_object_object_get(fault_record, "pasidValue"));
179 fault_record_cper->AddressType = json_object_get_uint64(
180 json_object_object_get(fault_record, "addressType"));
181 fault_record_cper->Type = readable_pair_to_integer(
182 json_object_object_get(fault_record, "type"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100183
Lawrence Tange407b4c2022-07-21 13:54:01 +0100184 //Root entry.
185 json_object *encoded = json_object_object_get(section, "rootEntry");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700186 int32_t decoded_len = 0;
187
188 UINT8 *decoded = base64_decode(json_object_get_string(encoded),
189 json_object_get_string_len(encoded),
190 &decoded_len);
191 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700192 cper_print_log("Failed to allocate decode output buffer. \n");
John Chungf8fc7052024-05-03 20:05:29 +0800193 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800194 memcpy(section_cper->RootEntry, decoded, decoded_len);
195 free(decoded);
196 }
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700197
Lawrence Tange407b4c2022-07-21 13:54:01 +0100198 //Context entry.
199 encoded = json_object_object_get(section, "contextEntry");
John Chungf8fc7052024-05-03 20:05:29 +0800200 decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700201
202 decoded = base64_decode(json_object_get_string(encoded),
203 json_object_get_string_len(encoded),
204 &decoded_len);
205 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700206 cper_print_log("Failed to allocate decode output buffer. \n");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700207
John Chungf8fc7052024-05-03 20:05:29 +0800208 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800209 memcpy(section_cper->ContextEntry, decoded, decoded_len);
210 free(decoded);
211 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100212
Lawrence Tange407b4c2022-07-21 13:54:01 +0100213 //Page table entries.
214 section_cper->PteL1 = json_object_get_uint64(
215 json_object_object_get(section, "pageTableEntry_Level1"));
216 section_cper->PteL2 = json_object_get_uint64(
217 json_object_object_get(section, "pageTableEntry_Level2"));
218 section_cper->PteL3 = json_object_get_uint64(
219 json_object_object_get(section, "pageTableEntry_Level3"));
220 section_cper->PteL4 = json_object_get_uint64(
221 json_object_object_get(section, "pageTableEntry_Level4"));
222 section_cper->PteL5 = json_object_get_uint64(
223 json_object_object_get(section, "pageTableEntry_Level5"));
224 section_cper->PteL6 = json_object_get_uint64(
225 json_object_object_get(section, "pageTableEntry_Level6"));
226
227 //Write to stream, free resources.
228 fwrite(section_cper, sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA), 1, out);
229 fflush(out);
230 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800231}