blob: b88d69eb054b9cdbcf79af08fa0e5301bf826636 [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.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tangc60a2432022-07-06 14:58:33 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7#include <stdio.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +01008#include <json.h>
Thu Nguyene42fb482024-10-15 14:43:11 +00009#include <libcper/Cper.h>
10#include <libcper/cper-utils.h>
11#include <libcper/sections/cper-section-firmware.h>
Ed Tanous50b966f2025-03-11 09:06:19 -070012#include <libcper/log.h>
Lawrence Tangc60a2432022-07-06 14:58:33 +010013
14//Converts a single firmware CPER section into JSON IR.
Ed Tanous12dbd4f2025-03-08 19:05:01 -080015json_object *cper_section_firmware_to_ir(const UINT8 *section, UINT32 size)
Lawrence Tangc60a2432022-07-06 14:58:33 +010016{
Ed Tanous12dbd4f2025-03-08 19:05:01 -080017 if (size < sizeof(EFI_FIRMWARE_ERROR_DATA)) {
18 return NULL;
19 }
20
Lawrence Tange407b4c2022-07-21 13:54:01 +010021 EFI_FIRMWARE_ERROR_DATA *firmware_error =
22 (EFI_FIRMWARE_ERROR_DATA *)section;
23 json_object *section_ir = json_object_new_object();
Lawrence Tangc60a2432022-07-06 14:58:33 +010024
Lawrence Tange407b4c2022-07-21 13:54:01 +010025 //Record type.
26 json_object *record_type = integer_to_readable_pair(
27 firmware_error->ErrorType, 3, FIRMWARE_ERROR_RECORD_TYPES_KEYS,
28 FIRMWARE_ERROR_RECORD_TYPES_VALUES, "Unknown (Reserved)");
29 json_object_object_add(section_ir, "errorRecordType", record_type);
Lawrence Tangc60a2432022-07-06 14:58:33 +010030
Lawrence Tange407b4c2022-07-21 13:54:01 +010031 //Revision, record identifier.
32 json_object_object_add(section_ir, "revision",
33 json_object_new_int(firmware_error->Revision));
34 json_object_object_add(
35 section_ir, "recordID",
36 json_object_new_uint64(firmware_error->RecordId));
Lawrence Tangc60a2432022-07-06 14:58:33 +010037
Lawrence Tange407b4c2022-07-21 13:54:01 +010038 //Record GUID.
Ed Tanousc2ebddd2025-03-09 10:07:01 -070039 add_guid(section_ir, "recordIDGUID", &firmware_error->RecordIdGuid);
Lawrence Tange407b4c2022-07-21 13:54:01 +010040
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);
John Chungf8fc7052024-05-03 20:05:29 +080066}