blob: 1117c664aa675a685f1fc06dd7c252d884bbab2a [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)
Kyösti Mälkkib3594ab2014-06-18 00:43:35 +030039#define IS_ENABLED(x) (defined (x) && (x))
Stefan Reinauer05cbce62013-01-03 14:30:33 -080040
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070041#include "boot/coreboot_tables.h"
42
43typedef uint16_t u16;
44typedef uint32_t u32;
45typedef uint64_t u64;
46
47#include "cbmem.h"
48#include "timestamp.h"
49
Stefan Reinauera9c83612013-07-16 17:47:35 -070050#define CBMEM_VERSION "1.1"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080051
Stefan Reinauer05cbce62013-01-03 14:30:33 -080052/* verbose output? */
53static int verbose = 0;
54#define debug(x...) if(verbose) printf(x)
55
56/* File handle used to access /dev/mem */
57static int fd;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070058
59/*
60 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
61 * the buffer length is odd last byte is excluded from the calculation
62 */
63static u16 ipchcksum(const void *addr, unsigned size)
64{
65 const u16 *p = addr;
66 unsigned i, n = size / 2; /* don't expect odd sized blocks */
67 u32 sum = 0;
68
69 for (i = 0; i < n; i++)
70 sum += p[i];
71
72 sum = (sum >> 16) + (sum & 0xffff);
73 sum += (sum >> 16);
74 sum = ~sum & 0xffff;
75 return (u16) sum;
76}
77
78/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080079 * Functions to map / unmap physical memory into virtual address space. These
80 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070081 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080082static void *mapped_virtual;
Aaron Durbinab180d82014-03-31 11:59:58 -050083static size_t mapped_size;
84
85static inline size_t size_to_mib(size_t sz)
86{
87 return sz >> 20;
88}
89
90static void unmap_memory(void)
91{
92 if (mapped_virtual == NULL) {
93 fprintf(stderr, "Error unmapping memory\n");
94 return;
95 }
96 debug("Unmapping %zuMB of virtual memory at %p.\n",
97 size_to_mib(mapped_size), mapped_virtual);
98 munmap(mapped_virtual, mapped_size);
99 mapped_virtual = NULL;
100 mapped_size = 0;
101}
102
103static void *map_memory_size(u64 physical, size_t size)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700104{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800105 void *v;
106 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -0700107 u64 page = getpagesize();
Aaron Durbinab180d82014-03-31 11:59:58 -0500108 size_t padding;
109
110 if (mapped_virtual != NULL)
111 unmap_memory();
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800112
113 /* Mapped memory must be aligned to page size */
114 p = physical & ~(page - 1);
Aaron Durbinab180d82014-03-31 11:59:58 -0500115 padding = physical & (page-1);
116 size += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800117
Aaron Durbinab180d82014-03-31 11:59:58 -0500118 debug("Mapping %zuMB of physical memory at 0x%jx.\n",
119 size_to_mib(size), (intmax_t)p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800120
Aaron Durbinab180d82014-03-31 11:59:58 -0500121 v = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800122
123 if (v == MAP_FAILED) {
124 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
125 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700126 exit(1);
127 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800128
129 /* Remember what we actually mapped ... */
130 mapped_virtual = v;
Aaron Durbinab180d82014-03-31 11:59:58 -0500131 mapped_size = size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800132
133 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700134 if (padding)
Aaron Durbinab180d82014-03-31 11:59:58 -0500135 debug(" ... padding virtual address with 0x%zx bytes.\n",
Stefan Reinauera9c83612013-07-16 17:47:35 -0700136 padding);
137 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800138
139 return v;
140}
141
Aaron Durbinab180d82014-03-31 11:59:58 -0500142static void *map_memory(u64 physical)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800143{
Aaron Durbinab180d82014-03-31 11:59:58 -0500144 return map_memory_size(physical, MAP_BYTES);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700145}
146
147/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800148 * Try finding the timestamp table and coreboot cbmem console starting from the
149 * passed in memory offset. Could be called recursively in case a forwarding
150 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700151 *
152 * Returns pointer to a memory buffer containg the timestamp table or zero if
153 * none found.
154 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800155
156static struct lb_cbmem_ref timestamps;
157static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800158static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800159
Stefan Reinauer8c594772013-04-19 14:22:29 -0700160/* This is a work-around for a nasty problem introduced by initially having
161 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
162 * on 64bit x86 systems because coreboot is 32bit on those systems.
163 * When the problem was found, it was corrected, but there are a lot of
164 * systems out there with a firmware that does not produce the right
165 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
166 */
167static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
168{
169 struct lb_cbmem_ref ret;
170
171 ret = *cbmem_ref;
172
173 if (cbmem_ref->size < sizeof(*cbmem_ref))
174 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
175
Stefan Reinauera9c83612013-07-16 17:47:35 -0700176 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
177
Stefan Reinauer8c594772013-04-19 14:22:29 -0700178 return ret;
179}
180
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800181static int parse_cbtable(u64 address)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700182{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800183 int i, found = 0;
184 void *buf;
185
Nico Huber8e4bb9282013-05-26 18:17:54 +0200186 debug("Looking for coreboot table at %" PRIx64 "\n", address);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800187 buf = map_memory(address);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700188
189 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800190
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700191 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800192 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700193 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800194 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700195 int j;
196
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800197 lbh = (struct lb_header *)(buf + i);
198 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
199 !lbh->header_bytes ||
200 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700201 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700202 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800203 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700204
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800205 if (ipchcksum(lbtable, lbh->table_bytes) !=
206 lbh->table_checksum) {
207 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700208 continue;
209 }
210
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800211 found = 1;
212 debug("Found!\n");
213
214 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
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;
Paul Menzel747c07f2014-10-17 13:46:12 +0200223 while ((char *)&memory->map[i] < ((char *)lbr_p
Stefan Reinauerc0199072013-01-07 16:26:10 -0800224 + 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 */
Gabe Black06b13a32013-08-09 00:40:06 -0700474 size = ((uint32_t *)console_p)[0];
475 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100476 /* 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;
Gabe Black06b13a32013-08-09 00:40:06 -0700491 console_c[cursor] = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800492
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100493 printf("%s\n", console_c);
494 if (size < cursor)
495 printf("%d %s lost\n", cursor - size,
496 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800497
498 free(console_c);
499
500 unmap_memory();
501}
502
Stefan Reinauera9c83612013-07-16 17:47:35 -0700503static void hexdump(unsigned long memory, int length)
504{
505 int i;
506 uint8_t *m;
507 int all_zero = 0;
508
509 m = map_memory((intptr_t)memory);
510
511 if (length > MAP_BYTES) {
512 printf("Truncating hex dump from %d to %d bytes\n\n",
513 length, MAP_BYTES);
514 length = MAP_BYTES;
515 }
516
517 for (i = 0; i < length; i += 16) {
518 int j;
519
520 all_zero++;
521 for (j = 0; j < 16; j++) {
522 if(m[i+j] != 0) {
523 all_zero = 0;
524 break;
525 }
526 }
527
528 if (all_zero < 2) {
529 printf("%08lx:", memory + i);
530 for (j = 0; j < 16; j++)
531 printf(" %02x", m[i+j]);
532 printf(" ");
533 for (j = 0; j < 16; j++)
534 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
535 printf("\n");
536 } else if (all_zero == 2) {
537 printf("...\n");
538 }
539 }
540
541 unmap_memory();
542}
543
544static void dump_cbmem_hex(void)
545{
546 if (cbmem.type != LB_MEM_TABLE) {
547 fprintf(stderr, "No coreboot CBMEM area found!\n");
548 return;
549 }
550
551 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
552}
553
554/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
555#define DYN_CBMEM_ALIGN_SIZE (4096)
556#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
557#define CBMEM_POINTER_MAGIC 0xc0389479
558#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
559
560struct cbmem_root_pointer {
561 uint32_t magic;
562 uint32_t root;
563} __attribute__((packed));
564
565struct dynamic_cbmem_entry {
566 uint32_t magic;
567 uint32_t start;
568 uint32_t size;
569 uint32_t id;
570} __attribute__((packed));
571
572struct cbmem_root {
573 uint32_t max_entries;
574 uint32_t num_entries;
575 uint32_t locked;
576 uint32_t size;
577 struct dynamic_cbmem_entry entries[0];
578} __attribute__((packed));
579
Stefan Reinauerc0199072013-01-07 16:26:10 -0800580#define CBMEM_MAGIC 0x434f5245
581#define MAX_CBMEM_ENTRIES 16
582
583struct cbmem_entry {
584 uint32_t magic;
585 uint32_t id;
586 uint64_t base;
587 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700588} __attribute__((packed));
589
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700590static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800591
Stefan Reinauera9c83612013-07-16 17:47:35 -0700592void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
593{
594 int i;
595 const char *name;
596
597 name = NULL;
598 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
599 if (cbmem_ids[i].id == id) {
600 name = cbmem_ids[i].name;
601 break;
602 }
603 }
604
605 printf("%2d. ", n);
606 if (name == NULL)
607 printf("%08x ", id);
608 else
609 printf("%s", name);
610 printf(" %08" PRIx64 " ", base);
611 printf(" %08" PRIx64 "\n", size);
612}
613
614static void dump_static_cbmem_toc(struct cbmem_entry *entries)
615{
616 int i;
617
618 printf("CBMEM table of contents:\n");
619 printf(" ID START LENGTH\n");
620
621 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
622 if (entries[i].magic != CBMEM_MAGIC)
623 break;
624 cbmem_print_entry(i, entries[i].id,
625 entries[i].base, entries[i].size);
626 }
627}
628
629static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
630{
631 int i;
632 debug("CBMEM: max_entries=%d num_entries=%d locked=0x%x, size=%d\n\n",
633 root->max_entries, root->num_entries, root->locked, root->size);
634
635 printf("CBMEM table of contents:\n");
636 printf(" ID START LENGTH\n");
637
638 for (i = 0; i < root->num_entries; i++) {
639 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
640 break;
641 cbmem_print_entry(i, root->entries[i].id,
642 root->entries[i].start, root->entries[i].size);
643 }
644}
645
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800646static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800647{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800648 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700649 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800650 struct cbmem_entry *entries;
651
652 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700653 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800654 return;
655 }
656
657 start = unpack_lb64(cbmem.start);
658
Stefan Reinauera9c83612013-07-16 17:47:35 -0700659 cbmem_area = map_memory(start);
660 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800661
Stefan Reinauera9c83612013-07-16 17:47:35 -0700662 if (entries[0].magic == CBMEM_MAGIC) {
663 dump_static_cbmem_toc(entries);
664 } else {
665 uint64_t rootptr;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800666
Stefan Reinauera9c83612013-07-16 17:47:35 -0700667 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
668 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
669 rootptr -= sizeof(struct cbmem_root_pointer);
670 unmap_memory();
671 struct cbmem_root_pointer *r =
672 (struct cbmem_root_pointer *)map_memory(rootptr);
673 if (r->magic == CBMEM_POINTER_MAGIC) {
674 struct cbmem_root *root;
675 uint64_t rootaddr = r->root;
676 unmap_memory();
677 /* Note that this only works because our default mmap
678 * size is 1MiB which happens to be larger than the
679 * root entry size which is default to be 4KiB.
680 */
681 root = (struct cbmem_root *)map_memory(rootaddr);
682 dump_dynamic_cbmem_toc(root);
683 } else
684 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800685 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700686
Stefan Reinauerc0199072013-01-07 16:26:10 -0800687 unmap_memory();
688}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800689
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800690#define COVERAGE_MAGIC 0x584d4153
691struct file {
692 uint32_t magic;
693 uint32_t next;
694 uint32_t filename;
695 uint32_t data;
696 int offset;
697 int len;
698};
699
700static int mkpath(char *path, mode_t mode)
701{
702 assert (path && *path);
703 char *p;
704 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
705 *p = '\0';
706 if (mkdir(path, mode) == -1) {
707 if (errno != EEXIST) {
708 *p = '/';
709 return -1;
710 }
711 }
712 *p = '/';
713 }
714 return 0;
715}
716
717static void dump_coverage(void)
718{
719 int i, found = 0;
720 uint64_t start;
721 struct cbmem_entry *entries;
722 void *coverage;
723 unsigned long phys_offset;
724#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
725
726 if (cbmem.type != LB_MEM_TABLE) {
727 fprintf(stderr, "No coreboot table area found!\n");
728 return;
729 }
730
731 start = unpack_lb64(cbmem.start);
732
733 entries = (struct cbmem_entry *)map_memory(start);
734
735 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
736 if (entries[i].magic != CBMEM_MAGIC)
737 break;
738 if (entries[i].id == CBMEM_ID_COVERAGE) {
739 found = 1;
740 break;
741 }
742 }
743
744 if (!found) {
745 unmap_memory();
746 fprintf(stderr, "No coverage information found in"
747 " CBMEM area.\n");
748 return;
749 }
750
751 start = entries[i].base;
752 unmap_memory();
753 /* Map coverage area */
754 coverage = map_memory(start);
755 phys_offset = (unsigned long)coverage - (unsigned long)start;
756
757 printf("Dumping coverage data...\n");
758
759 struct file *file = (struct file *)coverage;
760 while (file && file->magic == COVERAGE_MAGIC) {
761 FILE *f;
762 char *filename;
763
764 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
765 filename = strdup((char *)phys_to_virt(file->filename));
766 if (mkpath(filename, 0755) == -1) {
767 perror("Directory for coverage data could "
768 "not be created");
769 exit(1);
770 }
771 f = fopen(filename, "wb");
772 if (!f) {
773 printf("Could not open %s: %s\n",
774 filename, strerror(errno));
775 exit(1);
776 }
777 if (fwrite((void *)phys_to_virt(file->data),
778 file->len, 1, f) != 1) {
779 printf("Could not write to %s: %s\n",
780 filename, strerror(errno));
781 exit(1);
782 }
783 fclose(f);
784 free(filename);
785
786 if (file->next)
787 file = (struct file *)phys_to_virt(file->next);
788 else
789 file = NULL;
790 }
791 unmap_memory();
792}
793
794static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800795{
796 printf("cbmem v%s -- ", CBMEM_VERSION);
797 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
798 printf(
799 "This program is free software: you can redistribute it and/or modify\n"
800 "it under the terms of the GNU General Public License as published by\n"
801 "the Free Software Foundation, version 2 of the License.\n\n"
802 "This program is distributed in the hope that it will be useful,\n"
803 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
804 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
805 "GNU General Public License for more details.\n\n"
806 "You should have received a copy of the GNU General Public License\n"
807 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
808}
809
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800810static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800811{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700812 printf("usage: %s [-cCltxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800813 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800814 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800815 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800816 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700817 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800818 " -t | --timestamps: print timestamp information\n"
819 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800820 " -v | --version: print the version\n"
821 " -h | --help: print this help\n"
822 "\n");
823 exit(1);
824}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700825
826int main(int argc, char** argv)
827{
Stefan Reinauer19f87562013-01-07 13:37:12 -0800828 int print_defaults = 1;
829 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800830 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800831 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700832 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800833 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800834
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800835 int opt, option_index = 0;
836 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800837 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800838 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -0800839 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -0800840 {"timestamps", 0, 0, 't'},
Stefan Reinauera9c83612013-07-16 17:47:35 -0700841 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800842 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800843 {"version", 0, 0, 'v'},
844 {"help", 0, 0, 'h'},
845 {0, 0, 0, 0}
846 };
Stefan Reinauera9c83612013-07-16 17:47:35 -0700847 while ((opt = getopt_long(argc, argv, "cCltxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800848 long_options, &option_index)) != EOF) {
849 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800850 case 'c':
851 print_console = 1;
852 print_defaults = 0;
853 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800854 case 'C':
855 print_coverage = 1;
856 print_defaults = 0;
857 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800858 case 'l':
859 print_list = 1;
860 print_defaults = 0;
861 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700862 case 'x':
863 print_hexdump = 1;
864 print_defaults = 0;
865 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800866 case 't':
867 print_timestamps = 1;
868 print_defaults = 0;
869 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800870 case 'V':
871 verbose = 1;
872 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800873 case 'v':
874 print_version();
875 exit(0);
876 break;
877 case 'h':
878 case '?':
879 default:
880 print_usage(argv[0]);
881 exit(0);
882 break;
883 }
884 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700885
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800886 fd = open("/dev/mem", O_RDONLY, 0);
887 if (fd < 0) {
888 fprintf(stderr, "Failed to gain memory access: %s\n",
889 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700890 return 1;
891 }
892
Stefan Reinauer7f681502013-06-19 15:39:09 -0700893#ifdef __arm__
894 int dt_fd;
895 uint32_t cbtable_base;
896
897 dt_fd = open("/proc/device-tree/firmware/coreboot/coreboot-table",
898 O_RDONLY, 0);
899 if (dt_fd < 0) {
900 fprintf(stderr, "Failed to open device tree node: %s\n",
901 strerror(errno));
902 return 1;
903 }
904
905 if (read(dt_fd, &cbtable_base, 4) != 4) {
906 fprintf(stderr, "Failed to read device tree node: %s\n",
907 strerror(errno));
908 return 1;
909 }
910 close(dt_fd);
911
912 parse_cbtable(ntohl(cbtable_base));
913#else
914 int j;
915 static const int possible_base_addresses[] = { 0, 0xf0000 };
916
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800917 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700918 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800919 if (parse_cbtable(possible_base_addresses[j]))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700920 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700921 }
Stefan Reinauer7f681502013-06-19 15:39:09 -0700922#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700923
Stefan Reinauer19f87562013-01-07 13:37:12 -0800924 if (print_console)
925 dump_console();
926
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800927 if (print_coverage)
928 dump_coverage();
929
Stefan Reinauerc0199072013-01-07 16:26:10 -0800930 if (print_list)
931 dump_cbmem_toc();
932
Stefan Reinauera9c83612013-07-16 17:47:35 -0700933 if (print_hexdump)
934 dump_cbmem_hex();
935
Stefan Reinauer19f87562013-01-07 13:37:12 -0800936 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800937 dump_timestamps();
938
939 close(fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700940 return 0;
941}