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