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