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