Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for parsing JSON IR CPER data into binary CPER format. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdio.h> |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 8 | #include <string.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 9 | #include <json.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 10 | #include <libcper/base64.h> |
| 11 | #include <libcper/Cper.h> |
| 12 | #include <libcper/cper-parse.h> |
| 13 | #include <libcper/cper-utils.h> |
| 14 | #include <libcper/sections/cper-section.h> |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 15 | |
| 16 | //Private pre-declarations. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 17 | void ir_header_to_cper(json_object *header_ir, |
| 18 | EFI_COMMON_ERROR_RECORD_HEADER *header); |
| 19 | void ir_section_descriptor_to_cper(json_object *section_descriptor_ir, |
| 20 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 21 | void ir_section_to_cper(json_object *section, |
| 22 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out); |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 23 | |
| 24 | //Converts the given JSON IR CPER representation into CPER binary format, piped to the provided file stream. |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 25 | //This function performs no validation of the IR against the CPER-JSON specification. To ensure a safe call, |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 26 | //use validate_schema() from json-schema.h before attempting to call this function. |
| 27 | void ir_to_cper(json_object *ir, FILE *out) |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 28 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 29 | //Create the CPER header. |
| 30 | EFI_COMMON_ERROR_RECORD_HEADER *header = |
| 31 | (EFI_COMMON_ERROR_RECORD_HEADER *)calloc( |
| 32 | 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER)); |
| 33 | ir_header_to_cper(json_object_object_get(ir, "header"), header); |
| 34 | fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out); |
| 35 | fflush(out); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 36 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 37 | //Create the CPER section descriptors. |
| 38 | json_object *section_descriptors = |
| 39 | json_object_object_get(ir, "sectionDescriptors"); |
| 40 | int amt_descriptors = json_object_array_length(section_descriptors); |
| 41 | EFI_ERROR_SECTION_DESCRIPTOR *descriptors[amt_descriptors]; |
| 42 | for (int i = 0; i < amt_descriptors; i++) { |
| 43 | descriptors[i] = (EFI_ERROR_SECTION_DESCRIPTOR *)calloc( |
| 44 | 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
| 45 | ir_section_descriptor_to_cper( |
| 46 | json_object_array_get_idx(section_descriptors, i), |
| 47 | descriptors[i]); |
| 48 | fwrite(descriptors[i], sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, |
| 49 | out); |
| 50 | fflush(out); |
| 51 | } |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 52 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 53 | //Run through each section in turn. |
| 54 | json_object *sections = json_object_object_get(ir, "sections"); |
| 55 | int amt_sections = json_object_array_length(sections); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 56 | if (amt_sections == amt_descriptors) { |
| 57 | for (int i = 0; i < amt_sections; i++) { |
| 58 | //Get the section itself from the IR. |
| 59 | json_object *section = |
| 60 | json_object_array_get_idx(sections, i); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 61 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 62 | //Convert. |
| 63 | ir_section_to_cper(section, descriptors[i], out); |
| 64 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | //Free all remaining resources. |
| 68 | free(header); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 69 | for (int i = 0; i < amt_descriptors; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 70 | free(descriptors[i]); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 71 | } |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | //Converts a CPER-JSON IR header to a CPER header structure. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 75 | void ir_header_to_cper(json_object *header_ir, |
| 76 | EFI_COMMON_ERROR_RECORD_HEADER *header) |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 77 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 78 | header->SignatureStart = 0x52455043; //CPER |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 79 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 80 | //Revision. |
| 81 | json_object *revision = json_object_object_get(header_ir, "revision"); |
| 82 | int minor = |
| 83 | json_object_get_int(json_object_object_get(revision, "minor")); |
| 84 | int major = |
| 85 | json_object_get_int(json_object_object_get(revision, "major")); |
| 86 | header->Revision = minor + (major << 8); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 87 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 88 | header->SignatureEnd = 0xFFFFFFFF; |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 89 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 90 | //Section count. |
| 91 | int section_count = json_object_get_int( |
| 92 | json_object_object_get(header_ir, "sectionCount")); |
| 93 | header->SectionCount = (UINT16)section_count; |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 94 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 95 | //Error severity. |
| 96 | json_object *severity = json_object_object_get(header_ir, "severity"); |
| 97 | header->ErrorSeverity = (UINT32)json_object_get_uint64( |
| 98 | json_object_object_get(severity, "code")); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 99 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 100 | //Validation bits. |
| 101 | header->ValidationBits = ir_to_bitfield( |
| 102 | json_object_object_get(header_ir, "validationBits"), 3, |
| 103 | CPER_HEADER_VALID_BITFIELD_NAMES); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 104 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 105 | //Record length. |
| 106 | header->RecordLength = (UINT32)json_object_get_uint64( |
| 107 | json_object_object_get(header_ir, "recordLength")); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 108 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 109 | //Timestamp, if present. |
| 110 | json_object *timestamp = json_object_object_get(header_ir, "timestamp"); |
| 111 | if (timestamp != NULL) { |
| 112 | string_to_timestamp(&header->TimeStamp, |
| 113 | json_object_get_string(timestamp)); |
| 114 | header->TimeStamp.Flag = json_object_get_boolean( |
| 115 | json_object_object_get(header_ir, |
| 116 | "timestampIsPrecise")); |
| 117 | } |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 118 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 119 | //Various GUIDs. |
| 120 | json_object *platform_id = |
| 121 | json_object_object_get(header_ir, "platformID"); |
| 122 | json_object *partition_id = |
| 123 | json_object_object_get(header_ir, "partitionID"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 124 | if (platform_id != NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 125 | string_to_guid(&header->PlatformID, |
| 126 | json_object_get_string(platform_id)); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 127 | } |
| 128 | if (partition_id != NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 129 | string_to_guid(&header->PartitionID, |
| 130 | json_object_get_string(partition_id)); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 131 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 132 | string_to_guid(&header->CreatorID, |
| 133 | json_object_get_string( |
| 134 | json_object_object_get(header_ir, "creatorID"))); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 135 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 136 | //Notification type. |
| 137 | json_object *notification_type = |
| 138 | json_object_object_get(header_ir, "notificationType"); |
| 139 | string_to_guid(&header->NotificationType, |
| 140 | json_object_get_string(json_object_object_get( |
| 141 | notification_type, "guid"))); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 142 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 143 | //Record ID, persistence info. |
| 144 | header->RecordID = json_object_get_uint64( |
| 145 | json_object_object_get(header_ir, "recordID")); |
| 146 | header->PersistenceInfo = json_object_get_uint64( |
| 147 | json_object_object_get(header_ir, "persistenceInfo")); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 148 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 149 | //Flags. |
| 150 | json_object *flags = json_object_object_get(header_ir, "flags"); |
| 151 | header->Flags = (UINT32)json_object_get_uint64( |
| 152 | json_object_object_get(flags, "value")); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 155 | //Converts a single given IR section into CPER, outputting to the given stream. |
| 156 | void ir_section_to_cper(json_object *section, |
| 157 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out) |
| 158 | { |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 159 | json_object *ir = NULL; |
| 160 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 161 | //Find the correct section type, and parse. |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 162 | int section_converted = 0; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 163 | for (size_t i = 0; i < section_definitions_len; i++) { |
Lawrence Tang | f1f3b83 | 2022-08-24 09:42:39 +0100 | [diff] [blame] | 164 | if (guid_equal(section_definitions[i].Guid, |
| 165 | &descriptor->SectionType) && |
| 166 | section_definitions[i].ToCPER != NULL) { |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 167 | ir = json_object_object_get( |
| 168 | section, section_definitions[i].ShortName); |
| 169 | section_definitions[i].ToCPER(ir, out); |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 170 | section_converted = 1; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | //If unknown GUID, so read as a base64 unknown section. |
| 176 | if (!section_converted) { |
Ed Tanous | b07061a | 2024-09-22 10:33:29 -0700 | [diff] [blame] | 177 | ir = json_object_object_get(section, "Unknown"); |
| 178 | json_object *encoded = json_object_object_get(ir, "data"); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 179 | |
| 180 | int32_t decoded_len = 0; |
| 181 | |
| 182 | UINT8 *decoded = base64_decode( |
| 183 | json_object_get_string(encoded), |
| 184 | json_object_get_string_len(encoded), &decoded_len); |
| 185 | if (decoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 186 | printf("Failed to allocate decode output buffer. \n"); |
| 187 | } else { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 188 | fwrite(decoded, decoded_len, 1, out); |
| 189 | fflush(out); |
| 190 | free(decoded); |
| 191 | } |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 195 | //Converts a single CPER-JSON IR section descriptor into a CPER structure. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 196 | void ir_section_descriptor_to_cper(json_object *section_descriptor_ir, |
| 197 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor) |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 198 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 199 | //Section offset, length. |
| 200 | descriptor->SectionOffset = (UINT32)json_object_get_uint64( |
| 201 | json_object_object_get(section_descriptor_ir, "sectionOffset")); |
| 202 | descriptor->SectionLength = (UINT32)json_object_get_uint64( |
| 203 | json_object_object_get(section_descriptor_ir, "sectionLength")); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 204 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 205 | //Revision. |
| 206 | json_object *revision = |
| 207 | json_object_object_get(section_descriptor_ir, "revision"); |
| 208 | int minor = |
| 209 | json_object_get_int(json_object_object_get(revision, "minor")); |
| 210 | int major = |
| 211 | json_object_get_int(json_object_object_get(revision, "major")); |
| 212 | descriptor->Revision = minor + (major << 8); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 213 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 214 | //Validation bits, flags. |
| 215 | descriptor->SecValidMask = ir_to_bitfield( |
| 216 | json_object_object_get(section_descriptor_ir, "validationBits"), |
| 217 | 2, CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES); |
| 218 | descriptor->SectionFlags = ir_to_bitfield( |
| 219 | json_object_object_get(section_descriptor_ir, "flags"), 8, |
| 220 | CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 221 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 222 | //Section type. |
| 223 | json_object *section_type = |
| 224 | json_object_object_get(section_descriptor_ir, "sectionType"); |
| 225 | string_to_guid(&descriptor->SectionType, |
| 226 | json_object_get_string( |
| 227 | json_object_object_get(section_type, "data"))); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 228 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 229 | //FRU ID, if present. |
| 230 | json_object *fru_id = |
| 231 | json_object_object_get(section_descriptor_ir, "fruID"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 232 | if (fru_id != NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 233 | string_to_guid(&descriptor->FruId, |
| 234 | json_object_get_string(fru_id)); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 235 | } |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 236 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 237 | //Severity code. |
| 238 | json_object *severity = |
| 239 | json_object_object_get(section_descriptor_ir, "severity"); |
| 240 | descriptor->Severity = (UINT32)json_object_get_uint64( |
| 241 | json_object_object_get(severity, "code")); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 242 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 243 | //FRU text, if present. |
| 244 | json_object *fru_text = |
| 245 | json_object_object_get(section_descriptor_ir, "fruText"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 246 | if (fru_text != NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 247 | strncpy(descriptor->FruString, json_object_get_string(fru_text), |
Patrick Williams | 379e492 | 2024-08-28 11:14:34 -0400 | [diff] [blame] | 248 | sizeof(descriptor->FruString) - 1); |
| 249 | descriptor->FruString[sizeof(descriptor->FruString) - 1] = '\0'; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 250 | } |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | //Converts IR for a given single section format CPER record into CPER binary. |
| 254 | void ir_single_section_to_cper(json_object *ir, FILE *out) |
| 255 | { |
| 256 | //Create & write a section descriptor to file. |
| 257 | EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor = |
| 258 | (EFI_ERROR_SECTION_DESCRIPTOR *)calloc( |
| 259 | 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
| 260 | ir_section_descriptor_to_cper( |
| 261 | json_object_object_get(ir, "sectionDescriptor"), |
| 262 | section_descriptor); |
| 263 | fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, |
| 264 | out); |
| 265 | fflush(out); |
| 266 | |
| 267 | //Write section to file. |
| 268 | ir_section_to_cper(json_object_object_get(ir, "section"), |
| 269 | section_descriptor, out); |
| 270 | |
| 271 | //Free remaining resources. |
| 272 | free(section_descriptor); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 273 | } |