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