1 /**
2  * Describes functions for converting CXL component error 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 "json.h"
9 #include "b64.h"
10 #include "../edk/Cper.h"
11 #include "../cper-utils.h"
12 #include "cper-section-cxl-component.h"
13 
14 //Converts a single CXL component error CPER section into JSON IR.
15 json_object* cper_section_cxl_component_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
16 {
17     EFI_CXL_COMPONENT_EVENT_HEADER* cxl_error = (EFI_CXL_COMPONENT_EVENT_HEADER*)section;
18     json_object* section_ir = json_object_new_object();
19 
20     //Length (bytes) for the entire structure.
21     json_object_object_add(section_ir, "length", json_object_new_uint64(cxl_error->Length));
22 
23     //Validation bits.
24     json_object* validation = bitfield_to_ir(cxl_error->ValidBits, 3, CXL_COMPONENT_ERROR_VALID_BITFIELD_NAMES);
25     json_object_object_add(section_ir, "validationBits", validation);
26 
27     //Device ID.
28     json_object* device_id = json_object_new_object();
29     json_object_object_add(device_id, "vendorID", json_object_new_int(cxl_error->DeviceId.VendorId));
30     json_object_object_add(device_id, "deviceID", json_object_new_int(cxl_error->DeviceId.DeviceId));
31     json_object_object_add(device_id, "functionNumber", json_object_new_int(cxl_error->DeviceId.FunctionNumber));
32     json_object_object_add(device_id, "deviceNumber", json_object_new_int(cxl_error->DeviceId.DeviceNumber));
33     json_object_object_add(device_id, "busNumber", json_object_new_int(cxl_error->DeviceId.BusNumber));
34     json_object_object_add(device_id, "segmentNumber", json_object_new_int(cxl_error->DeviceId.SegmentNumber));
35     json_object_object_add(device_id, "slotNumber", json_object_new_int(cxl_error->DeviceId.SlotNumber));
36     json_object_object_add(section_ir, "deviceID", device_id);
37 
38     //Device serial.
39     json_object_object_add(section_ir, "deviceSerial", json_object_new_uint64(cxl_error->DeviceSerial));
40 
41     //The specification for this is defined within the CXL Specification Section 8.2.9.1.
42     unsigned char* cur_pos = (unsigned char*)(cxl_error + 1);
43     int remaining_len = section - (void*)cur_pos + cxl_error->Length;
44     if (remaining_len > 0)
45     {
46         json_object* event_log = json_object_new_object();
47         char* encoded = b64_encode(cur_pos, remaining_len);
48         json_object_object_add(event_log, "data", json_object_new_string(encoded));
49         free(encoded);
50         json_object_object_add(section_ir, "cxlComponentEventLog", event_log);
51     }
52 
53     return section_ir;
54 }