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