blob: b813a6e48006763bfe38a0eb41d8a4ae51b2af0a [file] [log] [blame]
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +01001/**
2 * Describes functions for converting IOMMU 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>
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070010#include "base64.h"
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010011#include "../edk/Cper.h"
12#include "../cper-utils.h"
13#include "cper-section-dmar-iommu.h"
14
15//Converts a single IOMMU specific DMAr CPER section into JSON IR.
John Chungf8fc7052024-05-03 20:05:29 +080016json_object *cper_section_dmar_iommu_to_ir(void *section)
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010017{
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 EFI_IOMMU_DMAR_ERROR_DATA *iommu_error =
19 (EFI_IOMMU_DMAR_ERROR_DATA *)section;
20 json_object *section_ir = json_object_new_object();
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010021
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 //Revision.
23 json_object_object_add(section_ir, "revision",
24 json_object_new_int(iommu_error->Revision));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010025
Lawrence Tange407b4c2022-07-21 13:54:01 +010026 //IOMMU registers.
27 json_object_object_add(section_ir, "controlRegister",
28 json_object_new_uint64(iommu_error->Control));
29 json_object_object_add(section_ir, "statusRegister",
30 json_object_new_uint64(iommu_error->Status));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010031
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //IOMMU event log entry.
33 //The format of these entries differ widely by the type of error.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070034 int32_t encoded_len = 0;
35
36 char *encoded = base64_encode((UINT8 *)iommu_error->EventLogEntry, 16,
37 &encoded_len);
38 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +080039 printf("Failed to allocate encode output buffer. \n");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070040
41 return NULL;
John Chungf8fc7052024-05-03 20:05:29 +080042 }
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070043 json_object_object_add(section_ir, "eventLogEntry",
44 json_object_new_string_len(encoded,
45 encoded_len));
46 free(encoded);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010047
Lawrence Tange407b4c2022-07-21 13:54:01 +010048 //Device table entry (as base64).
John Chungf8fc7052024-05-03 20:05:29 +080049 encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070050
51 encoded = base64_encode((UINT8 *)iommu_error->DeviceTableEntry, 32,
52 &encoded_len);
53 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +080054 printf("Failed to allocate encode output buffer. \n");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070055 return NULL;
John Chungf8fc7052024-05-03 20:05:29 +080056 }
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070057 json_object_object_add(section_ir, "deviceTableEntry",
58 json_object_new_string_len(encoded,
59 encoded_len));
60 free(encoded);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010061
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 //Page table entries.
63 json_object_object_add(section_ir, "pageTableEntry_Level6",
64 json_object_new_uint64(iommu_error->PteL6));
65 json_object_object_add(section_ir, "pageTableEntry_Level5",
66 json_object_new_uint64(iommu_error->PteL5));
67 json_object_object_add(section_ir, "pageTableEntry_Level4",
68 json_object_new_uint64(iommu_error->PteL4));
69 json_object_object_add(section_ir, "pageTableEntry_Level3",
70 json_object_new_uint64(iommu_error->PteL3));
71 json_object_object_add(section_ir, "pageTableEntry_Level2",
72 json_object_new_uint64(iommu_error->PteL2));
73 json_object_object_add(section_ir, "pageTableEntry_Level1",
74 json_object_new_uint64(iommu_error->PteL1));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010075
Lawrence Tange407b4c2022-07-21 13:54:01 +010076 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010077}
78
79//Converts a single DMAR IOMMU CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010080void ir_section_dmar_iommu_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010081{
Lawrence Tange407b4c2022-07-21 13:54:01 +010082 EFI_IOMMU_DMAR_ERROR_DATA *section_cper =
83 (EFI_IOMMU_DMAR_ERROR_DATA *)calloc(
84 1, sizeof(EFI_IOMMU_DMAR_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010085
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 //Revision, registers.
87 section_cper->Revision = (UINT8)json_object_get_int(
88 json_object_object_get(section, "revision"));
89 section_cper->Control = json_object_get_uint64(
90 json_object_object_get(section, "controlRegister"));
91 section_cper->Status = json_object_get_uint64(
92 json_object_object_get(section, "statusRegister"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010093
Lawrence Tange407b4c2022-07-21 13:54:01 +010094 //IOMMU event log entry.
95 json_object *encoded = json_object_object_get(section, "eventLogEntry");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070096 int32_t decoded_len = 0;
97
98 UINT8 *decoded = base64_decode(json_object_get_string(encoded),
99 json_object_get_string_len(encoded),
100 &decoded_len);
101 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800102 printf("Failed to allocate decode output buffer. \n");
103 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800104 memcpy(section_cper->EventLogEntry, decoded, decoded_len);
105 free(decoded);
106 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100107 //Device table entry.
108 encoded = json_object_object_get(section, "deviceTableEntry");
John Chungf8fc7052024-05-03 20:05:29 +0800109 decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700110
111 decoded = base64_decode(json_object_get_string(encoded),
112 json_object_get_string_len(encoded),
113 &decoded_len);
114 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800115 printf("Failed to allocate decode output buffer. \n");
116 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800117 memcpy(section_cper->DeviceTableEntry, decoded, decoded_len);
118 free(decoded);
119 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100120
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 //Page table entries.
122 section_cper->PteL1 = json_object_get_uint64(
123 json_object_object_get(section, "pageTableEntry_Level1"));
124 section_cper->PteL2 = json_object_get_uint64(
125 json_object_object_get(section, "pageTableEntry_Level2"));
126 section_cper->PteL3 = json_object_get_uint64(
127 json_object_object_get(section, "pageTableEntry_Level3"));
128 section_cper->PteL4 = json_object_get_uint64(
129 json_object_object_get(section, "pageTableEntry_Level4"));
130 section_cper->PteL5 = json_object_get_uint64(
131 json_object_object_get(section, "pageTableEntry_Level5"));
132 section_cper->PteL6 = json_object_get_uint64(
133 json_object_object_get(section, "pageTableEntry_Level6"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100134
Lawrence Tange407b4c2022-07-21 13:54:01 +0100135 //Write to stream, free resources.
136 fwrite(section_cper, sizeof(EFI_IOMMU_DMAR_ERROR_DATA), 1, out);
137 fflush(out);
138 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800139}