1db1b7ce2SLawrence Tang /** 2db1b7ce2SLawrence Tang * Describes functions for converting IOMMU specific DMAr CPER sections from binary and JSON format 3db1b7ce2SLawrence Tang * into an intermediate format. 4db1b7ce2SLawrence Tang * 5db1b7ce2SLawrence Tang * Author: Lawrence.Tang@arm.com 6db1b7ce2SLawrence Tang **/ 7db1b7ce2SLawrence Tang #include <stdio.h> 8*205dd1d7SLawrence Tang #include <string.h> 9db1b7ce2SLawrence Tang #include "json.h" 10d7e8ca34SLawrence Tang #include "b64.h" 11db1b7ce2SLawrence Tang #include "../edk/Cper.h" 12db1b7ce2SLawrence Tang #include "../cper-utils.h" 13db1b7ce2SLawrence Tang #include "cper-section-dmar-iommu.h" 14db1b7ce2SLawrence Tang 15db1b7ce2SLawrence Tang //Converts a single IOMMU specific DMAr CPER section into JSON IR. 16db1b7ce2SLawrence Tang json_object* cper_section_dmar_iommu_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) 17db1b7ce2SLawrence Tang { 18db1b7ce2SLawrence Tang EFI_IOMMU_DMAR_ERROR_DATA* iommu_error = (EFI_IOMMU_DMAR_ERROR_DATA*)section; 19db1b7ce2SLawrence Tang json_object* section_ir = json_object_new_object(); 20db1b7ce2SLawrence Tang 21db1b7ce2SLawrence Tang //Revision. 22db1b7ce2SLawrence Tang json_object_object_add(section_ir, "revision", json_object_new_int(iommu_error->Revision)); 23db1b7ce2SLawrence Tang 24db1b7ce2SLawrence Tang //IOMMU registers. 25db1b7ce2SLawrence Tang json_object_object_add(section_ir, "controlRegister", json_object_new_uint64(iommu_error->Control)); 26db1b7ce2SLawrence Tang json_object_object_add(section_ir, "statusRegister", json_object_new_uint64(iommu_error->Status)); 27db1b7ce2SLawrence Tang 28db1b7ce2SLawrence Tang //IOMMU event log entry. 29ce0f82bfSLawrence Tang //The format of these entries differ widely by the type of error. 30ce0f82bfSLawrence Tang char* encoded = b64_encode((unsigned char*)iommu_error->EventLogEntry, 16); 31ce0f82bfSLawrence Tang json_object_object_add(section_ir, "eventLogEntry", json_object_new_string(encoded)); 32ce0f82bfSLawrence Tang free(encoded); 33db1b7ce2SLawrence Tang 34d7e8ca34SLawrence Tang //Device table entry (as base64). 35ce0f82bfSLawrence Tang encoded = b64_encode((unsigned char*)iommu_error->DeviceTableEntry, 32); 36d7e8ca34SLawrence Tang json_object_object_add(section_ir, "deviceTableEntry", json_object_new_string(encoded)); 37d7e8ca34SLawrence Tang free(encoded); 38db1b7ce2SLawrence Tang 39db1b7ce2SLawrence Tang //Page table entries. 40db1b7ce2SLawrence Tang json_object_object_add(section_ir, "pageTableEntry_Level6", json_object_new_uint64(iommu_error->PteL6)); 41db1b7ce2SLawrence Tang json_object_object_add(section_ir, "pageTableEntry_Level5", json_object_new_uint64(iommu_error->PteL5)); 42db1b7ce2SLawrence Tang json_object_object_add(section_ir, "pageTableEntry_Level4", json_object_new_uint64(iommu_error->PteL4)); 43db1b7ce2SLawrence Tang json_object_object_add(section_ir, "pageTableEntry_Level3", json_object_new_uint64(iommu_error->PteL3)); 44db1b7ce2SLawrence Tang json_object_object_add(section_ir, "pageTableEntry_Level2", json_object_new_uint64(iommu_error->PteL2)); 45db1b7ce2SLawrence Tang json_object_object_add(section_ir, "pageTableEntry_Level1", json_object_new_uint64(iommu_error->PteL1)); 46db1b7ce2SLawrence Tang 47db1b7ce2SLawrence Tang return section_ir; 48db1b7ce2SLawrence Tang } 49*205dd1d7SLawrence Tang 50*205dd1d7SLawrence Tang //Converts a single DMAR IOMMU CPER-JSON section into CPER binary, outputting to the given stream. 51*205dd1d7SLawrence Tang void ir_section_dmar_iommu_to_cper(json_object* section, FILE* out) 52*205dd1d7SLawrence Tang { 53*205dd1d7SLawrence Tang EFI_IOMMU_DMAR_ERROR_DATA* section_cper = 54*205dd1d7SLawrence Tang (EFI_IOMMU_DMAR_ERROR_DATA*)calloc(1, sizeof(EFI_IOMMU_DMAR_ERROR_DATA)); 55*205dd1d7SLawrence Tang 56*205dd1d7SLawrence Tang //Revision, registers. 57*205dd1d7SLawrence Tang section_cper->Revision = (UINT8)json_object_get_int(json_object_object_get(section, "revision")); 58*205dd1d7SLawrence Tang section_cper->Control = json_object_get_uint64(json_object_object_get(section, "controlRegister")); 59*205dd1d7SLawrence Tang section_cper->Status = json_object_get_uint64(json_object_object_get(section, "statusRegister")); 60*205dd1d7SLawrence Tang 61*205dd1d7SLawrence Tang //IOMMU event log entry. 62*205dd1d7SLawrence Tang json_object* encoded = json_object_object_get(section, "eventLogEntry"); 63*205dd1d7SLawrence Tang UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 64*205dd1d7SLawrence Tang memcpy(section_cper->EventLogEntry, decoded, 16); 65*205dd1d7SLawrence Tang free(decoded); 66*205dd1d7SLawrence Tang 67*205dd1d7SLawrence Tang //Device table entry. 68*205dd1d7SLawrence Tang encoded = json_object_object_get(section, "deviceTableEntry"); 69*205dd1d7SLawrence Tang decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); 70*205dd1d7SLawrence Tang memcpy(section_cper->DeviceTableEntry, decoded, 32); 71*205dd1d7SLawrence Tang free(decoded); 72*205dd1d7SLawrence Tang 73*205dd1d7SLawrence Tang //Page table entries. 74*205dd1d7SLawrence Tang section_cper->PteL1 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level1")); 75*205dd1d7SLawrence Tang section_cper->PteL2 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level2")); 76*205dd1d7SLawrence Tang section_cper->PteL3 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level3")); 77*205dd1d7SLawrence Tang section_cper->PteL4 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level4")); 78*205dd1d7SLawrence Tang section_cper->PteL5 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level5")); 79*205dd1d7SLawrence Tang section_cper->PteL6 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level6")); 80*205dd1d7SLawrence Tang 81*205dd1d7SLawrence Tang //Write to stream, free resources. 82*205dd1d7SLawrence Tang fwrite(§ion_cper, sizeof(section_cper), 1, out); 83*205dd1d7SLawrence Tang fflush(out); 84*205dd1d7SLawrence Tang free(section_cper); 85*205dd1d7SLawrence Tang }