blob: 072c5d2a0a1ea3912ae712fe3e30ca8f379ae3d0 [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.
4 *
5 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang205dd1d2022-07-14 16:23:38 +01008#include <string.h>
Lawrence Tang864c0da2022-07-06 15:55:40 +01009#include "json.h"
Lawrence Tang27217392022-07-07 11:55:39 +010010#include "b64.h"
Lawrence Tang864c0da2022-07-06 15:55:40 +010011#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.
16json_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.
Lawrence Tang27217392022-07-07 11:55:39 +010033 //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);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010035 int remaining_length = ccix_error->Length - sizeof(ccix_error);
Lawrence Tang27217392022-07-07 11:55:39 +010036 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 }
Lawrence Tang864c0da2022-07-06 15:55:40 +010042
43 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010044}
45
46//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
47void 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.
Lawrence Tang3ab351f2022-07-20 16:09:34 +010064 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010065 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(section_cper), 1, out);
71 fflush(out);
72
73 //Free resources.
74 free(decoded);
75 free(section_cper);
Lawrence Tang864c0da2022-07-06 15:55:40 +010076}