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