blob: f70cf545baa2064fe61692e9e940c3e30cdc9b54 [file] [log] [blame]
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01001/**
2 * Describes utility functions for parsing CPER into JSON IR.
3 *
4 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdio.h>
8#include "json.h"
9#include "edk/Cper.h"
10#include "cper-utils.h"
11
12//The available severity types for CPER.
13const char* CPER_SEVERITY_TYPES[4] = {"Recoverable", "Fatal", "Corrected", "Informational"};
14
Lawrence Tang3c43f742022-07-05 11:37:17 +010015//Converts a single integer value to an object containing a value, and a readable name if possible.
16json_object* integer_to_readable_pair(int value, int len, int keys[], const char* values[], const char* default_value)
17{
18 json_object* result = json_object_new_object();
19 json_object_object_add(result, "value", json_object_new_int(value));
20
21 //Search for human readable name, add.
Lawrence Tang794312c2022-07-05 14:46:10 +010022 const char* name = default_value;
Lawrence Tang3c43f742022-07-05 11:37:17 +010023 for (int i=0; i<len; i++)
24 {
25 if (keys[i] == value)
26 name = values[i];
27 }
28
29 json_object_object_add(result, "name", json_object_new_string(name));
30 return result;
31}
32
Lawrence Tang794312c2022-07-05 14:46:10 +010033//Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left.
Lawrence Tang3d0e4f22022-07-05 17:17:41 +010034json_object* bitfield_to_ir(UINT64 bitfield, int num_fields, const char* names[])
Lawrence Tang794312c2022-07-05 14:46:10 +010035{
36 json_object* result = json_object_new_object();
37 for (int i=0; i<num_fields; i++)
38 {
Lawrence Tang2800cd82022-07-05 16:08:20 +010039 json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1));
Lawrence Tang794312c2022-07-05 14:46:10 +010040 }
41
42 return result;
43}
44
45
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010046//Converts a single UINT16 revision number into JSON IR representation.
47json_object* revision_to_ir(UINT16 revision)
48{
49 json_object* revision_info = json_object_new_object();
50 json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8));
51 json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF));
52 return revision_info;
53}
54
55//Returns the appropriate string for the given integer severity.
56const char* severity_to_string(UINT8 severity)
57{
58 return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown";
59}
60
61//Helper function to convert an EDK EFI GUID into a string for intermediate use.
62void guid_to_string(char* out, EFI_GUID* guid)
63{
64 sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
65 guid->Data1,
66 guid->Data2,
67 guid->Data3,
68 guid->Data4[0],
69 guid->Data4[1],
70 guid->Data4[2],
71 guid->Data4[3],
72 guid->Data4[4],
73 guid->Data4[5],
74 guid->Data4[6],
75 guid->Data4[7]);
76}
77
78//Returns one if two EFI GUIDs are equal, zero otherwise.
79int guid_equal(EFI_GUID* a, EFI_GUID* b)
80{
81 //Check top base 3 components.
82 if (a->Data1 != b->Data1
83 || a->Data2 != b->Data2
84 || a->Data3 != b->Data3)
85 {
86 return 0;
87 }
88
89 //Check Data4 array for equality.
90 for (int i=0; i<8; i++)
91 {
92 if (a->Data4[i] != b->Data4[i])
93 return 0;
94 }
95
96 return 1;
97}