-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcper-parse.c
More file actions
581 lines (494 loc) · 18.1 KB
/
Copy pathcper-parse.c
File metadata and controls
581 lines (494 loc) · 18.1 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
* 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: Lawrence.Tang@arm.com
**/
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <json.h>
#include <libcper/base64.h>
#include <libcper/Cper.h>
#include <libcper/log.h>
#include <libcper/cper-parse.h>
#include <libcper/cper-parse-str.h>
#include <libcper/cper-utils.h>
#include <libcper/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_buf_section_to_ir(const void *cper_section_buf, size_t size,
EFI_ERROR_SECTION_DESCRIPTOR *descriptor);
static int header_signature_valid(EFI_COMMON_ERROR_RECORD_HEADER *header)
{
if (header->SignatureStart != EFI_ERROR_RECORD_SIGNATURE_START) {
cper_print_log(
"Invalid CPER file: Invalid header (incorrect signature). %x\n",
header->SignatureStart);
return 0;
}
if (header->SignatureEnd != EFI_ERROR_RECORD_SIGNATURE_END) {
cper_print_log(
"Invalid CPER file: Invalid header (incorrect signature end). %x\n",
header->SignatureEnd);
return 0;
}
if (header->SectionCount == 0) {
cper_print_log(
"Invalid CPER file: Invalid section count (0).\n");
return 0;
}
return 1;
}
int header_valid(const char *cper_buf, size_t size)
{
if (size < sizeof(EFI_COMMON_ERROR_RECORD_HEADER)) {
return 0;
}
EFI_COMMON_ERROR_RECORD_HEADER *header =
(EFI_COMMON_ERROR_RECORD_HEADER *)cper_buf;
if (!header_signature_valid(header)) {
return 0;
}
return header_signature_valid(header);
}
json_object *cper_buf_to_ir(const unsigned char *cper_buf, size_t size)
{
json_object *parent = NULL;
json_object *header_ir = NULL;
json_object *section_descriptors_ir = NULL;
json_object *sections_ir = NULL;
const unsigned char *pos = cper_buf;
unsigned int remaining = size;
if (size < sizeof(EFI_COMMON_ERROR_RECORD_HEADER)) {
goto fail;
}
EFI_COMMON_ERROR_RECORD_HEADER *header =
(EFI_COMMON_ERROR_RECORD_HEADER *)cper_buf;
if (!header_signature_valid(header)) {
goto fail;
}
pos += sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
remaining -= sizeof(EFI_COMMON_ERROR_RECORD_HEADER);
if (remaining < sizeof(EFI_ERROR_SECTION_DESCRIPTOR)) {
cper_print_log(
"Invalid CPER file: Invalid section descriptor (section offset + length > size).\n");
goto fail;
}
//Create the header JSON object from the read bytes.
parent = json_object_new_object();
header_ir = cper_header_to_ir(header);
json_object_object_add(parent, "header", header_ir);
//Read the appropriate number of section descriptors & sections, and convert them into IR format.
section_descriptors_ir = json_object_new_array();
sections_ir = json_object_new_array();
for (int i = 0; i < header->SectionCount; i++) {
//Create the section descriptor.
if (remaining < sizeof(EFI_ERROR_SECTION_DESCRIPTOR)) {
cper_print_log(
"Invalid number of section headers: Header states %d sections, could not read section %d.\n",
header->SectionCount, i + 1);
goto fail;
}
EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor;
section_descriptor = (EFI_ERROR_SECTION_DESCRIPTOR *)(pos);
pos += sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
remaining -= sizeof(EFI_ERROR_SECTION_DESCRIPTOR);
if (section_descriptor->SectionOffset > size) {
cper_print_log(
"Invalid section descriptor: Section offset > size.\n");
goto fail;
}
if (section_descriptor->SectionLength <= 0) {
cper_print_log(
"Invalid section descriptor: Section length <= 0.\n");
goto fail;
}
if (section_descriptor->SectionOffset >
UINT_MAX - section_descriptor->SectionLength) {
cper_print_log(
"Invalid section descriptor: Section offset + length would overflow.\n");
goto fail;
}
if (section_descriptor->SectionOffset +
section_descriptor->SectionLength >
size) {
cper_print_log(
"Invalid section descriptor: Section offset + length > size.\n");
goto fail;
}
const unsigned char *section_begin =
cper_buf + section_descriptor->SectionOffset;
json_object *section_descriptor_ir =
cper_section_descriptor_to_ir(section_descriptor);
json_object_array_add(section_descriptors_ir,
section_descriptor_ir);
//Read the section itself.
json_object *section_ir = cper_buf_section_to_ir(
section_begin, section_descriptor->SectionLength,
section_descriptor);
json_object_array_add(sections_ir, section_ir);
}
//Add the header, section descriptors, and sections to a parent object.
json_object_object_add(parent, "sectionDescriptors",
section_descriptors_ir);
json_object_object_add(parent, "sections", sections_ir);
return parent;
fail:
json_object_put(sections_ir);
json_object_put(section_descriptors_ir);
json_object_put(parent);
cper_print_log("Failed to parse CPER file.\n");
return NULL;
}
//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)
{
//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) {
cper_print_log(
"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) {
cper_print_log(
"Invalid CPER file: Invalid header (incorrect signature).\n");
return NULL;
}
fseek(cper_file, -sizeof(EFI_COMMON_ERROR_RECORD_HEADER), SEEK_CUR);
unsigned char *cper_buf = malloc(header.RecordLength);
int bytes_read = fread(cper_buf, 1, header.RecordLength, cper_file);
if (bytes_read < 0) {
cper_print_log("File read failed with code %u\n", bytes_read);
free(cper_buf);
return NULL;
}
if ((UINT32)bytes_read != header.RecordLength) {
int position = ftell(cper_file);
cper_print_log(
"File read failed file was %u bytes, expecting %u bytes from header.\n",
position, header.RecordLength);
free(cper_buf);
return NULL;
}
json_object *ir = cper_buf_to_ir(cper_buf, bytes_read);
free(cper_buf);
return ir;
}
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 *revision = revision_to_ir(header->Revision);
json_object_object_add(header_ir, "revision", revision);
//Section count.
add_int(header_ir, "sectionCount", header->SectionCount);
//Error severity (with interpreted string version).
json_object *error_severity = json_object_new_object();
add_uint(error_severity, "code", header->ErrorSeverity);
const char *severity_name = severity_to_string(header->ErrorSeverity);
add_string(error_severity, "name", severity_name);
json_object_object_add(header_ir, "severity", error_severity);
//Total length of the record (including headers) in bytes.
add_uint(header_ir, "recordLength", header->RecordLength);
//If a timestamp exists according to validation bits, then add it.
if (header->ValidationBits & 0x2) {
char timestamp_string[TIMESTAMP_LENGTH];
if (timestamp_to_string(timestamp_string, TIMESTAMP_LENGTH,
&header->TimeStamp) >= 0) {
add_string(header_ir, "timestamp", timestamp_string);
add_bool(header_ir, "timestampIsPrecise",
header->TimeStamp.Flag);
}
}
//If a platform ID exists according to the validation bits, then add it.
if (header->ValidationBits & 0x1) {
add_guid(header_ir, "platformID", &header->PlatformID);
}
//If a partition ID exists according to the validation bits, then add it.
if (header->ValidationBits & 0x4) {
add_guid(header_ir, "partitionID", &header->PartitionID);
}
//Creator ID of the header.
add_guid(header_ir, "creatorID", &header->CreatorID);
//Notification type for the header. Some defined types are available.
json_object *notification_type = json_object_new_object();
add_guid(notification_type, "guid", &header->NotificationType);
//Add the human readable notification type if possible.
const char *notification_type_readable = "Unknown";
EFI_GUID *guids[] = {
&gEfiEventNotificationTypeCmcGuid,
&gEfiEventNotificationTypeCpeGuid,
&gEfiEventNotificationTypeMceGuid,
&gEfiEventNotificationTypePcieGuid,
&gEfiEventNotificationTypeInitGuid,
&gEfiEventNotificationTypeNmiGuid,
&gEfiEventNotificationTypeBootGuid,
&gEfiEventNotificationTypeDmarGuid,
&gEfiEventNotificationTypeSeaGuid,
&gEfiEventNotificationTypeSeiGuid,
&gEfiEventNotificationTypePeiGuid,
&gEfiEventNotificationTypeCxlGuid,
};
const char *readable_names[] = {
"CMC", "CPE", "MCE", "PCIe", "INIT", "NMI",
"Boot", "DMAr", "SEA", "SEI", "PEI", "CXL Component"
};
int index = select_guid_from_list(&header->NotificationType, guids,
sizeof(guids) / sizeof(EFI_GUID *));
if (index < (int)(sizeof(readable_names) / sizeof(char *))) {
notification_type_readable = readable_names[index];
}
add_string(notification_type, "type", notification_type_readable);
json_object_object_add(header_ir, "notificationType",
notification_type);
//The record ID for this record, unique on a given system.
add_uint(header_ir, "recordID", 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.
add_uint(header_ir, "persistenceInfo", 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.
add_uint(section_descriptor_ir, "sectionOffset",
section_descriptor->SectionOffset);
add_uint(section_descriptor_ir, "sectionLength",
section_descriptor->SectionLength);
//Revision.
json_object *revision = revision_to_ir(section_descriptor->Revision);
json_object_object_add(section_descriptor_ir, "revision", revision);
//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();
add_guid(section_type, "data", §ion_descriptor->SectionType);
//Readable section type, if possible.
const char *section_type_readable = "Unknown";
CPER_SECTION_DEFINITION *section =
select_section_by_guid(§ion_descriptor->SectionType);
if (section != NULL) {
section_type_readable = section->ReadableName;
}
add_string(section_type, "type", 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) {
add_guid(section_descriptor_ir, "fruID",
§ion_descriptor->FruId);
}
//If validation bits indicate it exists, add FRU text.
if ((section_descriptor->SecValidMask & 0x2) >> 1) {
add_untrusted_string(section_descriptor_ir, "fruText",
section_descriptor->FruString,
sizeof(section_descriptor->FruString));
}
//Section severity.
json_object *section_severity = json_object_new_object();
add_uint(section_severity, "code", section_descriptor->Severity);
add_string(section_severity, "name",
severity_to_string(section_descriptor->Severity));
json_object_object_add(section_descriptor_ir, "severity",
section_severity);
return section_descriptor_ir;
}
json_object *read_section(const unsigned char *cper_section_buf, size_t size,
CPER_SECTION_DEFINITION *definition)
{
if (definition->ToIR == NULL) {
return NULL;
}
char *cper_description = NULL;
json_object *section_ir =
definition->ToIR(cper_section_buf, size, &cper_description);
if (section_ir == NULL) {
free(cper_description);
return NULL;
}
json_object *result = json_object_new_object();
if (cper_description != NULL) {
add_string(result, "message", cper_description);
free(cper_description);
}
json_object_object_add(result, definition->ShortName, section_ir);
return result;
}
CPER_SECTION_DEFINITION *select_section_by_guid(EFI_GUID *guid)
{
size_t i = 0;
for (; i < section_definitions_len; i++) {
if (guid_equal(guid, section_definitions[i].Guid)) {
break;
}
}
// It's unlikely fuzzing can reliably come up with a correct guid, given how
// much entropy there is. If we're in fuzzing mode, and if we haven't found
// a match, try to force a match so we get some coverage. Note, we still
// want coverage of the section failed to convert code, so treat index ==
// size as section failed to convert.
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (i == section_definitions_len) {
i = guid->Data1 % (section_definitions_len + 1);
}
#endif
if (i < section_definitions_len) {
return §ion_definitions[i];
}
return NULL;
}
//Converts the section described by a single given section descriptor.
json_object *cper_buf_section_to_ir(const void *cper_section_buf, size_t size,
EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
{
if (descriptor->SectionLength > size) {
cper_print_log(
"Invalid CPER file: Invalid header (incorrect signature).\n");
return NULL;
}
//Parse section to IR based on GUID.
json_object *result = NULL;
json_object *section_ir = NULL;
CPER_SECTION_DEFINITION *section =
select_section_by_guid(&descriptor->SectionType);
if (section == NULL) {
cper_print_log("Unknown section type guid\n");
} else {
result = read_section(cper_section_buf, size, section);
}
//Was it an unknown GUID/failed read?
if (result == NULL) {
//Output the data as formatted base64.
section_ir = json_object_new_object();
add_binary_base64(section_ir, "data", cper_section_buf,
descriptor->SectionLength);
result = json_object_new_object();
json_object_object_add(result, "Unknown", section_ir);
}
if (result == NULL) {
cper_print_log("RETURNING NULL!! !!\n");
}
return result;
}
json_object *cper_buf_single_section_to_ir(const unsigned char *cper_buf,
size_t size)
{
const unsigned char *cper_end;
const unsigned char *section_begin;
json_object *ir;
cper_end = cper_buf + size;
//Read the section descriptor out.
EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor;
if (sizeof(EFI_ERROR_SECTION_DESCRIPTOR) > size) {
cper_print_log(
"Size of cper buffer was too small to read section descriptor %zu\n",
size);
return NULL;
}
ir = json_object_new_object();
section_descriptor = (EFI_ERROR_SECTION_DESCRIPTOR *)cper_buf;
//Convert the section descriptor to IR.
json_object *section_descriptor_ir =
cper_section_descriptor_to_ir(section_descriptor);
json_object_object_add(ir, "sectionDescriptor", section_descriptor_ir);
section_begin = cper_buf + section_descriptor->SectionOffset;
if (section_begin + section_descriptor->SectionLength > cper_end) {
json_object_put(ir);
//cper_print_log("Invalid CPER file: Invalid section descriptor (section offset + length > size).\n");
return NULL;
}
const unsigned char *section =
cper_buf + section_descriptor->SectionOffset;
//Parse the single section.
json_object *section_ir = cper_buf_section_to_ir(
section, section_descriptor->SectionLength, section_descriptor);
if (section_ir == NULL) {
cper_print_log("RETURNING NULL2!! !!\n");
}
json_object_object_add(ir, "section", section_ir);
return ir;
}
//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) {
cper_print_log(
"Failed to read section descriptor for CPER single section (fread() returned an unexpected value).\n");
json_object_put(ir);
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);
//Save our current position in the stream.
long position = ftell(cper_section_file);
//Read section as described by the section descriptor.
fseek(cper_section_file, base_pos + section_descriptor.SectionOffset,
SEEK_SET);
void *section = malloc(section_descriptor.SectionLength);
if (fread(section, section_descriptor.SectionLength, 1,
cper_section_file) != 1) {
cper_print_log(
"Section read failed: Could not read %u bytes from global offset %d.\n",
section_descriptor.SectionLength,
section_descriptor.SectionOffset);
json_object_put(ir);
free(section);
return NULL;
}
//Seek back to our original position.
fseek(cper_section_file, position, SEEK_SET);
//Parse the single section.
json_object *section_ir = cper_buf_section_to_ir(
section, section_descriptor.SectionLength, §ion_descriptor);
json_object_object_add(ir, "section", section_ir);
free(section);
return ir;
}
char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section,
size_t size)
{
json_object *jobj = cper_buf_single_section_to_ir(cper_section, size);
char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
json_object_put(jobj);
return str;
}