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. |
| 4 | * |
| 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include "json.h" |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 10 | #include "b64.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 | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 16 | json_object* cper_arm_error_info_to_ir(EFI_ARM_ERROR_INFORMATION_ENTRY* error_info); |
| 17 | json_object* cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER* header, void** cur_pos); |
| 18 | json_object* cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE* cache_tlb_error, EFI_ARM_ERROR_INFORMATION_ENTRY* error_info); |
| 19 | json_object* cper_arm_bus_error_to_ir(EFI_ARM_BUS_ERROR_STRUCTURE* bus_error); |
| 20 | json_object* cper_arm_misc_register_array_to_ir(EFI_ARM_MISC_CONTEXT_REGISTER* misc_register); |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame^] | 21 | void ir_arm_error_info_to_cper(json_object* error_info, FILE* out); |
| 22 | void ir_arm_context_info_to_cper(json_object* context_info, FILE* out); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 23 | |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 24 | //Converts the given processor-generic CPER section into JSON IR. |
| 25 | json_object* cper_section_arm_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) |
| 26 | { |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 27 | EFI_ARM_ERROR_RECORD* record = (EFI_ARM_ERROR_RECORD*)section; |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 28 | json_object* section_ir = json_object_new_object(); |
| 29 | |
| 30 | //Validation bits. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 31 | json_object* validation = bitfield_to_ir(record->ValidFields, 4, ARM_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 32 | json_object_object_add(section_ir, "validationBits", validation); |
| 33 | |
| 34 | //Number of error info and context info structures, and length. |
| 35 | json_object_object_add(section_ir, "errorInfoNum", json_object_new_int(record->ErrInfoNum)); |
| 36 | json_object_object_add(section_ir, "contextInfoNum", json_object_new_int(record->ContextInfoNum)); |
Lawrence Tang | 3636d3c | 2022-07-11 12:16:25 +0100 | [diff] [blame] | 37 | json_object_object_add(section_ir, "sectionLength", json_object_new_uint64(record->SectionLength)); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 38 | |
| 39 | //Error affinity. |
| 40 | json_object* error_affinity = json_object_new_object(); |
| 41 | json_object_object_add(error_affinity, "value", json_object_new_int(record->ErrorAffinityLevel)); |
| 42 | json_object_object_add(error_affinity, "type", |
| 43 | json_object_new_string(record->ErrorAffinityLevel < 4 ? "Vendor Defined" : "Reserved")); |
| 44 | json_object_object_add(section_ir, "errorAffinity", error_affinity); |
| 45 | |
| 46 | //Processor ID (MPIDR_EL1) and chip ID (MIDR_EL1). |
| 47 | json_object_object_add(section_ir, "mpidrEl1", json_object_new_uint64(record->MPIDR_EL1)); |
| 48 | json_object_object_add(section_ir, "midrEl1", json_object_new_uint64(record->MIDR_EL1)); |
| 49 | |
| 50 | //Whether the processor is running, and the state of it if so. |
Lawrence Tang | 3636d3c | 2022-07-11 12:16:25 +0100 | [diff] [blame] | 51 | json_object_object_add(section_ir, "running", json_object_new_boolean(record->RunningState & 0b1)); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 52 | if (record->RunningState >> 31) |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 53 | { |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 54 | //Bit 32 of running state is on, so PSCI state information is included. |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 55 | //This can't be made human readable, as it is unknown whether this will be the pre-PSCI 1.0 format |
| 56 | //or the newer Extended StateID format. |
Lawrence Tang | 3636d3c | 2022-07-11 12:16:25 +0100 | [diff] [blame] | 57 | json_object_object_add(section_ir, "psciState", json_object_new_uint64(record->PsciState)); |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 60 | //Processor error structures. |
| 61 | json_object* error_info_array = json_object_new_array(); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 62 | EFI_ARM_ERROR_INFORMATION_ENTRY* cur_error = (EFI_ARM_ERROR_INFORMATION_ENTRY*)(record + 1); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 63 | for (int i=0; i<record->ErrInfoNum; i++) |
| 64 | { |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 65 | json_object_array_add(error_info_array, cper_arm_error_info_to_ir(cur_error)); |
| 66 | cur_error++; |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 67 | } |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 68 | json_object_object_add(section_ir, "errorInfo", error_info_array); |
| 69 | |
| 70 | //Processor context structures. |
| 71 | //The current position is moved within the processing, as it is a dynamic size structure. |
| 72 | void* cur_pos = (void*)cur_error; |
Lawrence Tang | 3636d3c | 2022-07-11 12:16:25 +0100 | [diff] [blame] | 73 | json_object* context_info_array = json_object_new_array(); |
| 74 | for (int i=0; i<record->ContextInfoNum; i++) |
| 75 | { |
| 76 | EFI_ARM_CONTEXT_INFORMATION_HEADER* header = (EFI_ARM_CONTEXT_INFORMATION_HEADER*)cur_pos; |
| 77 | json_object* processor_context = cper_arm_processor_context_to_ir(header, &cur_pos); |
| 78 | json_object_array_add(context_info_array, processor_context); |
| 79 | } |
| 80 | json_object_object_add(section_ir, "contextInfo", context_info_array); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 81 | |
| 82 | //Is there any vendor-specific information following? |
| 83 | if (cur_pos < section + record->SectionLength) |
| 84 | { |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 85 | json_object* vendor_specific = json_object_new_object(); |
| 86 | char* encoded = b64_encode((unsigned char*)cur_pos, section + record->SectionLength - cur_pos); |
| 87 | json_object_object_add(vendor_specific, "data", json_object_new_string(encoded)); |
| 88 | free(encoded); |
| 89 | |
| 90 | json_object_object_add(section_ir, "vendorSpecificInfo", vendor_specific); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 93 | return section_ir; |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | //Converts a single ARM Process Error Information structure into JSON IR. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 97 | json_object* 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] | 98 | { |
| 99 | json_object* error_info_ir = json_object_new_object(); |
| 100 | |
| 101 | //Version, length. |
| 102 | json_object_object_add(error_info_ir, "version", json_object_new_int(error_info->Version)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 103 | json_object_object_add(error_info_ir, "length", json_object_new_int(error_info->Length)); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 104 | |
| 105 | //Validation bitfield. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 106 | json_object* validation = bitfield_to_ir(error_info->ValidationBits, 5, ARM_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 107 | json_object_object_add(error_info_ir, "validationBits", validation); |
| 108 | |
| 109 | //The type of error information in this log. |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 110 | json_object* error_type = integer_to_readable_pair(error_info->Type, 4, |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 111 | ARM_ERROR_INFO_ENTRY_INFO_TYPES_KEYS, |
| 112 | ARM_ERROR_INFO_ENTRY_INFO_TYPES_VALUES, |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 113 | "Unknown (Reserved)"); |
| 114 | json_object_object_add(error_info_ir, "errorType", error_type); |
| 115 | |
| 116 | //Multiple error count. |
Lawrence Tang | 22a467c | 2022-07-05 17:21:06 +0100 | [diff] [blame] | 117 | json_object* multiple_error = json_object_new_object(); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 118 | json_object_object_add(multiple_error, "value", json_object_new_int(error_info->MultipleError)); |
| 119 | json_object_object_add(multiple_error, "type", |
| 120 | json_object_new_string(error_info->MultipleError < 1 ? "Single Error" : "Multiple Errors")); |
| 121 | json_object_object_add(error_info_ir, "multipleError", multiple_error); |
| 122 | |
| 123 | //Flags. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 124 | json_object* flags = bitfield_to_ir(error_info->Flags, 4, ARM_ERROR_INFO_ENTRY_FLAGS_NAMES); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 125 | json_object_object_add(error_info_ir, "flags", flags); |
| 126 | |
| 127 | //Error information, split by type. |
| 128 | json_object* error_subinfo = NULL; |
| 129 | switch (error_info->Type) |
| 130 | { |
| 131 | case 0: //Cache |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 132 | case 1: //TLB |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 133 | error_subinfo = cper_arm_cache_tlb_error_to_ir((EFI_ARM_CACHE_ERROR_STRUCTURE*)&error_info->ErrorInformation, error_info); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 134 | break; |
| 135 | case 2: //Bus |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 136 | error_subinfo = cper_arm_bus_error_to_ir((EFI_ARM_BUS_ERROR_STRUCTURE*)&error_info->ErrorInformation); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | json_object_object_add(error_info_ir, "errorInformation", error_subinfo); |
| 140 | |
Lawrence Tang | b98ec66 | 2022-07-06 16:50:21 +0100 | [diff] [blame] | 141 | //Virtual fault address, physical fault address. |
| 142 | json_object_object_add(error_info_ir, "virtualFaultAddress", json_object_new_uint64(error_info->VirtualFaultAddress)); |
| 143 | json_object_object_add(error_info_ir, "physicalFaultAddress", json_object_new_uint64(error_info->PhysicalFaultAddress)); |
| 144 | |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 145 | return error_info_ir; |
| 146 | } |
| 147 | |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 148 | //Converts a single ARM cache/TLB error information structure into JSON IR format. |
| 149 | json_object* cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE* cache_tlb_error, EFI_ARM_ERROR_INFORMATION_ENTRY* error_info) |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 150 | { |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 151 | json_object* cache_tlb_error_ir = json_object_new_object(); |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 152 | |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 153 | //Validation bitfield. |
| 154 | json_object* validation = bitfield_to_ir(cache_tlb_error->ValidationBits, 7, ARM_CACHE_TLB_ERROR_VALID_BITFIELD_NAMES); |
| 155 | json_object_object_add(cache_tlb_error_ir, "validationBits", validation); |
| 156 | |
| 157 | //Transaction type. |
| 158 | json_object* transaction_type = integer_to_readable_pair(cache_tlb_error->TransactionType, 3, |
| 159 | ARM_ERROR_TRANSACTION_TYPES_KEYS, |
| 160 | ARM_ERROR_TRANSACTION_TYPES_VALUES, |
| 161 | "Unknown (Reserved)"); |
| 162 | json_object_object_add(cache_tlb_error_ir, "transactionType", transaction_type); |
| 163 | |
| 164 | //Operation. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 165 | json_object* operation; |
| 166 | if (error_info->Type == 0) |
| 167 | { |
| 168 | //Cache operation. |
| 169 | operation = integer_to_readable_pair(cache_tlb_error->Operation, 11, |
| 170 | ARM_CACHE_BUS_OPERATION_TYPES_KEYS, |
| 171 | ARM_CACHE_BUS_OPERATION_TYPES_VALUES, |
| 172 | "Unknown (Reserved)"); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | //TLB operation. |
| 177 | operation = integer_to_readable_pair(cache_tlb_error->Operation, 9, |
| 178 | ARM_TLB_OPERATION_TYPES_KEYS, |
| 179 | ARM_TLB_OPERATION_TYPES_VALUES, |
| 180 | "Unknown (Reserved)"); |
| 181 | } |
| 182 | json_object_object_add(cache_tlb_error_ir, "operation", operation); |
| 183 | |
| 184 | //Miscellaneous remaining fields. |
| 185 | json_object_object_add(cache_tlb_error_ir, "level", json_object_new_int(cache_tlb_error->Level)); |
| 186 | json_object_object_add(cache_tlb_error_ir, "processorContextCorrupt", json_object_new_boolean(cache_tlb_error->ProcessorContextCorrupt)); |
| 187 | json_object_object_add(cache_tlb_error_ir, "corrected", json_object_new_boolean(cache_tlb_error->Corrected)); |
| 188 | json_object_object_add(cache_tlb_error_ir, "precisePC", json_object_new_boolean(cache_tlb_error->PrecisePC)); |
| 189 | json_object_object_add(cache_tlb_error_ir, "restartablePC", json_object_new_boolean(cache_tlb_error->RestartablePC)); |
| 190 | return cache_tlb_error_ir; |
Lawrence Tang | 3d0e4f2 | 2022-07-05 17:17:41 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | //Converts a single ARM bus error information structure into JSON IR format. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 194 | 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] | 195 | { |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 196 | json_object* bus_error_ir = json_object_new_object(); |
| 197 | |
| 198 | //Validation bits. |
| 199 | json_object* validation = bitfield_to_ir(bus_error->ValidationBits, 7, ARM_BUS_ERROR_VALID_BITFIELD_NAMES); |
| 200 | json_object_object_add(bus_error_ir, "validationBits", validation); |
| 201 | |
| 202 | //Transaction type. |
| 203 | json_object* transaction_type = integer_to_readable_pair(bus_error->TransactionType, 3, |
| 204 | ARM_ERROR_TRANSACTION_TYPES_KEYS, |
| 205 | ARM_ERROR_TRANSACTION_TYPES_VALUES, |
| 206 | "Unknown (Reserved)"); |
| 207 | json_object_object_add(bus_error_ir, "transactionType", transaction_type); |
| 208 | |
| 209 | //Operation. |
| 210 | json_object* operation = integer_to_readable_pair(bus_error->Operation, 7, |
| 211 | ARM_CACHE_BUS_OPERATION_TYPES_KEYS, |
| 212 | ARM_CACHE_BUS_OPERATION_TYPES_VALUES, |
| 213 | "Unknown (Reserved)"); |
| 214 | json_object_object_add(bus_error_ir, "operation", operation); |
| 215 | |
| 216 | //Affinity level of bus error, + miscellaneous fields. |
| 217 | json_object_object_add(bus_error_ir, "level", json_object_new_int(bus_error->Level)); |
| 218 | json_object_object_add(bus_error_ir, "processorContextCorrupt", json_object_new_boolean(bus_error->ProcessorContextCorrupt)); |
| 219 | json_object_object_add(bus_error_ir, "corrected", json_object_new_boolean(bus_error->Corrected)); |
| 220 | json_object_object_add(bus_error_ir, "precisePC", json_object_new_boolean(bus_error->PrecisePC)); |
| 221 | json_object_object_add(bus_error_ir, "restartablePC", json_object_new_boolean(bus_error->RestartablePC)); |
| 222 | json_object_object_add(bus_error_ir, "timedOut", json_object_new_boolean(bus_error->TimeOut)); |
| 223 | |
| 224 | //Participation type. |
| 225 | json_object* participation_type = integer_to_readable_pair(bus_error->ParticipationType, 4, |
| 226 | ARM_BUS_PARTICIPATION_TYPES_KEYS, |
| 227 | ARM_BUS_PARTICIPATION_TYPES_VALUES, |
| 228 | "Unknown"); |
| 229 | json_object_object_add(bus_error_ir, "participationType", participation_type); |
| 230 | |
| 231 | //Address space. |
| 232 | json_object* address_space = integer_to_readable_pair(bus_error->AddressSpace, 3, |
| 233 | ARM_BUS_ADDRESS_SPACE_TYPES_KEYS, |
| 234 | ARM_BUS_ADDRESS_SPACE_TYPES_VALUES, |
| 235 | "Unknown"); |
| 236 | json_object_object_add(bus_error_ir, "addressSpace", address_space); |
| 237 | |
| 238 | //Memory access attributes. |
| 239 | //todo: find the specification of these in the ARM ARM |
Lawrence Tang | 3636d3c | 2022-07-11 12:16:25 +0100 | [diff] [blame] | 240 | json_object_object_add(bus_error_ir, "memoryAttributes", json_object_new_int(bus_error->MemoryAddressAttributes)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 241 | |
| 242 | //Access Mode |
| 243 | json_object* access_mode = json_object_new_object(); |
| 244 | json_object_object_add(access_mode, "value", json_object_new_int(bus_error->AccessMode)); |
| 245 | json_object_object_add(access_mode, "name", json_object_new_string(bus_error->AccessMode == 0 ? "Secure" : "Normal")); |
| 246 | json_object_object_add(bus_error_ir, "accessMode", access_mode); |
| 247 | |
| 248 | return bus_error_ir; |
| 249 | } |
| 250 | |
| 251 | //Converts a single ARM processor context block into JSON IR. |
| 252 | json_object* cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER* header, void** cur_pos) |
| 253 | { |
| 254 | json_object* context_ir = json_object_new_object(); |
| 255 | |
| 256 | //Add the context type. |
| 257 | json_object* context_type = integer_to_readable_pair(header->RegisterContextType, 9, |
| 258 | ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_KEYS, |
| 259 | ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_VALUES, |
| 260 | "Unknown (Reserved)"); |
| 261 | json_object_object_add(context_ir, "registerContextType", context_type); |
| 262 | |
| 263 | //Register array size (bytes). |
| 264 | json_object_object_add(context_ir, "registerArraySize", json_object_new_uint64(header->RegisterArraySize)); |
| 265 | |
| 266 | //The register array itself. |
| 267 | *cur_pos = (void*)(header + 1); |
| 268 | json_object* register_array = NULL; |
| 269 | switch (header->RegisterContextType) |
| 270 | { |
| 271 | case EFI_ARM_CONTEXT_TYPE_AARCH32_GPR: |
| 272 | register_array = uniform_struct_to_ir((UINT32*)cur_pos, |
| 273 | sizeof(EFI_ARM_V8_AARCH32_GPR) / sizeof(UINT32), ARM_AARCH32_GPR_NAMES); |
| 274 | break; |
| 275 | case EFI_ARM_CONTEXT_TYPE_AARCH32_EL1: |
| 276 | register_array = uniform_struct_to_ir((UINT32*)cur_pos, |
| 277 | sizeof(EFI_ARM_AARCH32_EL1_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_EL1_REGISTER_NAMES); |
| 278 | break; |
| 279 | case EFI_ARM_CONTEXT_TYPE_AARCH32_EL2: |
| 280 | register_array = uniform_struct_to_ir((UINT32*)cur_pos, |
| 281 | sizeof(EFI_ARM_AARCH32_EL2_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_EL2_REGISTER_NAMES); |
| 282 | break; |
| 283 | case EFI_ARM_CONTEXT_TYPE_AARCH32_SECURE: |
| 284 | register_array = uniform_struct_to_ir((UINT32*)cur_pos, |
| 285 | sizeof(EFI_ARM_AARCH32_SECURE_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_SECURE_REGISTER_NAMES); |
| 286 | break; |
| 287 | case EFI_ARM_CONTEXT_TYPE_AARCH64_GPR: |
| 288 | register_array = uniform_struct64_to_ir((UINT64*)cur_pos, |
| 289 | sizeof(EFI_ARM_V8_AARCH64_GPR) / sizeof(UINT64), ARM_AARCH64_GPR_NAMES); |
| 290 | break; |
| 291 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL1: |
| 292 | register_array = uniform_struct64_to_ir((UINT64*)cur_pos, |
| 293 | sizeof(EFI_ARM_AARCH64_EL1_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL1_REGISTER_NAMES); |
| 294 | break; |
| 295 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL2: |
| 296 | register_array = uniform_struct64_to_ir((UINT64*)cur_pos, |
| 297 | sizeof(EFI_ARM_AARCH64_EL2_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL2_REGISTER_NAMES); |
| 298 | break; |
| 299 | case EFI_ARM_CONTEXT_TYPE_AARCH64_EL3: |
| 300 | register_array = uniform_struct64_to_ir((UINT64*)cur_pos, |
| 301 | sizeof(EFI_ARM_AARCH64_EL3_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL3_REGISTER_NAMES); |
| 302 | break; |
| 303 | case EFI_ARM_CONTEXT_TYPE_MISC: |
| 304 | register_array = cper_arm_misc_register_array_to_ir((EFI_ARM_MISC_CONTEXT_REGISTER*)cur_pos); |
| 305 | break; |
| 306 | default: |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 307 | //Unknown register array type, add as base64 data instead. |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 308 | register_array = json_object_new_object(); |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 309 | char* encoded = b64_encode((unsigned char*)cur_pos, header->RegisterArraySize); |
| 310 | json_object_object_add(register_array, "data", json_object_new_string(encoded)); |
| 311 | free(encoded); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 312 | break; |
| 313 | } |
Lawrence Tang | 3636d3c | 2022-07-11 12:16:25 +0100 | [diff] [blame] | 314 | json_object_object_add(context_ir, "registerArray", register_array); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 315 | |
| 316 | //Set the current position to after the processor context structure. |
| 317 | *cur_pos = (UINT8*)(*cur_pos) + header->RegisterArraySize; |
| 318 | |
| 319 | return context_ir; |
| 320 | } |
| 321 | |
| 322 | //Converts a single CPER ARM miscellaneous register array to JSON IR format. |
| 323 | json_object* cper_arm_misc_register_array_to_ir(EFI_ARM_MISC_CONTEXT_REGISTER* misc_register) |
| 324 | { |
| 325 | json_object* register_array = json_object_new_object(); |
| 326 | json_object* mrs_encoding = json_object_new_object(); |
Lawrence Tang | 583cdee | 2022-07-11 14:31:11 +0100 | [diff] [blame] | 327 | json_object_object_add(mrs_encoding, "op2", json_object_new_uint64(misc_register->MrsOp2)); |
Lawrence Tang | d0c225b | 2022-07-11 14:32:36 +0100 | [diff] [blame] | 328 | json_object_object_add(mrs_encoding, "crm", json_object_new_uint64(misc_register->MrsCrm)); |
| 329 | json_object_object_add(mrs_encoding, "crn", json_object_new_uint64(misc_register->MrsCrn)); |
| 330 | json_object_object_add(mrs_encoding, "op1", json_object_new_uint64(misc_register->MrsOp1)); |
| 331 | json_object_object_add(mrs_encoding, "o0", json_object_new_uint64(misc_register->MrsO0)); |
Lawrence Tang | 7f21db6 | 2022-07-06 11:09:39 +0100 | [diff] [blame] | 332 | json_object_object_add(register_array, "mrsEncoding", mrs_encoding); |
| 333 | json_object_object_add(register_array, "value", json_object_new_uint64(misc_register->Value)); |
| 334 | |
| 335 | return register_array; |
Lawrence Tang | 7cd1390 | 2022-07-13 16:59:25 +0100 | [diff] [blame^] | 336 | } |
| 337 | |
| 338 | //Converts a single CPER-JSON ARM error section into CPER binary, outputting to the given stream. |
| 339 | void ir_section_arm_to_cper(json_object* section, FILE* out) |
| 340 | { |
| 341 | EFI_ARM_ERROR_RECORD* section_cper = (EFI_ARM_ERROR_RECORD*)calloc(1, sizeof(EFI_ARM_ERROR_RECORD)); |
| 342 | |
| 343 | //Validation bits. |
| 344 | section_cper->ValidFields = ir_to_bitfield(json_object_object_get(section, "validationBits"), |
| 345 | 4, ARM_ERROR_VALID_BITFIELD_NAMES); |
| 346 | |
| 347 | //Count of error/context info structures. |
| 348 | section_cper->ErrInfoNum = json_object_get_int(json_object_object_get(section, "errorInfoNum")); |
| 349 | section_cper->ContextInfoNum = json_object_get_int(json_object_object_get(section, "contextInfoNum")); |
| 350 | |
| 351 | //Miscellaneous raw value fields. |
| 352 | section_cper->SectionLength = json_object_get_uint64(json_object_object_get(section, "sectionLength")); |
| 353 | section_cper->ErrorAffinityLevel = readable_pair_to_integer(json_object_object_get(section, "errorAffinity")); |
| 354 | section_cper->MPIDR_EL1 = json_object_get_uint64(json_object_object_get(section, "mpidrEl1")); |
| 355 | section_cper->MIDR_EL1 = json_object_get_uint64(json_object_object_get(section, "midrEl1")); |
| 356 | section_cper->RunningState = json_object_get_boolean(json_object_object_get(section, "running")); |
| 357 | |
| 358 | //Optional PSCI state. |
| 359 | json_object* psci_state = json_object_object_get(section, "psciState"); |
| 360 | if (psci_state != NULL) |
| 361 | section_cper->PsciState = json_object_get_uint64(psci_state); |
| 362 | |
| 363 | //Flush header to stream. |
| 364 | fwrite(section_cper, sizeof(EFI_ARM_ERROR_RECORD), 1, out); |
| 365 | fflush(out); |
| 366 | |
| 367 | //Error info structure array. |
| 368 | json_object* error_info = json_object_object_get(section, "errorInfo"); |
| 369 | for (int i=0; i<section_cper->ErrInfoNum; i++) |
| 370 | ir_arm_error_info_to_cper(json_object_array_get_idx(error_info, i), out); |
| 371 | |
| 372 | //Context info structure array. |
| 373 | json_object* context_info = json_object_object_get(section, "contextInfo"); |
| 374 | for (int i=0; i<section_cper->ContextInfoNum; i++) |
| 375 | ir_arm_context_info_to_cper(json_object_array_get_idx(context_info, i), out); |
| 376 | |
| 377 | //Vendor specific error info. |
| 378 | json_object* vendor_specific_info = json_object_object_get(section, "vendorSpecificInfo"); |
| 379 | if (vendor_specific_info != NULL) |
| 380 | { |
| 381 | int vendor_specific_len = json_object_get_string_len(vendor_specific_info); |
| 382 | UINT8* decoded = b64_decode(json_object_get_string(vendor_specific_info), vendor_specific_len); |
| 383 | fwrite(decoded, vendor_specific_len / 4 * 3, 1, out); //b64 length to byte length |
| 384 | fflush(out); |
| 385 | free(decoded); |
| 386 | } |
| 387 | |
| 388 | //Free remaining resources. |
| 389 | free(section_cper); |
| 390 | } |
| 391 | |
| 392 | //Converts a single ARM error information structure into CPER binary, outputting to the given stream. |
| 393 | void ir_arm_error_info_to_cper(json_object* error_info, FILE* out) |
| 394 | { |
| 395 | EFI_ARM_ERROR_INFORMATION_ENTRY error_info_cper; |
| 396 | |
| 397 | //Version, length. |
| 398 | error_info_cper.Version = json_object_get_int(json_object_object_get(error_info, "version")); |
| 399 | error_info_cper.Length = json_object_get_int(json_object_object_get(error_info, "version")); |
| 400 | |
| 401 | //Validation bits. |
| 402 | error_info_cper.ValidationBits = ir_to_bitfield(json_object_object_get(error_info, "validationBits"), |
| 403 | 5, ARM_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES); |
| 404 | |
| 405 | //Type, multiple error. |
| 406 | error_info_cper.Type = (UINT8)readable_pair_to_integer(json_object_object_get(error_info, "type")); |
| 407 | error_info_cper.Type = (UINT8)readable_pair_to_integer(json_object_object_get(error_info, "multipleError")); |
| 408 | |
| 409 | //Flags object. |
| 410 | error_info_cper.Flags = ir_to_bitfield(json_object_object_get(error_info, "flags"), |
| 411 | 4, ARM_ERROR_INFO_ENTRY_FLAGS_NAMES); |
| 412 | |
| 413 | //Error information. |
| 414 | //... |
| 415 | |
| 416 | //Virtual/physical fault address. |
| 417 | error_info_cper.VirtualFaultAddress = json_object_get_uint64(json_object_object_get(error_info, "virtualFaultAddress")); |
| 418 | error_info_cper.PhysicalFaultAddress = json_object_get_uint64(json_object_object_get(error_info, "physicalFaultAddress")); |
| 419 | |
| 420 | //Write out to stream. |
| 421 | fwrite(&error_info_cper, sizeof(EFI_ARM_ERROR_INFORMATION_ENTRY), 1, out); |
| 422 | fflush(out); |
| 423 | } |
| 424 | |
| 425 | //Converts a single ARM context information structure into CPER binary, outputting to the given stream. |
| 426 | void ir_arm_context_info_to_cper(json_object* context_info, FILE* out) |
| 427 | { |
| 428 | |
Lawrence Tang | 2800cd8 | 2022-07-05 16:08:20 +0100 | [diff] [blame] | 429 | } |