blob: ff28b50dd45572d19e03a65ddb858fa5bb74b16d [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 Tang7f21db62022-07-06 11:09:39 +010015//Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order.
16json_object* uniform_struct64_to_ir(UINT64* start, int len, const char* names[])
17{
18 json_object* result = json_object_new_object();
19
20 UINT64* cur = start;
21 for (int i=0; i<len; i++)
22 {
23 json_object_object_add(result, names[i], json_object_new_uint64(*cur));
24 cur++;
25 }
26
27 return result;
28}
29
30//Converts a single uniform struct of UINT32s into intermediate JSON IR format, given names for each field in byte order.
31json_object* uniform_struct_to_ir(UINT32* start, int len, const char* names[])
32{
33 json_object* result = json_object_new_object();
34
35 UINT32* cur = start;
36 for (int i=0; i<len; i++)
37 {
38 json_object_object_add(result, names[i], json_object_new_uint64(*cur));
39 cur++;
40 }
41
42 return result;
43}
44
Lawrence Tang3c43f742022-07-05 11:37:17 +010045//Converts a single integer value to an object containing a value, and a readable name if possible.
46json_object* integer_to_readable_pair(int value, int len, int keys[], const char* values[], const char* default_value)
47{
48 json_object* result = json_object_new_object();
49 json_object_object_add(result, "value", json_object_new_int(value));
50
51 //Search for human readable name, add.
Lawrence Tang794312c2022-07-05 14:46:10 +010052 const char* name = default_value;
Lawrence Tang3c43f742022-07-05 11:37:17 +010053 for (int i=0; i<len; i++)
54 {
55 if (keys[i] == value)
56 name = values[i];
57 }
58
59 json_object_object_add(result, "name", json_object_new_string(name));
60 return result;
61}
62
Lawrence Tang7f21db62022-07-06 11:09:39 +010063//Converts a single integer value to an object containing a value, readable name and description if possible.
64json_object* integer_to_readable_pair_with_desc(int value, int len, int keys[], const char* values[],
65 const char* descriptions[], const char* default_value)
66{
67 json_object* result = json_object_new_object();
68 json_object_object_add(result, "value", json_object_new_int(value));
69
70 //Search for human readable name, add.
71 const char* name = default_value;
72 for (int i=0; i<len; i++)
73 {
74 if (keys[i] == value)
75 {
76 name = values[i];
77 json_object_object_add(result, "description", json_object_new_string(descriptions[i]));
78 }
79 }
80
81 json_object_object_add(result, "name", json_object_new_string(name));
82 return result;
83}
84
Lawrence Tang794312c2022-07-05 14:46:10 +010085//Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left.
Lawrence Tang3d0e4f22022-07-05 17:17:41 +010086json_object* bitfield_to_ir(UINT64 bitfield, int num_fields, const char* names[])
Lawrence Tang794312c2022-07-05 14:46:10 +010087{
88 json_object* result = json_object_new_object();
89 for (int i=0; i<num_fields; i++)
90 {
Lawrence Tang2800cd82022-07-05 16:08:20 +010091 json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1));
Lawrence Tang794312c2022-07-05 14:46:10 +010092 }
93
94 return result;
95}
96
97
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010098//Converts a single UINT16 revision number into JSON IR representation.
99json_object* revision_to_ir(UINT16 revision)
100{
101 json_object* revision_info = json_object_new_object();
102 json_object_object_add(revision_info, "major", json_object_new_int(revision >> 8));
103 json_object_object_add(revision_info, "minor", json_object_new_int(revision & 0xFF));
104 return revision_info;
105}
106
107//Returns the appropriate string for the given integer severity.
108const char* severity_to_string(UINT8 severity)
109{
110 return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown";
111}
112
113//Helper function to convert an EDK EFI GUID into a string for intermediate use.
114void guid_to_string(char* out, EFI_GUID* guid)
115{
116 sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
117 guid->Data1,
118 guid->Data2,
119 guid->Data3,
120 guid->Data4[0],
121 guid->Data4[1],
122 guid->Data4[2],
123 guid->Data4[3],
124 guid->Data4[4],
125 guid->Data4[5],
126 guid->Data4[6],
127 guid->Data4[7]);
128}
129
130//Returns one if two EFI GUIDs are equal, zero otherwise.
131int guid_equal(EFI_GUID* a, EFI_GUID* b)
132{
133 //Check top base 3 components.
134 if (a->Data1 != b->Data1
135 || a->Data2 != b->Data2
136 || a->Data3 != b->Data3)
137 {
138 return 0;
139 }
140
141 //Check Data4 array for equality.
142 for (int i=0; i<8; i++)
143 {
144 if (a->Data4[i] != b->Data4[i])
145 return 0;
146 }
147
148 return 1;
149}