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 "base64.h"
11 #include "../edk/Cper.h"
12 #include "../cper-utils.h"
13 #include "cper-section-ccix-per.h"
14 
15 //Converts a single CCIX PER log CPER section into JSON IR.
16 json_object *cper_section_ccix_per_to_ir(void *section)
17 {
18 	EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
19 	json_object *section_ir = json_object_new_object();
20 
21 	//Length (bytes) for the entire structure.
22 	json_object_object_add(section_ir, "length",
23 			       json_object_new_uint64(ccix_error->Length));
24 
25 	//Validation bits.
26 	json_object *validation = bitfield_to_ir(
27 		ccix_error->ValidBits, 3, CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
28 	json_object_object_add(section_ir, "validationBits", validation);
29 
30 	//CCIX source/port IDs.
31 	json_object_object_add(section_ir, "ccixSourceID",
32 			       json_object_new_int(ccix_error->CcixSourceId));
33 	json_object_object_add(section_ir, "ccixPortID",
34 			       json_object_new_int(ccix_error->CcixPortId));
35 
36 	//CCIX PER Log.
37 	//This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
38 	const char *cur_pos = (const char *)(ccix_error + 1);
39 	int remaining_length =
40 		ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
41 	if (remaining_length > 0) {
42 		int32_t encoded_len = 0;
43 
44 		char *encoded = base64_encode((UINT8 *)cur_pos,
45 					      remaining_length, &encoded_len);
46 		if (encoded == NULL) {
47 			printf("Failed to allocate encode output buffer. \n");
48 		} else {
49 			json_object_object_add(section_ir, "ccixPERLog",
50 					       json_object_new_string_len(
51 						       encoded, encoded_len));
52 			free(encoded);
53 		}
54 	}
55 
56 	return section_ir;
57 }
58 
59 //Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
60 void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
61 {
62 	EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
63 		1, sizeof(EFI_CCIX_PER_LOG_DATA));
64 
65 	//Length.
66 	section_cper->Length = json_object_get_uint64(
67 		json_object_object_get(section, "length"));
68 
69 	//Validation bits.
70 	section_cper->ValidBits = ir_to_bitfield(
71 		json_object_object_get(section, "validationBits"), 3,
72 		CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
73 
74 	//CCIX source/port IDs.
75 	section_cper->CcixSourceId = (UINT8)json_object_get_int(
76 		json_object_object_get(section, "ccixSourceID"));
77 	section_cper->CcixPortId = (UINT8)json_object_get_int(
78 		json_object_object_get(section, "ccixPortID"));
79 
80 	//Write header out to stream.
81 	fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
82 	fflush(out);
83 
84 	//Write CCIX PER log itself to stream.
85 	json_object *encoded = json_object_object_get(section, "ccixPERLog");
86 	int32_t decoded_len = 0;
87 
88 	UINT8 *decoded = base64_decode(json_object_get_string(encoded),
89 				       json_object_get_string_len(encoded),
90 				       &decoded_len);
91 	if (decoded == NULL) {
92 		printf("Failed to allocate decode output buffer. \n");
93 	} else {
94 		fwrite(decoded, decoded_len, 1, out);
95 		fflush(out);
96 		free(decoded);
97 	}
98 	//Free resources.
99 	free(section_cper);
100 }
101