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