blob: 6f85939586f6f66b601baec2a0ddd23ef2aaef7b [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.
John Chungf8fc7052024-05-03 20:05:29 +080016json_object *cper_section_ccix_per_to_ir(void *section)
Lawrence Tang864c0da2022-07-06 15:55:40 +010017{
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
19 json_object *section_ir = json_object_new_object();
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080020 ValidationTypes ui64Type = { UINT_64T,
21 .value.ui64 = ccix_error->ValidBits };
Lawrence Tang864c0da2022-07-06 15:55:40 +010022
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 //Length (bytes) for the entire structure.
24 json_object_object_add(section_ir, "length",
25 json_object_new_uint64(ccix_error->Length));
Lawrence Tang864c0da2022-07-06 15:55:40 +010026
Lawrence Tange407b4c2022-07-21 13:54:01 +010027 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080028 if (isvalid_prop_to_ir(&ui64Type, 0)) {
29 json_object_object_add(
30 section_ir, "ccixSourceID",
31 json_object_new_int(ccix_error->CcixSourceId));
32 }
33 if (isvalid_prop_to_ir(&ui64Type, 1)) {
34 json_object_object_add(
35 section_ir, "ccixPortID",
36 json_object_new_int(ccix_error->CcixPortId));
37 }
Lawrence Tang864c0da2022-07-06 15:55:40 +010038
Lawrence Tange407b4c2022-07-21 13:54:01 +010039 //CCIX PER Log.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080040 if (isvalid_prop_to_ir(&ui64Type, 2)) {
41 //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
42 const char *cur_pos = (const char *)(ccix_error + 1);
43 int remaining_length =
44 ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
45 if (remaining_length > 0) {
46 int32_t encoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070047
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080048 char *encoded = base64_encode((UINT8 *)cur_pos,
49 remaining_length,
50 &encoded_len);
51 if (encoded == NULL) {
52 printf("Failed to allocate encode output buffer. \n");
53 } else {
54 json_object_object_add(
55 section_ir, "ccixPERLog",
56 json_object_new_string_len(
57 encoded, encoded_len));
58 free(encoded);
59 }
John Chungf8fc7052024-05-03 20:05:29 +080060 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 }
62
63 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010064}
65
66//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010067void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010068{
Lawrence Tange407b4c2022-07-21 13:54:01 +010069 EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
70 1, sizeof(EFI_CCIX_PER_LOG_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010071
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080072 ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
73 struct json_object *obj = NULL;
74
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 //Length.
76 section_cper->Length = json_object_get_uint64(
77 json_object_object_get(section, "length"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010078
Lawrence Tange407b4c2022-07-21 13:54:01 +010079 //Validation bits.
80 section_cper->ValidBits = ir_to_bitfield(
81 json_object_object_get(section, "validationBits"), 3,
82 CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010083
Lawrence Tange407b4c2022-07-21 13:54:01 +010084 //CCIX source/port IDs.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080085 if (json_object_object_get_ex(section, "ccixSourceID", &obj)) {
86 section_cper->CcixSourceId = (UINT8)json_object_get_int(obj);
87 add_to_valid_bitfield(&ui64Type, 0);
88 }
89 if (json_object_object_get_ex(section, "ccixPortID", &obj)) {
90 section_cper->CcixPortId = (UINT8)json_object_get_int(obj);
91 add_to_valid_bitfield(&ui64Type, 1);
92 }
93
94 bool perlog_exists = false;
95 if (json_object_object_get_ex(section, "ccixPERLog", &obj)) {
96 perlog_exists = true;
97 add_to_valid_bitfield(&ui64Type, 2);
98 }
99 section_cper->ValidBits = ui64Type.value.ui64;
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100100
Lawrence Tange407b4c2022-07-21 13:54:01 +0100101 //Write header out to stream.
102 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
103 fflush(out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100104
Lawrence Tange407b4c2022-07-21 13:54:01 +0100105 //Write CCIX PER log itself to stream.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800106 if (perlog_exists) {
107 json_object *encoded = obj;
108 int32_t decoded_len = 0;
Ed Tanousa7d2cdd2024-07-15 11:07:27 -0700109
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -0800110 UINT8 *decoded = base64_decode(
111 json_object_get_string(encoded),
112 json_object_get_string_len(encoded), &decoded_len);
113 if (decoded == NULL) {
114 printf("Failed to allocate decode output buffer. \n");
115 } else {
116 fwrite(decoded, decoded_len, 1, out);
117 fflush(out);
118 free(decoded);
119 }
John Chungf8fc7052024-05-03 20:05:29 +0800120 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100121 //Free resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800123}