Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting CCIX PER log 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 | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 8 | #include <string.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 | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 11 | #include "../edk/Cper.h" |
| 12 | #include "../cper-utils.h" |
| 13 | #include "cper-section-ccix-per.h" |
| 14 | |
| 15 | //Converts a single CCIX PER log CPER section into JSON IR. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 16 | json_object *cper_section_ccix_per_to_ir(void *section) |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 17 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 18 | EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section; |
| 19 | json_object *section_ir = json_object_new_object(); |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +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(ccix_error->Length)); |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 24 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 25 | //Validation bits. |
| 26 | json_object *validation = bitfield_to_ir( |
| 27 | ccix_error->ValidBits, 3, CCIX_PER_ERROR_VALID_BITFIELD_NAMES); |
| 28 | json_object_object_add(section_ir, "validationBits", validation); |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 29 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 30 | //CCIX source/port IDs. |
| 31 | json_object_object_add(section_ir, "ccixSourceID", |
| 32 | json_object_new_int(ccix_error->CcixSourceId)); |
| 33 | json_object_object_add(section_ir, "ccixPortID", |
| 34 | json_object_new_int(ccix_error->CcixPortId)); |
Lawrence Tang | 864c0da | 2022-07-06 15:55:40 +0100 | [diff] [blame] | 35 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 36 | //CCIX PER Log. |
| 37 | //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0). |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 38 | const char *cur_pos = (const char *)(ccix_error + 1); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 39 | int remaining_length = |
| 40 | ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA); |
| 41 | if (remaining_length > 0) { |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 42 | int32_t encoded_len = 0; |
| 43 | |
| 44 | char *encoded = base64_encode((UINT8 *)cur_pos, |
| 45 | remaining_length, &encoded_len); |
| 46 | if (encoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 47 | printf("Failed to allocate encode output buffer. \n"); |
| 48 | } else { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 49 | json_object_object_add(section_ir, "ccixPERLog", |
| 50 | json_object_new_string_len( |
| 51 | encoded, encoded_len)); |
| 52 | free(encoded); |
| 53 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | //Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 60 | void ir_section_ccix_per_to_cper(json_object *section, FILE *out) |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 61 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 62 | EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc( |
| 63 | 1, sizeof(EFI_CCIX_PER_LOG_DATA)); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 64 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | //Length. |
| 66 | section_cper->Length = json_object_get_uint64( |
| 67 | json_object_object_get(section, "length")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 68 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 69 | //Validation bits. |
| 70 | section_cper->ValidBits = ir_to_bitfield( |
| 71 | json_object_object_get(section, "validationBits"), 3, |
| 72 | CCIX_PER_ERROR_VALID_BITFIELD_NAMES); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 73 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 74 | //CCIX source/port IDs. |
| 75 | section_cper->CcixSourceId = (UINT8)json_object_get_int( |
| 76 | json_object_object_get(section, "ccixSourceID")); |
| 77 | section_cper->CcixPortId = (UINT8)json_object_get_int( |
| 78 | json_object_object_get(section, "ccixPortID")); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 79 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 80 | //Write header out to stream. |
| 81 | fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out); |
| 82 | fflush(out); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 83 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 84 | //Write CCIX PER log itself to stream. |
| 85 | json_object *encoded = json_object_object_get(section, "ccixPERLog"); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 86 | int32_t decoded_len = 0; |
| 87 | |
| 88 | UINT8 *decoded = base64_decode(json_object_get_string(encoded), |
| 89 | json_object_get_string_len(encoded), |
| 90 | &decoded_len); |
| 91 | if (decoded == NULL) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 92 | printf("Failed to allocate decode output buffer. \n"); |
| 93 | } else { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 94 | fwrite(decoded, decoded_len, 1, out); |
| 95 | fflush(out); |
Ed Tanous | a7d2cdd | 2024-07-15 11:07:27 -0700 | [diff] [blame] | 96 | free(decoded); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 97 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 98 | //Free resources. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 99 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 100 | } |