blob: d68f422552e3d36db9f23e93aed8de7bf9d9e1b5 [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>
Lawrence Tang864c0da2022-07-06 15:55:40 +010014
15//Converts a single CCIX PER log CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080016json_object *cper_section_ccix_per_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tang864c0da2022-07-06 15:55:40 +010017{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080018 if (size < sizeof(EFI_CCIX_PER_LOG_DATA)) {
19 return NULL;
20 }
21
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
Ed Tanous12dbd4f2025-03-08 19:05:01 -080023
24 if (size < ccix_error->Length) {
25 return NULL;
26 }
27
Lawrence Tange407b4c2022-07-21 13:54:01 +010028 json_object *section_ir = json_object_new_object();
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080029 ValidationTypes ui64Type = { UINT_64T,
30 .value.ui64 = ccix_error->ValidBits };
Lawrence Tang864c0da2022-07-06 15:55:40 +010031
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //Length (bytes) for the entire structure.
33 json_object_object_add(section_ir, "length",
34 json_object_new_uint64(ccix_error->Length));
Lawrence Tang864c0da2022-07-06 15:55:40 +010035
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080037 if (isvalid_prop_to_ir(&ui64Type, 0)) {
38 json_object_object_add(
39 section_ir, "ccixSourceID",
40 json_object_new_int(ccix_error->CcixSourceId));
41 }
42 if (isvalid_prop_to_ir(&ui64Type, 1)) {
43 json_object_object_add(
44 section_ir, "ccixPortID",
45 json_object_new_int(ccix_error->CcixPortId));
46 }
Lawrence Tang864c0da2022-07-06 15:55:40 +010047
Lawrence Tange407b4c2022-07-21 13:54:01 +010048 //CCIX PER Log.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080049 if (isvalid_prop_to_ir(&ui64Type, 2)) {
50 //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
Ed Tanous12dbd4f2025-03-08 19:05:01 -080051 const UINT8 *cur_pos = (const UINT8 *)(ccix_error + 1);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080052 int remaining_length =
53 ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
54 if (remaining_length > 0) {
55 int32_t encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070056
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080057 char *encoded = base64_encode((UINT8 *)cur_pos,
58 remaining_length,
59 &encoded_len);
60 if (encoded == NULL) {
61 printf("Failed to allocate encode output buffer. \n");
62 } else {
63 json_object_object_add(
64 section_ir, "ccixPERLog",
65 json_object_new_string_len(
66 encoded, encoded_len));
67 free(encoded);
68 }
John Chungf8fc7052024-05-03 20:05:29 +080069 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010070 }
71
72 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010073}
74
75//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010076void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010077{
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
79 1, sizeof(EFI_CCIX_PER_LOG_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010080
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080081 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
82 struct json_object *obj = NULL;
83
Lawrence Tange407b4c2022-07-21 13:54:01 +010084 //Length.
85 section_cper->Length = json_object_get_uint64(
86 json_object_object_get(section, "length"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010087
Lawrence Tange407b4c2022-07-21 13:54:01 +010088 //Validation bits.
89 section_cper->ValidBits = ir_to_bitfield(
90 json_object_object_get(section, "validationBits"), 3,
91 CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010092
Lawrence Tange407b4c2022-07-21 13:54:01 +010093 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080094 if (json_object_object_get_ex(section, "ccixSourceID", &obj)) {
95 section_cper->CcixSourceId = (UINT8)json_object_get_int(obj);
96 add_to_valid_bitfield(&ui64Type, 0);
97 }
98 if (json_object_object_get_ex(section, "ccixPortID", &obj)) {
99 section_cper->CcixPortId = (UINT8)json_object_get_int(obj);
100 add_to_valid_bitfield(&ui64Type, 1);
101 }
102
103 bool perlog_exists = false;
104 if (json_object_object_get_ex(section, "ccixPERLog", &obj)) {
105 perlog_exists = true;
106 add_to_valid_bitfield(&ui64Type, 2);
107 }
108 section_cper->ValidBits = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100109
Lawrence Tange407b4c2022-07-21 13:54:01 +0100110 //Write header out to stream.
111 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
112 fflush(out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100113
Lawrence Tange407b4c2022-07-21 13:54:01 +0100114 //Write CCIX PER log itself to stream.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800115 if (perlog_exists) {
116 json_object *encoded = obj;
117 int32_t decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700118
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800119 UINT8 *decoded = base64_decode(
120 json_object_get_string(encoded),
121 json_object_get_string_len(encoded), &decoded_len);
122 if (decoded == NULL) {
123 printf("Failed to allocate decode output buffer. \n");
124 } else {
125 fwrite(decoded, decoded_len, 1, out);
126 fflush(out);
127 free(decoded);
128 }
John Chungf8fc7052024-05-03 20:05:29 +0800129 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100130 //Free resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100131 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800132}