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 "b64.h"
11 #include "../edk/Cper.h"
12 #include "../cper-utils.h"
13 #include "cper-section-pcie.h"
14 
15 //Converts a single PCIe CPER section into JSON IR.
16 json_object *cper_section_pcie_to_ir(void *section,
17 				     EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
18 {
19 	EFI_PCIE_ERROR_DATA *pcie_error = (EFI_PCIE_ERROR_DATA *)section;
20 	json_object *section_ir = json_object_new_object();
21 
22 	//Validation bits.
23 	json_object *validation = bitfield_to_ir(
24 		pcie_error->ValidFields, 8, PCIE_ERROR_VALID_BITFIELD_NAMES);
25 	json_object_object_add(section_ir, "validationBits", validation);
26 
27 	//Port type.
28 	json_object *port_type = integer_to_readable_pair(
29 		pcie_error->PortType, 9, PCIE_ERROR_PORT_TYPES_KEYS,
30 		PCIE_ERROR_PORT_TYPES_VALUES, "Unknown");
31 	json_object_object_add(section_ir, "portType", port_type);
32 
33 	//Version, provided each half in BCD.
34 	json_object *version = json_object_new_object();
35 	json_object_object_add(
36 		version, "minor",
37 		json_object_new_int(bcd_to_int(pcie_error->Version & 0xFF)));
38 	json_object_object_add(
39 		version, "major",
40 		json_object_new_int(bcd_to_int(pcie_error->Version >> 8)));
41 	json_object_object_add(section_ir, "version", version);
42 
43 	//Command & status.
44 	json_object *command_status = json_object_new_object();
45 	json_object_object_add(
46 		command_status, "commandRegister",
47 		json_object_new_uint64(pcie_error->CommandStatus & 0xFFFF));
48 	json_object_object_add(
49 		command_status, "statusRegister",
50 		json_object_new_uint64(pcie_error->CommandStatus >> 16));
51 	json_object_object_add(section_ir, "commandStatus", command_status);
52 
53 	//PCIe Device ID.
54 	json_object *device_id = json_object_new_object();
55 	UINT64 class_id = (pcie_error->DevBridge.ClassCode[0] << 16) +
56 			  (pcie_error->DevBridge.ClassCode[1] << 8) +
57 			  pcie_error->DevBridge.ClassCode[2];
58 	json_object_object_add(
59 		device_id, "vendorID",
60 		json_object_new_uint64(pcie_error->DevBridge.VendorId));
61 	json_object_object_add(
62 		device_id, "deviceID",
63 		json_object_new_uint64(pcie_error->DevBridge.DeviceId));
64 	json_object_object_add(device_id, "classCode",
65 			       json_object_new_uint64(class_id));
66 	json_object_object_add(
67 		device_id, "functionNumber",
68 		json_object_new_uint64(pcie_error->DevBridge.Function));
69 	json_object_object_add(
70 		device_id, "deviceNumber",
71 		json_object_new_uint64(pcie_error->DevBridge.Device));
72 	json_object_object_add(
73 		device_id, "segmentNumber",
74 		json_object_new_uint64(pcie_error->DevBridge.Segment));
75 	json_object_object_add(
76 		device_id, "primaryOrDeviceBusNumber",
77 		json_object_new_uint64(
78 			pcie_error->DevBridge.PrimaryOrDeviceBus));
79 	json_object_object_add(
80 		device_id, "secondaryBusNumber",
81 		json_object_new_uint64(pcie_error->DevBridge.SecondaryBus));
82 	json_object_object_add(
83 		device_id, "slotNumber",
84 		json_object_new_uint64(pcie_error->DevBridge.Slot.Number));
85 	json_object_object_add(section_ir, "deviceID", device_id);
86 
87 	//Device serial number.
88 	json_object_object_add(section_ir, "deviceSerialNumber",
89 			       json_object_new_uint64(pcie_error->SerialNo));
90 
91 	//Bridge control status.
92 	json_object *bridge_control_status = json_object_new_object();
93 	json_object_object_add(
94 		bridge_control_status, "secondaryStatusRegister",
95 		json_object_new_uint64(pcie_error->BridgeControlStatus &
96 				       0xFFFF));
97 	json_object_object_add(
98 		bridge_control_status, "controlRegister",
99 		json_object_new_uint64(pcie_error->BridgeControlStatus >> 16));
100 	json_object_object_add(section_ir, "bridgeControlStatus",
101 			       bridge_control_status);
102 
103 	//Capability structure.
104 	//The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
105 	//(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
106 	//to be a way to differentiate these, so this is left as a b64 dump.
107 	char *encoded =
108 		b64_encode((unsigned char *)pcie_error->Capability.PcieCap, 60);
109 	json_object *capability = json_object_new_object();
110 	json_object_object_add(capability, "data",
111 			       json_object_new_string(encoded));
112 	free(encoded);
113 	json_object_object_add(section_ir, "capabilityStructure", capability);
114 
115 	//AER information.
116 	json_object *aer_capability_ir = json_object_new_object();
117 	encoded = b64_encode((unsigned char *)pcie_error->AerInfo.PcieAer, 96);
118 	json_object_object_add(aer_capability_ir, "data",
119 			       json_object_new_string(encoded));
120 	free(encoded);
121 	json_object_object_add(section_ir, "aerInfo", aer_capability_ir);
122 	return section_ir;
123 }
124 
125 //Converts a single CPER-JSON PCIe section into CPER binary, outputting to the given stream.
126 void ir_section_pcie_to_cper(json_object *section, FILE *out)
127 {
128 	EFI_PCIE_ERROR_DATA *section_cper =
129 		(EFI_PCIE_ERROR_DATA *)calloc(1, sizeof(EFI_PCIE_ERROR_DATA));
130 
131 	//Validation bits.
132 	section_cper->ValidFields = ir_to_bitfield(
133 		json_object_object_get(section, "validationBits"), 8,
134 		PCIE_ERROR_VALID_BITFIELD_NAMES);
135 
136 	//Version.
137 	json_object *version = json_object_object_get(section, "version");
138 	UINT32 minor = int_to_bcd(
139 		json_object_get_int(json_object_object_get(version, "minor")));
140 	UINT32 major = int_to_bcd(
141 		json_object_get_int(json_object_object_get(version, "major")));
142 	section_cper->Version = minor + (major << 8);
143 
144 	//Command/status registers.
145 	json_object *command_status =
146 		json_object_object_get(section, "commandStatus");
147 	UINT32 command = (UINT16)json_object_get_uint64(
148 		json_object_object_get(command_status, "commandRegister"));
149 	UINT32 status = (UINT16)json_object_get_uint64(
150 		json_object_object_get(command_status, "statusRegister"));
151 	section_cper->CommandStatus = command + (status << 16);
152 
153 	//Device ID.
154 	json_object *device_id = json_object_object_get(section, "deviceID");
155 	UINT64 class_id = json_object_get_uint64(
156 		json_object_object_get(device_id, "classCode"));
157 	section_cper->DevBridge.VendorId = (UINT16)json_object_get_uint64(
158 		json_object_object_get(device_id, "vendorID"));
159 	section_cper->DevBridge.DeviceId = (UINT16)json_object_get_uint64(
160 		json_object_object_get(device_id, "deviceID"));
161 	section_cper->DevBridge.ClassCode[0] = class_id >> 16;
162 	section_cper->DevBridge.ClassCode[1] = (class_id >> 8) & 0xFF;
163 	section_cper->DevBridge.ClassCode[2] = class_id & 0xFF;
164 	section_cper->DevBridge.Function = (UINT8)json_object_get_uint64(
165 		json_object_object_get(device_id, "functionNumber"));
166 	section_cper->DevBridge.Device = (UINT8)json_object_get_uint64(
167 		json_object_object_get(device_id, "deviceNumber"));
168 	section_cper->DevBridge.Segment = (UINT16)json_object_get_uint64(
169 		json_object_object_get(device_id, "segmentNumber"));
170 	section_cper->DevBridge.PrimaryOrDeviceBus =
171 		(UINT8)json_object_get_uint64(json_object_object_get(
172 			device_id, "primaryOrDeviceBusNumber"));
173 	section_cper->DevBridge.SecondaryBus = (UINT8)json_object_get_uint64(
174 		json_object_object_get(device_id, "secondaryBusNumber"));
175 	section_cper->DevBridge.Slot.Number = (UINT16)json_object_get_uint64(
176 		json_object_object_get(device_id, "slotNumber"));
177 
178 	//Bridge/control status.
179 	json_object *bridge_control =
180 		json_object_object_get(section, "bridgeControlStatus");
181 	UINT32 bridge_status = (UINT16)json_object_get_uint64(
182 		json_object_object_get(bridge_control,
183 				       "secondaryStatusRegister"));
184 	UINT32 control_status = (UINT16)json_object_get_uint64(
185 		json_object_object_get(bridge_control, "controlRegister"));
186 	section_cper->BridgeControlStatus =
187 		bridge_status + (control_status << 16);
188 
189 	//Capability structure.
190 	json_object *capability =
191 		json_object_object_get(section, "capabilityStructure");
192 	json_object *encoded = json_object_object_get(capability, "data");
193 	UINT8 *decoded = b64_decode(json_object_get_string(encoded),
194 				    json_object_get_string_len(encoded));
195 	memcpy(section_cper->Capability.PcieCap, decoded, 60);
196 	free(decoded);
197 
198 	//AER capability structure.
199 	json_object *aer_info = json_object_object_get(section, "aerInfo");
200 	encoded = json_object_object_get(aer_info, "data");
201 	decoded = b64_decode(json_object_get_string(encoded),
202 			     json_object_get_string_len(encoded));
203 	memcpy(section_cper->AerInfo.PcieAer, decoded, 96);
204 	free(decoded);
205 
206 	//Miscellaneous value fields.
207 	section_cper->PortType = (UINT32)readable_pair_to_integer(
208 		json_object_object_get(section, "portType"));
209 	section_cper->SerialNo = json_object_get_uint64(
210 		json_object_object_get(section, "deviceSerialNumber"));
211 
212 	//Write out to stream, free resources.
213 	fwrite(section_cper, sizeof(EFI_PCIE_ERROR_DATA), 1, out);
214 	fflush(out);
215 	free(section_cper);
216 }