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