blob: 3b08cde7e60ce77711190c5a8d6f97ff36a3fa2b [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 Tang5202bbb2022-08-12 14:54:36 +01009#include <json.h>
John Chungf8fc7052024-05-03 20:05:29 +080010#include "libbase64.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.
John Chungf8fc7052024-05-03 20:05:29 +080016json_object *cper_section_ccix_per_to_ir(void *section)
Lawrence Tang864c0da2022-07-06 15:55:40 +010017{
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 EFI_CCIX_PER_LOG_DATA *ccix_error = (EFI_CCIX_PER_LOG_DATA *)section;
19 json_object *section_ir = json_object_new_object();
Lawrence Tang864c0da2022-07-06 15:55:40 +010020
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 //Length (bytes) for the entire structure.
22 json_object_object_add(section_ir, "length",
23 json_object_new_uint64(ccix_error->Length));
Lawrence Tang864c0da2022-07-06 15:55:40 +010024
Lawrence Tange407b4c2022-07-21 13:54:01 +010025 //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);
Lawrence Tang864c0da2022-07-06 15:55:40 +010029
Lawrence Tange407b4c2022-07-21 13:54:01 +010030 //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));
Lawrence Tang864c0da2022-07-06 15:55:40 +010035
Lawrence Tange407b4c2022-07-21 13:54:01 +010036 //CCIX PER Log.
37 //This is formatted as described in Section 7.3.2 of CCIX Base Specification (Rev 1.0).
John Chungf8fc7052024-05-03 20:05:29 +080038 const char *cur_pos = (const char *)(ccix_error + 1);
Lawrence Tange407b4c2022-07-21 13:54:01 +010039 int remaining_length =
40 ccix_error->Length - sizeof(EFI_CCIX_PER_LOG_DATA);
41 if (remaining_length > 0) {
John Chungf8fc7052024-05-03 20:05:29 +080042 char *encoded = malloc(2 * remaining_length);
43 size_t encoded_len = 0;
44 if (!encoded) {
45 printf("Failed to allocate encode output buffer. \n");
46 } else {
47 base64_encode((const char *)cur_pos, remaining_length,
48 encoded, &encoded_len, 0);
49 json_object_object_add(section_ir, "ccixPERLog",
50 json_object_new_string_len(
51 encoded, encoded_len));
52 free(encoded);
53 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010054 }
55
56 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010057}
58
59//Converts a single CCIX PER CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010060void ir_section_ccix_per_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010061{
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 EFI_CCIX_PER_LOG_DATA *section_cper = (EFI_CCIX_PER_LOG_DATA *)calloc(
63 1, sizeof(EFI_CCIX_PER_LOG_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010064
Lawrence Tange407b4c2022-07-21 13:54:01 +010065 //Length.
66 section_cper->Length = json_object_get_uint64(
67 json_object_object_get(section, "length"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010068
Lawrence Tange407b4c2022-07-21 13:54:01 +010069 //Validation bits.
70 section_cper->ValidBits = ir_to_bitfield(
71 json_object_object_get(section, "validationBits"), 3,
72 CCIX_PER_ERROR_VALID_BITFIELD_NAMES);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010073
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 //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"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010079
Lawrence Tange407b4c2022-07-21 13:54:01 +010080 //Write header out to stream.
81 fwrite(section_cper, sizeof(EFI_CCIX_PER_LOG_DATA), 1, out);
82 fflush(out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +010083
Lawrence Tange407b4c2022-07-21 13:54:01 +010084 //Write CCIX PER log itself to stream.
85 json_object *encoded = json_object_object_get(section, "ccixPERLog");
John Chungf8fc7052024-05-03 20:05:29 +080086 char *decoded = malloc(json_object_get_string_len(encoded));
87 size_t decoded_len = 0;
88 if (!decoded) {
89 printf("Failed to allocate decode output buffer. \n");
90 } else {
91 base64_decode(json_object_get_string(encoded),
92 json_object_get_string_len(encoded), decoded,
93 &decoded_len, 0);
94 fwrite(decoded, decoded_len, 1, out);
95 fflush(out);
96 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010097
98 //Free resources.
99 free(decoded);
100 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800101}