blob: 5beebeaee659fec7dd936d3ae0a2aee12e6af0e1 [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.
Lawrence Tange407b4c2022-07-21 13:54:01 +010016json_object *
17cper_section_ccix_per_to_ir(void *section,
18 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tang864c0da2022-07-06 15:55:40 +010019{
Lawrence Tange407b4c2022-07-21 13:54:01 +010020 EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
21 json_object *section_ir = json_object_new_object();
Lawrence Tang864c0da2022-07-06 15:55:40 +010022
Lawrence Tange407b4c2022-07-21 13:54:01 +010023 //Length (bytes) for the entire structure.
24 json_object_object_add(section_ir, "length",
25 json_object_new_uint64(ccix_error->Length));
Lawrence Tang864c0da2022-07-06 15:55:40 +010026
Lawrence Tange407b4c2022-07-21 13:54:01 +010027 //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);
Lawrence Tang864c0da2022-07-06 15:55:40 +010031
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //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));
Lawrence Tang864c0da2022-07-06 15:55:40 +010037
Lawrence Tange407b4c2022-07-21 13:54:01 +010038 //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;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010051}
52
53//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010054void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010055{
Lawrence Tange407b4c2022-07-21 13:54:01 +010056 EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
57 1, sizeof(EFI_CCIX_PER_LOG_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010058
Lawrence Tange407b4c2022-07-21 13:54:01 +010059 //Length.
60 section_cper->Length = json_object_get_uint64(
61 json_object_object_get(section, "length"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010062
Lawrence Tange407b4c2022-07-21 13:54:01 +010063 //Validation bits.
64 section_cper->ValidBits = ir_to_bitfield(
65 json_object_object_get(section, "validationBits"), 3,
66 CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010067
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 //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"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010073
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 //Write header out to stream.
75 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
76 fflush(out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010077
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 //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);
Lawrence Tang864c0da2022-07-06 15:55:40 +010089}