Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting ARM CPER sections from binary and JSON format |
| 3 | * into an intermediate format. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 4 | * |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +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 | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 11 | #include "../edk/Cper.h" |
| 12 | #include "../cper-utils.h" |
| 13 | #include "cper-section-arm.h" |
| 14 | |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 15 | //Private pre-definitions. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 16 | json_object * |
| 17 | cper_arm_error_info_to_ir(EFI_ARM_ERROR_INFORMATION_ENTRY *error_info); |
| 18 | json_object * |
| 19 | cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER *header, |
| 20 | void **cur_pos); |
| 21 | json_object * |
| 22 | cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE *cache_tlb_error, |
| 23 | EFI_ARM_ERROR_INFORMATION_ENTRY *error_info); |
| 24 | json_object *cper_arm_bus_error_to_ir(EFI_ARM_BUS_ERROR_STRUCTURE *bus_error); |
| 25 | json_object *cper_arm_misc_register_array_to_ir( |
| 26 | EFI_ARM_MISC_CONTEXT_REGISTER *misc_register); |
| 27 | void ir_arm_error_info_to_cper(json_object *error_info, FILE *out); |
| 28 | void ir_arm_context_info_to_cper(json_object *context_info, FILE *out); |
| 29 | void ir_arm_error_cache_tlb_info_to_cper( |
| 30 | json_object *error_information, |
| 31 | EFI_ARM_CACHE_ERROR_STRUCTURE *error_info_cper); |
| 32 | void ir_arm_error_bus_info_to_cper(json_object *error_information, |
| 33 | EFI_ARM_BUS_ERROR_STRUCTURE *error_info_cper); |
| 34 | void ir_arm_aarch32_gpr_to_cper(json_object *registers, FILE *out); |
| 35 | void ir_arm_aarch32_el1_to_cper(json_object *registers, FILE *out); |
| 36 | void ir_arm_aarch32_el2_to_cper(json_object *registers, FILE *out); |
| 37 | void ir_arm_aarch32_secure_to_cper(json_object *registers, FILE *out); |
| 38 | void ir_arm_aarch64_gpr_to_cper(json_object *registers, FILE *out); |
| 39 | void ir_arm_aarch64_el1_to_cper(json_object *registers, FILE *out); |
| 40 | void ir_arm_aarch64_el2_to_cper(json_object *registers, FILE *out); |
| 41 | void ir_arm_aarch64_el3_to_cper(json_object *registers, FILE *out); |
| 42 | void ir_arm_misc_registers_to_cper(json_object *registers, FILE *out); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 43 | void ir_arm_unknown_register_to_cper(json_object *registers, FILE *out); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 44 | |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 45 | //Converts the given processor-generic CPER section into JSON IR. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 46 | json_object *cper_section_arm_to_ir(void *section) |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 47 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 48 | EFI_ARM_ERROR_RECORD *record = (EFI_ARM_ERROR_RECORD *)section; |
| 49 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 50 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 51 | //Validation bits. |
| 52 | json_object *validation = bitfield_to_ir( |
| 53 | record->ValidFields, 4, ARM_ERROR_VALID_BITFIELD_NAMES); |
| 54 | json_object_object_add(section_ir, "validationBits", validation); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 55 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 56 | //Number of error info and context info structures, and length. |
| 57 | json_object_object_add(section_ir, "errorInfoNum", |
| 58 | json_object_new_int(record->ErrInfoNum)); |
| 59 | json_object_object_add(section_ir, "contextInfoNum", |
| 60 | json_object_new_int(record->ContextInfoNum)); |
| 61 | json_object_object_add(section_ir, "sectionLength", |
| 62 | json_object_new_uint64(record->SectionLength)); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 63 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 64 | //Error affinity. |
| 65 | json_object *error_affinity = json_object_new_object(); |
| 66 | json_object_object_add(error_affinity, "value", |
| 67 | json_object_new_int(record->ErrorAffinityLevel)); |
| 68 | json_object_object_add( |
| 69 | error_affinity, "type", |
| 70 | json_object_new_string(record->ErrorAffinityLevel < 4 ? |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 71 | "Vendor Defined" : |
| 72 | "Reserved")); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 73 | json_object_object_add(section_ir, "errorAffinity", error_affinity); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 74 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 75 | //Processor ID (MPIDR_EL1) and chip ID (MIDR_EL1). |
| 76 | json_object_object_add(section_ir, "mpidrEl1", |
| 77 | json_object_new_uint64(record->MPIDR_EL1)); |
| 78 | json_object_object_add(section_ir, "midrEl1", |
| 79 | json_object_new_uint64(record->MIDR_EL1)); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 80 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 81 | //Whether the processor is running, and the state of it if so. |
| 82 | json_object_object_add(section_ir, "running", |
| 83 | json_object_new_boolean(record->RunningState & |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 84 | 0x1)); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 85 | if (!(record->RunningState >> 31)) { |
| 86 | //Bit 32 of running state is on, so PSCI state information is included. |
| 87 | //This can't be made human readable, as it is unknown whether this will be the pre-PSCI 1.0 format |
| 88 | //or the newer Extended StateID format. |
| 89 | json_object_object_add( |
| 90 | section_ir, "psciState", |
| 91 | json_object_new_uint64(record->PsciState)); |
| 92 | } |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 93 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 94 | //Processor error structures. |
| 95 | json_object *error_info_array = json_object_new_array(); |
| 96 | EFI_ARM_ERROR_INFORMATION_ENTRY *cur_error = |
| 97 | (EFI_ARM_ERROR_INFORMATION_ENTRY *)(record + 1); |
| 98 | for (int i = 0; i < record->ErrInfoNum; i++) { |
| 99 | json_object_array_add(error_info_array, |
| 100 | cper_arm_error_info_to_ir(cur_error)); |
| 101 | cur_error++; |
| 102 | } |
| 103 | json_object_object_add(section_ir, "errorInfo", error_info_array); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 104 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 105 | //Processor context structures. |
| 106 | //The current position is moved within the processing, as it is a dynamic size structure. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 107 | uint8_t *cur_pos = (uint8_t *)cur_error; |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 108 | json_object *context_info_array = json_object_new_array(); |
| 109 | for (int i = 0; i < record->ContextInfoNum; i++) { |
| 110 | EFI_ARM_CONTEXT_INFORMATION_HEADER *header = |
| 111 | (EFI_ARM_CONTEXT_INFORMATION_HEADER *)cur_pos; |
| 112 | json_object *processor_context = |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 113 | cper_arm_processor_context_to_ir(header, |
| 114 | (void **)&cur_pos); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 115 | json_object_array_add(context_info_array, processor_context); |
| 116 | } |
| 117 | json_object_object_add(section_ir, "contextInfo", context_info_array); |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 118 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 119 | //Is there any vendor-specific information following? |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 120 | if (cur_pos < (uint8_t *)section + record->SectionLength) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 121 | json_object *vendor_specific = json_object_new_object(); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 122 | size_t input_size = |
| 123 | (uint8_t *)section + record->SectionLength - cur_pos; |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 124 | int32_t encoded_len = 0; |
| 125 | char *encoded = |
| 126 | base64_encode(cur_pos, input_size, &encoded_len); |
| 127 | if (encoded == NULL) { |
| 128 | return NULL; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 129 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 130 | json_object_object_add(vendor_specific, "data", |
| 131 | json_object_new_string_len(encoded, |
| 132 | encoded_len)); |
| 133 | free(encoded); |
| 134 | |
| 135 | json_object_object_add(section_ir, "vendorSpecificInfo", |
| 136 | vendor_specific); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return section_ir; |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | //Converts a single ARM Process Error Information structure into JSON IR. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 143 | json_object * |
| 144 | cper_arm_error_info_to_ir(EFI_ARM_ERROR_INFORMATION_ENTRY *error_info) |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 145 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 146 | json_object *error_info_ir = json_object_new_object(); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 147 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 148 | //Version, length. |
| 149 | json_object_object_add(error_info_ir, "version", |
| 150 | json_object_new_int(error_info->Version)); |
| 151 | json_object_object_add(error_info_ir, "length", |
| 152 | json_object_new_int(error_info->Length)); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 153 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 154 | //Validation bitfield. |
| 155 | json_object *validation = |
| 156 | bitfield_to_ir(error_info->ValidationBits, 5, |
| 157 | ARM_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES); |
| 158 | json_object_object_add(error_info_ir, "validationBits", validation); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 159 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 160 | //The type of error information in this log. |
| 161 | json_object *error_type = integer_to_readable_pair( |
| 162 | error_info->Type, 4, ARM_ERROR_INFO_ENTRY_INFO_TYPES_KEYS, |
| 163 | ARM_ERROR_INFO_ENTRY_INFO_TYPES_VALUES, "Unknown (Reserved)"); |
| 164 | json_object_object_add(error_info_ir, "errorType", error_type); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 165 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 166 | //Multiple error count. |
| 167 | json_object *multiple_error = json_object_new_object(); |
| 168 | json_object_object_add(multiple_error, "value", |
| 169 | json_object_new_int(error_info->MultipleError)); |
| 170 | json_object_object_add( |
| 171 | multiple_error, "type", |
| 172 | json_object_new_string(error_info->MultipleError < 1 ? |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 173 | "Single Error" : |
| 174 | "Multiple Errors")); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 175 | json_object_object_add(error_info_ir, "multipleError", multiple_error); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 176 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 177 | //Flags. |
| 178 | json_object *flags = bitfield_to_ir(error_info->Flags, 4, |
| 179 | ARM_ERROR_INFO_ENTRY_FLAGS_NAMES); |
| 180 | json_object_object_add(error_info_ir, "flags", flags); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 181 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 182 | //Error information, split by type. |
| 183 | json_object *error_subinfo = NULL; |
| 184 | switch (error_info->Type) { |
| 185 | case ARM_ERROR_INFORMATION_TYPE_CACHE: //Cache |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 186 | case ARM_ERROR_INFORMATION_TYPE_TLB: //TLB |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 187 | error_subinfo = cper_arm_cache_tlb_error_to_ir( |
| 188 | (EFI_ARM_CACHE_ERROR_STRUCTURE *)&error_info |
| 189 | ->ErrorInformation, |
| 190 | error_info); |
| 191 | break; |
| 192 | case ARM_ERROR_INFORMATION_TYPE_BUS: //Bus |
| 193 | error_subinfo = cper_arm_bus_error_to_ir( |
| 194 | (EFI_ARM_BUS_ERROR_STRUCTURE *)&error_info |
| 195 | ->ErrorInformation); |
| 196 | break; |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 197 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 198 | default: |
| 199 | //Unknown/microarch, so can't be made readable. Simply dump as a uint64 data object. |
| 200 | error_subinfo = json_object_new_object(); |
| 201 | json_object_object_add( |
| 202 | error_subinfo, "data", |
| 203 | json_object_new_uint64( |
John Chung | 0b9c940 | 2024-05-22 22:52:55 +0800 | [diff] [blame] | 204 | error_info->ErrorInformation.Value)); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | json_object_object_add(error_info_ir, "errorInformation", |
| 208 | error_subinfo); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 209 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 210 | //Virtual fault address, physical fault address. |
| 211 | json_object_object_add( |
| 212 | error_info_ir, "virtualFaultAddress", |
| 213 | json_object_new_uint64(error_info->VirtualFaultAddress)); |
| 214 | json_object_object_add( |
| 215 | error_info_ir, "physicalFaultAddress", |
| 216 | json_object_new_uint64(error_info->PhysicalFaultAddress)); |
| 217 | |
| 218 | return error_info_ir; |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 221 | //Converts a single ARM cache/TLB error information structure into JSON IR format. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 222 | json_object * |
| 223 | cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE *cache_tlb_error, |
| 224 | EFI_ARM_ERROR_INFORMATION_ENTRY *error_info) |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 225 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 226 | json_object *cache_tlb_error_ir = json_object_new_object(); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 227 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 228 | //Validation bitfield. |
| 229 | json_object *validation = |
| 230 | bitfield_to_ir(cache_tlb_error->ValidationBits, 7, |
| 231 | ARM_CACHE_TLB_ERROR_VALID_BITFIELD_NAMES); |
| 232 | json_object_object_add(cache_tlb_error_ir, "validationBits", |
| 233 | validation); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 234 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 235 | //Transaction type. |
| 236 | json_object *transaction_type = integer_to_readable_pair( |
| 237 | cache_tlb_error->TransactionType, 3, |
| 238 | ARM_ERROR_TRANSACTION_TYPES_KEYS, |
| 239 | ARM_ERROR_TRANSACTION_TYPES_VALUES, "Unknown (Reserved)"); |
| 240 | json_object_object_add(cache_tlb_error_ir, "transactionType", |
| 241 | transaction_type); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 242 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 243 | //Operation. |
| 244 | json_object *operation; |
| 245 | if (error_info->Type == 0) { |
| 246 | //Cache operation. |
| 247 | operation = integer_to_readable_pair( |
| 248 | cache_tlb_error->Operation, 11, |
| 249 | ARM_CACHE_BUS_OPERATION_TYPES_KEYS, |
| 250 | ARM_CACHE_BUS_OPERATION_TYPES_VALUES, |
| 251 | "Unknown (Reserved)"); |
| 252 | } else { |
| 253 | //TLB operation. |
| 254 | operation = integer_to_readable_pair( |
| 255 | cache_tlb_error->Operation, 9, |
| 256 | ARM_TLB_OPERATION_TYPES_KEYS, |
| 257 | ARM_TLB_OPERATION_TYPES_VALUES, "Unknown (Reserved)"); |
| 258 | } |
| 259 | json_object_object_add(cache_tlb_error_ir, "operation", operation); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 260 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 261 | //Miscellaneous remaining fields. |
| 262 | json_object_object_add(cache_tlb_error_ir, "level", |
| 263 | json_object_new_int(cache_tlb_error->Level)); |
| 264 | json_object_object_add( |
| 265 | cache_tlb_error_ir, "processorContextCorrupt", |
| 266 | json_object_new_boolean( |
| 267 | cache_tlb_error->ProcessorContextCorrupt)); |
| 268 | json_object_object_add( |
| 269 | cache_tlb_error_ir, "corrected", |
| 270 | json_object_new_boolean(cache_tlb_error->Corrected)); |
| 271 | json_object_object_add( |
| 272 | cache_tlb_error_ir, "precisePC", |
| 273 | json_object_new_boolean(cache_tlb_error->PrecisePC)); |
| 274 | json_object_object_add( |
| 275 | cache_tlb_error_ir, "restartablePC", |
| 276 | json_object_new_boolean(cache_tlb_error->RestartablePC)); |
| 277 | return cache_tlb_error_ir; |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | //Converts a single ARM bus error information structure into JSON IR format. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 281 | json_object *cper_arm_bus_error_to_ir(EFI_ARM_BUS_ERROR_STRUCTURE *bus_error) |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 282 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 283 | json_object *bus_error_ir = json_object_new_object(); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 284 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 285 | //Validation bits. |
| 286 | json_object *validation = |
| 287 | bitfield_to_ir(bus_error->ValidationBits, 12, |
| 288 | ARM_BUS_ERROR_VALID_BITFIELD_NAMES); |
| 289 | json_object_object_add(bus_error_ir, "validationBits", validation); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 290 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 291 | //Transaction type. |
| 292 | json_object *transaction_type = integer_to_readable_pair( |
| 293 | bus_error->TransactionType, 3, ARM_ERROR_TRANSACTION_TYPES_KEYS, |
| 294 | ARM_ERROR_TRANSACTION_TYPES_VALUES, "Unknown (Reserved)"); |
| 295 | json_object_object_add(bus_error_ir, "transactionType", |
| 296 | transaction_type); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 297 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 298 | //Operation. |
| 299 | json_object *operation = integer_to_readable_pair( |
| 300 | bus_error->Operation, 7, ARM_CACHE_BUS_OPERATION_TYPES_KEYS, |
| 301 | ARM_CACHE_BUS_OPERATION_TYPES_VALUES, "Unknown (Reserved)"); |
| 302 | json_object_object_add(bus_error_ir, "operation", operation); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 303 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 304 | //Affinity level of bus error, + miscellaneous fields. |
| 305 | json_object_object_add(bus_error_ir, "level", |
| 306 | json_object_new_int(bus_error->Level)); |
| 307 | json_object_object_add( |
| 308 | bus_error_ir, "processorContextCorrupt", |
| 309 | json_object_new_boolean(bus_error->ProcessorContextCorrupt)); |
| 310 | json_object_object_add(bus_error_ir, "corrected", |
| 311 | json_object_new_boolean(bus_error->Corrected)); |
| 312 | json_object_object_add(bus_error_ir, "precisePC", |
| 313 | json_object_new_boolean(bus_error->PrecisePC)); |
| 314 | json_object_object_add( |
| 315 | bus_error_ir, "restartablePC", |
| 316 | json_object_new_boolean(bus_error->RestartablePC)); |
| 317 | json_object_object_add(bus_error_ir, "timedOut", |
| 318 | json_object_new_boolean(bus_error->TimeOut)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 319 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 320 | //Participation type. |
| 321 | json_object *participation_type = integer_to_readable_pair( |
| 322 | bus_error->ParticipationType, 4, |
| 323 | ARM_BUS_PARTICIPATION_TYPES_KEYS, |
| 324 | ARM_BUS_PARTICIPATION_TYPES_VALUES, "Unknown"); |
| 325 | json_object_object_add(bus_error_ir, "participationType", |
| 326 | participation_type); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 327 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 328 | //Address space. |
| 329 | json_object *address_space = integer_to_readable_pair( |
| 330 | bus_error->AddressSpace, 3, ARM_BUS_ADDRESS_SPACE_TYPES_KEYS, |
| 331 | ARM_BUS_ADDRESS_SPACE_TYPES_VALUES, "Unknown"); |
| 332 | json_object_object_add(bus_error_ir, "addressSpace", address_space); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 333 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 334 | //Memory access attributes. |
| 335 | //todo: find the specification of these in the ARM ARM |
| 336 | json_object_object_add( |
| 337 | bus_error_ir, "memoryAttributes", |
| 338 | json_object_new_int(bus_error->MemoryAddressAttributes)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 339 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 340 | //Access Mode |
| 341 | json_object *access_mode = json_object_new_object(); |
| 342 | json_object_object_add(access_mode, "value", |
| 343 | json_object_new_int(bus_error->AccessMode)); |
| 344 | json_object_object_add( |
| 345 | access_mode, "name", |
| 346 | json_object_new_string(bus_error->AccessMode == 0 ? "Secure" : |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 347 | "Normal")); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 348 | json_object_object_add(bus_error_ir, "accessMode", access_mode); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 349 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 350 | return bus_error_ir; |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | //Converts a single ARM processor context block into JSON IR. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 354 | json_object * |
| 355 | cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER *header, |
| 356 | void **cur_pos) |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 357 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 358 | json_object *context_ir = json_object_new_object(); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 359 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 360 | //Version. |
| 361 | json_object_object_add(context_ir, "version", |
| 362 | json_object_new_int(header->Version)); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 363 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 364 | //Add the context type. |
| 365 | json_object *context_type = integer_to_readable_pair( |
| 366 | header->RegisterContextType, 9, |
| 367 | ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_KEYS, |
| 368 | ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_VALUES, |
| 369 | "Unknown (Reserved)"); |
| 370 | json_object_object_add(context_ir, "registerContextType", context_type); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 371 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 372 | //Register array size (bytes). |
| 373 | json_object_object_add( |
| 374 | context_ir, "registerArraySize", |
| 375 | json_object_new_uint64(header->RegisterArraySize)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 376 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 377 | //The register array itself. |
| 378 | *cur_pos = (void *)(header + 1); |
| 379 | json_object *register_array = NULL; |
| 380 | switch (header->RegisterContextType) { |
| 381 | case EFI_ARM_CONTEXT_TYPE_AARCH32_GPR: |
| 382 | register_array = uniform_struct_to_ir( |
| 383 | (UINT32 *)cur_pos, |
| 384 | sizeof(EFI_ARM_V8_AARCH32_GPR) / sizeof(UINT32), |
| 385 | ARM_AARCH32_GPR_NAMES); |
| 386 | break; |
| 387 | case EFI_ARM_CONTEXT_TYPE_AARCH32_EL1: |
| 388 | register_array = uniform_struct_to_ir( |
| 389 | (UINT32 *)cur_pos, |
| 390 | sizeof(EFI_ARM_AARCH32_EL1_CONTEXT_REGISTERS) / |
| 391 | sizeof(UINT32), |
| 392 | ARM_AARCH32_EL1_REGISTER_NAMES); |
| 393 | break; |
| 394 | case EFI_ARM_CONTEXT_TYPE_AARCH32_EL2: |
| 395 | register_array = uniform_struct_to_ir( |
| 396 | (UINT32 *)cur_pos, |
| 397 | sizeof(EFI_ARM_AARCH32_EL2_CONTEXT_REGISTERS) / |
| 398 | sizeof(UINT32), |
| 399 | ARM_AARCH32_EL2_REGISTER_NAMES); |
| 400 | break; |
| 401 | case EFI_ARM_CONTEXT_TYPE_AARCH32_SECURE: |
| 402 | register_array = uniform_struct_to_ir( |
| 403 | (UINT32 *)cur_pos, |
| 404 | sizeof(EFI_ARM_AARCH32_SECURE_CONTEXT_REGISTERS) / |
| 405 | sizeof(UINT32), |
| 406 | ARM_AARCH32_SECURE_REGISTER_NAMES); |
| 407 | break; |
| 408 | case EFI_ARM_CONTEXT_TYPE_AARCH64_GPR: |
| 409 | register_array = uniform_struct64_to_ir( |
| 410 | (UINT64 *)cur_pos, |
| 411 | sizeof(EFI_ARM_V8_AARCH64_GPR) / sizeof(UINT64), |
| 412 | ARM_AARCH64_GPR_NAMES); |
| 413 | break; |
| 414 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL1: |
| 415 | register_array = uniform_struct64_to_ir( |
| 416 | (UINT64 *)cur_pos, |
| 417 | sizeof(EFI_ARM_AARCH64_EL1_CONTEXT_REGISTERS) / |
| 418 | sizeof(UINT64), |
| 419 | ARM_AARCH64_EL1_REGISTER_NAMES); |
| 420 | break; |
| 421 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL2: |
| 422 | register_array = uniform_struct64_to_ir( |
| 423 | (UINT64 *)cur_pos, |
| 424 | sizeof(EFI_ARM_AARCH64_EL2_CONTEXT_REGISTERS) / |
| 425 | sizeof(UINT64), |
| 426 | ARM_AARCH64_EL2_REGISTER_NAMES); |
| 427 | break; |
| 428 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL3: |
| 429 | register_array = uniform_struct64_to_ir( |
| 430 | (UINT64 *)cur_pos, |
| 431 | sizeof(EFI_ARM_AARCH64_EL3_CONTEXT_REGISTERS) / |
| 432 | sizeof(UINT64), |
| 433 | ARM_AARCH64_EL3_REGISTER_NAMES); |
| 434 | break; |
| 435 | case EFI_ARM_CONTEXT_TYPE_MISC: |
| 436 | register_array = cper_arm_misc_register_array_to_ir( |
| 437 | (EFI_ARM_MISC_CONTEXT_REGISTER *)cur_pos); |
| 438 | break; |
| 439 | default: |
| 440 | //Unknown register array type, add as base64 data instead. |
| 441 | register_array = json_object_new_object(); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 442 | int32_t encoded_len = 0; |
| 443 | char *encoded = base64_encode((UINT8 *)cur_pos, |
| 444 | header->RegisterArraySize, |
| 445 | &encoded_len); |
| 446 | if (encoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 447 | printf("Failed to allocate encode output buffer. \n"); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 448 | return NULL; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 449 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 450 | json_object_object_add(register_array, "data", |
| 451 | json_object_new_string_len(encoded, |
| 452 | encoded_len)); |
| 453 | free(encoded); |
| 454 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | json_object_object_add(context_ir, "registerArray", register_array); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 458 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 459 | //Set the current position to after the processor context structure. |
| 460 | *cur_pos = (UINT8 *)(*cur_pos) + header->RegisterArraySize; |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 461 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 462 | return context_ir; |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | //Converts a single CPER ARM miscellaneous register array to JSON IR format. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 466 | json_object * |
| 467 | cper_arm_misc_register_array_to_ir(EFI_ARM_MISC_CONTEXT_REGISTER *misc_register) |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 468 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 469 | json_object *register_array = json_object_new_object(); |
| 470 | json_object *mrs_encoding = json_object_new_object(); |
| 471 | json_object_object_add(mrs_encoding, "op2", |
| 472 | json_object_new_uint64(misc_register->MrsOp2)); |
| 473 | json_object_object_add(mrs_encoding, "crm", |
| 474 | json_object_new_uint64(misc_register->MrsCrm)); |
| 475 | json_object_object_add(mrs_encoding, "crn", |
| 476 | json_object_new_uint64(misc_register->MrsCrn)); |
| 477 | json_object_object_add(mrs_encoding, "op1", |
| 478 | json_object_new_uint64(misc_register->MrsOp1)); |
| 479 | json_object_object_add(mrs_encoding, "o0", |
| 480 | json_object_new_uint64(misc_register->MrsO0)); |
| 481 | json_object_object_add(register_array, "mrsEncoding", mrs_encoding); |
| 482 | json_object_object_add(register_array, "value", |
| 483 | json_object_new_uint64(misc_register->Value)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 484 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 485 | return register_array; |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | //Converts a single CPER-JSON ARM error section into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 489 | void ir_section_arm_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 490 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 491 | EFI_ARM_ERROR_RECORD *section_cper = |
| 492 | (EFI_ARM_ERROR_RECORD *)calloc(1, sizeof(EFI_ARM_ERROR_RECORD)); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 493 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 494 | //Validation bits. |
| 495 | section_cper->ValidFields = ir_to_bitfield( |
| 496 | json_object_object_get(section, "validationBits"), 4, |
| 497 | ARM_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 498 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 499 | //Count of error/context info structures. |
| 500 | section_cper->ErrInfoNum = json_object_get_int( |
| 501 | json_object_object_get(section, "errorInfoNum")); |
| 502 | section_cper->ContextInfoNum = json_object_get_int( |
| 503 | json_object_object_get(section, "contextInfoNum")); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 504 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 505 | //Miscellaneous raw value fields. |
| 506 | section_cper->SectionLength = json_object_get_uint64( |
| 507 | json_object_object_get(section, "sectionLength")); |
| 508 | section_cper->ErrorAffinityLevel = readable_pair_to_integer( |
| 509 | json_object_object_get(section, "errorAffinity")); |
| 510 | section_cper->MPIDR_EL1 = json_object_get_uint64( |
| 511 | json_object_object_get(section, "mpidrEl1")); |
| 512 | section_cper->MIDR_EL1 = json_object_get_uint64( |
| 513 | json_object_object_get(section, "midrEl1")); |
| 514 | section_cper->RunningState = json_object_get_boolean( |
| 515 | json_object_object_get(section, "running")); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 516 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 517 | //Optional PSCI state. |
| 518 | json_object *psci_state = json_object_object_get(section, "psciState"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 519 | if (psci_state != NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 520 | section_cper->PsciState = json_object_get_uint64(psci_state); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 521 | } |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 522 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 523 | //Flush header to stream. |
| 524 | fwrite(section_cper, sizeof(EFI_ARM_ERROR_RECORD), 1, out); |
| 525 | fflush(out); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 526 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 527 | //Error info structure array. |
| 528 | json_object *error_info = json_object_object_get(section, "errorInfo"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 529 | for (int i = 0; i < section_cper->ErrInfoNum; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 530 | ir_arm_error_info_to_cper( |
| 531 | json_object_array_get_idx(error_info, i), out); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 532 | } |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 533 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 534 | //Context info structure array. |
| 535 | json_object *context_info = |
| 536 | json_object_object_get(section, "contextInfo"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 537 | for (int i = 0; i < section_cper->ContextInfoNum; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 538 | ir_arm_context_info_to_cper( |
| 539 | json_object_array_get_idx(context_info, i), out); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 540 | } |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 541 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 542 | //Vendor specific error info. |
| 543 | json_object *vendor_specific_info = |
| 544 | json_object_object_get(section, "vendorSpecificInfo"); |
| 545 | if (vendor_specific_info != NULL) { |
| 546 | json_object *vendor_info_string = |
| 547 | json_object_object_get(vendor_specific_info, "data"); |
| 548 | int vendor_specific_len = |
| 549 | json_object_get_string_len(vendor_info_string); |
Lawrence Tang | 01e3a44 | 2022-07-20 15:14:50 +0100 | [diff] [blame] | 550 | |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 551 | int32_t decoded_len = 0; |
| 552 | |
| 553 | UINT8 *decoded = base64_decode( |
| 554 | json_object_get_string(vendor_info_string), |
| 555 | vendor_specific_len, &decoded_len); |
| 556 | |
| 557 | //Write out to file. |
| 558 | fwrite(decoded, decoded_len, 1, out); |
| 559 | fflush(out); |
| 560 | free(decoded); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 561 | } |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 562 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 563 | //Free remaining resources. |
| 564 | free(section_cper); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | //Converts a single ARM error information structure into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 568 | void ir_arm_error_info_to_cper(json_object *error_info, FILE *out) |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 569 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 570 | EFI_ARM_ERROR_INFORMATION_ENTRY error_info_cper; |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 571 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 572 | //Version, length. |
| 573 | error_info_cper.Version = json_object_get_int( |
| 574 | json_object_object_get(error_info, "version")); |
| 575 | error_info_cper.Length = json_object_get_int( |
| 576 | json_object_object_get(error_info, "length")); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 577 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 578 | //Validation bits. |
| 579 | error_info_cper.ValidationBits = ir_to_bitfield( |
| 580 | json_object_object_get(error_info, "validationBits"), 5, |
| 581 | ARM_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 582 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 583 | //Type, multiple error. |
| 584 | error_info_cper.Type = (UINT8)readable_pair_to_integer( |
| 585 | json_object_object_get(error_info, "type")); |
| 586 | error_info_cper.MultipleError = (UINT16)readable_pair_to_integer( |
| 587 | json_object_object_get(error_info, "multipleError")); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 588 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 589 | //Flags object. |
| 590 | error_info_cper.Flags = (UINT8)ir_to_bitfield( |
| 591 | json_object_object_get(error_info, "flags"), 4, |
| 592 | ARM_ERROR_INFO_ENTRY_FLAGS_NAMES); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 593 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 594 | //Error information. |
| 595 | json_object *error_info_information = |
| 596 | json_object_object_get(error_info, "errorInformation"); |
| 597 | switch (error_info_cper.Type) { |
| 598 | case ARM_ERROR_INFORMATION_TYPE_CACHE: |
| 599 | case ARM_ERROR_INFORMATION_TYPE_TLB: |
| 600 | ir_arm_error_cache_tlb_info_to_cper( |
| 601 | error_info_information, |
| 602 | &error_info_cper.ErrorInformation.CacheError); |
| 603 | break; |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 604 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 605 | case ARM_ERROR_INFORMATION_TYPE_BUS: |
| 606 | ir_arm_error_bus_info_to_cper( |
| 607 | error_info_information, |
| 608 | &error_info_cper.ErrorInformation.BusError); |
| 609 | break; |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 610 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 611 | default: |
| 612 | //Unknown error information type. |
John Chung | 0b9c940 | 2024-05-22 22:52:55 +0800 | [diff] [blame] | 613 | error_info_cper.ErrorInformation.Value = json_object_get_uint64( |
| 614 | json_object_object_get(error_info_information, "data")); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 615 | break; |
| 616 | } |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 617 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 618 | //Virtual/physical fault address. |
| 619 | error_info_cper.VirtualFaultAddress = json_object_get_uint64( |
| 620 | json_object_object_get(error_info, "virtualFaultAddress")); |
| 621 | error_info_cper.PhysicalFaultAddress = json_object_get_uint64( |
| 622 | json_object_object_get(error_info, "physicalFaultAddress")); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 623 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 624 | //Write out to stream. |
| 625 | fwrite(&error_info_cper, sizeof(EFI_ARM_ERROR_INFORMATION_ENTRY), 1, |
| 626 | out); |
| 627 | fflush(out); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 630 | //Converts a single ARM cache/TLB error information structure into a CPER structure. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 631 | void ir_arm_error_cache_tlb_info_to_cper( |
| 632 | json_object *error_information, |
| 633 | EFI_ARM_CACHE_ERROR_STRUCTURE *error_info_cper) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 634 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 635 | //Validation bits. |
| 636 | error_info_cper->ValidationBits = ir_to_bitfield( |
| 637 | json_object_object_get(error_information, "validationBits"), 7, |
| 638 | ARM_CACHE_TLB_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 639 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 640 | //Miscellaneous value fields. |
| 641 | error_info_cper->TransactionType = readable_pair_to_integer( |
| 642 | json_object_object_get(error_information, "transactionType")); |
| 643 | error_info_cper->Operation = readable_pair_to_integer( |
| 644 | json_object_object_get(error_information, "operation")); |
| 645 | error_info_cper->Level = json_object_get_uint64( |
| 646 | json_object_object_get(error_information, "level")); |
| 647 | error_info_cper->ProcessorContextCorrupt = json_object_get_boolean( |
| 648 | json_object_object_get(error_information, |
| 649 | "processorContextCorrupt")); |
| 650 | error_info_cper->Corrected = json_object_get_boolean( |
| 651 | json_object_object_get(error_information, "corrected")); |
| 652 | error_info_cper->PrecisePC = json_object_get_boolean( |
| 653 | json_object_object_get(error_information, "precisePC")); |
| 654 | error_info_cper->RestartablePC = json_object_get_boolean( |
| 655 | json_object_object_get(error_information, "restartablePC")); |
| 656 | error_info_cper->Reserved = 0; |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | //Converts a single ARM bus error information structure into a CPER structure. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 660 | void ir_arm_error_bus_info_to_cper(json_object *error_information, |
| 661 | EFI_ARM_BUS_ERROR_STRUCTURE *error_info_cper) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 662 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 663 | //Validation bits. |
| 664 | error_info_cper->ValidationBits = ir_to_bitfield( |
| 665 | json_object_object_get(error_information, "validationBits"), 7, |
| 666 | ARM_BUS_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 667 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 668 | //Miscellaneous value fields. |
| 669 | error_info_cper->TransactionType = readable_pair_to_integer( |
| 670 | json_object_object_get(error_information, "transactionType")); |
| 671 | error_info_cper->Operation = readable_pair_to_integer( |
| 672 | json_object_object_get(error_information, "operation")); |
| 673 | error_info_cper->Level = json_object_get_uint64( |
| 674 | json_object_object_get(error_information, "level")); |
| 675 | error_info_cper->ProcessorContextCorrupt = json_object_get_boolean( |
| 676 | json_object_object_get(error_information, |
| 677 | "processorContextCorrupt")); |
| 678 | error_info_cper->Corrected = json_object_get_boolean( |
| 679 | json_object_object_get(error_information, "corrected")); |
| 680 | error_info_cper->PrecisePC = json_object_get_boolean( |
| 681 | json_object_object_get(error_information, "precisePC")); |
| 682 | error_info_cper->RestartablePC = json_object_get_boolean( |
| 683 | json_object_object_get(error_information, "restartablePC")); |
| 684 | error_info_cper->ParticipationType = readable_pair_to_integer( |
| 685 | json_object_object_get(error_information, "participationType")); |
| 686 | error_info_cper->AddressSpace = readable_pair_to_integer( |
| 687 | json_object_object_get(error_information, "addressSpace")); |
| 688 | error_info_cper->AccessMode = readable_pair_to_integer( |
| 689 | json_object_object_get(error_information, "accessMode")); |
| 690 | error_info_cper->MemoryAddressAttributes = json_object_get_uint64( |
| 691 | json_object_object_get(error_information, "memoryAttributes")); |
| 692 | error_info_cper->Reserved = 0; |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 695 | //Converts a single ARM context information structure into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 696 | void ir_arm_context_info_to_cper(json_object *context_info, FILE *out) |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 697 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 698 | EFI_ARM_CONTEXT_INFORMATION_HEADER info_header; |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame] | 699 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 700 | //Version, array size, context type. |
| 701 | info_header.Version = json_object_get_int( |
| 702 | json_object_object_get(context_info, "version")); |
| 703 | info_header.RegisterArraySize = json_object_get_int( |
| 704 | json_object_object_get(context_info, "registerArraySize")); |
| 705 | info_header.RegisterContextType = readable_pair_to_integer( |
| 706 | json_object_object_get(context_info, "registerContextType")); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 707 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 708 | //Flush to stream, write the register array itself. |
| 709 | fwrite(&info_header, sizeof(EFI_ARM_CONTEXT_INFORMATION_HEADER), 1, |
| 710 | out); |
| 711 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 712 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 713 | json_object *register_array = |
| 714 | json_object_object_get(context_info, "registerArray"); |
| 715 | switch (info_header.RegisterContextType) { |
| 716 | case EFI_ARM_CONTEXT_TYPE_AARCH32_GPR: |
| 717 | ir_arm_aarch32_gpr_to_cper(register_array, out); |
| 718 | break; |
| 719 | case EFI_ARM_CONTEXT_TYPE_AARCH32_EL1: |
| 720 | ir_arm_aarch32_el1_to_cper(register_array, out); |
| 721 | break; |
| 722 | case EFI_ARM_CONTEXT_TYPE_AARCH32_EL2: |
| 723 | ir_arm_aarch32_el2_to_cper(register_array, out); |
| 724 | break; |
| 725 | case EFI_ARM_CONTEXT_TYPE_AARCH32_SECURE: |
| 726 | ir_arm_aarch32_secure_to_cper(register_array, out); |
| 727 | break; |
| 728 | case EFI_ARM_CONTEXT_TYPE_AARCH64_GPR: |
| 729 | ir_arm_aarch64_gpr_to_cper(register_array, out); |
| 730 | break; |
| 731 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL1: |
| 732 | ir_arm_aarch64_el1_to_cper(register_array, out); |
| 733 | break; |
| 734 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL2: |
| 735 | ir_arm_aarch64_el2_to_cper(register_array, out); |
| 736 | break; |
| 737 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL3: |
| 738 | ir_arm_aarch64_el3_to_cper(register_array, out); |
| 739 | break; |
| 740 | case EFI_ARM_CONTEXT_TYPE_MISC: |
| 741 | ir_arm_misc_registers_to_cper(register_array, out); |
| 742 | break; |
| 743 | default: |
| 744 | //Unknown register structure. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 745 | ir_arm_unknown_register_to_cper(register_array, out); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 746 | break; |
| 747 | } |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | //Converts a single AARCH32 GPR CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 751 | void ir_arm_aarch32_gpr_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 752 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 753 | //Get uniform register array. |
| 754 | EFI_ARM_V8_AARCH32_GPR reg_array; |
| 755 | ir_to_uniform_struct(registers, (UINT32 *)®_array, |
| 756 | sizeof(EFI_ARM_V8_AARCH32_GPR) / sizeof(UINT32), |
| 757 | ARM_AARCH32_GPR_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 758 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 759 | //Flush to stream. |
| 760 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 761 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | //Converts a single AARCH32 EL1 register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 765 | void ir_arm_aarch32_el1_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 766 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 767 | //Get uniform register array. |
| 768 | EFI_ARM_AARCH32_EL1_CONTEXT_REGISTERS reg_array; |
| 769 | ir_to_uniform_struct(registers, (UINT32 *)®_array, |
| 770 | sizeof(EFI_ARM_AARCH32_EL1_CONTEXT_REGISTERS) / |
| 771 | sizeof(UINT32), |
| 772 | ARM_AARCH32_EL1_REGISTER_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 773 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 774 | //Flush to stream. |
| 775 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 776 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | //Converts a single AARCH32 EL2 register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 780 | void ir_arm_aarch32_el2_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 781 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 782 | //Get uniform register array. |
| 783 | EFI_ARM_AARCH32_EL2_CONTEXT_REGISTERS reg_array; |
| 784 | ir_to_uniform_struct(registers, (UINT32 *)®_array, |
| 785 | sizeof(EFI_ARM_AARCH32_EL2_CONTEXT_REGISTERS) / |
| 786 | sizeof(UINT32), |
| 787 | ARM_AARCH32_EL2_REGISTER_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 788 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 789 | //Flush to stream. |
| 790 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 791 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | //Converts a single AARCH32 secure register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 795 | void ir_arm_aarch32_secure_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 796 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 797 | //Get uniform register array. |
| 798 | EFI_ARM_AARCH32_SECURE_CONTEXT_REGISTERS reg_array; |
| 799 | ir_to_uniform_struct(registers, (UINT32 *)®_array, |
| 800 | sizeof(EFI_ARM_AARCH32_SECURE_CONTEXT_REGISTERS) / |
| 801 | sizeof(UINT32), |
| 802 | ARM_AARCH32_SECURE_REGISTER_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 803 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 804 | //Flush to stream. |
| 805 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 806 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | //Converts a single AARCH64 GPR CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 810 | void ir_arm_aarch64_gpr_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 811 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 812 | //Get uniform register array. |
| 813 | EFI_ARM_V8_AARCH64_GPR reg_array; |
| 814 | ir_to_uniform_struct64(registers, (UINT64 *)®_array, |
| 815 | sizeof(EFI_ARM_V8_AARCH64_GPR) / sizeof(UINT64), |
| 816 | ARM_AARCH64_GPR_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 817 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 818 | //Flush to stream. |
| 819 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 820 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | //Converts a single AARCH64 EL1 register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 824 | void ir_arm_aarch64_el1_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 825 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 826 | //Get uniform register array. |
| 827 | EFI_ARM_AARCH64_EL1_CONTEXT_REGISTERS reg_array; |
| 828 | ir_to_uniform_struct64(registers, (UINT64 *)®_array, |
| 829 | sizeof(EFI_ARM_AARCH64_EL1_CONTEXT_REGISTERS) / |
| 830 | sizeof(UINT64), |
| 831 | ARM_AARCH64_EL1_REGISTER_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 832 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 833 | //Flush to stream. |
| 834 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 835 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | //Converts a single AARCH64 EL2 register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 839 | void ir_arm_aarch64_el2_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 840 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 841 | //Get uniform register array. |
| 842 | EFI_ARM_AARCH64_EL2_CONTEXT_REGISTERS reg_array; |
| 843 | ir_to_uniform_struct64(registers, (UINT64 *)®_array, |
| 844 | sizeof(EFI_ARM_AARCH64_EL2_CONTEXT_REGISTERS) / |
| 845 | sizeof(UINT64), |
| 846 | ARM_AARCH64_EL2_REGISTER_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 847 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 848 | //Flush to stream. |
| 849 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 850 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | //Converts a single AARCH64 EL3 register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 854 | void ir_arm_aarch64_el3_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 855 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 856 | //Get uniform register array. |
| 857 | EFI_ARM_AARCH64_EL3_CONTEXT_REGISTERS reg_array; |
| 858 | ir_to_uniform_struct64(registers, (UINT64 *)®_array, |
| 859 | sizeof(EFI_ARM_AARCH64_EL3_CONTEXT_REGISTERS) / |
| 860 | sizeof(UINT64), |
| 861 | ARM_AARCH64_EL3_REGISTER_NAMES); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 862 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 863 | //Flush to stream. |
| 864 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 865 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | //Converts a single ARM miscellaneous register set CPER-JSON object to CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 869 | void ir_arm_misc_registers_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 870 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 871 | EFI_ARM_MISC_CONTEXT_REGISTER reg_array; |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 872 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 873 | //MRS encoding information. |
| 874 | json_object *mrs_encoding = |
| 875 | json_object_object_get(registers, "mrsEncoding"); |
| 876 | reg_array.MrsOp2 = json_object_get_uint64( |
| 877 | json_object_object_get(mrs_encoding, "op2")); |
| 878 | reg_array.MrsCrm = json_object_get_uint64( |
| 879 | json_object_object_get(mrs_encoding, "crm")); |
| 880 | reg_array.MrsCrn = json_object_get_uint64( |
| 881 | json_object_object_get(mrs_encoding, "crn")); |
| 882 | reg_array.MrsOp1 = json_object_get_uint64( |
| 883 | json_object_object_get(mrs_encoding, "op1")); |
| 884 | reg_array.MrsO0 = json_object_get_uint64( |
| 885 | json_object_object_get(mrs_encoding, "o0")); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 886 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 887 | //Actual register value. |
| 888 | reg_array.Value = json_object_get_uint64( |
| 889 | json_object_object_get(registers, "value")); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 890 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 891 | //Flush to stream. |
| 892 | fwrite(®_array, sizeof(reg_array), 1, out); |
| 893 | fflush(out); |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | //Converts a single ARM unknown register CPER-JSON object to CPER binary, outputting to the given stream. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 897 | void ir_arm_unknown_register_to_cper(json_object *registers, FILE *out) |
Lawrence Tang | 71570a2 | 2022-07-14 11:45:28 +0100 | [diff] [blame] | 898 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 899 | //Get base64 represented data. |
| 900 | json_object *encoded = json_object_object_get(registers, "data"); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 901 | |
| 902 | int32_t decoded_len = 0; |
| 903 | |
| 904 | UINT8 *decoded = base64_decode(json_object_get_string(encoded), |
| 905 | json_object_get_string_len(encoded), |
| 906 | &decoded_len); |
| 907 | |
| 908 | if (decoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 909 | printf("Failed to allocate decode output buffer. \n"); |
| 910 | } else { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 911 | //Flush out to stream. |
| 912 | fwrite(&decoded, decoded_len, 1, out); |
| 913 | fflush(out); |
| 914 | free(decoded); |
| 915 | } |
| 916 | } |