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