| 1 |
|
| 2 |
|
| 3 |
#include <inttypes.h> |
| 4 |
#include <stdbool.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
#include <strings.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <getopt.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <ctype.h> |
| 14 |
#include <arpa/inet.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
#include <libgen.h> |
| 17 |
#include <assert.h> |
| 18 |
#include <regex.h> |
| 19 |
#include <commonlib/bsd/cbmem_id.h> |
| 20 |
#include <commonlib/bsd/helpers.h> |
| 21 |
#include <commonlib/bsd/tpm_log_defs.h> |
| 22 |
#include <commonlib/loglevel.h> |
| 23 |
#include <commonlib/timestamp_serialized.h> |
| 24 |
#include <commonlib/tpm_log_serialized.h> |
| 25 |
#include <commonlib/coreboot_tables.h> |
| 26 |
|
| 27 |
#if defined(__i386__) || defined(__x86_64__) |
| 28 |
#include <x86intrin.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "cbmem_util.h" |
| 32 |
|
| 33 |
#define CBMEM_VERSION "1.2" |
| 34 |
|
| 35 |
|
| 36 |
int cbmem_util_verbose; |
| 37 |
|
| 38 |
#if defined(linux) && (defined(__i386__) || defined(__x86_64__)) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static unsigned long arch_tick_frequency(void) |
| 44 |
{ |
| 45 |
FILE *cpuf; |
| 46 |
char freqs[100]; |
| 47 |
int size; |
| 48 |
char *endp; |
| 49 |
uint64_t rv; |
| 50 |
|
| 51 |
const char* freq_file = |
| 52 |
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 53 |
|
| 54 |
cpuf = fopen(freq_file, "r"); |
| 55 |
if (!cpuf) { |
| 56 |
fprintf(stderr, "Could not open %s: %s\n", |
| 57 |
freq_file, strerror(errno)); |
| 58 |
exit(1); |
| 59 |
} |
| 60 |
|
| 61 |
memset(freqs, 0, sizeof(freqs)); |
| 62 |
size = fread(freqs, 1, sizeof(freqs), cpuf); |
| 63 |
if (!size || (size == sizeof(freqs))) { |
| 64 |
fprintf(stderr, "Wrong number of bytes(%d) read from %s\n", |
| 65 |
size, freq_file); |
| 66 |
exit(1); |
| 67 |
} |
| 68 |
fclose(cpuf); |
| 69 |
rv = strtoull(freqs, &endp, 10); |
| 70 |
|
| 71 |
if (*endp == '\0' || *endp == '\n') |
| 72 |
|
| 73 |
return rv / 1000; |
| 74 |
fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n", |
| 75 |
freqs, freq_file); |
| 76 |
exit(1); |
| 77 |
} |
| 78 |
#elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__)) |
| 79 |
static unsigned long arch_tick_frequency(void) |
| 80 |
{ |
| 81 |
int mib[2] = { CTL_HW, HW_CPUSPEED }; |
| 82 |
static int value = 0; |
| 83 |
size_t value_len = sizeof(value); |
| 84 |
|
| 85 |
|
| 86 |
if ((value == 0) && (sysctl(mib, 2, &value, &value_len, NULL, 0) == -1)) |
| 87 |
return 1; |
| 88 |
|
| 89 |
return value; |
| 90 |
} |
| 91 |
#else |
| 92 |
static unsigned long arch_tick_frequency(void) |
| 93 |
{ |
| 94 |
|
| 95 |
return 1; |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
static unsigned long tick_freq_mhz; |
| 100 |
|
| 101 |
static void timestamp_set_tick_freq(unsigned long table_tick_freq_mhz) |
| 102 |
{ |
| 103 |
tick_freq_mhz = table_tick_freq_mhz; |
| 104 |
|
| 105 |
|
| 106 |
if (!tick_freq_mhz) |
| 107 |
tick_freq_mhz = arch_tick_frequency(); |
| 108 |
|
| 109 |
if (!tick_freq_mhz) { |
| 110 |
fprintf(stderr, "Cannot determine timestamp tick frequency.\n"); |
| 111 |
exit(1); |
| 112 |
} |
| 113 |
|
| 114 |
debug("Timestamp tick frequency: %ld MHz\n", tick_freq_mhz); |
| 115 |
} |
| 116 |
|
| 117 |
static uint64_t arch_convert_raw_ts_entry(uint64_t ts) |
| 118 |
{ |
| 119 |
return ts / tick_freq_mhz; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static void print_norm(uint64_t v) |
| 127 |
{ |
| 128 |
if (v >= 1000) { |
| 129 |
|
| 130 |
print_norm(v / 1000); |
| 131 |
printf(",%3.3u", (uint32_t)(v % 1000)); |
| 132 |
} else { |
| 133 |
printf("%u", (uint32_t)(v % 1000)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
#if defined(__i386__) || defined(__x86_64__) |
| 138 |
static void cbmem_get_lb_table_entry(uint32_t tag, uint8_t **buf_out, size_t *size_out) |
| 139 |
{ |
| 140 |
const struct lb_record *lbr_p; |
| 141 |
const uint8_t *lbtable_raw; |
| 142 |
bool tag_found = false; |
| 143 |
|
| 144 |
if (!cbmem_drv_get_cbmem_entry(CBMEM_ID_CBTABLE, (uint8_t **)&lbtable_raw, NULL, NULL)) |
| 145 |
die("coreboot table not found.\n"); |
| 146 |
|
| 147 |
const struct lb_header *lbh = (const struct lb_header *)lbtable_raw; |
| 148 |
|
| 149 |
for (size_t i = 0; i < lbh->table_bytes - sizeof(struct lb_record); i += lbr_p->size) { |
| 150 |
lbr_p = (const struct lb_record *)(&lbtable_raw[lbh->header_bytes + i]); |
| 151 |
if (lbr_p->tag == tag) { |
| 152 |
tag_found = true; |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (!tag_found) { |
| 158 |
free((void *)lbtable_raw); |
| 159 |
die("coreboot table entry %#x not found.\n", tag); |
| 160 |
} |
| 161 |
|
| 162 |
debug("coreboot table entry %#x found.\n", tag); |
| 163 |
|
| 164 |
*buf_out = malloc(lbr_p->size); |
| 165 |
if (!*buf_out) { |
| 166 |
free((void *)lbtable_raw); |
| 167 |
die("Unable to allocate memory for coreboot table entry %#x, size: %d\n", tag, |
| 168 |
lbr_p->size); |
| 169 |
} |
| 170 |
memcpy(*buf_out, lbr_p, lbr_p->size); |
| 171 |
*size_out = lbr_p->size; |
| 172 |
free((void *)lbtable_raw); |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
static uint64_t timestamp_get(uint64_t table_tick_freq_mhz) |
| 177 |
{ |
| 178 |
#if defined(__i386__) || defined(__x86_64__) |
| 179 |
uint64_t tsc = __rdtsc(); |
| 180 |
struct lb_tsc_info *tsc_info; |
| 181 |
size_t size; |
| 182 |
|
| 183 |
|
| 184 |
if (!table_tick_freq_mhz) |
| 185 |
return tsc; |
| 186 |
|
| 187 |
cbmem_get_lb_table_entry(LB_TAG_TSC_INFO, (uint8_t **)&tsc_info, &size); |
| 188 |
|
| 189 |
const uint32_t tsc_freq_khz = tsc_info->freq_khz; |
| 190 |
|
| 191 |
free(tsc_info); |
| 192 |
|
| 193 |
if (tsc_freq_khz) |
| 194 |
return tsc * table_tick_freq_mhz * 1000 / tsc_freq_khz; |
| 195 |
#else |
| 196 |
(void)table_tick_freq_mhz; |
| 197 |
#endif |
| 198 |
die("Don't know how to obtain timestamps on this platform.\n"); |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
static const char *timestamp_name(uint32_t id) |
| 203 |
{ |
| 204 |
for (size_t i = 0; i < ARRAY_SIZE(timestamp_ids); i++) { |
| 205 |
if (timestamp_ids[i].id == id) |
| 206 |
return timestamp_ids[i].name; |
| 207 |
} |
| 208 |
return "<unknown>"; |
| 209 |
} |
| 210 |
|
| 211 |
static uint32_t timestamp_enum_name_to_id(const char *name) |
| 212 |
{ |
| 213 |
for (size_t i = 0; i < ARRAY_SIZE(timestamp_ids); i++) { |
| 214 |
if (!strcmp(timestamp_ids[i].enum_name, name)) |
| 215 |
return timestamp_ids[i].id; |
| 216 |
} |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp, |
| 221 |
uint64_t prev_stamp) |
| 222 |
{ |
| 223 |
const char *name; |
| 224 |
uint64_t step_time; |
| 225 |
|
| 226 |
name = timestamp_name(id); |
| 227 |
|
| 228 |
step_time = arch_convert_raw_ts_entry(stamp - prev_stamp); |
| 229 |
|
| 230 |
|
| 231 |
printf("%d\t", id); |
| 232 |
printf("%llu\t", (long long)arch_convert_raw_ts_entry(stamp)); |
| 233 |
printf("%llu\t", (long long)step_time); |
| 234 |
printf("%s\n", name); |
| 235 |
|
| 236 |
return step_time; |
| 237 |
} |
| 238 |
|
| 239 |
static uint64_t timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp) |
| 240 |
{ |
| 241 |
const char *name; |
| 242 |
uint64_t step_time; |
| 243 |
|
| 244 |
name = timestamp_name(id); |
| 245 |
|
| 246 |
printf("%4d:", id); |
| 247 |
printf("%-50s", name); |
| 248 |
print_norm(arch_convert_raw_ts_entry(stamp)); |
| 249 |
step_time = arch_convert_raw_ts_entry(stamp - prev_stamp); |
| 250 |
if (prev_stamp) { |
| 251 |
printf(" ("); |
| 252 |
print_norm(step_time); |
| 253 |
printf(")"); |
| 254 |
} |
| 255 |
printf("\n"); |
| 256 |
|
| 257 |
return step_time; |
| 258 |
} |
| 259 |
|
| 260 |
static int compare_timestamp_entries(const void *a, const void *b) |
| 261 |
{ |
| 262 |
const struct timestamp_entry *tse_a = (struct timestamp_entry *)a; |
| 263 |
const struct timestamp_entry *tse_b = (struct timestamp_entry *)b; |
| 264 |
|
| 265 |
if (tse_a->entry_stamp > tse_b->entry_stamp) |
| 266 |
return 1; |
| 267 |
else if (tse_a->entry_stamp < tse_b->entry_stamp) |
| 268 |
return -1; |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
static int find_matching_end(struct timestamp_table *sorted_tst_p, uint32_t start, uint32_t end) |
| 274 |
{ |
| 275 |
uint32_t id = sorted_tst_p->entries[start].entry_id; |
| 276 |
uint32_t possible_match = 0; |
| 277 |
|
| 278 |
for (uint32_t i = 0; i < ARRAY_SIZE(timestamp_ids); ++i) { |
| 279 |
if (timestamp_ids[i].id == id) { |
| 280 |
possible_match = timestamp_ids[i].id_end; |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
if (!possible_match) |
| 287 |
return -1; |
| 288 |
|
| 289 |
for (uint32_t i = start + 1; i < end; i++) |
| 290 |
if (sorted_tst_p->entries[i].entry_id == possible_match) |
| 291 |
return i; |
| 292 |
|
| 293 |
return -1; |
| 294 |
} |
| 295 |
|
| 296 |
static const char *get_timestamp_name(const uint32_t id) |
| 297 |
{ |
| 298 |
for (uint32_t i = 0; i < ARRAY_SIZE(timestamp_ids); i++) |
| 299 |
if (timestamp_ids[i].id == id) |
| 300 |
return timestamp_ids[i].enum_name; |
| 301 |
|
| 302 |
return "UNKNOWN"; |
| 303 |
} |
| 304 |
|
| 305 |
struct ts_range_stack { |
| 306 |
const char *name; |
| 307 |
const char *end_name; |
| 308 |
uint32_t end; |
| 309 |
}; |
| 310 |
|
| 311 |
static void print_with_path(struct ts_range_stack *range_stack, const int stacklvl, |
| 312 |
const uint64_t stamp, const char *last_part) |
| 313 |
{ |
| 314 |
for (int i = 1; i <= stacklvl; ++i) { |
| 315 |
printf("%s -> %s", range_stack[i].name, range_stack[i].end_name); |
| 316 |
if (i < stacklvl || last_part) |
| 317 |
putchar(';'); |
| 318 |
} |
| 319 |
if (last_part) |
| 320 |
printf("%s", last_part); |
| 321 |
printf(" %llu\n", (long long)arch_convert_raw_ts_entry(stamp)); |
| 322 |
} |
| 323 |
|
| 324 |
enum timestamps_print_type { |
| 325 |
TIMESTAMPS_PRINT_NONE, |
| 326 |
TIMESTAMPS_PRINT_NORMAL, |
| 327 |
TIMESTAMPS_PRINT_MACHINE_READABLE, |
| 328 |
TIMESTAMPS_PRINT_STACKED, |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
static void dump_timestamps(enum timestamps_print_type output_type) |
| 333 |
{ |
| 334 |
const struct timestamp_table *tst_p; |
| 335 |
struct timestamp_table *sorted_tst_p; |
| 336 |
size_t size; |
| 337 |
uint64_t prev_stamp = 0; |
| 338 |
uint64_t total_time = 0; |
| 339 |
|
| 340 |
if (!cbmem_drv_get_cbmem_entry(CBMEM_ID_TIMESTAMP, (uint8_t **)&tst_p, &size, NULL)) |
| 341 |
die("Timestamps not found.\n"); |
| 342 |
|
| 343 |
timestamp_set_tick_freq(tst_p->tick_freq_mhz); |
| 344 |
|
| 345 |
if (output_type == TIMESTAMPS_PRINT_NORMAL) |
| 346 |
printf("%d entries total:\n\n", tst_p->num_entries); |
| 347 |
|
| 348 |
sorted_tst_p = malloc(size + sizeof(struct timestamp_entry)); |
| 349 |
if (!sorted_tst_p) { |
| 350 |
free((void *)tst_p); |
| 351 |
die("Failed to allocate memory"); |
| 352 |
} |
| 353 |
memcpy(sorted_tst_p, tst_p, size); |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
sorted_tst_p->entries[tst_p->num_entries].entry_id = 0; |
| 360 |
sorted_tst_p->entries[tst_p->num_entries].entry_stamp = 0; |
| 361 |
sorted_tst_p->num_entries += 1; |
| 362 |
|
| 363 |
qsort(&sorted_tst_p->entries[0], sorted_tst_p->num_entries, |
| 364 |
sizeof(struct timestamp_entry), compare_timestamp_entries); |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
if (sorted_tst_p->entries[0].entry_stamp < 0) { |
| 371 |
sorted_tst_p->base_time = -sorted_tst_p->entries[0].entry_stamp; |
| 372 |
prev_stamp = 0; |
| 373 |
} else { |
| 374 |
prev_stamp = tst_p->base_time; |
| 375 |
} |
| 376 |
|
| 377 |
struct ts_range_stack range_stack[20]; |
| 378 |
range_stack[0].end = sorted_tst_p->num_entries; |
| 379 |
int stacklvl = 0; |
| 380 |
|
| 381 |
for (uint32_t i = 0; i < sorted_tst_p->num_entries; i++) { |
| 382 |
uint64_t stamp; |
| 383 |
const struct timestamp_entry *tse = &sorted_tst_p->entries[i]; |
| 384 |
|
| 385 |
|
| 386 |
stamp = tse->entry_stamp + sorted_tst_p->base_time; |
| 387 |
if (output_type == TIMESTAMPS_PRINT_MACHINE_READABLE) { |
| 388 |
timestamp_print_parseable_entry(tse->entry_id, stamp, prev_stamp); |
| 389 |
} else if (output_type == TIMESTAMPS_PRINT_NORMAL) { |
| 390 |
total_time += timestamp_print_entry(tse->entry_id, stamp, prev_stamp); |
| 391 |
} else if (output_type == TIMESTAMPS_PRINT_STACKED) { |
| 392 |
bool end_of_range = false; |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
while (stacklvl > 0 && range_stack[stacklvl].end == i) { |
| 397 |
end_of_range = true; |
| 398 |
stacklvl--; |
| 399 |
} |
| 400 |
|
| 401 |
int match = |
| 402 |
find_matching_end(sorted_tst_p, i, range_stack[stacklvl].end); |
| 403 |
if (match != -1) { |
| 404 |
const uint64_t match_stamp = |
| 405 |
sorted_tst_p->entries[match].entry_stamp |
| 406 |
+ sorted_tst_p->base_time; |
| 407 |
stacklvl++; |
| 408 |
assert(stacklvl < (int)ARRAY_SIZE(range_stack)); |
| 409 |
range_stack[stacklvl].name = get_timestamp_name(tse->entry_id); |
| 410 |
range_stack[stacklvl].end_name = get_timestamp_name( |
| 411 |
sorted_tst_p->entries[match].entry_id); |
| 412 |
range_stack[stacklvl].end = match; |
| 413 |
print_with_path(range_stack, stacklvl, match_stamp - stamp, |
| 414 |
NULL); |
| 415 |
} else if (!end_of_range) { |
| 416 |
print_with_path(range_stack, stacklvl, stamp - prev_stamp, |
| 417 |
get_timestamp_name(tse->entry_id)); |
| 418 |
} |
| 419 |
|
| 420 |
} |
| 421 |
prev_stamp = stamp; |
| 422 |
} |
| 423 |
|
| 424 |
if (output_type == TIMESTAMPS_PRINT_NORMAL) { |
| 425 |
printf("\nTotal Time: "); |
| 426 |
print_norm(total_time); |
| 427 |
printf("\n"); |
| 428 |
} |
| 429 |
|
| 430 |
free(sorted_tst_p); |
| 431 |
free((void *)tst_p); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
static void timestamp_add_now(uint32_t timestamp_id) |
| 436 |
{ |
| 437 |
struct timestamp_table *tst_p; |
| 438 |
size_t tst_size; |
| 439 |
|
| 440 |
if (!cbmem_drv_get_cbmem_entry(CBMEM_ID_TIMESTAMP, (uint8_t **)&tst_p, &tst_size, NULL)) |
| 441 |
die("Unable to find timestamps.\n"); |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
if (tst_p->num_entries >= tst_p->max_entries) { |
| 448 |
die("Not enough space to add timestamp.\n"); |
| 449 |
} else { |
| 450 |
int64_t time = |
| 451 |
timestamp_get(tst_p->tick_freq_mhz) - tst_p->base_time; |
| 452 |
tst_p->entries[tst_p->num_entries].entry_id = timestamp_id; |
| 453 |
tst_p->entries[tst_p->num_entries].entry_stamp = time; |
| 454 |
tst_p->num_entries += 1; |
| 455 |
} |
| 456 |
|
| 457 |
if (!cbmem_drv_write_cbmem_entry(CBMEM_ID_TIMESTAMP, (uint8_t *)tst_p, tst_size)) |
| 458 |
die("Unable to write timestamps.\n"); |
| 459 |
free((uint8_t *)tst_p); |
| 460 |
} |
| 461 |
|
| 462 |
static bool can_print(const uint8_t *data, size_t len) |
| 463 |
{ |
| 464 |
unsigned int i; |
| 465 |
for (i = 0; i < len; i++) { |
| 466 |
if (!isprint(data[i]) && !isspace(data[i])) { |
| 467 |
|
| 468 |
for (; i < len; i++) { |
| 469 |
if (data[i] != 0) |
| 470 |
return false; |
| 471 |
} |
| 472 |
return true; |
| 473 |
} |
| 474 |
} |
| 475 |
return true; |
| 476 |
} |
| 477 |
|
| 478 |
static void print_hex_string(const uint8_t *hex, size_t len) |
| 479 |
{ |
| 480 |
unsigned int i; |
| 481 |
for (i = 0; i < len; i++) |
| 482 |
printf("%02x", hex[i]); |
| 483 |
} |
| 484 |
|
| 485 |
static void print_hex_line(const uint8_t *hex, size_t len) |
| 486 |
{ |
| 487 |
print_hex_string(hex, len); |
| 488 |
printf("\n"); |
| 489 |
} |
| 490 |
|
| 491 |
static void print_event_type(uint32_t event_type) |
| 492 |
{ |
| 493 |
unsigned int known_event_count = ARRAY_SIZE(tpm_event_types); |
| 494 |
if (event_type >= known_event_count) |
| 495 |
printf("Unknown (0x%x >= %u)", event_type, known_event_count); |
| 496 |
else |
| 497 |
printf("%s", tpm_event_types[event_type]); |
| 498 |
} |
| 499 |
|
| 500 |
static void parse_tpm12_log(const struct tcpa_spec_entry *spec_log) |
| 501 |
{ |
| 502 |
const uint8_t zero_block[sizeof(struct tcpa_spec_entry)] = {0}; |
| 503 |
|
| 504 |
uintptr_t current; |
| 505 |
uint32_t counter = 0; |
| 506 |
|
| 507 |
printf("TCPA log:\n"); |
| 508 |
printf("\tSpecification: %d.%d%d\n", |
| 509 |
spec_log->spec_version_major, |
| 510 |
spec_log->spec_version_minor, |
| 511 |
spec_log->spec_errata); |
| 512 |
printf("\tPlatform class: %s\n", |
| 513 |
le32toh(spec_log->platform_class) == 0 ? "PC Client" : |
| 514 |
le32toh(spec_log->platform_class) == 1 ? "Server" : "Unknown"); |
| 515 |
|
| 516 |
current = (uintptr_t)&spec_log->vendor_info[spec_log->vendor_info_size]; |
| 517 |
while (memcmp((const void *)current, (const void *)zero_block, sizeof(zero_block))) { |
| 518 |
uint32_t len; |
| 519 |
struct tcpa_log_entry *log_entry = (void *)current; |
| 520 |
uint32_t event_type = le32toh(log_entry->event_type); |
| 521 |
|
| 522 |
printf("TCPA log entry %u:\n", ++counter); |
| 523 |
printf("\tPCR: %d\n", le32toh(log_entry->pcr)); |
| 524 |
printf("\tEvent type: "); |
| 525 |
print_event_type(event_type); |
| 526 |
printf("\n"); |
| 527 |
printf("\tDigest: "); |
| 528 |
print_hex_line(log_entry->digest, SHA1_DIGEST_SIZE); |
| 529 |
current += sizeof(struct tcpa_log_entry); |
| 530 |
len = le32toh(log_entry->event_data_size); |
| 531 |
if (len != 0) { |
| 532 |
current += len; |
| 533 |
printf("\tEvent data: "); |
| 534 |
if (can_print(log_entry->event, len)) |
| 535 |
printf("%.*s\n", len, log_entry->event); |
| 536 |
else |
| 537 |
print_hex_line(log_entry->event, len); |
| 538 |
} else { |
| 539 |
printf("\tEvent data not provided\n"); |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
static uint32_t print_tpm2_digests(struct tcg_pcr_event2_header *log_entry) |
| 545 |
{ |
| 546 |
unsigned int i; |
| 547 |
uintptr_t current = (uintptr_t)log_entry->digests; |
| 548 |
|
| 549 |
for (i = 0; i < le32toh(log_entry->digest_count); i++) { |
| 550 |
struct tpm_hash_algorithm *hash = (struct tpm_hash_algorithm *)current; |
| 551 |
switch (le16toh(hash->hashAlg)) { |
| 552 |
case TPM2_ALG_SHA1: |
| 553 |
printf("\t\t SHA1: "); |
| 554 |
print_hex_line(hash->digest.sha1, SHA1_DIGEST_SIZE); |
| 555 |
current += sizeof(hash->hashAlg) + SHA1_DIGEST_SIZE; |
| 556 |
break; |
| 557 |
case TPM2_ALG_SHA256: |
| 558 |
printf("\t\t SHA256: "); |
| 559 |
print_hex_line(hash->digest.sha256, SHA256_DIGEST_SIZE); |
| 560 |
current += sizeof(hash->hashAlg) + SHA256_DIGEST_SIZE; |
| 561 |
break; |
| 562 |
case TPM2_ALG_SHA384: |
| 563 |
printf("\t\t SHA384: "); |
| 564 |
print_hex_line(hash->digest.sha384, SHA384_DIGEST_SIZE); |
| 565 |
current += sizeof(hash->hashAlg) + SHA384_DIGEST_SIZE; |
| 566 |
break; |
| 567 |
case TPM2_ALG_SHA512: |
| 568 |
printf("\t\t SHA512: "); |
| 569 |
print_hex_line(hash->digest.sha512, SHA512_DIGEST_SIZE); |
| 570 |
current += sizeof(hash->hashAlg) + SHA512_DIGEST_SIZE; |
| 571 |
break; |
| 572 |
case TPM2_ALG_SM3_256: |
| 573 |
printf("\t\t SM3: "); |
| 574 |
print_hex_line(hash->digest.sm3_256, SM3_256_DIGEST_SIZE); |
| 575 |
current += sizeof(hash->hashAlg) + SM3_256_DIGEST_SIZE; |
| 576 |
break; |
| 577 |
default: |
| 578 |
die("Unknown hash algorithm\n"); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
return current - (uintptr_t)&log_entry->digest_count; |
| 583 |
} |
| 584 |
|
| 585 |
static void parse_tpm2_log(const struct tcg_efi_spec_id_event *tpm2_log) |
| 586 |
{ |
| 587 |
const uint8_t zero_block[12] = {0}; |
| 588 |
|
| 589 |
uintptr_t current; |
| 590 |
uint32_t counter = 0; |
| 591 |
|
| 592 |
printf("TPM2 log:\n"); |
| 593 |
printf("\tSpecification: %d.%d%d\n", |
| 594 |
tpm2_log->spec_version_major, |
| 595 |
tpm2_log->spec_version_minor, |
| 596 |
tpm2_log->spec_errata); |
| 597 |
printf("\tPlatform class: %s\n", |
| 598 |
le32toh(tpm2_log->platform_class) == 0 ? "PC Client" : |
| 599 |
le32toh(tpm2_log->platform_class) == 1 ? "Server" : "Unknown"); |
| 600 |
|
| 601 |
|
| 602 |
current = (uintptr_t)&tpm2_log->digest_sizes[le32toh(tpm2_log->num_of_algorithms)]; |
| 603 |
|
| 604 |
current += 1 + *(uint8_t *)current; |
| 605 |
|
| 606 |
while (memcmp((const void *)current, (const void *)zero_block, sizeof(zero_block))) { |
| 607 |
uint32_t len; |
| 608 |
struct tcg_pcr_event2_header *log_entry = (void *)current; |
| 609 |
uint32_t event_type = le32toh(log_entry->event_type); |
| 610 |
|
| 611 |
printf("TPM2 log entry %u:\n", ++counter); |
| 612 |
printf("\tPCR: %d\n", le32toh(log_entry->pcr_index)); |
| 613 |
printf("\tEvent type: "); |
| 614 |
print_event_type(event_type); |
| 615 |
printf("\n"); |
| 616 |
|
| 617 |
current = (uintptr_t)&log_entry->digest_count; |
| 618 |
if (le32toh(log_entry->digest_count) > 0) { |
| 619 |
printf("\tDigests:\n"); |
| 620 |
current += print_tpm2_digests(log_entry); |
| 621 |
} else { |
| 622 |
printf("\tNo digests in this log entry\n"); |
| 623 |
current += sizeof(log_entry->digest_count); |
| 624 |
} |
| 625 |
|
| 626 |
len = le32toh(*(uint32_t *)current); |
| 627 |
current += sizeof(uint32_t); |
| 628 |
if (len != 0) { |
| 629 |
printf("\tEvent data: %.*s\n", len, (const char *)current); |
| 630 |
current += len; |
| 631 |
} else { |
| 632 |
printf("\tEvent data not provided\n"); |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
static void dump_tpm_std_log(void *buf) |
| 639 |
{ |
| 640 |
const struct tcpa_spec_entry *tspec_entry; |
| 641 |
const struct tcg_efi_spec_id_event *tcg_spec_entry; |
| 642 |
|
| 643 |
tspec_entry = buf; |
| 644 |
if (!strcmp((const char *)tspec_entry->signature, TCPA_SPEC_ID_EVENT_SIGNATURE)) { |
| 645 |
if (tspec_entry->spec_version_major == 1 && |
| 646 |
tspec_entry->spec_version_minor == 2 && tspec_entry->spec_errata >= 1 && |
| 647 |
le32toh(tspec_entry->entry.event_type) == EV_NO_ACTION) { |
| 648 |
parse_tpm12_log(tspec_entry); |
| 649 |
} else { |
| 650 |
fprintf(stderr, "Unknown TPM1.2 log specification\n"); |
| 651 |
} |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
tcg_spec_entry = buf; |
| 656 |
if (!strcmp((const char *)tcg_spec_entry->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE)) { |
| 657 |
if (tcg_spec_entry->spec_version_major == 2 && |
| 658 |
tcg_spec_entry->spec_version_minor == 0 && |
| 659 |
le32toh(tcg_spec_entry->event_type) == EV_NO_ACTION) { |
| 660 |
parse_tpm2_log(tcg_spec_entry); |
| 661 |
} else { |
| 662 |
fprintf(stderr, "Unknown TPM2 log specification.\n"); |
| 663 |
} |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
fprintf(stderr, "Unknown TPM log specification: %.*s\n", |
| 668 |
(int)sizeof(tcg_spec_entry->signature), |
| 669 |
(const char *)tcg_spec_entry->signature); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
static void dump_tpm_cb_log(void) |
| 674 |
{ |
| 675 |
const struct tpm_cb_log_table *tclt_p; |
| 676 |
|
| 677 |
if (!cbmem_drv_get_cbmem_entry(CBMEM_ID_TPM_CB_LOG, (uint8_t **)&tclt_p, NULL, NULL)) |
| 678 |
die("coreboot TPM log not found.\n"); |
| 679 |
|
| 680 |
printf("coreboot TPM log:\n\n"); |
| 681 |
|
| 682 |
for (uint16_t i = 0; i < tclt_p->num_entries; i++) { |
| 683 |
const struct tpm_cb_log_entry *tce = &tclt_p->entries[i]; |
| 684 |
|
| 685 |
printf(" PCR-%u ", tce->pcr); |
| 686 |
print_hex_string(tce->digest, tce->digest_length); |
| 687 |
printf(" %s [%s]\n", tce->digest_type, tce->name); |
| 688 |
} |
| 689 |
|
| 690 |
free((uint8_t *)tclt_p); |
| 691 |
} |
| 692 |
|
| 693 |
static void dump_tpm_log(void) |
| 694 |
{ |
| 695 |
uint8_t *buf; |
| 696 |
|
| 697 |
if (cbmem_drv_get_cbmem_entry(CBMEM_ID_TCPA_TCG_LOG, &buf, NULL, NULL) || |
| 698 |
cbmem_drv_get_cbmem_entry(CBMEM_ID_TPM2_TCG_LOG, &buf, NULL, NULL)) { |
| 699 |
dump_tpm_std_log(buf); |
| 700 |
free(buf); |
| 701 |
} else |
| 702 |
dump_tpm_cb_log(); |
| 703 |
} |
| 704 |
|
| 705 |
enum console_print_type { |
| 706 |
CONSOLE_PRINT_FULL = 0, |
| 707 |
CONSOLE_PRINT_LAST, |
| 708 |
CONSOLE_PRINT_PREVIOUS, |
| 709 |
}; |
| 710 |
|
| 711 |
static int parse_loglevel(char *arg, int *print_unknown_logs) |
| 712 |
{ |
| 713 |
if (arg[0] == '+') { |
| 714 |
*print_unknown_logs = 1; |
| 715 |
arg++; |
| 716 |
} else { |
| 717 |
*print_unknown_logs = 0; |
| 718 |
} |
| 719 |
|
| 720 |
char *endptr; |
| 721 |
int loglevel = strtol(arg, &endptr, 0); |
| 722 |
if (*endptr == '\0' && loglevel >= BIOS_EMERG && loglevel <= BIOS_LOG_PREFIX_MAX_LEVEL) |
| 723 |
return loglevel; |
| 724 |
|
| 725 |
|
| 726 |
for (int i = BIOS_EMERG; i <= BIOS_LOG_PREFIX_MAX_LEVEL; i++) |
| 727 |
if (!strncasecmp(arg, bios_log_prefix[i], 3)) |
| 728 |
return i; |
| 729 |
|
| 730 |
*print_unknown_logs = 1; |
| 731 |
return BIOS_NEVER; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
static void dump_console(enum console_print_type type, int max_loglevel, int print_unknown_logs) |
| 736 |
{ |
| 737 |
const struct cbmem_console *console_p; |
| 738 |
char *console_c; |
| 739 |
size_t size, cursor, previous; |
| 740 |
|
| 741 |
if (!cbmem_drv_get_cbmem_entry(CBMEM_ID_CONSOLE, (uint8_t **)&console_p, NULL, NULL)) |
| 742 |
die("CBMEM console not found.\n"); |
| 743 |
|
| 744 |
cursor = console_p->cursor & CBMC_CURSOR_MASK; |
| 745 |
if (!(console_p->cursor & CBMC_OVERFLOW) && cursor < console_p->size) |
| 746 |
size = cursor; |
| 747 |
else |
| 748 |
size = console_p->size; |
| 749 |
|
| 750 |
console_c = malloc(size + 1); |
| 751 |
if (!console_c) { |
| 752 |
free((uint8_t *)console_p); |
| 753 |
die("Not enough memory for console.\n"); |
| 754 |
} |
| 755 |
console_c[size] = '\0'; |
| 756 |
|
| 757 |
if (console_p->cursor & CBMC_OVERFLOW) { |
| 758 |
if (cursor >= size) { |
| 759 |
printf("cbmem: ERROR: CBMEM console struct is illegal, " |
| 760 |
"output may be corrupt or out of order!\n\n"); |
| 761 |
cursor = 0; |
| 762 |
} |
| 763 |
memcpy(console_c, console_p->body + cursor, size - cursor); |
| 764 |
memcpy(console_c + size - cursor, console_p->body, cursor); |
| 765 |
} else { |
| 766 |
memcpy(console_c, console_p->body, size); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
for (cursor = 0; cursor < size; cursor++) |
| 772 |
if (!isprint(console_c[cursor]) && !isspace(console_c[cursor]) |
| 773 |
&& !BIOS_LOG_IS_MARKER(console_c[cursor])) |
| 774 |
console_c[cursor] = '?'; |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
cursor = previous = 0; |
| 781 |
if (type != CONSOLE_PRINT_FULL) { |
| 782 |
#define BANNER_REGEX(stage) \ |
| 783 |
"\n\n.?coreboot-[^\n]* " stage " starting.*\\.\\.\\.\n" |
| 784 |
#define OVERFLOW_REGEX(stage) "\n.?\\*\\*\\* Pre-CBMEM " stage " console overflow" |
| 785 |
const char *regex[] = { BANNER_REGEX("verstage-before-bootblock"), |
| 786 |
BANNER_REGEX("bootblock"), |
| 787 |
BANNER_REGEX("verstage"), |
| 788 |
OVERFLOW_REGEX("romstage"), |
| 789 |
BANNER_REGEX("romstage"), |
| 790 |
OVERFLOW_REGEX("ramstage"), |
| 791 |
BANNER_REGEX("ramstage") }; |
| 792 |
|
| 793 |
for (size_t i = 0; !cursor && i < ARRAY_SIZE(regex); i++) { |
| 794 |
regex_t re; |
| 795 |
regmatch_t match; |
| 796 |
int res = regcomp(&re, regex[i], REG_EXTENDED | REG_NEWLINE); |
| 797 |
assert(res == 0); |
| 798 |
|
| 799 |
|
| 800 |
while (!regexec(&re, console_c + cursor, 1, &match, 0)) { |
| 801 |
previous = cursor; |
| 802 |
cursor += match.rm_so + 1; |
| 803 |
} |
| 804 |
regfree(&re); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
if (type == CONSOLE_PRINT_PREVIOUS) { |
| 809 |
console_c[cursor] = '\0'; |
| 810 |
cursor = previous; |
| 811 |
} |
| 812 |
|
| 813 |
char c; |
| 814 |
int suppressed = 0; |
| 815 |
int tty = isatty(fileno(stdout)); |
| 816 |
while ((c = console_c[cursor++])) { |
| 817 |
if (BIOS_LOG_IS_MARKER(c)) { |
| 818 |
int lvl = BIOS_LOG_MARKER_TO_LEVEL(c); |
| 819 |
if (lvl > max_loglevel) { |
| 820 |
suppressed = 1; |
| 821 |
continue; |
| 822 |
} |
| 823 |
suppressed = 0; |
| 824 |
if (tty) |
| 825 |
printf(BIOS_LOG_ESCAPE_PATTERN, bios_log_escape[lvl]); |
| 826 |
printf(BIOS_LOG_PREFIX_PATTERN, bios_log_prefix[lvl]); |
| 827 |
} else { |
| 828 |
if (!suppressed) |
| 829 |
putchar(c); |
| 830 |
if (c == '\n') { |
| 831 |
if (tty && !suppressed) |
| 832 |
printf(BIOS_LOG_ESCAPE_RESET); |
| 833 |
suppressed = !print_unknown_logs; |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
if (tty) |
| 838 |
printf(BIOS_LOG_ESCAPE_RESET); |
| 839 |
|
| 840 |
free(console_c); |
| 841 |
free((uint8_t *)console_p); |
| 842 |
} |
| 843 |
|
| 844 |
static void hexdump(const uintptr_t start_address, const uint8_t *buf, const int length) |
| 845 |
{ |
| 846 |
int i; |
| 847 |
int all_zero = 0; |
| 848 |
|
| 849 |
for (i = 0; i < length; i += 16) { |
| 850 |
int j; |
| 851 |
|
| 852 |
all_zero++; |
| 853 |
for (j = 0; j < 16; j++) { |
| 854 |
if (buf[i + j] != 0) { |
| 855 |
all_zero = 0; |
| 856 |
break; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
if (all_zero < 2) { |
| 861 |
printf("%08" PRIxPTR ":", start_address + i); |
| 862 |
for (j = 0; j < 16; j++) |
| 863 |
printf(" %02x", buf[i + j]); |
| 864 |
printf(" "); |
| 865 |
for (j = 0; j < 16; j++) |
| 866 |
printf("%c", isprint(buf[i + j]) ? buf[i + j] : '.'); |
| 867 |
printf("\n"); |
| 868 |
} else if (all_zero == 2) { |
| 869 |
printf("...\n"); |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
static bool hexdump_handler(const uint32_t id, const uint64_t physical_address, const uint8_t *buf, |
| 875 |
const size_t size, void *data) |
| 876 |
{ |
| 877 |
(void)id; |
| 878 |
(void)data; |
| 879 |
hexdump(physical_address, buf, size); |
| 880 |
return false; |
| 881 |
} |
| 882 |
|
| 883 |
static void dump_cbmem_hex(void) |
| 884 |
{ |
| 885 |
cbmem_drv_foreach_cbmem_entry(hexdump_handler, NULL, true); |
| 886 |
} |
| 887 |
|
| 888 |
static void dump_cbmem_raw(unsigned int id) |
| 889 |
{ |
| 890 |
uint8_t *buf; |
| 891 |
size_t size; |
| 892 |
|
| 893 |
if (!cbmem_drv_get_cbmem_entry(id, &buf, &size, NULL)) |
| 894 |
die("cbmem entry id: %#x not found.\n", id); |
| 895 |
|
| 896 |
fwrite(buf, 1, size, stdout); |
| 897 |
|
| 898 |
free(buf); |
| 899 |
} |
| 900 |
|
| 901 |
struct cbmem_id_to_name { |
| 902 |
uint32_t id; |
| 903 |
const char *name; |
| 904 |
}; |
| 905 |
static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE }; |
| 906 |
|
| 907 |
#define MAX_STAGEx 10 |
| 908 |
static void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size) |
| 909 |
{ |
| 910 |
const char *name; |
| 911 |
char stage_x[20]; |
| 912 |
|
| 913 |
name = NULL; |
| 914 |
for (size_t i = 0; i < ARRAY_SIZE(cbmem_ids); i++) { |
| 915 |
if (cbmem_ids[i].id == id) { |
| 916 |
name = cbmem_ids[i].name; |
| 917 |
break; |
| 918 |
} |
| 919 |
if (id >= CBMEM_ID_STAGEx_META && |
| 920 |
id < CBMEM_ID_STAGEx_META + MAX_STAGEx) { |
| 921 |
snprintf(stage_x, sizeof(stage_x), "STAGE%d META", |
| 922 |
(id - CBMEM_ID_STAGEx_META)); |
| 923 |
name = stage_x; |
| 924 |
} |
| 925 |
if (id >= CBMEM_ID_STAGEx_CACHE && |
| 926 |
id < CBMEM_ID_STAGEx_CACHE + MAX_STAGEx) { |
| 927 |
snprintf(stage_x, sizeof(stage_x), "STAGE%d $ ", |
| 928 |
(id - CBMEM_ID_STAGEx_CACHE)); |
| 929 |
name = stage_x; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
printf("%2d. ", n); |
| 934 |
if (name == NULL) |
| 935 |
name = "(unknown)"; |
| 936 |
printf("%-20s %08x", name, id); |
| 937 |
printf(" %08" PRIx64 " ", base); |
| 938 |
printf(" %08" PRIx64 "\n", size); |
| 939 |
} |
| 940 |
|
| 941 |
static bool toc_handler(const uint32_t id, const uint64_t physical_address, const uint8_t *buf, |
| 942 |
const size_t size, void *data) |
| 943 |
{ |
| 944 |
(void)buf; |
| 945 |
int *i = data; |
| 946 |
|
| 947 |
cbmem_print_entry(*i, id, physical_address, size); |
| 948 |
|
| 949 |
(*i)++; |
| 950 |
|
| 951 |
return false; |
| 952 |
} |
| 953 |
|
| 954 |
static void dump_cbmem_toc(void) |
| 955 |
{ |
| 956 |
int i = 0; |
| 957 |
|
| 958 |
printf("CBMEM table of contents:\n"); |
| 959 |
printf(" %-20s %-8s %-8s %-8s\n", "NAME", "ID", "START", "LENGTH"); |
| 960 |
|
| 961 |
cbmem_drv_foreach_cbmem_entry(toc_handler, &i, false); |
| 962 |
} |
| 963 |
|
| 964 |
#define COVERAGE_MAGIC 0x584d4153 |
| 965 |
struct file { |
| 966 |
uint32_t magic; |
| 967 |
uint32_t next; |
| 968 |
uint32_t filename; |
| 969 |
uint32_t data; |
| 970 |
int offset; |
| 971 |
int len; |
| 972 |
}; |
| 973 |
|
| 974 |
static int mkpath(char *path, mode_t mode) |
| 975 |
{ |
| 976 |
assert (path && *path); |
| 977 |
char *p; |
| 978 |
for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) { |
| 979 |
*p = '\0'; |
| 980 |
if (mkdir(path, mode) == -1) { |
| 981 |
if (errno != EEXIST) { |
| 982 |
*p = '/'; |
| 983 |
return -1; |
| 984 |
} |
| 985 |
} |
| 986 |
*p = '/'; |
| 987 |
} |
| 988 |
return 0; |
| 989 |
} |
| 990 |
|
| 991 |
static void dump_coverage(void) |
| 992 |
{ |
| 993 |
uint64_t start; |
| 994 |
uint8_t *coverage; |
| 995 |
unsigned long phys_offset; |
| 996 |
#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset) |
| 997 |
|
| 998 |
if (!cbmem_drv_get_cbmem_entry(CBMEM_ID_COVERAGE, &coverage, NULL, &start)) |
| 999 |
die("No coverage information found\n"); |
| 1000 |
|
| 1001 |
|
| 1002 |
phys_offset = (unsigned long)coverage - (unsigned long)start; |
| 1003 |
|
| 1004 |
printf("Dumping coverage data...\n"); |
| 1005 |
|
| 1006 |
struct file *file = (struct file *)coverage; |
| 1007 |
while (file && file->magic == COVERAGE_MAGIC) { |
| 1008 |
FILE *f; |
| 1009 |
char *filename; |
| 1010 |
|
| 1011 |
debug(" -> %s\n", (char *)phys_to_virt(file->filename)); |
| 1012 |
filename = strdup((char *)phys_to_virt(file->filename)); |
| 1013 |
if (mkpath(filename, 0755) == -1) { |
| 1014 |
perror("Directory for coverage data could " |
| 1015 |
"not be created"); |
| 1016 |
exit(1); |
| 1017 |
} |
| 1018 |
f = fopen(filename, "wb"); |
| 1019 |
if (!f) { |
| 1020 |
printf("Could not open %s: %s\n", |
| 1021 |
filename, strerror(errno)); |
| 1022 |
exit(1); |
| 1023 |
} |
| 1024 |
if (fwrite((void *)phys_to_virt(file->data), |
| 1025 |
file->len, 1, f) != 1) { |
| 1026 |
printf("Could not write to %s: %s\n", |
| 1027 |
filename, strerror(errno)); |
| 1028 |
exit(1); |
| 1029 |
} |
| 1030 |
fclose(f); |
| 1031 |
free(filename); |
| 1032 |
|
| 1033 |
if (file->next) |
| 1034 |
file = (struct file *)phys_to_virt(file->next); |
| 1035 |
else |
| 1036 |
file = NULL; |
| 1037 |
} |
| 1038 |
free(coverage); |
| 1039 |
} |
| 1040 |
|
| 1041 |
static void print_version(void) |
| 1042 |
{ |
| 1043 |
printf("cbmem v%s -- ", CBMEM_VERSION); |
| 1044 |
printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n"); |
| 1045 |
printf( |
| 1046 |
"This program is free software: you can redistribute it and/or modify\n" |
| 1047 |
"it under the terms of the GNU General Public License as published by\n" |
| 1048 |
"the Free Software Foundation, version 2 of the License.\n\n" |
| 1049 |
"This program is distributed in the hope that it will be useful,\n" |
| 1050 |
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 1051 |
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
| 1052 |
"GNU General Public License for more details.\n\n"); |
| 1053 |
} |
| 1054 |
|
| 1055 |
static void print_usage(const char *name, int exit_code) |
| 1056 |
{ |
| 1057 |
printf("usage: %s [-cCltTLxVvh?]\n", name); |
| 1058 |
printf("\n" |
| 1059 |
" -c | --console: print cbmem console\n" |
| 1060 |
" -1 | --oneboot: print cbmem console for last boot only\n" |
| 1061 |
" -2 | --2ndtolast: print cbmem console for the boot that came before the last one only\n" |
| 1062 |
" -b | --backend [devmem|sysfs]: select specific CBMEM backend\n" |
| 1063 |
" -B | --loglevel: maximum loglevel to print; prefix `+` (e.g. -B +INFO) to also print lines that have no level\n" |
| 1064 |
" -C | --coverage: dump coverage information\n" |
| 1065 |
" -l | --list: print cbmem table of contents\n" |
| 1066 |
" -x | --hexdump: print hexdump of cbmem area\n" |
| 1067 |
" -r | --rawdump ID: print rawdump of specific ID (in hex) of cbtable\n" |
| 1068 |
" -t | --timestamps: print timestamp information\n" |
| 1069 |
" -T | --parseable-timestamps: print parseable timestamps\n" |
| 1070 |
" -S | --stacked-timestamps: print stacked timestamps (e.g. for flame graph tools)\n" |
| 1071 |
" -a | --add-timestamp ID: append timestamp with ID\n" |
| 1072 |
" -L | --tcpa-log print TPM log\n" |
| 1073 |
" -V | --verbose: verbose (debugging) output\n" |
| 1074 |
" -v | --version: print the version\n" |
| 1075 |
" -h | --help: print this help\n" |
| 1076 |
"\n"); |
| 1077 |
exit(exit_code); |
| 1078 |
} |
| 1079 |
|
| 1080 |
int main(int argc, char** argv) |
| 1081 |
{ |
| 1082 |
int print_defaults = 1; |
| 1083 |
int print_console = 0; |
| 1084 |
int print_coverage = 0; |
| 1085 |
int print_list = 0; |
| 1086 |
int print_hexdump = 0; |
| 1087 |
int print_rawdump = 0; |
| 1088 |
int print_tcpa_log = 0; |
| 1089 |
enum timestamps_print_type timestamp_type = TIMESTAMPS_PRINT_NONE; |
| 1090 |
enum console_print_type console_type = CONSOLE_PRINT_FULL; |
| 1091 |
unsigned int rawdump_id = 0; |
| 1092 |
int max_loglevel = BIOS_NEVER; |
| 1093 |
int print_unknown_logs = 1; |
| 1094 |
uint32_t timestamp_id = 0; |
| 1095 |
enum cbmem_drv_backend_type backend_type = CBMEM_DRV_BACKEND_ANY; |
| 1096 |
|
| 1097 |
int opt, option_index = 0; |
| 1098 |
static struct option long_options[] = { |
| 1099 |
{"console", 0, 0, 'c'}, |
| 1100 |
{"backend", required_argument, 0, 'b'}, |
| 1101 |
{"oneboot", 0, 0, '1'}, |
| 1102 |
{"2ndtolast", 0, 0, '2'}, |
| 1103 |
{"loglevel", required_argument, 0, 'B'}, |
| 1104 |
{"coverage", 0, 0, 'C'}, |
| 1105 |
{"list", 0, 0, 'l'}, |
| 1106 |
{"tcpa-log", 0, 0, 'L'}, |
| 1107 |
{"timestamps", 0, 0, 't'}, |
| 1108 |
{"parseable-timestamps", 0, 0, 'T'}, |
| 1109 |
{"stacked-timestamps", 0, 0, 'S'}, |
| 1110 |
{"add-timestamp", required_argument, 0, 'a'}, |
| 1111 |
{"hexdump", 0, 0, 'x'}, |
| 1112 |
{"rawdump", required_argument, 0, 'r'}, |
| 1113 |
{"verbose", 0, 0, 'V'}, |
| 1114 |
{"version", 0, 0, 'v'}, |
| 1115 |
{"help", 0, 0, 'h'}, |
| 1116 |
{0, 0, 0, 0} |
| 1117 |
}; |
| 1118 |
while ((opt = getopt_long(argc, argv, "cb:12B:CltTSa:LxVvh?r:", |
| 1119 |
long_options, &option_index)) != EOF) { |
| 1120 |
switch (opt) { |
| 1121 |
case 'c': |
| 1122 |
print_console = 1; |
| 1123 |
print_defaults = 0; |
| 1124 |
break; |
| 1125 |
case 'b': |
| 1126 |
if (!strcasecmp(optarg, "devmem")) |
| 1127 |
backend_type = CBMEM_DRV_BACKEND_DEVMEM; |
| 1128 |
else if (!strcasecmp(optarg, "sysfs")) |
| 1129 |
backend_type = CBMEM_DRV_BACKEND_SYSFS; |
| 1130 |
else if (!strcasecmp(optarg, "any")) |
| 1131 |
backend_type = CBMEM_DRV_BACKEND_ANY; |
| 1132 |
else |
| 1133 |
die("Unrecognized backend type: '%s'\n", optarg); |
| 1134 |
break; |
| 1135 |
|
| 1136 |
case '1': |
| 1137 |
print_console = 1; |
| 1138 |
console_type = CONSOLE_PRINT_LAST; |
| 1139 |
print_defaults = 0; |
| 1140 |
break; |
| 1141 |
case '2': |
| 1142 |
print_console = 1; |
| 1143 |
console_type = CONSOLE_PRINT_PREVIOUS; |
| 1144 |
print_defaults = 0; |
| 1145 |
break; |
| 1146 |
case 'B': |
| 1147 |
max_loglevel = parse_loglevel(optarg, &print_unknown_logs); |
| 1148 |
break; |
| 1149 |
case 'C': |
| 1150 |
print_coverage = 1; |
| 1151 |
print_defaults = 0; |
| 1152 |
break; |
| 1153 |
case 'l': |
| 1154 |
print_list = 1; |
| 1155 |
print_defaults = 0; |
| 1156 |
break; |
| 1157 |
case 'L': |
| 1158 |
print_tcpa_log = 1; |
| 1159 |
print_defaults = 0; |
| 1160 |
break; |
| 1161 |
case 'x': |
| 1162 |
print_hexdump = 1; |
| 1163 |
print_defaults = 0; |
| 1164 |
break; |
| 1165 |
case 'r': |
| 1166 |
print_rawdump = 1; |
| 1167 |
print_defaults = 0; |
| 1168 |
rawdump_id = strtoul(optarg, NULL, 16); |
| 1169 |
break; |
| 1170 |
case 't': |
| 1171 |
timestamp_type = TIMESTAMPS_PRINT_NORMAL; |
| 1172 |
print_defaults = 0; |
| 1173 |
break; |
| 1174 |
case 'T': |
| 1175 |
timestamp_type = TIMESTAMPS_PRINT_MACHINE_READABLE; |
| 1176 |
print_defaults = 0; |
| 1177 |
break; |
| 1178 |
case 'S': |
| 1179 |
timestamp_type = TIMESTAMPS_PRINT_STACKED; |
| 1180 |
print_defaults = 0; |
| 1181 |
break; |
| 1182 |
case 'a': |
| 1183 |
print_defaults = 0; |
| 1184 |
timestamp_id = timestamp_enum_name_to_id(optarg); |
| 1185 |
|
| 1186 |
if (timestamp_id == 0) |
| 1187 |
timestamp_id = strtoul(optarg, NULL, 0); |
| 1188 |
break; |
| 1189 |
case 'V': |
| 1190 |
cbmem_util_verbose++; |
| 1191 |
break; |
| 1192 |
case 'v': |
| 1193 |
print_version(); |
| 1194 |
exit(0); |
| 1195 |
break; |
| 1196 |
case 'h': |
| 1197 |
print_usage(argv[0], 0); |
| 1198 |
break; |
| 1199 |
case '?': |
| 1200 |
default: |
| 1201 |
print_usage(argv[0], 1); |
| 1202 |
break; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (optind < argc) { |
| 1207 |
fprintf(stderr, "Error: Extra parameter found.\n"); |
| 1208 |
print_usage(argv[0], 1); |
| 1209 |
} |
| 1210 |
|
| 1211 |
if (print_hexdump) { |
| 1212 |
debug("Hexdump requested. CBMEM backend force-set to DEVMEM.\n"); |
| 1213 |
backend_type = CBMEM_DRV_BACKEND_DEVMEM; |
| 1214 |
} |
| 1215 |
|
| 1216 |
if (!cbmem_drv_init(backend_type, timestamp_id != 0)) { |
| 1217 |
if (print_hexdump) |
| 1218 |
fprintf(stderr, "Hexdump is only available on systems with /dev/mem.\n"); |
| 1219 |
die("Unable to initialize CBMEM access. Check if you have either /dev/mem access or sysfs CBMEM entries.\n"); |
| 1220 |
} |
| 1221 |
|
| 1222 |
if (print_console) |
| 1223 |
dump_console(console_type, max_loglevel, print_unknown_logs); |
| 1224 |
|
| 1225 |
if (print_coverage) |
| 1226 |
dump_coverage(); |
| 1227 |
|
| 1228 |
if (print_list) |
| 1229 |
dump_cbmem_toc(); |
| 1230 |
|
| 1231 |
if (print_hexdump) |
| 1232 |
dump_cbmem_hex(); |
| 1233 |
|
| 1234 |
if (print_rawdump) |
| 1235 |
dump_cbmem_raw(rawdump_id); |
| 1236 |
|
| 1237 |
if (timestamp_id) |
| 1238 |
timestamp_add_now(timestamp_id); |
| 1239 |
|
| 1240 |
if (print_defaults) |
| 1241 |
timestamp_type = TIMESTAMPS_PRINT_NORMAL; |
| 1242 |
|
| 1243 |
if (timestamp_type != TIMESTAMPS_PRINT_NONE) |
| 1244 |
dump_timestamps(timestamp_type); |
| 1245 |
|
| 1246 |
if (print_tcpa_log) |
| 1247 |
dump_tpm_log(); |
| 1248 |
|
| 1249 |
cbmem_drv_terminate(); |
| 1250 |
|
| 1251 |
return 0; |
| 1252 |
} |