xref: /openbmc/libcper/cper-utils.c (revision 7f21db6c)
1 /**
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.
13 const char* CPER_SEVERITY_TYPES[4] = {"Recoverable", "Fatal", "Corrected", "Informational"};
14 
15 //Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order.
16 json_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.
31 json_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 
45 //Converts a single integer value to an object containing a value, and a readable name if possible.
46 json_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.
52     const char* name = default_value;
53     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 
63 //Converts a single integer value to an object containing a value, readable name and description if possible.
64 json_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 
85 //Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left.
86 json_object* bitfield_to_ir(UINT64 bitfield, int num_fields, const char* names[])
87 {
88     json_object* result = json_object_new_object();
89     for (int i=0; i<num_fields; i++)
90     {
91         json_object_object_add(result, names[i], json_object_new_boolean((bitfield >> i) & 0b1));
92     }
93 
94     return result;
95 }
96 
97 
98 //Converts a single UINT16 revision number into JSON IR representation.
99 json_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.
108 const 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.
114 void 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.
131 int 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 }