blob: fa6b0b28d28e060ff1b54dabac6abcddd934069f [file] [log] [blame]
Lawrence Tang4dbe3d72022-07-06 13:51:01 +01001/**
2 * Describes functions for converting PCIe CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang4dbe3d72022-07-06 13:51:01 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang3b7f45b2022-07-14 14:14:30 +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"
Ed Tanousa3b7f8a2024-11-04 16:38:59 -080011#include "Cper.h"
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010012#include "../cper-utils.h"
13#include "cper-section-pcie.h"
14
15//Converts a single PCIe CPER section into JSON IR.
John Chungf8fc7052024-05-03 20:05:29 +080016json_object *cper_section_pcie_to_ir(void *section)
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010017{
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section;
19 json_object *section_ir = json_object_new_object();
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010020
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 //Validation bits.
22 json_object *validation = bitfield_to_ir(
23 pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES);
24 json_object_object_add(section_ir, "validationBits", validation);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010025
Lawrence Tange407b4c2022-07-21 13:54:01 +010026 //Port type.
27 json_object *port_type = integer_to_readable_pair(
28 pcie_error->PortType, 9, PCIE_ERROR_PORT_TYPES_KEYS,
29 PCIE_ERROR_PORT_TYPES_VALUES, "Unknown");
30 json_object_object_add(section_ir, "portType", port_type);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010031
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //Version, provided each half in BCD.
33 json_object *version = json_object_new_object();
34 json_object_object_add(
35 version, "minor",
36 json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF)));
37 json_object_object_add(
38 version, "major",
39 json_object_new_int(bcd_to_int(pcie_error->Version >> 8)));
40 json_object_object_add(section_ir, "version", version);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010041
Lawrence Tange407b4c2022-07-21 13:54:01 +010042 //Command & status.
43 json_object *command_status = json_object_new_object();
44 json_object_object_add(
45 command_status, "commandRegister",
46 json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF));
47 json_object_object_add(
48 command_status, "statusRegister",
49 json_object_new_uint64(pcie_error->CommandStatus >> 16));
50 json_object_object_add(section_ir, "commandStatus", command_status);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010051
Lawrence Tange407b4c2022-07-21 13:54:01 +010052 //PCIe Device ID.
53 json_object *device_id = json_object_new_object();
54 UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) +
55 (pcie_error->DevBridge.ClassCode[1] << 8) +
56 pcie_error->DevBridge.ClassCode[2];
57 json_object_object_add(
58 device_id, "vendorID",
59 json_object_new_uint64(pcie_error->DevBridge.VendorId));
60 json_object_object_add(
61 device_id, "deviceID",
62 json_object_new_uint64(pcie_error->DevBridge.DeviceId));
63 json_object_object_add(device_id, "classCode",
64 json_object_new_uint64(class_id));
65 json_object_object_add(
66 device_id, "functionNumber",
67 json_object_new_uint64(pcie_error->DevBridge.Function));
68 json_object_object_add(
69 device_id, "deviceNumber",
70 json_object_new_uint64(pcie_error->DevBridge.Device));
71 json_object_object_add(
72 device_id, "segmentNumber",
73 json_object_new_uint64(pcie_error->DevBridge.Segment));
74 json_object_object_add(
75 device_id, "primaryOrDeviceBusNumber",
76 json_object_new_uint64(
77 pcie_error->DevBridge.PrimaryOrDeviceBus));
78 json_object_object_add(
79 device_id, "secondaryBusNumber",
80 json_object_new_uint64(pcie_error->DevBridge.SecondaryBus));
81 json_object_object_add(
82 device_id, "slotNumber",
83 json_object_new_uint64(pcie_error->DevBridge.Slot.Number));
84 json_object_object_add(section_ir, "deviceID", device_id);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010085
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 //Device serial number.
87 json_object_object_add(section_ir, "deviceSerialNumber",
88 json_object_new_uint64(pcie_error->SerialNo));
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010089
Lawrence Tange407b4c2022-07-21 13:54:01 +010090 //Bridge control status.
91 json_object *bridge_control_status = json_object_new_object();
92 json_object_object_add(
93 bridge_control_status, "secondaryStatusRegister",
94 json_object_new_uint64(pcie_error->BridgeControlStatus &
95 0xFFFF));
96 json_object_object_add(
97 bridge_control_status, "controlRegister",
98 json_object_new_uint64(pcie_error->BridgeControlStatus >> 16));
99 json_object_object_add(section_ir, "bridgeControlStatus",
100 bridge_control_status);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100101
Lawrence Tange407b4c2022-07-21 13:54:01 +0100102 //Capability structure.
103 //The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
104 //(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
105 //to be a way to differentiate these, so this is left as a b64 dump.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700106 int32_t encoded_len = 0;
107
108 char *encoded = base64_encode((UINT8 *)pcie_error->Capability.PcieCap,
109 60, &encoded_len);
110 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800111 printf("Failed to allocate encode output buffer. \n");
112 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800113 json_object *capability = json_object_new_object();
114 json_object_object_add(capability, "data",
115 json_object_new_string_len(encoded,
116 encoded_len));
117 free(encoded);
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700118
John Chungf8fc7052024-05-03 20:05:29 +0800119 json_object_object_add(section_ir, "capabilityStructure",
120 capability);
121 }
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100122
Lawrence Tange407b4c2022-07-21 13:54:01 +0100123 //AER information.
124 json_object *aer_capability_ir = json_object_new_object();
John Chungf8fc7052024-05-03 20:05:29 +0800125 encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700126
127 encoded = base64_encode((UINT8 *)pcie_error->AerInfo.PcieAer, 96,
128 &encoded_len);
129 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800130 printf("Failed to allocate encode output buffer. \n");
131 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800132 json_object_object_add(aer_capability_ir, "data",
133 json_object_new_string_len(encoded,
134 encoded_len));
135 free(encoded);
John Chungf8fc7052024-05-03 20:05:29 +0800136 }
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700137 json_object_object_add(section_ir, "aerInfo", aer_capability_ir);
138
Lawrence Tange407b4c2022-07-21 13:54:01 +0100139 return section_ir;
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100140}
141
142//Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143void ir_section_pcie_to_cper(json_object *section, FILE *out)
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100144{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100145 EFI_PCIE_ERROR_DATA *section_cper =
146 (EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA));
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100147
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 //Validation bits.
149 section_cper->ValidFields = ir_to_bitfield(
150 json_object_object_get(section, "validationBits"), 8,
151 PCIE_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100152
Lawrence Tange407b4c2022-07-21 13:54:01 +0100153 //Version.
154 json_object *version = json_object_object_get(section, "version");
155 UINT32 minor = int_to_bcd(
156 json_object_get_int(json_object_object_get(version, "minor")));
157 UINT32 major = int_to_bcd(
158 json_object_get_int(json_object_object_get(version, "major")));
159 section_cper->Version = minor + (major << 8);
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100160
Lawrence Tange407b4c2022-07-21 13:54:01 +0100161 //Command/status registers.
162 json_object *command_status =
163 json_object_object_get(section, "commandStatus");
164 UINT32 command = (UINT16)json_object_get_uint64(
165 json_object_object_get(command_status, "commandRegister"));
166 UINT32 status = (UINT16)json_object_get_uint64(
167 json_object_object_get(command_status, "statusRegister"));
168 section_cper->CommandStatus = command + (status << 16);
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100169
Lawrence Tange407b4c2022-07-21 13:54:01 +0100170 //Device ID.
171 json_object *device_id = json_object_object_get(section, "deviceID");
172 UINT64 class_id = json_object_get_uint64(
173 json_object_object_get(device_id, "classCode"));
174 section_cper->DevBridge.VendorId = (UINT16)json_object_get_uint64(
175 json_object_object_get(device_id, "vendorID"));
176 section_cper->DevBridge.DeviceId = (UINT16)json_object_get_uint64(
177 json_object_object_get(device_id, "deviceID"));
178 section_cper->DevBridge.ClassCode[0] = class_id >> 16;
179 section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF;
180 section_cper->DevBridge.ClassCode[2] = class_id & 0xFF;
181 section_cper->DevBridge.Function = (UINT8)json_object_get_uint64(
182 json_object_object_get(device_id, "functionNumber"));
183 section_cper->DevBridge.Device = (UINT8)json_object_get_uint64(
184 json_object_object_get(device_id, "deviceNumber"));
185 section_cper->DevBridge.Segment = (UINT16)json_object_get_uint64(
186 json_object_object_get(device_id, "segmentNumber"));
187 section_cper->DevBridge.PrimaryOrDeviceBus =
188 (UINT8)json_object_get_uint64(json_object_object_get(
189 device_id, "primaryOrDeviceBusNumber"));
190 section_cper->DevBridge.SecondaryBus = (UINT8)json_object_get_uint64(
191 json_object_object_get(device_id, "secondaryBusNumber"));
192 section_cper->DevBridge.Slot.Number = (UINT16)json_object_get_uint64(
193 json_object_object_get(device_id, "slotNumber"));
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100194
Lawrence Tange407b4c2022-07-21 13:54:01 +0100195 //Bridge/control status.
196 json_object *bridge_control =
197 json_object_object_get(section, "bridgeControlStatus");
198 UINT32 bridge_status = (UINT16)json_object_get_uint64(
199 json_object_object_get(bridge_control,
200 "secondaryStatusRegister"));
201 UINT32 control_status = (UINT16)json_object_get_uint64(
202 json_object_object_get(bridge_control, "controlRegister"));
203 section_cper->BridgeControlStatus =
204 bridge_status + (control_status << 16);
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100205
Lawrence Tange407b4c2022-07-21 13:54:01 +0100206 //Capability structure.
207 json_object *capability =
208 json_object_object_get(section, "capabilityStructure");
209 json_object *encoded = json_object_object_get(capability, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700210
211 int32_t decoded_len = 0;
212
213 UINT8 *decoded = base64_decode(json_object_get_string(encoded),
214 json_object_get_string_len(encoded),
215 &decoded_len);
216 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800217 printf("Failed to allocate decode output buffer. \n");
218 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800219 memcpy(section_cper->Capability.PcieCap, decoded, decoded_len);
220 free(decoded);
221 }
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100222
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 //AER capability structure.
224 json_object *aer_info = json_object_object_get(section, "aerInfo");
225 encoded = json_object_object_get(aer_info, "data");
John Chungf8fc7052024-05-03 20:05:29 +0800226 decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700227
228 decoded = base64_decode(json_object_get_string(encoded),
229 json_object_get_string_len(encoded),
230 &decoded_len);
231
232 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800233 printf("Failed to allocate decode output buffer. \n");
234 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800235 memcpy(section_cper->AerInfo.PcieAer, decoded, decoded_len);
236 free(decoded);
237 }
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100238
Lawrence Tange407b4c2022-07-21 13:54:01 +0100239 //Miscellaneous value fields.
240 section_cper->PortType = (UINT32)readable_pair_to_integer(
241 json_object_object_get(section, "portType"));
242 section_cper->SerialNo = json_object_get_uint64(
243 json_object_object_get(section, "deviceSerialNumber"));
Lawrence Tang3b7f45b2022-07-14 14:14:30 +0100244
Lawrence Tange407b4c2022-07-21 13:54:01 +0100245 //Write out to stream, free resources.
246 fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out);
247 fflush(out);
248 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800249}