blob: e45015a33f7506524cb2cae5a48d20752815a932 [file] [log] [blame]
Lawrence Tangc60a2432022-07-06 14:58:33 +01001/**
2 * Describes functions for converting firmware 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 Tang5202bbb2022-08-12 14:54:36 +01008#include <json.h>
Lawrence Tangc60a2432022-07-06 14:58:33 +01009#include "../edk/Cper.h"
10#include "../cper-utils.h"
11#include "cper-section-firmware.h"
12
13//Converts a single firmware CPER section into JSON IR.
Lawrence Tange407b4c2022-07-21 13:54:01 +010014json_object *
15cper_section_firmware_to_ir(void *section,
16 EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
Lawrence Tangc60a2432022-07-06 14:58:33 +010017{
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 EFI_FIRMWARE_ERROR_DATA *firmware_error =
19 (EFI_FIRMWARE_ERROR_DATA *)section;
20 json_object *section_ir = json_object_new_object();
Lawrence Tangc60a2432022-07-06 14:58:33 +010021
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 //Record type.
23 json_object *record_type = integer_to_readable_pair(
24 firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS,
25 FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)");
26 json_object_object_add(section_ir, "errorRecordType", record_type);
Lawrence Tangc60a2432022-07-06 14:58:33 +010027
Lawrence Tange407b4c2022-07-21 13:54:01 +010028 //Revision, record identifier.
29 json_object_object_add(section_ir, "revision",
30 json_object_new_int(firmware_error->Revision));
31 json_object_object_add(
32 section_ir, "recordID",
33 json_object_new_uint64(firmware_error->RecordId));
Lawrence Tangc60a2432022-07-06 14:58:33 +010034
Lawrence Tange407b4c2022-07-21 13:54:01 +010035 //Record GUID.
36 char record_id_guid[GUID_STRING_LENGTH];
37 guid_to_string(record_id_guid, &firmware_error->RecordIdGuid);
38 json_object_object_add(section_ir, "recordIDGUID",
39 json_object_new_string(record_id_guid));
40
41 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010042}
43
44//Converts a single firmware CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010045void ir_section_firmware_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010046{
Lawrence Tange407b4c2022-07-21 13:54:01 +010047 EFI_FIRMWARE_ERROR_DATA *section_cper =
48 (EFI_FIRMWARE_ERROR_DATA *)calloc(
49 1, sizeof(EFI_FIRMWARE_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010050
Lawrence Tange407b4c2022-07-21 13:54:01 +010051 //Record fields.
52 section_cper->ErrorType = readable_pair_to_integer(
53 json_object_object_get(section, "errorRecordType"));
54 section_cper->Revision = json_object_get_int(
55 json_object_object_get(section, "revision"));
56 section_cper->RecordId = json_object_get_uint64(
57 json_object_object_get(section, "recordID"));
58 string_to_guid(&section_cper->RecordIdGuid,
59 json_object_get_string(json_object_object_get(
60 section, "recordIDGUID")));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010061
Lawrence Tange407b4c2022-07-21 13:54:01 +010062 //Write to stream, free resources.
63 fwrite(section_cper, sizeof(EFI_FIRMWARE_ERROR_DATA), 1, out);
64 fflush(out);
65 free(section_cper);
Lawrence Tangc60a2432022-07-06 14:58:33 +010066}