blob: 3a94f3e67227008be5bb195b04711f7924ce8ea8 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07002
Nico Huber8e4bb9282013-05-26 18:17:54 +02003#include <inttypes.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07004#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08007#include <unistd.h>
8#include <getopt.h>
Julius Werner337de4c2014-06-16 23:02:03 -07009#include <dirent.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080010#include <errno.h>
11#include <fcntl.h>
Stefan Reinauera9c83612013-07-16 17:47:35 -070012#include <ctype.h>
Stefan Reinauer7f681502013-06-19 15:39:09 -070013#include <arpa/inet.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080014#include <sys/types.h>
15#include <sys/stat.h>
16#include <sys/mman.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080017#include <libgen.h>
18#include <assert.h>
Julius Wernerb7b64a92017-04-28 16:31:46 -070019#include <regex.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050020#include <commonlib/cbmem_id.h>
21#include <commonlib/timestamp_serialized.h>
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +020022#include <commonlib/tcpa_log_serialized.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050023#include <commonlib/coreboot_tables.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070024
Patrick Georgid00f1802015-07-13 16:53:50 +020025#ifdef __OpenBSD__
26#include <sys/param.h>
27#include <sys/sysctl.h>
28#endif
29
Stefan Reinauer05cbce62013-01-03 14:30:33 -080030#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070031
Julius Werner337de4c2014-06-16 23:02:03 -070032typedef uint8_t u8;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070033typedef uint16_t u16;
34typedef uint32_t u32;
35typedef uint64_t u64;
36
Aaron Durbin46300aa2017-09-26 16:22:38 -060037/* Return < 0 on error, 0 on success. */
38static int parse_cbtable(u64 address, size_t table_size);
39
40struct mapping {
41 void *virt;
42 size_t offset;
43 size_t virt_size;
44 unsigned long long phys;
45 size_t size;
46};
47
Stefan Reinauera9c83612013-07-16 17:47:35 -070048#define CBMEM_VERSION "1.1"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080049
Stefan Reinauer05cbce62013-01-03 14:30:33 -080050/* verbose output? */
51static int verbose = 0;
52#define debug(x...) if(verbose) printf(x)
53
54/* File handle used to access /dev/mem */
Julius Werner337de4c2014-06-16 23:02:03 -070055static int mem_fd;
Aaron Durbin46300aa2017-09-26 16:22:38 -060056static struct mapping lbtable_mapping;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070057
Aaron Durbin46300aa2017-09-26 16:22:38 -060058static void die(const char *msg)
59{
60 if (msg)
61 fputs(msg, stderr);
62 exit(1);
63}
64
65static unsigned long long system_page_size(void)
66{
67 static unsigned long long page_size;
68
69 if (!page_size)
70 page_size = getpagesize();
71
72 return page_size;
73}
74
75static inline size_t size_to_mib(size_t sz)
76{
77 return sz >> 20;
78}
79
80/* Return mapping of physical address requested. */
81static const void *mapping_virt(const struct mapping *mapping)
82{
83 const char *v = mapping->virt;
84
85 if (v == NULL)
86 return NULL;
87
88 return v + mapping->offset;
89}
90
Aaron Durbincf20c902017-09-28 17:52:59 -060091/* Returns virtual address on success, NULL on error. mapping is filled in. */
Aaron Durbin46300aa2017-09-26 16:22:38 -060092static const void *map_memory(struct mapping *mapping, unsigned long long phys,
93 size_t sz)
94{
95 void *v;
96 unsigned long long page_size;
97
98 page_size = system_page_size();
99
100 mapping->virt = NULL;
101 mapping->offset = phys % page_size;
102 mapping->virt_size = sz + mapping->offset;
103 mapping->size = sz;
104 mapping->phys = phys;
105
106 if (size_to_mib(mapping->virt_size) == 0) {
107 debug("Mapping %zuB of physical memory at 0x%llx (requested 0x%llx).\n",
108 mapping->virt_size, phys - mapping->offset, phys);
109 } else {
110 debug("Mapping %zuMB of physical memory at 0x%llx (requested 0x%llx).\n",
111 size_to_mib(mapping->virt_size), phys - mapping->offset,
112 phys);
113 }
114
115 v = mmap(NULL, mapping->virt_size, PROT_READ, MAP_SHARED, mem_fd,
116 phys - mapping->offset);
117
118 if (v == MAP_FAILED) {
119 debug("Mapping failed %zuB of physical memory at 0x%llx.\n",
120 mapping->virt_size, phys - mapping->offset);
121 return NULL;
122 }
123
124 mapping->virt = v;
125
126 if (mapping->offset != 0)
127 debug(" ... padding virtual address with 0x%zx bytes.\n",
128 mapping->offset);
129
130 return mapping_virt(mapping);
131}
132
133/* Returns 0 on success, < 0 on error. mapping is cleared if successful. */
134static int unmap_memory(struct mapping *mapping)
135{
136 if (mapping->virt == NULL)
137 return -1;
138
139 munmap(mapping->virt, mapping->virt_size);
140 mapping->virt = NULL;
141 mapping->offset = 0;
142 mapping->virt_size = 0;
143
144 return 0;
145}
146
147/* Return size of physical address mapping requested. */
148static size_t mapping_size(const struct mapping *mapping)
149{
150 if (mapping->virt == NULL)
151 return 0;
152
153 return mapping->size;
154}
Timothy Pearsondf699d52015-05-16 14:55:54 -0500155
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700156/*
Julius Werner127a79e2017-02-03 12:50:03 -0800157 * Some architectures map /dev/mem memory in a way that doesn't support
158 * unaligned accesses. Most normal libc memcpy()s aren't safe to use in this
159 * case, so build our own which makes sure to never do unaligned accesses on
160 * *src (*dest is fine since we never map /dev/mem for writing).
161 */
162static void *aligned_memcpy(void *dest, const void *src, size_t n)
163{
164 u8 *d = dest;
165 const volatile u8 *s = src; /* volatile to prevent optimization */
166
167 while ((uintptr_t)s & (sizeof(size_t) - 1)) {
168 if (n-- == 0)
169 return dest;
170 *d++ = *s++;
171 }
172
173 while (n >= sizeof(size_t)) {
174 *(size_t *)d = *(const volatile size_t *)s;
175 d += sizeof(size_t);
176 s += sizeof(size_t);
177 n -= sizeof(size_t);
178 }
179
180 while (n-- > 0)
181 *d++ = *s++;
182
183 return dest;
184}
185
186/*
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700187 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
188 * the buffer length is odd last byte is excluded from the calculation
189 */
190static u16 ipchcksum(const void *addr, unsigned size)
191{
192 const u16 *p = addr;
193 unsigned i, n = size / 2; /* don't expect odd sized blocks */
194 u32 sum = 0;
195
196 for (i = 0; i < n; i++)
197 sum += p[i];
198
199 sum = (sum >> 16) + (sum & 0xffff);
200 sum += (sum >> 16);
201 sum = ~sum & 0xffff;
202 return (u16) sum;
203}
204
Aaron Durbin09c0c112015-09-30 12:33:01 -0500205/* Find the first cbmem entry filling in the details. */
206static int find_cbmem_entry(uint32_t id, uint64_t *addr, size_t *size)
207{
Aaron Durbinf2a38222017-09-26 17:44:28 -0600208 const uint8_t *table;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500209 size_t offset;
210 int ret = -1;
211
Aaron Durbin46300aa2017-09-26 16:22:38 -0600212 table = mapping_virt(&lbtable_mapping);
Aaron Durbin09c0c112015-09-30 12:33:01 -0500213
214 if (table == NULL)
215 return -1;
216
217 offset = 0;
218
Aaron Durbin46300aa2017-09-26 16:22:38 -0600219 while (offset < mapping_size(&lbtable_mapping)) {
Aaron Durbinf2a38222017-09-26 17:44:28 -0600220 const struct lb_record *lbr;
221 const struct lb_cbmem_entry *lbe;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500222
Aaron Durbinf2a38222017-09-26 17:44:28 -0600223 lbr = (const void *)(table + offset);
Aaron Durbin09c0c112015-09-30 12:33:01 -0500224 offset += lbr->size;
225
226 if (lbr->tag != LB_TAG_CBMEM_ENTRY)
227 continue;
228
Aaron Durbinf2a38222017-09-26 17:44:28 -0600229 lbe = (const void *)lbr;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500230 if (lbe->id != id)
231 continue;
232
233 *addr = lbe->address;
234 *size = lbe->entry_size;
235 ret = 0;
236 break;
237 }
238
Aaron Durbin09c0c112015-09-30 12:33:01 -0500239 return ret;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700240}
241
242/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800243 * Try finding the timestamp table and coreboot cbmem console starting from the
244 * passed in memory offset. Could be called recursively in case a forwarding
245 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700246 *
Patrick Georgi220c2092020-01-30 12:58:08 +0100247 * Returns pointer to a memory buffer containing the timestamp table or zero if
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700248 * none found.
249 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800250
251static struct lb_cbmem_ref timestamps;
252static struct lb_cbmem_ref console;
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200253static struct lb_cbmem_ref tcpa_log;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800254static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800255
Stefan Reinauer8c594772013-04-19 14:22:29 -0700256/* This is a work-around for a nasty problem introduced by initially having
257 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
258 * on 64bit x86 systems because coreboot is 32bit on those systems.
259 * When the problem was found, it was corrected, but there are a lot of
260 * systems out there with a firmware that does not produce the right
261 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
262 */
Aaron Durbinf2a38222017-09-26 17:44:28 -0600263static struct lb_cbmem_ref parse_cbmem_ref(const struct lb_cbmem_ref *cbmem_ref)
Stefan Reinauer8c594772013-04-19 14:22:29 -0700264{
265 struct lb_cbmem_ref ret;
266
Julius Wernere3c23912018-11-26 15:57:59 -0800267 aligned_memcpy(&ret, cbmem_ref, sizeof(ret));
Stefan Reinauer8c594772013-04-19 14:22:29 -0700268
269 if (cbmem_ref->size < sizeof(*cbmem_ref))
270 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
271
Stefan Reinauera9c83612013-07-16 17:47:35 -0700272 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
273
Stefan Reinauer8c594772013-04-19 14:22:29 -0700274 return ret;
275}
276
Aaron Durbin46300aa2017-09-26 16:22:38 -0600277static void parse_memory_tags(const struct lb_memory *mem)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700278{
Aaron Durbin46300aa2017-09-26 16:22:38 -0600279 int num_entries;
280 int i;
281
282 /* Peel off the header size and calculate the number of entries. */
283 num_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
284
285 for (i = 0; i < num_entries; i++) {
286 if (mem->map[i].type != LB_MEM_TABLE)
287 continue;
288 debug(" LB_MEM_TABLE found.\n");
289 /* The last one found is CBMEM */
Aaron Durbineb722282019-01-31 09:40:26 -0700290 aligned_memcpy(&cbmem, &mem->map[i], sizeof(cbmem));
Aaron Durbin46300aa2017-09-26 16:22:38 -0600291 }
292}
293
294/* Return < 0 on error, 0 on success, 1 if forwarding table entry found. */
295static int parse_cbtable_entries(const struct mapping *table_mapping)
296{
297 size_t i;
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200298 const struct lb_record *lbr_p;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600299 size_t table_size = mapping_size(table_mapping);
300 const void *lbtable = mapping_virt(table_mapping);
301 int forwarding_table_found = 0;
302
303 for (i = 0; i < table_size; i += lbr_p->size) {
304 lbr_p = lbtable + i;
305 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
306 switch (lbr_p->tag) {
307 case LB_TAG_MEMORY:
308 debug(" Found memory map.\n");
309 parse_memory_tags(lbtable + i);
310 continue;
311 case LB_TAG_TIMESTAMPS: {
312 debug(" Found timestamp table.\n");
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200313 timestamps =
314 parse_cbmem_ref((struct lb_cbmem_ref *)lbr_p);
Aaron Durbin46300aa2017-09-26 16:22:38 -0600315 continue;
316 }
317 case LB_TAG_CBMEM_CONSOLE: {
318 debug(" Found cbmem console.\n");
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200319 console = parse_cbmem_ref((struct lb_cbmem_ref *)lbr_p);
320 continue;
321 }
322 case LB_TAG_TCPA_LOG: {
323 debug(" Found tcpa log table.\n");
324 tcpa_log =
325 parse_cbmem_ref((struct lb_cbmem_ref *)lbr_p);
Aaron Durbin46300aa2017-09-26 16:22:38 -0600326 continue;
327 }
328 case LB_TAG_FORWARD: {
329 int ret;
330 /*
331 * This is a forwarding entry - repeat the
332 * search at the new address.
333 */
334 struct lb_forward lbf_p =
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200335 *(const struct lb_forward *)lbr_p;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600336 debug(" Found forwarding entry.\n");
337 ret = parse_cbtable(lbf_p.forward, 0);
338
339 /* Assume the forwarding entry is valid. If this fails
340 * then there's a total failure. */
341 if (ret < 0)
342 return -1;
343 forwarding_table_found = 1;
344 }
345 default:
346 break;
347 }
348 }
349
350 return forwarding_table_found;
351}
352
353/* Return < 0 on error, 0 on success. */
354static int parse_cbtable(u64 address, size_t table_size)
355{
Aaron Durbinf2a38222017-09-26 17:44:28 -0600356 const void *buf;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600357 struct mapping header_mapping;
358 size_t req_size;
359 size_t i;
360
361 req_size = table_size;
362 /* Default to 4 KiB search space. */
363 if (req_size == 0)
364 req_size = 4 * 1024;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800365
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500366 debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n",
Aaron Durbin46300aa2017-09-26 16:22:38 -0600367 address, req_size);
368
369 buf = map_memory(&header_mapping, address, req_size);
370
Timothy Pearsonbea71402015-09-05 18:07:17 -0500371 if (!buf)
Aaron Durbin46300aa2017-09-26 16:22:38 -0600372 return -1;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700373
Aaron Durbin46300aa2017-09-26 16:22:38 -0600374 /* look at every 16 bytes */
Aaron Durbincf20c902017-09-28 17:52:59 -0600375 for (i = 0; i <= req_size - sizeof(struct lb_header); i += 16) {
Aaron Durbin46300aa2017-09-26 16:22:38 -0600376 int ret;
Aaron Durbinf2a38222017-09-26 17:44:28 -0600377 const struct lb_header *lbh;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600378 struct mapping table_mapping;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700379
Aaron Durbin46300aa2017-09-26 16:22:38 -0600380 lbh = buf + i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800381 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
382 !lbh->header_bytes ||
383 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700384 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700385 }
386
Aaron Durbin46300aa2017-09-26 16:22:38 -0600387 /* Map in the whole table to parse. */
388 if (!map_memory(&table_mapping, address + i + lbh->header_bytes,
389 lbh->table_bytes)) {
390 debug("Couldn't map in table\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700391 continue;
392 }
393
Aaron Durbin46300aa2017-09-26 16:22:38 -0600394 if (ipchcksum(mapping_virt(&table_mapping), lbh->table_bytes) !=
395 lbh->table_checksum) {
396 debug("Signature found, but wrong checksum.\n");
397 unmap_memory(&table_mapping);
398 continue;
399 }
400
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800401 debug("Found!\n");
402
Aaron Durbin46300aa2017-09-26 16:22:38 -0600403 ret = parse_cbtable_entries(&table_mapping);
Aaron Durbin09c0c112015-09-30 12:33:01 -0500404
Aaron Durbin46300aa2017-09-26 16:22:38 -0600405 /* Table parsing failed. */
406 if (ret < 0) {
407 unmap_memory(&table_mapping);
408 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700409 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800410
Aaron Durbin46300aa2017-09-26 16:22:38 -0600411 /* Succeeded in parsing the table. Header not needed anymore. */
412 unmap_memory(&header_mapping);
413
414 /*
415 * Table parsing succeeded. If forwarding table not found update
416 * coreboot table mapping for future use.
417 */
418 if (ret == 0)
419 lbtable_mapping = table_mapping;
420 else
421 unmap_memory(&table_mapping);
422
423 return 0;
424 }
425
426 unmap_memory(&header_mapping);
427
428 return -1;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700429}
430
Patrick Georgid00f1802015-07-13 16:53:50 +0200431#if defined(linux) && (defined(__i386__) || defined(__x86_64__))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700432/*
Aaron Durbin08e920e2016-03-12 08:41:34 +0100433 * read CPU frequency from a sysfs file, return an frequency in Megahertz as
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700434 * an int or exit on any error.
435 */
Aaron Durbinc49014e2015-08-30 21:19:55 -0500436static unsigned long arch_tick_frequency(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700437{
438 FILE *cpuf;
439 char freqs[100];
440 int size;
441 char *endp;
442 u64 rv;
443
444 const char* freq_file =
445 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
446
447 cpuf = fopen(freq_file, "r");
448 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800449 fprintf(stderr, "Could not open %s: %s\n",
450 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700451 exit(1);
452 }
453
454 memset(freqs, 0, sizeof(freqs));
455 size = fread(freqs, 1, sizeof(freqs), cpuf);
456 if (!size || (size == sizeof(freqs))) {
457 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
458 size, freq_file);
459 exit(1);
460 }
461 fclose(cpuf);
462 rv = strtoull(freqs, &endp, 10);
463
464 if (*endp == '\0' || *endp == '\n')
Aaron Durbin08e920e2016-03-12 08:41:34 +0100465 /* cpuinfo_max_freq is in kHz. Convert it to MHz. */
466 return rv / 1000;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700467 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
468 freqs, freq_file);
469 exit(1);
470}
Patrick Georgid00f1802015-07-13 16:53:50 +0200471#elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__))
Aaron Durbinc49014e2015-08-30 21:19:55 -0500472static unsigned long arch_tick_frequency(void)
Patrick Georgid00f1802015-07-13 16:53:50 +0200473{
474 int mib[2] = { CTL_HW, HW_CPUSPEED };
475 static int value = 0;
476 size_t value_len = sizeof(value);
477
Aaron Durbinc49014e2015-08-30 21:19:55 -0500478 /* Return 1 MHz when sysctl fails. */
Patrick Georgid00f1802015-07-13 16:53:50 +0200479 if ((value == 0) && (sysctl(mib, 2, &value, &value_len, NULL, 0) == -1))
Aaron Durbinc49014e2015-08-30 21:19:55 -0500480 return 1;
Patrick Georgid00f1802015-07-13 16:53:50 +0200481
Aaron Durbinc49014e2015-08-30 21:19:55 -0500482 return value;
Patrick Georgid00f1802015-07-13 16:53:50 +0200483}
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700484#else
Aaron Durbinc49014e2015-08-30 21:19:55 -0500485static unsigned long arch_tick_frequency(void)
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700486{
Aaron Durbinc49014e2015-08-30 21:19:55 -0500487 /* 1 MHz = 1us. */
488 return 1;
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700489}
490#endif
491
Aaron Durbinc49014e2015-08-30 21:19:55 -0500492static unsigned long tick_freq_mhz;
493
494static void timestamp_set_tick_freq(unsigned long table_tick_freq_mhz)
495{
496 tick_freq_mhz = table_tick_freq_mhz;
497
Martin Rothac9d6b82017-12-11 13:27:56 -0700498 /* Honor table frequency if present. */
499 if (!tick_freq_mhz)
500 tick_freq_mhz = arch_tick_frequency();
Aaron Durbinc49014e2015-08-30 21:19:55 -0500501
502 if (!tick_freq_mhz) {
503 fprintf(stderr, "Cannot determine timestamp tick frequency.\n");
504 exit(1);
505 }
Martin Rothac9d6b82017-12-11 13:27:56 -0700506
507 debug("Timestamp tick frequency: %ld MHz\n", tick_freq_mhz);
Aaron Durbinc49014e2015-08-30 21:19:55 -0500508}
509
Jacob Garber79a2f472019-06-27 17:23:25 -0600510static u64 arch_convert_raw_ts_entry(u64 ts)
Aaron Durbinc49014e2015-08-30 21:19:55 -0500511{
512 return ts / tick_freq_mhz;
513}
514
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700515/*
516 * Print an integer in 'normalized' form - with commas separating every three
Julius Wernera7d92442014-12-02 20:51:19 -0800517 * decimal orders.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700518 */
Julius Wernera7d92442014-12-02 20:51:19 -0800519static void print_norm(u64 v)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700520{
Julius Wernera7d92442014-12-02 20:51:19 -0800521 if (v >= 1000) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700522 /* print the higher order sections first */
Julius Wernera7d92442014-12-02 20:51:19 -0800523 print_norm(v / 1000);
524 printf(",%3.3u", (u32)(v % 1000));
525 } else {
526 printf("%u", (u32)(v % 1000));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700527 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700528}
529
Aaron Durbinfbff3012015-08-30 22:00:12 -0500530static const char *timestamp_name(uint32_t id)
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700531{
Jacob Garber414d5d82019-06-27 16:02:32 -0600532 for (size_t i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
Aaron Durbinfbff3012015-08-30 22:00:12 -0500533 if (timestamp_ids[i].id == id)
534 return timestamp_ids[i].name;
535 }
536 return "<unknown>";
537}
538
539static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp,
540 uint64_t prev_stamp)
541{
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700542 const char *name;
Aaron Durbin799bf782015-08-06 13:52:08 -0500543 uint64_t step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700544
Aaron Durbinfbff3012015-08-30 22:00:12 -0500545 name = timestamp_name(id);
546
547 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
548
549 /* ID<tab>absolute time<tab>relative time<tab>description */
550 printf("%d\t", id);
551 printf("%llu\t", (long long)arch_convert_raw_ts_entry(stamp));
552 printf("%llu\t", (long long)step_time);
553 printf("%s\n", name);
554
555 return step_time;
556}
557
Jacob Garber79a2f472019-06-27 17:23:25 -0600558static uint64_t timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
Aaron Durbinfbff3012015-08-30 22:00:12 -0500559{
560 const char *name;
561 uint64_t step_time;
562
563 name = timestamp_name(id);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700564
565 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800566 printf("%-50s", name);
567 print_norm(arch_convert_raw_ts_entry(stamp));
Aaron Durbin799bf782015-08-06 13:52:08 -0500568 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700569 if (prev_stamp) {
570 printf(" (");
Aaron Durbin799bf782015-08-06 13:52:08 -0500571 print_norm(step_time);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700572 printf(")");
573 }
574 printf("\n");
Aaron Durbin799bf782015-08-06 13:52:08 -0500575
576 return step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700577}
578
Raul E Rangeld4fec682018-05-11 11:08:07 -0600579static int compare_timestamp_entries(const void *a, const void *b)
580{
581 const struct timestamp_entry *tse_a = (struct timestamp_entry *)a;
582 const struct timestamp_entry *tse_b = (struct timestamp_entry *)b;
583
Furquan Shaikh8f567322018-05-17 15:08:03 -0700584 if (tse_a->entry_stamp > tse_b->entry_stamp)
585 return 1;
586 else if (tse_a->entry_stamp < tse_b->entry_stamp)
587 return -1;
588
589 return 0;
Raul E Rangeld4fec682018-05-11 11:08:07 -0600590}
591
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700592/* dump the timestamp table */
Aaron Durbinfbff3012015-08-30 22:00:12 -0500593static void dump_timestamps(int mach_readable)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700594{
Aaron Durbinf2a38222017-09-26 17:44:28 -0600595 const struct timestamp_table *tst_p;
Raul E Rangeld4fec682018-05-11 11:08:07 -0600596 struct timestamp_table *sorted_tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500597 size_t size;
Aaron Durbin31540fb2015-07-11 12:44:10 -0500598 uint64_t prev_stamp;
Aaron Durbin799bf782015-08-06 13:52:08 -0500599 uint64_t total_time;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600600 struct mapping timestamp_mapping;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800601
602 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
603 fprintf(stderr, "No timestamps found in coreboot table.\n");
604 return;
605 }
606
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500607 size = sizeof(*tst_p);
Aaron Durbin46300aa2017-09-26 16:22:38 -0600608 tst_p = map_memory(&timestamp_mapping, timestamps.cbmem_addr, size);
609 if (!tst_p)
610 die("Unable to map timestamp header\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700611
Aaron Durbinc49014e2015-08-30 21:19:55 -0500612 timestamp_set_tick_freq(tst_p->tick_freq_mhz);
613
Aaron Durbinfbff3012015-08-30 22:00:12 -0500614 if (!mach_readable)
615 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500616 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
617
Aaron Durbin46300aa2017-09-26 16:22:38 -0600618 unmap_memory(&timestamp_mapping);
619
620 tst_p = map_memory(&timestamp_mapping, timestamps.cbmem_addr, size);
621 if (!tst_p)
622 die("Unable to map full timestamp table\n");
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500623
Aaron Durbin31540fb2015-07-11 12:44:10 -0500624 /* Report the base time within the table. */
625 prev_stamp = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500626 if (mach_readable)
627 timestamp_print_parseable_entry(0, tst_p->base_time,
628 prev_stamp);
629 else
630 timestamp_print_entry(0, tst_p->base_time, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500631 prev_stamp = tst_p->base_time;
632
Raul E Rangeld4fec682018-05-11 11:08:07 -0600633 sorted_tst_p = malloc(size);
634 if (!sorted_tst_p)
635 die("Failed to allocate memory");
Julius Wernere3c23912018-11-26 15:57:59 -0800636 aligned_memcpy(sorted_tst_p, tst_p, size);
Raul E Rangeld4fec682018-05-11 11:08:07 -0600637
638 qsort(&sorted_tst_p->entries[0], sorted_tst_p->num_entries,
639 sizeof(struct timestamp_entry), compare_timestamp_entries);
640
Aaron Durbin799bf782015-08-06 13:52:08 -0500641 total_time = 0;
Jacob Garber414d5d82019-06-27 16:02:32 -0600642 for (uint32_t i = 0; i < sorted_tst_p->num_entries; i++) {
Aaron Durbin31540fb2015-07-11 12:44:10 -0500643 uint64_t stamp;
Raul E Rangeld4fec682018-05-11 11:08:07 -0600644 const struct timestamp_entry *tse = &sorted_tst_p->entries[i];
Aaron Durbin31540fb2015-07-11 12:44:10 -0500645
646 /* Make all timestamps absolute. */
Raul E Rangeld4fec682018-05-11 11:08:07 -0600647 stamp = tse->entry_stamp + sorted_tst_p->base_time;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500648 if (mach_readable)
649 total_time +=
650 timestamp_print_parseable_entry(tse->entry_id,
651 stamp, prev_stamp);
652 else
653 total_time += timestamp_print_entry(tse->entry_id,
Aaron Durbin799bf782015-08-06 13:52:08 -0500654 stamp, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500655 prev_stamp = stamp;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700656 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800657
Aaron Durbinfbff3012015-08-30 22:00:12 -0500658 if (!mach_readable) {
659 printf("\nTotal Time: ");
660 print_norm(total_time);
661 printf("\n");
662 }
Aaron Durbin799bf782015-08-06 13:52:08 -0500663
Aaron Durbin46300aa2017-09-26 16:22:38 -0600664 unmap_memory(&timestamp_mapping);
Raul E Rangeld4fec682018-05-11 11:08:07 -0600665 free(sorted_tst_p);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700666}
667
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200668/* dump the tcpa log table */
669static void dump_tcpa_log(void)
670{
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200671 const struct tcpa_table *tclt_p;
672 size_t size;
673 struct mapping tcpa_mapping;
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200674
675 if (tcpa_log.tag != LB_TAG_TCPA_LOG) {
676 fprintf(stderr, "No tcpa log found in coreboot table.\n");
677 return;
678 }
679
680 size = sizeof(*tclt_p);
681 tclt_p = map_memory(&tcpa_mapping, tcpa_log.cbmem_addr, size);
682 if (!tclt_p)
683 die("Unable to map tcpa log header\n");
684
685 size += tclt_p->num_entries * sizeof(tclt_p->entries[0]);
686
687 unmap_memory(&tcpa_mapping);
688
689 tclt_p = map_memory(&tcpa_mapping, tcpa_log.cbmem_addr, size);
690 if (!tclt_p)
691 die("Unable to map full tcpa log table\n");
692
693 printf("coreboot TCPA log:\n\n");
694
Jacob Garber414d5d82019-06-27 16:02:32 -0600695 for (uint16_t i = 0; i < tclt_p->num_entries; i++) {
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200696 const struct tcpa_entry *tce = &tclt_p->entries[i];
697
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100698 printf(" PCR-%u ", tce->pcr);
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200699
Jacob Garber414d5d82019-06-27 16:02:32 -0600700 for (uint32_t j = 0; j < tce->digest_length; j++)
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100701 printf("%02x", tce->digest[j]);
702
703 printf(" %s [%s]\n", tce->digest_type, tce->name);
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +0200704 }
705
706 unmap_memory(&tcpa_mapping);
707}
708
Julius Wernerd67c6872017-02-02 17:32:00 -0800709struct cbmem_console {
710 u32 size;
711 u32 cursor;
712 u8 body[0];
713} __attribute__ ((__packed__));
714
715#define CBMC_CURSOR_MASK ((1 << 28) - 1)
716#define CBMC_OVERFLOW (1 << 31)
717
Stefan Reinauer19f87562013-01-07 13:37:12 -0800718/* dump the cbmem console */
Julius Wernerb7b64a92017-04-28 16:31:46 -0700719static void dump_console(int one_boot_only)
Stefan Reinauer19f87562013-01-07 13:37:12 -0800720{
Aaron Durbinf2a38222017-09-26 17:44:28 -0600721 const struct cbmem_console *console_p;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800722 char *console_c;
Julius Wernerd67c6872017-02-02 17:32:00 -0800723 size_t size, cursor;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600724 struct mapping console_mapping;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800725
726 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
727 fprintf(stderr, "No console found in coreboot table.\n");
728 return;
729 }
730
Julius Wernerd67c6872017-02-02 17:32:00 -0800731 size = sizeof(*console_p);
Aaron Durbin46300aa2017-09-26 16:22:38 -0600732 console_p = map_memory(&console_mapping, console.cbmem_addr, size);
733 if (!console_p)
734 die("Unable to map console object.\n");
735
Julius Wernerd67c6872017-02-02 17:32:00 -0800736 cursor = console_p->cursor & CBMC_CURSOR_MASK;
737 if (!(console_p->cursor & CBMC_OVERFLOW) && cursor < console_p->size)
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100738 size = cursor;
Julius Wernerd67c6872017-02-02 17:32:00 -0800739 else
740 size = console_p->size;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600741 unmap_memory(&console_mapping);
Julius Wernerd67c6872017-02-02 17:32:00 -0800742
743 console_c = malloc(size + 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800744 if (!console_c) {
745 fprintf(stderr, "Not enough memory for console.\n");
746 exit(1);
747 }
Julius Wernerd67c6872017-02-02 17:32:00 -0800748 console_c[size] = '\0';
Stefan Reinauer19f87562013-01-07 13:37:12 -0800749
Aaron Durbin46300aa2017-09-26 16:22:38 -0600750 console_p = map_memory(&console_mapping, console.cbmem_addr,
751 size + sizeof(*console_p));
752
753 if (!console_p)
754 die("Unable to map full console object.\n");
755
Julius Wernerd67c6872017-02-02 17:32:00 -0800756 if (console_p->cursor & CBMC_OVERFLOW) {
757 if (cursor >= size) {
758 printf("cbmem: ERROR: CBMEM console struct is illegal, "
759 "output may be corrupt or out of order!\n\n");
760 cursor = 0;
761 }
762 aligned_memcpy(console_c, console_p->body + cursor,
763 size - cursor);
764 aligned_memcpy(console_c + size - cursor,
765 console_p->body, cursor);
766 } else {
767 aligned_memcpy(console_c, console_p->body, size);
768 }
Stefan Reinauer19f87562013-01-07 13:37:12 -0800769
Julius Wernerd67c6872017-02-02 17:32:00 -0800770 /* Slight memory corruption may occur between reboots and give us a few
771 unprintable characters like '\0'. Replace them with '?' on output. */
772 for (cursor = 0; cursor < size; cursor++)
773 if (!isprint(console_c[cursor]) && !isspace(console_c[cursor]))
774 console_c[cursor] = '?';
Stefan Reinauer19f87562013-01-07 13:37:12 -0800775
Julius Wernerb7b64a92017-04-28 16:31:46 -0700776 /* We detect the last boot by looking for a bootblock, romstage or
777 ramstage banner, in that order (to account for platforms without
778 CONFIG_BOOTBLOCK_CONSOLE and/or CONFIG_EARLY_CONSOLE). Once we find
779 a banner, store the last match for that stage in cursor and stop. */
780 cursor = 0;
781 if (one_boot_only) {
You-Cheng Syu1430b392019-07-01 16:40:25 +0800782#define BANNER_REGEX(stage) \
783 "\n\ncoreboot-[^\n]* " stage " starting.*\\.\\.\\.\n"
Julius Wernerbe3aa042017-06-12 17:35:15 -0700784#define OVERFLOW_REGEX(stage) "\n\\*\\*\\* Pre-CBMEM " stage " console overflow"
Kangheui Won4b5c8b52020-10-07 14:29:38 +1100785 const char *regex[] = { BANNER_REGEX("verstage-before-bootblock"),
786 BANNER_REGEX("bootblock"),
You-Cheng Syu1430b392019-07-01 16:40:25 +0800787 BANNER_REGEX("verstage"),
Julius Wernerd906bb62017-05-16 13:54:18 -0700788 OVERFLOW_REGEX("romstage"),
Furquan Shaikh35972de2018-02-16 16:12:02 -0800789 BANNER_REGEX("romstage"),
790 OVERFLOW_REGEX("ramstage"),
791 BANNER_REGEX("ramstage") };
Julius Wernerb7b64a92017-04-28 16:31:46 -0700792
Jacob Garber414d5d82019-06-27 16:02:32 -0600793 for (size_t i = 0; !cursor && i < ARRAY_SIZE(regex); i++) {
Julius Wernerb7b64a92017-04-28 16:31:46 -0700794 regex_t re;
795 regmatch_t match;
796 assert(!regcomp(&re, regex[i], 0));
797
798 /* Keep looking for matches so we find the last one. */
799 while (!regexec(&re, console_c + cursor, 1, &match, 0))
800 cursor += match.rm_so + 1;
801 regfree(&re);
802 }
803 }
804
805 puts(console_c + cursor);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800806 free(console_c);
Aaron Durbin46300aa2017-09-26 16:22:38 -0600807 unmap_memory(&console_mapping);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800808}
809
Stefan Reinauera9c83612013-07-16 17:47:35 -0700810static void hexdump(unsigned long memory, int length)
811{
812 int i;
Aaron Durbinf2a38222017-09-26 17:44:28 -0600813 const uint8_t *m;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700814 int all_zero = 0;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600815 struct mapping hexdump_mapping;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700816
Aaron Durbin46300aa2017-09-26 16:22:38 -0600817 m = map_memory(&hexdump_mapping, memory, length);
818 if (!m)
819 die("Unable to map hexdump memory.\n");
Stefan Reinauera9c83612013-07-16 17:47:35 -0700820
821 for (i = 0; i < length; i += 16) {
822 int j;
823
824 all_zero++;
825 for (j = 0; j < 16; j++) {
826 if(m[i+j] != 0) {
827 all_zero = 0;
828 break;
829 }
830 }
831
832 if (all_zero < 2) {
833 printf("%08lx:", memory + i);
834 for (j = 0; j < 16; j++)
835 printf(" %02x", m[i+j]);
836 printf(" ");
837 for (j = 0; j < 16; j++)
838 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
839 printf("\n");
840 } else if (all_zero == 2) {
841 printf("...\n");
842 }
843 }
844
Aaron Durbin46300aa2017-09-26 16:22:38 -0600845 unmap_memory(&hexdump_mapping);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700846}
847
848static void dump_cbmem_hex(void)
849{
850 if (cbmem.type != LB_MEM_TABLE) {
851 fprintf(stderr, "No coreboot CBMEM area found!\n");
852 return;
853 }
854
855 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
856}
857
Jacob Garber79a2f472019-06-27 17:23:25 -0600858static void rawdump(uint64_t base, uint64_t size)
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700859{
Aaron Durbinf2a38222017-09-26 17:44:28 -0600860 const uint8_t *m;
Aaron Durbin46300aa2017-09-26 16:22:38 -0600861 struct mapping dump_mapping;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700862
Aaron Durbin46300aa2017-09-26 16:22:38 -0600863 m = map_memory(&dump_mapping, base, size);
864 if (!m)
865 die("Unable to map rawdump memory\n");
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700866
Jacob Garber414d5d82019-06-27 16:02:32 -0600867 for (uint64_t i = 0 ; i < size; i++)
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700868 printf("%c", m[i]);
Aaron Durbin46300aa2017-09-26 16:22:38 -0600869
870 unmap_memory(&dump_mapping);
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700871}
872
873static void dump_cbmem_raw(unsigned int id)
874{
Aaron Durbinf2a38222017-09-26 17:44:28 -0600875 const uint8_t *table;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700876 size_t offset;
877 uint64_t base = 0;
878 uint64_t size = 0;
879
Aaron Durbin46300aa2017-09-26 16:22:38 -0600880 table = mapping_virt(&lbtable_mapping);
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700881
882 if (table == NULL)
883 return;
884
885 offset = 0;
886
Aaron Durbin46300aa2017-09-26 16:22:38 -0600887 while (offset < mapping_size(&lbtable_mapping)) {
Aaron Durbinf2a38222017-09-26 17:44:28 -0600888 const struct lb_record *lbr;
889 const struct lb_cbmem_entry *lbe;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700890
Aaron Durbinf2a38222017-09-26 17:44:28 -0600891 lbr = (const void *)(table + offset);
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700892 offset += lbr->size;
893
894 if (lbr->tag != LB_TAG_CBMEM_ENTRY)
895 continue;
896
Aaron Durbinf2a38222017-09-26 17:44:28 -0600897 lbe = (const void *)lbr;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700898 if (lbe->id == id) {
899 debug("found id for raw dump %0x", lbe->id);
900 base = lbe->address;
901 size = lbe->entry_size;
902 break;
903 }
904 }
905
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700906 if (!base)
907 fprintf(stderr, "id %0x not found in cbtable\n", id);
908 else
909 rawdump(base, size);
910}
911
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600912struct cbmem_id_to_name {
913 uint32_t id;
914 const char *name;
915};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700916static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800917
Kyösti Mälkkieab5c122017-09-04 11:10:17 +0300918#define MAX_STAGEx 10
Jacob Garber79a2f472019-06-27 17:23:25 -0600919static void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
Stefan Reinauera9c83612013-07-16 17:47:35 -0700920{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700921 const char *name;
Kyösti Mälkkieab5c122017-09-04 11:10:17 +0300922 char stage_x[20];
Stefan Reinauera9c83612013-07-16 17:47:35 -0700923
924 name = NULL;
Jacob Garber414d5d82019-06-27 16:02:32 -0600925 for (size_t i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700926 if (cbmem_ids[i].id == id) {
927 name = cbmem_ids[i].name;
928 break;
929 }
Kyösti Mälkkieab5c122017-09-04 11:10:17 +0300930 if (id >= CBMEM_ID_STAGEx_META &&
931 id < CBMEM_ID_STAGEx_META + MAX_STAGEx) {
932 snprintf(stage_x, sizeof(stage_x), "STAGE%d META",
933 (id - CBMEM_ID_STAGEx_META));
934 name = stage_x;
935 }
936 if (id >= CBMEM_ID_STAGEx_CACHE &&
937 id < CBMEM_ID_STAGEx_CACHE + MAX_STAGEx) {
938 snprintf(stage_x, sizeof(stage_x), "STAGE%d $ ",
939 (id - CBMEM_ID_STAGEx_CACHE));
940 name = stage_x;
941 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700942 }
943
944 printf("%2d. ", n);
945 if (name == NULL)
Kyösti Mälkkieab5c122017-09-04 11:10:17 +0300946 printf("\t\t%08x", id);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700947 else
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700948 printf("%s\t%08x", name, id);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700949 printf(" %08" PRIx64 " ", base);
950 printf(" %08" PRIx64 "\n", size);
951}
952
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800953static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800954{
Aaron Durbin09c0c112015-09-30 12:33:01 -0500955 int i;
Aaron Durbinf2a38222017-09-26 17:44:28 -0600956 const uint8_t *table;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500957 size_t offset;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800958
Aaron Durbin46300aa2017-09-26 16:22:38 -0600959 table = mapping_virt(&lbtable_mapping);
Aaron Durbin09c0c112015-09-30 12:33:01 -0500960
961 if (table == NULL)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800962 return;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500963
964 printf("CBMEM table of contents:\n");
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700965 printf(" NAME ID START LENGTH\n");
Aaron Durbin09c0c112015-09-30 12:33:01 -0500966
967 i = 0;
968 offset = 0;
969
Aaron Durbin46300aa2017-09-26 16:22:38 -0600970 while (offset < mapping_size(&lbtable_mapping)) {
Aaron Durbinf2a38222017-09-26 17:44:28 -0600971 const struct lb_record *lbr;
972 const struct lb_cbmem_entry *lbe;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500973
Aaron Durbinf2a38222017-09-26 17:44:28 -0600974 lbr = (const void *)(table + offset);
Aaron Durbin09c0c112015-09-30 12:33:01 -0500975 offset += lbr->size;
976
977 if (lbr->tag != LB_TAG_CBMEM_ENTRY)
978 continue;
979
Aaron Durbinf2a38222017-09-26 17:44:28 -0600980 lbe = (const void *)lbr;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500981 cbmem_print_entry(i, lbe->id, lbe->address, lbe->entry_size);
982 i++;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800983 }
Stefan Reinauerc0199072013-01-07 16:26:10 -0800984}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800985
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800986#define COVERAGE_MAGIC 0x584d4153
987struct file {
988 uint32_t magic;
989 uint32_t next;
990 uint32_t filename;
991 uint32_t data;
992 int offset;
993 int len;
994};
995
996static int mkpath(char *path, mode_t mode)
997{
998 assert (path && *path);
999 char *p;
1000 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
1001 *p = '\0';
1002 if (mkdir(path, mode) == -1) {
1003 if (errno != EEXIST) {
1004 *p = '/';
1005 return -1;
1006 }
1007 }
1008 *p = '/';
1009 }
1010 return 0;
1011}
1012
1013static void dump_coverage(void)
1014{
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001015 uint64_t start;
Aaron Durbin09c0c112015-09-30 12:33:01 -05001016 size_t size;
Aaron Durbinf2a38222017-09-26 17:44:28 -06001017 const void *coverage;
Aaron Durbin46300aa2017-09-26 16:22:38 -06001018 struct mapping coverage_mapping;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001019 unsigned long phys_offset;
1020#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
1021
Aaron Durbin09c0c112015-09-30 12:33:01 -05001022 if (find_cbmem_entry(CBMEM_ID_COVERAGE, &start, &size)) {
1023 fprintf(stderr, "No coverage information found\n");
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001024 return;
1025 }
1026
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001027 /* Map coverage area */
Aaron Durbin46300aa2017-09-26 16:22:38 -06001028 coverage = map_memory(&coverage_mapping, start, size);
1029 if (!coverage)
1030 die("Unable to map coverage area.\n");
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001031 phys_offset = (unsigned long)coverage - (unsigned long)start;
1032
1033 printf("Dumping coverage data...\n");
1034
1035 struct file *file = (struct file *)coverage;
1036 while (file && file->magic == COVERAGE_MAGIC) {
1037 FILE *f;
1038 char *filename;
1039
1040 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
1041 filename = strdup((char *)phys_to_virt(file->filename));
1042 if (mkpath(filename, 0755) == -1) {
1043 perror("Directory for coverage data could "
1044 "not be created");
1045 exit(1);
1046 }
1047 f = fopen(filename, "wb");
1048 if (!f) {
1049 printf("Could not open %s: %s\n",
1050 filename, strerror(errno));
1051 exit(1);
1052 }
1053 if (fwrite((void *)phys_to_virt(file->data),
1054 file->len, 1, f) != 1) {
1055 printf("Could not write to %s: %s\n",
1056 filename, strerror(errno));
1057 exit(1);
1058 }
1059 fclose(f);
1060 free(filename);
1061
1062 if (file->next)
1063 file = (struct file *)phys_to_virt(file->next);
1064 else
1065 file = NULL;
1066 }
Aaron Durbin46300aa2017-09-26 16:22:38 -06001067 unmap_memory(&coverage_mapping);
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001068}
1069
1070static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001071{
1072 printf("cbmem v%s -- ", CBMEM_VERSION);
1073 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
1074 printf(
1075 "This program is free software: you can redistribute it and/or modify\n"
1076 "it under the terms of the GNU General Public License as published by\n"
1077 "the Free Software Foundation, version 2 of the License.\n\n"
1078 "This program is distributed in the hope that it will be useful,\n"
1079 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1080 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
Martin Rotheb20e602016-01-12 13:30:50 -07001081 "GNU General Public License for more details.\n\n");
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001082}
1083
Martin Roth8448ac42016-09-11 15:43:22 -06001084static void print_usage(const char *name, int exit_code)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001085{
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001086 printf("usage: %s [-cCltTLxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001087 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -08001088 " -c | --console: print cbmem console\n"
Julius Wernerb7b64a92017-04-28 16:31:46 -07001089 " -1 | --oneboot: print cbmem console for last boot only\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001090 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -08001091 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -07001092 " -x | --hexdump: print hexdump of cbmem area\n"
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001093 " -r | --rawdump ID: print rawdump of specific ID (in hex) of cbtable\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -08001094 " -t | --timestamps: print timestamp information\n"
Aaron Durbinfbff3012015-08-30 22:00:12 -05001095 " -T | --parseable-timestamps: print parseable timestamps\n"
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001096 " -L | --tcpa-log print TCPA log\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -08001097 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001098 " -v | --version: print the version\n"
1099 " -h | --help: print this help\n"
1100 "\n");
Martin Roth8448ac42016-09-11 15:43:22 -06001101 exit(exit_code);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001102}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001103
Adam Kallai66c22502018-11-30 11:31:50 +01001104#if defined(__arm__) || defined(__aarch64__)
Julius Werner337de4c2014-06-16 23:02:03 -07001105static void dt_update_cells(const char *name, int *addr_cells_ptr,
1106 int *size_cells_ptr)
1107{
1108 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
1109 return;
1110
1111 int buffer;
1112 size_t nlen = strlen(name);
1113 char *prop = alloca(nlen + sizeof("/#address-cells"));
1114 strcpy(prop, name);
1115
1116 if (*addr_cells_ptr < 0) {
1117 strcpy(prop + nlen, "/#address-cells");
1118 int fd = open(prop, O_RDONLY);
1119 if (fd < 0 && errno != ENOENT) {
1120 perror(prop);
1121 } else if (fd >= 0) {
1122 if (read(fd, &buffer, sizeof(int)) < 0)
1123 perror(prop);
1124 else
1125 *addr_cells_ptr = ntohl(buffer);
1126 close(fd);
1127 }
1128 }
1129
1130 if (*size_cells_ptr < 0) {
1131 strcpy(prop + nlen, "/#size-cells");
1132 int fd = open(prop, O_RDONLY);
1133 if (fd < 0 && errno != ENOENT) {
1134 perror(prop);
1135 } else if (fd >= 0) {
1136 if (read(fd, &buffer, sizeof(int)) < 0)
1137 perror(prop);
1138 else
1139 *size_cells_ptr = ntohl(buffer);
1140 close(fd);
1141 }
1142 }
1143}
1144
1145static char *dt_find_compat(const char *parent, const char *compat,
1146 int *addr_cells_ptr, int *size_cells_ptr)
1147{
1148 char *ret = NULL;
1149 struct dirent *entry;
1150 DIR *dir;
1151
1152 if (!(dir = opendir(parent))) {
1153 perror(parent);
1154 return NULL;
1155 }
1156
1157 /* Loop through all files in the directory (DT node). */
1158 while ((entry = readdir(dir))) {
1159 /* We only care about compatible props or subnodes. */
1160 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
1161 !strcmp(entry->d_name, "compatible")))
1162 continue;
1163
1164 /* Assemble the file name (on the stack, for speed). */
1165 size_t plen = strlen(parent);
1166 char *name = alloca(plen + strlen(entry->d_name) + 2);
1167
1168 strcpy(name, parent);
1169 name[plen] = '/';
1170 strcpy(name + plen + 1, entry->d_name);
1171
1172 /* If it's a subnode, recurse. */
1173 if (entry->d_type & DT_DIR) {
1174 ret = dt_find_compat(name, compat, addr_cells_ptr,
1175 size_cells_ptr);
1176
1177 /* There is only one matching node to find, abort. */
1178 if (ret) {
1179 /* Gather cells values on the way up. */
1180 dt_update_cells(parent, addr_cells_ptr,
1181 size_cells_ptr);
1182 break;
1183 }
1184 continue;
1185 }
1186
1187 /* If it's a compatible string, see if it's the right one. */
1188 int fd = open(name, O_RDONLY);
1189 int clen = strlen(compat);
1190 char *buffer = alloca(clen + 1);
1191
1192 if (fd < 0) {
1193 perror(name);
1194 continue;
1195 }
1196
1197 if (read(fd, buffer, clen + 1) < 0) {
1198 perror(name);
1199 close(fd);
1200 continue;
1201 }
1202 close(fd);
1203
1204 if (!strcmp(compat, buffer)) {
1205 /* Initialize these to "unset" for the way up. */
1206 *addr_cells_ptr = *size_cells_ptr = -1;
1207
1208 /* Can't leave string on the stack or we'll lose it! */
1209 ret = strdup(parent);
1210 break;
1211 }
1212 }
1213
1214 closedir(dir);
1215 return ret;
1216}
Adam Kallai66c22502018-11-30 11:31:50 +01001217#endif /* defined(__arm__) || defined(__aarch64__) */
Julius Werner337de4c2014-06-16 23:02:03 -07001218
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001219int main(int argc, char** argv)
1220{
Stefan Reinauer19f87562013-01-07 13:37:12 -08001221 int print_defaults = 1;
1222 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001223 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001224 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001225 int print_hexdump = 0;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001226 int print_rawdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001227 int print_timestamps = 0;
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001228 int print_tcpa_log = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001229 int machine_readable_timestamps = 0;
Julius Wernerb7b64a92017-04-28 16:31:46 -07001230 int one_boot_only = 0;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001231 unsigned int rawdump_id = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001232
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001233 int opt, option_index = 0;
1234 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001235 {"console", 0, 0, 'c'},
Julius Wernerb7b64a92017-04-28 16:31:46 -07001236 {"oneboot", 0, 0, '1'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001237 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -08001238 {"list", 0, 0, 'l'},
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001239 {"tcpa-log", 0, 0, 'L'},
Stefan Reinauer19f87562013-01-07 13:37:12 -08001240 {"timestamps", 0, 0, 't'},
Aaron Durbinfbff3012015-08-30 22:00:12 -05001241 {"parseable-timestamps", 0, 0, 'T'},
Stefan Reinauera9c83612013-07-16 17:47:35 -07001242 {"hexdump", 0, 0, 'x'},
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001243 {"rawdump", required_argument, 0, 'r'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001244 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001245 {"version", 0, 0, 'v'},
1246 {"help", 0, 0, 'h'},
1247 {0, 0, 0, 0}
1248 };
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001249 while ((opt = getopt_long(argc, argv, "c1CltTLxVvh?r:",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001250 long_options, &option_index)) != EOF) {
1251 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001252 case 'c':
1253 print_console = 1;
1254 print_defaults = 0;
1255 break;
Julius Wernerb7b64a92017-04-28 16:31:46 -07001256 case '1':
1257 print_console = 1;
1258 one_boot_only = 1;
1259 print_defaults = 0;
1260 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001261 case 'C':
1262 print_coverage = 1;
1263 print_defaults = 0;
1264 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001265 case 'l':
1266 print_list = 1;
1267 print_defaults = 0;
1268 break;
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001269 case 'L':
1270 print_tcpa_log = 1;
1271 print_defaults = 0;
1272 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001273 case 'x':
1274 print_hexdump = 1;
1275 print_defaults = 0;
1276 break;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001277 case 'r':
1278 print_rawdump = 1;
1279 print_defaults = 0;
1280 rawdump_id = strtoul(optarg, NULL, 16);
1281 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001282 case 't':
1283 print_timestamps = 1;
1284 print_defaults = 0;
1285 break;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001286 case 'T':
1287 print_timestamps = 1;
1288 machine_readable_timestamps = 1;
1289 print_defaults = 0;
1290 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001291 case 'V':
1292 verbose = 1;
1293 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001294 case 'v':
1295 print_version();
1296 exit(0);
1297 break;
1298 case 'h':
Martin Roth8448ac42016-09-11 15:43:22 -06001299 print_usage(argv[0], 0);
1300 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001301 case '?':
1302 default:
Martin Roth8448ac42016-09-11 15:43:22 -06001303 print_usage(argv[0], 1);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001304 break;
1305 }
1306 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001307
Arthur Heymans8cd17ea2018-08-01 17:21:32 +02001308 if (optind < argc) {
1309 fprintf(stderr, "Error: Extra parameter found.\n");
1310 print_usage(argv[0], 1);
1311 }
1312
Julius Werner337de4c2014-06-16 23:02:03 -07001313 mem_fd = open("/dev/mem", O_RDONLY, 0);
1314 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001315 fprintf(stderr, "Failed to gain memory access: %s\n",
1316 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001317 return 1;
1318 }
1319
Adam Kallai66c22502018-11-30 11:31:50 +01001320#if defined(__arm__) || defined(__aarch64__)
Julius Werner337de4c2014-06-16 23:02:03 -07001321 int addr_cells, size_cells;
1322 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1323 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001324
Julius Werner337de4c2014-06-16 23:02:03 -07001325 if (!coreboot_node) {
1326 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001327 return 1;
1328 }
1329
Julius Werner337de4c2014-06-16 23:02:03 -07001330 if (addr_cells < 0) {
1331 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1332 addr_cells = 1;
1333 }
1334
1335 int nlen = strlen(coreboot_node);
1336 char *reg = alloca(nlen + sizeof("/reg"));
1337
1338 strcpy(reg, coreboot_node);
1339 strcpy(reg + nlen, "/reg");
1340 free(coreboot_node);
1341
1342 int fd = open(reg, O_RDONLY);
1343 if (fd < 0) {
1344 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001345 return 1;
1346 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001347
Julius Werner337de4c2014-06-16 23:02:03 -07001348 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001349 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1350 u8 *dtbuffer = alloca(size_to_read);
1351 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001352 perror(reg);
1353 return 1;
1354 }
1355 close(fd);
1356
1357 /* No variable-length byte swap function anywhere in C... how sad. */
1358 u64 baseaddr = 0;
1359 for (i = 0; i < addr_cells * 4; i++) {
1360 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001361 baseaddr |= *dtbuffer;
1362 dtbuffer++;
1363 }
1364 u64 cb_table_size = 0;
1365 for (i = 0; i < size_cells * 4; i++) {
1366 cb_table_size <<= 8;
1367 cb_table_size |= *dtbuffer;
1368 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001369 }
1370
Aaron Durbin46300aa2017-09-26 16:22:38 -06001371 parse_cbtable(baseaddr, cb_table_size);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001372#else
Aaron Durbin46300aa2017-09-26 16:22:38 -06001373 unsigned long long possible_base_addresses[] = { 0, 0xf0000 };
Stefan Reinauer7f681502013-06-19 15:39:09 -07001374
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001375 /* Find and parse coreboot table */
Jacob Garber414d5d82019-06-27 16:02:32 -06001376 for (size_t j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Aaron Durbin46300aa2017-09-26 16:22:38 -06001377 if (!parse_cbtable(possible_base_addresses[j], 0))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001378 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001379 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001380#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001381
Aaron Durbin46300aa2017-09-26 16:22:38 -06001382 if (mapping_virt(&lbtable_mapping) == NULL)
1383 die("Table not found.\n");
1384
Stefan Reinauer19f87562013-01-07 13:37:12 -08001385 if (print_console)
Julius Wernerb7b64a92017-04-28 16:31:46 -07001386 dump_console(one_boot_only);
Stefan Reinauer19f87562013-01-07 13:37:12 -08001387
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001388 if (print_coverage)
1389 dump_coverage();
1390
Stefan Reinauerc0199072013-01-07 16:26:10 -08001391 if (print_list)
1392 dump_cbmem_toc();
1393
Stefan Reinauera9c83612013-07-16 17:47:35 -07001394 if (print_hexdump)
1395 dump_cbmem_hex();
1396
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001397 if (print_rawdump)
1398 dump_cbmem_raw(rawdump_id);
1399
Stefan Reinauer19f87562013-01-07 13:37:12 -08001400 if (print_defaults || print_timestamps)
Aaron Durbinfbff3012015-08-30 22:00:12 -05001401 dump_timestamps(machine_readable_timestamps);
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001402
Philipp Deppenwiesefa1f6ff2018-05-13 12:42:42 +02001403 if (print_tcpa_log)
1404 dump_tcpa_log();
1405
Aaron Durbin46300aa2017-09-26 16:22:38 -06001406 unmap_memory(&lbtable_mapping);
1407
Julius Werner337de4c2014-06-16 23:02:03 -07001408 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001409 return 0;
1410}