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