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