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