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> |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 13 | #include <string.h> |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 14 | |
| 15 | //Converts a single generic DMAr CPER section into JSON IR. |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 16 | json_object *cper_section_dmar_generic_to_ir(const UINT8 *section, UINT32 size, |
| 17 | char **desc_string) |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 18 | { |
Aushim Nagarkatti | ad6c880 | 2025-06-18 16:45:28 -0700 | [diff] [blame^] | 19 | int outstr_len = 0; |
| 20 | *desc_string = malloc(SECTION_DESC_STRING_SIZE); |
| 21 | outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE, |
| 22 | "A DMAr Generic Error occurred"); |
| 23 | if (outstr_len < 0) { |
| 24 | cper_print_log( |
| 25 | "Error: Could not write to DMAr Generic description string\n"); |
| 26 | } else if (outstr_len > SECTION_DESC_STRING_SIZE) { |
| 27 | cper_print_log( |
| 28 | "Error: DMAr Generic description string truncated\n"); |
| 29 | } |
| 30 | |
Ed Tanous | 12dbd4f | 2025-03-08 19:05:01 -0800 | [diff] [blame] | 31 | if (size < sizeof(EFI_DMAR_GENERIC_ERROR_DATA)) { |
| 32 | return NULL; |
| 33 | } |
| 34 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 35 | EFI_DMAR_GENERIC_ERROR_DATA *firmware_error = |
| 36 | (EFI_DMAR_GENERIC_ERROR_DATA *)section; |
| 37 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 38 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 39 | //Requester ID, segment. |
| 40 | json_object_object_add( |
| 41 | section_ir, "requesterID", |
| 42 | json_object_new_int(firmware_error->RequesterId)); |
| 43 | json_object_object_add( |
| 44 | section_ir, "segmentNumber", |
| 45 | json_object_new_int(firmware_error->SegmentNumber)); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 46 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 47 | //Fault reason. |
| 48 | json_object *fault_reason = integer_to_readable_pair_with_desc( |
| 49 | firmware_error->FaultReason, 11, |
| 50 | DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_KEYS, |
| 51 | DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_VALUES, |
| 52 | DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_DESCRIPTIONS, |
| 53 | "Unknown (Reserved)"); |
| 54 | json_object_object_add(section_ir, "faultReason", fault_reason); |
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 | //Access type. |
| 57 | json_object *access_type = integer_to_readable_pair( |
| 58 | firmware_error->AccessType, 2, |
| 59 | DMAR_GENERIC_ERROR_ACCESS_TYPES_KEYS, |
| 60 | DMAR_GENERIC_ERROR_ACCESS_TYPES_VALUES, "Unknown (Reserved)"); |
| 61 | json_object_object_add(section_ir, "accessType", access_type); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 62 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 63 | //Address type. |
| 64 | json_object *address_type = integer_to_readable_pair( |
| 65 | firmware_error->AddressType, 2, |
| 66 | DMAR_GENERIC_ERROR_ADDRESS_TYPES_KEYS, |
| 67 | DMAR_GENERIC_ERROR_ADDRESS_TYPES_VALUES, "Unknown (Reserved)"); |
| 68 | json_object_object_add(section_ir, "addressType", address_type); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 69 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 70 | //Architecture type. |
| 71 | json_object *arch_type = integer_to_readable_pair( |
| 72 | firmware_error->ArchType, 2, DMAR_GENERIC_ERROR_ARCH_TYPES_KEYS, |
| 73 | DMAR_GENERIC_ERROR_ARCH_TYPES_VALUES, "Unknown (Reserved)"); |
| 74 | json_object_object_add(section_ir, "architectureType", arch_type); |
Lawrence Tang | 4795d4a | 2022-07-06 15:19:31 +0100 | [diff] [blame] | 75 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 76 | //Device address. |
| 77 | json_object_object_add( |
| 78 | section_ir, "deviceAddress", |
| 79 | json_object_new_uint64(firmware_error->DeviceAddr)); |
| 80 | |
| 81 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //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] | 85 | void ir_section_dmar_generic_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 86 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 87 | EFI_DMAR_GENERIC_ERROR_DATA *section_cper = |
| 88 | (EFI_DMAR_GENERIC_ERROR_DATA *)calloc( |
| 89 | 1, sizeof(EFI_DMAR_GENERIC_ERROR_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 90 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 91 | //Record fields. |
| 92 | section_cper->RequesterId = (UINT16)json_object_get_int( |
| 93 | json_object_object_get(section, "requesterID")); |
| 94 | section_cper->SegmentNumber = (UINT16)json_object_get_int( |
| 95 | json_object_object_get(section, "segmentNumber")); |
| 96 | section_cper->FaultReason = (UINT8)readable_pair_to_integer( |
| 97 | json_object_object_get(section, "faultReason")); |
| 98 | section_cper->AccessType = (UINT8)readable_pair_to_integer( |
| 99 | json_object_object_get(section, "accessType")); |
| 100 | section_cper->AddressType = (UINT8)readable_pair_to_integer( |
| 101 | json_object_object_get(section, "addressType")); |
| 102 | section_cper->ArchType = (UINT8)readable_pair_to_integer( |
| 103 | json_object_object_get(section, "architectureType")); |
| 104 | section_cper->DeviceAddr = json_object_get_uint64( |
| 105 | json_object_object_get(section, "deviceAddress")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 106 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 107 | //Write to stream, free resources. |
| 108 | fwrite(section_cper, sizeof(EFI_DMAR_GENERIC_ERROR_DATA), 1, out); |
| 109 | fflush(out); |
| 110 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 111 | } |