blob: 6c743a15d074f0c58a49b8f4ec15bec397cd5ec0 [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>
9#include "json.h"
10#include "edk/Cper.h"
11#include "cper-parse.h"
12#include "cper-utils.h"
13#include "sections/cper-section-generic.h"
Lawrence Tang794312c2022-07-05 14:46:10 +010014#include "sections/cper-section-ia32x64.h"
Lawrence Tang2800cd82022-07-05 16:08:20 +010015#include "sections/cper-section-arm.h"
Lawrence Tanga0865e32022-07-06 11:59:52 +010016#include "sections/cper-section-memory.h"
Lawrence Tang4dbe3d72022-07-06 13:51:01 +010017#include "sections/cper-section-pcie.h"
Lawrence Tang214a1542022-07-06 14:11:28 +010018#include "sections/cper-section-pci-bus.h"
Lawrence Tanga416ec92022-07-06 14:34:40 +010019#include "sections/cper-section-pci-dev.h"
Lawrence Tangc60a2432022-07-06 14:58:33 +010020#include "sections/cper-section-firmware.h"
Lawrence Tang4795d4a2022-07-06 15:19:31 +010021#include "sections/cper-section-dmar-generic.h"
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010022#include "sections/cper-section-dmar-vtd.h"
23#include "sections/cper-section-dmar-iommu.h"
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010024
25//Private pre-definitions.
26json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header);
27json_object* cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR* section_descriptor);
28json_object* cper_section_to_ir(FILE* handle, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
29
30//Reads a CPER log file at the given file location, and returns an intermediate
31//JSON representation of this CPER record.
32json_object* cper_to_ir(const char* filename)
33{
34 //Get a handle for the log file.
35 FILE* cper_file = fopen(filename, "r");
36 if (cper_file == NULL) {
37 printf("Could not open CPER record, file handle returned null.");
38 return NULL;
39 }
40
41 //Ensure this is really a CPER log.
42 EFI_COMMON_ERROR_RECORD_HEADER header;
43 fseek(cper_file, 0, SEEK_SET);
44 if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, cper_file) != 1)
45 {
46 printf("Invalid CPER file: Invalid length (log too short).");
47 return NULL;
48 }
49
50 //Check if the header contains the magic bytes ("CPER").
51 if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
52 printf("Invalid CPER file: Invalid header (incorrect signature).");
53 return NULL;
54 }
55
56 // //Print struct contents (debug).
57 // fpos_t file_pos;
58 // fgetpos(cper_file, &file_pos);
59 // printf("Stream is at position %d.\n", file_pos);
60 // printf("SignatureStart: %s\n", (char*)&header.SignatureStart);
61 // printf("Revision: %u\n", header.Revision);
62 // printf("SectionCount: %u\n", header.SectionCount);
63 // printf("Severity: %d\n", header.ErrorSeverity);
64 // printf("RecordLength: %d\n", header.RecordLength);
65
66 //Create the header JSON object from the read bytes.
67 json_object* header_ir = cper_header_to_ir(&header);
68
69 //Read the appropriate number of section descriptors & sections, and convert them into IR format.
70 json_object* section_descriptors_ir = json_object_new_array();
71 json_object* sections_ir = json_object_new_array();
72 for (int i=0; i<header.SectionCount; i++)
73 {
74 //Create the section descriptor.
75 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
76 if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, cper_file) != 1)
77 {
78 printf("Invalid number of section headers: Header states %d sections, could not read section %d.", header.SectionCount, i+1);
79 return NULL;
80 }
81 json_object_array_add(section_descriptors_ir, cper_section_descriptor_to_ir(&section_descriptor));
82
83 //Read the section itself.
84 json_object_array_add(sections_ir, cper_section_to_ir(cper_file, &section_descriptor));
85 }
86
87 //Add the header, section descriptors, and sections to a parent object.
88 json_object* parent = json_object_new_object();
89 json_object_object_add(parent, "header", header_ir);
90 json_object_object_add(parent, "sectionDescriptors", section_descriptors_ir);
Lawrence Tang2800cd82022-07-05 16:08:20 +010091 json_object_object_add(parent, "sections", sections_ir);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010092
93 //...
94 return parent;
95}
96
97//Converts a parsed CPER record header into intermediate JSON object format.
98json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header)
99{
100 json_object* header_ir = json_object_new_object();
101
102 //Revision/version information.
103 json_object_object_add(header_ir, "revision", revision_to_ir(header->Revision));
104
105 //Section count.
106 json_object_object_add(header_ir, "sectionCount", json_object_new_int(header->SectionCount));
107
108 //Error severity (with interpreted string version).
109 json_object* error_severity = json_object_new_object();
110 json_object_object_add(error_severity, "code", json_object_new_int(header->ErrorSeverity));
111 json_object_object_add(error_severity, "name", json_object_new_string(severity_to_string(header->ErrorSeverity)));
112 json_object_object_add(header_ir, "severity", error_severity);
113
114 //The validation bits for each section.
Lawrence Tang2800cd82022-07-05 16:08:20 +0100115 json_object* validation_bits = bitfield_to_ir(header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100116 json_object_object_add(header_ir, "validationBits", validation_bits);
117
118 //Total length of the record (including headers) in bytes.
119 json_object_object_add(header_ir, "recordLength", json_object_new_int(header->RecordLength));
120
121 //If a timestamp exists according to validation bits, then add it.
122 if (header->ValidationBits & 0b10)
123 {
124 char timestamp_string[TIMESTAMP_LENGTH];
125 sprintf(timestamp_string, "%02d%02d-%02d-%02dT%02d:%02d:%02d.000",
126 header->TimeStamp.Century,
127 header->TimeStamp.Year,
128 header->TimeStamp.Month,
129 header->TimeStamp.Day,
130 header->TimeStamp.Hours,
131 header->TimeStamp.Minutes,
132 header->TimeStamp.Seconds);
133
134 json_object_object_add(header_ir, "timestamp", json_object_new_string(timestamp_string));
135 json_object_object_add(header_ir, "timestampIsPrecise", json_object_new_boolean(header->TimeStamp.Flag));
136 }
137
138 //If a platform ID exists according to the validation bits, then add it.
139 if (header->ValidationBits & 0b1)
140 {
141 char platform_string[GUID_STRING_LENGTH];
142 guid_to_string(platform_string, &header->PlatformID);
143 json_object_object_add(header_ir, "platformID", json_object_new_string(platform_string));
144 }
145
146 //If a partition ID exists according to the validation bits, then add it.
147 if (header->ValidationBits & 0b100)
148 {
149 char partition_string[GUID_STRING_LENGTH];
150 guid_to_string(partition_string, &header->PartitionID);
151 json_object_object_add(header_ir, "partitionID", json_object_new_string(partition_string));
152 }
153
154 //Creator ID of the header.
155 char creator_string[GUID_STRING_LENGTH];
156 guid_to_string(creator_string, &header->CreatorID);
157 json_object_object_add(header_ir, "creatorID", json_object_new_string(creator_string));
158
159 //Notification type for the header. Some defined types are available.
160 json_object* notification_type = json_object_new_object();
161 char notification_type_string[GUID_STRING_LENGTH];
162 guid_to_string(notification_type_string, &header->NotificationType);
163 json_object_object_add(notification_type, "guid", json_object_new_string(notification_type_string));
164
165 //Add the human readable notification type if possible.
166 char* notification_type_readable = "Unknown";
167 if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCmcGuid))
168 notification_type_readable = "CMC";
Lawrence Tang794312c2022-07-05 14:46:10 +0100169 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCpeGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100170 notification_type_readable = "CPE";
Lawrence Tang794312c2022-07-05 14:46:10 +0100171 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeMceGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100172 notification_type_readable = "MCE";
Lawrence Tang794312c2022-07-05 14:46:10 +0100173 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypePcieGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100174 notification_type_readable = "PCIe";
Lawrence Tang794312c2022-07-05 14:46:10 +0100175 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeInitGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100176 notification_type_readable = "INIT";
Lawrence Tang794312c2022-07-05 14:46:10 +0100177 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeNmiGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100178 notification_type_readable = "NMI";
Lawrence Tang794312c2022-07-05 14:46:10 +0100179 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeBootGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100180 notification_type_readable = "Boot";
Lawrence Tang794312c2022-07-05 14:46:10 +0100181 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeDmarGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100182 notification_type_readable = "DMAr";
Lawrence Tang794312c2022-07-05 14:46:10 +0100183 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeSeaGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100184 notification_type_readable = "SEA";
Lawrence Tang794312c2022-07-05 14:46:10 +0100185 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeSeiGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100186 notification_type_readable = "SEI";
Lawrence Tang794312c2022-07-05 14:46:10 +0100187 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypePeiGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100188 notification_type_readable = "PEI";
Lawrence Tang794312c2022-07-05 14:46:10 +0100189 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCxlGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100190 notification_type_readable = "CXL Component";
191 json_object_object_add(notification_type, "type", json_object_new_string(notification_type_readable));
192 json_object_object_add(header_ir, "notificationType", notification_type);
193
194 //The record ID for this record, unique on a given system.
195 json_object_object_add(header_ir, "recordID", json_object_new_uint64(header->RecordID));
196
197 //Flag for the record, and a human readable form.
Lawrence Tang3c43f742022-07-05 11:37:17 +0100198 json_object* flags = integer_to_readable_pair(header->Flags,
199 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
200 CPER_HEADER_FLAG_TYPES_KEYS,
201 CPER_HEADER_FLAG_TYPES_VALUES,
202 "Unknown");
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100203 json_object_object_add(header_ir, "flags", flags);
204
205 //Persistence information. Outside the scope of specification, so just a uint32 here.
206 json_object_object_add(header_ir, "persistenceInformation", json_object_new_uint64(header->PersistenceInfo));
207 return header_ir;
208}
209
210//Converts the given EFI section descriptor into JSON IR format.
211json_object* cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR* section_descriptor)
212{
213 json_object* section_descriptor_ir = json_object_new_object();
214
215 //The offset of the section from the base of the record header, length.
216 json_object_object_add(section_descriptor_ir, "sectionOffset", json_object_new_int(section_descriptor->SectionOffset));
217 json_object_object_add(section_descriptor_ir, "sectionLength", json_object_new_int(section_descriptor->SectionLength));
218
219 //Revision.
220 json_object_object_add(section_descriptor_ir, "revision", revision_to_ir(section_descriptor->Revision));
221
222 //Validation bits.
223 json_object* validation_bits = json_object_new_object();
224 json_object_object_add(validation_bits, "fruID", json_object_new_boolean(section_descriptor->SecValidMask & 0b1));
225 json_object_object_add(validation_bits, "fruString", json_object_new_boolean((section_descriptor->SecValidMask & 0b10) >> 1));
226 json_object_object_add(section_descriptor_ir, "validationBits", validation_bits);
227
228 //Flag bits.
Lawrence Tang2800cd82022-07-05 16:08:20 +0100229 json_object* flags = bitfield_to_ir(section_descriptor->SectionFlags, 8, CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100230 json_object_object_add(section_descriptor_ir, "flags", flags);
231
232 //Section type (GUID).
233 json_object* section_type = json_object_new_object();
234 char section_type_string[GUID_STRING_LENGTH];
235 guid_to_string(section_type_string, &section_descriptor->SectionType);
236 json_object_object_add(section_type, "data", json_object_new_string(section_type_string));
237
238 //Readable section type, if possible.
239 char* section_type_readable = "Unknown";
240 if (guid_equal(&section_descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
241 section_type_readable = "Processor Generic";
242 if (guid_equal(&section_descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
243 section_type_readable = "IA32/X64";
244 //todo: Why does IPF have an overly long GUID?
245 // if (guid_equal(&section_descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
246 // section_type_readable = "IPF";
247 if (guid_equal(&section_descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
248 section_type_readable = "ARM";
249 if (guid_equal(&section_descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
250 section_type_readable = "Platform Memory";
251 if (guid_equal(&section_descriptor->SectionType, &gEfiPcieErrorSectionGuid))
252 section_type_readable = "PCIe";
253 if (guid_equal(&section_descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
254 section_type_readable = "Firmware Error Record Reference";
255 if (guid_equal(&section_descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
256 section_type_readable = "PCI/PCI-X Bus";
257 if (guid_equal(&section_descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
258 section_type_readable = "PCI Component/Device";
259 if (guid_equal(&section_descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
260 section_type_readable = "DMAr Generic";
261 if (guid_equal(&section_descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
262 section_type_readable = "Intel VT for Directed I/O specific DMAr section";
263 if (guid_equal(&section_descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
264 section_type_readable = "IOMMU specific DMAr section";
265
266 //todo: How do you determine if this is a CXL component event?
267 // if (guid_equal(&section_descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
268 // section_type_readable = "CXL Component Event";
269
270 json_object_object_add(section_type, "type", json_object_new_string(section_type_readable));
271 json_object_object_add(section_descriptor_ir, "sectionType", section_type);
272
273 //If validation bits indicate it exists, add FRU ID.
274 if (section_descriptor->SecValidMask & 0b1)
275 {
276 char fru_id_string[GUID_STRING_LENGTH];
277 guid_to_string(fru_id_string, &section_descriptor->FruId);
278 json_object_object_add(section_descriptor_ir, "fruID", json_object_new_string(fru_id_string));
279 }
280
281 //If validation bits indicate it exists, add FRU text.
282 if ((section_descriptor->SecValidMask & 0b10) >> 1)
283 json_object_object_add(section_descriptor_ir, "fruText", json_object_new_string(section_descriptor->FruString));
284
285 //Section severity.
286 json_object* section_severity = json_object_new_object();
287 json_object_object_add(section_severity, "code", json_object_new_int(section_descriptor->Severity));
288 json_object_object_add(section_severity, "name", json_object_new_string(severity_to_string(section_descriptor->Severity)));
289 json_object_object_add(section_descriptor_ir, "severity", section_severity);
290
291 return section_descriptor_ir;
292}
293
294
295//Converts the section described by a single given section descriptor.
296json_object* cper_section_to_ir(FILE* handle, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
297{
298 //Read section as described by the section descriptor.
299 fseek(handle, descriptor->SectionOffset, SEEK_SET);
Lawrence Tang2800cd82022-07-05 16:08:20 +0100300 void* section = calloc(1, descriptor->SectionLength);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100301 if (fread(section, descriptor->SectionLength, 1, handle) != 1)
302 {
303 printf("Section read failed: Could not read %d bytes from global offset %d.",
304 descriptor->SectionLength,
305 descriptor->SectionOffset);
306 }
307
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +0100308 //Parse section to IR based on GUID.
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100309 json_object* result = NULL;
310 if (guid_equal(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100311 result = cper_section_generic_to_ir(section, descriptor);
312 else if (guid_equal(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
313 result = cper_section_ia32x64_to_ir(section, descriptor);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100314 // //todo: Why does IPF have an overly long GUID?
315 // // if (guid_equal(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100316 else if (guid_equal(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
317 result = cper_section_arm_to_ir(section, descriptor);
Lawrence Tanga0865e32022-07-06 11:59:52 +0100318 else if (guid_equal(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
319 result = cper_section_platform_memory_to_ir(section, descriptor);
Lawrence Tang54da4412022-07-06 13:14:58 +0100320 else if (guid_equal(&descriptor->SectionType, &gEfiPlatformMemoryError2SectionGuid))
321 result = cper_section_platform_memory2_to_ir(section, descriptor);
Lawrence Tang4dbe3d72022-07-06 13:51:01 +0100322 else if (guid_equal(&descriptor->SectionType, &gEfiPcieErrorSectionGuid))
323 result = cper_section_pcie_to_ir(section, descriptor);
Lawrence Tangc60a2432022-07-06 14:58:33 +0100324 else if (guid_equal(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
325 result = cper_section_firmware_to_ir(section, descriptor);
Lawrence Tang214a1542022-07-06 14:11:28 +0100326 else if (guid_equal(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
327 result = cper_section_pci_bus_to_ir(section, descriptor);
Lawrence Tanga416ec92022-07-06 14:34:40 +0100328 else if (guid_equal(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
329 result = cper_section_pci_dev_to_ir(section, descriptor);
Lawrence Tang4795d4a2022-07-06 15:19:31 +0100330 else if (guid_equal(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
331 result = cper_section_dmar_generic_to_ir(section, descriptor);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +0100332 else if (guid_equal(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
333 result = cper_section_dmar_vtd_to_ir(section, descriptor);
334 else if (guid_equal(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
335 result = cper_section_dmar_iommu_to_ir(section, descriptor);
336 else
337 {
338 //Failed read, unknown GUID.
339 //todo: dump the binary data out to b64.
340 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100341
342 //Free section memory, return result.
343 free(section);
344 return result;
345}