blob: 9ad121467d42dc2468ad85447672b3f6e1c78542 [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;
Aaron Durbinab180d82014-03-31 11:59:58 -050082static size_t mapped_size;
83
84static inline size_t size_to_mib(size_t sz)
85{
86 return sz >> 20;
87}
88
89static void unmap_memory(void)
90{
91 if (mapped_virtual == NULL) {
92 fprintf(stderr, "Error unmapping memory\n");
93 return;
94 }
95 debug("Unmapping %zuMB of virtual memory at %p.\n",
96 size_to_mib(mapped_size), mapped_virtual);
97 munmap(mapped_virtual, mapped_size);
98 mapped_virtual = NULL;
99 mapped_size = 0;
100}
101
102static void *map_memory_size(u64 physical, size_t size)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700103{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800104 void *v;
105 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -0700106 u64 page = getpagesize();
Aaron Durbinab180d82014-03-31 11:59:58 -0500107 size_t padding;
108
109 if (mapped_virtual != NULL)
110 unmap_memory();
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800111
112 /* Mapped memory must be aligned to page size */
113 p = physical & ~(page - 1);
Aaron Durbinab180d82014-03-31 11:59:58 -0500114 padding = physical & (page-1);
115 size += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800116
Aaron Durbinab180d82014-03-31 11:59:58 -0500117 debug("Mapping %zuMB of physical memory at 0x%jx.\n",
118 size_to_mib(size), (intmax_t)p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800119
Aaron Durbinab180d82014-03-31 11:59:58 -0500120 v = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800121
122 if (v == MAP_FAILED) {
123 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
124 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700125 exit(1);
126 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800127
128 /* Remember what we actually mapped ... */
129 mapped_virtual = v;
Aaron Durbinab180d82014-03-31 11:59:58 -0500130 mapped_size = size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800131
132 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700133 if (padding)
Aaron Durbinab180d82014-03-31 11:59:58 -0500134 debug(" ... padding virtual address with 0x%zx bytes.\n",
Stefan Reinauera9c83612013-07-16 17:47:35 -0700135 padding);
136 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800137
138 return v;
139}
140
Aaron Durbinab180d82014-03-31 11:59:58 -0500141static void *map_memory(u64 physical)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800142{
Aaron Durbinab180d82014-03-31 11:59:58 -0500143 return map_memory_size(physical, MAP_BYTES);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700144}
145
146/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800147 * Try finding the timestamp table and coreboot cbmem console starting from the
148 * passed in memory offset. Could be called recursively in case a forwarding
149 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700150 *
151 * Returns pointer to a memory buffer containg the timestamp table or zero if
152 * none found.
153 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800154
155static struct lb_cbmem_ref timestamps;
156static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800157static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800158
Stefan Reinauer8c594772013-04-19 14:22:29 -0700159/* This is a work-around for a nasty problem introduced by initially having
160 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
161 * on 64bit x86 systems because coreboot is 32bit on those systems.
162 * When the problem was found, it was corrected, but there are a lot of
163 * systems out there with a firmware that does not produce the right
164 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
165 */
166static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
167{
168 struct lb_cbmem_ref ret;
169
170 ret = *cbmem_ref;
171
172 if (cbmem_ref->size < sizeof(*cbmem_ref))
173 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
174
Stefan Reinauera9c83612013-07-16 17:47:35 -0700175 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
176
Stefan Reinauer8c594772013-04-19 14:22:29 -0700177 return ret;
178}
179
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800180static int parse_cbtable(u64 address)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700181{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800182 int i, found = 0;
183 void *buf;
184
Nico Huber8e4bb9282013-05-26 18:17:54 +0200185 debug("Looking for coreboot table at %" PRIx64 "\n", address);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800186 buf = map_memory(address);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700187
188 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800189
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700190 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800191 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700192 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800193 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700194 int j;
195
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800196 lbh = (struct lb_header *)(buf + i);
197 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
198 !lbh->header_bytes ||
199 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700200 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700201 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800202 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700203
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800204 if (ipchcksum(lbtable, lbh->table_bytes) !=
205 lbh->table_checksum) {
206 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700207 continue;
208 }
209
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800210 found = 1;
211 debug("Found!\n");
212
213 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700214 /* look for the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800215 lbr_p = (struct lb_record*) ((char *)lbtable + j);
216 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700217 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800218 case LB_TAG_MEMORY: {
219 int i = 0;
220 debug(" Found memory map.\n");
221 struct lb_memory *memory =
222 (struct lb_memory *)lbr_p;
223 while ((char *)&memory->map[i] < ((char *)lbtable
224 + lbr_p->size)) {
225 if (memory->map[i].type == LB_MEM_TABLE) {
226 debug(" LB_MEM_TABLE found.\n");
227 /* The last one found is CBMEM */
228 cbmem = memory->map[i];
229 }
230 i++;
231 }
232 continue;
233 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700234 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800235 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700236 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800237 continue;
238 }
239 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800240 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700241 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800242 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700243 }
244 case LB_TAG_FORWARD: {
245 /*
246 * This is a forwarding entry - repeat the
247 * search at the new address.
248 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800249 struct lb_forward lbf_p =
250 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800251 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800252 unmap_memory();
253 return parse_cbtable(lbf_p.forward);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700254 }
255 default:
256 break;
257 }
258
259 }
260 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800261 unmap_memory();
262
263 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700264}
265
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700266#if defined(__i386__) || defined(__x86_64__)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700267/*
268 * read CPU frequency from a sysfs file, return an frequency in Kilohertz as
269 * an int or exit on any error.
270 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800271static u64 get_cpu_freq_KHz(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700272{
273 FILE *cpuf;
274 char freqs[100];
275 int size;
276 char *endp;
277 u64 rv;
278
279 const char* freq_file =
280 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
281
282 cpuf = fopen(freq_file, "r");
283 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800284 fprintf(stderr, "Could not open %s: %s\n",
285 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700286 exit(1);
287 }
288
289 memset(freqs, 0, sizeof(freqs));
290 size = fread(freqs, 1, sizeof(freqs), cpuf);
291 if (!size || (size == sizeof(freqs))) {
292 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
293 size, freq_file);
294 exit(1);
295 }
296 fclose(cpuf);
297 rv = strtoull(freqs, &endp, 10);
298
299 if (*endp == '\0' || *endp == '\n')
300 return rv;
301 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
302 freqs, freq_file);
303 exit(1);
304}
305
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700306/* On x86 platforms timestamps are stored
307 * in CPU cycles (from rdtsc). Hence the
308 * timestamp divider is the CPU frequency
309 * in MHz.
310 */
311u64 arch_convert_raw_ts_entry(u64 ts)
312{
313 static u64 cpu_freq_mhz = 0;
314
315 if (!cpu_freq_mhz)
316 cpu_freq_mhz = get_cpu_freq_KHz() / 1000;
317
318 return ts / cpu_freq_mhz;
319}
320
321#else
322
323/* On non-x86 platforms the timestamp entries
324 * are not in clock cycles but in usecs
325 */
326u64 arch_convert_raw_ts_entry(u64 ts)
327{
328 return ts;
329}
330#endif
331
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700332/*
333 * Print an integer in 'normalized' form - with commas separating every three
334 * decimal orders. The 'comma' parameter indicates if a comma is needed after
335 * the value is printed.
336 */
337static void print_norm(u64 v, int comma)
338{
339 int first_triple = 1;
340
341 if (v > 1000) {
342 /* print the higher order sections first */
343 print_norm(v / 1000, 1);
344 first_triple = 0;
345 }
346 if (first_triple)
347 printf("%d", (u32)(v % 1000));
348 else
349 printf("%3.3d", (u32)(v % 1000));
350 if (comma)
351 printf(",");
352}
353
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700354enum additional_timestamp_id {
355 // Depthcharge entry IDs start at 1000.
356 TS_DC_START = 1000,
357
358 TS_RO_PARAMS_INIT = 1001,
359 TS_RO_VB_INIT = 1002,
360 TS_RO_VB_SELECT_FIRMWARE = 1003,
361 TS_RO_VB_SELECT_AND_LOAD_KERNEL = 1004,
362
363 TS_RW_VB_SELECT_AND_LOAD_KERNEL = 1010,
364
365 TS_VB_SELECT_AND_LOAD_KERNEL = 1020,
366
367 TS_CROSSYSTEM_DATA = 1100,
368 TS_START_KERNEL = 1101
369};
370
371static const struct timestamp_id_to_name {
372 u32 id;
373 const char *name;
374} timestamp_ids[] = {
375 { TS_START_ROMSTAGE, "start of rom stage" },
376 { TS_BEFORE_INITRAM, "before ram initialization" },
377 { TS_AFTER_INITRAM, "after ram initialization" },
378 { TS_END_ROMSTAGE, "end of romstage" },
379 { TS_START_VBOOT, "start of verified boot" },
380 { TS_END_VBOOT, "end of verified boot" },
381 { TS_START_COPYRAM, "start of copying ram stage" },
382 { TS_END_COPYRAM, "end of copying ram stage" },
383 { TS_START_RAMSTAGE, "start of ramstage" },
384 { TS_DEVICE_ENUMERATE, "device enumeration" },
385 { TS_DEVICE_CONFIGURE, "device configuration" },
386 { TS_DEVICE_ENABLE, "device enable" },
387 { TS_DEVICE_INITIALIZE, "device initialization" },
388 { TS_DEVICE_DONE, "device setup done" },
389 { TS_CBMEM_POST, "cbmem post" },
390 { TS_WRITE_TABLES, "write tables" },
391 { TS_LOAD_PAYLOAD, "load payload" },
392 { TS_ACPI_WAKE_JUMP, "ACPI wake jump" },
393 { TS_SELFBOOT_JUMP, "selfboot jump" },
394 { TS_DC_START, "depthcharge start" },
395 { TS_RO_PARAMS_INIT, "RO parameter init" },
396 { TS_RO_VB_INIT, "RO vboot init" },
397 { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" },
398 { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" },
399 { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" },
400 { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" },
401 { TS_CROSSYSTEM_DATA, "crossystem data" },
402 { TS_START_KERNEL, "start kernel" }
403};
404
405void timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
406{
407 int i;
408 const char *name;
409
410 name = "<unknown>";
411 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
412 if (timestamp_ids[i].id == id) {
413 name = timestamp_ids[i].name;
414 break;
415 }
416 }
417
418 printf("%4d:", id);
419 printf("%-30s", name);
420 print_norm(arch_convert_raw_ts_entry(stamp), 0);
421 if (prev_stamp) {
422 printf(" (");
423 print_norm(arch_convert_raw_ts_entry(stamp
424 - prev_stamp), 0);
425 printf(")");
426 }
427 printf("\n");
428}
429
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700430/* dump the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800431static void dump_timestamps(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700432{
433 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800434 struct timestamp_table *tst_p;
435
436 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
437 fprintf(stderr, "No timestamps found in coreboot table.\n");
438 return;
439 }
440
441 tst_p = (struct timestamp_table *)
442 map_memory((unsigned long)timestamps.cbmem_addr);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700443
444 printf("%d entries total:\n\n", tst_p->num_entries);
445 for (i = 0; i < tst_p->num_entries; i++) {
446 const struct timestamp_entry *tse_p = tst_p->entries + i;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700447 timestamp_print_entry(tse_p->entry_id, tse_p->entry_stamp,
448 i ? tse_p[-1].entry_stamp : 0);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700449 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800450
451 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700452}
453
Stefan Reinauer19f87562013-01-07 13:37:12 -0800454/* dump the cbmem console */
455static void dump_console(void)
456{
457 void *console_p;
458 char *console_c;
459 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100460 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800461
462 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
463 fprintf(stderr, "No console found in coreboot table.\n");
464 return;
465 }
466
467 console_p = map_memory((unsigned long)console.cbmem_addr);
468 /* The in-memory format of the console area is:
469 * u32 size
470 * u32 cursor
471 * char console[size]
472 * Hence we have to add 8 to get to the actual console string.
473 */
474 size = *(uint32_t *)console_p;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100475 cursor = *(uint32_t *) (console_p + 4);
476 /* Cursor continues to go on even after no more data fits in
477 * the buffer but the data is dropped in this case.
478 */
479 if (size > cursor)
480 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800481 console_c = malloc(size + 1);
482 if (!console_c) {
483 fprintf(stderr, "Not enough memory for console.\n");
484 exit(1);
485 }
486
Aaron Durbinab180d82014-03-31 11:59:58 -0500487 console_p = map_memory_size((unsigned long)console.cbmem_addr,
488 size + sizeof(size) + sizeof(cursor));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800489 memcpy(console_c, console_p + 8, size);
490 console_c[size] = 0;
491
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100492 printf("%s\n", console_c);
493 if (size < cursor)
494 printf("%d %s lost\n", cursor - size,
495 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800496
497 free(console_c);
498
499 unmap_memory();
500}
501
Stefan Reinauera9c83612013-07-16 17:47:35 -0700502static void hexdump(unsigned long memory, int length)
503{
504 int i;
505 uint8_t *m;
506 int all_zero = 0;
507
508 m = map_memory((intptr_t)memory);
509
510 if (length > MAP_BYTES) {
511 printf("Truncating hex dump from %d to %d bytes\n\n",
512 length, MAP_BYTES);
513 length = MAP_BYTES;
514 }
515
516 for (i = 0; i < length; i += 16) {
517 int j;
518
519 all_zero++;
520 for (j = 0; j < 16; j++) {
521 if(m[i+j] != 0) {
522 all_zero = 0;
523 break;
524 }
525 }
526
527 if (all_zero < 2) {
528 printf("%08lx:", memory + i);
529 for (j = 0; j < 16; j++)
530 printf(" %02x", m[i+j]);
531 printf(" ");
532 for (j = 0; j < 16; j++)
533 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
534 printf("\n");
535 } else if (all_zero == 2) {
536 printf("...\n");
537 }
538 }
539
540 unmap_memory();
541}
542
543static void dump_cbmem_hex(void)
544{
545 if (cbmem.type != LB_MEM_TABLE) {
546 fprintf(stderr, "No coreboot CBMEM area found!\n");
547 return;
548 }
549
550 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
551}
552
553/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
554#define DYN_CBMEM_ALIGN_SIZE (4096)
555#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
556#define CBMEM_POINTER_MAGIC 0xc0389479
557#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
558
559struct cbmem_root_pointer {
560 uint32_t magic;
561 uint32_t root;
562} __attribute__((packed));
563
564struct dynamic_cbmem_entry {
565 uint32_t magic;
566 uint32_t start;
567 uint32_t size;
568 uint32_t id;
569} __attribute__((packed));
570
571struct cbmem_root {
572 uint32_t max_entries;
573 uint32_t num_entries;
574 uint32_t locked;
575 uint32_t size;
576 struct dynamic_cbmem_entry entries[0];
577} __attribute__((packed));
578
Stefan Reinauerc0199072013-01-07 16:26:10 -0800579#define CBMEM_MAGIC 0x434f5245
580#define MAX_CBMEM_ENTRIES 16
581
582struct cbmem_entry {
583 uint32_t magic;
584 uint32_t id;
585 uint64_t base;
586 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700587} __attribute__((packed));
588
589static const struct cbmem_id_to_name {
590 u32 id;
591 const char *name;
592} cbmem_ids[] = {
593 { CBMEM_ID_FREESPACE, "FREE SPACE " },
594 { CBMEM_ID_GDT, "GDT " },
595 { CBMEM_ID_ACPI, "ACPI " },
596 { CBMEM_ID_CBTABLE, "COREBOOT " },
597 { CBMEM_ID_PIRQ, "IRQ TABLE " },
598 { CBMEM_ID_MPTABLE, "SMP TABLE " },
599 { CBMEM_ID_RESUME, "ACPI RESUME" },
600 { CBMEM_ID_RESUME_SCRATCH, "ACPISCRATCH" },
601 { CBMEM_ID_ACPI_GNVS, "ACPI GNVS " },
602 { CBMEM_ID_ACPI_GNVS_PTR, "GNVS PTR " },
603 { CBMEM_ID_SMBIOS, "SMBIOS " },
604 { CBMEM_ID_TIMESTAMP, "TIME STAMP " },
605 { CBMEM_ID_MRCDATA, "MRC DATA " },
606 { CBMEM_ID_CONSOLE, "CONSOLE " },
607 { CBMEM_ID_ELOG, "ELOG " },
608 { CBMEM_ID_COVERAGE, "COVERAGE " },
609 { CBMEM_ID_ROMSTAGE_INFO, "ROMSTAGE " },
610 { CBMEM_ID_ROMSTAGE_RAM_STACK, "ROMSTG STCK" },
611 { CBMEM_ID_RAMSTAGE, "RAMSTAGE " },
612 { CBMEM_ID_RAMSTAGE_CACHE, "RAMSTAGE $ " },
613 { CBMEM_ID_ROOT, "CBMEM ROOT " },
614 { CBMEM_ID_VBOOT_HANDOFF, "VBOOT " },
615 { CBMEM_ID_CAR_GLOBALS, "CAR GLOBALS" },
Stefan Reinauerc0199072013-01-07 16:26:10 -0800616};
617
Stefan Reinauera9c83612013-07-16 17:47:35 -0700618void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
619{
620 int i;
621 const char *name;
622
623 name = NULL;
624 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
625 if (cbmem_ids[i].id == id) {
626 name = cbmem_ids[i].name;
627 break;
628 }
629 }
630
631 printf("%2d. ", n);
632 if (name == NULL)
633 printf("%08x ", id);
634 else
635 printf("%s", name);
636 printf(" %08" PRIx64 " ", base);
637 printf(" %08" PRIx64 "\n", size);
638}
639
640static void dump_static_cbmem_toc(struct cbmem_entry *entries)
641{
642 int i;
643
644 printf("CBMEM table of contents:\n");
645 printf(" ID START LENGTH\n");
646
647 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
648 if (entries[i].magic != CBMEM_MAGIC)
649 break;
650 cbmem_print_entry(i, entries[i].id,
651 entries[i].base, entries[i].size);
652 }
653}
654
655static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
656{
657 int i;
658 debug("CBMEM: max_entries=%d num_entries=%d locked=0x%x, size=%d\n\n",
659 root->max_entries, root->num_entries, root->locked, root->size);
660
661 printf("CBMEM table of contents:\n");
662 printf(" ID START LENGTH\n");
663
664 for (i = 0; i < root->num_entries; i++) {
665 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
666 break;
667 cbmem_print_entry(i, root->entries[i].id,
668 root->entries[i].start, root->entries[i].size);
669 }
670}
671
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800672static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800673{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800674 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700675 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800676 struct cbmem_entry *entries;
677
678 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700679 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800680 return;
681 }
682
683 start = unpack_lb64(cbmem.start);
684
Stefan Reinauera9c83612013-07-16 17:47:35 -0700685 cbmem_area = map_memory(start);
686 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800687
Stefan Reinauera9c83612013-07-16 17:47:35 -0700688 if (entries[0].magic == CBMEM_MAGIC) {
689 dump_static_cbmem_toc(entries);
690 } else {
691 uint64_t rootptr;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800692
Stefan Reinauera9c83612013-07-16 17:47:35 -0700693 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
694 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
695 rootptr -= sizeof(struct cbmem_root_pointer);
696 unmap_memory();
697 struct cbmem_root_pointer *r =
698 (struct cbmem_root_pointer *)map_memory(rootptr);
699 if (r->magic == CBMEM_POINTER_MAGIC) {
700 struct cbmem_root *root;
701 uint64_t rootaddr = r->root;
702 unmap_memory();
703 /* Note that this only works because our default mmap
704 * size is 1MiB which happens to be larger than the
705 * root entry size which is default to be 4KiB.
706 */
707 root = (struct cbmem_root *)map_memory(rootaddr);
708 dump_dynamic_cbmem_toc(root);
709 } else
710 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800711 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700712
Stefan Reinauerc0199072013-01-07 16:26:10 -0800713 unmap_memory();
714}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800715
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800716#define COVERAGE_MAGIC 0x584d4153
717struct file {
718 uint32_t magic;
719 uint32_t next;
720 uint32_t filename;
721 uint32_t data;
722 int offset;
723 int len;
724};
725
726static int mkpath(char *path, mode_t mode)
727{
728 assert (path && *path);
729 char *p;
730 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
731 *p = '\0';
732 if (mkdir(path, mode) == -1) {
733 if (errno != EEXIST) {
734 *p = '/';
735 return -1;
736 }
737 }
738 *p = '/';
739 }
740 return 0;
741}
742
743static void dump_coverage(void)
744{
745 int i, found = 0;
746 uint64_t start;
747 struct cbmem_entry *entries;
748 void *coverage;
749 unsigned long phys_offset;
750#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
751
752 if (cbmem.type != LB_MEM_TABLE) {
753 fprintf(stderr, "No coreboot table area found!\n");
754 return;
755 }
756
757 start = unpack_lb64(cbmem.start);
758
759 entries = (struct cbmem_entry *)map_memory(start);
760
761 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
762 if (entries[i].magic != CBMEM_MAGIC)
763 break;
764 if (entries[i].id == CBMEM_ID_COVERAGE) {
765 found = 1;
766 break;
767 }
768 }
769
770 if (!found) {
771 unmap_memory();
772 fprintf(stderr, "No coverage information found in"
773 " CBMEM area.\n");
774 return;
775 }
776
777 start = entries[i].base;
778 unmap_memory();
779 /* Map coverage area */
780 coverage = map_memory(start);
781 phys_offset = (unsigned long)coverage - (unsigned long)start;
782
783 printf("Dumping coverage data...\n");
784
785 struct file *file = (struct file *)coverage;
786 while (file && file->magic == COVERAGE_MAGIC) {
787 FILE *f;
788 char *filename;
789
790 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
791 filename = strdup((char *)phys_to_virt(file->filename));
792 if (mkpath(filename, 0755) == -1) {
793 perror("Directory for coverage data could "
794 "not be created");
795 exit(1);
796 }
797 f = fopen(filename, "wb");
798 if (!f) {
799 printf("Could not open %s: %s\n",
800 filename, strerror(errno));
801 exit(1);
802 }
803 if (fwrite((void *)phys_to_virt(file->data),
804 file->len, 1, f) != 1) {
805 printf("Could not write to %s: %s\n",
806 filename, strerror(errno));
807 exit(1);
808 }
809 fclose(f);
810 free(filename);
811
812 if (file->next)
813 file = (struct file *)phys_to_virt(file->next);
814 else
815 file = NULL;
816 }
817 unmap_memory();
818}
819
820static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800821{
822 printf("cbmem v%s -- ", CBMEM_VERSION);
823 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
824 printf(
825 "This program is free software: you can redistribute it and/or modify\n"
826 "it under the terms of the GNU General Public License as published by\n"
827 "the Free Software Foundation, version 2 of the License.\n\n"
828 "This program is distributed in the hope that it will be useful,\n"
829 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
830 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
831 "GNU General Public License for more details.\n\n"
832 "You should have received a copy of the GNU General Public License\n"
833 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
834}
835
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800836static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800837{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700838 printf("usage: %s [-cCltxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800839 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800840 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800841 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800842 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700843 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800844 " -t | --timestamps: print timestamp information\n"
845 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800846 " -v | --version: print the version\n"
847 " -h | --help: print this help\n"
848 "\n");
849 exit(1);
850}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700851
852int main(int argc, char** argv)
853{
Stefan Reinauer19f87562013-01-07 13:37:12 -0800854 int print_defaults = 1;
855 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800856 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800857 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700858 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800859 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800860
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800861 int opt, option_index = 0;
862 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800863 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800864 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -0800865 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -0800866 {"timestamps", 0, 0, 't'},
Stefan Reinauera9c83612013-07-16 17:47:35 -0700867 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800868 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800869 {"version", 0, 0, 'v'},
870 {"help", 0, 0, 'h'},
871 {0, 0, 0, 0}
872 };
Stefan Reinauera9c83612013-07-16 17:47:35 -0700873 while ((opt = getopt_long(argc, argv, "cCltxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800874 long_options, &option_index)) != EOF) {
875 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800876 case 'c':
877 print_console = 1;
878 print_defaults = 0;
879 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800880 case 'C':
881 print_coverage = 1;
882 print_defaults = 0;
883 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800884 case 'l':
885 print_list = 1;
886 print_defaults = 0;
887 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700888 case 'x':
889 print_hexdump = 1;
890 print_defaults = 0;
891 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800892 case 't':
893 print_timestamps = 1;
894 print_defaults = 0;
895 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800896 case 'V':
897 verbose = 1;
898 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800899 case 'v':
900 print_version();
901 exit(0);
902 break;
903 case 'h':
904 case '?':
905 default:
906 print_usage(argv[0]);
907 exit(0);
908 break;
909 }
910 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700911
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800912 fd = open("/dev/mem", O_RDONLY, 0);
913 if (fd < 0) {
914 fprintf(stderr, "Failed to gain memory access: %s\n",
915 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700916 return 1;
917 }
918
Stefan Reinauer7f681502013-06-19 15:39:09 -0700919#ifdef __arm__
920 int dt_fd;
921 uint32_t cbtable_base;
922
923 dt_fd = open("/proc/device-tree/firmware/coreboot/coreboot-table",
924 O_RDONLY, 0);
925 if (dt_fd < 0) {
926 fprintf(stderr, "Failed to open device tree node: %s\n",
927 strerror(errno));
928 return 1;
929 }
930
931 if (read(dt_fd, &cbtable_base, 4) != 4) {
932 fprintf(stderr, "Failed to read device tree node: %s\n",
933 strerror(errno));
934 return 1;
935 }
936 close(dt_fd);
937
938 parse_cbtable(ntohl(cbtable_base));
939#else
940 int j;
941 static const int possible_base_addresses[] = { 0, 0xf0000 };
942
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800943 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700944 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800945 if (parse_cbtable(possible_base_addresses[j]))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700946 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700947 }
Stefan Reinauer7f681502013-06-19 15:39:09 -0700948#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700949
Stefan Reinauer19f87562013-01-07 13:37:12 -0800950 if (print_console)
951 dump_console();
952
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800953 if (print_coverage)
954 dump_coverage();
955
Stefan Reinauerc0199072013-01-07 16:26:10 -0800956 if (print_list)
957 dump_cbmem_toc();
958
Stefan Reinauera9c83612013-07-16 17:47:35 -0700959 if (print_hexdump)
960 dump_cbmem_hex();
961
Stefan Reinauer19f87562013-01-07 13:37:12 -0800962 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800963 dump_timestamps();
964
965 close(fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700966 return 0;
967}