1 /**
2  * Describes functions for converting VT-d specific 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 "b64.h"
10 #include "../edk/Cper.h"
11 #include "../cper-utils.h"
12 #include "cper-section-dmar-vtd.h"
13 
14 //Converts a single VT-d specific DMAr CPER section into JSON IR.
15 json_object* cper_section_dmar_vtd_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
16 {
17     EFI_DIRECTED_IO_DMAR_ERROR_DATA* vtd_error = (EFI_DIRECTED_IO_DMAR_ERROR_DATA*)section;
18     json_object* section_ir = json_object_new_object();
19 
20     //Version, revision and OEM ID, as defined in the VT-d architecture.
21     UINT64 oem_id = (vtd_error->OemId[0] << 16) + (vtd_error->OemId[1] << 8) + vtd_error->OemId[2];
22     json_object_object_add(section_ir, "version", json_object_new_int(vtd_error->Version));
23     json_object_object_add(section_ir, "revision", json_object_new_int(vtd_error->Revision));
24     json_object_object_add(section_ir, "oemID", json_object_new_uint64(oem_id));
25 
26     //Registers.
27     json_object_object_add(section_ir, "capabilityRegister", json_object_new_uint64(vtd_error->Capability));
28     json_object_object_add(section_ir, "extendedCapabilityRegister", json_object_new_uint64(vtd_error->CapabilityEx));
29     json_object_object_add(section_ir, "globalCommandRegister", json_object_new_uint64(vtd_error->GlobalCommand));
30     json_object_object_add(section_ir, "globalStatusRegister", json_object_new_uint64(vtd_error->GlobalStatus));
31     json_object_object_add(section_ir, "faultStatusRegister", json_object_new_uint64(vtd_error->FaultStatus));
32 
33     //Fault record basic fields.
34     json_object* fault_record_ir = json_object_new_object();
35     EFI_VTD_FAULT_RECORD* fault_record = (EFI_VTD_FAULT_RECORD*)vtd_error->FaultRecord;
36     json_object_object_add(fault_record_ir, "faultInformation", json_object_new_uint64(fault_record->FaultInformation));
37     json_object_object_add(fault_record_ir, "sourceIdentifier", json_object_new_uint64(fault_record->SourceIdentifier));
38     json_object_object_add(fault_record_ir, "privelegeModeRequested",
39         json_object_new_boolean(fault_record->PrivelegeModeRequested));
40     json_object_object_add(fault_record_ir, "executePermissionRequested",
41         json_object_new_boolean(fault_record->ExecutePermissionRequested));
42     json_object_object_add(fault_record_ir, "pasidPresent", json_object_new_boolean(fault_record->PasidPresent));
43     json_object_object_add(fault_record_ir, "faultReason", json_object_new_uint64(fault_record->FaultReason));
44     json_object_object_add(fault_record_ir, "pasidValue", json_object_new_uint64(fault_record->PasidValue));
45     json_object_object_add(fault_record_ir, "addressType", json_object_new_uint64(fault_record->AddressType));
46 
47     //Fault record address type.
48     json_object* fault_record_type = integer_to_readable_pair(fault_record->Type, 2,
49         VTD_FAULT_RECORD_TYPES_KEYS,
50         VTD_FAULT_RECORD_TYPES_VALUES,
51         "Unknown");
52     json_object_object_add(fault_record_ir, "type", fault_record_type);
53     json_object_object_add(section_ir, "faultRecord", fault_record_ir);
54 
55     //Root entry.
56     char* encoded = b64_encode((unsigned char*)vtd_error->RootEntry, 16);
57     json_object_object_add(section_ir, "rootEntry", json_object_new_string(encoded));
58     free(encoded);
59 
60     //Context entry.
61     encoded = b64_encode((unsigned char*)vtd_error->ContextEntry, 16);
62     json_object_object_add(section_ir, "contextEntry", json_object_new_string(encoded));
63     free(encoded);
64 
65     //PTE entry for all page levels.
66     json_object_object_add(section_ir, "pageTableEntry_Level6", json_object_new_uint64(vtd_error->PteL6));
67     json_object_object_add(section_ir, "pageTableEntry_Level5", json_object_new_uint64(vtd_error->PteL5));
68     json_object_object_add(section_ir, "pageTableEntry_Level4", json_object_new_uint64(vtd_error->PteL4));
69     json_object_object_add(section_ir, "pageTableEntry_Level3", json_object_new_uint64(vtd_error->PteL3));
70     json_object_object_add(section_ir, "pageTableEntry_Level2", json_object_new_uint64(vtd_error->PteL2));
71     json_object_object_add(section_ir, "pageTableEntry_Level1", json_object_new_uint64(vtd_error->PteL1));
72 
73     return section_ir;
74 }