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 "base64.h" 11 #include "edk/Cper.h" 12 #include "cper-parse.h" 13 #include "cper-utils.h" 14 #include "sections/cper-section.h" 15 16 //Private pre-definitions. 17 json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header); 18 json_object * 19 cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor); 20 json_object *cper_section_to_ir(FILE *handle, long base_pos, 21 EFI_ERROR_SECTION_DESCRIPTOR *descriptor); 22 23 //Reads a CPER log file at the given file location, and returns an intermediate 24 //JSON representation of this CPER record. 25 json_object *cper_to_ir(FILE *cper_file) 26 { 27 //Read the current file pointer location as the base of the record. 28 long base_pos = ftell(cper_file); 29 30 //Ensure this is really a CPER log. 31 EFI_COMMON_ERROR_RECORD_HEADER header; 32 if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, 33 cper_file) != 1) { 34 printf("Invalid CPER file: Invalid length (log too short).\n"); 35 return NULL; 36 } 37 38 //Check if the header contains the magic bytes ("CPER"). 39 if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) { 40 printf("Invalid CPER file: Invalid header (incorrect signature).\n"); 41 return NULL; 42 } 43 44 //Create the header JSON object from the read bytes. 45 json_object *header_ir = cper_header_to_ir(&header); 46 47 //Read the appropriate number of section descriptors & sections, and convert them into IR format. 48 json_object *section_descriptors_ir = json_object_new_array(); 49 json_object *sections_ir = json_object_new_array(); 50 for (int i = 0; i < header.SectionCount; i++) { 51 //Create the section descriptor. 52 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor; 53 if (fread(§ion_descriptor, 54 sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, 55 cper_file) != 1) { 56 printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n", 57 header.SectionCount, i + 1); 58 // Free json objects 59 json_object_put(sections_ir); 60 json_object_put(section_descriptors_ir); 61 json_object_put(header_ir); 62 return NULL; 63 } 64 json_object_array_add( 65 section_descriptors_ir, 66 cper_section_descriptor_to_ir(§ion_descriptor)); 67 68 //Read the section itself. 69 json_object_array_add(sections_ir, 70 cper_section_to_ir(cper_file, base_pos, 71 §ion_descriptor)); 72 } 73 74 //Add the header, section descriptors, and sections to a parent object. 75 json_object *parent = json_object_new_object(); 76 json_object_object_add(parent, "header", header_ir); 77 json_object_object_add(parent, "sectionDescriptors", 78 section_descriptors_ir); 79 json_object_object_add(parent, "sections", sections_ir); 80 81 return parent; 82 } 83 84 //Converts a parsed CPER record header into intermediate JSON object format. 85 json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header) 86 { 87 json_object *header_ir = json_object_new_object(); 88 89 //Revision/version information. 90 json_object_object_add(header_ir, "revision", 91 revision_to_ir(header->Revision)); 92 93 //Section count. 94 json_object_object_add(header_ir, "sectionCount", 95 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", 100 json_object_new_uint64(header->ErrorSeverity)); 101 json_object_object_add(error_severity, "name", 102 json_object_new_string(severity_to_string( 103 header->ErrorSeverity))); 104 json_object_object_add(header_ir, "severity", error_severity); 105 106 //The validation bits for each section. 107 json_object *validation_bits = bitfield_to_ir( 108 header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES); 109 json_object_object_add(header_ir, "validationBits", validation_bits); 110 111 //Total length of the record (including headers) in bytes. 112 json_object_object_add(header_ir, "recordLength", 113 json_object_new_uint64(header->RecordLength)); 114 115 //If a timestamp exists according to validation bits, then add it. 116 if (header->ValidationBits & 0x2) { 117 char timestamp_string[TIMESTAMP_LENGTH]; 118 timestamp_to_string(timestamp_string, &header->TimeStamp); 119 120 json_object_object_add( 121 header_ir, "timestamp", 122 json_object_new_string(timestamp_string)); 123 json_object_object_add( 124 header_ir, "timestampIsPrecise", 125 json_object_new_boolean(header->TimeStamp.Flag)); 126 } 127 128 //If a platform ID exists according to the validation bits, then add it. 129 if (header->ValidationBits & 0x1) { 130 char platform_string[GUID_STRING_LENGTH]; 131 guid_to_string(platform_string, &header->PlatformID); 132 json_object_object_add(header_ir, "platformID", 133 json_object_new_string(platform_string)); 134 } 135 136 //If a partition ID exists according to the validation bits, then add it. 137 if (header->ValidationBits & 0x4) { 138 char partition_string[GUID_STRING_LENGTH]; 139 guid_to_string(partition_string, &header->PartitionID); 140 json_object_object_add( 141 header_ir, "partitionID", 142 json_object_new_string(partition_string)); 143 } 144 145 //Creator ID of the header. 146 char creator_string[GUID_STRING_LENGTH]; 147 guid_to_string(creator_string, &header->CreatorID); 148 json_object_object_add(header_ir, "creatorID", 149 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( 156 notification_type, "guid", 157 json_object_new_string(notification_type_string)); 158 159 //Add the human readable notification type if possible. 160 char *notification_type_readable = "Unknown"; 161 if (guid_equal(&header->NotificationType, 162 &gEfiEventNotificationTypeCmcGuid)) { 163 notification_type_readable = "CMC"; 164 } else if (guid_equal(&header->NotificationType, 165 &gEfiEventNotificationTypeCpeGuid)) { 166 notification_type_readable = "CPE"; 167 } else if (guid_equal(&header->NotificationType, 168 &gEfiEventNotificationTypeMceGuid)) { 169 notification_type_readable = "MCE"; 170 } else if (guid_equal(&header->NotificationType, 171 &gEfiEventNotificationTypePcieGuid)) { 172 notification_type_readable = "PCIe"; 173 } else if (guid_equal(&header->NotificationType, 174 &gEfiEventNotificationTypeInitGuid)) { 175 notification_type_readable = "INIT"; 176 } else if (guid_equal(&header->NotificationType, 177 &gEfiEventNotificationTypeNmiGuid)) { 178 notification_type_readable = "NMI"; 179 } else if (guid_equal(&header->NotificationType, 180 &gEfiEventNotificationTypeBootGuid)) { 181 notification_type_readable = "Boot"; 182 } else if (guid_equal(&header->NotificationType, 183 &gEfiEventNotificationTypeDmarGuid)) { 184 notification_type_readable = "DMAr"; 185 } else if (guid_equal(&header->NotificationType, 186 &gEfiEventNotificationTypeSeaGuid)) { 187 notification_type_readable = "SEA"; 188 } else if (guid_equal(&header->NotificationType, 189 &gEfiEventNotificationTypeSeiGuid)) { 190 notification_type_readable = "SEI"; 191 } else if (guid_equal(&header->NotificationType, 192 &gEfiEventNotificationTypePeiGuid)) { 193 notification_type_readable = "PEI"; 194 } else if (guid_equal(&header->NotificationType, 195 &gEfiEventNotificationTypeCxlGuid)) { 196 notification_type_readable = "CXL Component"; 197 } 198 json_object_object_add( 199 notification_type, "type", 200 json_object_new_string(notification_type_readable)); 201 json_object_object_add(header_ir, "notificationType", 202 notification_type); 203 204 //The record ID for this record, unique on a given system. 205 json_object_object_add(header_ir, "recordID", 206 json_object_new_uint64(header->RecordID)); 207 208 //Flag for the record, and a human readable form. 209 json_object *flags = integer_to_readable_pair( 210 header->Flags, 211 sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int), 212 CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES, 213 "Unknown"); 214 json_object_object_add(header_ir, "flags", flags); 215 216 //Persistence information. Outside the scope of specification, so just a uint32 here. 217 json_object_object_add(header_ir, "persistenceInfo", 218 json_object_new_uint64(header->PersistenceInfo)); 219 return header_ir; 220 } 221 222 //Converts the given EFI section descriptor into JSON IR format. 223 json_object * 224 cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor) 225 { 226 json_object *section_descriptor_ir = json_object_new_object(); 227 228 //The offset of the section from the base of the record header, length. 229 json_object_object_add( 230 section_descriptor_ir, "sectionOffset", 231 json_object_new_uint64(section_descriptor->SectionOffset)); 232 json_object_object_add( 233 section_descriptor_ir, "sectionLength", 234 json_object_new_uint64(section_descriptor->SectionLength)); 235 236 //Revision. 237 json_object_object_add(section_descriptor_ir, "revision", 238 revision_to_ir(section_descriptor->Revision)); 239 240 //Validation bits. 241 json_object *validation_bits = 242 bitfield_to_ir(section_descriptor->SecValidMask, 2, 243 CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES); 244 json_object_object_add(section_descriptor_ir, "validationBits", 245 validation_bits); 246 247 //Flag bits. 248 json_object *flags = 249 bitfield_to_ir(section_descriptor->SectionFlags, 8, 250 CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES); 251 json_object_object_add(section_descriptor_ir, "flags", flags); 252 253 //Section type (GUID). 254 json_object *section_type = json_object_new_object(); 255 char section_type_string[GUID_STRING_LENGTH]; 256 guid_to_string(section_type_string, §ion_descriptor->SectionType); 257 json_object_object_add(section_type, "data", 258 json_object_new_string(section_type_string)); 259 260 //Readable section type, if possible. 261 const char *section_type_readable = "Unknown"; 262 for (size_t i = 0; i < section_definitions_len; i++) { 263 if (guid_equal(section_definitions[i].Guid, 264 §ion_descriptor->SectionType)) { 265 section_type_readable = 266 section_definitions[i].ReadableName; 267 break; 268 } 269 } 270 271 json_object_object_add(section_type, "type", 272 json_object_new_string(section_type_readable)); 273 json_object_object_add(section_descriptor_ir, "sectionType", 274 section_type); 275 276 //If validation bits indicate it exists, add FRU ID. 277 if (section_descriptor->SecValidMask & 0x1) { 278 char fru_id_string[GUID_STRING_LENGTH]; 279 guid_to_string(fru_id_string, §ion_descriptor->FruId); 280 json_object_object_add(section_descriptor_ir, "fruID", 281 json_object_new_string(fru_id_string)); 282 } 283 284 //If validation bits indicate it exists, add FRU text. 285 if ((section_descriptor->SecValidMask & 0x2) >> 1) { 286 json_object_object_add( 287 section_descriptor_ir, "fruText", 288 json_object_new_string(section_descriptor->FruString)); 289 } 290 291 //Section severity. 292 json_object *section_severity = json_object_new_object(); 293 json_object_object_add( 294 section_severity, "code", 295 json_object_new_uint64(section_descriptor->Severity)); 296 json_object_object_add(section_severity, "name", 297 json_object_new_string(severity_to_string( 298 section_descriptor->Severity))); 299 json_object_object_add(section_descriptor_ir, "severity", 300 section_severity); 301 302 return section_descriptor_ir; 303 } 304 305 //Converts the section described by a single given section descriptor. 306 json_object *cper_section_to_ir(FILE *handle, long base_pos, 307 EFI_ERROR_SECTION_DESCRIPTOR *descriptor) 308 { 309 //Save our current position in the stream. 310 long position = ftell(handle); 311 312 //Read section as described by the section descriptor. 313 fseek(handle, base_pos + descriptor->SectionOffset, SEEK_SET); 314 void *section = malloc(descriptor->SectionLength); 315 if (fread(section, descriptor->SectionLength, 1, handle) != 1) { 316 printf("Section read failed: Could not read %u bytes from global offset %d.\n", 317 descriptor->SectionLength, descriptor->SectionOffset); 318 free(section); 319 return NULL; 320 } 321 322 //Seek back to our original position. 323 fseek(handle, position, SEEK_SET); 324 325 //Parse section to IR based on GUID. 326 json_object *result = NULL; 327 int section_converted = 0; 328 for (size_t i = 0; i < section_definitions_len; i++) { 329 if (guid_equal(section_definitions[i].Guid, 330 &descriptor->SectionType) && 331 section_definitions[i].ToIR != NULL) { 332 result = section_definitions[i].ToIR(section); 333 section_converted = 1; 334 break; 335 } 336 } 337 338 //Was it an unknown GUID/failed read? 339 if (!section_converted) { 340 //Output the data as formatted base64. 341 result = json_object_new_object(); 342 343 int32_t encoded_len = 0; 344 char *encoded = base64_encode( 345 section, descriptor->SectionLength, &encoded_len); 346 if (encoded == NULL) { 347 printf("Failed to allocate encode output buffer. \n"); 348 } else { 349 json_object_object_add(result, "data", 350 json_object_new_string_len( 351 encoded, encoded_len)); 352 free(encoded); 353 } 354 } 355 //Free section memory, return result. 356 free(section); 357 return result; 358 } 359 360 //Converts a single CPER section, without a header but with a section descriptor, to JSON. 361 json_object *cper_single_section_to_ir(FILE *cper_section_file) 362 { 363 json_object *ir = json_object_new_object(); 364 365 //Read the current file pointer location as base record position. 366 long base_pos = ftell(cper_section_file); 367 368 //Read the section descriptor out. 369 EFI_ERROR_SECTION_DESCRIPTOR section_descriptor; 370 if (fread(§ion_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, 371 cper_section_file) != 1) { 372 printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n"); 373 return NULL; 374 } 375 376 //Convert the section descriptor to IR. 377 json_object *section_descriptor_ir = 378 cper_section_descriptor_to_ir(§ion_descriptor); 379 json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir); 380 381 //Parse the single section. 382 json_object *section_ir = cper_section_to_ir( 383 cper_section_file, base_pos, §ion_descriptor); 384 json_object_object_add(ir, "section", section_ir); 385 386 return ir; 387 } 388