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