blob: e66ed286690e8fe8204794ea0de3e50a32032486 [file] [log] [blame]
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01001/**
Lawrence Tang2800cd82022-07-05 16:08:20 +01002 * Describes high level functions for converting an entire CPER log, and functions for parsing
3 * CPER headers and section descriptions into an intermediate JSON format.
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01004 *
5 * Author: Lawrence.Tang@arm.com
6 **/
7
8#include <stdio.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010010#include "b64.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);
58 return NULL;
59 }
60 json_object_array_add(
61 section_descriptors_ir,
62 cper_section_descriptor_to_ir(&section_descriptor));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010063
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 //Read the section itself.
65 json_object_array_add(sections_ir,
Lawrence Tang94153492022-09-05 13:07:54 +010066 cper_section_to_ir(cper_file, base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +010067 &section_descriptor));
68 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010069
Lawrence Tange407b4c2022-07-21 13:54:01 +010070 //Add the header, section descriptors, and sections to a parent object.
71 json_object *parent = json_object_new_object();
72 json_object_object_add(parent, "header", header_ir);
73 json_object_object_add(parent, "sectionDescriptors",
74 section_descriptors_ir);
75 json_object_object_add(parent, "sections", sections_ir);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010076
Lawrence Tange407b4c2022-07-21 13:54:01 +010077 return parent;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010078}
79
80//Converts a parsed CPER record header into intermediate JSON object format.
Lawrence Tange407b4c2022-07-21 13:54:01 +010081json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010082{
Lawrence Tange407b4c2022-07-21 13:54:01 +010083 json_object *header_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010084
Lawrence Tange407b4c2022-07-21 13:54:01 +010085 //Revision/version information.
86 json_object_object_add(header_ir, "revision",
87 revision_to_ir(header->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010088
Lawrence Tange407b4c2022-07-21 13:54:01 +010089 //Section count.
90 json_object_object_add(header_ir, "sectionCount",
91 json_object_new_int(header->SectionCount));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010092
Lawrence Tange407b4c2022-07-21 13:54:01 +010093 //Error severity (with interpreted string version).
94 json_object *error_severity = json_object_new_object();
95 json_object_object_add(error_severity, "code",
96 json_object_new_uint64(header->ErrorSeverity));
97 json_object_object_add(error_severity, "name",
98 json_object_new_string(severity_to_string(
99 header->ErrorSeverity)));
100 json_object_object_add(header_ir, "severity", error_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100101
Lawrence Tange407b4c2022-07-21 13:54:01 +0100102 //The validation bits for each section.
103 json_object *validation_bits = bitfield_to_ir(
104 header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
105 json_object_object_add(header_ir, "validationBits", validation_bits);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100106
Lawrence Tange407b4c2022-07-21 13:54:01 +0100107 //Total length of the record (including headers) in bytes.
108 json_object_object_add(header_ir, "recordLength",
109 json_object_new_uint64(header->RecordLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100110
Lawrence Tange407b4c2022-07-21 13:54:01 +0100111 //If a timestamp exists according to validation bits, then add it.
112 if (header->ValidationBits & 0b10) {
113 char timestamp_string[TIMESTAMP_LENGTH];
114 timestamp_to_string(timestamp_string, &header->TimeStamp);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100115
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116 json_object_object_add(
117 header_ir, "timestamp",
118 json_object_new_string(timestamp_string));
119 json_object_object_add(
120 header_ir, "timestampIsPrecise",
121 json_object_new_boolean(header->TimeStamp.Flag));
122 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100123
Lawrence Tange407b4c2022-07-21 13:54:01 +0100124 //If a platform ID exists according to the validation bits, then add it.
125 if (header->ValidationBits & 0b1) {
126 char platform_string[GUID_STRING_LENGTH];
127 guid_to_string(platform_string, &header->PlatformID);
128 json_object_object_add(header_ir, "platformID",
129 json_object_new_string(platform_string));
130 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100131
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 //If a partition ID exists according to the validation bits, then add it.
133 if (header->ValidationBits & 0b100) {
134 char partition_string[GUID_STRING_LENGTH];
135 guid_to_string(partition_string, &header->PartitionID);
136 json_object_object_add(
137 header_ir, "partitionID",
138 json_object_new_string(partition_string));
139 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100140
Lawrence Tange407b4c2022-07-21 13:54:01 +0100141 //Creator ID of the header.
142 char creator_string[GUID_STRING_LENGTH];
143 guid_to_string(creator_string, &header->CreatorID);
144 json_object_object_add(header_ir, "creatorID",
145 json_object_new_string(creator_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100146
Lawrence Tange407b4c2022-07-21 13:54:01 +0100147 //Notification type for the header. Some defined types are available.
148 json_object *notification_type = json_object_new_object();
149 char notification_type_string[GUID_STRING_LENGTH];
150 guid_to_string(notification_type_string, &header->NotificationType);
151 json_object_object_add(
152 notification_type, "guid",
153 json_object_new_string(notification_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100154
Lawrence Tange407b4c2022-07-21 13:54:01 +0100155 //Add the human readable notification type if possible.
156 char *notification_type_readable = "Unknown";
157 if (guid_equal(&header->NotificationType,
158 &gEfiEventNotificationTypeCmcGuid))
159 notification_type_readable = "CMC";
160 else if (guid_equal(&header->NotificationType,
161 &gEfiEventNotificationTypeCpeGuid))
162 notification_type_readable = "CPE";
163 else if (guid_equal(&header->NotificationType,
164 &gEfiEventNotificationTypeMceGuid))
165 notification_type_readable = "MCE";
166 else if (guid_equal(&header->NotificationType,
167 &gEfiEventNotificationTypePcieGuid))
168 notification_type_readable = "PCIe";
169 else if (guid_equal(&header->NotificationType,
170 &gEfiEventNotificationTypeInitGuid))
171 notification_type_readable = "INIT";
172 else if (guid_equal(&header->NotificationType,
173 &gEfiEventNotificationTypeNmiGuid))
174 notification_type_readable = "NMI";
175 else if (guid_equal(&header->NotificationType,
176 &gEfiEventNotificationTypeBootGuid))
177 notification_type_readable = "Boot";
178 else if (guid_equal(&header->NotificationType,
179 &gEfiEventNotificationTypeDmarGuid))
180 notification_type_readable = "DMAr";
181 else if (guid_equal(&header->NotificationType,
182 &gEfiEventNotificationTypeSeaGuid))
183 notification_type_readable = "SEA";
184 else if (guid_equal(&header->NotificationType,
185 &gEfiEventNotificationTypeSeiGuid))
186 notification_type_readable = "SEI";
187 else if (guid_equal(&header->NotificationType,
188 &gEfiEventNotificationTypePeiGuid))
189 notification_type_readable = "PEI";
190 else if (guid_equal(&header->NotificationType,
191 &gEfiEventNotificationTypeCxlGuid))
192 notification_type_readable = "CXL Component";
193 json_object_object_add(
194 notification_type, "type",
195 json_object_new_string(notification_type_readable));
196 json_object_object_add(header_ir, "notificationType",
197 notification_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100198
Lawrence Tange407b4c2022-07-21 13:54:01 +0100199 //The record ID for this record, unique on a given system.
200 json_object_object_add(header_ir, "recordID",
201 json_object_new_uint64(header->RecordID));
202
203 //Flag for the record, and a human readable form.
204 json_object *flags = integer_to_readable_pair(
205 header->Flags,
206 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
207 CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES,
208 "Unknown");
209 json_object_object_add(header_ir, "flags", flags);
210
211 //Persistence information. Outside the scope of specification, so just a uint32 here.
212 json_object_object_add(header_ir, "persistenceInfo",
213 json_object_new_uint64(header->PersistenceInfo));
214 return header_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100215}
216
217//Converts the given EFI section descriptor into JSON IR format.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100218json_object *
219cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100220{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100221 json_object *section_descriptor_ir = json_object_new_object();
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100222
Lawrence Tange407b4c2022-07-21 13:54:01 +0100223 //The offset of the section from the base of the record header, length.
224 json_object_object_add(
225 section_descriptor_ir, "sectionOffset",
226 json_object_new_uint64(section_descriptor->SectionOffset));
227 json_object_object_add(
228 section_descriptor_ir, "sectionLength",
229 json_object_new_uint64(section_descriptor->SectionLength));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100230
Lawrence Tange407b4c2022-07-21 13:54:01 +0100231 //Revision.
232 json_object_object_add(section_descriptor_ir, "revision",
233 revision_to_ir(section_descriptor->Revision));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100234
Lawrence Tange407b4c2022-07-21 13:54:01 +0100235 //Validation bits.
236 json_object *validation_bits =
237 bitfield_to_ir(section_descriptor->SecValidMask, 2,
238 CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
239 json_object_object_add(section_descriptor_ir, "validationBits",
240 validation_bits);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100241
Lawrence Tange407b4c2022-07-21 13:54:01 +0100242 //Flag bits.
243 json_object *flags =
244 bitfield_to_ir(section_descriptor->SectionFlags, 8,
245 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
246 json_object_object_add(section_descriptor_ir, "flags", flags);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100247
Lawrence Tange407b4c2022-07-21 13:54:01 +0100248 //Section type (GUID).
249 json_object *section_type = json_object_new_object();
250 char section_type_string[GUID_STRING_LENGTH];
251 guid_to_string(section_type_string, &section_descriptor->SectionType);
252 json_object_object_add(section_type, "data",
253 json_object_new_string(section_type_string));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100254
Lawrence Tange407b4c2022-07-21 13:54:01 +0100255 //Readable section type, if possible.
Lawrence Tang580423f2022-08-24 09:37:53 +0100256 const char *section_type_readable = "Unknown";
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100257 for (int i = 0; i < section_definitions_len; i++) {
258 if (guid_equal(section_definitions[i].Guid,
259 &section_descriptor->SectionType)) {
260 section_type_readable =
261 section_definitions[i].ReadableName;
Lawrence Tang580423f2022-08-24 09:37:53 +0100262 break;
263 }
264 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100265
Lawrence Tange407b4c2022-07-21 13:54:01 +0100266 json_object_object_add(section_type, "type",
267 json_object_new_string(section_type_readable));
268 json_object_object_add(section_descriptor_ir, "sectionType",
269 section_type);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100270
Lawrence Tange407b4c2022-07-21 13:54:01 +0100271 //If validation bits indicate it exists, add FRU ID.
272 if (section_descriptor->SecValidMask & 0b1) {
273 char fru_id_string[GUID_STRING_LENGTH];
274 guid_to_string(fru_id_string, &section_descriptor->FruId);
275 json_object_object_add(section_descriptor_ir, "fruID",
276 json_object_new_string(fru_id_string));
277 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100278
Lawrence Tange407b4c2022-07-21 13:54:01 +0100279 //If validation bits indicate it exists, add FRU text.
280 if ((section_descriptor->SecValidMask & 0b10) >> 1)
281 json_object_object_add(
282 section_descriptor_ir, "fruText",
283 json_object_new_string(section_descriptor->FruString));
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100284
Lawrence Tange407b4c2022-07-21 13:54:01 +0100285 //Section severity.
286 json_object *section_severity = json_object_new_object();
287 json_object_object_add(
288 section_severity, "code",
289 json_object_new_uint64(section_descriptor->Severity));
290 json_object_object_add(section_severity, "name",
291 json_object_new_string(severity_to_string(
292 section_descriptor->Severity)));
293 json_object_object_add(section_descriptor_ir, "severity",
294 section_severity);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100295
Lawrence Tange407b4c2022-07-21 13:54:01 +0100296 return section_descriptor_ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100297}
298
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100299//Converts the section described by a single given section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100300json_object *cper_section_to_ir(FILE *handle, long base_pos,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100301 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100302{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100303 //Save our current position in the stream.
304 long position = ftell(handle);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100305
Lawrence Tange407b4c2022-07-21 13:54:01 +0100306 //Read section as described by the section descriptor.
Lawrence Tang94153492022-09-05 13:07:54 +0100307 fseek(handle, base_pos + descriptor->SectionOffset, SEEK_SET);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100308 void *section = malloc(descriptor->SectionLength);
309 if (fread(section, descriptor->SectionLength, 1, handle) != 1) {
310 printf("Section read failed: Could not read %d bytes from global offset %d.\n",
311 descriptor->SectionLength, descriptor->SectionOffset);
312 free(section);
313 return NULL;
314 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100315
Lawrence Tange407b4c2022-07-21 13:54:01 +0100316 //Seek back to our original position.
317 fseek(handle, position, SEEK_SET);
Lawrence Tangde9707f2022-07-19 10:54:31 +0100318
Lawrence Tange407b4c2022-07-21 13:54:01 +0100319 //Parse section to IR based on GUID.
320 json_object *result = NULL;
Lawrence Tang580423f2022-08-24 09:37:53 +0100321 int section_converted = 0;
Lawrence Tangf1f3b832022-08-24 09:42:39 +0100322 for (int i = 0; i < section_definitions_len; i++) {
323 if (guid_equal(section_definitions[i].Guid,
324 &descriptor->SectionType) &&
325 section_definitions[i].ToIR != NULL) {
326 result = section_definitions[i].ToIR(section,
327 descriptor);
Lawrence Tang580423f2022-08-24 09:37:53 +0100328 section_converted = 1;
329 break;
330 }
331 }
332
333 //Was it an unknown GUID/failed read?
334 if (!section_converted) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100335 //Output the data as formatted base64.
336 result = json_object_new_object();
337 char *encoded = b64_encode((unsigned char *)section,
338 descriptor->SectionLength);
339 json_object_object_add(result, "data",
340 json_object_new_string(encoded));
341 free(encoded);
342 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100343
Lawrence Tange407b4c2022-07-21 13:54:01 +0100344 //Free section memory, return result.
345 free(section);
346 return result;
Lawrence Tang617949e2022-08-08 14:21:42 +0100347}
348
349//Converts a single CPER section, without a header but with a section descriptor, to JSON.
350json_object *cper_single_section_to_ir(FILE *cper_section_file)
351{
352 json_object *ir = json_object_new_object();
353
Lawrence Tang94153492022-09-05 13:07:54 +0100354 //Read the current file pointer location as base record position.
355 long base_pos = ftell(cper_section_file);
356
Lawrence Tang617949e2022-08-08 14:21:42 +0100357 //Read the section descriptor out.
358 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
359 if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
360 cper_section_file) != 1) {
361 printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
362 return NULL;
363 }
364
365 //Convert the section descriptor to IR.
366 json_object *section_descriptor_ir =
367 cper_section_descriptor_to_ir(&section_descriptor);
368 json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
369
370 //Parse the single section.
371 json_object *section_ir =
Lawrence Tang94153492022-09-05 13:07:54 +0100372 cper_section_to_ir(cper_section_file, base_pos, &section_descriptor);
Lawrence Tang617949e2022-08-08 14:21:42 +0100373 json_object_object_add(ir, "section", section_ir);
374
375 return ir;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100376}