Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting generic DMAr 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 | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 8 | #include <json.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 9 | #include <libcper/Cper.h> |
| 10 | #include <libcper/cper-utils.h> |
| 11 | #include <libcper/sections/cper-section-dmar-generic.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 12 | #include <libcper/log.h> |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 13 | |
| 14 | //Converts a single generic DMAr CPER section into JSON IR. |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 15 | json_object *cper_section_dmar_generic_to_ir(const UINT8 *section, UINT32 size) |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 16 | { |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 17 | if (size < sizeof(EFI_DMAR_GENERIC_ERROR_DATA)) { |
| 18 | return NULL; |
| 19 | } |
| 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | EFI_DMAR_GENERIC_ERROR_DATA *firmware_error = |
| 22 | (EFI_DMAR_GENERIC_ERROR_DATA *)section; |
| 23 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 24 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 25 | //Requester ID, segment. |
| 26 | json_object_object_add( |
| 27 | section_ir, "requesterID", |
| 28 | json_object_new_int(firmware_error->RequesterId)); |
| 29 | json_object_object_add( |
| 30 | section_ir, "segmentNumber", |
| 31 | json_object_new_int(firmware_error->SegmentNumber)); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 32 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 33 | //Fault reason. |
| 34 | json_object *fault_reason = integer_to_readable_pair_with_desc( |
| 35 | firmware_error->FaultReason, 11, |
| 36 | DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_KEYS, |
| 37 | DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_VALUES, |
| 38 | DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_DESCRIPTIONS, |
| 39 | "Unknown (Reserved)"); |
| 40 | json_object_object_add(section_ir, "faultReason", fault_reason); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 41 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 42 | //Access type. |
| 43 | json_object *access_type = integer_to_readable_pair( |
| 44 | firmware_error->AccessType, 2, |
| 45 | DMAR_GENERIC_ERROR_ACCESS_TYPES_KEYS, |
| 46 | DMAR_GENERIC_ERROR_ACCESS_TYPES_VALUES, "Unknown (Reserved)"); |
| 47 | json_object_object_add(section_ir, "accessType", access_type); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 48 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 49 | //Address type. |
| 50 | json_object *address_type = integer_to_readable_pair( |
| 51 | firmware_error->AddressType, 2, |
| 52 | DMAR_GENERIC_ERROR_ADDRESS_TYPES_KEYS, |
| 53 | DMAR_GENERIC_ERROR_ADDRESS_TYPES_VALUES, "Unknown (Reserved)"); |
| 54 | json_object_object_add(section_ir, "addressType", address_type); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 55 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 56 | //Architecture type. |
| 57 | json_object *arch_type = integer_to_readable_pair( |
| 58 | firmware_error->ArchType, 2, DMAR_GENERIC_ERROR_ARCH_TYPES_KEYS, |
| 59 | DMAR_GENERIC_ERROR_ARCH_TYPES_VALUES, "Unknown (Reserved)"); |
| 60 | json_object_object_add(section_ir, "architectureType", arch_type); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 61 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 62 | //Device address. |
| 63 | json_object_object_add( |
| 64 | section_ir, "deviceAddress", |
| 65 | json_object_new_uint64(firmware_error->DeviceAddr)); |
| 66 | |
| 67 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | //Converts a single generic DMAR CPER-JSON section into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 71 | void ir_section_dmar_generic_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 72 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 73 | EFI_DMAR_GENERIC_ERROR_DATA *section_cper = |
| 74 | (EFI_DMAR_GENERIC_ERROR_DATA *)calloc( |
| 75 | 1, sizeof(EFI_DMAR_GENERIC_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 76 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 77 | //Record fields. |
| 78 | section_cper->RequesterId = (UINT16)json_object_get_int( |
| 79 | json_object_object_get(section, "requesterID")); |
| 80 | section_cper->SegmentNumber = (UINT16)json_object_get_int( |
| 81 | json_object_object_get(section, "segmentNumber")); |
| 82 | section_cper->FaultReason = (UINT8)readable_pair_to_integer( |
| 83 | json_object_object_get(section, "faultReason")); |
| 84 | section_cper->AccessType = (UINT8)readable_pair_to_integer( |
| 85 | json_object_object_get(section, "accessType")); |
| 86 | section_cper->AddressType = (UINT8)readable_pair_to_integer( |
| 87 | json_object_object_get(section, "addressType")); |
| 88 | section_cper->ArchType = (UINT8)readable_pair_to_integer( |
| 89 | json_object_object_get(section, "architectureType")); |
| 90 | section_cper->DeviceAddr = json_object_get_uint64( |
| 91 | json_object_object_get(section, "deviceAddress")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 92 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 93 | //Write to stream, free resources. |
| 94 | fwrite(section_cper, sizeof(EFI_DMAR_GENERIC_ERROR_DATA), 1, out); |
| 95 | fflush(out); |
| 96 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 97 | } |