blob: bb07da6b2b4d5ee18f0478eb5d7d2e4a649d3632 [file] [log] [blame]
Lawrence Tang794312c2022-07-05 14:46:10 +01001/**
2 * Describes functions for converting IA32/x64 CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang794312c2022-07-05 14:46:10 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7
8#include <stdio.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070010#include "base64.h"
Lawrence Tang794312c2022-07-05 14:46:10 +010011#include "../edk/Cper.h"
12#include "../cper-utils.h"
13#include "cper-section-ia32x64.h"
14
15//Private pre-definitions.
Lawrence Tange407b4c2022-07-21 13:54:01 +010016json_object *cper_ia32x64_processor_error_info_to_ir(
17 EFI_IA32_X64_PROCESS_ERROR_INFO *error_info);
18json_object *cper_ia32x64_cache_tlb_check_to_ir(
19 EFI_IA32_X64_CACHE_CHECK_INFO *cache_tlb_check);
20json_object *
21cper_ia32x64_bus_check_to_ir(EFI_IA32_X64_BUS_CHECK_INFO *bus_check);
22json_object *cper_ia32x64_ms_check_to_ir(EFI_IA32_X64_MS_CHECK_INFO *ms_check);
23json_object *cper_ia32x64_processor_context_info_to_ir(
24 EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *context_info, void **cur_pos);
25json_object *
26cper_ia32x64_register_32bit_to_ir(EFI_CONTEXT_IA32_REGISTER_STATE *registers);
27json_object *
28cper_ia32x64_register_64bit_to_ir(EFI_CONTEXT_X64_REGISTER_STATE *registers);
29void ir_ia32x64_error_info_to_cper(json_object *error_info, FILE *out);
30void ir_ia32x64_context_info_to_cper(json_object *context_info, FILE *out);
31void ir_ia32x64_cache_tlb_check_error_to_cper(
32 json_object *check_info,
33 EFI_IA32_X64_CACHE_CHECK_INFO *check_info_cper);
34void ir_ia32x64_bus_check_error_to_cper(
35 json_object *check_info, EFI_IA32_X64_BUS_CHECK_INFO *check_info_cper);
36void ir_ia32x64_ms_check_error_to_cper(
37 json_object *check_info, EFI_IA32_X64_MS_CHECK_INFO *check_info_cper);
38void ir_ia32x64_ia32_registers_to_cper(json_object *registers, FILE *out);
39void ir_ia32x64_x64_registers_to_cper(json_object *registers, FILE *out);
Lawrence Tangd0c88842022-07-13 14:51:17 +010040
41//////////////////
42/// CPER TO IR ///
43//////////////////
Lawrence Tang794312c2022-07-05 14:46:10 +010044
45//Converts the IA32/x64 error section described in the given descriptor into intermediate format.
John Chungf8fc7052024-05-03 20:05:29 +080046json_object *cper_section_ia32x64_to_ir(void *section)
Lawrence Tang794312c2022-07-05 14:46:10 +010047{
Lawrence Tange407b4c2022-07-21 13:54:01 +010048 EFI_IA32_X64_PROCESSOR_ERROR_RECORD *record =
49 (EFI_IA32_X64_PROCESSOR_ERROR_RECORD *)section;
50 json_object *record_ir = json_object_new_object();
Lawrence Tang794312c2022-07-05 14:46:10 +010051
Lawrence Tange407b4c2022-07-21 13:54:01 +010052 //Validation bits.
53 json_object *validationBits = json_object_new_object();
54 json_object_object_add(validationBits, "localAPICIDValid",
55 json_object_new_boolean(record->ValidFields &
John Chungf8fc7052024-05-03 20:05:29 +080056 0x1));
Lawrence Tange407b4c2022-07-21 13:54:01 +010057 json_object_object_add(
58 validationBits, "cpuIDInfoValid",
John Chungf8fc7052024-05-03 20:05:29 +080059 json_object_new_boolean((record->ValidFields >> 1) & 0x1));
60 int processor_error_info_num = (record->ValidFields >> 2) & 0x3F;
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 json_object_object_add(validationBits, "processorErrorInfoNum",
62 json_object_new_int(processor_error_info_num));
John Chungf8fc7052024-05-03 20:05:29 +080063 int processor_context_info_num = (record->ValidFields >> 8) & 0x3F;
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 json_object_object_add(validationBits, "processorContextInfoNum",
65 json_object_new_int(processor_context_info_num));
66 json_object_object_add(record_ir, "validationBits", validationBits);
Lawrence Tang794312c2022-07-05 14:46:10 +010067
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 //APIC ID.
69 json_object_object_add(record_ir, "localAPICID",
70 json_object_new_uint64(record->ApicId));
Lawrence Tang794312c2022-07-05 14:46:10 +010071
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 //CPUID information.
73 json_object *cpuid_info_ir = json_object_new_object();
74 EFI_IA32_X64_CPU_ID *cpuid_info =
75 (EFI_IA32_X64_CPU_ID *)record->CpuIdInfo;
76 json_object_object_add(cpuid_info_ir, "eax",
77 json_object_new_uint64(cpuid_info->Eax));
78 json_object_object_add(cpuid_info_ir, "ebx",
79 json_object_new_uint64(cpuid_info->Ebx));
80 json_object_object_add(cpuid_info_ir, "ecx",
81 json_object_new_uint64(cpuid_info->Ecx));
82 json_object_object_add(cpuid_info_ir, "edx",
83 json_object_new_uint64(cpuid_info->Edx));
84 json_object_object_add(record_ir, "cpuidInfo", cpuid_info_ir);
Lawrence Tang794312c2022-07-05 14:46:10 +010085
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 //Processor error information, of the amount described above.
87 EFI_IA32_X64_PROCESS_ERROR_INFO *current_error_info =
88 (EFI_IA32_X64_PROCESS_ERROR_INFO *)(record + 1);
89 json_object *error_info_array = json_object_new_array();
90 for (int i = 0; i < processor_error_info_num; i++) {
91 json_object_array_add(error_info_array,
92 cper_ia32x64_processor_error_info_to_ir(
93 current_error_info));
94 current_error_info++;
95 }
96 json_object_object_add(record_ir, "processorErrorInfo",
97 error_info_array);
Lawrence Tang794312c2022-07-05 14:46:10 +010098
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 //Processor context information, of the amount described above.
100 EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *current_context_info =
101 (EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *)current_error_info;
102 void *cur_pos = (void *)current_context_info;
103 json_object *context_info_array = json_object_new_array();
104 for (int i = 0; i < processor_context_info_num; i++) {
105 json_object_array_add(context_info_array,
106 cper_ia32x64_processor_context_info_to_ir(
107 current_context_info, &cur_pos));
108 current_context_info =
109 (EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *)cur_pos;
110 //The context array is a non-fixed size, pointer is shifted within the above function.
111 }
112 json_object_object_add(record_ir, "processorContextInfo",
113 context_info_array);
Lawrence Tang794312c2022-07-05 14:46:10 +0100114
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 return record_ir;
Lawrence Tang794312c2022-07-05 14:46:10 +0100116}
117
118//Converts a single IA32/x64 processor error info block into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100119json_object *cper_ia32x64_processor_error_info_to_ir(
120 EFI_IA32_X64_PROCESS_ERROR_INFO *error_info)
Lawrence Tang794312c2022-07-05 14:46:10 +0100121{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122 json_object *error_info_ir = json_object_new_object();
Lawrence Tang794312c2022-07-05 14:46:10 +0100123
Lawrence Tange407b4c2022-07-21 13:54:01 +0100124 //Error structure type (as GUID).
125 char error_type[GUID_STRING_LENGTH];
126 guid_to_string(error_type, &error_info->ErrorType);
Lawrence Tang3592da72022-07-21 16:50:07 +0100127 json_object *type = json_object_new_object();
128 json_object_object_add(type, "guid",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100129 json_object_new_string(error_type));
Lawrence Tang794312c2022-07-05 14:46:10 +0100130
Lawrence Tang3592da72022-07-21 16:50:07 +0100131 //Get the error structure type as a readable string.
132 const char *readable_type = "Unknown";
133 if (guid_equal(&error_info->ErrorType,
John Chungf8fc7052024-05-03 20:05:29 +0800134 &gEfiIa32x64ErrorTypeCacheCheckGuid)) {
Lawrence Tang3592da72022-07-21 16:50:07 +0100135 readable_type = "Cache Check Error";
John Chungf8fc7052024-05-03 20:05:29 +0800136 } else if (guid_equal(&error_info->ErrorType,
137 &gEfiIa32x64ErrorTypeTlbCheckGuid)) {
Lawrence Tang3592da72022-07-21 16:50:07 +0100138 readable_type = "TLB Check Error";
John Chungf8fc7052024-05-03 20:05:29 +0800139 } else if (guid_equal(&error_info->ErrorType,
140 &gEfiIa32x64ErrorTypeBusCheckGuid)) {
Lawrence Tang3592da72022-07-21 16:50:07 +0100141 readable_type = "Bus Check Error";
John Chungf8fc7052024-05-03 20:05:29 +0800142 } else if (guid_equal(&error_info->ErrorType,
143 &gEfiIa32x64ErrorTypeMsCheckGuid)) {
Lawrence Tang3592da72022-07-21 16:50:07 +0100144 readable_type = "MS Check Error";
John Chungf8fc7052024-05-03 20:05:29 +0800145 }
146 json_object_object_add(type, "name",
147 json_object_new_string(readable_type));
Lawrence Tang3592da72022-07-21 16:50:07 +0100148 json_object_object_add(error_info_ir, "type", type);
149
Lawrence Tange407b4c2022-07-21 13:54:01 +0100150 //Validation bits.
151 json_object *validation =
152 bitfield_to_ir(error_info->ValidFields, 5,
153 IA32X64_PROCESSOR_ERROR_VALID_BITFIELD_NAMES);
154 json_object_object_add(error_info_ir, "validationBits", validation);
Lawrence Tang794312c2022-07-05 14:46:10 +0100155
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 //Add the check information on a per-structure basis.
157 //Cache and TLB check information are identical, so can be equated.
158 json_object *check_information = NULL;
159 if (guid_equal(&error_info->ErrorType,
160 &gEfiIa32x64ErrorTypeCacheCheckGuid) ||
161 guid_equal(&error_info->ErrorType,
162 &gEfiIa32x64ErrorTypeTlbCheckGuid)) {
163 check_information = cper_ia32x64_cache_tlb_check_to_ir(
164 (EFI_IA32_X64_CACHE_CHECK_INFO *)&error_info->CheckInfo);
165 } else if (guid_equal(&error_info->ErrorType,
166 &gEfiIa32x64ErrorTypeBusCheckGuid)) {
167 check_information = cper_ia32x64_bus_check_to_ir(
168 (EFI_IA32_X64_BUS_CHECK_INFO *)&error_info->CheckInfo);
169 } else if (guid_equal(&error_info->ErrorType,
170 &gEfiIa32x64ErrorTypeMsCheckGuid)) {
171 check_information = cper_ia32x64_ms_check_to_ir(
172 (EFI_IA32_X64_MS_CHECK_INFO *)&error_info->CheckInfo);
173 } else {
174 //Unknown check information.
175 printf("WARN: Invalid/unknown check information GUID found in IA32/x64 CPER section. Ignoring.\n");
176 }
177 json_object_object_add(error_info_ir, "checkInfo", check_information);
Lawrence Tang794312c2022-07-05 14:46:10 +0100178
Lawrence Tange407b4c2022-07-21 13:54:01 +0100179 //Target, requestor, and responder identifiers.
180 json_object_object_add(error_info_ir, "targetAddressID",
181 json_object_new_uint64(error_info->TargetId));
182 json_object_object_add(error_info_ir, "requestorID",
183 json_object_new_uint64(error_info->RequestorId));
184 json_object_object_add(error_info_ir, "responderID",
185 json_object_new_uint64(error_info->ResponderId));
186 json_object_object_add(
187 error_info_ir, "instructionPointer",
188 json_object_new_uint64(error_info->InstructionIP));
Lawrence Tang794312c2022-07-05 14:46:10 +0100189
Lawrence Tange407b4c2022-07-21 13:54:01 +0100190 return error_info_ir;
Lawrence Tang794312c2022-07-05 14:46:10 +0100191}
192
193//Converts a single IA32/x64 cache or TLB check check info block into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100194json_object *cper_ia32x64_cache_tlb_check_to_ir(
195 EFI_IA32_X64_CACHE_CHECK_INFO *cache_tlb_check)
Lawrence Tang794312c2022-07-05 14:46:10 +0100196{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100197 json_object *cache_tlb_check_ir = json_object_new_object();
Lawrence Tang794312c2022-07-05 14:46:10 +0100198
Lawrence Tange407b4c2022-07-21 13:54:01 +0100199 //Validation bits.
200 json_object *validation =
201 bitfield_to_ir(cache_tlb_check->ValidFields, 8,
202 IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES);
203 json_object_object_add(cache_tlb_check_ir, "validationBits",
204 validation);
Lawrence Tang794312c2022-07-05 14:46:10 +0100205
Lawrence Tange407b4c2022-07-21 13:54:01 +0100206 //Transaction type.
207 json_object *transaction_type = integer_to_readable_pair(
208 cache_tlb_check->TransactionType, 3,
209 IA32X64_CHECK_INFO_TRANSACTION_TYPES_KEYS,
210 IA32X64_CHECK_INFO_TRANSACTION_TYPES_VALUES,
211 "Unknown (Reserved)");
212 json_object_object_add(cache_tlb_check_ir, "transactionType",
213 transaction_type);
Lawrence Tang794312c2022-07-05 14:46:10 +0100214
Lawrence Tange407b4c2022-07-21 13:54:01 +0100215 //Operation.
216 json_object *operation = integer_to_readable_pair(
217 cache_tlb_check->Operation, 9,
218 IA32X64_CHECK_INFO_OPERATION_TYPES_KEYS,
219 IA32X64_CHECK_INFO_OPERATION_TYPES_VALUES,
220 "Unknown (Reserved)");
221 json_object_object_add(cache_tlb_check_ir, "operation", operation);
Lawrence Tang794312c2022-07-05 14:46:10 +0100222
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 //Affected cache/TLB level.
224 json_object_object_add(cache_tlb_check_ir, "level",
225 json_object_new_uint64(cache_tlb_check->Level));
Lawrence Tang794312c2022-07-05 14:46:10 +0100226
Lawrence Tange407b4c2022-07-21 13:54:01 +0100227 //Miscellaneous boolean fields.
228 json_object_object_add(
229 cache_tlb_check_ir, "processorContextCorrupt",
230 json_object_new_boolean(cache_tlb_check->ContextCorrupt));
231 json_object_object_add(
232 cache_tlb_check_ir, "uncorrected",
233 json_object_new_boolean(cache_tlb_check->ErrorUncorrected));
234 json_object_object_add(
235 cache_tlb_check_ir, "preciseIP",
236 json_object_new_boolean(cache_tlb_check->PreciseIp));
237 json_object_object_add(
238 cache_tlb_check_ir, "restartableIP",
239 json_object_new_boolean(cache_tlb_check->RestartableIp));
240 json_object_object_add(
241 cache_tlb_check_ir, "overflow",
242 json_object_new_boolean(cache_tlb_check->Overflow));
Lawrence Tang794312c2022-07-05 14:46:10 +0100243
Lawrence Tange407b4c2022-07-21 13:54:01 +0100244 return cache_tlb_check_ir;
Lawrence Tang794312c2022-07-05 14:46:10 +0100245}
246
247//Converts a single IA32/x64 bus check check info block into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100248json_object *
249cper_ia32x64_bus_check_to_ir(EFI_IA32_X64_BUS_CHECK_INFO *bus_check)
250{
251 json_object *bus_check_ir = json_object_new_object();
Lawrence Tang794312c2022-07-05 14:46:10 +0100252
Lawrence Tange407b4c2022-07-21 13:54:01 +0100253 //Validation bits.
254 json_object *validation =
255 bitfield_to_ir(bus_check->ValidFields, 11,
256 IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES);
257 json_object_object_add(bus_check_ir, "validationBits", validation);
Lawrence Tang794312c2022-07-05 14:46:10 +0100258
Lawrence Tange407b4c2022-07-21 13:54:01 +0100259 //Transaction type.
260 json_object *transaction_type = integer_to_readable_pair(
261 bus_check->TransactionType, 3,
262 IA32X64_CHECK_INFO_TRANSACTION_TYPES_KEYS,
263 IA32X64_CHECK_INFO_TRANSACTION_TYPES_VALUES,
264 "Unknown (Reserved)");
265 json_object_object_add(bus_check_ir, "transactionType",
266 transaction_type);
Lawrence Tang794312c2022-07-05 14:46:10 +0100267
Lawrence Tange407b4c2022-07-21 13:54:01 +0100268 //Operation.
269 json_object *operation = integer_to_readable_pair(
270 bus_check->Operation, 9,
271 IA32X64_CHECK_INFO_OPERATION_TYPES_KEYS,
272 IA32X64_CHECK_INFO_OPERATION_TYPES_VALUES,
273 "Unknown (Reserved)");
274 json_object_object_add(bus_check_ir, "operation", operation);
Lawrence Tang794312c2022-07-05 14:46:10 +0100275
Lawrence Tange407b4c2022-07-21 13:54:01 +0100276 //Affected bus level.
277 json_object_object_add(bus_check_ir, "level",
278 json_object_new_uint64(bus_check->Level));
Lawrence Tang794312c2022-07-05 14:46:10 +0100279
Lawrence Tange407b4c2022-07-21 13:54:01 +0100280 //Miscellaneous boolean fields.
281 json_object_object_add(
282 bus_check_ir, "processorContextCorrupt",
283 json_object_new_boolean(bus_check->ContextCorrupt));
284 json_object_object_add(
285 bus_check_ir, "uncorrected",
286 json_object_new_boolean(bus_check->ErrorUncorrected));
287 json_object_object_add(bus_check_ir, "preciseIP",
288 json_object_new_boolean(bus_check->PreciseIp));
289 json_object_object_add(
290 bus_check_ir, "restartableIP",
291 json_object_new_boolean(bus_check->RestartableIp));
292 json_object_object_add(bus_check_ir, "overflow",
293 json_object_new_boolean(bus_check->Overflow));
294 json_object_object_add(bus_check_ir, "timedOut",
295 json_object_new_boolean(bus_check->TimeOut));
Lawrence Tang794312c2022-07-05 14:46:10 +0100296
Lawrence Tange407b4c2022-07-21 13:54:01 +0100297 //Participation type.
298 json_object *participation_type = integer_to_readable_pair(
299 bus_check->ParticipationType, 4,
300 IA32X64_BUS_CHECK_INFO_PARTICIPATION_TYPES_KEYS,
301 IA32X64_BUS_CHECK_INFO_PARTICIPATION_TYPES_VALUES, "Unknown");
302 json_object_object_add(bus_check_ir, "participationType",
303 participation_type);
Lawrence Tang794312c2022-07-05 14:46:10 +0100304
Lawrence Tange407b4c2022-07-21 13:54:01 +0100305 //Address space.
306 json_object *address_space = integer_to_readable_pair(
307 bus_check->AddressSpace, 4,
308 IA32X64_BUS_CHECK_INFO_ADDRESS_SPACE_TYPES_KEYS,
309 IA32X64_BUS_CHECK_INFO_ADDRESS_SPACE_TYPES_VALUES, "Unknown");
310 json_object_object_add(bus_check_ir, "addressSpace", address_space);
311
312 return bus_check_ir;
Lawrence Tang794312c2022-07-05 14:46:10 +0100313}
314
315//Converts a single IA32/x64 MS check check info block into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100316json_object *cper_ia32x64_ms_check_to_ir(EFI_IA32_X64_MS_CHECK_INFO *ms_check)
Lawrence Tang794312c2022-07-05 14:46:10 +0100317{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100318 json_object *ms_check_ir = json_object_new_object();
Lawrence Tang794312c2022-07-05 14:46:10 +0100319
Lawrence Tange407b4c2022-07-21 13:54:01 +0100320 //Validation bits.
321 json_object *validation = bitfield_to_ir(
322 ms_check->ValidFields, 6,
323 IA32X64_CHECK_INFO_MS_CHECK_VALID_BITFIELD_NAMES);
324 json_object_object_add(ms_check_ir, "validationBits", validation);
Lawrence Tang794312c2022-07-05 14:46:10 +0100325
Lawrence Tange407b4c2022-07-21 13:54:01 +0100326 //Error type (operation that caused the error).
327 json_object *error_type = integer_to_readable_pair(
328 ms_check->ErrorType, 4, IA32X64_MS_CHECK_INFO_ERROR_TYPES_KEYS,
329 IA32X64_MS_CHECK_INFO_ERROR_TYPES_VALUES,
330 "Unknown (Processor Specific)");
331 json_object_object_add(ms_check_ir, "errorType", error_type);
Lawrence Tang794312c2022-07-05 14:46:10 +0100332
Lawrence Tange407b4c2022-07-21 13:54:01 +0100333 //Miscellaneous fields.
334 json_object_object_add(
335 ms_check_ir, "processorContextCorrupt",
336 json_object_new_boolean(ms_check->ContextCorrupt));
337 json_object_object_add(
338 ms_check_ir, "uncorrected",
339 json_object_new_boolean(ms_check->ErrorUncorrected));
340 json_object_object_add(ms_check_ir, "preciseIP",
341 json_object_new_boolean(ms_check->PreciseIp));
342 json_object_object_add(
343 ms_check_ir, "restartableIP",
344 json_object_new_boolean(ms_check->RestartableIp));
345 json_object_object_add(ms_check_ir, "overflow",
346 json_object_new_boolean(ms_check->Overflow));
347
348 return ms_check_ir;
Lawrence Tang794312c2022-07-05 14:46:10 +0100349}
350
351//Converts a single IA32/x64 processor context info entry into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100352json_object *cper_ia32x64_processor_context_info_to_ir(
353 EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *context_info, void **cur_pos)
Lawrence Tang794312c2022-07-05 14:46:10 +0100354{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100355 json_object *context_info_ir = json_object_new_object();
Lawrence Tang794312c2022-07-05 14:46:10 +0100356
Lawrence Tange407b4c2022-07-21 13:54:01 +0100357 //Register context type.
358 json_object *context_type = integer_to_readable_pair(
359 context_info->RegisterType, 8,
360 IA32X64_REGISTER_CONTEXT_TYPES_KEYS,
361 IA32X64_REGISTER_CONTEXT_TYPES_VALUES, "Unknown (Reserved)");
362 json_object_object_add(context_info_ir, "registerContextType",
363 context_type);
Lawrence Tang794312c2022-07-05 14:46:10 +0100364
Lawrence Tange407b4c2022-07-21 13:54:01 +0100365 //Register array size, MSR and MM address.
366 json_object_object_add(context_info_ir, "registerArraySize",
367 json_object_new_uint64(context_info->ArraySize));
368 json_object_object_add(
369 context_info_ir, "msrAddress",
370 json_object_new_uint64(context_info->MsrAddress));
371 json_object_object_add(
372 context_info_ir, "mmRegisterAddress",
373 json_object_new_uint64(context_info->MmRegisterAddress));
Lawrence Tang794312c2022-07-05 14:46:10 +0100374
Lawrence Tange407b4c2022-07-21 13:54:01 +0100375 //Register array.
376 json_object *register_array = NULL;
377 if (context_info->RegisterType == EFI_REG_CONTEXT_TYPE_IA32) {
378 EFI_CONTEXT_IA32_REGISTER_STATE *register_state =
379 (EFI_CONTEXT_IA32_REGISTER_STATE *)(context_info + 1);
380 register_array =
381 cper_ia32x64_register_32bit_to_ir(register_state);
382 *cur_pos = (void *)(register_state + 1);
383 } else if (context_info->RegisterType == EFI_REG_CONTEXT_TYPE_X64) {
384 EFI_CONTEXT_X64_REGISTER_STATE *register_state =
385 (EFI_CONTEXT_X64_REGISTER_STATE *)(context_info + 1);
386 register_array =
387 cper_ia32x64_register_64bit_to_ir(register_state);
388 *cur_pos = (void *)(register_state + 1);
389 } else {
390 //No parseable data, just dump as base64 and shift the head to the next item.
391 *cur_pos = (void *)(context_info + 1);
Lawrence Tangd7e8ca32022-07-07 10:25:53 +0100392
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700393 int32_t encoded_len = 0;
394 char *encoded = base64_encode((UINT8 *)*cur_pos,
395 context_info->ArraySize,
396 &encoded_len);
397 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800398 printf("Failed to allocate encode output buffer. \n");
399 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800400 register_array = json_object_new_object();
401 json_object_object_add(register_array, "data",
402 json_object_new_string_len(
403 encoded, encoded_len));
404 free(encoded);
405 }
Lawrence Tangd7e8ca32022-07-07 10:25:53 +0100406
Lawrence Tange407b4c2022-07-21 13:54:01 +0100407 *cur_pos =
408 (void *)(((char *)*cur_pos) + context_info->ArraySize);
409 }
410 json_object_object_add(context_info_ir, "registerArray",
411 register_array);
Lawrence Tang794312c2022-07-05 14:46:10 +0100412
Lawrence Tange407b4c2022-07-21 13:54:01 +0100413 return context_info_ir;
Lawrence Tang794312c2022-07-05 14:46:10 +0100414}
415
416//Converts a single CPER IA32 register state into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100417json_object *
418cper_ia32x64_register_32bit_to_ir(EFI_CONTEXT_IA32_REGISTER_STATE *registers)
Lawrence Tang794312c2022-07-05 14:46:10 +0100419{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100420 json_object *ia32_registers = json_object_new_object();
421 json_object_object_add(ia32_registers, "eax",
422 json_object_new_uint64(registers->Eax));
423 json_object_object_add(ia32_registers, "ebx",
424 json_object_new_uint64(registers->Ebx));
425 json_object_object_add(ia32_registers, "ecx",
426 json_object_new_uint64(registers->Ecx));
427 json_object_object_add(ia32_registers, "edx",
428 json_object_new_uint64(registers->Edx));
429 json_object_object_add(ia32_registers, "esi",
430 json_object_new_uint64(registers->Esi));
431 json_object_object_add(ia32_registers, "edi",
432 json_object_new_uint64(registers->Edi));
433 json_object_object_add(ia32_registers, "ebp",
434 json_object_new_uint64(registers->Ebp));
435 json_object_object_add(ia32_registers, "esp",
436 json_object_new_uint64(registers->Esp));
437 json_object_object_add(ia32_registers, "cs",
438 json_object_new_uint64(registers->Cs));
439 json_object_object_add(ia32_registers, "ds",
440 json_object_new_uint64(registers->Ds));
441 json_object_object_add(ia32_registers, "ss",
442 json_object_new_uint64(registers->Ss));
443 json_object_object_add(ia32_registers, "es",
444 json_object_new_uint64(registers->Es));
445 json_object_object_add(ia32_registers, "fs",
446 json_object_new_uint64(registers->Fs));
447 json_object_object_add(ia32_registers, "gs",
448 json_object_new_uint64(registers->Gs));
449 json_object_object_add(ia32_registers, "eflags",
450 json_object_new_uint64(registers->Eflags));
451 json_object_object_add(ia32_registers, "eip",
452 json_object_new_uint64(registers->Eip));
453 json_object_object_add(ia32_registers, "cr0",
454 json_object_new_uint64(registers->Cr0));
455 json_object_object_add(ia32_registers, "cr1",
456 json_object_new_uint64(registers->Cr1));
457 json_object_object_add(ia32_registers, "cr2",
458 json_object_new_uint64(registers->Cr2));
459 json_object_object_add(ia32_registers, "cr3",
460 json_object_new_uint64(registers->Cr3));
461 json_object_object_add(ia32_registers, "cr4",
462 json_object_new_uint64(registers->Cr4));
463 json_object_object_add(
464 ia32_registers, "gdtr",
465 json_object_new_uint64(registers->Gdtr[0] +
466 ((UINT64)registers->Gdtr[1] << 32)));
467 json_object_object_add(
468 ia32_registers, "idtr",
469 json_object_new_uint64(registers->Idtr[0] +
470 ((UINT64)registers->Idtr[1] << 32)));
471 json_object_object_add(ia32_registers, "ldtr",
472 json_object_new_uint64(registers->Ldtr));
473 json_object_object_add(ia32_registers, "tr",
474 json_object_new_uint64(registers->Tr));
Lawrence Tang794312c2022-07-05 14:46:10 +0100475
Lawrence Tange407b4c2022-07-21 13:54:01 +0100476 return ia32_registers;
Lawrence Tang794312c2022-07-05 14:46:10 +0100477}
478
479//Converts a single CPER x64 register state into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100480json_object *
481cper_ia32x64_register_64bit_to_ir(EFI_CONTEXT_X64_REGISTER_STATE *registers)
Lawrence Tang794312c2022-07-05 14:46:10 +0100482{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100483 json_object *x64_registers = json_object_new_object();
484 json_object_object_add(x64_registers, "rax",
485 json_object_new_uint64(registers->Rax));
486 json_object_object_add(x64_registers, "rbx",
487 json_object_new_uint64(registers->Rbx));
488 json_object_object_add(x64_registers, "rcx",
489 json_object_new_uint64(registers->Rcx));
490 json_object_object_add(x64_registers, "rdx",
491 json_object_new_uint64(registers->Rdx));
492 json_object_object_add(x64_registers, "rsi",
493 json_object_new_uint64(registers->Rsi));
494 json_object_object_add(x64_registers, "rdi",
495 json_object_new_uint64(registers->Rdi));
496 json_object_object_add(x64_registers, "rbp",
497 json_object_new_uint64(registers->Rbp));
498 json_object_object_add(x64_registers, "rsp",
499 json_object_new_uint64(registers->Rsp));
500 json_object_object_add(x64_registers, "r8",
501 json_object_new_uint64(registers->R8));
502 json_object_object_add(x64_registers, "r9",
503 json_object_new_uint64(registers->R9));
504 json_object_object_add(x64_registers, "r10",
505 json_object_new_uint64(registers->R10));
506 json_object_object_add(x64_registers, "r11",
507 json_object_new_uint64(registers->R11));
508 json_object_object_add(x64_registers, "r12",
509 json_object_new_uint64(registers->R12));
510 json_object_object_add(x64_registers, "r13",
511 json_object_new_uint64(registers->R13));
512 json_object_object_add(x64_registers, "r14",
513 json_object_new_uint64(registers->R14));
514 json_object_object_add(x64_registers, "r15",
515 json_object_new_uint64(registers->R15));
516 json_object_object_add(x64_registers, "cs",
517 json_object_new_int(registers->Cs));
518 json_object_object_add(x64_registers, "ds",
519 json_object_new_int(registers->Ds));
520 json_object_object_add(x64_registers, "ss",
521 json_object_new_int(registers->Ss));
522 json_object_object_add(x64_registers, "es",
523 json_object_new_int(registers->Es));
524 json_object_object_add(x64_registers, "fs",
525 json_object_new_int(registers->Fs));
526 json_object_object_add(x64_registers, "gs",
527 json_object_new_int(registers->Gs));
528 json_object_object_add(x64_registers, "rflags",
529 json_object_new_uint64(registers->Rflags));
530 json_object_object_add(x64_registers, "eip",
531 json_object_new_uint64(registers->Rip));
532 json_object_object_add(x64_registers, "cr0",
533 json_object_new_uint64(registers->Cr0));
534 json_object_object_add(x64_registers, "cr1",
535 json_object_new_uint64(registers->Cr1));
536 json_object_object_add(x64_registers, "cr2",
537 json_object_new_uint64(registers->Cr2));
538 json_object_object_add(x64_registers, "cr3",
539 json_object_new_uint64(registers->Cr3));
540 json_object_object_add(x64_registers, "cr4",
541 json_object_new_uint64(registers->Cr4));
542 json_object_object_add(x64_registers, "cr8",
543 json_object_new_uint64(registers->Cr8));
544 json_object_object_add(x64_registers, "gdtr_0",
545 json_object_new_uint64(registers->Gdtr[0]));
546 json_object_object_add(x64_registers, "gdtr_1",
547 json_object_new_uint64(registers->Gdtr[1]));
548 json_object_object_add(x64_registers, "idtr_0",
549 json_object_new_uint64(registers->Idtr[0]));
550 json_object_object_add(x64_registers, "idtr_1",
551 json_object_new_uint64(registers->Idtr[1]));
552 json_object_object_add(x64_registers, "ldtr",
553 json_object_new_int(registers->Ldtr));
554 json_object_object_add(x64_registers, "tr",
555 json_object_new_int(registers->Tr));
Lawrence Tang794312c2022-07-05 14:46:10 +0100556
Lawrence Tange407b4c2022-07-21 13:54:01 +0100557 return x64_registers;
Lawrence Tangd0c88842022-07-13 14:51:17 +0100558}
559
560//////////////////
561/// IR TO CPER ///
562//////////////////
563
564//Converts a single IA32/x64 CPER-JSON section into CPER binary, outputting to the provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100565void ir_section_ia32x64_to_cper(json_object *section, FILE *out)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100566{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100567 EFI_IA32_X64_PROCESSOR_ERROR_RECORD *section_cper =
568 (EFI_IA32_X64_PROCESSOR_ERROR_RECORD *)calloc(
569 1, sizeof(EFI_IA32_X64_PROCESSOR_ERROR_RECORD));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100570
Lawrence Tange407b4c2022-07-21 13:54:01 +0100571 //Validation bits.
572 json_object *validation =
573 json_object_object_get(section, "validationBits");
574 section_cper->ValidFields = 0x0;
575 section_cper->ValidFields |= json_object_get_boolean(
576 json_object_object_get(validation, "localAPICIDValid"));
577 section_cper->ValidFields |=
578 json_object_get_boolean(
579 json_object_object_get(validation, "cpuIDInfoValid"))
580 << 1;
581 int proc_error_info_num =
582 json_object_get_int(json_object_object_get(
583 validation, "processorErrorInfoNum")) &
John Chungf8fc7052024-05-03 20:05:29 +0800584 0x3F;
Lawrence Tange407b4c2022-07-21 13:54:01 +0100585 int proc_ctx_info_num =
586 json_object_get_int(json_object_object_get(
587 validation, "processorContextInfoNum")) &
John Chungf8fc7052024-05-03 20:05:29 +0800588 0x3F;
Lawrence Tange407b4c2022-07-21 13:54:01 +0100589 section_cper->ValidFields |= proc_error_info_num << 2;
590 section_cper->ValidFields |= proc_ctx_info_num << 8;
Lawrence Tangd0c88842022-07-13 14:51:17 +0100591
Lawrence Tange407b4c2022-07-21 13:54:01 +0100592 //Local APIC ID.
593 section_cper->ApicId = json_object_get_uint64(
594 json_object_object_get(section, "localAPICID"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100595
Lawrence Tange407b4c2022-07-21 13:54:01 +0100596 //CPUID info.
597 json_object *cpuid_info = json_object_object_get(section, "cpuidInfo");
598 EFI_IA32_X64_CPU_ID *cpuid_info_cper =
599 (EFI_IA32_X64_CPU_ID *)section_cper->CpuIdInfo;
600 cpuid_info_cper->Eax = json_object_get_uint64(
601 json_object_object_get(cpuid_info, "eax"));
602 cpuid_info_cper->Ebx = json_object_get_uint64(
603 json_object_object_get(cpuid_info, "ebx"));
604 cpuid_info_cper->Ecx = json_object_get_uint64(
605 json_object_object_get(cpuid_info, "ecx"));
606 cpuid_info_cper->Edx = json_object_get_uint64(
607 json_object_object_get(cpuid_info, "edx"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100608
Lawrence Tange407b4c2022-07-21 13:54:01 +0100609 //Flush the header to file before dealing w/ info sections.
610 fwrite(section_cper, sizeof(EFI_IA32_X64_PROCESSOR_ERROR_RECORD), 1,
611 out);
612 fflush(out);
613 free(section_cper);
614
615 //Iterate and deal with sections.
616 json_object *error_info =
617 json_object_object_get(section, "processorErrorInfo");
618 json_object *context_info =
619 json_object_object_get(section, "processorContextInfo");
John Chungf8fc7052024-05-03 20:05:29 +0800620 for (int i = 0; i < proc_error_info_num; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100621 ir_ia32x64_error_info_to_cper(
622 json_object_array_get_idx(error_info, i), out);
John Chungf8fc7052024-05-03 20:05:29 +0800623 }
624 for (int i = 0; i < proc_ctx_info_num; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100625 ir_ia32x64_context_info_to_cper(
626 json_object_array_get_idx(context_info, i), out);
John Chungf8fc7052024-05-03 20:05:29 +0800627 }
Lawrence Tangd0c88842022-07-13 14:51:17 +0100628}
629
630//Converts a single CPER-JSON IA32/x64 error information structure into CPER binary, outputting to the
631//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100632void ir_ia32x64_error_info_to_cper(json_object *error_info, FILE *out)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100633{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100634 EFI_IA32_X64_PROCESS_ERROR_INFO *error_info_cper =
635 (EFI_IA32_X64_PROCESS_ERROR_INFO *)calloc(
636 1, sizeof(EFI_IA32_X64_PROCESS_ERROR_INFO));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100637
Lawrence Tange407b4c2022-07-21 13:54:01 +0100638 //Error structure type.
Lawrence Tang3592da72022-07-21 16:50:07 +0100639 json_object *type = json_object_object_get(error_info, "type");
640 string_to_guid(
641 &error_info_cper->ErrorType,
642 json_object_get_string(json_object_object_get(type, "guid")));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100643
Lawrence Tange407b4c2022-07-21 13:54:01 +0100644 //Validation bits.
645 error_info_cper->ValidFields = ir_to_bitfield(
646 json_object_object_get(error_info, "validationBits"), 5,
647 IA32X64_PROCESSOR_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100648
Lawrence Tange407b4c2022-07-21 13:54:01 +0100649 //Check information, parsed based on the error type.
650 json_object *check_info =
651 json_object_object_get(error_info, "checkInfo");
652 if (guid_equal(&error_info_cper->ErrorType,
653 &gEfiIa32x64ErrorTypeCacheCheckGuid) ||
654 guid_equal(&error_info_cper->ErrorType,
655 &gEfiIa32x64ErrorTypeTlbCheckGuid)) {
656 ir_ia32x64_cache_tlb_check_error_to_cper(
657 check_info,
658 (EFI_IA32_X64_CACHE_CHECK_INFO *)&error_info_cper
659 ->CheckInfo);
660 } else if (guid_equal(&error_info_cper->ErrorType,
John Chungf8fc7052024-05-03 20:05:29 +0800661 &gEfiIa32x64ErrorTypeBusCheckGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100662 ir_ia32x64_bus_check_error_to_cper(
663 check_info,
664 (EFI_IA32_X64_BUS_CHECK_INFO *)&error_info_cper
665 ->CheckInfo);
John Chungf8fc7052024-05-03 20:05:29 +0800666 } else if (guid_equal(&error_info_cper->ErrorType,
667 &gEfiIa32x64ErrorTypeMsCheckGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100668 ir_ia32x64_ms_check_error_to_cper(
669 check_info,
670 (EFI_IA32_X64_MS_CHECK_INFO *)&error_info_cper
671 ->CheckInfo);
John Chungf8fc7052024-05-03 20:05:29 +0800672 }
Lawrence Tangd0c88842022-07-13 14:51:17 +0100673
Lawrence Tange407b4c2022-07-21 13:54:01 +0100674 //Miscellaneous numeric fields.
675 error_info_cper->TargetId = json_object_get_uint64(
676 json_object_object_get(error_info, "targetAddressID"));
677 error_info_cper->RequestorId = json_object_get_uint64(
678 json_object_object_get(error_info, "requestorID"));
679 error_info_cper->ResponderId = json_object_get_uint64(
680 json_object_object_get(error_info, "responderID"));
681 error_info_cper->InstructionIP = json_object_get_uint64(
682 json_object_object_get(error_info, "instructionPointer"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100683
Lawrence Tange407b4c2022-07-21 13:54:01 +0100684 //Write out to stream, then free resources.
685 fwrite(error_info_cper, sizeof(EFI_IA32_X64_PROCESS_ERROR_INFO), 1,
686 out);
687 fflush(out);
688 free(error_info_cper);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100689}
690
691//Converts a single CPER-JSON IA32/x64 cache/TLB check error info structure to CPER binary.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100692void ir_ia32x64_cache_tlb_check_error_to_cper(
693 json_object *check_info, EFI_IA32_X64_CACHE_CHECK_INFO *check_info_cper)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100694{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100695 //Validation bits.
696 check_info_cper->ValidFields = ir_to_bitfield(
697 json_object_object_get(check_info, "validationBits"), 8,
698 IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100699
Lawrence Tange407b4c2022-07-21 13:54:01 +0100700 //Transaction type, operation.
701 check_info_cper->TransactionType = readable_pair_to_integer(
702 json_object_object_get(check_info, "transactionType"));
703 check_info_cper->Operation = readable_pair_to_integer(
704 json_object_object_get(check_info, "operation"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100705
Lawrence Tange407b4c2022-07-21 13:54:01 +0100706 //Miscellaneous raw value fields.
707 check_info_cper->Level = json_object_get_uint64(
708 json_object_object_get(check_info, "level"));
709 check_info_cper->ContextCorrupt = json_object_get_boolean(
710 json_object_object_get(check_info, "processorContextCorrupt"));
711 check_info_cper->ErrorUncorrected = json_object_get_boolean(
712 json_object_object_get(check_info, "uncorrected"));
713 check_info_cper->PreciseIp = json_object_get_boolean(
714 json_object_object_get(check_info, "preciseIP"));
715 check_info_cper->RestartableIp = json_object_get_boolean(
716 json_object_object_get(check_info, "restartableIP"));
717 check_info_cper->Overflow = json_object_get_boolean(
718 json_object_object_get(check_info, "overflow"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100719}
720
721//Converts a single CPER-JSON IA32/x64 bus error info structure to CPER binary.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100722void ir_ia32x64_bus_check_error_to_cper(
723 json_object *check_info, EFI_IA32_X64_BUS_CHECK_INFO *check_info_cper)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100724{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100725 //Validation bits.
726 check_info_cper->ValidFields = ir_to_bitfield(
727 json_object_object_get(check_info, "validationBits"), 11,
728 IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100729
Lawrence Tange407b4c2022-07-21 13:54:01 +0100730 //Readable pair fields.
731 check_info_cper->TransactionType = readable_pair_to_integer(
732 json_object_object_get(check_info, "transactionType"));
733 check_info_cper->Operation = readable_pair_to_integer(
734 json_object_object_get(check_info, "operation"));
735 check_info_cper->ParticipationType = readable_pair_to_integer(
736 json_object_object_get(check_info, "participationType"));
737 check_info_cper->AddressSpace = readable_pair_to_integer(
738 json_object_object_get(check_info, "addressSpace"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100739
Lawrence Tange407b4c2022-07-21 13:54:01 +0100740 //Miscellaneous raw value fields.
741 check_info_cper->Level = json_object_get_uint64(
742 json_object_object_get(check_info, "level"));
743 check_info_cper->ContextCorrupt = json_object_get_boolean(
744 json_object_object_get(check_info, "processorContextCorrupt"));
745 check_info_cper->ErrorUncorrected = json_object_get_boolean(
746 json_object_object_get(check_info, "uncorrected"));
747 check_info_cper->PreciseIp = json_object_get_boolean(
748 json_object_object_get(check_info, "preciseIP"));
749 check_info_cper->RestartableIp = json_object_get_boolean(
750 json_object_object_get(check_info, "restartableIP"));
751 check_info_cper->Overflow = json_object_get_boolean(
752 json_object_object_get(check_info, "overflow"));
753 check_info_cper->TimeOut = json_object_get_boolean(
754 json_object_object_get(check_info, "timedOut"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100755}
756
757//Converts a single CPER-JSON IA32/x64 MS error info structure to CPER binary.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100758void ir_ia32x64_ms_check_error_to_cper(
759 json_object *check_info, EFI_IA32_X64_MS_CHECK_INFO *check_info_cper)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100760{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100761 //Validation bits.
762 check_info_cper->ValidFields = ir_to_bitfield(
763 json_object_object_get(check_info, "validationBits"), 6,
764 IA32X64_CHECK_INFO_MS_CHECK_VALID_BITFIELD_NAMES);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100765
Lawrence Tange407b4c2022-07-21 13:54:01 +0100766 //Type of MS check error.
767 check_info_cper->ErrorType = readable_pair_to_integer(
768 json_object_object_get(check_info, "errorType"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100769
Lawrence Tange407b4c2022-07-21 13:54:01 +0100770 //Miscellaneous raw value fields.
771 check_info_cper->ContextCorrupt = json_object_get_boolean(
772 json_object_object_get(check_info, "processorContextCorrupt"));
773 check_info_cper->ErrorUncorrected = json_object_get_boolean(
774 json_object_object_get(check_info, "uncorrected"));
775 check_info_cper->PreciseIp = json_object_get_boolean(
776 json_object_object_get(check_info, "preciseIP"));
777 check_info_cper->RestartableIp = json_object_get_boolean(
778 json_object_object_get(check_info, "restartableIP"));
779 check_info_cper->Overflow = json_object_get_boolean(
780 json_object_object_get(check_info, "overflow"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100781}
782
783//Converts a single CPER-JSON IA32/x64 context information structure into CPER binary, outputting to the
784//provided stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100785void ir_ia32x64_context_info_to_cper(json_object *context_info, FILE *out)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100786{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100787 EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *context_info_cper =
788 (EFI_IA32_X64_PROCESSOR_CONTEXT_INFO *)calloc(
789 1, sizeof(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100790
Lawrence Tange407b4c2022-07-21 13:54:01 +0100791 //Register context type.
792 context_info_cper->RegisterType = (UINT16)readable_pair_to_integer(
793 json_object_object_get(context_info, "registerContextType"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100794
Lawrence Tange407b4c2022-07-21 13:54:01 +0100795 //Miscellaneous numeric fields.
796 context_info_cper->ArraySize = (UINT16)json_object_get_uint64(
797 json_object_object_get(context_info, "registerArraySize"));
798 context_info_cper->MsrAddress = (UINT32)json_object_get_uint64(
799 json_object_object_get(context_info, "msrAddress"));
800 context_info_cper->MmRegisterAddress = json_object_get_uint64(
801 json_object_object_get(context_info, "mmRegisterAddress"));
Lawrence Tangd0c88842022-07-13 14:51:17 +0100802
Lawrence Tange407b4c2022-07-21 13:54:01 +0100803 //Flush header to stream.
804 fwrite(context_info_cper, sizeof(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO),
805 1, out);
806 fflush(out);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100807
Lawrence Tange407b4c2022-07-21 13:54:01 +0100808 //Handle the register array, depending on type provided.
809 json_object *register_array =
810 json_object_object_get(context_info, "registerArray");
811 if (context_info_cper->RegisterType == EFI_REG_CONTEXT_TYPE_IA32) {
812 ir_ia32x64_ia32_registers_to_cper(register_array, out);
813 } else if (context_info_cper->RegisterType ==
814 EFI_REG_CONTEXT_TYPE_X64) {
815 ir_ia32x64_x64_registers_to_cper(register_array, out);
816 } else {
817 //Unknown/structure is not defined.
818 json_object *encoded =
819 json_object_object_get(register_array, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700820 int32_t decoded_len = 0;
821 const char *j_string = json_object_get_string(encoded);
822 int j_size = json_object_get_string_len(encoded);
823 UINT8 *decoded = base64_decode(j_string, j_size, &decoded_len);
824 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800825 printf("Failed to allocate decode output buffer. \n");
826 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800827 fwrite(decoded, decoded_len, 1, out);
828 fflush(out);
829 free(decoded);
830 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100831 }
Lawrence Tangd0c88842022-07-13 14:51:17 +0100832
Lawrence Tange407b4c2022-07-21 13:54:01 +0100833 //Free remaining resources.
834 free(context_info_cper);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100835}
836
837//Converts a single CPER-JSON IA32 register array into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100838void ir_ia32x64_ia32_registers_to_cper(json_object *registers, FILE *out)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100839{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100840 EFI_CONTEXT_IA32_REGISTER_STATE register_state;
841 register_state.Eax = (UINT32)json_object_get_uint64(
842 json_object_object_get(registers, "eax"));
843 register_state.Ebx = (UINT32)json_object_get_uint64(
844 json_object_object_get(registers, "ebx"));
845 register_state.Ecx = (UINT32)json_object_get_uint64(
846 json_object_object_get(registers, "ecx"));
847 register_state.Edx = (UINT32)json_object_get_uint64(
848 json_object_object_get(registers, "edx"));
849 register_state.Esi = (UINT32)json_object_get_uint64(
850 json_object_object_get(registers, "esi"));
851 register_state.Edi = (UINT32)json_object_get_uint64(
852 json_object_object_get(registers, "edi"));
853 register_state.Ebp = (UINT32)json_object_get_uint64(
854 json_object_object_get(registers, "ebp"));
855 register_state.Esp = (UINT32)json_object_get_uint64(
856 json_object_object_get(registers, "esp"));
857 register_state.Cs = (UINT16)json_object_get_uint64(
858 json_object_object_get(registers, "cs"));
859 register_state.Ds = (UINT32)json_object_get_uint64(
860 json_object_object_get(registers, "ds"));
861 register_state.Ss = (UINT16)json_object_get_uint64(
862 json_object_object_get(registers, "ss"));
863 register_state.Es = (UINT16)json_object_get_uint64(
864 json_object_object_get(registers, "es"));
865 register_state.Fs = (UINT16)json_object_get_uint64(
866 json_object_object_get(registers, "fs"));
867 register_state.Gs = (UINT16)json_object_get_uint64(
868 json_object_object_get(registers, "gs"));
869 register_state.Eflags = (UINT32)json_object_get_uint64(
870 json_object_object_get(registers, "eflags"));
871 register_state.Eip = (UINT32)json_object_get_uint64(
872 json_object_object_get(registers, "eip"));
873 register_state.Cr0 = (UINT32)json_object_get_uint64(
874 json_object_object_get(registers, "cr0"));
875 register_state.Cr1 = (UINT32)json_object_get_uint64(
876 json_object_object_get(registers, "cr1"));
877 register_state.Cr2 = (UINT32)json_object_get_uint64(
878 json_object_object_get(registers, "cr2"));
879 register_state.Cr3 = (UINT32)json_object_get_uint64(
880 json_object_object_get(registers, "cr3"));
881 register_state.Cr4 = (UINT32)json_object_get_uint64(
882 json_object_object_get(registers, "cr4"));
Lawrence Tangbd9a84a2022-07-13 15:41:16 +0100883
Lawrence Tange407b4c2022-07-21 13:54:01 +0100884 //64-bit registers are split into two 32-bit parts.
885 UINT64 gdtr = json_object_get_uint64(
886 json_object_object_get(registers, "gdtr"));
887 register_state.Gdtr[0] = gdtr & 0xFFFFFFFF;
888 register_state.Gdtr[1] = gdtr >> 32;
889 UINT64 idtr = json_object_get_uint64(
890 json_object_object_get(registers, "idtr"));
891 register_state.Idtr[0] = idtr & 0xFFFFFFFF;
892 register_state.Idtr[1] = idtr >> 32;
Lawrence Tangbd9a84a2022-07-13 15:41:16 +0100893
Lawrence Tange407b4c2022-07-21 13:54:01 +0100894 //16-bit registers.
895 register_state.Ldtr = (UINT16)json_object_get_uint64(
896 json_object_object_get(registers, "ldtr"));
897 register_state.Tr = (UINT16)json_object_get_uint64(
898 json_object_object_get(registers, "tr"));
Lawrence Tangbd9a84a2022-07-13 15:41:16 +0100899
Lawrence Tange407b4c2022-07-21 13:54:01 +0100900 //Write out to stream.
901 fwrite(&register_state, sizeof(EFI_CONTEXT_IA32_REGISTER_STATE), 1,
902 out);
903 fflush(out);
Lawrence Tangd0c88842022-07-13 14:51:17 +0100904}
905
906//Converts a single CPER-JSON x64 register array into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100907void ir_ia32x64_x64_registers_to_cper(json_object *registers, FILE *out)
Lawrence Tangd0c88842022-07-13 14:51:17 +0100908{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100909 EFI_CONTEXT_X64_REGISTER_STATE register_state;
910 register_state.Rax = json_object_get_uint64(
911 json_object_object_get(registers, "rax"));
912 register_state.Rbx = json_object_get_uint64(
913 json_object_object_get(registers, "rbx"));
914 register_state.Rcx = json_object_get_uint64(
915 json_object_object_get(registers, "rcx"));
916 register_state.Rdx = json_object_get_uint64(
917 json_object_object_get(registers, "rdx"));
918 register_state.Rsi = json_object_get_uint64(
919 json_object_object_get(registers, "rsi"));
920 register_state.Rdi = json_object_get_uint64(
921 json_object_object_get(registers, "rdi"));
922 register_state.Rbp = json_object_get_uint64(
923 json_object_object_get(registers, "rbp"));
924 register_state.Rsp = json_object_get_uint64(
925 json_object_object_get(registers, "rsp"));
926 register_state.R8 =
927 json_object_get_uint64(json_object_object_get(registers, "r8"));
928 register_state.R9 =
929 json_object_get_uint64(json_object_object_get(registers, "r9"));
930 register_state.R10 = json_object_get_uint64(
931 json_object_object_get(registers, "r10"));
932 register_state.R11 = json_object_get_uint64(
933 json_object_object_get(registers, "r11"));
934 register_state.R12 = json_object_get_uint64(
935 json_object_object_get(registers, "r12"));
936 register_state.R13 = json_object_get_uint64(
937 json_object_object_get(registers, "r13"));
938 register_state.R14 = json_object_get_uint64(
939 json_object_object_get(registers, "r14"));
940 register_state.R15 = json_object_get_uint64(
941 json_object_object_get(registers, "r15"));
942 register_state.Cs = (UINT16)json_object_get_int(
943 json_object_object_get(registers, "cs"));
944 register_state.Ds = (UINT16)json_object_get_int(
945 json_object_object_get(registers, "ds"));
946 register_state.Ss = (UINT16)json_object_get_int(
947 json_object_object_get(registers, "ss"));
948 register_state.Es = (UINT16)json_object_get_int(
949 json_object_object_get(registers, "es"));
950 register_state.Fs = (UINT16)json_object_get_int(
951 json_object_object_get(registers, "fs"));
952 register_state.Gs = (UINT16)json_object_get_int(
953 json_object_object_get(registers, "gs"));
954 register_state.Resv1 = 0;
955 register_state.Rflags = json_object_get_uint64(
956 json_object_object_get(registers, "rflags"));
957 register_state.Rip = json_object_get_uint64(
958 json_object_object_get(registers, "eip"));
959 register_state.Cr0 = json_object_get_uint64(
960 json_object_object_get(registers, "cr0"));
961 register_state.Cr1 = json_object_get_uint64(
962 json_object_object_get(registers, "cr1"));
963 register_state.Cr2 = json_object_get_uint64(
964 json_object_object_get(registers, "cr2"));
965 register_state.Cr3 = json_object_get_uint64(
966 json_object_object_get(registers, "cr3"));
967 register_state.Cr4 = json_object_get_uint64(
968 json_object_object_get(registers, "cr4"));
969 register_state.Cr8 = json_object_get_uint64(
970 json_object_object_get(registers, "cr8"));
971 register_state.Gdtr[0] = json_object_get_uint64(
972 json_object_object_get(registers, "gdtr_0"));
973 register_state.Gdtr[1] = json_object_get_uint64(
974 json_object_object_get(registers, "gdtr_1"));
975 register_state.Idtr[0] = json_object_get_uint64(
976 json_object_object_get(registers, "idtr_0"));
977 register_state.Idtr[1] = json_object_get_uint64(
978 json_object_object_get(registers, "idtr_1"));
979 register_state.Ldtr = (UINT16)json_object_get_int(
980 json_object_object_get(registers, "ldtr"));
981 register_state.Tr = (UINT16)json_object_get_int(
982 json_object_object_get(registers, "tr"));
Lawrence Tangbd9a84a2022-07-13 15:41:16 +0100983
Lawrence Tange407b4c2022-07-21 13:54:01 +0100984 //Write out to stream.
985 fwrite(&register_state, sizeof(EFI_CONTEXT_X64_REGISTER_STATE), 1, out);
986 fflush(out);
John Chungf8fc7052024-05-03 20:05:29 +0800987}