blob: e05a72aed95f2aae5a1aca42258ece96a1000af3 [file] [log] [blame]
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.
5 *
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>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/mman.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080032#include <libgen.h>
33#include <assert.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070034
Stefan Reinauer05cbce62013-01-03 14:30:33 -080035#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
36#define MAP_BYTES (1024*1024)
37
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070038#include "boot/coreboot_tables.h"
39
40typedef uint16_t u16;
41typedef uint32_t u32;
42typedef uint64_t u64;
43
44#include "cbmem.h"
45#include "timestamp.h"
46
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080047#define CBMEM_VERSION "1.0"
48
Stefan Reinauer05cbce62013-01-03 14:30:33 -080049/* verbose output? */
50static int verbose = 0;
51#define debug(x...) if(verbose) printf(x)
52
53/* File handle used to access /dev/mem */
54static int fd;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070055
56/*
57 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
58 * the buffer length is odd last byte is excluded from the calculation
59 */
60static u16 ipchcksum(const void *addr, unsigned size)
61{
62 const u16 *p = addr;
63 unsigned i, n = size / 2; /* don't expect odd sized blocks */
64 u32 sum = 0;
65
66 for (i = 0; i < n; i++)
67 sum += p[i];
68
69 sum = (sum >> 16) + (sum & 0xffff);
70 sum += (sum >> 16);
71 sum = ~sum & 0xffff;
72 return (u16) sum;
73}
74
75/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080076 * Functions to map / unmap physical memory into virtual address space. These
77 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070078 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080079static void *mapped_virtual;
80static void *map_memory(u64 physical)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070081{
Stefan Reinauer05cbce62013-01-03 14:30:33 -080082 void *v;
83 off_t p;
84 int page = getpagesize();
85
86 /* Mapped memory must be aligned to page size */
87 p = physical & ~(page - 1);
88
Paul Menzel8d9ffd92013-04-13 18:25:56 +020089 debug("Mapping 1MB of physical memory at 0x%jx.\n", (intmax_t)p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -080090
91 v = mmap(NULL, MAP_BYTES, PROT_READ, MAP_SHARED, fd, p);
92
93 if (v == MAP_FAILED) {
94 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
95 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070096 exit(1);
97 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -080098
99 /* Remember what we actually mapped ... */
100 mapped_virtual = v;
101
102 /* ... but return address to the physical memory that was requested */
103 v += physical & (page-1);
104
105 return v;
106}
107
108static void unmap_memory(void)
109{
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800110 if (mapped_virtual == NULL) {
111 fprintf(stderr, "Error unmapping memory\n");
112 return;
113 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800114 debug("Unmapping 1MB of virtual memory at %p.\n", mapped_virtual);
115 munmap(mapped_virtual, MAP_BYTES);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800116 mapped_virtual = NULL;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700117}
118
119/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800120 * Try finding the timestamp table and coreboot cbmem console starting from the
121 * passed in memory offset. Could be called recursively in case a forwarding
122 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700123 *
124 * Returns pointer to a memory buffer containg the timestamp table or zero if
125 * none found.
126 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800127
128static struct lb_cbmem_ref timestamps;
129static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800130static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800131
Stefan Reinauer8c594772013-04-19 14:22:29 -0700132/* This is a work-around for a nasty problem introduced by initially having
133 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
134 * on 64bit x86 systems because coreboot is 32bit on those systems.
135 * When the problem was found, it was corrected, but there are a lot of
136 * systems out there with a firmware that does not produce the right
137 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
138 */
139static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
140{
141 struct lb_cbmem_ref ret;
142
143 ret = *cbmem_ref;
144
145 if (cbmem_ref->size < sizeof(*cbmem_ref))
146 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
147
148 return ret;
149}
150
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800151static int parse_cbtable(u64 address)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700152{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800153 int i, found = 0;
154 void *buf;
155
Nico Huber8e4bb9282013-05-26 18:17:54 +0200156 debug("Looking for coreboot table at %" PRIx64 "\n", address);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800157 buf = map_memory(address);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700158
159 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800160
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700161 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800162 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700163 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800164 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700165 int j;
166
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800167 lbh = (struct lb_header *)(buf + i);
168 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
169 !lbh->header_bytes ||
170 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700171 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700172 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800173 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700174
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800175 if (ipchcksum(lbtable, lbh->table_bytes) !=
176 lbh->table_checksum) {
177 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700178 continue;
179 }
180
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800181 found = 1;
182 debug("Found!\n");
183
184 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700185 /* look for the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800186 lbr_p = (struct lb_record*) ((char *)lbtable + j);
187 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700188 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800189 case LB_TAG_MEMORY: {
190 int i = 0;
191 debug(" Found memory map.\n");
192 struct lb_memory *memory =
193 (struct lb_memory *)lbr_p;
194 while ((char *)&memory->map[i] < ((char *)lbtable
195 + lbr_p->size)) {
196 if (memory->map[i].type == LB_MEM_TABLE) {
197 debug(" LB_MEM_TABLE found.\n");
198 /* The last one found is CBMEM */
199 cbmem = memory->map[i];
200 }
201 i++;
202 }
203 continue;
204 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700205 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800206 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700207 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800208 continue;
209 }
210 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800211 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700212 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800213 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700214 }
215 case LB_TAG_FORWARD: {
216 /*
217 * This is a forwarding entry - repeat the
218 * search at the new address.
219 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800220 struct lb_forward lbf_p =
221 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800222 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800223 unmap_memory();
224 return parse_cbtable(lbf_p.forward);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700225 }
226 default:
227 break;
228 }
229
230 }
231 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800232 unmap_memory();
233
234 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700235}
236
237/*
238 * read CPU frequency from a sysfs file, return an frequency in Kilohertz as
239 * an int or exit on any error.
240 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800241static u64 get_cpu_freq_KHz(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700242{
243 FILE *cpuf;
244 char freqs[100];
245 int size;
246 char *endp;
247 u64 rv;
248
249 const char* freq_file =
250 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
251
252 cpuf = fopen(freq_file, "r");
253 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800254 fprintf(stderr, "Could not open %s: %s\n",
255 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700256 exit(1);
257 }
258
259 memset(freqs, 0, sizeof(freqs));
260 size = fread(freqs, 1, sizeof(freqs), cpuf);
261 if (!size || (size == sizeof(freqs))) {
262 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
263 size, freq_file);
264 exit(1);
265 }
266 fclose(cpuf);
267 rv = strtoull(freqs, &endp, 10);
268
269 if (*endp == '\0' || *endp == '\n')
270 return rv;
271 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
272 freqs, freq_file);
273 exit(1);
274}
275
276/*
277 * Print an integer in 'normalized' form - with commas separating every three
278 * decimal orders. The 'comma' parameter indicates if a comma is needed after
279 * the value is printed.
280 */
281static void print_norm(u64 v, int comma)
282{
283 int first_triple = 1;
284
285 if (v > 1000) {
286 /* print the higher order sections first */
287 print_norm(v / 1000, 1);
288 first_triple = 0;
289 }
290 if (first_triple)
291 printf("%d", (u32)(v % 1000));
292 else
293 printf("%3.3d", (u32)(v % 1000));
294 if (comma)
295 printf(",");
296}
297
298/* dump the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800299static void dump_timestamps(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700300{
301 int i;
302 u64 cpu_freq_MHz = get_cpu_freq_KHz() / 1000;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800303 struct timestamp_table *tst_p;
304
305 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
306 fprintf(stderr, "No timestamps found in coreboot table.\n");
307 return;
308 }
309
310 tst_p = (struct timestamp_table *)
311 map_memory((unsigned long)timestamps.cbmem_addr);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700312
313 printf("%d entries total:\n\n", tst_p->num_entries);
314 for (i = 0; i < tst_p->num_entries; i++) {
315 const struct timestamp_entry *tse_p = tst_p->entries + i;
316
317 printf("%4d:", tse_p->entry_id);
318 print_norm(tse_p->entry_stamp / cpu_freq_MHz, 0);
319 if (i) {
320 printf(" (");
321 print_norm((tse_p->entry_stamp -
322 tse_p[-1].entry_stamp) /
323 cpu_freq_MHz, 0);
324 printf(")");
325 }
326 printf("\n");
327 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800328
329 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700330}
331
Stefan Reinauer19f87562013-01-07 13:37:12 -0800332/* dump the cbmem console */
333static void dump_console(void)
334{
335 void *console_p;
336 char *console_c;
337 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100338 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800339
340 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
341 fprintf(stderr, "No console found in coreboot table.\n");
342 return;
343 }
344
345 console_p = map_memory((unsigned long)console.cbmem_addr);
346 /* The in-memory format of the console area is:
347 * u32 size
348 * u32 cursor
349 * char console[size]
350 * Hence we have to add 8 to get to the actual console string.
351 */
352 size = *(uint32_t *)console_p;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100353 cursor = *(uint32_t *) (console_p + 4);
354 /* Cursor continues to go on even after no more data fits in
355 * the buffer but the data is dropped in this case.
356 */
357 if (size > cursor)
358 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800359 console_c = malloc(size + 1);
360 if (!console_c) {
361 fprintf(stderr, "Not enough memory for console.\n");
362 exit(1);
363 }
364
365 memcpy(console_c, console_p + 8, size);
366 console_c[size] = 0;
367
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100368 printf("%s\n", console_c);
369 if (size < cursor)
370 printf("%d %s lost\n", cursor - size,
371 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800372
373 free(console_c);
374
375 unmap_memory();
376}
377
Stefan Reinauerc0199072013-01-07 16:26:10 -0800378#define CBMEM_MAGIC 0x434f5245
379#define MAX_CBMEM_ENTRIES 16
380
381struct cbmem_entry {
382 uint32_t magic;
383 uint32_t id;
384 uint64_t base;
385 uint64_t size;
386};
387
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800388static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800389{
390 int i;
391 uint64_t start;
392 struct cbmem_entry *entries;
393
394 if (cbmem.type != LB_MEM_TABLE) {
395 fprintf(stderr, "No coreboot table area found!\n");
396 return;
397 }
398
399 start = unpack_lb64(cbmem.start);
400
401 entries = (struct cbmem_entry *)map_memory(start);
402
403 printf("CBMEM table of contents:\n");
404 printf(" ID START LENGTH\n");
405 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
406 if (entries[i].magic != CBMEM_MAGIC)
407 break;
408
409 printf("%2d. ", i);
410 switch (entries[i].id) {
411 case CBMEM_ID_FREESPACE: printf("FREE SPACE "); break;
412 case CBMEM_ID_GDT: printf("GDT "); break;
413 case CBMEM_ID_ACPI: printf("ACPI "); break;
414 case CBMEM_ID_ACPI_GNVS: printf("ACPI GNVS "); break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800415 case CBMEM_ID_CBTABLE: printf("COREBOOT "); break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800416 case CBMEM_ID_PIRQ: printf("IRQ TABLE "); break;
417 case CBMEM_ID_MPTABLE: printf("SMP TABLE "); break;
418 case CBMEM_ID_RESUME: printf("ACPI RESUME "); break;
419 case CBMEM_ID_RESUME_SCRATCH: printf("ACPI SCRATCH"); break;
420 case CBMEM_ID_SMBIOS: printf("SMBIOS "); break;
421 case CBMEM_ID_TIMESTAMP: printf("TIME STAMP "); break;
422 case CBMEM_ID_MRCDATA: printf("MRC DATA "); break;
423 case CBMEM_ID_CONSOLE: printf("CONSOLE "); break;
424 case CBMEM_ID_ELOG: printf("ELOG "); break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800425 case CBMEM_ID_COVERAGE: printf("COVERAGE "); break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800426 default: printf("%08x ",
427 entries[i].id); break;
428 }
429 printf(" 0x%08jx 0x%08jx\n", (uintmax_t)entries[i].base,
430 (uintmax_t)entries[i].size);
431 }
432 unmap_memory();
433}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800434
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800435#define COVERAGE_MAGIC 0x584d4153
436struct file {
437 uint32_t magic;
438 uint32_t next;
439 uint32_t filename;
440 uint32_t data;
441 int offset;
442 int len;
443};
444
445static int mkpath(char *path, mode_t mode)
446{
447 assert (path && *path);
448 char *p;
449 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
450 *p = '\0';
451 if (mkdir(path, mode) == -1) {
452 if (errno != EEXIST) {
453 *p = '/';
454 return -1;
455 }
456 }
457 *p = '/';
458 }
459 return 0;
460}
461
462static void dump_coverage(void)
463{
464 int i, found = 0;
465 uint64_t start;
466 struct cbmem_entry *entries;
467 void *coverage;
468 unsigned long phys_offset;
469#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
470
471 if (cbmem.type != LB_MEM_TABLE) {
472 fprintf(stderr, "No coreboot table area found!\n");
473 return;
474 }
475
476 start = unpack_lb64(cbmem.start);
477
478 entries = (struct cbmem_entry *)map_memory(start);
479
480 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
481 if (entries[i].magic != CBMEM_MAGIC)
482 break;
483 if (entries[i].id == CBMEM_ID_COVERAGE) {
484 found = 1;
485 break;
486 }
487 }
488
489 if (!found) {
490 unmap_memory();
491 fprintf(stderr, "No coverage information found in"
492 " CBMEM area.\n");
493 return;
494 }
495
496 start = entries[i].base;
497 unmap_memory();
498 /* Map coverage area */
499 coverage = map_memory(start);
500 phys_offset = (unsigned long)coverage - (unsigned long)start;
501
502 printf("Dumping coverage data...\n");
503
504 struct file *file = (struct file *)coverage;
505 while (file && file->magic == COVERAGE_MAGIC) {
506 FILE *f;
507 char *filename;
508
509 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
510 filename = strdup((char *)phys_to_virt(file->filename));
511 if (mkpath(filename, 0755) == -1) {
512 perror("Directory for coverage data could "
513 "not be created");
514 exit(1);
515 }
516 f = fopen(filename, "wb");
517 if (!f) {
518 printf("Could not open %s: %s\n",
519 filename, strerror(errno));
520 exit(1);
521 }
522 if (fwrite((void *)phys_to_virt(file->data),
523 file->len, 1, f) != 1) {
524 printf("Could not write to %s: %s\n",
525 filename, strerror(errno));
526 exit(1);
527 }
528 fclose(f);
529 free(filename);
530
531 if (file->next)
532 file = (struct file *)phys_to_virt(file->next);
533 else
534 file = NULL;
535 }
536 unmap_memory();
537}
538
539static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800540{
541 printf("cbmem v%s -- ", CBMEM_VERSION);
542 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
543 printf(
544 "This program is free software: you can redistribute it and/or modify\n"
545 "it under the terms of the GNU General Public License as published by\n"
546 "the Free Software Foundation, version 2 of the License.\n\n"
547 "This program is distributed in the hope that it will be useful,\n"
548 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
549 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
550 "GNU General Public License for more details.\n\n"
551 "You should have received a copy of the GNU General Public License\n"
552 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
553}
554
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800555static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800556{
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800557 printf("usage: %s [-cCltVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800558 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800559 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800560 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800561 " -l | --list: print cbmem table of contents\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800562 " -t | --timestamps: print timestamp information\n"
563 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800564 " -v | --version: print the version\n"
565 " -h | --help: print this help\n"
566 "\n");
567 exit(1);
568}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700569
570int main(int argc, char** argv)
571{
572 int j;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800573 static const int possible_base_addresses[] = { 0, 0xf0000 };
574
Stefan Reinauer19f87562013-01-07 13:37:12 -0800575 int print_defaults = 1;
576 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800577 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800578 int print_list = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800579 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800580
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800581 int opt, option_index = 0;
582 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800583 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800584 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -0800585 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -0800586 {"timestamps", 0, 0, 't'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800587 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800588 {"version", 0, 0, 'v'},
589 {"help", 0, 0, 'h'},
590 {0, 0, 0, 0}
591 };
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800592 while ((opt = getopt_long(argc, argv, "cCltVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800593 long_options, &option_index)) != EOF) {
594 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800595 case 'c':
596 print_console = 1;
597 print_defaults = 0;
598 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800599 case 'C':
600 print_coverage = 1;
601 print_defaults = 0;
602 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800603 case 'l':
604 print_list = 1;
605 print_defaults = 0;
606 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800607 case 't':
608 print_timestamps = 1;
609 print_defaults = 0;
610 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800611 case 'V':
612 verbose = 1;
613 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800614 case 'v':
615 print_version();
616 exit(0);
617 break;
618 case 'h':
619 case '?':
620 default:
621 print_usage(argv[0]);
622 exit(0);
623 break;
624 }
625 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700626
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800627 fd = open("/dev/mem", O_RDONLY, 0);
628 if (fd < 0) {
629 fprintf(stderr, "Failed to gain memory access: %s\n",
630 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700631 return 1;
632 }
633
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800634 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700635 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800636 if (parse_cbtable(possible_base_addresses[j]))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700637 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700638 }
639
Stefan Reinauer19f87562013-01-07 13:37:12 -0800640 if (print_console)
641 dump_console();
642
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800643 if (print_coverage)
644 dump_coverage();
645
Stefan Reinauerc0199072013-01-07 16:26:10 -0800646 if (print_list)
647 dump_cbmem_toc();
648
Stefan Reinauer19f87562013-01-07 13:37:12 -0800649 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800650 dump_timestamps();
651
652 close(fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700653 return 0;
654}