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 "b64.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 *
17 cper_section_ccix_per_to_ir(void *section,
18 			    EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
19 {
20 	EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
21 	json_object *section_ir = json_object_new_object();
22 
23 	//Length (bytes) for the entire structure.
24 	json_object_object_add(section_ir, "length",
25 			       json_object_new_uint64(ccix_error->Length));
26 
27 	//Validation bits.
28 	json_object *validation = bitfield_to_ir(
29 		ccix_error->ValidBits, 3, CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
30 	json_object_object_add(section_ir, "validationBits", validation);
31 
32 	//CCIX source/port IDs.
33 	json_object_object_add(section_ir, "ccixSourceID",
34 			       json_object_new_int(ccix_error->CcixSourceId));
35 	json_object_object_add(section_ir, "ccixPortID",
36 			       json_object_new_int(ccix_error->CcixPortId));
37 
38 	//CCIX PER Log.
39 	//This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
40 	unsigned char *cur_pos = (unsigned char *)(ccix_error + 1);
41 	int remaining_length =
42 		ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
43 	if (remaining_length > 0) {
44 		char *encoded = b64_encode(cur_pos, remaining_length);
45 		json_object_object_add(section_ir, "ccixPERLog",
46 				       json_object_new_string(encoded));
47 		free(encoded);
48 	}
49 
50 	return section_ir;
51 }
52 
53 //Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
54 void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
55 {
56 	EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
57 		1, sizeof(EFI_CCIX_PER_LOG_DATA));
58 
59 	//Length.
60 	section_cper->Length = json_object_get_uint64(
61 		json_object_object_get(section, "length"));
62 
63 	//Validation bits.
64 	section_cper->ValidBits = ir_to_bitfield(
65 		json_object_object_get(section, "validationBits"), 3,
66 		CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
67 
68 	//CCIX source/port IDs.
69 	section_cper->CcixSourceId = (UINT8)json_object_get_int(
70 		json_object_object_get(section, "ccixSourceID"));
71 	section_cper->CcixPortId = (UINT8)json_object_get_int(
72 		json_object_object_get(section, "ccixPortID"));
73 
74 	//Write header out to stream.
75 	fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
76 	fflush(out);
77 
78 	//Write CCIX PER log itself to stream.
79 	json_object *encoded = json_object_object_get(section, "ccixPERLog");
80 	UINT8 *decoded = b64_decode(json_object_get_string(encoded),
81 				    json_object_get_string_len(encoded));
82 	fwrite(decoded, section_cper->Length - sizeof(EFI_CCIX_PER_LOG_DATA), 1,
83 	       out);
84 	fflush(out);
85 
86 	//Free resources.
87 	free(decoded);
88 	free(section_cper);
89 }