xref: /openbmc/libcper/cper-utils.c (revision f8fc7052)
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",
14 				       "Informational" };
15 
16 //Converts the given generic CPER error status to JSON IR.
17 json_object *
18 cper_generic_error_status_to_ir(EFI_GENERIC_ERROR_STATUS *error_status)
19 {
20 	json_object *error_status_ir = json_object_new_object();
21 
22 	//Error type.
23 	json_object_object_add(error_status_ir, "errorType",
24 			       integer_to_readable_pair_with_desc(
25 				       error_status->Type, 18,
26 				       CPER_GENERIC_ERROR_TYPES_KEYS,
27 				       CPER_GENERIC_ERROR_TYPES_VALUES,
28 				       CPER_GENERIC_ERROR_TYPES_DESCRIPTIONS,
29 				       "Unknown (Reserved)"));
30 
31 	//Boolean bit fields.
32 	json_object_object_add(
33 		error_status_ir, "addressSignal",
34 		json_object_new_boolean(error_status->AddressSignal));
35 	json_object_object_add(
36 		error_status_ir, "controlSignal",
37 		json_object_new_boolean(error_status->ControlSignal));
38 	json_object_object_add(
39 		error_status_ir, "dataSignal",
40 		json_object_new_boolean(error_status->DataSignal));
41 	json_object_object_add(
42 		error_status_ir, "detectedByResponder",
43 		json_object_new_boolean(error_status->DetectedByResponder));
44 	json_object_object_add(
45 		error_status_ir, "detectedByRequester",
46 		json_object_new_boolean(error_status->DetectedByRequester));
47 	json_object_object_add(
48 		error_status_ir, "firstError",
49 		json_object_new_boolean(error_status->FirstError));
50 	json_object_object_add(
51 		error_status_ir, "overflowDroppedLogs",
52 		json_object_new_boolean(error_status->OverflowNotLogged));
53 
54 	return error_status_ir;
55 }
56 
57 //Converts the given CPER-JSON generic error status into a CPER structure.
58 void ir_generic_error_status_to_cper(
59 	json_object *error_status, EFI_GENERIC_ERROR_STATUS *error_status_cper)
60 {
61 	error_status_cper->Type = readable_pair_to_integer(
62 		json_object_object_get(error_status, "errorType"));
63 	error_status_cper->AddressSignal = json_object_get_boolean(
64 		json_object_object_get(error_status, "addressSignal"));
65 	error_status_cper->ControlSignal = json_object_get_boolean(
66 		json_object_object_get(error_status, "controlSignal"));
67 	error_status_cper->DataSignal = json_object_get_boolean(
68 		json_object_object_get(error_status, "dataSignal"));
69 	error_status_cper->DetectedByResponder = json_object_get_boolean(
70 		json_object_object_get(error_status, "detectedByResponder"));
71 	error_status_cper->DetectedByRequester = json_object_get_boolean(
72 		json_object_object_get(error_status, "detectedByRequester"));
73 	error_status_cper->FirstError = json_object_get_boolean(
74 		json_object_object_get(error_status, "firstError"));
75 	error_status_cper->OverflowNotLogged = json_object_get_boolean(
76 		json_object_object_get(error_status, "overflowDroppedLogs"));
77 }
78 
79 //Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order.
80 json_object *uniform_struct64_to_ir(UINT64 *start, int len, const char *names[])
81 {
82 	json_object *result = json_object_new_object();
83 
84 	UINT64 *cur = start;
85 	for (int i = 0; i < len; i++) {
86 		json_object_object_add(result, names[i],
87 				       json_object_new_uint64(*cur));
88 		cur++;
89 	}
90 
91 	return result;
92 }
93 
94 //Converts a single uniform struct of UINT32s into intermediate JSON IR format, given names for each field in byte order.
95 json_object *uniform_struct_to_ir(UINT32 *start, int len, const char *names[])
96 {
97 	json_object *result = json_object_new_object();
98 
99 	UINT32 *cur = start;
100 	for (int i = 0; i < len; i++) {
101 		json_object_object_add(result, names[i],
102 				       json_object_new_uint64(*cur));
103 		cur++;
104 	}
105 
106 	return result;
107 }
108 
109 //Converts a single object containing UINT32s into a uniform struct.
110 void ir_to_uniform_struct64(json_object *ir, UINT64 *start, int len,
111 			    const char *names[])
112 {
113 	UINT64 *cur = start;
114 	for (int i = 0; i < len; i++) {
115 		*cur = json_object_get_uint64(
116 			json_object_object_get(ir, names[i]));
117 		cur++;
118 	}
119 }
120 
121 //Converts a single object containing UINT32s into a uniform struct.
122 void ir_to_uniform_struct(json_object *ir, UINT32 *start, int len,
123 			  const char *names[])
124 {
125 	UINT32 *cur = start;
126 	for (int i = 0; i < len; i++) {
127 		*cur = (UINT32)json_object_get_uint64(
128 			json_object_object_get(ir, names[i]));
129 		cur++;
130 	}
131 }
132 
133 //Converts a single integer value to an object containing a value, and a readable name if possible.
134 json_object *integer_to_readable_pair(UINT64 value, int len, int keys[],
135 				      const char *values[],
136 				      const char *default_value)
137 {
138 	json_object *result = json_object_new_object();
139 	json_object_object_add(result, "value", json_object_new_uint64(value));
140 
141 	//Search for human readable name, add.
142 	const char *name = default_value;
143 	for (int i = 0; i < len; i++) {
144 		if ((UINT64)keys[i] == value) {
145 			name = values[i];
146 		}
147 	}
148 
149 	json_object_object_add(result, "name", json_object_new_string(name));
150 	return result;
151 }
152 
153 //Converts a single integer value to an object containing a value, readable name and description if possible.
154 json_object *integer_to_readable_pair_with_desc(int value, int len, int keys[],
155 						const char *values[],
156 						const char *descriptions[],
157 						const char *default_value)
158 {
159 	json_object *result = json_object_new_object();
160 	json_object_object_add(result, "value", json_object_new_int(value));
161 
162 	//Search for human readable name, add.
163 	const char *name = default_value;
164 	for (int i = 0; i < len; i++) {
165 		if (keys[i] == value) {
166 			name = values[i];
167 			json_object_object_add(
168 				result, "description",
169 				json_object_new_string(descriptions[i]));
170 		}
171 	}
172 
173 	json_object_object_add(result, "name", json_object_new_string(name));
174 	return result;
175 }
176 
177 //Returns a single UINT64 value from the given readable pair object.
178 //Assumes the integer value is held in the "value" field.
179 UINT64 readable_pair_to_integer(json_object *pair)
180 {
181 	return json_object_get_uint64(json_object_object_get(pair, "value"));
182 }
183 
184 //Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left.
185 json_object *bitfield_to_ir(UINT64 bitfield, int num_fields,
186 			    const char *names[])
187 {
188 	json_object *result = json_object_new_object();
189 	for (int i = 0; i < num_fields; i++) {
190 		json_object_object_add(result, names[i],
191 				       json_object_new_boolean((bitfield >> i) &
192 							       0x1));
193 	}
194 
195 	return result;
196 }
197 
198 //Converts the given IR bitfield into a standard UINT64 bitfield, with fields beginning from bit 0.
199 UINT64 ir_to_bitfield(json_object *ir, int num_fields, const char *names[])
200 {
201 	UINT64 result = 0x0;
202 	for (int i = 0; i < num_fields; i++) {
203 		if (json_object_get_boolean(
204 			    json_object_object_get(ir, names[i]))) {
205 			result |= (0x1 << i);
206 		}
207 	}
208 
209 	return result;
210 }
211 
212 //Converts the given UINT64 array into a JSON IR array, given the length.
213 json_object *uint64_array_to_ir_array(UINT64 *array, int len)
214 {
215 	json_object *array_ir = json_object_new_array();
216 	for (int i = 0; i < len; i++) {
217 		json_object_array_add(array_ir,
218 				      json_object_new_uint64(array[i]));
219 	}
220 	return array_ir;
221 }
222 
223 //Converts a single UINT16 revision number into JSON IR representation.
224 json_object *revision_to_ir(UINT16 revision)
225 {
226 	json_object *revision_info = json_object_new_object();
227 	json_object_object_add(revision_info, "major",
228 			       json_object_new_int(revision >> 8));
229 	json_object_object_add(revision_info, "minor",
230 			       json_object_new_int(revision & 0xFF));
231 	return revision_info;
232 }
233 
234 //Returns the appropriate string for the given integer severity.
235 const char *severity_to_string(UINT32 severity)
236 {
237 	return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown";
238 }
239 
240 //Converts a single EFI timestamp to string, at the given output.
241 //Output must be at least TIMESTAMP_LENGTH bytes long.
242 void timestamp_to_string(char *out, EFI_ERROR_TIME_STAMP *timestamp)
243 {
244 	sprintf(out, "%02hhu%02hhu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu.000",
245 		bcd_to_int(timestamp->Century) %
246 			100,			   //Cannot go to three digits.
247 		bcd_to_int(timestamp->Year) % 100, //Cannot go to three digits.
248 		bcd_to_int(timestamp->Month), bcd_to_int(timestamp->Day),
249 		bcd_to_int(timestamp->Hours), bcd_to_int(timestamp->Minutes),
250 		bcd_to_int(timestamp->Seconds));
251 }
252 
253 //Converts a single timestamp string to an EFI timestamp.
254 void string_to_timestamp(EFI_ERROR_TIME_STAMP *out, const char *timestamp)
255 {
256 	//Ignore invalid timestamps.
257 	if (timestamp == NULL) {
258 		return;
259 	}
260 
261 	sscanf(timestamp, "%2hhu%2hhu-%hhu-%hhuT%hhu:%hhu:%hhu.000",
262 	       &out->Century, &out->Year, &out->Month, &out->Day, &out->Hours,
263 	       &out->Minutes, &out->Seconds);
264 
265 	//Convert back to BCD.
266 	out->Century = int_to_bcd(out->Century);
267 	out->Year = int_to_bcd(out->Year);
268 	out->Month = int_to_bcd(out->Month);
269 	out->Day = int_to_bcd(out->Day);
270 	out->Hours = int_to_bcd(out->Hours);
271 	out->Minutes = int_to_bcd(out->Minutes);
272 	out->Seconds = int_to_bcd(out->Seconds);
273 }
274 
275 //Helper function to convert an EDK EFI GUID into a string for intermediate use.
276 void guid_to_string(char *out, EFI_GUID *guid)
277 {
278 	sprintf(out, "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
279 		guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
280 		guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
281 		guid->Data4[5], guid->Data4[6], guid->Data4[7]);
282 }
283 
284 //Helper function to convert a string into an EDK EFI GUID.
285 void string_to_guid(EFI_GUID *out, const char *guid)
286 {
287 	//Ignore invalid GUIDs.
288 	if (guid == NULL) {
289 		return;
290 	}
291 
292 	sscanf(guid,
293 	       "%08x-%04hx-%04hx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
294 	       &out->Data1, &out->Data2, &out->Data3, out->Data4,
295 	       out->Data4 + 1, out->Data4 + 2, out->Data4 + 3, out->Data4 + 4,
296 	       out->Data4 + 5, out->Data4 + 6, out->Data4 + 7);
297 }
298 
299 //Returns one if two EFI GUIDs are equal, zero otherwise.
300 int guid_equal(EFI_GUID *a, EFI_GUID *b)
301 {
302 	//Check top base 3 components.
303 	if (a->Data1 != b->Data1 || a->Data2 != b->Data2 ||
304 	    a->Data3 != b->Data3) {
305 		return 0;
306 	}
307 
308 	//Check Data4 array for equality.
309 	for (int i = 0; i < 8; i++) {
310 		if (a->Data4[i] != b->Data4[i]) {
311 			return 0;
312 		}
313 	}
314 
315 	return 1;
316 }
317