blob: 9e5f06af2166cc2b3c0abb766066877f6cac5133 [file] [log] [blame]
Lawrence Tang4795d4a2022-07-06 15:19:31 +01001/**
2 * Describes functions for converting generic DMAr CPER sections from binary and JSON format
3 * into an intermediate format.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tang4795d4a2022-07-06 15:19:31 +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-dmar-generic.h>
Lawrence Tang4795d4a2022-07-06 15:19:31 +010012
13//Converts a single generic DMAr CPER section into JSON IR.
John Chungf8fc7052024-05-03 20:05:29 +080014json_object *cper_section_dmar_generic_to_ir(void *section)
Lawrence Tang4795d4a2022-07-06 15:19:31 +010015{
Lawrence Tange407b4c2022-07-21 13:54:01 +010016 EFI_DMAR_GENERIC_ERROR_DATA *firmware_error =
17 (EFI_DMAR_GENERIC_ERROR_DATA *)section;
18 json_object *section_ir = json_object_new_object();
Lawrence Tang4795d4a2022-07-06 15:19:31 +010019
Lawrence Tange407b4c2022-07-21 13:54:01 +010020 //Requester ID, segment.
21 json_object_object_add(
22 section_ir, "requesterID",
23 json_object_new_int(firmware_error->RequesterId));
24 json_object_object_add(
25 section_ir, "segmentNumber",
26 json_object_new_int(firmware_error->SegmentNumber));
Lawrence Tang4795d4a2022-07-06 15:19:31 +010027
Lawrence Tange407b4c2022-07-21 13:54:01 +010028 //Fault reason.
29 json_object *fault_reason = integer_to_readable_pair_with_desc(
30 firmware_error->FaultReason, 11,
31 DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_KEYS,
32 DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_VALUES,
33 DMAR_GENERIC_ERROR_FAULT_REASON_TYPES_DESCRIPTIONS,
34 "Unknown (Reserved)");
35 json_object_object_add(section_ir, "faultReason", fault_reason);
Lawrence Tang4795d4a2022-07-06 15:19:31 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //Access type.
38 json_object *access_type = integer_to_readable_pair(
39 firmware_error->AccessType, 2,
40 DMAR_GENERIC_ERROR_ACCESS_TYPES_KEYS,
41 DMAR_GENERIC_ERROR_ACCESS_TYPES_VALUES, "Unknown (Reserved)");
42 json_object_object_add(section_ir, "accessType", access_type);
Lawrence Tang4795d4a2022-07-06 15:19:31 +010043
Lawrence Tange407b4c2022-07-21 13:54:01 +010044 //Address type.
45 json_object *address_type = integer_to_readable_pair(
46 firmware_error->AddressType, 2,
47 DMAR_GENERIC_ERROR_ADDRESS_TYPES_KEYS,
48 DMAR_GENERIC_ERROR_ADDRESS_TYPES_VALUES, "Unknown (Reserved)");
49 json_object_object_add(section_ir, "addressType", address_type);
Lawrence Tang4795d4a2022-07-06 15:19:31 +010050
Lawrence Tange407b4c2022-07-21 13:54:01 +010051 //Architecture type.
52 json_object *arch_type = integer_to_readable_pair(
53 firmware_error->ArchType, 2, DMAR_GENERIC_ERROR_ARCH_TYPES_KEYS,
54 DMAR_GENERIC_ERROR_ARCH_TYPES_VALUES, "Unknown (Reserved)");
55 json_object_object_add(section_ir, "architectureType", arch_type);
Lawrence Tang4795d4a2022-07-06 15:19:31 +010056
Lawrence Tange407b4c2022-07-21 13:54:01 +010057 //Device address.
58 json_object_object_add(
59 section_ir, "deviceAddress",
60 json_object_new_uint64(firmware_error->DeviceAddr));
61
62 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010063}
64
65//Converts a single generic DMAR CPER-JSON section into CPER binary, outputting to the given stream.
Lawrence Tange407b4c2022-07-21 13:54:01 +010066void ir_section_dmar_generic_to_cper(json_object *section, FILE *out)
Lawrence Tang205dd1d2022-07-14 16:23:38 +010067{
Lawrence Tange407b4c2022-07-21 13:54:01 +010068 EFI_DMAR_GENERIC_ERROR_DATA *section_cper =
69 (EFI_DMAR_GENERIC_ERROR_DATA *)calloc(
70 1, sizeof(EFI_DMAR_GENERIC_ERROR_DATA));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010071
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 //Record fields.
73 section_cper->RequesterId = (UINT16)json_object_get_int(
74 json_object_object_get(section, "requesterID"));
75 section_cper->SegmentNumber = (UINT16)json_object_get_int(
76 json_object_object_get(section, "segmentNumber"));
77 section_cper->FaultReason = (UINT8)readable_pair_to_integer(
78 json_object_object_get(section, "faultReason"));
79 section_cper->AccessType = (UINT8)readable_pair_to_integer(
80 json_object_object_get(section, "accessType"));
81 section_cper->AddressType = (UINT8)readable_pair_to_integer(
82 json_object_object_get(section, "addressType"));
83 section_cper->ArchType = (UINT8)readable_pair_to_integer(
84 json_object_object_get(section, "architectureType"));
85 section_cper->DeviceAddr = json_object_get_uint64(
86 json_object_object_get(section, "deviceAddress"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010087
Lawrence Tange407b4c2022-07-21 13:54:01 +010088 //Write to stream, free resources.
89 fwrite(section_cper, sizeof(EFI_DMAR_GENERIC_ERROR_DATA), 1, out);
90 fflush(out);
91 free(section_cper);
John Chungf8fc7052024-05-03 20:05:29 +080092}