blob: 4f41a2800053dfd23d96d526d87ce29a02fd84a0 [file] [log] [blame]
Lawrence Tang864c0da2022-07-06 15:55:40 +01001/**
2 * Describes functions for converting CCIX PER log CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang864c0da2022-07-06 15:55:40 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang205dd1d2022-07-14 16:23:38 +01008#include <string.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/base64.h>
11#include <libcper/Cper.h>
12#include <libcper/cper-utils.h>
13#include <libcper/sections/cper-section-ccix-per.h>
Ed Tanous50b966f2025-03-11 09:06:19 -070014#include <libcper/log.h>
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070015#include <string.h>
Lawrence Tang864c0da2022-07-06 15:55:40 +010016
17//Converts a single CCIX PER log CPER section into JSON IR.
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070018json_object *cper_section_ccix_per_to_ir(const UINT8 *section, UINT32 size,
19 char **desc_string)
Lawrence Tang864c0da2022-07-06 15:55:40 +010020{
Aushim Nagarkattiad6c8802025-06-18 16:45:28 -070021 int outstr_len = 0;
22 *desc_string = malloc(SECTION_DESC_STRING_SIZE);
23 outstr_len = snprintf(*desc_string, SECTION_DESC_STRING_SIZE,
24 "A CCIX PER Log Error occurred");
25 if (outstr_len < 0) {
26 cper_print_log(
27 "Error: Could not write to CCIX PER Log description string\n");
28 } else if (outstr_len > SECTION_DESC_STRING_SIZE) {
29 cper_print_log(
30 "Error: CCIX PER Log description string truncated\n");
31 }
32
Ed Tanous12dbd4f2025-03-08 19:05:01 -080033 if (size < sizeof(EFI_CCIX_PER_LOG_DATA)) {
34 return NULL;
35 }
36
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
Ed Tanous12dbd4f2025-03-08 19:05:01 -080038
39 if (size < ccix_error->Length) {
40 return NULL;
41 }
42
Lawrence Tange407b4c2022-07-21 13:54:01 +010043 json_object *section_ir = json_object_new_object();
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080044 ValidationTypes ui64Type = { UINT_64T,
45 .value.ui64 = ccix_error->ValidBits };
Lawrence Tang864c0da2022-07-06 15:55:40 +010046
Lawrence Tange407b4c2022-07-21 13:54:01 +010047 //Length (bytes) for the entire structure.
48 json_object_object_add(section_ir, "length",
49 json_object_new_uint64(ccix_error->Length));
Lawrence Tang864c0da2022-07-06 15:55:40 +010050
Lawrence Tange407b4c2022-07-21 13:54:01 +010051 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080052 if (isvalid_prop_to_ir(&ui64Type, 0)) {
53 json_object_object_add(
54 section_ir, "ccixSourceID",
55 json_object_new_int(ccix_error->CcixSourceId));
56 }
57 if (isvalid_prop_to_ir(&ui64Type, 1)) {
58 json_object_object_add(
59 section_ir, "ccixPortID",
60 json_object_new_int(ccix_error->CcixPortId));
61 }
Lawrence Tang864c0da2022-07-06 15:55:40 +010062
Lawrence Tange407b4c2022-07-21 13:54:01 +010063 //CCIX PER Log.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080064 if (isvalid_prop_to_ir(&ui64Type, 2)) {
65 //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
Ed Tanous12dbd4f2025-03-08 19:05:01 -080066 const UINT8 *cur_pos = (const UINT8 *)(ccix_error + 1);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080067 int remaining_length =
68 ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
69 if (remaining_length > 0) {
70 int32_t encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070071
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080072 char *encoded = base64_encode((UINT8 *)cur_pos,
73 remaining_length,
74 &encoded_len);
75 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070076 cper_print_log(
77 "Failed to allocate encode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080078 } else {
79 json_object_object_add(
80 section_ir, "ccixPERLog",
81 json_object_new_string_len(
82 encoded, encoded_len));
83 free(encoded);
84 }
John Chungf8fc7052024-05-03 20:05:29 +080085 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 }
87
88 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010089}
90
91//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010092void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010093{
Lawrence Tange407b4c2022-07-21 13:54:01 +010094 EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
95 1, sizeof(EFI_CCIX_PER_LOG_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010096
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080097 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
98 struct json_object *obj = NULL;
99
Lawrence Tange407b4c2022-07-21 13:54:01 +0100100 //Length.
101 section_cper->Length = json_object_get_uint64(
102 json_object_object_get(section, "length"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100103
Lawrence Tange407b4c2022-07-21 13:54:01 +0100104 //Validation bits.
105 section_cper->ValidBits = ir_to_bitfield(
106 json_object_object_get(section, "validationBits"), 3,
107 CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100108
Lawrence Tange407b4c2022-07-21 13:54:01 +0100109 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800110 if (json_object_object_get_ex(section, "ccixSourceID", &obj)) {
111 section_cper->CcixSourceId = (UINT8)json_object_get_int(obj);
112 add_to_valid_bitfield(&ui64Type, 0);
113 }
114 if (json_object_object_get_ex(section, "ccixPortID", &obj)) {
115 section_cper->CcixPortId = (UINT8)json_object_get_int(obj);
116 add_to_valid_bitfield(&ui64Type, 1);
117 }
118
119 bool perlog_exists = false;
120 if (json_object_object_get_ex(section, "ccixPERLog", &obj)) {
121 perlog_exists = true;
122 add_to_valid_bitfield(&ui64Type, 2);
123 }
124 section_cper->ValidBits = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100125
Lawrence Tange407b4c2022-07-21 13:54:01 +0100126 //Write header out to stream.
127 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
128 fflush(out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100129
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 //Write CCIX PER log itself to stream.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800131 if (perlog_exists) {
132 json_object *encoded = obj;
133 int32_t decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700134
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800135 UINT8 *decoded = base64_decode(
136 json_object_get_string(encoded),
137 json_object_get_string_len(encoded), &decoded_len);
138 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700139 cper_print_log(
140 "Failed to allocate decode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800141 } else {
142 fwrite(decoded, decoded_len, 1, out);
143 fflush(out);
144 free(decoded);
145 }
John Chungf8fc7052024-05-03 20:05:29 +0800146 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100147 //Free resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100148 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800149}