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. |
| 4 | * |
| 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> |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 10 | #include "libbase64.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) { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 42 | char *encoded = malloc(2 * remaining_length); |
| 43 | size_t encoded_len = 0; |
| 44 | if (!encoded) { |
| 45 | printf("Failed to allocate encode output buffer. \n"); |
| 46 | } else { |
| 47 | base64_encode((const char *)cur_pos, remaining_length, |
| 48 | encoded, &encoded_len, 0); |
| 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"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 86 | char *decoded = malloc(json_object_get_string_len(encoded)); |
| 87 | size_t decoded_len = 0; |
| 88 | if (!decoded) { |
| 89 | printf("Failed to allocate decode output buffer. \n"); |
| 90 | } else { |
| 91 | base64_decode(json_object_get_string(encoded), |
| 92 | json_object_get_string_len(encoded), decoded, |
| 93 | &decoded_len, 0); |
| 94 | fwrite(decoded, decoded_len, 1, out); |
| 95 | fflush(out); |
| 96 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 97 | |
| 98 | //Free resources. |
| 99 | free(decoded); |
| 100 | free(section_cper); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 101 | } |