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 = cxl_error->Length - sizeof(cxl_error);
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 }
55 
56 //Converts a single given CXL Component CPER-JSON section into CPER binary, outputting to the
57 //given stream.
58 void ir_section_cxl_component_to_cper(json_object* section, FILE* out)
59 {
60     EFI_CXL_COMPONENT_EVENT_HEADER* section_cper =
61         (EFI_CXL_COMPONENT_EVENT_HEADER*)calloc(1, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER));
62 
63     //Length of the structure.
64     section_cper->Length = json_object_get_uint64(json_object_object_get(section, "length"));
65 
66     //Validation bits.
67     section_cper->ValidBits = ir_to_bitfield(json_object_object_get(section, "validationBits"),
68         3, CXL_COMPONENT_ERROR_VALID_BITFIELD_NAMES);
69 
70     //Device ID information.
71     json_object* device_id = json_object_object_get(section, "deviceID");
72     section_cper->DeviceId.VendorId = json_object_get_uint64(json_object_object_get(device_id, "vendorID"));
73     section_cper->DeviceId.DeviceId = json_object_get_uint64(json_object_object_get(device_id, "deviceID"));
74     section_cper->DeviceId.FunctionNumber =
75         json_object_get_uint64(json_object_object_get(device_id, "functionNumber"));
76     section_cper->DeviceId.DeviceNumber =
77         json_object_get_uint64(json_object_object_get(device_id, "deviceNumber"));
78     section_cper->DeviceId.BusNumber =
79         json_object_get_uint64(json_object_object_get(device_id, "busNumber"));
80     section_cper->DeviceId.SegmentNumber =
81         json_object_get_uint64(json_object_object_get(device_id, "segmentNumber"));
82     section_cper->DeviceId.SlotNumber =
83         json_object_get_uint64(json_object_object_get(device_id, "slotNumber"));
84 
85     //Device serial number.
86     section_cper->DeviceSerial = json_object_get_uint64(json_object_object_get(section, "deviceSerial"));
87 
88     //Write header out to stream.
89     fwrite(section_cper, sizeof(section_cper), 1, out);
90     fflush(out);
91 
92     //CXL component event log, decoded from base64.
93     json_object* encoded = json_object_object_get(section, "cxlComponentEventLog");
94     int log_length = section_cper->Length - sizeof(section_cper);
95     char* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
96     fwrite(decoded, log_length, 1, out);
97     fflush(out);
98     free(decoded);
99 
100     free(section_cper);
101 }