blob: bf34c671b9bf6eb8b257ebf425a6f00865e7ef26 [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 Tang1b0b00e2022-07-05 10:33:10 +010016
17//Private pre-definitions.
18json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header);
19json_object* cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR* section_descriptor);
20json_object* cper_section_to_ir(FILE* handle, EFI_ERROR_SECTION_DESCRIPTOR* descriptor);
21
22//Reads a CPER log file at the given file location, and returns an intermediate
23//JSON representation of this CPER record.
24json_object* cper_to_ir(const char* filename)
25{
26 //Get a handle for the log file.
27 FILE* cper_file = fopen(filename, "r");
28 if (cper_file == NULL) {
29 printf("Could not open CPER record, file handle returned null.");
30 return NULL;
31 }
32
33 //Ensure this is really a CPER log.
34 EFI_COMMON_ERROR_RECORD_HEADER header;
35 fseek(cper_file, 0, SEEK_SET);
36 if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, cper_file) != 1)
37 {
38 printf("Invalid CPER file: Invalid length (log too short).");
39 return NULL;
40 }
41
42 //Check if the header contains the magic bytes ("CPER").
43 if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
44 printf("Invalid CPER file: Invalid header (incorrect signature).");
45 return NULL;
46 }
47
48 // //Print struct contents (debug).
49 // fpos_t file_pos;
50 // fgetpos(cper_file, &file_pos);
51 // printf("Stream is at position %d.\n", file_pos);
52 // printf("SignatureStart: %s\n", (char*)&header.SignatureStart);
53 // printf("Revision: %u\n", header.Revision);
54 // printf("SectionCount: %u\n", header.SectionCount);
55 // printf("Severity: %d\n", header.ErrorSeverity);
56 // printf("RecordLength: %d\n", header.RecordLength);
57
58 //Create the header JSON object from the read bytes.
59 json_object* header_ir = cper_header_to_ir(&header);
60
61 //Read the appropriate number of section descriptors & sections, and convert them into IR format.
62 json_object* section_descriptors_ir = json_object_new_array();
63 json_object* sections_ir = json_object_new_array();
64 for (int i=0; i<header.SectionCount; i++)
65 {
66 //Create the section descriptor.
67 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
68 if (fread(&section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, cper_file) != 1)
69 {
70 printf("Invalid number of section headers: Header states %d sections, could not read section %d.", header.SectionCount, i+1);
71 return NULL;
72 }
73 json_object_array_add(section_descriptors_ir, cper_section_descriptor_to_ir(&section_descriptor));
74
75 //Read the section itself.
76 json_object_array_add(sections_ir, cper_section_to_ir(cper_file, &section_descriptor));
77 }
78
79 //Add the header, section descriptors, and sections to a parent object.
80 json_object* parent = json_object_new_object();
81 json_object_object_add(parent, "header", header_ir);
82 json_object_object_add(parent, "sectionDescriptors", section_descriptors_ir);
Lawrence Tang2800cd82022-07-05 16:08:20 +010083 json_object_object_add(parent, "sections", sections_ir);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010084
85 //...
86 return parent;
87}
88
89//Converts a parsed CPER record header into intermediate JSON object format.
90json_object* cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER* header)
91{
92 json_object* header_ir = json_object_new_object();
93
94 //Revision/version information.
95 json_object_object_add(header_ir, "revision", revision_to_ir(header->Revision));
96
97 //Section count.
98 json_object_object_add(header_ir, "sectionCount", json_object_new_int(header->SectionCount));
99
100 //Error severity (with interpreted string version).
101 json_object* error_severity = json_object_new_object();
102 json_object_object_add(error_severity, "code", json_object_new_int(header->ErrorSeverity));
103 json_object_object_add(error_severity, "name", json_object_new_string(severity_to_string(header->ErrorSeverity)));
104 json_object_object_add(header_ir, "severity", error_severity);
105
106 //The validation bits for each section.
Lawrence Tang2800cd82022-07-05 16:08:20 +0100107 json_object* validation_bits = bitfield_to_ir(header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100108 json_object_object_add(header_ir, "validationBits", validation_bits);
109
110 //Total length of the record (including headers) in bytes.
111 json_object_object_add(header_ir, "recordLength", json_object_new_int(header->RecordLength));
112
113 //If a timestamp exists according to validation bits, then add it.
114 if (header->ValidationBits & 0b10)
115 {
116 char timestamp_string[TIMESTAMP_LENGTH];
117 sprintf(timestamp_string, "%02d%02d-%02d-%02dT%02d:%02d:%02d.000",
118 header->TimeStamp.Century,
119 header->TimeStamp.Year,
120 header->TimeStamp.Month,
121 header->TimeStamp.Day,
122 header->TimeStamp.Hours,
123 header->TimeStamp.Minutes,
124 header->TimeStamp.Seconds);
125
126 json_object_object_add(header_ir, "timestamp", json_object_new_string(timestamp_string));
127 json_object_object_add(header_ir, "timestampIsPrecise", json_object_new_boolean(header->TimeStamp.Flag));
128 }
129
130 //If a platform ID exists according to the validation bits, then add it.
131 if (header->ValidationBits & 0b1)
132 {
133 char platform_string[GUID_STRING_LENGTH];
134 guid_to_string(platform_string, &header->PlatformID);
135 json_object_object_add(header_ir, "platformID", json_object_new_string(platform_string));
136 }
137
138 //If a partition ID exists according to the validation bits, then add it.
139 if (header->ValidationBits & 0b100)
140 {
141 char partition_string[GUID_STRING_LENGTH];
142 guid_to_string(partition_string, &header->PartitionID);
143 json_object_object_add(header_ir, "partitionID", json_object_new_string(partition_string));
144 }
145
146 //Creator ID of the header.
147 char creator_string[GUID_STRING_LENGTH];
148 guid_to_string(creator_string, &header->CreatorID);
149 json_object_object_add(header_ir, "creatorID", json_object_new_string(creator_string));
150
151 //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(notification_type, "guid", json_object_new_string(notification_type_string));
156
157 //Add the human readable notification type if possible.
158 char* notification_type_readable = "Unknown";
159 if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCmcGuid))
160 notification_type_readable = "CMC";
Lawrence Tang794312c2022-07-05 14:46:10 +0100161 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCpeGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100162 notification_type_readable = "CPE";
Lawrence Tang794312c2022-07-05 14:46:10 +0100163 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeMceGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100164 notification_type_readable = "MCE";
Lawrence Tang794312c2022-07-05 14:46:10 +0100165 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypePcieGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100166 notification_type_readable = "PCIe";
Lawrence Tang794312c2022-07-05 14:46:10 +0100167 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeInitGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100168 notification_type_readable = "INIT";
Lawrence Tang794312c2022-07-05 14:46:10 +0100169 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeNmiGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100170 notification_type_readable = "NMI";
Lawrence Tang794312c2022-07-05 14:46:10 +0100171 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeBootGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100172 notification_type_readable = "Boot";
Lawrence Tang794312c2022-07-05 14:46:10 +0100173 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeDmarGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100174 notification_type_readable = "DMAr";
Lawrence Tang794312c2022-07-05 14:46:10 +0100175 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeSeaGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100176 notification_type_readable = "SEA";
Lawrence Tang794312c2022-07-05 14:46:10 +0100177 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeSeiGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100178 notification_type_readable = "SEI";
Lawrence Tang794312c2022-07-05 14:46:10 +0100179 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypePeiGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100180 notification_type_readable = "PEI";
Lawrence Tang794312c2022-07-05 14:46:10 +0100181 else if (guid_equal(&header->NotificationType, &gEfiEventNotificationTypeCxlGuid))
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100182 notification_type_readable = "CXL Component";
183 json_object_object_add(notification_type, "type", json_object_new_string(notification_type_readable));
184 json_object_object_add(header_ir, "notificationType", notification_type);
185
186 //The record ID for this record, unique on a given system.
187 json_object_object_add(header_ir, "recordID", json_object_new_uint64(header->RecordID));
188
189 //Flag for the record, and a human readable form.
Lawrence Tang3c43f742022-07-05 11:37:17 +0100190 json_object* flags = integer_to_readable_pair(header->Flags,
191 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
192 CPER_HEADER_FLAG_TYPES_KEYS,
193 CPER_HEADER_FLAG_TYPES_VALUES,
194 "Unknown");
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100195 json_object_object_add(header_ir, "flags", flags);
196
197 //Persistence information. Outside the scope of specification, so just a uint32 here.
198 json_object_object_add(header_ir, "persistenceInformation", json_object_new_uint64(header->PersistenceInfo));
199 return header_ir;
200}
201
202//Converts the given EFI section descriptor into JSON IR format.
203json_object* cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR* section_descriptor)
204{
205 json_object* section_descriptor_ir = json_object_new_object();
206
207 //The offset of the section from the base of the record header, length.
208 json_object_object_add(section_descriptor_ir, "sectionOffset", json_object_new_int(section_descriptor->SectionOffset));
209 json_object_object_add(section_descriptor_ir, "sectionLength", json_object_new_int(section_descriptor->SectionLength));
210
211 //Revision.
212 json_object_object_add(section_descriptor_ir, "revision", revision_to_ir(section_descriptor->Revision));
213
214 //Validation bits.
215 json_object* validation_bits = json_object_new_object();
216 json_object_object_add(validation_bits, "fruID", json_object_new_boolean(section_descriptor->SecValidMask & 0b1));
217 json_object_object_add(validation_bits, "fruString", json_object_new_boolean((section_descriptor->SecValidMask & 0b10) >> 1));
218 json_object_object_add(section_descriptor_ir, "validationBits", validation_bits);
219
220 //Flag bits.
Lawrence Tang2800cd82022-07-05 16:08:20 +0100221 json_object* flags = bitfield_to_ir(section_descriptor->SectionFlags, 8, CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100222 json_object_object_add(section_descriptor_ir, "flags", flags);
223
224 //Section type (GUID).
225 json_object* section_type = json_object_new_object();
226 char section_type_string[GUID_STRING_LENGTH];
227 guid_to_string(section_type_string, &section_descriptor->SectionType);
228 json_object_object_add(section_type, "data", json_object_new_string(section_type_string));
229
230 //Readable section type, if possible.
231 char* section_type_readable = "Unknown";
232 if (guid_equal(&section_descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
233 section_type_readable = "Processor Generic";
234 if (guid_equal(&section_descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
235 section_type_readable = "IA32/X64";
236 //todo: Why does IPF have an overly long GUID?
237 // if (guid_equal(&section_descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
238 // section_type_readable = "IPF";
239 if (guid_equal(&section_descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
240 section_type_readable = "ARM";
241 if (guid_equal(&section_descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
242 section_type_readable = "Platform Memory";
243 if (guid_equal(&section_descriptor->SectionType, &gEfiPcieErrorSectionGuid))
244 section_type_readable = "PCIe";
245 if (guid_equal(&section_descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
246 section_type_readable = "Firmware Error Record Reference";
247 if (guid_equal(&section_descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
248 section_type_readable = "PCI/PCI-X Bus";
249 if (guid_equal(&section_descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
250 section_type_readable = "PCI Component/Device";
251 if (guid_equal(&section_descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
252 section_type_readable = "DMAr Generic";
253 if (guid_equal(&section_descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
254 section_type_readable = "Intel VT for Directed I/O specific DMAr section";
255 if (guid_equal(&section_descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
256 section_type_readable = "IOMMU specific DMAr section";
257
258 //todo: How do you determine if this is a CXL component event?
259 // if (guid_equal(&section_descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
260 // section_type_readable = "CXL Component Event";
261
262 json_object_object_add(section_type, "type", json_object_new_string(section_type_readable));
263 json_object_object_add(section_descriptor_ir, "sectionType", section_type);
264
265 //If validation bits indicate it exists, add FRU ID.
266 if (section_descriptor->SecValidMask & 0b1)
267 {
268 char fru_id_string[GUID_STRING_LENGTH];
269 guid_to_string(fru_id_string, &section_descriptor->FruId);
270 json_object_object_add(section_descriptor_ir, "fruID", json_object_new_string(fru_id_string));
271 }
272
273 //If validation bits indicate it exists, add FRU text.
274 if ((section_descriptor->SecValidMask & 0b10) >> 1)
275 json_object_object_add(section_descriptor_ir, "fruText", json_object_new_string(section_descriptor->FruString));
276
277 //Section severity.
278 json_object* section_severity = json_object_new_object();
279 json_object_object_add(section_severity, "code", json_object_new_int(section_descriptor->Severity));
280 json_object_object_add(section_severity, "name", json_object_new_string(severity_to_string(section_descriptor->Severity)));
281 json_object_object_add(section_descriptor_ir, "severity", section_severity);
282
283 return section_descriptor_ir;
284}
285
286
287//Converts the section described by a single given section descriptor.
288json_object* cper_section_to_ir(FILE* handle, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
289{
290 //Read section as described by the section descriptor.
291 fseek(handle, descriptor->SectionOffset, SEEK_SET);
Lawrence Tang2800cd82022-07-05 16:08:20 +0100292 void* section = calloc(1, descriptor->SectionLength);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100293 if (fread(section, descriptor->SectionLength, 1, handle) != 1)
294 {
295 printf("Section read failed: Could not read %d bytes from global offset %d.",
296 descriptor->SectionLength,
297 descriptor->SectionOffset);
298 }
299
300 json_object* result = NULL;
301 if (guid_equal(&descriptor->SectionType, &gEfiProcessorGenericErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100302 result = cper_section_generic_to_ir(section, descriptor);
303 else if (guid_equal(&descriptor->SectionType, &gEfiIa32X64ProcessorErrorSectionGuid))
304 result = cper_section_ia32x64_to_ir(section, descriptor);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100305 // //todo: Why does IPF have an overly long GUID?
306 // // if (guid_equal(&descriptor->SectionType, &gEfiIpfProcessorErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100307 else if (guid_equal(&descriptor->SectionType, &gEfiArmProcessorErrorSectionGuid))
308 result = cper_section_arm_to_ir(section, descriptor);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100309 // if (guid_equal(&descriptor->SectionType, &gEfiPlatformMemoryErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100310 // result = cper_section_platform_memory_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100311 // if (guid_equal(&descriptor->SectionType, &gEfiPcieErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100312 // result = cper_section_pcie_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100313 // if (guid_equal(&descriptor->SectionType, &gEfiFirmwareErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100314 // result = cper_section_firmware_error_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100315 // if (guid_equal(&descriptor->SectionType, &gEfiPciBusErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100316 // result = cper_section_pci_bus_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100317 // if (guid_equal(&descriptor->SectionType, &gEfiPciDevErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100318 // result = cper_section_pci_dev_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100319 // if (guid_equal(&descriptor->SectionType, &gEfiDMArGenericErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100320 // result = cper_section_dmar_generic_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100321 // if (guid_equal(&descriptor->SectionType, &gEfiDirectedIoDMArErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100322 // result = cper_section_intel_io_dma_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100323 // if (guid_equal(&descriptor->SectionType, &gEfiIommuDMArErrorSectionGuid))
Lawrence Tang2800cd82022-07-05 16:08:20 +0100324 // result = cper_section_iommu_dma_to_ir(section);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100325
326 //Free section memory, return result.
327 free(section);
328 return result;
329}