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