blob: df22ce05698d0a46ad3398d3e0af33b667005f98 [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>
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-iommu.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 IOMMU specific DMAr CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080017json_object *cper_section_dmar_iommu_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_IOMMU_DMAR_ERROR_DATA)) {
20 return NULL;
21 }
22
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 EFI_IOMMU_DMAR_ERROR_DATA *iommu_error =
24 (EFI_IOMMU_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 //Revision.
28 json_object_object_add(section_ir, "revision",
29 json_object_new_int(iommu_error->Revision));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010030
Lawrence Tange407b4c2022-07-21 13:54:01 +010031 //IOMMU registers.
32 json_object_object_add(section_ir, "controlRegister",
33 json_object_new_uint64(iommu_error->Control));
34 json_object_object_add(section_ir, "statusRegister",
35 json_object_new_uint64(iommu_error->Status));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //IOMMU event log entry.
38 //The format of these entries differ widely by the type of error.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070039 int32_t encoded_len = 0;
40
41 char *encoded = base64_encode((UINT8 *)iommu_error->EventLogEntry, 16,
42 &encoded_len);
43 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070044 cper_print_log("Failed to allocate encode output buffer. \n");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070045
46 return NULL;
John Chungf8fc7052024-05-03 20:05:29 +080047 }
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070048 json_object_object_add(section_ir, "eventLogEntry",
49 json_object_new_string_len(encoded,
50 encoded_len));
51 free(encoded);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010052
Lawrence Tange407b4c2022-07-21 13:54:01 +010053 //Device table entry (as base64).
John Chungf8fc7052024-05-03 20:05:29 +080054 encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070055
56 encoded = base64_encode((UINT8 *)iommu_error->DeviceTableEntry, 32,
57 &encoded_len);
58 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070059 cper_print_log("Failed to allocate encode output buffer. \n");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070060 return NULL;
John Chungf8fc7052024-05-03 20:05:29 +080061 }
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070062 json_object_object_add(section_ir, "deviceTableEntry",
63 json_object_new_string_len(encoded,
64 encoded_len));
65 free(encoded);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010066
Lawrence Tange407b4c2022-07-21 13:54:01 +010067 //Page table entries.
68 json_object_object_add(section_ir, "pageTableEntry_Level6",
69 json_object_new_uint64(iommu_error->PteL6));
70 json_object_object_add(section_ir, "pageTableEntry_Level5",
71 json_object_new_uint64(iommu_error->PteL5));
72 json_object_object_add(section_ir, "pageTableEntry_Level4",
73 json_object_new_uint64(iommu_error->PteL4));
74 json_object_object_add(section_ir, "pageTableEntry_Level3",
75 json_object_new_uint64(iommu_error->PteL3));
76 json_object_object_add(section_ir, "pageTableEntry_Level2",
77 json_object_new_uint64(iommu_error->PteL2));
78 json_object_object_add(section_ir, "pageTableEntry_Level1",
79 json_object_new_uint64(iommu_error->PteL1));
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010080
Lawrence Tange407b4c2022-07-21 13:54:01 +010081 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010082}
83
84//Converts a single DMAR IOMMU CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010085void ir_section_dmar_iommu_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010086{
Lawrence Tange407b4c2022-07-21 13:54:01 +010087 EFI_IOMMU_DMAR_ERROR_DATA *section_cper =
88 (EFI_IOMMU_DMAR_ERROR_DATA *)calloc(
89 1, sizeof(EFI_IOMMU_DMAR_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010090
Lawrence Tange407b4c2022-07-21 13:54:01 +010091 //Revision, registers.
92 section_cper->Revision = (UINT8)json_object_get_int(
93 json_object_object_get(section, "revision"));
94 section_cper->Control = json_object_get_uint64(
95 json_object_object_get(section, "controlRegister"));
96 section_cper->Status = json_object_get_uint64(
97 json_object_object_get(section, "statusRegister"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010098
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 //IOMMU event log entry.
100 json_object *encoded = json_object_object_get(section, "eventLogEntry");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700101 int32_t decoded_len = 0;
102
103 UINT8 *decoded = base64_decode(json_object_get_string(encoded),
104 json_object_get_string_len(encoded),
105 &decoded_len);
106 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700107 cper_print_log("Failed to allocate decode output buffer. \n");
John Chungf8fc7052024-05-03 20:05:29 +0800108 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800109 memcpy(section_cper->EventLogEntry, decoded, decoded_len);
110 free(decoded);
111 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 //Device table entry.
113 encoded = json_object_object_get(section, "deviceTableEntry");
John Chungf8fc7052024-05-03 20:05:29 +0800114 decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700115
116 decoded = base64_decode(json_object_get_string(encoded),
117 json_object_get_string_len(encoded),
118 &decoded_len);
119 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700120 cper_print_log("Failed to allocate decode output buffer. \n");
John Chungf8fc7052024-05-03 20:05:29 +0800121 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800122 memcpy(section_cper->DeviceTableEntry, decoded, decoded_len);
123 free(decoded);
124 }
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100125
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 //Page table entries.
127 section_cper->PteL1 = json_object_get_uint64(
128 json_object_object_get(section, "pageTableEntry_Level1"));
129 section_cper->PteL2 = json_object_get_uint64(
130 json_object_object_get(section, "pageTableEntry_Level2"));
131 section_cper->PteL3 = json_object_get_uint64(
132 json_object_object_get(section, "pageTableEntry_Level3"));
133 section_cper->PteL4 = json_object_get_uint64(
134 json_object_object_get(section, "pageTableEntry_Level4"));
135 section_cper->PteL5 = json_object_get_uint64(
136 json_object_object_get(section, "pageTableEntry_Level5"));
137 section_cper->PteL6 = json_object_get_uint64(
138 json_object_object_get(section, "pageTableEntry_Level6"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100139
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 //Write to stream, free resources.
141 fwrite(section_cper, sizeof(EFI_IOMMU_DMAR_ERROR_DATA), 1, out);
142 fflush(out);
143 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800144}