Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting CXL component error 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 | d7e8ca3 | 2022-07-07 10:25:53 +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/base64.h> |
| 10 | #include <libcper/Cper.h> |
| 11 | #include <libcper/cper-utils.h> |
| 12 | #include <libcper/sections/cper-section-cxl-component.h> |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 13 | |
| 14 | //Converts a single CXL component error CPER section into JSON IR. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 15 | json_object *cper_section_cxl_component_to_ir(void *section) |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 16 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 17 | EFI_CXL_COMPONENT_EVENT_HEADER *cxl_error = |
| 18 | (EFI_CXL_COMPONENT_EVENT_HEADER *)section; |
| 19 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | //Length (bytes) for the entire structure. |
| 22 | json_object_object_add(section_ir, "length", |
| 23 | json_object_new_uint64(cxl_error->Length)); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 24 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 25 | //Validation bits. |
| 26 | json_object *validation = |
| 27 | bitfield_to_ir(cxl_error->ValidBits, 3, |
| 28 | CXL_COMPONENT_ERROR_VALID_BITFIELD_NAMES); |
| 29 | json_object_object_add(section_ir, "validationBits", validation); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 30 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 31 | //Device ID. |
| 32 | json_object *device_id = json_object_new_object(); |
| 33 | json_object_object_add( |
| 34 | device_id, "vendorID", |
| 35 | json_object_new_int(cxl_error->DeviceId.VendorId)); |
| 36 | json_object_object_add( |
| 37 | device_id, "deviceID", |
| 38 | json_object_new_int(cxl_error->DeviceId.DeviceId)); |
| 39 | json_object_object_add( |
| 40 | device_id, "functionNumber", |
| 41 | json_object_new_int(cxl_error->DeviceId.FunctionNumber)); |
| 42 | json_object_object_add( |
| 43 | device_id, "deviceNumber", |
| 44 | json_object_new_int(cxl_error->DeviceId.DeviceNumber)); |
| 45 | json_object_object_add( |
| 46 | device_id, "busNumber", |
| 47 | json_object_new_int(cxl_error->DeviceId.BusNumber)); |
| 48 | json_object_object_add( |
| 49 | device_id, "segmentNumber", |
| 50 | json_object_new_int(cxl_error->DeviceId.SegmentNumber)); |
| 51 | json_object_object_add( |
| 52 | device_id, "slotNumber", |
| 53 | json_object_new_int(cxl_error->DeviceId.SlotNumber)); |
| 54 | json_object_object_add(section_ir, "deviceID", device_id); |
Lawrence Tang | 2721739 | 2022-07-07 11:55:39 +0100 | [diff] [blame] | 55 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 56 | //Device serial. |
| 57 | json_object_object_add(section_ir, "deviceSerial", |
| 58 | json_object_new_uint64(cxl_error->DeviceSerial)); |
Lawrence Tang | d7e8ca3 | 2022-07-07 10:25:53 +0100 | [diff] [blame] | 59 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 60 | //The specification for this is defined within the CXL Specification Section 8.2.9.1. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 61 | const char *cur_pos = (const char *)(cxl_error + 1); |
| 62 | int remaining_len = |
| 63 | cxl_error->Length - sizeof(EFI_CXL_COMPONENT_EVENT_HEADER); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 64 | if (remaining_len > 0) { |
| 65 | json_object *event_log = json_object_new_object(); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 66 | |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 67 | int32_t encoded_len = 0; |
| 68 | |
| 69 | char *encoded = base64_encode((UINT8 *)cur_pos, remaining_len, |
| 70 | &encoded_len); |
| 71 | if (encoded == NULL) { |
| 72 | printf("Failed to allocate encode output buffer. \n"); |
| 73 | return NULL; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 74 | } |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 75 | json_object_object_add(event_log, "data", |
| 76 | json_object_new_string_len(encoded, |
| 77 | encoded_len)); |
| 78 | |
| 79 | free(encoded); |
| 80 | json_object_object_add(section_ir, "cxlComponentEventLog", |
| 81 | event_log); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return section_ir; |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | //Converts a single given CXL Component CPER-JSON section into CPER binary, outputting to the |
| 88 | //given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 89 | void ir_section_cxl_component_to_cper(json_object *section, FILE *out) |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 90 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 91 | EFI_CXL_COMPONENT_EVENT_HEADER *section_cper = |
| 92 | (EFI_CXL_COMPONENT_EVENT_HEADER *)calloc( |
| 93 | 1, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER)); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 94 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 95 | //Length of the structure. |
| 96 | section_cper->Length = json_object_get_uint64( |
| 97 | json_object_object_get(section, "length")); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 98 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 99 | //Validation bits. |
| 100 | section_cper->ValidBits = ir_to_bitfield( |
| 101 | json_object_object_get(section, "validationBits"), 3, |
| 102 | CXL_COMPONENT_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 103 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 104 | //Device ID information. |
| 105 | json_object *device_id = json_object_object_get(section, "deviceID"); |
| 106 | section_cper->DeviceId.VendorId = json_object_get_uint64( |
| 107 | json_object_object_get(device_id, "vendorID")); |
| 108 | section_cper->DeviceId.DeviceId = json_object_get_uint64( |
| 109 | json_object_object_get(device_id, "deviceID")); |
| 110 | section_cper->DeviceId.FunctionNumber = json_object_get_uint64( |
| 111 | json_object_object_get(device_id, "functionNumber")); |
| 112 | section_cper->DeviceId.DeviceNumber = json_object_get_uint64( |
| 113 | json_object_object_get(device_id, "deviceNumber")); |
| 114 | section_cper->DeviceId.BusNumber = json_object_get_uint64( |
| 115 | json_object_object_get(device_id, "busNumber")); |
| 116 | section_cper->DeviceId.SegmentNumber = json_object_get_uint64( |
| 117 | json_object_object_get(device_id, "segmentNumber")); |
| 118 | section_cper->DeviceId.SlotNumber = json_object_get_uint64( |
| 119 | json_object_object_get(device_id, "slotNumber")); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 120 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 121 | //Device serial number. |
| 122 | section_cper->DeviceSerial = json_object_get_uint64( |
| 123 | json_object_object_get(section, "deviceSerial")); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 124 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 125 | //Write header out to stream. |
| 126 | fwrite(section_cper, sizeof(EFI_CXL_COMPONENT_EVENT_HEADER), 1, out); |
| 127 | fflush(out); |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 128 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 129 | //CXL component event log, decoded from base64. |
| 130 | json_object *event_log = |
| 131 | json_object_object_get(section, "cxlComponentEventLog"); |
| 132 | json_object *encoded = json_object_object_get(event_log, "data"); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 133 | |
| 134 | int32_t decoded_len = 0; |
| 135 | |
| 136 | UINT8 *decoded = base64_decode(json_object_get_string(encoded), |
| 137 | json_object_get_string_len(encoded), |
| 138 | &decoded_len); |
| 139 | |
| 140 | if (decoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 141 | printf("Failed to allocate decode output buffer. \n"); |
| 142 | } else { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 143 | fwrite(decoded, decoded_len, 1, out); |
| 144 | fflush(out); |
| 145 | free(decoded); |
| 146 | } |
Lawrence Tang | aec8390 | 2022-07-18 09:41:08 +0100 | [diff] [blame] | 147 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 148 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 149 | } |