blob: dd80c08753f32d2e49336273b68172f500f0d20f [file] [log] [blame]
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauera9c83612013-07-16 17:47:35 -07004 * Copyright 2012 Google Inc.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Nico Huber8e4bb9282013-05-26 18:17:54 +020020#include <inttypes.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080024#include <unistd.h>
Stefan Reinauer8c594772013-04-19 14:22:29 -070025#include <inttypes.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080026#include <getopt.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080027#include <errno.h>
28#include <fcntl.h>
Stefan Reinauera9c83612013-07-16 17:47:35 -070029#include <ctype.h>
Stefan Reinauer7f681502013-06-19 15:39:09 -070030#include <arpa/inet.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080031#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/mman.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080034#include <libgen.h>
35#include <assert.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070036
Stefan Reinauer05cbce62013-01-03 14:30:33 -080037#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
38#define MAP_BYTES (1024*1024)
39
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070040#include "boot/coreboot_tables.h"
41
42typedef uint16_t u16;
43typedef uint32_t u32;
44typedef uint64_t u64;
45
46#include "cbmem.h"
47#include "timestamp.h"
48
Stefan Reinauera9c83612013-07-16 17:47:35 -070049#define CBMEM_VERSION "1.1"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080050
Stefan Reinauer05cbce62013-01-03 14:30:33 -080051/* verbose output? */
52static int verbose = 0;
53#define debug(x...) if(verbose) printf(x)
54
55/* File handle used to access /dev/mem */
56static int fd;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070057
58/*
59 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
60 * the buffer length is odd last byte is excluded from the calculation
61 */
62static u16 ipchcksum(const void *addr, unsigned size)
63{
64 const u16 *p = addr;
65 unsigned i, n = size / 2; /* don't expect odd sized blocks */
66 u32 sum = 0;
67
68 for (i = 0; i < n; i++)
69 sum += p[i];
70
71 sum = (sum >> 16) + (sum & 0xffff);
72 sum += (sum >> 16);
73 sum = ~sum & 0xffff;
74 return (u16) sum;
75}
76
77/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080078 * Functions to map / unmap physical memory into virtual address space. These
79 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070080 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080081static void *mapped_virtual;
82static void *map_memory(u64 physical)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070083{
Stefan Reinauer05cbce62013-01-03 14:30:33 -080084 void *v;
85 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -070086 u64 page = getpagesize();
Stefan Reinauera9c83612013-07-16 17:47:35 -070087 int padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -080088
89 /* Mapped memory must be aligned to page size */
90 p = physical & ~(page - 1);
91
Paul Menzel8d9ffd92013-04-13 18:25:56 +020092 debug("Mapping 1MB of physical memory at 0x%jx.\n", (intmax_t)p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -080093
94 v = mmap(NULL, MAP_BYTES, PROT_READ, MAP_SHARED, fd, p);
95
96 if (v == MAP_FAILED) {
97 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
98 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070099 exit(1);
100 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800101
102 /* Remember what we actually mapped ... */
103 mapped_virtual = v;
104
105 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700106 padding = physical & (page-1);
107 if (padding)
108 debug(" ... padding virtual address with 0x%x bytes.\n",
109 padding);
110 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800111
112 return v;
113}
114
115static void unmap_memory(void)
116{
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800117 if (mapped_virtual == NULL) {
118 fprintf(stderr, "Error unmapping memory\n");
119 return;
120 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800121 debug("Unmapping 1MB of virtual memory at %p.\n", mapped_virtual);
122 munmap(mapped_virtual, MAP_BYTES);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800123 mapped_virtual = NULL;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700124}
125
126/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800127 * Try finding the timestamp table and coreboot cbmem console starting from the
128 * passed in memory offset. Could be called recursively in case a forwarding
129 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700130 *
131 * Returns pointer to a memory buffer containg the timestamp table or zero if
132 * none found.
133 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800134
135static struct lb_cbmem_ref timestamps;
136static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800137static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800138
Stefan Reinauer8c594772013-04-19 14:22:29 -0700139/* This is a work-around for a nasty problem introduced by initially having
140 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
141 * on 64bit x86 systems because coreboot is 32bit on those systems.
142 * When the problem was found, it was corrected, but there are a lot of
143 * systems out there with a firmware that does not produce the right
144 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
145 */
146static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
147{
148 struct lb_cbmem_ref ret;
149
150 ret = *cbmem_ref;
151
152 if (cbmem_ref->size < sizeof(*cbmem_ref))
153 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
154
Stefan Reinauera9c83612013-07-16 17:47:35 -0700155 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
156
Stefan Reinauer8c594772013-04-19 14:22:29 -0700157 return ret;
158}
159
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800160static int parse_cbtable(u64 address)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700161{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800162 int i, found = 0;
163 void *buf;
164
Nico Huber8e4bb9282013-05-26 18:17:54 +0200165 debug("Looking for coreboot table at %" PRIx64 "\n", address);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800166 buf = map_memory(address);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700167
168 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800169
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700170 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800171 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700172 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800173 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700174 int j;
175
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800176 lbh = (struct lb_header *)(buf + i);
177 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
178 !lbh->header_bytes ||
179 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700180 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700181 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800182 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700183
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800184 if (ipchcksum(lbtable, lbh->table_bytes) !=
185 lbh->table_checksum) {
186 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700187 continue;
188 }
189
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800190 found = 1;
191 debug("Found!\n");
192
193 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700194 /* look for the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800195 lbr_p = (struct lb_record*) ((char *)lbtable + j);
196 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700197 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800198 case LB_TAG_MEMORY: {
199 int i = 0;
200 debug(" Found memory map.\n");
201 struct lb_memory *memory =
202 (struct lb_memory *)lbr_p;
203 while ((char *)&memory->map[i] < ((char *)lbtable
204 + lbr_p->size)) {
205 if (memory->map[i].type == LB_MEM_TABLE) {
206 debug(" LB_MEM_TABLE found.\n");
207 /* The last one found is CBMEM */
208 cbmem = memory->map[i];
209 }
210 i++;
211 }
212 continue;
213 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700214 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800215 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700216 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800217 continue;
218 }
219 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800220 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700221 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800222 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700223 }
224 case LB_TAG_FORWARD: {
225 /*
226 * This is a forwarding entry - repeat the
227 * search at the new address.
228 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800229 struct lb_forward lbf_p =
230 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800231 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800232 unmap_memory();
233 return parse_cbtable(lbf_p.forward);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700234 }
235 default:
236 break;
237 }
238
239 }
240 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800241 unmap_memory();
242
243 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700244}
245
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700246#if defined(__i386__) || defined(__x86_64__)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700247/*
248 * read CPU frequency from a sysfs file, return an frequency in Kilohertz as
249 * an int or exit on any error.
250 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800251static u64 get_cpu_freq_KHz(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700252{
253 FILE *cpuf;
254 char freqs[100];
255 int size;
256 char *endp;
257 u64 rv;
258
259 const char* freq_file =
260 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
261
262 cpuf = fopen(freq_file, "r");
263 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800264 fprintf(stderr, "Could not open %s: %s\n",
265 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700266 exit(1);
267 }
268
269 memset(freqs, 0, sizeof(freqs));
270 size = fread(freqs, 1, sizeof(freqs), cpuf);
271 if (!size || (size == sizeof(freqs))) {
272 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
273 size, freq_file);
274 exit(1);
275 }
276 fclose(cpuf);
277 rv = strtoull(freqs, &endp, 10);
278
279 if (*endp == '\0' || *endp == '\n')
280 return rv;
281 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
282 freqs, freq_file);
283 exit(1);
284}
285
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700286/* On x86 platforms timestamps are stored
287 * in CPU cycles (from rdtsc). Hence the
288 * timestamp divider is the CPU frequency
289 * in MHz.
290 */
291u64 arch_convert_raw_ts_entry(u64 ts)
292{
293 static u64 cpu_freq_mhz = 0;
294
295 if (!cpu_freq_mhz)
296 cpu_freq_mhz = get_cpu_freq_KHz() / 1000;
297
298 return ts / cpu_freq_mhz;
299}
300
301#else
302
303/* On non-x86 platforms the timestamp entries
304 * are not in clock cycles but in usecs
305 */
306u64 arch_convert_raw_ts_entry(u64 ts)
307{
308 return ts;
309}
310#endif
311
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700312/*
313 * Print an integer in 'normalized' form - with commas separating every three
314 * decimal orders. The 'comma' parameter indicates if a comma is needed after
315 * the value is printed.
316 */
317static void print_norm(u64 v, int comma)
318{
319 int first_triple = 1;
320
321 if (v > 1000) {
322 /* print the higher order sections first */
323 print_norm(v / 1000, 1);
324 first_triple = 0;
325 }
326 if (first_triple)
327 printf("%d", (u32)(v % 1000));
328 else
329 printf("%3.3d", (u32)(v % 1000));
330 if (comma)
331 printf(",");
332}
333
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700334enum additional_timestamp_id {
335 // Depthcharge entry IDs start at 1000.
336 TS_DC_START = 1000,
337
338 TS_RO_PARAMS_INIT = 1001,
339 TS_RO_VB_INIT = 1002,
340 TS_RO_VB_SELECT_FIRMWARE = 1003,
341 TS_RO_VB_SELECT_AND_LOAD_KERNEL = 1004,
342
343 TS_RW_VB_SELECT_AND_LOAD_KERNEL = 1010,
344
345 TS_VB_SELECT_AND_LOAD_KERNEL = 1020,
346
347 TS_CROSSYSTEM_DATA = 1100,
348 TS_START_KERNEL = 1101
349};
350
351static const struct timestamp_id_to_name {
352 u32 id;
353 const char *name;
354} timestamp_ids[] = {
355 { TS_START_ROMSTAGE, "start of rom stage" },
356 { TS_BEFORE_INITRAM, "before ram initialization" },
357 { TS_AFTER_INITRAM, "after ram initialization" },
358 { TS_END_ROMSTAGE, "end of romstage" },
359 { TS_START_VBOOT, "start of verified boot" },
360 { TS_END_VBOOT, "end of verified boot" },
361 { TS_START_COPYRAM, "start of copying ram stage" },
362 { TS_END_COPYRAM, "end of copying ram stage" },
363 { TS_START_RAMSTAGE, "start of ramstage" },
364 { TS_DEVICE_ENUMERATE, "device enumeration" },
365 { TS_DEVICE_CONFIGURE, "device configuration" },
366 { TS_DEVICE_ENABLE, "device enable" },
367 { TS_DEVICE_INITIALIZE, "device initialization" },
368 { TS_DEVICE_DONE, "device setup done" },
369 { TS_CBMEM_POST, "cbmem post" },
370 { TS_WRITE_TABLES, "write tables" },
371 { TS_LOAD_PAYLOAD, "load payload" },
372 { TS_ACPI_WAKE_JUMP, "ACPI wake jump" },
373 { TS_SELFBOOT_JUMP, "selfboot jump" },
374 { TS_DC_START, "depthcharge start" },
375 { TS_RO_PARAMS_INIT, "RO parameter init" },
376 { TS_RO_VB_INIT, "RO vboot init" },
377 { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" },
378 { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" },
379 { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" },
380 { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" },
381 { TS_CROSSYSTEM_DATA, "crossystem data" },
382 { TS_START_KERNEL, "start kernel" }
383};
384
385void timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
386{
387 int i;
388 const char *name;
389
390 name = "<unknown>";
391 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
392 if (timestamp_ids[i].id == id) {
393 name = timestamp_ids[i].name;
394 break;
395 }
396 }
397
398 printf("%4d:", id);
399 printf("%-30s", name);
400 print_norm(arch_convert_raw_ts_entry(stamp), 0);
401 if (prev_stamp) {
402 printf(" (");
403 print_norm(arch_convert_raw_ts_entry(stamp
404 - prev_stamp), 0);
405 printf(")");
406 }
407 printf("\n");
408}
409
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700410/* dump the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800411static void dump_timestamps(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700412{
413 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800414 struct timestamp_table *tst_p;
415
416 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
417 fprintf(stderr, "No timestamps found in coreboot table.\n");
418 return;
419 }
420
421 tst_p = (struct timestamp_table *)
422 map_memory((unsigned long)timestamps.cbmem_addr);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700423
424 printf("%d entries total:\n\n", tst_p->num_entries);
425 for (i = 0; i < tst_p->num_entries; i++) {
426 const struct timestamp_entry *tse_p = tst_p->entries + i;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700427 timestamp_print_entry(tse_p->entry_id, tse_p->entry_stamp,
428 i ? tse_p[-1].entry_stamp : 0);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700429 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800430
431 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700432}
433
Stefan Reinauer19f87562013-01-07 13:37:12 -0800434/* dump the cbmem console */
435static void dump_console(void)
436{
437 void *console_p;
438 char *console_c;
439 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100440 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800441
442 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
443 fprintf(stderr, "No console found in coreboot table.\n");
444 return;
445 }
446
447 console_p = map_memory((unsigned long)console.cbmem_addr);
448 /* The in-memory format of the console area is:
449 * u32 size
450 * u32 cursor
451 * char console[size]
452 * Hence we have to add 8 to get to the actual console string.
453 */
454 size = *(uint32_t *)console_p;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100455 cursor = *(uint32_t *) (console_p + 4);
456 /* Cursor continues to go on even after no more data fits in
457 * the buffer but the data is dropped in this case.
458 */
459 if (size > cursor)
460 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800461 console_c = malloc(size + 1);
462 if (!console_c) {
463 fprintf(stderr, "Not enough memory for console.\n");
464 exit(1);
465 }
466
467 memcpy(console_c, console_p + 8, size);
468 console_c[size] = 0;
469
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100470 printf("%s\n", console_c);
471 if (size < cursor)
472 printf("%d %s lost\n", cursor - size,
473 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800474
475 free(console_c);
476
477 unmap_memory();
478}
479
Stefan Reinauera9c83612013-07-16 17:47:35 -0700480static void hexdump(unsigned long memory, int length)
481{
482 int i;
483 uint8_t *m;
484 int all_zero = 0;
485
486 m = map_memory((intptr_t)memory);
487
488 if (length > MAP_BYTES) {
489 printf("Truncating hex dump from %d to %d bytes\n\n",
490 length, MAP_BYTES);
491 length = MAP_BYTES;
492 }
493
494 for (i = 0; i < length; i += 16) {
495 int j;
496
497 all_zero++;
498 for (j = 0; j < 16; j++) {
499 if(m[i+j] != 0) {
500 all_zero = 0;
501 break;
502 }
503 }
504
505 if (all_zero < 2) {
506 printf("%08lx:", memory + i);
507 for (j = 0; j < 16; j++)
508 printf(" %02x", m[i+j]);
509 printf(" ");
510 for (j = 0; j < 16; j++)
511 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
512 printf("\n");
513 } else if (all_zero == 2) {
514 printf("...\n");
515 }
516 }
517
518 unmap_memory();
519}
520
521static void dump_cbmem_hex(void)
522{
523 if (cbmem.type != LB_MEM_TABLE) {
524 fprintf(stderr, "No coreboot CBMEM area found!\n");
525 return;
526 }
527
528 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
529}
530
531/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
532#define DYN_CBMEM_ALIGN_SIZE (4096)
533#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
534#define CBMEM_POINTER_MAGIC 0xc0389479
535#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
536
537struct cbmem_root_pointer {
538 uint32_t magic;
539 uint32_t root;
540} __attribute__((packed));
541
542struct dynamic_cbmem_entry {
543 uint32_t magic;
544 uint32_t start;
545 uint32_t size;
546 uint32_t id;
547} __attribute__((packed));
548
549struct cbmem_root {
550 uint32_t max_entries;
551 uint32_t num_entries;
552 uint32_t locked;
553 uint32_t size;
554 struct dynamic_cbmem_entry entries[0];
555} __attribute__((packed));
556
Stefan Reinauerc0199072013-01-07 16:26:10 -0800557#define CBMEM_MAGIC 0x434f5245
558#define MAX_CBMEM_ENTRIES 16
559
560struct cbmem_entry {
561 uint32_t magic;
562 uint32_t id;
563 uint64_t base;
564 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700565} __attribute__((packed));
566
567static const struct cbmem_id_to_name {
568 u32 id;
569 const char *name;
570} cbmem_ids[] = {
571 { CBMEM_ID_FREESPACE, "FREE SPACE " },
572 { CBMEM_ID_GDT, "GDT " },
573 { CBMEM_ID_ACPI, "ACPI " },
574 { CBMEM_ID_CBTABLE, "COREBOOT " },
575 { CBMEM_ID_PIRQ, "IRQ TABLE " },
576 { CBMEM_ID_MPTABLE, "SMP TABLE " },
577 { CBMEM_ID_RESUME, "ACPI RESUME" },
578 { CBMEM_ID_RESUME_SCRATCH, "ACPISCRATCH" },
579 { CBMEM_ID_ACPI_GNVS, "ACPI GNVS " },
580 { CBMEM_ID_ACPI_GNVS_PTR, "GNVS PTR " },
581 { CBMEM_ID_SMBIOS, "SMBIOS " },
582 { CBMEM_ID_TIMESTAMP, "TIME STAMP " },
583 { CBMEM_ID_MRCDATA, "MRC DATA " },
584 { CBMEM_ID_CONSOLE, "CONSOLE " },
585 { CBMEM_ID_ELOG, "ELOG " },
586 { CBMEM_ID_COVERAGE, "COVERAGE " },
587 { CBMEM_ID_ROMSTAGE_INFO, "ROMSTAGE " },
588 { CBMEM_ID_ROMSTAGE_RAM_STACK, "ROMSTG STCK" },
589 { CBMEM_ID_RAMSTAGE, "RAMSTAGE " },
590 { CBMEM_ID_RAMSTAGE_CACHE, "RAMSTAGE $ " },
591 { CBMEM_ID_ROOT, "CBMEM ROOT " },
592 { CBMEM_ID_VBOOT_HANDOFF, "VBOOT " },
593 { CBMEM_ID_CAR_GLOBALS, "CAR GLOBALS" },
Stefan Reinauerc0199072013-01-07 16:26:10 -0800594};
595
Stefan Reinauera9c83612013-07-16 17:47:35 -0700596void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
597{
598 int i;
599 const char *name;
600
601 name = NULL;
602 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
603 if (cbmem_ids[i].id == id) {
604 name = cbmem_ids[i].name;
605 break;
606 }
607 }
608
609 printf("%2d. ", n);
610 if (name == NULL)
611 printf("%08x ", id);
612 else
613 printf("%s", name);
614 printf(" %08" PRIx64 " ", base);
615 printf(" %08" PRIx64 "\n", size);
616}
617
618static void dump_static_cbmem_toc(struct cbmem_entry *entries)
619{
620 int i;
621
622 printf("CBMEM table of contents:\n");
623 printf(" ID START LENGTH\n");
624
625 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
626 if (entries[i].magic != CBMEM_MAGIC)
627 break;
628 cbmem_print_entry(i, entries[i].id,
629 entries[i].base, entries[i].size);
630 }
631}
632
633static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
634{
635 int i;
636 debug("CBMEM: max_entries=%d num_entries=%d locked=0x%x, size=%d\n\n",
637 root->max_entries, root->num_entries, root->locked, root->size);
638
639 printf("CBMEM table of contents:\n");
640 printf(" ID START LENGTH\n");
641
642 for (i = 0; i < root->num_entries; i++) {
643 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
644 break;
645 cbmem_print_entry(i, root->entries[i].id,
646 root->entries[i].start, root->entries[i].size);
647 }
648}
649
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800650static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800651{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800652 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700653 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800654 struct cbmem_entry *entries;
655
656 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700657 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800658 return;
659 }
660
661 start = unpack_lb64(cbmem.start);
662
Stefan Reinauera9c83612013-07-16 17:47:35 -0700663 cbmem_area = map_memory(start);
664 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800665
Stefan Reinauera9c83612013-07-16 17:47:35 -0700666 if (entries[0].magic == CBMEM_MAGIC) {
667 dump_static_cbmem_toc(entries);
668 } else {
669 uint64_t rootptr;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800670
Stefan Reinauera9c83612013-07-16 17:47:35 -0700671 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
672 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
673 rootptr -= sizeof(struct cbmem_root_pointer);
674 unmap_memory();
675 struct cbmem_root_pointer *r =
676 (struct cbmem_root_pointer *)map_memory(rootptr);
677 if (r->magic == CBMEM_POINTER_MAGIC) {
678 struct cbmem_root *root;
679 uint64_t rootaddr = r->root;
680 unmap_memory();
681 /* Note that this only works because our default mmap
682 * size is 1MiB which happens to be larger than the
683 * root entry size which is default to be 4KiB.
684 */
685 root = (struct cbmem_root *)map_memory(rootaddr);
686 dump_dynamic_cbmem_toc(root);
687 } else
688 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800689 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700690
Stefan Reinauerc0199072013-01-07 16:26:10 -0800691 unmap_memory();
692}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800693
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800694#define COVERAGE_MAGIC 0x584d4153
695struct file {
696 uint32_t magic;
697 uint32_t next;
698 uint32_t filename;
699 uint32_t data;
700 int offset;
701 int len;
702};
703
704static int mkpath(char *path, mode_t mode)
705{
706 assert (path && *path);
707 char *p;
708 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
709 *p = '\0';
710 if (mkdir(path, mode) == -1) {
711 if (errno != EEXIST) {
712 *p = '/';
713 return -1;
714 }
715 }
716 *p = '/';
717 }
718 return 0;
719}
720
721static void dump_coverage(void)
722{
723 int i, found = 0;
724 uint64_t start;
725 struct cbmem_entry *entries;
726 void *coverage;
727 unsigned long phys_offset;
728#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
729
730 if (cbmem.type != LB_MEM_TABLE) {
731 fprintf(stderr, "No coreboot table area found!\n");
732 return;
733 }
734
735 start = unpack_lb64(cbmem.start);
736
737 entries = (struct cbmem_entry *)map_memory(start);
738
739 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
740 if (entries[i].magic != CBMEM_MAGIC)
741 break;
742 if (entries[i].id == CBMEM_ID_COVERAGE) {
743 found = 1;
744 break;
745 }
746 }
747
748 if (!found) {
749 unmap_memory();
750 fprintf(stderr, "No coverage information found in"
751 " CBMEM area.\n");
752 return;
753 }
754
755 start = entries[i].base;
756 unmap_memory();
757 /* Map coverage area */
758 coverage = map_memory(start);
759 phys_offset = (unsigned long)coverage - (unsigned long)start;
760
761 printf("Dumping coverage data...\n");
762
763 struct file *file = (struct file *)coverage;
764 while (file && file->magic == COVERAGE_MAGIC) {
765 FILE *f;
766 char *filename;
767
768 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
769 filename = strdup((char *)phys_to_virt(file->filename));
770 if (mkpath(filename, 0755) == -1) {
771 perror("Directory for coverage data could "
772 "not be created");
773 exit(1);
774 }
775 f = fopen(filename, "wb");
776 if (!f) {
777 printf("Could not open %s: %s\n",
778 filename, strerror(errno));
779 exit(1);
780 }
781 if (fwrite((void *)phys_to_virt(file->data),
782 file->len, 1, f) != 1) {
783 printf("Could not write to %s: %s\n",
784 filename, strerror(errno));
785 exit(1);
786 }
787 fclose(f);
788 free(filename);
789
790 if (file->next)
791 file = (struct file *)phys_to_virt(file->next);
792 else
793 file = NULL;
794 }
795 unmap_memory();
796}
797
798static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800799{
800 printf("cbmem v%s -- ", CBMEM_VERSION);
801 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
802 printf(
803 "This program is free software: you can redistribute it and/or modify\n"
804 "it under the terms of the GNU General Public License as published by\n"
805 "the Free Software Foundation, version 2 of the License.\n\n"
806 "This program is distributed in the hope that it will be useful,\n"
807 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
808 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
809 "GNU General Public License for more details.\n\n"
810 "You should have received a copy of the GNU General Public License\n"
811 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
812}
813
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800814static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800815{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700816 printf("usage: %s [-cCltxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800817 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800818 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800819 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800820 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700821 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800822 " -t | --timestamps: print timestamp information\n"
823 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800824 " -v | --version: print the version\n"
825 " -h | --help: print this help\n"
826 "\n");
827 exit(1);
828}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700829
830int main(int argc, char** argv)
831{
Stefan Reinauer19f87562013-01-07 13:37:12 -0800832 int print_defaults = 1;
833 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800834 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800835 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700836 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800837 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800838
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800839 int opt, option_index = 0;
840 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800841 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800842 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -0800843 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -0800844 {"timestamps", 0, 0, 't'},
Stefan Reinauera9c83612013-07-16 17:47:35 -0700845 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800846 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800847 {"version", 0, 0, 'v'},
848 {"help", 0, 0, 'h'},
849 {0, 0, 0, 0}
850 };
Stefan Reinauera9c83612013-07-16 17:47:35 -0700851 while ((opt = getopt_long(argc, argv, "cCltxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800852 long_options, &option_index)) != EOF) {
853 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800854 case 'c':
855 print_console = 1;
856 print_defaults = 0;
857 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800858 case 'C':
859 print_coverage = 1;
860 print_defaults = 0;
861 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800862 case 'l':
863 print_list = 1;
864 print_defaults = 0;
865 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700866 case 'x':
867 print_hexdump = 1;
868 print_defaults = 0;
869 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800870 case 't':
871 print_timestamps = 1;
872 print_defaults = 0;
873 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800874 case 'V':
875 verbose = 1;
876 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800877 case 'v':
878 print_version();
879 exit(0);
880 break;
881 case 'h':
882 case '?':
883 default:
884 print_usage(argv[0]);
885 exit(0);
886 break;
887 }
888 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700889
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800890 fd = open("/dev/mem", O_RDONLY, 0);
891 if (fd < 0) {
892 fprintf(stderr, "Failed to gain memory access: %s\n",
893 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700894 return 1;
895 }
896
Stefan Reinauer7f681502013-06-19 15:39:09 -0700897#ifdef __arm__
898 int dt_fd;
899 uint32_t cbtable_base;
900
901 dt_fd = open("/proc/device-tree/firmware/coreboot/coreboot-table",
902 O_RDONLY, 0);
903 if (dt_fd < 0) {
904 fprintf(stderr, "Failed to open device tree node: %s\n",
905 strerror(errno));
906 return 1;
907 }
908
909 if (read(dt_fd, &cbtable_base, 4) != 4) {
910 fprintf(stderr, "Failed to read device tree node: %s\n",
911 strerror(errno));
912 return 1;
913 }
914 close(dt_fd);
915
916 parse_cbtable(ntohl(cbtable_base));
917#else
918 int j;
919 static const int possible_base_addresses[] = { 0, 0xf0000 };
920
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800921 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700922 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800923 if (parse_cbtable(possible_base_addresses[j]))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700924 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700925 }
Stefan Reinauer7f681502013-06-19 15:39:09 -0700926#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700927
Stefan Reinauer19f87562013-01-07 13:37:12 -0800928 if (print_console)
929 dump_console();
930
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800931 if (print_coverage)
932 dump_coverage();
933
Stefan Reinauerc0199072013-01-07 16:26:10 -0800934 if (print_list)
935 dump_cbmem_toc();
936
Stefan Reinauera9c83612013-07-16 17:47:35 -0700937 if (print_hexdump)
938 dump_cbmem_hex();
939
Stefan Reinauer19f87562013-01-07 13:37:12 -0800940 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800941 dump_timestamps();
942
943 close(fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700944 return 0;
945}