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