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