blob: 39255ebe9aea7c69435f2222e2ec92a8be07b1c9 [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>
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070014#include <string.h>
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010015
16//Converts a single CXL component error CPER section into JSON IR.
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070017json_object *cper_section_cxl_component_to_ir(const UINT8 *section, UINT32 size,
18 char **desc_string)
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010019{
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070020 int outstr_len = 0;
21 *desc_string = malloc(SECTION_DESC_STRING_SIZE);
22 outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE,
23 "A CXL Component Error occurred");
24 if (outstr_len < 0) {
25 cper_print_log(
26 "Error: Could not write to CXL Component description string\n");
27 } else if (outstr_len > SECTION_DESC_STRING_SIZE) {
28 cper_print_log(
29 "Error: CXL Component description string truncated\n");
30 }
31
Ed Tanous12dbd4f2025-03-08 19:05:01 -080032 if (size < sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)) {
33 return NULL;
34 }
35
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 EFI_CXL_COMPONENT_EVENT_HEADER *cxl_error =
37 (EFI_CXL_COMPONENT_EVENT_HEADER *)section;
Ed Tanous12dbd4f2025-03-08 19:05:01 -080038 if (cxl_error->Length < sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)) {
39 return NULL;
40 }
41 if (size < cxl_error->Length) {
42 return NULL;
43 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 json_object *section_ir = json_object_new_object();
Lawrence Tang27217392022-07-07 11:55:39 +010045
Lawrence Tange407b4c2022-07-21 13:54:01 +010046 //Length (bytes) for the entire structure.
47 json_object_object_add(section_ir, "length",
48 json_object_new_uint64(cxl_error->Length));
Lawrence Tang27217392022-07-07 11:55:39 +010049
Lawrence Tange407b4c2022-07-21 13:54:01 +010050 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080051 ValidationTypes ui64Type = { UINT_64T,
52 .value.ui64 = cxl_error->ValidBits };
Lawrence Tang27217392022-07-07 11:55:39 +010053
Lawrence Tange407b4c2022-07-21 13:54:01 +010054 //Device ID.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080055 if (isvalid_prop_to_ir(&ui64Type, 0)) {
56 json_object *device_id = json_object_new_object();
57 json_object_object_add(
58 device_id, "vendorID",
59 json_object_new_int(cxl_error->DeviceId.VendorId));
60 json_object_object_add(
61 device_id, "deviceID",
62 json_object_new_int(cxl_error->DeviceId.DeviceId));
63 json_object_object_add(
64 device_id, "functionNumber",
65 json_object_new_int(
66 cxl_error->DeviceId.FunctionNumber));
67 json_object_object_add(
68 device_id, "deviceNumber",
69 json_object_new_int(cxl_error->DeviceId.DeviceNumber));
70 json_object_object_add(
71 device_id, "busNumber",
72 json_object_new_int(cxl_error->DeviceId.BusNumber));
73 json_object_object_add(
74 device_id, "segmentNumber",
75 json_object_new_int(cxl_error->DeviceId.SegmentNumber));
76 json_object_object_add(
77 device_id, "slotNumber",
78 json_object_new_int(cxl_error->DeviceId.SlotNumber));
79 json_object_object_add(section_ir, "deviceID", device_id);
80 }
Lawrence Tang27217392022-07-07 11:55:39 +010081
Lawrence Tange407b4c2022-07-21 13:54:01 +010082 //Device serial.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080083 if (isvalid_prop_to_ir(&ui64Type, 1)) {
84 json_object_object_add(
85 section_ir, "deviceSerial",
86 json_object_new_uint64(cxl_error->DeviceSerial));
87 }
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010088
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 //The specification for this is defined within the CXL Specification Section 8.2.9.1.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080090 if (isvalid_prop_to_ir(&ui64Type, 2)) {
Ed Tanous12dbd4f2025-03-08 19:05:01 -080091 const UINT8 *cur_pos = (const UINT8 *)(cxl_error + 1);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080092 int remaining_len = cxl_error->Length -
93 sizeof(EFI_CXL_COMPONENT_EVENT_HEADER);
94 if (remaining_len > 0) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080095 int32_t encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070096
Ed Tanous12dbd4f2025-03-08 19:05:01 -080097 char *encoded = base64_encode(cur_pos, remaining_len,
98 &encoded_len);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080099 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700100 cper_print_log(
101 "Failed to allocate encode output buffer. \n");
Ed Tanous12dbd4f2025-03-08 19:05:01 -0800102 json_object_put(section_ir);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800103 return NULL;
104 }
Ed Tanous12dbd4f2025-03-08 19:05:01 -0800105 json_object *event_log = json_object_new_object();
106
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800107 json_object_object_add(event_log, "data",
108 json_object_new_string_len(
109 encoded, encoded_len));
110
111 free(encoded);
112 json_object_object_add(
113 section_ir, "cxlComponentEventLog", event_log);
John Chungf8fc7052024-05-03 20:05:29 +0800114 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 }
116
117 return section_ir;
Lawrence Tangaec83902022-07-18 09:41:08 +0100118}
119
120//Converts a single given CXL Component CPER-JSON section into CPER binary, outputting to the
121//given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122void ir_section_cxl_component_to_cper(json_object *section, FILE *out)
Lawrence Tangaec83902022-07-18 09:41:08 +0100123{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100124 EFI_CXL_COMPONENT_EVENT_HEADER *section_cper =
125 (EFI_CXL_COMPONENT_EVENT_HEADER *)calloc(
126 1, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER));
Lawrence Tangaec83902022-07-18 09:41:08 +0100127
Lawrence Tange407b4c2022-07-21 13:54:01 +0100128 //Length of the structure.
129 section_cper->Length = json_object_get_uint64(
130 json_object_object_get(section, "length"));
Lawrence Tangaec83902022-07-18 09:41:08 +0100131
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 //Validation bits.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800133 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
134 struct json_object *obj = NULL;
Lawrence Tangaec83902022-07-18 09:41:08 +0100135
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 //Device ID information.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800137 if (json_object_object_get_ex(section, "deviceID", &obj)) {
138 json_object *device_id = obj;
139 section_cper->DeviceId.VendorId = json_object_get_uint64(
140 json_object_object_get(device_id, "vendorID"));
141 section_cper->DeviceId.DeviceId = json_object_get_uint64(
142 json_object_object_get(device_id, "deviceID"));
143 section_cper->DeviceId.FunctionNumber = json_object_get_uint64(
144 json_object_object_get(device_id, "functionNumber"));
145 section_cper->DeviceId.DeviceNumber = json_object_get_uint64(
146 json_object_object_get(device_id, "deviceNumber"));
147 section_cper->DeviceId.BusNumber = json_object_get_uint64(
148 json_object_object_get(device_id, "busNumber"));
149 section_cper->DeviceId.SegmentNumber = json_object_get_uint64(
150 json_object_object_get(device_id, "segmentNumber"));
151 section_cper->DeviceId.SlotNumber = json_object_get_uint64(
152 json_object_object_get(device_id, "slotNumber"));
153 add_to_valid_bitfield(&ui64Type, 0);
154 }
Lawrence Tangaec83902022-07-18 09:41:08 +0100155
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 //Device serial number.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800157 if (json_object_object_get_ex(section, "deviceSerial", &obj)) {
158 section_cper->DeviceSerial = json_object_get_uint64(obj);
159 add_to_valid_bitfield(&ui64Type, 1);
160 }
161
162 //CXL component event log, decoded from base64.
163 json_object *event_log = NULL;
164 if (json_object_object_get_ex(section, "cxlComponentEventLog", &obj)) {
165 event_log = obj;
166 add_to_valid_bitfield(&ui64Type, 2);
167 }
168 section_cper->ValidBits = ui64Type.value.ui64;
Lawrence Tangaec83902022-07-18 09:41:08 +0100169
Lawrence Tange407b4c2022-07-21 13:54:01 +0100170 //Write header out to stream.
171 fwrite(section_cper, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER), 1, out);
172 fflush(out);
Lawrence Tangaec83902022-07-18 09:41:08 +0100173
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800174 if (event_log != NULL) {
175 json_object *encoded =
176 json_object_object_get(event_log, "data");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700177
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800178 int32_t decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700179
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800180 UINT8 *decoded = base64_decode(
181 json_object_get_string(encoded),
182 json_object_get_string_len(encoded), &decoded_len);
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700183
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800184 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700185 cper_print_log(
186 "Failed to allocate decode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800187 } else {
188 fwrite(decoded, decoded_len, 1, out);
189 fflush(out);
190 free(decoded);
191 }
John Chungf8fc7052024-05-03 20:05:29 +0800192 }
Lawrence Tangaec83902022-07-18 09:41:08 +0100193
Lawrence Tange407b4c2022-07-21 13:54:01 +0100194 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800195}