blob: a8a53103b6f45e70866c72311bd71fb2c04dc88d [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>
Lawrence Tang864c0da2022-07-06 15:55:40 +010015
16//Converts a single CCIX PER log CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080017json_object *cper_section_ccix_per_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tang864c0da2022-07-06 15:55:40 +010018{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080019 if (size < sizeof(EFI_CCIX_PER_LOG_DATA)) {
20 return NULL;
21 }
22
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
Ed Tanous12dbd4f2025-03-08 19:05:01 -080024
25 if (size < ccix_error->Length) {
26 return NULL;
27 }
28
Lawrence Tange407b4c2022-07-21 13:54:01 +010029 json_object *section_ir = json_object_new_object();
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080030 ValidationTypes ui64Type = { UINT_64T,
31 .value.ui64 = ccix_error->ValidBits };
Lawrence Tang864c0da2022-07-06 15:55:40 +010032
Lawrence Tange407b4c2022-07-21 13:54:01 +010033 //Length (bytes) for the entire structure.
34 json_object_object_add(section_ir, "length",
35 json_object_new_uint64(ccix_error->Length));
Lawrence Tang864c0da2022-07-06 15:55:40 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080038 if (isvalid_prop_to_ir(&ui64Type, 0)) {
39 json_object_object_add(
40 section_ir, "ccixSourceID",
41 json_object_new_int(ccix_error->CcixSourceId));
42 }
43 if (isvalid_prop_to_ir(&ui64Type, 1)) {
44 json_object_object_add(
45 section_ir, "ccixPortID",
46 json_object_new_int(ccix_error->CcixPortId));
47 }
Lawrence Tang864c0da2022-07-06 15:55:40 +010048
Lawrence Tange407b4c2022-07-21 13:54:01 +010049 //CCIX PER Log.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080050 if (isvalid_prop_to_ir(&ui64Type, 2)) {
51 //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
Ed Tanous12dbd4f2025-03-08 19:05:01 -080052 const UINT8 *cur_pos = (const UINT8 *)(ccix_error + 1);
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080053 int remaining_length =
54 ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
55 if (remaining_length > 0) {
56 int32_t encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070057
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080058 char *encoded = base64_encode((UINT8 *)cur_pos,
59 remaining_length,
60 &encoded_len);
61 if (encoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -070062 cper_print_log(
63 "Failed to allocate encode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080064 } else {
65 json_object_object_add(
66 section_ir, "ccixPERLog",
67 json_object_new_string_len(
68 encoded, encoded_len));
69 free(encoded);
70 }
John Chungf8fc7052024-05-03 20:05:29 +080071 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 }
73
74 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010075}
76
77//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010078void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010079{
Lawrence Tange407b4c2022-07-21 13:54:01 +010080 EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
81 1, sizeof(EFI_CCIX_PER_LOG_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010082
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080083 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
84 struct json_object *obj = NULL;
85
Lawrence Tange407b4c2022-07-21 13:54:01 +010086 //Length.
87 section_cper->Length = json_object_get_uint64(
88 json_object_object_get(section, "length"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010089
Lawrence Tange407b4c2022-07-21 13:54:01 +010090 //Validation bits.
91 section_cper->ValidBits = ir_to_bitfield(
92 json_object_object_get(section, "validationBits"), 3,
93 CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010094
Lawrence Tange407b4c2022-07-21 13:54:01 +010095 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080096 if (json_object_object_get_ex(section, "ccixSourceID", &obj)) {
97 section_cper->CcixSourceId = (UINT8)json_object_get_int(obj);
98 add_to_valid_bitfield(&ui64Type, 0);
99 }
100 if (json_object_object_get_ex(section, "ccixPortID", &obj)) {
101 section_cper->CcixPortId = (UINT8)json_object_get_int(obj);
102 add_to_valid_bitfield(&ui64Type, 1);
103 }
104
105 bool perlog_exists = false;
106 if (json_object_object_get_ex(section, "ccixPERLog", &obj)) {
107 perlog_exists = true;
108 add_to_valid_bitfield(&ui64Type, 2);
109 }
110 section_cper->ValidBits = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100111
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 //Write header out to stream.
113 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
114 fflush(out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100115
Lawrence Tange407b4c2022-07-21 13:54:01 +0100116 //Write CCIX PER log itself to stream.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800117 if (perlog_exists) {
118 json_object *encoded = obj;
119 int32_t decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700120
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800121 UINT8 *decoded = base64_decode(
122 json_object_get_string(encoded),
123 json_object_get_string_len(encoded), &decoded_len);
124 if (decoded == NULL) {
Ed Tanous50b966f2025-03-11 09:06:19 -0700125 cper_print_log(
126 "Failed to allocate decode output buffer. \n");
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800127 } else {
128 fwrite(decoded, decoded_len, 1, out);
129 fflush(out);
130 free(decoded);
131 }
John Chungf8fc7052024-05-03 20:05:29 +0800132 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100133 //Free resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100134 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800135}