blob: f6ffd39a775bea3ab743cedf0ab93873d23f8e89 [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.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang864c0da2022-07-06 15:55:40 +01005 * 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>
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070010#include "base64.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) {
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070042 int32_t encoded_len = 0;
43
44 char *encoded = base64_encode((UINT8 *)cur_pos,
45 remaining_length, &encoded_len);
46 if (encoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +080047 printf("Failed to allocate encode output buffer. \n");
48 } else {
John Chungf8fc7052024-05-03 20:05:29 +080049 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");
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070086 int32_t decoded_len = 0;
87
88 UINT8 *decoded = base64_decode(json_object_get_string(encoded),
89 json_object_get_string_len(encoded),
90 &decoded_len);
91 if (decoded == NULL) {
John Chungf8fc7052024-05-03 20:05:29 +080092 printf("Failed to allocate decode output buffer. \n");
93 } else {
John Chungf8fc7052024-05-03 20:05:29 +080094 fwrite(decoded, decoded_len, 1, out);
95 fflush(out);
Ed Tanousa7d2cdd2024-07-15 11:07:27 -070096 free(decoded);
John Chungf8fc7052024-05-03 20:05:29 +080097 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010098 //Free resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +0800100}