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