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. |
| 3 | * |
| 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> |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 10 | #include "b64.h" |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 11 | #include "edk/Cper.h" |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 12 | #include "cper-parse.h" |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 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 | 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); |
| 56 | for (int i = 0; i < amt_sections; i++) { |
| 57 | //Get the section itself from the IR. |
| 58 | json_object *section = json_object_array_get_idx(sections, i); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 59 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 60 | //Convert. |
| 61 | ir_section_to_cper(section, descriptors[i], out); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | //Free all remaining resources. |
| 65 | free(header); |
| 66 | for (int i = 0; i < amt_descriptors; i++) |
| 67 | free(descriptors[i]); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | //Converts a CPER-JSON IR header to a CPER header structure. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 71 | void ir_header_to_cper(json_object *header_ir, |
| 72 | EFI_COMMON_ERROR_RECORD_HEADER *header) |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 73 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 74 | header->SignatureStart = 0x52455043; //CPER |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 75 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 76 | //Revision. |
| 77 | json_object *revision = json_object_object_get(header_ir, "revision"); |
| 78 | int minor = |
| 79 | json_object_get_int(json_object_object_get(revision, "minor")); |
| 80 | int major = |
| 81 | json_object_get_int(json_object_object_get(revision, "major")); |
| 82 | header->Revision = minor + (major << 8); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 83 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 84 | header->SignatureEnd = 0xFFFFFFFF; |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 85 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 86 | //Section count. |
| 87 | int section_count = json_object_get_int( |
| 88 | json_object_object_get(header_ir, "sectionCount")); |
| 89 | header->SectionCount = (UINT16)section_count; |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 90 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 91 | //Error severity. |
| 92 | json_object *severity = json_object_object_get(header_ir, "severity"); |
| 93 | header->ErrorSeverity = (UINT32)json_object_get_uint64( |
| 94 | json_object_object_get(severity, "code")); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 95 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 96 | //Validation bits. |
| 97 | header->ValidationBits = ir_to_bitfield( |
| 98 | json_object_object_get(header_ir, "validationBits"), 3, |
| 99 | CPER_HEADER_VALID_BITFIELD_NAMES); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 100 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 101 | //Record length. |
| 102 | header->RecordLength = (UINT32)json_object_get_uint64( |
| 103 | json_object_object_get(header_ir, "recordLength")); |
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 | //Timestamp, if present. |
| 106 | json_object *timestamp = json_object_object_get(header_ir, "timestamp"); |
| 107 | if (timestamp != NULL) { |
| 108 | string_to_timestamp(&header->TimeStamp, |
| 109 | json_object_get_string(timestamp)); |
| 110 | header->TimeStamp.Flag = json_object_get_boolean( |
| 111 | json_object_object_get(header_ir, |
| 112 | "timestampIsPrecise")); |
| 113 | } |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 114 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 115 | //Various GUIDs. |
| 116 | json_object *platform_id = |
| 117 | json_object_object_get(header_ir, "platformID"); |
| 118 | json_object *partition_id = |
| 119 | json_object_object_get(header_ir, "partitionID"); |
| 120 | if (platform_id != NULL) |
| 121 | string_to_guid(&header->PlatformID, |
| 122 | json_object_get_string(platform_id)); |
| 123 | if (partition_id != NULL) |
| 124 | string_to_guid(&header->PartitionID, |
| 125 | json_object_get_string(partition_id)); |
| 126 | string_to_guid(&header->CreatorID, |
| 127 | json_object_get_string( |
| 128 | json_object_object_get(header_ir, "creatorID"))); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 129 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 130 | //Notification type. |
| 131 | json_object *notification_type = |
| 132 | json_object_object_get(header_ir, "notificationType"); |
| 133 | string_to_guid(&header->NotificationType, |
| 134 | json_object_get_string(json_object_object_get( |
| 135 | notification_type, "guid"))); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 136 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 137 | //Record ID, persistence info. |
| 138 | header->RecordID = json_object_get_uint64( |
| 139 | json_object_object_get(header_ir, "recordID")); |
| 140 | header->PersistenceInfo = json_object_get_uint64( |
| 141 | json_object_object_get(header_ir, "persistenceInfo")); |
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 | //Flags. |
| 144 | json_object *flags = json_object_object_get(header_ir, "flags"); |
| 145 | header->Flags = (UINT32)json_object_get_uint64( |
| 146 | json_object_object_get(flags, "value")); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 149 | //Converts a single given IR section into CPER, outputting to the given stream. |
| 150 | void ir_section_to_cper(json_object *section, |
| 151 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor, FILE *out) |
| 152 | { |
| 153 | //Find the correct section type, and parse. |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 154 | int section_converted = 0; |
Lawrence Tang | f1f3b83 | 2022-08-24 09:42:39 +0100 | [diff] [blame] | 155 | for (int i = 0; i < section_definitions_len; i++) { |
| 156 | if (guid_equal(section_definitions[i].Guid, |
| 157 | &descriptor->SectionType) && |
| 158 | section_definitions[i].ToCPER != NULL) { |
Lawrence Tang | 580423f | 2022-08-24 09:37:53 +0100 | [diff] [blame] | 159 | section_definitions[i].ToCPER(section, out); |
| 160 | section_converted = 1; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | //If unknown GUID, so read as a base64 unknown section. |
| 166 | if (!section_converted) { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 167 | json_object *encoded = json_object_object_get(section, "data"); |
| 168 | UINT8 *decoded = |
| 169 | b64_decode(json_object_get_string(encoded), |
| 170 | json_object_get_string_len(encoded)); |
| 171 | fwrite(decoded, descriptor->SectionLength, 1, out); |
| 172 | fflush(out); |
| 173 | free(decoded); |
| 174 | } |
| 175 | } |
| 176 | |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 177 | //Converts a single CPER-JSON IR section descriptor into a CPER structure. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 178 | void ir_section_descriptor_to_cper(json_object *section_descriptor_ir, |
| 179 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor) |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 180 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 181 | //Section offset, length. |
| 182 | descriptor->SectionOffset = (UINT32)json_object_get_uint64( |
| 183 | json_object_object_get(section_descriptor_ir, "sectionOffset")); |
| 184 | descriptor->SectionLength = (UINT32)json_object_get_uint64( |
| 185 | json_object_object_get(section_descriptor_ir, "sectionLength")); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 186 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 187 | //Revision. |
| 188 | json_object *revision = |
| 189 | json_object_object_get(section_descriptor_ir, "revision"); |
| 190 | int minor = |
| 191 | json_object_get_int(json_object_object_get(revision, "minor")); |
| 192 | int major = |
| 193 | json_object_get_int(json_object_object_get(revision, "major")); |
| 194 | descriptor->Revision = minor + (major << 8); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 195 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 196 | //Validation bits, flags. |
| 197 | descriptor->SecValidMask = ir_to_bitfield( |
| 198 | json_object_object_get(section_descriptor_ir, "validationBits"), |
| 199 | 2, CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES); |
| 200 | descriptor->SectionFlags = ir_to_bitfield( |
| 201 | json_object_object_get(section_descriptor_ir, "flags"), 8, |
| 202 | CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 203 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 204 | //Section type. |
| 205 | json_object *section_type = |
| 206 | json_object_object_get(section_descriptor_ir, "sectionType"); |
| 207 | string_to_guid(&descriptor->SectionType, |
| 208 | json_object_get_string( |
| 209 | json_object_object_get(section_type, "data"))); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 210 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 211 | //FRU ID, if present. |
| 212 | json_object *fru_id = |
| 213 | json_object_object_get(section_descriptor_ir, "fruID"); |
| 214 | if (fru_id != NULL) |
| 215 | string_to_guid(&descriptor->FruId, |
| 216 | json_object_get_string(fru_id)); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 217 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 218 | //Severity code. |
| 219 | json_object *severity = |
| 220 | json_object_object_get(section_descriptor_ir, "severity"); |
| 221 | descriptor->Severity = (UINT32)json_object_get_uint64( |
| 222 | json_object_object_get(severity, "code")); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 223 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 224 | //FRU text, if present. |
| 225 | json_object *fru_text = |
| 226 | json_object_object_get(section_descriptor_ir, "fruText"); |
| 227 | if (fru_text != NULL) |
| 228 | strncpy(descriptor->FruString, json_object_get_string(fru_text), |
| 229 | 20); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | //Converts IR for a given single section format CPER record into CPER binary. |
| 233 | void ir_single_section_to_cper(json_object *ir, FILE *out) |
| 234 | { |
| 235 | //Create & write a section descriptor to file. |
| 236 | EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor = |
| 237 | (EFI_ERROR_SECTION_DESCRIPTOR *)calloc( |
| 238 | 1, sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
| 239 | ir_section_descriptor_to_cper( |
| 240 | json_object_object_get(ir, "sectionDescriptor"), |
| 241 | section_descriptor); |
| 242 | fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, |
| 243 | out); |
| 244 | fflush(out); |
| 245 | |
| 246 | //Write section to file. |
| 247 | ir_section_to_cper(json_object_object_get(ir, "section"), |
| 248 | section_descriptor, out); |
| 249 | |
| 250 | //Free remaining resources. |
| 251 | free(section_descriptor); |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 252 | } |