1 /**
2 * Describes functions for converting PCIe 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 <string.h>
9 #include <json.h>
10 #include <libcper/base64.h>
11 #include <libcper/Cper.h>
12 #include <libcper/cper-utils.h>
13 #include <libcper/sections/cper-section-pcie.h>
14
15 //Converts a single PCIe CPER section into JSON IR.
cper_section_pcie_to_ir(void * section)16 json_object *cper_section_pcie_to_ir(void *section)
17 {
18 EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section;
19 json_object *section_ir = json_object_new_object();
20
21 //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);
25
26 //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);
31
32 //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);
41
42 //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);
51
52 //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);
85
86 //Device serial number.
87 json_object_object_add(section_ir, "deviceSerialNumber",
88 json_object_new_uint64(pcie_error->SerialNo));
89
90 //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);
101
102 //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.
106 int32_t encoded_len = 0;
107
108 char *encoded = base64_encode((UINT8 *)pcie_error->Capability.PcieCap,
109 60, &encoded_len);
110 if (encoded == NULL) {
111 printf("Failed to allocate encode output buffer. \n");
112 } else {
113 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);
118
119 json_object_object_add(section_ir, "capabilityStructure",
120 capability);
121 }
122
123 //AER information.
124 json_object *aer_capability_ir = json_object_new_object();
125 encoded_len = 0;
126
127 encoded = base64_encode((UINT8 *)pcie_error->AerInfo.PcieAer, 96,
128 &encoded_len);
129 if (encoded == NULL) {
130 printf("Failed to allocate encode output buffer. \n");
131 } else {
132 json_object_object_add(aer_capability_ir, "data",
133 json_object_new_string_len(encoded,
134 encoded_len));
135 free(encoded);
136 }
137 json_object_object_add(section_ir, "aerInfo", aer_capability_ir);
138
139 return section_ir;
140 }
141
142 //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream.
ir_section_pcie_to_cper(json_object * section,FILE * out)143 void ir_section_pcie_to_cper(json_object *section, FILE *out)
144 {
145 EFI_PCIE_ERROR_DATA *section_cper =
146 (EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA));
147
148 //Validation bits.
149 section_cper->ValidFields = ir_to_bitfield(
150 json_object_object_get(section, "validationBits"), 8,
151 PCIE_ERROR_VALID_BITFIELD_NAMES);
152
153 //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);
160
161 //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);
169
170 //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"));
194
195 //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);
205
206 //Capability structure.
207 json_object *capability =
208 json_object_object_get(section, "capabilityStructure");
209 json_object *encoded = json_object_object_get(capability, "data");
210
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) {
217 printf("Failed to allocate decode output buffer. \n");
218 } else {
219 memcpy(section_cper->Capability.PcieCap, decoded, decoded_len);
220 free(decoded);
221 }
222
223 //AER capability structure.
224 json_object *aer_info = json_object_object_get(section, "aerInfo");
225 encoded = json_object_object_get(aer_info, "data");
226 decoded_len = 0;
227
228 decoded = base64_decode(json_object_get_string(encoded),
229 json_object_get_string_len(encoded),
230 &decoded_len);
231
232 if (decoded == NULL) {
233 printf("Failed to allocate decode output buffer. \n");
234 } else {
235 memcpy(section_cper->AerInfo.PcieAer, decoded, decoded_len);
236 free(decoded);
237 }
238
239 //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"));
244
245 //Write out to stream, free resources.
246 fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out);
247 fflush(out);
248 free(section_cper);
249 }
250