blob: 1e259e8926c57907ed752a19f830e058920265a8 [file] [log] [blame]
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01001/**
Ed Tanousfedd4572024-07-12 13:56:00 -07002 * Describes utility functions for parsing CPER into JSON IR.
3 *
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01004 * Author: Lawrence.Tang@arm.com
5 **/
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>
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010011
12//The available severity types for CPER.
Lawrence Tange407b4c2022-07-21 13:54:01 +010013const char *CPER_SEVERITY_TYPES[4] = { "Recoverable", "Fatal", "Corrected",
14 "Informational" };
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010015
Lawrence Tanga0865e32022-07-06 11:59:52 +010016//Converts the given generic CPER error status to JSON IR.
Lawrence Tange407b4c2022-07-21 13:54:01 +010017json_object *
18cper_generic_error_status_to_ir(EFI_GENERIC_ERROR_STATUS *error_status)
Lawrence Tanga0865e32022-07-06 11:59:52 +010019{
Lawrence Tange407b4c2022-07-21 13:54:01 +010020 json_object *error_status_ir = json_object_new_object();
Lawrence Tanga0865e32022-07-06 11:59:52 +010021
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 //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)"));
Lawrence Tanga0865e32022-07-06 11:59:52 +010030
Lawrence Tange407b4c2022-07-21 13:54:01 +010031 //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;
Lawrence Tanga0865e32022-07-06 11:59:52 +010055}
56
Lawrence Tang3b7f45b2022-07-14 14:14:30 +010057//Converts the given CPER-JSON generic error status into a CPER structure.
Lawrence Tange407b4c2022-07-21 13:54:01 +010058void ir_generic_error_status_to_cper(
59 json_object *error_status, EFI_GENERIC_ERROR_STATUS *error_status_cper)
Lawrence Tang3b7f45b2022-07-14 14:14:30 +010060{
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 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"));
Lawrence Tang3b7f45b2022-07-14 14:14:30 +010077}
78
Lawrence Tang7f21db62022-07-06 11:09:39 +010079//Converts a single uniform struct of UINT64s into intermediate JSON IR format, given names for each field in byte order.
Lawrence Tange407b4c2022-07-21 13:54:01 +010080json_object *uniform_struct64_to_ir(UINT64 *start, int len, const char *names[])
Lawrence Tang7f21db62022-07-06 11:09:39 +010081{
Lawrence Tange407b4c2022-07-21 13:54:01 +010082 json_object *result = json_object_new_object();
Lawrence Tang7f21db62022-07-06 11:09:39 +010083
Lawrence Tange407b4c2022-07-21 13:54:01 +010084 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 }
Lawrence Tang7f21db62022-07-06 11:09:39 +010090
Lawrence Tange407b4c2022-07-21 13:54:01 +010091 return result;
Lawrence Tang7f21db62022-07-06 11:09:39 +010092}
93
94//Converts a single uniform struct of UINT32s into intermediate JSON IR format, given names for each field in byte order.
Lawrence Tange407b4c2022-07-21 13:54:01 +010095json_object *uniform_struct_to_ir(UINT32 *start, int len, const char *names[])
Lawrence Tang7f21db62022-07-06 11:09:39 +010096{
Lawrence Tange407b4c2022-07-21 13:54:01 +010097 json_object *result = json_object_new_object();
Lawrence Tang7f21db62022-07-06 11:09:39 +010098
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 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 }
Lawrence Tang7f21db62022-07-06 11:09:39 +0100105
Lawrence Tange407b4c2022-07-21 13:54:01 +0100106 return result;
Lawrence Tang7f21db62022-07-06 11:09:39 +0100107}
108
Lawrence Tang71570a22022-07-14 11:45:28 +0100109//Converts a single object containing UINT32s into a uniform struct.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100110void ir_to_uniform_struct64(json_object *ir, UINT64 *start, int len,
111 const char *names[])
Lawrence Tang71570a22022-07-14 11:45:28 +0100112{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100113 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 }
Lawrence Tang71570a22022-07-14 11:45:28 +0100119}
120
121//Converts a single object containing UINT32s into a uniform struct.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100122void ir_to_uniform_struct(json_object *ir, UINT32 *start, int len,
123 const char *names[])
Lawrence Tang71570a22022-07-14 11:45:28 +0100124{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100125 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 }
Lawrence Tang71570a22022-07-14 11:45:28 +0100131}
132
Lawrence Tang3c43f742022-07-05 11:37:17 +0100133//Converts a single integer value to an object containing a value, and a readable name if possible.
Ed Tanousb35d9572024-06-18 13:17:22 -0700134json_object *integer_to_readable_pair(UINT64 value, int len, const int keys[],
Lawrence Tange407b4c2022-07-21 13:54:01 +0100135 const char *values[],
136 const char *default_value)
Lawrence Tang3c43f742022-07-05 11:37:17 +0100137{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100138 json_object *result = json_object_new_object();
139 json_object_object_add(result, "value", json_object_new_uint64(value));
Lawrence Tang3c43f742022-07-05 11:37:17 +0100140
Lawrence Tange407b4c2022-07-21 13:54:01 +0100141 //Search for human readable name, add.
142 const char *name = default_value;
143 for (int i = 0; i < len; i++) {
John Chungf8fc7052024-05-03 20:05:29 +0800144 if ((UINT64)keys[i] == value) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100145 name = values[i];
John Chungf8fc7052024-05-03 20:05:29 +0800146 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100147 }
Lawrence Tang3c43f742022-07-05 11:37:17 +0100148
Lawrence Tange407b4c2022-07-21 13:54:01 +0100149 json_object_object_add(result, "name", json_object_new_string(name));
150 return result;
Lawrence Tang3c43f742022-07-05 11:37:17 +0100151}
152
Lawrence Tang7f21db62022-07-06 11:09:39 +0100153//Converts a single integer value to an object containing a value, readable name and description if possible.
Ed Tanousb35d9572024-06-18 13:17:22 -0700154json_object *integer_to_readable_pair_with_desc(int value, int len,
155 const int keys[],
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 const char *values[],
157 const char *descriptions[],
158 const char *default_value)
Lawrence Tang7f21db62022-07-06 11:09:39 +0100159{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100160 json_object *result = json_object_new_object();
161 json_object_object_add(result, "value", json_object_new_int(value));
Lawrence Tang7f21db62022-07-06 11:09:39 +0100162
Lawrence Tange407b4c2022-07-21 13:54:01 +0100163 //Search for human readable name, add.
164 const char *name = default_value;
165 for (int i = 0; i < len; i++) {
166 if (keys[i] == value) {
167 name = values[i];
168 json_object_object_add(
169 result, "description",
170 json_object_new_string(descriptions[i]));
171 }
172 }
Lawrence Tang7f21db62022-07-06 11:09:39 +0100173
Lawrence Tange407b4c2022-07-21 13:54:01 +0100174 json_object_object_add(result, "name", json_object_new_string(name));
175 return result;
Lawrence Tang7f21db62022-07-06 11:09:39 +0100176}
177
Lawrence Tangb44314c2022-07-13 11:45:22 +0100178//Returns a single UINT64 value from the given readable pair object.
179//Assumes the integer value is held in the "value" field.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100180UINT64 readable_pair_to_integer(json_object *pair)
Lawrence Tangb44314c2022-07-13 11:45:22 +0100181{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100182 return json_object_get_uint64(json_object_object_get(pair, "value"));
Lawrence Tangb44314c2022-07-13 11:45:22 +0100183}
184
Lawrence Tang794312c2022-07-05 14:46:10 +0100185//Converts the given 64 bit bitfield to IR, assuming bit 0 starts on the left.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100186json_object *bitfield_to_ir(UINT64 bitfield, int num_fields,
187 const char *names[])
Lawrence Tang794312c2022-07-05 14:46:10 +0100188{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100189 json_object *result = json_object_new_object();
190 for (int i = 0; i < num_fields; i++) {
191 json_object_object_add(result, names[i],
192 json_object_new_boolean((bitfield >> i) &
John Chungf8fc7052024-05-03 20:05:29 +0800193 0x1));
Lawrence Tange407b4c2022-07-21 13:54:01 +0100194 }
Lawrence Tang794312c2022-07-05 14:46:10 +0100195
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 return result;
Lawrence Tang794312c2022-07-05 14:46:10 +0100197}
198
Lawrence Tangb44314c2022-07-13 11:45:22 +0100199//Converts the given IR bitfield into a standard UINT64 bitfield, with fields beginning from bit 0.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100200UINT64 ir_to_bitfield(json_object *ir, int num_fields, const char *names[])
Lawrence Tangb44314c2022-07-13 11:45:22 +0100201{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100202 UINT64 result = 0x0;
203 for (int i = 0; i < num_fields; i++) {
204 if (json_object_get_boolean(
John Chungf8fc7052024-05-03 20:05:29 +0800205 json_object_object_get(ir, names[i]))) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100206 result |= (0x1 << i);
John Chungf8fc7052024-05-03 20:05:29 +0800207 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100208 }
Lawrence Tangb44314c2022-07-13 11:45:22 +0100209
Lawrence Tange407b4c2022-07-21 13:54:01 +0100210 return result;
Lawrence Tangb44314c2022-07-13 11:45:22 +0100211}
212
Lawrence Tange18aaee2022-07-07 09:01:30 +0100213//Converts the given UINT64 array into a JSON IR array, given the length.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100214json_object *uint64_array_to_ir_array(UINT64 *array, int len)
Lawrence Tange18aaee2022-07-07 09:01:30 +0100215{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100216 json_object *array_ir = json_object_new_array();
John Chungf8fc7052024-05-03 20:05:29 +0800217 for (int i = 0; i < len; i++) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100218 json_object_array_add(array_ir,
219 json_object_new_uint64(array[i]));
John Chungf8fc7052024-05-03 20:05:29 +0800220 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100221 return array_ir;
Lawrence Tange18aaee2022-07-07 09:01:30 +0100222}
Lawrence Tang794312c2022-07-05 14:46:10 +0100223
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100224//Converts a single UINT16 revision number into JSON IR representation.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100225json_object *revision_to_ir(UINT16 revision)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100226{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100227 json_object *revision_info = json_object_new_object();
228 json_object_object_add(revision_info, "major",
229 json_object_new_int(revision >> 8));
230 json_object_object_add(revision_info, "minor",
231 json_object_new_int(revision & 0xFF));
232 return revision_info;
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100233}
234
235//Returns the appropriate string for the given integer severity.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100236const char *severity_to_string(UINT32 severity)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100237{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100238 return severity < 4 ? CPER_SEVERITY_TYPES[severity] : "Unknown";
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100239}
240
Lawrence Tangb44314c2022-07-13 11:45:22 +0100241//Converts a single EFI timestamp to string, at the given output.
242//Output must be at least TIMESTAMP_LENGTH bytes long.
Ed Tanous13f099f2024-11-20 11:10:30 -0800243void timestamp_to_string(char *out, int out_len,
244 EFI_ERROR_TIME_STAMP *timestamp)
Lawrence Tangb44314c2022-07-13 11:45:22 +0100245{
Ed Tanous13f099f2024-11-20 11:10:30 -0800246 int written = snprintf(
247 out, out_len,
248 "%02hhu%02hhu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu+00:00",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100249 bcd_to_int(timestamp->Century) %
John Chungf8fc7052024-05-03 20:05:29 +0800250 100, //Cannot go to three digits.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100251 bcd_to_int(timestamp->Year) % 100, //Cannot go to three digits.
252 bcd_to_int(timestamp->Month), bcd_to_int(timestamp->Day),
253 bcd_to_int(timestamp->Hours), bcd_to_int(timestamp->Minutes),
254 bcd_to_int(timestamp->Seconds));
Ed Tanous13f099f2024-11-20 11:10:30 -0800255
256 if (written < 0 || written >= out_len) {
257 printf("Timestamp buffer of insufficient size\n");
258 }
Lawrence Tangb44314c2022-07-13 11:45:22 +0100259}
260
261//Converts a single timestamp string to an EFI timestamp.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100262void string_to_timestamp(EFI_ERROR_TIME_STAMP *out, const char *timestamp)
Lawrence Tangb44314c2022-07-13 11:45:22 +0100263{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100264 //Ignore invalid timestamps.
John Chungf8fc7052024-05-03 20:05:29 +0800265 if (timestamp == NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100266 return;
John Chungf8fc7052024-05-03 20:05:29 +0800267 }
Lawrence Tang0cb33792022-07-13 13:51:39 +0100268
Aushim Nagarkattide7dd062024-11-04 15:23:39 -0800269 sscanf(timestamp, "%2hhu%2hhu-%hhu-%hhuT%hhu:%hhu:%hhu+00:00",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100270 &out->Century, &out->Year, &out->Month, &out->Day, &out->Hours,
271 &out->Minutes, &out->Seconds);
Lawrence Tangaacf0e22022-07-20 13:28:52 +0100272
Lawrence Tange407b4c2022-07-21 13:54:01 +0100273 //Convert back to BCD.
274 out->Century = int_to_bcd(out->Century);
275 out->Year = int_to_bcd(out->Year);
276 out->Month = int_to_bcd(out->Month);
277 out->Day = int_to_bcd(out->Day);
278 out->Hours = int_to_bcd(out->Hours);
279 out->Minutes = int_to_bcd(out->Minutes);
280 out->Seconds = int_to_bcd(out->Seconds);
Lawrence Tangb44314c2022-07-13 11:45:22 +0100281}
282
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100283//Helper function to convert an EDK EFI GUID into a string for intermediate use.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100284void guid_to_string(char *out, EFI_GUID *guid)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100285{
Karthik Rajagopalan7623a6e2024-09-20 19:24:35 -0700286 sprintf(out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100287 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
288 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
289 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100290}
291
Lawrence Tangb44314c2022-07-13 11:45:22 +0100292//Helper function to convert a string into an EDK EFI GUID.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100293void string_to_guid(EFI_GUID *out, const char *guid)
Lawrence Tangb44314c2022-07-13 11:45:22 +0100294{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100295 //Ignore invalid GUIDs.
John Chungf8fc7052024-05-03 20:05:29 +0800296 if (guid == NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100297 return;
John Chungf8fc7052024-05-03 20:05:29 +0800298 }
Lawrence Tang0cb33792022-07-13 13:51:39 +0100299
Lawrence Tange407b4c2022-07-21 13:54:01 +0100300 sscanf(guid,
Karthik Rajagopalan7623a6e2024-09-20 19:24:35 -0700301 "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
Lawrence Tange407b4c2022-07-21 13:54:01 +0100302 &out->Data1, &out->Data2, &out->Data3, out->Data4,
303 out->Data4 + 1, out->Data4 + 2, out->Data4 + 3, out->Data4 + 4,
304 out->Data4 + 5, out->Data4 + 6, out->Data4 + 7);
Lawrence Tangb44314c2022-07-13 11:45:22 +0100305}
306
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100307//Returns one if two EFI GUIDs are equal, zero otherwise.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100308int guid_equal(EFI_GUID *a, EFI_GUID *b)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100309{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100310 //Check top base 3 components.
311 if (a->Data1 != b->Data1 || a->Data2 != b->Data2 ||
312 a->Data3 != b->Data3) {
313 return 0;
314 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100315
Lawrence Tange407b4c2022-07-21 13:54:01 +0100316 //Check Data4 array for equality.
317 for (int i = 0; i < 8; i++) {
John Chungf8fc7052024-05-03 20:05:29 +0800318 if (a->Data4[i] != b->Data4[i]) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100319 return 0;
John Chungf8fc7052024-05-03 20:05:29 +0800320 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100321 }
Lawrence Tang1b0b00e2022-07-05 10:33:10 +0100322
Lawrence Tange407b4c2022-07-21 13:54:01 +0100323 return 1;
John Chungf8fc7052024-05-03 20:05:29 +0800324}