blob: 4181a644176d08cf0b567a8405241a21068d0201 [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>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070010#include "base64.h"
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010011#include "edk/Cper.h"
12#include "cper-parse.h"
13#include "cper-utils.h"
Lawrence Tang580423f2022-08-24 09:37:53 +010014#include "sections/cper-section.h"
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010015
16//Private pre-definitions.
Lawrence Tange407b4c2022-07-21 13:54:01 +010017json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header);
18json_object *
19cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor);
Lawrence Tang94153492022-09-05 13:07:54 +010020json_object *cper_section_to_ir(FILE *handle, long base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010022
23//Reads a CPER log file at the given file location, and returns an intermediate
24//JSON representation of this CPER record.
Lawrence Tange407b4c2022-07-21 13:54:01 +010025json_object *cper_to_ir(FILE *cper_file)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010026{
Lawrence Tang94153492022-09-05 13:07:54 +010027 //Read the current file pointer location as the base of the record.
28 long base_pos = ftell(cper_file);
29
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 //Ensure this is really a CPER log.
31 EFI_COMMON_ERROR_RECORD_HEADER header;
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1,
33 cper_file) != 1) {
34 printf("Invalid CPER file: Invalid length (log too short).\n");
35 return NULL;
36 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010037
Lawrence Tange407b4c2022-07-21 13:54:01 +010038 //Check if the header contains the magic bytes ("CPER").
39 if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
40 printf("Invalid CPER file: Invalid header (incorrect signature).\n");
41 return NULL;
42 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010043
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 //Create the header JSON object from the read bytes.
45 json_object *header_ir = cper_header_to_ir(&header);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010046
Lawrence Tange407b4c2022-07-21 13:54:01 +010047 //Read the appropriate number of section descriptors & sections, and convert them into IR format.
48 json_object *section_descriptors_ir = json_object_new_array();
49 json_object *sections_ir = json_object_new_array();
50 for (int i = 0; i < header.SectionCount; i++) {
51 //Create the section descriptor.
52 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
53 if (fread(&section_descriptor,
54 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
55 cper_file) != 1) {
56 printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n",
57 header.SectionCount, i + 1);
Karthik Rajagopalanbf6ccc82024-08-05 15:39:51 -070058 // Free json objects
59 json_object_put(sections_ir);
60 json_object_put(section_descriptors_ir);
61 json_object_put(header_ir);
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 return NULL;
63 }
64 json_object_array_add(
65 section_descriptors_ir,
66 cper_section_descriptor_to_ir(&section_descriptor));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010067
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 //Read the section itself.
69 json_object_array_add(sections_ir,
Lawrence Tang94153492022-09-05 13:07:54 +010070 cper_section_to_ir(cper_file, base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +010071 &section_descriptor));
72 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010073
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 //Add the header, section descriptors, and sections to a parent object.
75 json_object *parent = json_object_new_object();
76 json_object_object_add(parent, "header", header_ir);
77 json_object_object_add(parent, "sectionDescriptors",
78 section_descriptors_ir);
79 json_object_object_add(parent, "sections", sections_ir);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010080
Lawrence Tange407b4c2022-07-21 13:54:01 +010081 return parent;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010082}
83
84//Converts a parsed CPER record header into intermediate JSON object format.
Lawrence Tange407b4c2022-07-21 13:54:01 +010085json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010086{
Lawrence Tange407b4c2022-07-21 13:54:01 +010087 json_object *header_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010088
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 //Revision/version information.
90 json_object_object_add(header_ir, "revision",
91 revision_to_ir(header->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010092
Lawrence Tange407b4c2022-07-21 13:54:01 +010093 //Section count.
94 json_object_object_add(header_ir, "sectionCount",
95 json_object_new_int(header->SectionCount));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010096
Lawrence Tange407b4c2022-07-21 13:54:01 +010097 //Error severity (with interpreted string version).
98 json_object *error_severity = json_object_new_object();
99 json_object_object_add(error_severity, "code",
100 json_object_new_uint64(header->ErrorSeverity));
101 json_object_object_add(error_severity, "name",
102 json_object_new_string(severity_to_string(
103 header->ErrorSeverity)));
104 json_object_object_add(header_ir, "severity", error_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100105
Lawrence Tange407b4c2022-07-21 13:54:01 +0100106 //The validation bits for each section.
107 json_object *validation_bits = bitfield_to_ir(
108 header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
109 json_object_object_add(header_ir, "validationBits", validation_bits);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100110
Lawrence Tange407b4c2022-07-21 13:54:01 +0100111 //Total length of the record (including headers) in bytes.
112 json_object_object_add(header_ir, "recordLength",
113 json_object_new_uint64(header->RecordLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100114
Lawrence Tange407b4c2022-07-21 13:54:01 +0100115 //If a timestamp exists according to validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800116 if (header->ValidationBits & 0x2) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100117 char timestamp_string[TIMESTAMP_LENGTH];
118 timestamp_to_string(timestamp_string, &header->TimeStamp);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100119
Lawrence Tange407b4c2022-07-21 13:54:01 +0100120 json_object_object_add(
121 header_ir, "timestamp",
122 json_object_new_string(timestamp_string));
123 json_object_object_add(
124 header_ir, "timestampIsPrecise",
125 json_object_new_boolean(header->TimeStamp.Flag));
126 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100127
Lawrence Tange407b4c2022-07-21 13:54:01 +0100128 //If a platform ID exists according to the validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800129 if (header->ValidationBits & 0x1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 char platform_string[GUID_STRING_LENGTH];
131 guid_to_string(platform_string, &header->PlatformID);
132 json_object_object_add(header_ir, "platformID",
133 json_object_new_string(platform_string));
134 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100135
Lawrence Tange407b4c2022-07-21 13:54:01 +0100136 //If a partition ID exists according to the validation bits, then add it.
John Chungf8fc7052024-05-03 20:05:29 +0800137 if (header->ValidationBits & 0x4) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100138 char partition_string[GUID_STRING_LENGTH];
139 guid_to_string(partition_string, &header->PartitionID);
140 json_object_object_add(
141 header_ir, "partitionID",
142 json_object_new_string(partition_string));
143 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100144
Lawrence Tange407b4c2022-07-21 13:54:01 +0100145 //Creator ID of the header.
146 char creator_string[GUID_STRING_LENGTH];
147 guid_to_string(creator_string, &header->CreatorID);
148 json_object_object_add(header_ir, "creatorID",
149 json_object_new_string(creator_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100150
Lawrence Tange407b4c2022-07-21 13:54:01 +0100151 //Notification type for the header. Some defined types are available.
152 json_object *notification_type = json_object_new_object();
153 char notification_type_string[GUID_STRING_LENGTH];
154 guid_to_string(notification_type_string, &header->NotificationType);
155 json_object_object_add(
156 notification_type, "guid",
157 json_object_new_string(notification_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100158
Lawrence Tange407b4c2022-07-21 13:54:01 +0100159 //Add the human readable notification type if possible.
160 char *notification_type_readable = "Unknown";
161 if (guid_equal(&header->NotificationType,
John Chungf8fc7052024-05-03 20:05:29 +0800162 &gEfiEventNotificationTypeCmcGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100163 notification_type_readable = "CMC";
John Chungf8fc7052024-05-03 20:05:29 +0800164 } else if (guid_equal(&header->NotificationType,
165 &gEfiEventNotificationTypeCpeGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100166 notification_type_readable = "CPE";
John Chungf8fc7052024-05-03 20:05:29 +0800167 } else if (guid_equal(&header->NotificationType,
168 &gEfiEventNotificationTypeMceGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100169 notification_type_readable = "MCE";
John Chungf8fc7052024-05-03 20:05:29 +0800170 } else if (guid_equal(&header->NotificationType,
171 &gEfiEventNotificationTypePcieGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100172 notification_type_readable = "PCIe";
John Chungf8fc7052024-05-03 20:05:29 +0800173 } else if (guid_equal(&header->NotificationType,
174 &gEfiEventNotificationTypeInitGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100175 notification_type_readable = "INIT";
John Chungf8fc7052024-05-03 20:05:29 +0800176 } else if (guid_equal(&header->NotificationType,
177 &gEfiEventNotificationTypeNmiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100178 notification_type_readable = "NMI";
John Chungf8fc7052024-05-03 20:05:29 +0800179 } else if (guid_equal(&header->NotificationType,
180 &gEfiEventNotificationTypeBootGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100181 notification_type_readable = "Boot";
John Chungf8fc7052024-05-03 20:05:29 +0800182 } else if (guid_equal(&header->NotificationType,
183 &gEfiEventNotificationTypeDmarGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100184 notification_type_readable = "DMAr";
John Chungf8fc7052024-05-03 20:05:29 +0800185 } else if (guid_equal(&header->NotificationType,
186 &gEfiEventNotificationTypeSeaGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100187 notification_type_readable = "SEA";
John Chungf8fc7052024-05-03 20:05:29 +0800188 } else if (guid_equal(&header->NotificationType,
189 &gEfiEventNotificationTypeSeiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100190 notification_type_readable = "SEI";
John Chungf8fc7052024-05-03 20:05:29 +0800191 } else if (guid_equal(&header->NotificationType,
192 &gEfiEventNotificationTypePeiGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100193 notification_type_readable = "PEI";
John Chungf8fc7052024-05-03 20:05:29 +0800194 } else if (guid_equal(&header->NotificationType,
195 &gEfiEventNotificationTypeCxlGuid)) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 notification_type_readable = "CXL Component";
John Chungf8fc7052024-05-03 20:05:29 +0800197 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100198 json_object_object_add(
199 notification_type, "type",
200 json_object_new_string(notification_type_readable));
201 json_object_object_add(header_ir, "notificationType",
202 notification_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100203
Lawrence Tange407b4c2022-07-21 13:54:01 +0100204 //The record ID for this record, unique on a given system.
205 json_object_object_add(header_ir, "recordID",
206 json_object_new_uint64(header->RecordID));
207
208 //Flag for the record, and a human readable form.
209 json_object *flags = integer_to_readable_pair(
210 header->Flags,
211 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
212 CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES,
213 "Unknown");
214 json_object_object_add(header_ir, "flags", flags);
215
216 //Persistence information. Outside the scope of specification, so just a uint32 here.
217 json_object_object_add(header_ir, "persistenceInfo",
218 json_object_new_uint64(header->PersistenceInfo));
219 return header_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100220}
221
222//Converts the given EFI section descriptor into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223json_object *
224cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100225{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100226 json_object *section_descriptor_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100227
Lawrence Tange407b4c2022-07-21 13:54:01 +0100228 //The offset of the section from the base of the record header, length.
229 json_object_object_add(
230 section_descriptor_ir, "sectionOffset",
231 json_object_new_uint64(section_descriptor->SectionOffset));
232 json_object_object_add(
233 section_descriptor_ir, "sectionLength",
234 json_object_new_uint64(section_descriptor->SectionLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100235
Lawrence Tange407b4c2022-07-21 13:54:01 +0100236 //Revision.
237 json_object_object_add(section_descriptor_ir, "revision",
238 revision_to_ir(section_descriptor->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100239
Lawrence Tange407b4c2022-07-21 13:54:01 +0100240 //Validation bits.
241 json_object *validation_bits =
242 bitfield_to_ir(section_descriptor->SecValidMask, 2,
243 CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
244 json_object_object_add(section_descriptor_ir, "validationBits",
245 validation_bits);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100246
Lawrence Tange407b4c2022-07-21 13:54:01 +0100247 //Flag bits.
248 json_object *flags =
249 bitfield_to_ir(section_descriptor->SectionFlags, 8,
250 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
251 json_object_object_add(section_descriptor_ir, "flags", flags);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100252
Lawrence Tange407b4c2022-07-21 13:54:01 +0100253 //Section type (GUID).
254 json_object *section_type = json_object_new_object();
255 char section_type_string[GUID_STRING_LENGTH];
256 guid_to_string(section_type_string, &section_descriptor->SectionType);
257 json_object_object_add(section_type, "data",
258 json_object_new_string(section_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100259
Lawrence Tange407b4c2022-07-21 13:54:01 +0100260 //Readable section type, if possible.
Lawrence Tang580423f2022-08-24 09:37:53 +0100261 const char *section_type_readable = "Unknown";
John Chungf8fc7052024-05-03 20:05:29 +0800262 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100263 if (guid_equal(section_definitions[i].Guid,
264 &section_descriptor->SectionType)) {
265 section_type_readable =
266 section_definitions[i].ReadableName;
Lawrence Tang580423f2022-08-24 09:37:53 +0100267 break;
268 }
269 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100270
Lawrence Tange407b4c2022-07-21 13:54:01 +0100271 json_object_object_add(section_type, "type",
272 json_object_new_string(section_type_readable));
273 json_object_object_add(section_descriptor_ir, "sectionType",
274 section_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100275
Lawrence Tange407b4c2022-07-21 13:54:01 +0100276 //If validation bits indicate it exists, add FRU ID.
John Chungf8fc7052024-05-03 20:05:29 +0800277 if (section_descriptor->SecValidMask & 0x1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100278 char fru_id_string[GUID_STRING_LENGTH];
279 guid_to_string(fru_id_string, &section_descriptor->FruId);
280 json_object_object_add(section_descriptor_ir, "fruID",
281 json_object_new_string(fru_id_string));
282 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100283
Lawrence Tange407b4c2022-07-21 13:54:01 +0100284 //If validation bits indicate it exists, add FRU text.
John Chungf8fc7052024-05-03 20:05:29 +0800285 if ((section_descriptor->SecValidMask & 0x2) >> 1) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100286 json_object_object_add(
287 section_descriptor_ir, "fruText",
288 json_object_new_string(section_descriptor->FruString));
John Chungf8fc7052024-05-03 20:05:29 +0800289 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100290
Lawrence Tange407b4c2022-07-21 13:54:01 +0100291 //Section severity.
292 json_object *section_severity = json_object_new_object();
293 json_object_object_add(
294 section_severity, "code",
295 json_object_new_uint64(section_descriptor->Severity));
296 json_object_object_add(section_severity, "name",
297 json_object_new_string(severity_to_string(
298 section_descriptor->Severity)));
299 json_object_object_add(section_descriptor_ir, "severity",
300 section_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100301
Lawrence Tange407b4c2022-07-21 13:54:01 +0100302 return section_descriptor_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100303}
304
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100305//Converts the section described by a single given section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100306json_object *cper_section_to_ir(FILE *handle, long base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100307 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100308{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100309 //Save our current position in the stream.
310 long position = ftell(handle);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100311
Lawrence Tange407b4c2022-07-21 13:54:01 +0100312 //Read section as described by the section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100313 fseek(handle, base_pos + descriptor->SectionOffset, SEEK_SET);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100314 void *section = malloc(descriptor->SectionLength);
315 if (fread(section, descriptor->SectionLength, 1, handle) != 1) {
John Chungf8fc7052024-05-03 20:05:29 +0800316 printf("Section read failed: Could not read %u bytes from global offset %d.\n",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100317 descriptor->SectionLength, descriptor->SectionOffset);
318 free(section);
319 return NULL;
320 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100321
Lawrence Tange407b4c2022-07-21 13:54:01 +0100322 //Seek back to our original position.
323 fseek(handle, position, SEEK_SET);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100324
Lawrence Tange407b4c2022-07-21 13:54:01 +0100325 //Parse section to IR based on GUID.
326 json_object *result = NULL;
Lawrence Tang580423f2022-08-24 09:37:53 +0100327 int section_converted = 0;
John Chungf8fc7052024-05-03 20:05:29 +0800328 for (size_t i = 0; i < section_definitions_len; i++) {
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100329 if (guid_equal(section_definitions[i].Guid,
330 &descriptor->SectionType) &&
331 section_definitions[i].ToIR != NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800332 result = section_definitions[i].ToIR(section);
Lawrence Tang580423f2022-08-24 09:37:53 +0100333 section_converted = 1;
334 break;
335 }
336 }
337
338 //Was it an unknown GUID/failed read?
339 if (!section_converted) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100340 //Output the data as formatted base64.
341 result = json_object_new_object();
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700342
343 int32_t encoded_len = 0;
344 char *encoded = base64_encode(
345 section, descriptor->SectionLength, &encoded_len);
346 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +0800347 printf("Failed to allocate encode output buffer. \n");
348 } else {
John Chungf8fc7052024-05-03 20:05:29 +0800349 json_object_object_add(result, "data",
350 json_object_new_string_len(
351 encoded, encoded_len));
352 free(encoded);
353 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100354 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100355 //Free section memory, return result.
356 free(section);
357 return result;
Lawrence Tang617949e2022-08-08 14:21:42 +0100358}
359
360//Converts a single CPER section, without a header but with a section descriptor, to JSON.
361json_object *cper_single_section_to_ir(FILE *cper_section_file)
362{
363 json_object *ir = json_object_new_object();
364
Lawrence Tang94153492022-09-05 13:07:54 +0100365 //Read the current file pointer location as base record position.
366 long base_pos = ftell(cper_section_file);
367
Lawrence Tang617949e2022-08-08 14:21:42 +0100368 //Read the section descriptor out.
369 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
370 if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
371 cper_section_file) != 1) {
372 printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
373 return NULL;
374 }
375
376 //Convert the section descriptor to IR.
377 json_object *section_descriptor_ir =
378 cper_section_descriptor_to_ir(&section_descriptor);
379 json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
380
381 //Parse the single section.
John Chungf8fc7052024-05-03 20:05:29 +0800382 json_object *section_ir = cper_section_to_ir(
383 cper_section_file, base_pos, &section_descriptor);
Lawrence Tang617949e2022-08-08 14:21:42 +0100384 json_object_object_add(ir, "section", section_ir);
385
386 return ir;
John Chungf8fc7052024-05-03 20:05:29 +0800387}