forked from openbmc/libcper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cper-parse.c
424 lines (366 loc) · 14.7 KB
/
cper-parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* Describes high level functions for converting an entire CPER log, and functions for parsing
* CPER headers and section descriptions into an intermediate JSON format.
*
* Author: [email protected]
**/
#include <stdio.h>
#include <string.h>
#include <json.h>
#include "base64.h"
#include "edk/Cper.h"
#include "cper-parse.h"
#include "cper-parse-str.h"
#include "cper-utils.h"
#include "sections/cper-section.h"
//Private pre-definitions.
json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header);
json_object *
cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor);
json_object *cper_section_to_ir(FILE *handle, long base_pos,
EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
//Reads a CPER log file at the given file location, and returns an intermediate
//JSON representation of this CPER record.
json_object *cper_to_ir(FILE *cper_file)
{
//Read the current file pointer location as the base of the record.
long base_pos = ftell(cper_file);
//Ensure this is really a CPER log.
EFI_COMMON_ERROR_RECORD_HEADER header;
if (fread(&header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1,
cper_file) != 1) {
printf("Invalid CPER file: Invalid length (log too short).\n");
return NULL;
}
//Check if the header contains the magic bytes ("CPER").
if (header.SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
printf("Invalid CPER file: Invalid header (incorrect signature).\n");
return NULL;
}
//Create the header JSON object from the read bytes.
json_object *header_ir = cper_header_to_ir(&header);
//Read the appropriate number of section descriptors & sections, and convert them into IR format.
json_object *section_descriptors_ir = json_object_new_array();
json_object *sections_ir = json_object_new_array();
for (int i = 0; i < header.SectionCount; i++) {
//Create the section descriptor.
EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
if (fread(§ion_descriptor,
sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
cper_file) != 1) {
printf("Invalid number of section headers: Header states %d sections, could not read section %d.\n",
header.SectionCount, i + 1);
// Free json objects
json_object_put(sections_ir);
json_object_put(section_descriptors_ir);
json_object_put(header_ir);
return NULL;
}
json_object_array_add(
section_descriptors_ir,
cper_section_descriptor_to_ir(§ion_descriptor));
//Read the section itself.
json_object_array_add(sections_ir,
cper_section_to_ir(cper_file, base_pos,
§ion_descriptor));
}
//Add the header, section descriptors, and sections to a parent object.
json_object *parent = json_object_new_object();
json_object_object_add(parent, "header", header_ir);
json_object_object_add(parent, "sectionDescriptors",
section_descriptors_ir);
json_object_object_add(parent, "sections", sections_ir);
return parent;
}
char *cper_to_str_ir(FILE *cper_file)
{
json_object *jobj = cper_to_ir(cper_file);
char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
json_object_put(jobj);
return str;
}
char *cperbuf_to_str_ir(const unsigned char *cper, size_t size)
{
FILE *cper_file = fmemopen((void *)cper, size, "r");
return cper_file ? cper_to_str_ir(cper_file) : NULL;
}
//Converts a parsed CPER record header into intermediate JSON object format.
json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
{
json_object *header_ir = json_object_new_object();
//Revision/version information.
json_object_object_add(header_ir, "revision",
revision_to_ir(header->Revision));
//Section count.
json_object_object_add(header_ir, "sectionCount",
json_object_new_int(header->SectionCount));
//Error severity (with interpreted string version).
json_object *error_severity = json_object_new_object();
json_object_object_add(error_severity, "code",
json_object_new_uint64(header->ErrorSeverity));
json_object_object_add(error_severity, "name",
json_object_new_string(severity_to_string(
header->ErrorSeverity)));
json_object_object_add(header_ir, "severity", error_severity);
//The validation bits for each section.
json_object *validation_bits = bitfield_to_ir(
header->ValidationBits, 3, CPER_HEADER_VALID_BITFIELD_NAMES);
json_object_object_add(header_ir, "validationBits", validation_bits);
//Total length of the record (including headers) in bytes.
json_object_object_add(header_ir, "recordLength",
json_object_new_uint64(header->RecordLength));
//If a timestamp exists according to validation bits, then add it.
if (header->ValidationBits & 0x2) {
char timestamp_string[TIMESTAMP_LENGTH];
timestamp_to_string(timestamp_string, &header->TimeStamp);
json_object_object_add(
header_ir, "timestamp",
json_object_new_string(timestamp_string));
json_object_object_add(
header_ir, "timestampIsPrecise",
json_object_new_boolean(header->TimeStamp.Flag));
}
//If a platform ID exists according to the validation bits, then add it.
if (header->ValidationBits & 0x1) {
char platform_string[GUID_STRING_LENGTH];
guid_to_string(platform_string, &header->PlatformID);
json_object_object_add(header_ir, "platformID",
json_object_new_string(platform_string));
}
//If a partition ID exists according to the validation bits, then add it.
if (header->ValidationBits & 0x4) {
char partition_string[GUID_STRING_LENGTH];
guid_to_string(partition_string, &header->PartitionID);
json_object_object_add(
header_ir, "partitionID",
json_object_new_string(partition_string));
}
//Creator ID of the header.
char creator_string[GUID_STRING_LENGTH];
guid_to_string(creator_string, &header->CreatorID);
json_object_object_add(header_ir, "creatorID",
json_object_new_string(creator_string));
//Notification type for the header. Some defined types are available.
json_object *notification_type = json_object_new_object();
char notification_type_string[GUID_STRING_LENGTH];
guid_to_string(notification_type_string, &header->NotificationType);
json_object_object_add(
notification_type, "guid",
json_object_new_string(notification_type_string));
//Add the human readable notification type if possible.
char *notification_type_readable = "Unknown";
if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeCmcGuid)) {
notification_type_readable = "CMC";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeCpeGuid)) {
notification_type_readable = "CPE";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeMceGuid)) {
notification_type_readable = "MCE";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypePcieGuid)) {
notification_type_readable = "PCIe";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeInitGuid)) {
notification_type_readable = "INIT";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeNmiGuid)) {
notification_type_readable = "NMI";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeBootGuid)) {
notification_type_readable = "Boot";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeDmarGuid)) {
notification_type_readable = "DMAr";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeSeaGuid)) {
notification_type_readable = "SEA";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeSeiGuid)) {
notification_type_readable = "SEI";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypePeiGuid)) {
notification_type_readable = "PEI";
} else if (guid_equal(&header->NotificationType,
&gEfiEventNotificationTypeCxlGuid)) {
notification_type_readable = "CXL Component";
}
json_object_object_add(
notification_type, "type",
json_object_new_string(notification_type_readable));
json_object_object_add(header_ir, "notificationType",
notification_type);
//The record ID for this record, unique on a given system.
json_object_object_add(header_ir, "recordID",
json_object_new_uint64(header->RecordID));
//Flag for the record, and a human readable form.
json_object *flags = integer_to_readable_pair(
header->Flags,
sizeof(CPER_HEADER_FLAG_TYPES_KEYS) / sizeof(int),
CPER_HEADER_FLAG_TYPES_KEYS, CPER_HEADER_FLAG_TYPES_VALUES,
"Unknown");
json_object_object_add(header_ir, "flags", flags);
//Persistence information. Outside the scope of specification, so just a uint32 here.
json_object_object_add(header_ir, "persistenceInfo",
json_object_new_uint64(header->PersistenceInfo));
return header_ir;
}
//Converts the given EFI section descriptor into JSON IR format.
json_object *
cper_section_descriptor_to_ir(EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor)
{
json_object *section_descriptor_ir = json_object_new_object();
//The offset of the section from the base of the record header, length.
json_object_object_add(
section_descriptor_ir, "sectionOffset",
json_object_new_uint64(section_descriptor->SectionOffset));
json_object_object_add(
section_descriptor_ir, "sectionLength",
json_object_new_uint64(section_descriptor->SectionLength));
//Revision.
json_object_object_add(section_descriptor_ir, "revision",
revision_to_ir(section_descriptor->Revision));
//Validation bits.
json_object *validation_bits =
bitfield_to_ir(section_descriptor->SecValidMask, 2,
CPER_SECTION_DESCRIPTOR_VALID_BITFIELD_NAMES);
json_object_object_add(section_descriptor_ir, "validationBits",
validation_bits);
//Flag bits.
json_object *flags =
bitfield_to_ir(section_descriptor->SectionFlags, 8,
CPER_SECTION_DESCRIPTOR_FLAGS_BITFIELD_NAMES);
json_object_object_add(section_descriptor_ir, "flags", flags);
//Section type (GUID).
json_object *section_type = json_object_new_object();
char section_type_string[GUID_STRING_LENGTH];
guid_to_string(section_type_string, §ion_descriptor->SectionType);
json_object_object_add(section_type, "data",
json_object_new_string(section_type_string));
//Readable section type, if possible.
const char *section_type_readable = "Unknown";
for (size_t i = 0; i < section_definitions_len; i++) {
if (guid_equal(section_definitions[i].Guid,
§ion_descriptor->SectionType)) {
section_type_readable =
section_definitions[i].ReadableName;
break;
}
}
json_object_object_add(section_type, "type",
json_object_new_string(section_type_readable));
json_object_object_add(section_descriptor_ir, "sectionType",
section_type);
//If validation bits indicate it exists, add FRU ID.
if (section_descriptor->SecValidMask & 0x1) {
char fru_id_string[GUID_STRING_LENGTH];
guid_to_string(fru_id_string, §ion_descriptor->FruId);
json_object_object_add(section_descriptor_ir, "fruID",
json_object_new_string(fru_id_string));
}
//If validation bits indicate it exists, add FRU text.
if ((section_descriptor->SecValidMask & 0x2) >> 1) {
json_object_object_add(
section_descriptor_ir, "fruText",
json_object_new_string(section_descriptor->FruString));
}
//Section severity.
json_object *section_severity = json_object_new_object();
json_object_object_add(
section_severity, "code",
json_object_new_uint64(section_descriptor->Severity));
json_object_object_add(section_severity, "name",
json_object_new_string(severity_to_string(
section_descriptor->Severity)));
json_object_object_add(section_descriptor_ir, "severity",
section_severity);
return section_descriptor_ir;
}
//Converts the section described by a single given section descriptor.
json_object *cper_section_to_ir(FILE *handle, long base_pos,
EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
{
//Save our current position in the stream.
long position = ftell(handle);
//Read section as described by the section descriptor.
fseek(handle, base_pos + descriptor->SectionOffset, SEEK_SET);
void *section = malloc(descriptor->SectionLength);
if (fread(section, descriptor->SectionLength, 1, handle) != 1) {
printf("Section read failed: Could not read %u bytes from global offset %d.\n",
descriptor->SectionLength, descriptor->SectionOffset);
free(section);
return NULL;
}
//Seek back to our original position.
fseek(handle, position, SEEK_SET);
//Parse section to IR based on GUID.
json_object *result = NULL;
int section_converted = 0;
for (size_t i = 0; i < section_definitions_len; i++) {
if (guid_equal(section_definitions[i].Guid,
&descriptor->SectionType) &&
section_definitions[i].ToIR != NULL) {
result = section_definitions[i].ToIR(section);
section_converted = 1;
break;
}
}
//Was it an unknown GUID/failed read?
if (!section_converted) {
//Output the data as formatted base64.
result = json_object_new_object();
int32_t encoded_len = 0;
char *encoded = base64_encode(
section, descriptor->SectionLength, &encoded_len);
if (encoded == NULL) {
printf("Failed to allocate encode output buffer. \n");
} else {
json_object_object_add(result, "data",
json_object_new_string_len(
encoded, encoded_len));
free(encoded);
}
}
//Free section memory, return result.
free(section);
return result;
}
//Converts a single CPER section, without a header but with a section descriptor, to JSON.
json_object *cper_single_section_to_ir(FILE *cper_section_file)
{
json_object *ir = json_object_new_object();
//Read the current file pointer location as base record position.
long base_pos = ftell(cper_section_file);
//Read the section descriptor out.
EFI_ERROR_SECTION_DESCRIPTOR section_descriptor;
if (fread(§ion_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1,
cper_section_file) != 1) {
printf("Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
return NULL;
}
//Convert the section descriptor to IR.
json_object *section_descriptor_ir =
cper_section_descriptor_to_ir(§ion_descriptor);
json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
//Parse the single section.
json_object *section_ir = cper_section_to_ir(
cper_section_file, base_pos, §ion_descriptor);
json_object_object_add(ir, "section", section_ir);
return ir;
}
char *cper_single_section_to_str_ir(FILE *cper_section_file)
{
json_object *jobj = cper_single_section_to_ir(cper_section_file);
char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
json_object_put(jobj);
return str;
}
char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section,
size_t size)
{
FILE *cper_section_file = fmemopen((void *)cper_section, size, "r");
return cper_section_file ?
cper_single_section_to_str_ir(cper_section_file) :
NULL;
}