blob: c4e8169599cd40309ecc11e5ce8ef4282e2e9133 [file] [log] [blame]
Lawrence Tangd7e8ca32022-07-07 10:25:53 +01001/**
2 * Describes functions for converting CXL component error CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tangd7e8ca32022-07-07 10:25:53 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01008#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +00009#include <libcper/base64.h>
10#include <libcper/Cper.h>
11#include <libcper/cper-utils.h>
12#include <libcper/sections/cper-section-cxl-component.h>
Ed Tanous50b966f2025-03-11 09:06:19 -070013#include <libcper/log.h>
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010014
15//Converts a single CXL component error CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080016json_object *cper_section_cxl_component_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010017{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080018 if (size < sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)) {
19 return NULL;
20 }
21
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 EFI_CXL_COMPONENT_EVENT_HEADER *cxl_error =
23 (EFI_CXL_COMPONENT_EVENT_HEADER *)section;
Ed Tanous12dbd4f2025-03-08 19:05:01 -080024 if (cxl_error->Length < sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)) {
25 return NULL;
26 }
27 if (size < cxl_error->Length) {
28 return NULL;
29 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 json_object *section_ir = json_object_new_object();
Lawrence Tang27217392022-07-07 11:55:39 +010031
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //Length (bytes) for the entire structure.
33 json_object_object_add(section_ir, "length",
34 json_object_new_uint64(cxl_error->Length));
Lawrence Tang27217392022-07-07 11:55:39 +010035
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080037 ValidationTypes ui64Type = { UINT_64T,
38 .value.ui64 = cxl_error->ValidBits };
Lawrence Tang27217392022-07-07 11:55:39 +010039
Lawrence Tange407b4c2022-07-21 13:54:01 +010040 //Device ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080041 if (isvalid_prop_to_ir(&ui64Type, 0)) {
42 json_object *device_id = json_object_new_object();
43 json_object_object_add(
44 device_id, "vendorID",
45 json_object_new_int(cxl_error->DeviceId.VendorId));
46 json_object_object_add(
47 device_id, "deviceID",
48 json_object_new_int(cxl_error->DeviceId.DeviceId));
49 json_object_object_add(
50 device_id, "functionNumber",
51 json_object_new_int(
52 cxl_error->DeviceId.FunctionNumber));
53 json_object_object_add(
54 device_id, "deviceNumber",
55 json_object_new_int(cxl_error->DeviceId.DeviceNumber));
56 json_object_object_add(
57 device_id, "busNumber",
58 json_object_new_int(cxl_error->DeviceId.BusNumber));
59 json_object_object_add(
60 device_id, "segmentNumber",
61 json_object_new_int(cxl_error->DeviceId.SegmentNumber));
62 json_object_object_add(
63 device_id, "slotNumber",
64 json_object_new_int(cxl_error->DeviceId.SlotNumber));
65 json_object_object_add(section_ir, "deviceID", device_id);
66 }
Lawrence Tang27217392022-07-07 11:55:39 +010067
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 //Device serial.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080069 if (isvalid_prop_to_ir(&ui64Type, 1)) {
70 json_object_object_add(
71 section_ir, "deviceSerial",
72 json_object_new_uint64(cxl_error->DeviceSerial));
73 }
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010074
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 //The specification for this is defined within the CXL Specification Section 8.2.9.1.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080076 if (isvalid_prop_to_ir(&ui64Type, 2)) {
Ed Tanous12dbd4f2025-03-08 19:05:01 -080077 const UINT8 *cur_pos = (const UINT8 *)(cxl_error + 1);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080078 int remaining_len = cxl_error->Length -
79 sizeof(EFI_CXL_COMPONENT_EVENT_HEADER);
80 if (remaining_len > 0) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080081 int32_t encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070082
Ed Tanous12dbd4f2025-03-08 19:05:01 -080083 char *encoded = base64_encode(cur_pos, remaining_len,
84 &encoded_len);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080085 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070086 cper_print_log(
87 "Failed to allocate encode output buffer. \n");
Ed Tanous12dbd4f2025-03-08 19:05:01 -080088 json_object_put(section_ir);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080089 return NULL;
90 }
Ed Tanous12dbd4f2025-03-08 19:05:01 -080091 json_object *event_log = json_object_new_object();
92
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080093 json_object_object_add(event_log, "data",
94 json_object_new_string_len(
95 encoded, encoded_len));
96
97 free(encoded);
98 json_object_object_add(
99 section_ir, "cxlComponentEventLog", event_log);
John Chungf8fc7052024-05-03 20:05:29 +0800100 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100101 }
102
103 return section_ir;
Lawrence Tangaec83902022-07-18 09:41:08 +0100104}
105
106//Converts a single given CXL Component CPER-JSON section into CPER binary, outputting to the
107//given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100108void ir_section_cxl_component_to_cper(json_object *section, FILE *out)
Lawrence Tangaec83902022-07-18 09:41:08 +0100109{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100110 EFI_CXL_COMPONENT_EVENT_HEADER *section_cper =
111 (EFI_CXL_COMPONENT_EVENT_HEADER *)calloc(
112 1, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER));
Lawrence Tangaec83902022-07-18 09:41:08 +0100113
Lawrence Tange407b4c2022-07-21 13:54:01 +0100114 //Length of the structure.
115 section_cper->Length = json_object_get_uint64(
116 json_object_object_get(section, "length"));
Lawrence Tangaec83902022-07-18 09:41:08 +0100117
Lawrence Tange407b4c2022-07-21 13:54:01 +0100118 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800119 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
120 struct json_object *obj = NULL;
Lawrence Tangaec83902022-07-18 09:41:08 +0100121
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122 //Device ID information.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800123 if (json_object_object_get_ex(section, "deviceID", &obj)) {
124 json_object *device_id = obj;
125 section_cper->DeviceId.VendorId = json_object_get_uint64(
126 json_object_object_get(device_id, "vendorID"));
127 section_cper->DeviceId.DeviceId = json_object_get_uint64(
128 json_object_object_get(device_id, "deviceID"));
129 section_cper->DeviceId.FunctionNumber = json_object_get_uint64(
130 json_object_object_get(device_id, "functionNumber"));
131 section_cper->DeviceId.DeviceNumber = json_object_get_uint64(
132 json_object_object_get(device_id, "deviceNumber"));
133 section_cper->DeviceId.BusNumber = json_object_get_uint64(
134 json_object_object_get(device_id, "busNumber"));
135 section_cper->DeviceId.SegmentNumber = json_object_get_uint64(
136 json_object_object_get(device_id, "segmentNumber"));
137 section_cper->DeviceId.SlotNumber = json_object_get_uint64(
138 json_object_object_get(device_id, "slotNumber"));
139 add_to_valid_bitfield(&ui64Type, 0);
140 }
Lawrence Tangaec83902022-07-18 09:41:08 +0100141
Lawrence Tange407b4c2022-07-21 13:54:01 +0100142 //Device serial number.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800143 if (json_object_object_get_ex(section, "deviceSerial", &obj)) {
144 section_cper->DeviceSerial = json_object_get_uint64(obj);
145 add_to_valid_bitfield(&ui64Type, 1);
146 }
147
148 //CXL component event log, decoded from base64.
149 json_object *event_log = NULL;
150 if (json_object_object_get_ex(section, "cxlComponentEventLog", &obj)) {
151 event_log = obj;
152 add_to_valid_bitfield(&ui64Type, 2);
153 }
154 section_cper->ValidBits = ui64Type.value.ui64;
Lawrence Tangaec83902022-07-18 09:41:08 +0100155
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 //Write header out to stream.
157 fwrite(section_cper, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER), 1, out);
158 fflush(out);
Lawrence Tangaec83902022-07-18 09:41:08 +0100159
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800160 if (event_log != NULL) {
161 json_object *encoded =
162 json_object_object_get(event_log, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700163
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800164 int32_t decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700165
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800166 UINT8 *decoded = base64_decode(
167 json_object_get_string(encoded),
168 json_object_get_string_len(encoded), &decoded_len);
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700169
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800170 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700171 cper_print_log(
172 "Failed to allocate decode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800173 } else {
174 fwrite(decoded, decoded_len, 1, out);
175 fflush(out);
176 free(decoded);
177 }
John Chungf8fc7052024-05-03 20:05:29 +0800178 }
Lawrence Tangaec83902022-07-18 09:41:08 +0100179
Lawrence Tange407b4c2022-07-21 13:54:01 +0100180 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800181}