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