xref: /openbmc/libcper/sections/cper-section-ccix-per.c (revision ad6c880fc739b6ca750c3ab594e270efd972c2ac)
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>
8 #include <string.h>
9 #include <json.h>
10 #include <libcper/base64.h>
11 #include <libcper/Cper.h>
12 #include <libcper/cper-utils.h>
13 #include <libcper/sections/cper-section-ccix-per.h>
14 #include <libcper/log.h>
15 #include <string.h>
16 
17 //Converts a single CCIX PER log CPER section into JSON IR.
cper_section_ccix_per_to_ir(const UINT8 * section,UINT32 size,char ** desc_string)18 json_object *cper_section_ccix_per_to_ir(const UINT8 *section, UINT32 size,
19 					 char **desc_string)
20 {
21 	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 
33 	if (size < sizeof(EFI_CCIX_PER_LOG_DATA)) {
34 		return NULL;
35 	}
36 
37 	EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
38 
39 	if (size < ccix_error->Length) {
40 		return NULL;
41 	}
42 
43 	json_object *section_ir = json_object_new_object();
44 	ValidationTypes ui64Type = { UINT_64T,
45 				     .value.ui64 = ccix_error->ValidBits };
46 
47 	//Length (bytes) for the entire structure.
48 	json_object_object_add(section_ir, "length",
49 			       json_object_new_uint64(ccix_error->Length));
50 
51 	//CCIX source/port IDs.
52 	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 	}
62 
63 	//CCIX PER Log.
64 	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).
66 		const UINT8 *cur_pos = (const UINT8 *)(ccix_error + 1);
67 		int remaining_length =
68 			ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
69 		if (remaining_length > 0) {
70 			int32_t encoded_len = 0;
71 
72 			char *encoded = base64_encode((UINT8 *)cur_pos,
73 						      remaining_length,
74 						      &encoded_len);
75 			if (encoded == NULL) {
76 				cper_print_log(
77 					"Failed to allocate encode output buffer. \n");
78 			} else {
79 				json_object_object_add(
80 					section_ir, "ccixPERLog",
81 					json_object_new_string_len(
82 						encoded, encoded_len));
83 				free(encoded);
84 			}
85 		}
86 	}
87 
88 	return section_ir;
89 }
90 
91 //Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
ir_section_ccix_per_to_cper(json_object * section,FILE * out)92 void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
93 {
94 	EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
95 		1, sizeof(EFI_CCIX_PER_LOG_DATA));
96 
97 	ValidationTypes ui64Type = { UINT_64T, .value.ui64 = 0 };
98 	struct json_object *obj = NULL;
99 
100 	//Length.
101 	section_cper->Length = json_object_get_uint64(
102 		json_object_object_get(section, "length"));
103 
104 	//Validation bits.
105 	section_cper->ValidBits = ir_to_bitfield(
106 		json_object_object_get(section, "validationBits"), 3,
107 		CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
108 
109 	//CCIX source/port IDs.
110 	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;
125 
126 	//Write header out to stream.
127 	fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
128 	fflush(out);
129 
130 	//Write CCIX PER log itself to stream.
131 	if (perlog_exists) {
132 		json_object *encoded = obj;
133 		int32_t decoded_len = 0;
134 
135 		UINT8 *decoded = base64_decode(
136 			json_object_get_string(encoded),
137 			json_object_get_string_len(encoded), &decoded_len);
138 		if (decoded == NULL) {
139 			cper_print_log(
140 				"Failed to allocate decode output buffer. \n");
141 		} else {
142 			fwrite(decoded, decoded_len, 1, out);
143 			fflush(out);
144 			free(decoded);
145 		}
146 	}
147 	//Free resources.
148 	free(section_cper);
149 }
150