blob: 27ca4b28140418f814ac65ce4442526b13e7ed27 [file] [log] [blame]
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01001/**
Ed Tanousfedd4572024-07-12 13:56:00 -07002 * Describes high level functions for converting an entire CPER log, and functions for parsing
Lawrence Tang2800cd82022-07-05 16:08:20 +01003 * CPER headers and section descriptions into an intermediate JSON format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7
8#include <stdio.h>
Karthik Rajagopalan5220c9b2024-08-08 00:24:44 -07009#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +010010#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000011#include <libcper/base64.h>
12#include <libcper/Cper.h>
13#include <libcper/cper-parse.h>
14#include <libcper/cper-parse-str.h>
15#include <libcper/cper-utils.h>
16#include <libcper/sections/cper-section.h>
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010017
18//Private pre-definitions.
Lawrence Tange407b4c2022-07-21 13:54:01 +010019json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header);
20json_object *
21cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor);
Lawrence Tang94153492022-09-05 13:07:54 +010022json_object *cper_section_to_ir(FILE *handle, long base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010024
Ed Tanous8d47a372025-03-05 15:55:36 -080025json_object *cper_buf_to_ir(void *cper_buf, size_t size)
26{
27 // TODO, this really should avoid the overhead of fmemopen()
28 // but doing so would require a lot of code changes to evict FILE* from
29 // The internals of libcper
30 FILE *cper_file = fmemopen(cper_buf, size, "r");
31 if (!cper_file) {
32 printf("Failed to open CPER buffer.\n");
33 return NULL;
34 }
35 json_object *ir = cper_to_ir(cper_file);
36 fclose(cper_file);
37 return ir;
38}
39
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010040//Reads a CPER log file at the given file location, and returns an intermediate
41//JSON representation of this CPER record.
Lawrence Tange407b4c2022-07-21 13:54:01 +010042json_object *cper_to_ir(FILE *cper_file)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010043{
Lawrence Tang94153492022-09-05 13:07:54 +010044 //Read the current file pointer location as the base of the record.
45 long base_pos = ftell(cper_file);
46
Lawrence Tange407b4c2022-07-21 13:54:01 +010047 //Ensure this is really a CPER log.
48 EFI_COMMON_ERROR_RECORD_HEADER header;
Lawrence Tange407b4c2022-07-21 13:54:01 +010049 if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1,
50 cper_file) != 1) {
51 printf("Invalid CPER file: Invalid length (log too short).\n");
52 return NULL;
53 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010054
Lawrence Tange407b4c2022-07-21 13:54:01 +010055 //Check if the header contains the magic bytes ("CPER").
56 if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
57 printf("Invalid CPER file: Invalid header (incorrect signature).\n");
58 return NULL;
59 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010060
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 //Create the header JSON object from the read bytes.
62 json_object *header_ir = cper_header_to_ir(&header);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010063
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 //Read the appropriate number of section descriptors & sections, and convert them into IR format.
65 json_object *section_descriptors_ir = json_object_new_array();
66 json_object *sections_ir = json_object_new_array();
67 for (int i = 0; i < header.SectionCount; i++) {
68 //Create the section descriptor.
69 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
70 if (fread(&section_descriptor,
71 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
72 cper_file) != 1) {
73 printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n",
74 header.SectionCount, i + 1);
Karthik Rajagopalanbf6ccc82024-08-05 15:39:51 -070075 // Free json objects
76 json_object_put(sections_ir);
77 json_object_put(section_descriptors_ir);
78 json_object_put(header_ir);
Lawrence Tange407b4c2022-07-21 13:54:01 +010079 return NULL;
80 }
81 json_object_array_add(
82 section_descriptors_ir,
83 cper_section_descriptor_to_ir(&section_descriptor));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010084
Lawrence Tange407b4c2022-07-21 13:54:01 +010085 //Read the section itself.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080086
Lawrence Tange407b4c2022-07-21 13:54:01 +010087 json_object_array_add(sections_ir,
Lawrence Tang94153492022-09-05 13:07:54 +010088 cper_section_to_ir(cper_file, base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 &section_descriptor));
90 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010091
Lawrence Tange407b4c2022-07-21 13:54:01 +010092 //Add the header, section descriptors, and sections to a parent object.
93 json_object *parent = json_object_new_object();
94 json_object_object_add(parent, "header", header_ir);
95 json_object_object_add(parent, "sectionDescriptors",
96 section_descriptors_ir);
97 json_object_object_add(parent, "sections", sections_ir);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010098
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 return parent;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100100}
101
Karthik Rajagopalan5220c9b2024-08-08 00:24:44 -0700102char *cper_to_str_ir(FILE *cper_file)
103{
104 json_object *jobj = cper_to_ir(cper_file);
105 char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
106
107 json_object_put(jobj);
108 return str;
109}
110
111char *cperbuf_to_str_ir(const unsigned char *cper, size_t size)
112{
113 FILE *cper_file = fmemopen((void *)cper, size, "r");
114
115 return cper_file ? cper_to_str_ir(cper_file) : NULL;
116}
117
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100118//Converts a parsed CPER record header into intermediate JSON object format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100119json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100120{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 json_object *header_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100122
Lawrence Tange407b4c2022-07-21 13:54:01 +0100123 //Revision/version information.
124 json_object_object_add(header_ir, "revision",
125 revision_to_ir(header->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100126
Lawrence Tange407b4c2022-07-21 13:54:01 +0100127 //Section count.
128 json_object_object_add(header_ir, "sectionCount",
129 json_object_new_int(header->SectionCount));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100130
Lawrence Tange407b4c2022-07-21 13:54:01 +0100131 //Error severity (with interpreted string version).
132 json_object *error_severity = json_object_new_object();
133 json_object_object_add(error_severity, "code",
134 json_object_new_uint64(header->ErrorSeverity));
135 json_object_object_add(error_severity, "name",
136 json_object_new_string(severity_to_string(
137 header->ErrorSeverity)));
138 json_object_object_add(header_ir, "severity", error_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100139
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 //Total length of the record (including headers) in bytes.
141 json_object_object_add(header_ir, "recordLength",
142 json_object_new_uint64(header->RecordLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100143
Lawrence Tange407b4c2022-07-21 13:54:01 +0100144 //If a timestamp exists according to validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800145 if (header->ValidationBits & 0x2) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100146 char timestamp_string[TIMESTAMP_LENGTH];
Ed Tanous13f099f2024-11-20 11:10:30 -0800147 timestamp_to_string(timestamp_string, TIMESTAMP_LENGTH,
148 &header->TimeStamp);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100149
Lawrence Tange407b4c2022-07-21 13:54:01 +0100150 json_object_object_add(
151 header_ir, "timestamp",
152 json_object_new_string(timestamp_string));
153 json_object_object_add(
154 header_ir, "timestampIsPrecise",
155 json_object_new_boolean(header->TimeStamp.Flag));
156 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100157
Lawrence Tange407b4c2022-07-21 13:54:01 +0100158 //If a platform ID exists according to the validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800159 if (header->ValidationBits & 0x1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160 char platform_string[GUID_STRING_LENGTH];
161 guid_to_string(platform_string, &header->PlatformID);
162 json_object_object_add(header_ir, "platformID",
163 json_object_new_string(platform_string));
164 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100165
Lawrence Tange407b4c2022-07-21 13:54:01 +0100166 //If a partition ID exists according to the validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800167 if (header->ValidationBits & 0x4) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100168 char partition_string[GUID_STRING_LENGTH];
169 guid_to_string(partition_string, &header->PartitionID);
170 json_object_object_add(
171 header_ir, "partitionID",
172 json_object_new_string(partition_string));
173 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100174
Lawrence Tange407b4c2022-07-21 13:54:01 +0100175 //Creator ID of the header.
176 char creator_string[GUID_STRING_LENGTH];
177 guid_to_string(creator_string, &header->CreatorID);
178 json_object_object_add(header_ir, "creatorID",
179 json_object_new_string(creator_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100180
Lawrence Tange407b4c2022-07-21 13:54:01 +0100181 //Notification type for the header. Some defined types are available.
182 json_object *notification_type = json_object_new_object();
183 char notification_type_string[GUID_STRING_LENGTH];
184 guid_to_string(notification_type_string, &header->NotificationType);
185 json_object_object_add(
186 notification_type, "guid",
187 json_object_new_string(notification_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100188
Lawrence Tange407b4c2022-07-21 13:54:01 +0100189 //Add the human readable notification type if possible.
190 char *notification_type_readable = "Unknown";
191 if (guid_equal(&header->NotificationType,
John Chungf8fc7052024-05-03 20:05:29 +0800192 &gEfiEventNotificationTypeCmcGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100193 notification_type_readable = "CMC";
John Chungf8fc7052024-05-03 20:05:29 +0800194 } else if (guid_equal(&header->NotificationType,
195 &gEfiEventNotificationTypeCpeGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 notification_type_readable = "CPE";
John Chungf8fc7052024-05-03 20:05:29 +0800197 } else if (guid_equal(&header->NotificationType,
198 &gEfiEventNotificationTypeMceGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100199 notification_type_readable = "MCE";
John Chungf8fc7052024-05-03 20:05:29 +0800200 } else if (guid_equal(&header->NotificationType,
201 &gEfiEventNotificationTypePcieGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100202 notification_type_readable = "PCIe";
John Chungf8fc7052024-05-03 20:05:29 +0800203 } else if (guid_equal(&header->NotificationType,
204 &gEfiEventNotificationTypeInitGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100205 notification_type_readable = "INIT";
John Chungf8fc7052024-05-03 20:05:29 +0800206 } else if (guid_equal(&header->NotificationType,
207 &gEfiEventNotificationTypeNmiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100208 notification_type_readable = "NMI";
John Chungf8fc7052024-05-03 20:05:29 +0800209 } else if (guid_equal(&header->NotificationType,
210 &gEfiEventNotificationTypeBootGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100211 notification_type_readable = "Boot";
John Chungf8fc7052024-05-03 20:05:29 +0800212 } else if (guid_equal(&header->NotificationType,
213 &gEfiEventNotificationTypeDmarGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100214 notification_type_readable = "DMAr";
John Chungf8fc7052024-05-03 20:05:29 +0800215 } else if (guid_equal(&header->NotificationType,
216 &gEfiEventNotificationTypeSeaGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100217 notification_type_readable = "SEA";
John Chungf8fc7052024-05-03 20:05:29 +0800218 } else if (guid_equal(&header->NotificationType,
219 &gEfiEventNotificationTypeSeiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100220 notification_type_readable = "SEI";
John Chungf8fc7052024-05-03 20:05:29 +0800221 } else if (guid_equal(&header->NotificationType,
222 &gEfiEventNotificationTypePeiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 notification_type_readable = "PEI";
John Chungf8fc7052024-05-03 20:05:29 +0800224 } else if (guid_equal(&header->NotificationType,
225 &gEfiEventNotificationTypeCxlGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100226 notification_type_readable = "CXL Component";
John Chungf8fc7052024-05-03 20:05:29 +0800227 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100228 json_object_object_add(
229 notification_type, "type",
230 json_object_new_string(notification_type_readable));
231 json_object_object_add(header_ir, "notificationType",
232 notification_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100233
Lawrence Tange407b4c2022-07-21 13:54:01 +0100234 //The record ID for this record, unique on a given system.
235 json_object_object_add(header_ir, "recordID",
236 json_object_new_uint64(header->RecordID));
237
238 //Flag for the record, and a human readable form.
239 json_object *flags = integer_to_readable_pair(
240 header->Flags,
241 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
242 CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES,
243 "Unknown");
244 json_object_object_add(header_ir, "flags", flags);
245
246 //Persistence information. Outside the scope of specification, so just a uint32 here.
247 json_object_object_add(header_ir, "persistenceInfo",
248 json_object_new_uint64(header->PersistenceInfo));
249 return header_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100250}
251
252//Converts the given EFI section descriptor into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100253json_object *
254cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100255{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100256 json_object *section_descriptor_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100257
Lawrence Tange407b4c2022-07-21 13:54:01 +0100258 //The offset of the section from the base of the record header, length.
259 json_object_object_add(
260 section_descriptor_ir, "sectionOffset",
261 json_object_new_uint64(section_descriptor->SectionOffset));
262 json_object_object_add(
263 section_descriptor_ir, "sectionLength",
264 json_object_new_uint64(section_descriptor->SectionLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100265
Lawrence Tange407b4c2022-07-21 13:54:01 +0100266 //Revision.
267 json_object_object_add(section_descriptor_ir, "revision",
268 revision_to_ir(section_descriptor->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100269
Lawrence Tange407b4c2022-07-21 13:54:01 +0100270 //Flag bits.
271 json_object *flags =
272 bitfield_to_ir(section_descriptor->SectionFlags, 8,
273 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
274 json_object_object_add(section_descriptor_ir, "flags", flags);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100275
Lawrence Tange407b4c2022-07-21 13:54:01 +0100276 //Section type (GUID).
277 json_object *section_type = json_object_new_object();
278 char section_type_string[GUID_STRING_LENGTH];
279 guid_to_string(section_type_string, &section_descriptor->SectionType);
280 json_object_object_add(section_type, "data",
281 json_object_new_string(section_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100282
Lawrence Tange407b4c2022-07-21 13:54:01 +0100283 //Readable section type, if possible.
Lawrence Tang580423f2022-08-24 09:37:53 +0100284 const char *section_type_readable = "Unknown";
John Chungf8fc7052024-05-03 20:05:29 +0800285 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100286 if (guid_equal(section_definitions[i].Guid,
287 &section_descriptor->SectionType)) {
288 section_type_readable =
289 section_definitions[i].ReadableName;
Lawrence Tang580423f2022-08-24 09:37:53 +0100290 break;
291 }
292 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100293
Lawrence Tange407b4c2022-07-21 13:54:01 +0100294 json_object_object_add(section_type, "type",
295 json_object_new_string(section_type_readable));
296 json_object_object_add(section_descriptor_ir, "sectionType",
297 section_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100298
Lawrence Tange407b4c2022-07-21 13:54:01 +0100299 //If validation bits indicate it exists, add FRU ID.
John Chungf8fc7052024-05-03 20:05:29 +0800300 if (section_descriptor->SecValidMask & 0x1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100301 char fru_id_string[GUID_STRING_LENGTH];
302 guid_to_string(fru_id_string, &section_descriptor->FruId);
303 json_object_object_add(section_descriptor_ir, "fruID",
304 json_object_new_string(fru_id_string));
305 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100306
Lawrence Tange407b4c2022-07-21 13:54:01 +0100307 //If validation bits indicate it exists, add FRU text.
John Chungf8fc7052024-05-03 20:05:29 +0800308 if ((section_descriptor->SecValidMask & 0x2) >> 1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100309 json_object_object_add(
310 section_descriptor_ir, "fruText",
311 json_object_new_string(section_descriptor->FruString));
John Chungf8fc7052024-05-03 20:05:29 +0800312 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100313
Lawrence Tange407b4c2022-07-21 13:54:01 +0100314 //Section severity.
315 json_object *section_severity = json_object_new_object();
316 json_object_object_add(
317 section_severity, "code",
318 json_object_new_uint64(section_descriptor->Severity));
319 json_object_object_add(section_severity, "name",
320 json_object_new_string(severity_to_string(
321 section_descriptor->Severity)));
322 json_object_object_add(section_descriptor_ir, "severity",
323 section_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100324
Lawrence Tange407b4c2022-07-21 13:54:01 +0100325 return section_descriptor_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100326}
327
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100328//Converts the section described by a single given section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100329json_object *cper_section_to_ir(FILE *handle, long base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100330 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100331{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100332 //Save our current position in the stream.
333 long position = ftell(handle);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100334
Lawrence Tange407b4c2022-07-21 13:54:01 +0100335 //Read section as described by the section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100336 fseek(handle, base_pos + descriptor->SectionOffset, SEEK_SET);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100337 void *section = malloc(descriptor->SectionLength);
338 if (fread(section, descriptor->SectionLength, 1, handle) != 1) {
John Chungf8fc7052024-05-03 20:05:29 +0800339 printf("Section read failed: Could not read %u bytes from global offset %d.\n",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100340 descriptor->SectionLength, descriptor->SectionOffset);
341 free(section);
342 return NULL;
343 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100344
Lawrence Tange407b4c2022-07-21 13:54:01 +0100345 //Seek back to our original position.
346 fseek(handle, position, SEEK_SET);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100347
Lawrence Tange407b4c2022-07-21 13:54:01 +0100348 //Parse section to IR based on GUID.
349 json_object *result = NULL;
Ed Tanousb07061a2024-09-22 10:33:29 -0700350
351 json_object *section_ir = NULL;
Lawrence Tang580423f2022-08-24 09:37:53 +0100352 int section_converted = 0;
John Chungf8fc7052024-05-03 20:05:29 +0800353 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100354 if (guid_equal(section_definitions[i].Guid,
355 &descriptor->SectionType) &&
356 section_definitions[i].ToIR != NULL) {
Ed Tanousb07061a2024-09-22 10:33:29 -0700357 section_ir = section_definitions[i].ToIR(section);
358
359 result = json_object_new_object();
360 json_object_object_add(result,
361 section_definitions[i].ShortName,
362 section_ir);
363
Lawrence Tang580423f2022-08-24 09:37:53 +0100364 section_converted = 1;
365 break;
366 }
367 }
368
369 //Was it an unknown GUID/failed read?
370 if (!section_converted) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100371 //Output the data as formatted base64.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700372 int32_t encoded_len = 0;
373 char *encoded = base64_encode(
374 section, descriptor->SectionLength, &encoded_len);
375 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800376 printf("Failed to allocate encode output buffer. \n");
377 } else {
Ed Tanousb07061a2024-09-22 10:33:29 -0700378 section_ir = json_object_new_object();
379 json_object_object_add(section_ir, "data",
John Chungf8fc7052024-05-03 20:05:29 +0800380 json_object_new_string_len(
381 encoded, encoded_len));
382 free(encoded);
Ed Tanousb07061a2024-09-22 10:33:29 -0700383
384 result = json_object_new_object();
385 json_object_object_add(result, "Unknown", section_ir);
John Chungf8fc7052024-05-03 20:05:29 +0800386 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100387 }
Ed Tanousb07061a2024-09-22 10:33:29 -0700388
Lawrence Tange407b4c2022-07-21 13:54:01 +0100389 //Free section memory, return result.
390 free(section);
391 return result;
Lawrence Tang617949e2022-08-08 14:21:42 +0100392}
393
Ed Tanous8d47a372025-03-05 15:55:36 -0800394json_object *cper_buf_single_section_to_ir(void *cper_buf, size_t size)
395{
396 // TODO, this really should avoid the overhead of fmemopen()
397 // but doing so would require a lot of code changes to evict FILE* from
398 // The internals of libcper.
399 FILE *cper_file = fmemopen(cper_buf, size, "r");
400 if (!cper_file) {
401 printf("Failed to open CPER buffer.\n");
402 return NULL;
403 }
404 json_object *ir = cper_to_ir(cper_file);
405 fclose(cper_file);
406 return ir;
407}
408
Lawrence Tang617949e2022-08-08 14:21:42 +0100409//Converts a single CPER section, without a header but with a section descriptor, to JSON.
410json_object *cper_single_section_to_ir(FILE *cper_section_file)
411{
412 json_object *ir = json_object_new_object();
413
Lawrence Tang94153492022-09-05 13:07:54 +0100414 //Read the current file pointer location as base record position.
415 long base_pos = ftell(cper_section_file);
416
Lawrence Tang617949e2022-08-08 14:21:42 +0100417 //Read the section descriptor out.
418 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
419 if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
420 cper_section_file) != 1) {
421 printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
422 return NULL;
423 }
424
425 //Convert the section descriptor to IR.
426 json_object *section_descriptor_ir =
427 cper_section_descriptor_to_ir(&section_descriptor);
428 json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
429
430 //Parse the single section.
John Chungf8fc7052024-05-03 20:05:29 +0800431 json_object *section_ir = cper_section_to_ir(
432 cper_section_file, base_pos, &section_descriptor);
Lawrence Tang617949e2022-08-08 14:21:42 +0100433 json_object_object_add(ir, "section", section_ir);
434
435 return ir;
John Chungf8fc7052024-05-03 20:05:29 +0800436}
Karthik Rajagopalan5220c9b2024-08-08 00:24:44 -0700437
438char *cper_single_section_to_str_ir(FILE *cper_section_file)
439{
440 json_object *jobj = cper_single_section_to_ir(cper_section_file);
441 char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
442
443 json_object_put(jobj);
444 return str;
445}
446
447char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section,
448 size_t size)
449{
450 FILE *cper_section_file = fmemopen((void *)cper_section, size, "r");
451
452 return cper_section_file ?
453 cper_single_section_to_str_ir(cper_section_file) :
454 NULL;
455}