blob: 128b76850c3db8f0706feb1e4c4bef59c5348b07 [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
25//Reads a CPER log file at the given file location, and returns an intermediate
26//JSON representation of this CPER record.
Lawrence Tange407b4c2022-07-21 13:54:01 +010027json_object *cper_to_ir(FILE *cper_file)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010028{
Lawrence Tang94153492022-09-05 13:07:54 +010029 //Read the current file pointer location as the base of the record.
30 long base_pos = ftell(cper_file);
31
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //Ensure this is really a CPER log.
33 EFI_COMMON_ERROR_RECORD_HEADER header;
Lawrence Tange407b4c2022-07-21 13:54:01 +010034 if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1,
35 cper_file) != 1) {
36 printf("Invalid CPER file: Invalid length (log too short).\n");
37 return NULL;
38 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010039
Lawrence Tange407b4c2022-07-21 13:54:01 +010040 //Check if the header contains the magic bytes ("CPER").
41 if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
42 printf("Invalid CPER file: Invalid header (incorrect signature).\n");
43 return NULL;
44 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010045
Lawrence Tange407b4c2022-07-21 13:54:01 +010046 //Create the header JSON object from the read bytes.
47 json_object *header_ir = cper_header_to_ir(&header);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010048
Lawrence Tange407b4c2022-07-21 13:54:01 +010049 //Read the appropriate number of section descriptors & sections, and convert them into IR format.
50 json_object *section_descriptors_ir = json_object_new_array();
51 json_object *sections_ir = json_object_new_array();
52 for (int i = 0; i < header.SectionCount; i++) {
53 //Create the section descriptor.
54 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
55 if (fread(&section_descriptor,
56 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
57 cper_file) != 1) {
58 printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n",
59 header.SectionCount, i + 1);
Karthik Rajagopalanbf6ccc82024-08-05 15:39:51 -070060 // Free json objects
61 json_object_put(sections_ir);
62 json_object_put(section_descriptors_ir);
63 json_object_put(header_ir);
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 return NULL;
65 }
66 json_object_array_add(
67 section_descriptors_ir,
68 cper_section_descriptor_to_ir(&section_descriptor));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010069
Lawrence Tange407b4c2022-07-21 13:54:01 +010070 //Read the section itself.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080071
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 json_object_array_add(sections_ir,
Lawrence Tang94153492022-09-05 13:07:54 +010073 cper_section_to_ir(cper_file, base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 &section_descriptor));
75 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010076
Lawrence Tange407b4c2022-07-21 13:54:01 +010077 //Add the header, section descriptors, and sections to a parent object.
78 json_object *parent = json_object_new_object();
79 json_object_object_add(parent, "header", header_ir);
80 json_object_object_add(parent, "sectionDescriptors",
81 section_descriptors_ir);
82 json_object_object_add(parent, "sections", sections_ir);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010083
Lawrence Tange407b4c2022-07-21 13:54:01 +010084 return parent;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010085}
86
Karthik Rajagopalan5220c9b2024-08-08 00:24:44 -070087char *cper_to_str_ir(FILE *cper_file)
88{
89 json_object *jobj = cper_to_ir(cper_file);
90 char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
91
92 json_object_put(jobj);
93 return str;
94}
95
96char *cperbuf_to_str_ir(const unsigned char *cper, size_t size)
97{
98 FILE *cper_file = fmemopen((void *)cper, size, "r");
99
100 return cper_file ? cper_to_str_ir(cper_file) : NULL;
101}
102
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100103//Converts a parsed CPER record header into intermediate JSON object format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100104json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100105{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100106 json_object *header_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100107
Lawrence Tange407b4c2022-07-21 13:54:01 +0100108 //Revision/version information.
109 json_object_object_add(header_ir, "revision",
110 revision_to_ir(header->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100111
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 //Section count.
113 json_object_object_add(header_ir, "sectionCount",
114 json_object_new_int(header->SectionCount));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100115
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116 //Error severity (with interpreted string version).
117 json_object *error_severity = json_object_new_object();
118 json_object_object_add(error_severity, "code",
119 json_object_new_uint64(header->ErrorSeverity));
120 json_object_object_add(error_severity, "name",
121 json_object_new_string(severity_to_string(
122 header->ErrorSeverity)));
123 json_object_object_add(header_ir, "severity", error_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100124
Lawrence Tange407b4c2022-07-21 13:54:01 +0100125 //Total length of the record (including headers) in bytes.
126 json_object_object_add(header_ir, "recordLength",
127 json_object_new_uint64(header->RecordLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100128
Lawrence Tange407b4c2022-07-21 13:54:01 +0100129 //If a timestamp exists according to validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800130 if (header->ValidationBits & 0x2) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100131 char timestamp_string[TIMESTAMP_LENGTH];
Ed Tanous13f099f2024-11-20 11:10:30 -0800132 timestamp_to_string(timestamp_string, TIMESTAMP_LENGTH,
133 &header->TimeStamp);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100134
Lawrence Tange407b4c2022-07-21 13:54:01 +0100135 json_object_object_add(
136 header_ir, "timestamp",
137 json_object_new_string(timestamp_string));
138 json_object_object_add(
139 header_ir, "timestampIsPrecise",
140 json_object_new_boolean(header->TimeStamp.Flag));
141 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //If a platform ID exists according to the validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800144 if (header->ValidationBits & 0x1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100145 char platform_string[GUID_STRING_LENGTH];
146 guid_to_string(platform_string, &header->PlatformID);
147 json_object_object_add(header_ir, "platformID",
148 json_object_new_string(platform_string));
149 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100150
Lawrence Tange407b4c2022-07-21 13:54:01 +0100151 //If a partition ID exists according to the validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800152 if (header->ValidationBits & 0x4) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100153 char partition_string[GUID_STRING_LENGTH];
154 guid_to_string(partition_string, &header->PartitionID);
155 json_object_object_add(
156 header_ir, "partitionID",
157 json_object_new_string(partition_string));
158 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100159
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160 //Creator ID of the header.
161 char creator_string[GUID_STRING_LENGTH];
162 guid_to_string(creator_string, &header->CreatorID);
163 json_object_object_add(header_ir, "creatorID",
164 json_object_new_string(creator_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100165
Lawrence Tange407b4c2022-07-21 13:54:01 +0100166 //Notification type for the header. Some defined types are available.
167 json_object *notification_type = json_object_new_object();
168 char notification_type_string[GUID_STRING_LENGTH];
169 guid_to_string(notification_type_string, &header->NotificationType);
170 json_object_object_add(
171 notification_type, "guid",
172 json_object_new_string(notification_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100173
Lawrence Tange407b4c2022-07-21 13:54:01 +0100174 //Add the human readable notification type if possible.
175 char *notification_type_readable = "Unknown";
176 if (guid_equal(&header->NotificationType,
John Chungf8fc7052024-05-03 20:05:29 +0800177 &gEfiEventNotificationTypeCmcGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100178 notification_type_readable = "CMC";
John Chungf8fc7052024-05-03 20:05:29 +0800179 } else if (guid_equal(&header->NotificationType,
180 &gEfiEventNotificationTypeCpeGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100181 notification_type_readable = "CPE";
John Chungf8fc7052024-05-03 20:05:29 +0800182 } else if (guid_equal(&header->NotificationType,
183 &gEfiEventNotificationTypeMceGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100184 notification_type_readable = "MCE";
John Chungf8fc7052024-05-03 20:05:29 +0800185 } else if (guid_equal(&header->NotificationType,
186 &gEfiEventNotificationTypePcieGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100187 notification_type_readable = "PCIe";
John Chungf8fc7052024-05-03 20:05:29 +0800188 } else if (guid_equal(&header->NotificationType,
189 &gEfiEventNotificationTypeInitGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100190 notification_type_readable = "INIT";
John Chungf8fc7052024-05-03 20:05:29 +0800191 } else if (guid_equal(&header->NotificationType,
192 &gEfiEventNotificationTypeNmiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100193 notification_type_readable = "NMI";
John Chungf8fc7052024-05-03 20:05:29 +0800194 } else if (guid_equal(&header->NotificationType,
195 &gEfiEventNotificationTypeBootGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 notification_type_readable = "Boot";
John Chungf8fc7052024-05-03 20:05:29 +0800197 } else if (guid_equal(&header->NotificationType,
198 &gEfiEventNotificationTypeDmarGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100199 notification_type_readable = "DMAr";
John Chungf8fc7052024-05-03 20:05:29 +0800200 } else if (guid_equal(&header->NotificationType,
201 &gEfiEventNotificationTypeSeaGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100202 notification_type_readable = "SEA";
John Chungf8fc7052024-05-03 20:05:29 +0800203 } else if (guid_equal(&header->NotificationType,
204 &gEfiEventNotificationTypeSeiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100205 notification_type_readable = "SEI";
John Chungf8fc7052024-05-03 20:05:29 +0800206 } else if (guid_equal(&header->NotificationType,
207 &gEfiEventNotificationTypePeiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100208 notification_type_readable = "PEI";
John Chungf8fc7052024-05-03 20:05:29 +0800209 } else if (guid_equal(&header->NotificationType,
210 &gEfiEventNotificationTypeCxlGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100211 notification_type_readable = "CXL Component";
John Chungf8fc7052024-05-03 20:05:29 +0800212 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100213 json_object_object_add(
214 notification_type, "type",
215 json_object_new_string(notification_type_readable));
216 json_object_object_add(header_ir, "notificationType",
217 notification_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100218
Lawrence Tange407b4c2022-07-21 13:54:01 +0100219 //The record ID for this record, unique on a given system.
220 json_object_object_add(header_ir, "recordID",
221 json_object_new_uint64(header->RecordID));
222
223 //Flag for the record, and a human readable form.
224 json_object *flags = integer_to_readable_pair(
225 header->Flags,
226 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
227 CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES,
228 "Unknown");
229 json_object_object_add(header_ir, "flags", flags);
230
231 //Persistence information. Outside the scope of specification, so just a uint32 here.
232 json_object_object_add(header_ir, "persistenceInfo",
233 json_object_new_uint64(header->PersistenceInfo));
234 return header_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100235}
236
237//Converts the given EFI section descriptor into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100238json_object *
239cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100240{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100241 json_object *section_descriptor_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100242
Lawrence Tange407b4c2022-07-21 13:54:01 +0100243 //The offset of the section from the base of the record header, length.
244 json_object_object_add(
245 section_descriptor_ir, "sectionOffset",
246 json_object_new_uint64(section_descriptor->SectionOffset));
247 json_object_object_add(
248 section_descriptor_ir, "sectionLength",
249 json_object_new_uint64(section_descriptor->SectionLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100250
Lawrence Tange407b4c2022-07-21 13:54:01 +0100251 //Revision.
252 json_object_object_add(section_descriptor_ir, "revision",
253 revision_to_ir(section_descriptor->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100254
Lawrence Tange407b4c2022-07-21 13:54:01 +0100255 //Flag bits.
256 json_object *flags =
257 bitfield_to_ir(section_descriptor->SectionFlags, 8,
258 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
259 json_object_object_add(section_descriptor_ir, "flags", flags);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100260
Lawrence Tange407b4c2022-07-21 13:54:01 +0100261 //Section type (GUID).
262 json_object *section_type = json_object_new_object();
263 char section_type_string[GUID_STRING_LENGTH];
264 guid_to_string(section_type_string, &section_descriptor->SectionType);
265 json_object_object_add(section_type, "data",
266 json_object_new_string(section_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100267
Lawrence Tange407b4c2022-07-21 13:54:01 +0100268 //Readable section type, if possible.
Lawrence Tang580423f2022-08-24 09:37:53 +0100269 const char *section_type_readable = "Unknown";
John Chungf8fc7052024-05-03 20:05:29 +0800270 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100271 if (guid_equal(section_definitions[i].Guid,
272 &section_descriptor->SectionType)) {
273 section_type_readable =
274 section_definitions[i].ReadableName;
Lawrence Tang580423f2022-08-24 09:37:53 +0100275 break;
276 }
277 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100278
Lawrence Tange407b4c2022-07-21 13:54:01 +0100279 json_object_object_add(section_type, "type",
280 json_object_new_string(section_type_readable));
281 json_object_object_add(section_descriptor_ir, "sectionType",
282 section_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100283
Lawrence Tange407b4c2022-07-21 13:54:01 +0100284 //If validation bits indicate it exists, add FRU ID.
John Chungf8fc7052024-05-03 20:05:29 +0800285 if (section_descriptor->SecValidMask & 0x1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100286 char fru_id_string[GUID_STRING_LENGTH];
287 guid_to_string(fru_id_string, &section_descriptor->FruId);
288 json_object_object_add(section_descriptor_ir, "fruID",
289 json_object_new_string(fru_id_string));
290 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100291
Lawrence Tange407b4c2022-07-21 13:54:01 +0100292 //If validation bits indicate it exists, add FRU text.
John Chungf8fc7052024-05-03 20:05:29 +0800293 if ((section_descriptor->SecValidMask & 0x2) >> 1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100294 json_object_object_add(
295 section_descriptor_ir, "fruText",
296 json_object_new_string(section_descriptor->FruString));
John Chungf8fc7052024-05-03 20:05:29 +0800297 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100298
Lawrence Tange407b4c2022-07-21 13:54:01 +0100299 //Section severity.
300 json_object *section_severity = json_object_new_object();
301 json_object_object_add(
302 section_severity, "code",
303 json_object_new_uint64(section_descriptor->Severity));
304 json_object_object_add(section_severity, "name",
305 json_object_new_string(severity_to_string(
306 section_descriptor->Severity)));
307 json_object_object_add(section_descriptor_ir, "severity",
308 section_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100309
Lawrence Tange407b4c2022-07-21 13:54:01 +0100310 return section_descriptor_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100311}
312
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100313//Converts the section described by a single given section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100314json_object *cper_section_to_ir(FILE *handle, long base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100315 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100316{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100317 //Save our current position in the stream.
318 long position = ftell(handle);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100319
Lawrence Tange407b4c2022-07-21 13:54:01 +0100320 //Read section as described by the section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100321 fseek(handle, base_pos + descriptor->SectionOffset, SEEK_SET);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100322 void *section = malloc(descriptor->SectionLength);
323 if (fread(section, descriptor->SectionLength, 1, handle) != 1) {
John Chungf8fc7052024-05-03 20:05:29 +0800324 printf("Section read failed: Could not read %u bytes from global offset %d.\n",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100325 descriptor->SectionLength, descriptor->SectionOffset);
326 free(section);
327 return NULL;
328 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100329
Lawrence Tange407b4c2022-07-21 13:54:01 +0100330 //Seek back to our original position.
331 fseek(handle, position, SEEK_SET);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100332
Lawrence Tange407b4c2022-07-21 13:54:01 +0100333 //Parse section to IR based on GUID.
334 json_object *result = NULL;
Ed Tanousb07061a2024-09-22 10:33:29 -0700335
336 json_object *section_ir = NULL;
Lawrence Tang580423f2022-08-24 09:37:53 +0100337 int section_converted = 0;
John Chungf8fc7052024-05-03 20:05:29 +0800338 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100339 if (guid_equal(section_definitions[i].Guid,
340 &descriptor->SectionType) &&
341 section_definitions[i].ToIR != NULL) {
Ed Tanousb07061a2024-09-22 10:33:29 -0700342 section_ir = section_definitions[i].ToIR(section);
343
344 result = json_object_new_object();
345 json_object_object_add(result,
346 section_definitions[i].ShortName,
347 section_ir);
348
Lawrence Tang580423f2022-08-24 09:37:53 +0100349 section_converted = 1;
350 break;
351 }
352 }
353
354 //Was it an unknown GUID/failed read?
355 if (!section_converted) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100356 //Output the data as formatted base64.
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700357 int32_t encoded_len = 0;
358 char *encoded = base64_encode(
359 section, descriptor->SectionLength, &encoded_len);
360 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800361 printf("Failed to allocate encode output buffer. \n");
362 } else {
Ed Tanousb07061a2024-09-22 10:33:29 -0700363 section_ir = json_object_new_object();
364 json_object_object_add(section_ir, "data",
John Chungf8fc7052024-05-03 20:05:29 +0800365 json_object_new_string_len(
366 encoded, encoded_len));
367 free(encoded);
Ed Tanousb07061a2024-09-22 10:33:29 -0700368
369 result = json_object_new_object();
370 json_object_object_add(result, "Unknown", section_ir);
John Chungf8fc7052024-05-03 20:05:29 +0800371 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100372 }
Ed Tanousb07061a2024-09-22 10:33:29 -0700373
Lawrence Tange407b4c2022-07-21 13:54:01 +0100374 //Free section memory, return result.
375 free(section);
376 return result;
Lawrence Tang617949e2022-08-08 14:21:42 +0100377}
378
379//Converts a single CPER section, without a header but with a section descriptor, to JSON.
380json_object *cper_single_section_to_ir(FILE *cper_section_file)
381{
382 json_object *ir = json_object_new_object();
383
Lawrence Tang94153492022-09-05 13:07:54 +0100384 //Read the current file pointer location as base record position.
385 long base_pos = ftell(cper_section_file);
386
Lawrence Tang617949e2022-08-08 14:21:42 +0100387 //Read the section descriptor out.
388 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
389 if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
390 cper_section_file) != 1) {
391 printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
392 return NULL;
393 }
394
395 //Convert the section descriptor to IR.
396 json_object *section_descriptor_ir =
397 cper_section_descriptor_to_ir(&section_descriptor);
398 json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
399
400 //Parse the single section.
John Chungf8fc7052024-05-03 20:05:29 +0800401 json_object *section_ir = cper_section_to_ir(
402 cper_section_file, base_pos, &section_descriptor);
Lawrence Tang617949e2022-08-08 14:21:42 +0100403 json_object_object_add(ir, "section", section_ir);
404
405 return ir;
John Chungf8fc7052024-05-03 20:05:29 +0800406}
Karthik Rajagopalan5220c9b2024-08-08 00:24:44 -0700407
408char *cper_single_section_to_str_ir(FILE *cper_section_file)
409{
410 json_object *jobj = cper_single_section_to_ir(cper_section_file);
411 char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
412
413 json_object_put(jobj);
414 return str;
415}
416
417char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section,
418 size_t size)
419{
420 FILE *cper_section_file = fmemopen((void *)cper_section, size, "r");
421
422 return cper_section_file ?
423 cper_single_section_to_str_ir(cper_section_file) :
424 NULL;
425}