blob: e342d6909a2a205535b2670035c08ba92449f7cb [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>
Julius Werner337de4c2014-06-16 23:02:03 -070027#include <dirent.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080028#include <errno.h>
29#include <fcntl.h>
Stefan Reinauera9c83612013-07-16 17:47:35 -070030#include <ctype.h>
Stefan Reinauer7f681502013-06-19 15:39:09 -070031#include <arpa/inet.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080032#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/mman.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080035#include <libgen.h>
36#include <assert.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070037
Stefan Reinauer05cbce62013-01-03 14:30:33 -080038#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
39#define MAP_BYTES (1024*1024)
Kyösti Mälkkib3594ab2014-06-18 00:43:35 +030040#define IS_ENABLED(x) (defined (x) && (x))
Stefan Reinauer05cbce62013-01-03 14:30:33 -080041
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070042#include "boot/coreboot_tables.h"
43
Julius Werner337de4c2014-06-16 23:02:03 -070044typedef uint8_t u8;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070045typedef uint16_t u16;
46typedef uint32_t u32;
47typedef uint64_t u64;
48
49#include "cbmem.h"
50#include "timestamp.h"
51
Stefan Reinauera9c83612013-07-16 17:47:35 -070052#define CBMEM_VERSION "1.1"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080053
Stefan Reinauer05cbce62013-01-03 14:30:33 -080054/* verbose output? */
55static int verbose = 0;
56#define debug(x...) if(verbose) printf(x)
57
58/* File handle used to access /dev/mem */
Julius Werner337de4c2014-06-16 23:02:03 -070059static int mem_fd;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070060
61/*
62 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
63 * the buffer length is odd last byte is excluded from the calculation
64 */
65static u16 ipchcksum(const void *addr, unsigned size)
66{
67 const u16 *p = addr;
68 unsigned i, n = size / 2; /* don't expect odd sized blocks */
69 u32 sum = 0;
70
71 for (i = 0; i < n; i++)
72 sum += p[i];
73
74 sum = (sum >> 16) + (sum & 0xffff);
75 sum += (sum >> 16);
76 sum = ~sum & 0xffff;
77 return (u16) sum;
78}
79
80/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080081 * Functions to map / unmap physical memory into virtual address space. These
82 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070083 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080084static void *mapped_virtual;
Aaron Durbinab180d82014-03-31 11:59:58 -050085static size_t mapped_size;
86
87static inline size_t size_to_mib(size_t sz)
88{
89 return sz >> 20;
90}
91
92static void unmap_memory(void)
93{
94 if (mapped_virtual == NULL) {
95 fprintf(stderr, "Error unmapping memory\n");
96 return;
97 }
98 debug("Unmapping %zuMB of virtual memory at %p.\n",
99 size_to_mib(mapped_size), mapped_virtual);
100 munmap(mapped_virtual, mapped_size);
101 mapped_virtual = NULL;
102 mapped_size = 0;
103}
104
105static void *map_memory_size(u64 physical, size_t size)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700106{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800107 void *v;
108 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -0700109 u64 page = getpagesize();
Aaron Durbinab180d82014-03-31 11:59:58 -0500110 size_t padding;
111
112 if (mapped_virtual != NULL)
113 unmap_memory();
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800114
115 /* Mapped memory must be aligned to page size */
116 p = physical & ~(page - 1);
Aaron Durbinab180d82014-03-31 11:59:58 -0500117 padding = physical & (page-1);
118 size += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800119
Aaron Durbinab180d82014-03-31 11:59:58 -0500120 debug("Mapping %zuMB of physical memory at 0x%jx.\n",
121 size_to_mib(size), (intmax_t)p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800122
Julius Werner337de4c2014-06-16 23:02:03 -0700123 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800124
125 if (v == MAP_FAILED) {
126 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
127 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700128 exit(1);
129 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800130
131 /* Remember what we actually mapped ... */
132 mapped_virtual = v;
Aaron Durbinab180d82014-03-31 11:59:58 -0500133 mapped_size = size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800134
135 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700136 if (padding)
Aaron Durbinab180d82014-03-31 11:59:58 -0500137 debug(" ... padding virtual address with 0x%zx bytes.\n",
Stefan Reinauera9c83612013-07-16 17:47:35 -0700138 padding);
139 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800140
141 return v;
142}
143
Aaron Durbinab180d82014-03-31 11:59:58 -0500144static void *map_memory(u64 physical)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800145{
Aaron Durbinab180d82014-03-31 11:59:58 -0500146 return map_memory_size(physical, MAP_BYTES);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700147}
148
149/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800150 * Try finding the timestamp table and coreboot cbmem console starting from the
151 * passed in memory offset. Could be called recursively in case a forwarding
152 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700153 *
154 * Returns pointer to a memory buffer containg the timestamp table or zero if
155 * none found.
156 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800157
158static struct lb_cbmem_ref timestamps;
159static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800160static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800161
Stefan Reinauer8c594772013-04-19 14:22:29 -0700162/* This is a work-around for a nasty problem introduced by initially having
163 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
164 * on 64bit x86 systems because coreboot is 32bit on those systems.
165 * When the problem was found, it was corrected, but there are a lot of
166 * systems out there with a firmware that does not produce the right
167 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
168 */
169static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
170{
171 struct lb_cbmem_ref ret;
172
173 ret = *cbmem_ref;
174
175 if (cbmem_ref->size < sizeof(*cbmem_ref))
176 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
177
Stefan Reinauera9c83612013-07-16 17:47:35 -0700178 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
179
Stefan Reinauer8c594772013-04-19 14:22:29 -0700180 return ret;
181}
182
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500183static int parse_cbtable(u64 address, size_t table_size)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700184{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800185 int i, found = 0;
186 void *buf;
187
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500188 debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n",
189 address, table_size);
190 buf = map_memory_size(address, table_size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700191
192 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800193
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700194 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800195 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700196 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800197 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700198 int j;
199
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800200 lbh = (struct lb_header *)(buf + i);
201 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
202 !lbh->header_bytes ||
203 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700204 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700205 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800206 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700207
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800208 if (ipchcksum(lbtable, lbh->table_bytes) !=
209 lbh->table_checksum) {
210 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700211 continue;
212 }
213
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800214 found = 1;
215 debug("Found!\n");
216
217 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800218 lbr_p = (struct lb_record*) ((char *)lbtable + j);
219 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700220 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800221 case LB_TAG_MEMORY: {
222 int i = 0;
223 debug(" Found memory map.\n");
224 struct lb_memory *memory =
225 (struct lb_memory *)lbr_p;
Paul Menzel747c07f2014-10-17 13:46:12 +0200226 while ((char *)&memory->map[i] < ((char *)lbr_p
Stefan Reinauerc0199072013-01-07 16:26:10 -0800227 + lbr_p->size)) {
228 if (memory->map[i].type == LB_MEM_TABLE) {
229 debug(" LB_MEM_TABLE found.\n");
230 /* The last one found is CBMEM */
231 cbmem = memory->map[i];
232 }
233 i++;
234 }
235 continue;
236 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700237 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800238 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700239 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800240 continue;
241 }
242 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800243 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700244 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800245 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700246 }
247 case LB_TAG_FORWARD: {
248 /*
249 * This is a forwarding entry - repeat the
250 * search at the new address.
251 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800252 struct lb_forward lbf_p =
253 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800254 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800255 unmap_memory();
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500256 return parse_cbtable(lbf_p.forward, table_size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700257 }
258 default:
259 break;
260 }
261
262 }
263 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800264 unmap_memory();
265
266 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700267}
268
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700269#if defined(__i386__) || defined(__x86_64__)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700270/*
271 * read CPU frequency from a sysfs file, return an frequency in Kilohertz as
272 * an int or exit on any error.
273 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800274static u64 get_cpu_freq_KHz(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700275{
276 FILE *cpuf;
277 char freqs[100];
278 int size;
279 char *endp;
280 u64 rv;
281
282 const char* freq_file =
283 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
284
285 cpuf = fopen(freq_file, "r");
286 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800287 fprintf(stderr, "Could not open %s: %s\n",
288 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700289 exit(1);
290 }
291
292 memset(freqs, 0, sizeof(freqs));
293 size = fread(freqs, 1, sizeof(freqs), cpuf);
294 if (!size || (size == sizeof(freqs))) {
295 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
296 size, freq_file);
297 exit(1);
298 }
299 fclose(cpuf);
300 rv = strtoull(freqs, &endp, 10);
301
302 if (*endp == '\0' || *endp == '\n')
303 return rv;
304 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
305 freqs, freq_file);
306 exit(1);
307}
308
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700309/* On x86 platforms timestamps are stored
310 * in CPU cycles (from rdtsc). Hence the
311 * timestamp divider is the CPU frequency
312 * in MHz.
313 */
314u64 arch_convert_raw_ts_entry(u64 ts)
315{
316 static u64 cpu_freq_mhz = 0;
317
318 if (!cpu_freq_mhz)
319 cpu_freq_mhz = get_cpu_freq_KHz() / 1000;
320
321 return ts / cpu_freq_mhz;
322}
323
324#else
325
326/* On non-x86 platforms the timestamp entries
327 * are not in clock cycles but in usecs
328 */
329u64 arch_convert_raw_ts_entry(u64 ts)
330{
331 return ts;
332}
333#endif
334
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700335/*
336 * Print an integer in 'normalized' form - with commas separating every three
Julius Wernera7d92442014-12-02 20:51:19 -0800337 * decimal orders.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700338 */
Julius Wernera7d92442014-12-02 20:51:19 -0800339static void print_norm(u64 v)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700340{
Julius Wernera7d92442014-12-02 20:51:19 -0800341 if (v >= 1000) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700342 /* print the higher order sections first */
Julius Wernera7d92442014-12-02 20:51:19 -0800343 print_norm(v / 1000);
344 printf(",%3.3u", (u32)(v % 1000));
345 } else {
346 printf("%u", (u32)(v % 1000));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700347 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700348}
349
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700350enum additional_timestamp_id {
351 // Depthcharge entry IDs start at 1000.
352 TS_DC_START = 1000,
353
354 TS_RO_PARAMS_INIT = 1001,
355 TS_RO_VB_INIT = 1002,
356 TS_RO_VB_SELECT_FIRMWARE = 1003,
357 TS_RO_VB_SELECT_AND_LOAD_KERNEL = 1004,
358
359 TS_RW_VB_SELECT_AND_LOAD_KERNEL = 1010,
360
361 TS_VB_SELECT_AND_LOAD_KERNEL = 1020,
362
363 TS_CROSSYSTEM_DATA = 1100,
364 TS_START_KERNEL = 1101
365};
366
367static const struct timestamp_id_to_name {
368 u32 id;
369 const char *name;
370} timestamp_ids[] = {
371 { TS_START_ROMSTAGE, "start of rom stage" },
372 { TS_BEFORE_INITRAM, "before ram initialization" },
373 { TS_AFTER_INITRAM, "after ram initialization" },
374 { TS_END_ROMSTAGE, "end of romstage" },
375 { TS_START_VBOOT, "start of verified boot" },
376 { TS_END_VBOOT, "end of verified boot" },
Julius Wernera7d92442014-12-02 20:51:19 -0800377 { TS_START_COPYRAM, "starting to load ramstage" },
378 { TS_END_COPYRAM, "finished loading ramstage" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700379 { TS_START_RAMSTAGE, "start of ramstage" },
Julius Wernera7d92442014-12-02 20:51:19 -0800380 { TS_START_BOOTBLOCK, "start of bootblock" },
381 { TS_END_BOOTBLOCK, "end of bootblock" },
382 { TS_START_COPYROM, "starting to load romstage" },
383 { TS_END_COPYROM, "finished loading romstage" },
384 { TS_START_ULZMA, "starting LZMA decompress (ignore for x86)" },
385 { TS_END_ULZMA, "finished LZMA decompress (ignore for x86)" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700386 { TS_DEVICE_ENUMERATE, "device enumeration" },
387 { TS_DEVICE_CONFIGURE, "device configuration" },
388 { TS_DEVICE_ENABLE, "device enable" },
389 { TS_DEVICE_INITIALIZE, "device initialization" },
390 { TS_DEVICE_DONE, "device setup done" },
391 { TS_CBMEM_POST, "cbmem post" },
392 { TS_WRITE_TABLES, "write tables" },
393 { TS_LOAD_PAYLOAD, "load payload" },
394 { TS_ACPI_WAKE_JUMP, "ACPI wake jump" },
395 { TS_SELFBOOT_JUMP, "selfboot jump" },
Julius Wernera7d92442014-12-02 20:51:19 -0800396
397 { TS_START_COPYVER, "starting to load verstage" },
398 { TS_END_COPYVER, "finished loading verstage" },
399 { TS_START_TPMINIT, "starting to initialize TPM" },
400 { TS_END_TPMINIT, "finished TPM initialization" },
401 { TS_START_VERIFY_SLOT, "starting to verify keyblock/preamble (RSA)" },
402 { TS_END_VERIFY_SLOT, "finished verifying keyblock/preamble (RSA)" },
403 { TS_START_HASH_BODY, "starting to verify body (load+SHA2+RSA) " },
404 { TS_DONE_LOADING, "finished loading body (ignore for x86)" },
405 { TS_DONE_HASHING, "finished calculating body hash (SHA2)" },
406 { TS_END_HASH_BODY, "finished verifying body signature (RSA)" },
407
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700408 { TS_DC_START, "depthcharge start" },
409 { TS_RO_PARAMS_INIT, "RO parameter init" },
410 { TS_RO_VB_INIT, "RO vboot init" },
411 { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" },
412 { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" },
413 { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" },
414 { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" },
415 { TS_CROSSYSTEM_DATA, "crossystem data" },
Lee Leahyb8179082015-02-24 11:30:38 -0800416 { TS_START_KERNEL, "start kernel" },
417
418 /* FSP related timestamps */
419 { TS_FSP_MEMORY_INIT_START, "calling FspMemoryInit" },
420 { TS_FSP_MEMORY_INIT_END, "returning from FspMemoryInit" },
421 { TS_FSP_TEMP_RAM_EXIT_START, "calling FspTempRamExit" },
422 { TS_FSP_TEMP_RAM_EXIT_END, "returning from FspTempRamExit" },
423 { TS_FSP_SILICON_INIT_START, "calling FspSiliconInit" },
424 { TS_FSP_SILICON_INIT_END, "returning from FspSiliconInit" },
425 { TS_FSP_BEFORE_ENUMERATE, "calling FspNotify(AfterPciEnumeration)" },
426 { TS_FSP_AFTER_ENUMERATE,
427 "returning from FspNotify(AfterPciEnumeration)" },
428 { TS_FSP_BEFORE_FINALIZE, "calling FspNotify(ReadyToBoot)" },
429 { TS_FSP_AFTER_FINALIZE, "returning from FspNotify(ReadyToBoot)" }
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700430};
431
432void timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
433{
434 int i;
435 const char *name;
436
437 name = "<unknown>";
438 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
439 if (timestamp_ids[i].id == id) {
440 name = timestamp_ids[i].name;
441 break;
442 }
443 }
444
445 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800446 printf("%-50s", name);
447 print_norm(arch_convert_raw_ts_entry(stamp));
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700448 if (prev_stamp) {
449 printf(" (");
Julius Wernera7d92442014-12-02 20:51:19 -0800450 print_norm(arch_convert_raw_ts_entry(stamp - prev_stamp));
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700451 printf(")");
452 }
453 printf("\n");
454}
455
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700456/* dump the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800457static void dump_timestamps(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700458{
459 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800460 struct timestamp_table *tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500461 size_t size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800462
463 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
464 fprintf(stderr, "No timestamps found in coreboot table.\n");
465 return;
466 }
467
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500468 size = sizeof(*tst_p);
469 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700470
471 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500472 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
473
474 unmap_memory();
475 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size);
476
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700477 for (i = 0; i < tst_p->num_entries; i++) {
478 const struct timestamp_entry *tse_p = tst_p->entries + i;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700479 timestamp_print_entry(tse_p->entry_id, tse_p->entry_stamp,
480 i ? tse_p[-1].entry_stamp : 0);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700481 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800482
483 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700484}
485
Stefan Reinauer19f87562013-01-07 13:37:12 -0800486/* dump the cbmem console */
487static void dump_console(void)
488{
489 void *console_p;
490 char *console_c;
491 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100492 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800493
494 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
495 fprintf(stderr, "No console found in coreboot table.\n");
496 return;
497 }
498
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500499 console_p = map_memory_size((unsigned long)console.cbmem_addr,
500 2 * sizeof(uint32_t));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800501 /* The in-memory format of the console area is:
502 * u32 size
503 * u32 cursor
504 * char console[size]
505 * Hence we have to add 8 to get to the actual console string.
506 */
Gabe Black06b13a32013-08-09 00:40:06 -0700507 size = ((uint32_t *)console_p)[0];
508 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100509 /* Cursor continues to go on even after no more data fits in
510 * the buffer but the data is dropped in this case.
511 */
512 if (size > cursor)
513 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800514 console_c = malloc(size + 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500515 unmap_memory();
Stefan Reinauer19f87562013-01-07 13:37:12 -0800516 if (!console_c) {
517 fprintf(stderr, "Not enough memory for console.\n");
518 exit(1);
519 }
520
Aaron Durbinab180d82014-03-31 11:59:58 -0500521 console_p = map_memory_size((unsigned long)console.cbmem_addr,
522 size + sizeof(size) + sizeof(cursor));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800523 memcpy(console_c, console_p + 8, size);
524 console_c[size] = 0;
Gabe Black06b13a32013-08-09 00:40:06 -0700525 console_c[cursor] = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800526
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100527 printf("%s\n", console_c);
528 if (size < cursor)
529 printf("%d %s lost\n", cursor - size,
530 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800531
532 free(console_c);
533
534 unmap_memory();
535}
536
Stefan Reinauera9c83612013-07-16 17:47:35 -0700537static void hexdump(unsigned long memory, int length)
538{
539 int i;
540 uint8_t *m;
541 int all_zero = 0;
542
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500543 m = map_memory_size((intptr_t)memory, length);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700544
545 if (length > MAP_BYTES) {
546 printf("Truncating hex dump from %d to %d bytes\n\n",
547 length, MAP_BYTES);
548 length = MAP_BYTES;
549 }
550
551 for (i = 0; i < length; i += 16) {
552 int j;
553
554 all_zero++;
555 for (j = 0; j < 16; j++) {
556 if(m[i+j] != 0) {
557 all_zero = 0;
558 break;
559 }
560 }
561
562 if (all_zero < 2) {
563 printf("%08lx:", memory + i);
564 for (j = 0; j < 16; j++)
565 printf(" %02x", m[i+j]);
566 printf(" ");
567 for (j = 0; j < 16; j++)
568 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
569 printf("\n");
570 } else if (all_zero == 2) {
571 printf("...\n");
572 }
573 }
574
575 unmap_memory();
576}
577
578static void dump_cbmem_hex(void)
579{
580 if (cbmem.type != LB_MEM_TABLE) {
581 fprintf(stderr, "No coreboot CBMEM area found!\n");
582 return;
583 }
584
585 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
586}
587
588/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
589#define DYN_CBMEM_ALIGN_SIZE (4096)
590#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
591#define CBMEM_POINTER_MAGIC 0xc0389479
592#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
593
594struct cbmem_root_pointer {
595 uint32_t magic;
596 uint32_t root;
597} __attribute__((packed));
598
599struct dynamic_cbmem_entry {
600 uint32_t magic;
601 uint32_t start;
602 uint32_t size;
603 uint32_t id;
604} __attribute__((packed));
605
606struct cbmem_root {
607 uint32_t max_entries;
608 uint32_t num_entries;
609 uint32_t locked;
610 uint32_t size;
611 struct dynamic_cbmem_entry entries[0];
612} __attribute__((packed));
613
Stefan Reinauerc0199072013-01-07 16:26:10 -0800614#define CBMEM_MAGIC 0x434f5245
615#define MAX_CBMEM_ENTRIES 16
616
617struct cbmem_entry {
618 uint32_t magic;
619 uint32_t id;
620 uint64_t base;
621 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700622} __attribute__((packed));
623
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600624struct cbmem_id_to_name {
625 uint32_t id;
626 const char *name;
627};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700628static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800629
Stefan Reinauera9c83612013-07-16 17:47:35 -0700630void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
631{
632 int i;
633 const char *name;
634
635 name = NULL;
636 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
637 if (cbmem_ids[i].id == id) {
638 name = cbmem_ids[i].name;
639 break;
640 }
641 }
642
643 printf("%2d. ", n);
644 if (name == NULL)
645 printf("%08x ", id);
646 else
647 printf("%s", name);
648 printf(" %08" PRIx64 " ", base);
649 printf(" %08" PRIx64 "\n", size);
650}
651
652static void dump_static_cbmem_toc(struct cbmem_entry *entries)
653{
654 int i;
655
656 printf("CBMEM table of contents:\n");
657 printf(" ID START LENGTH\n");
658
659 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
660 if (entries[i].magic != CBMEM_MAGIC)
661 break;
662 cbmem_print_entry(i, entries[i].id,
663 entries[i].base, entries[i].size);
664 }
665}
666
667static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
668{
669 int i;
670 debug("CBMEM: max_entries=%d num_entries=%d locked=0x%x, size=%d\n\n",
671 root->max_entries, root->num_entries, root->locked, root->size);
672
673 printf("CBMEM table of contents:\n");
674 printf(" ID START LENGTH\n");
675
676 for (i = 0; i < root->num_entries; i++) {
677 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
678 break;
679 cbmem_print_entry(i, root->entries[i].id,
680 root->entries[i].start, root->entries[i].size);
681 }
682}
683
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800684static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800685{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800686 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700687 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800688 struct cbmem_entry *entries;
689
690 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700691 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800692 return;
693 }
694
695 start = unpack_lb64(cbmem.start);
696
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500697 cbmem_area = map_memory_size(start, unpack_lb64(cbmem.size));
Stefan Reinauera9c83612013-07-16 17:47:35 -0700698 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800699
Stefan Reinauera9c83612013-07-16 17:47:35 -0700700 if (entries[0].magic == CBMEM_MAGIC) {
701 dump_static_cbmem_toc(entries);
702 } else {
703 uint64_t rootptr;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800704
Stefan Reinauera9c83612013-07-16 17:47:35 -0700705 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
706 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
707 rootptr -= sizeof(struct cbmem_root_pointer);
708 unmap_memory();
709 struct cbmem_root_pointer *r =
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500710 map_memory_size(rootptr, sizeof(*r));
Stefan Reinauera9c83612013-07-16 17:47:35 -0700711 if (r->magic == CBMEM_POINTER_MAGIC) {
712 struct cbmem_root *root;
713 uint64_t rootaddr = r->root;
714 unmap_memory();
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500715 root = map_memory_size(rootaddr, ROOT_MIN_SIZE);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700716 dump_dynamic_cbmem_toc(root);
717 } else
718 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800719 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700720
Stefan Reinauerc0199072013-01-07 16:26:10 -0800721 unmap_memory();
722}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800723
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800724#define COVERAGE_MAGIC 0x584d4153
725struct file {
726 uint32_t magic;
727 uint32_t next;
728 uint32_t filename;
729 uint32_t data;
730 int offset;
731 int len;
732};
733
734static int mkpath(char *path, mode_t mode)
735{
736 assert (path && *path);
737 char *p;
738 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
739 *p = '\0';
740 if (mkdir(path, mode) == -1) {
741 if (errno != EEXIST) {
742 *p = '/';
743 return -1;
744 }
745 }
746 *p = '/';
747 }
748 return 0;
749}
750
751static void dump_coverage(void)
752{
753 int i, found = 0;
754 uint64_t start;
755 struct cbmem_entry *entries;
756 void *coverage;
757 unsigned long phys_offset;
758#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
759
760 if (cbmem.type != LB_MEM_TABLE) {
761 fprintf(stderr, "No coreboot table area found!\n");
762 return;
763 }
764
765 start = unpack_lb64(cbmem.start);
766
767 entries = (struct cbmem_entry *)map_memory(start);
768
769 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
770 if (entries[i].magic != CBMEM_MAGIC)
771 break;
772 if (entries[i].id == CBMEM_ID_COVERAGE) {
773 found = 1;
774 break;
775 }
776 }
777
778 if (!found) {
779 unmap_memory();
780 fprintf(stderr, "No coverage information found in"
781 " CBMEM area.\n");
782 return;
783 }
784
785 start = entries[i].base;
786 unmap_memory();
787 /* Map coverage area */
788 coverage = map_memory(start);
789 phys_offset = (unsigned long)coverage - (unsigned long)start;
790
791 printf("Dumping coverage data...\n");
792
793 struct file *file = (struct file *)coverage;
794 while (file && file->magic == COVERAGE_MAGIC) {
795 FILE *f;
796 char *filename;
797
798 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
799 filename = strdup((char *)phys_to_virt(file->filename));
800 if (mkpath(filename, 0755) == -1) {
801 perror("Directory for coverage data could "
802 "not be created");
803 exit(1);
804 }
805 f = fopen(filename, "wb");
806 if (!f) {
807 printf("Could not open %s: %s\n",
808 filename, strerror(errno));
809 exit(1);
810 }
811 if (fwrite((void *)phys_to_virt(file->data),
812 file->len, 1, f) != 1) {
813 printf("Could not write to %s: %s\n",
814 filename, strerror(errno));
815 exit(1);
816 }
817 fclose(f);
818 free(filename);
819
820 if (file->next)
821 file = (struct file *)phys_to_virt(file->next);
822 else
823 file = NULL;
824 }
825 unmap_memory();
826}
827
828static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800829{
830 printf("cbmem v%s -- ", CBMEM_VERSION);
831 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
832 printf(
833 "This program is free software: you can redistribute it and/or modify\n"
834 "it under the terms of the GNU General Public License as published by\n"
835 "the Free Software Foundation, version 2 of the License.\n\n"
836 "This program is distributed in the hope that it will be useful,\n"
837 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
838 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
839 "GNU General Public License for more details.\n\n"
840 "You should have received a copy of the GNU General Public License\n"
841 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
842}
843
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800844static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800845{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700846 printf("usage: %s [-cCltxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800847 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800848 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800849 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800850 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700851 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800852 " -t | --timestamps: print timestamp information\n"
853 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800854 " -v | --version: print the version\n"
855 " -h | --help: print this help\n"
856 "\n");
857 exit(1);
858}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700859
Julius Werner337de4c2014-06-16 23:02:03 -0700860#ifdef __arm__
861static void dt_update_cells(const char *name, int *addr_cells_ptr,
862 int *size_cells_ptr)
863{
864 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
865 return;
866
867 int buffer;
868 size_t nlen = strlen(name);
869 char *prop = alloca(nlen + sizeof("/#address-cells"));
870 strcpy(prop, name);
871
872 if (*addr_cells_ptr < 0) {
873 strcpy(prop + nlen, "/#address-cells");
874 int fd = open(prop, O_RDONLY);
875 if (fd < 0 && errno != ENOENT) {
876 perror(prop);
877 } else if (fd >= 0) {
878 if (read(fd, &buffer, sizeof(int)) < 0)
879 perror(prop);
880 else
881 *addr_cells_ptr = ntohl(buffer);
882 close(fd);
883 }
884 }
885
886 if (*size_cells_ptr < 0) {
887 strcpy(prop + nlen, "/#size-cells");
888 int fd = open(prop, O_RDONLY);
889 if (fd < 0 && errno != ENOENT) {
890 perror(prop);
891 } else if (fd >= 0) {
892 if (read(fd, &buffer, sizeof(int)) < 0)
893 perror(prop);
894 else
895 *size_cells_ptr = ntohl(buffer);
896 close(fd);
897 }
898 }
899}
900
901static char *dt_find_compat(const char *parent, const char *compat,
902 int *addr_cells_ptr, int *size_cells_ptr)
903{
904 char *ret = NULL;
905 struct dirent *entry;
906 DIR *dir;
907
908 if (!(dir = opendir(parent))) {
909 perror(parent);
910 return NULL;
911 }
912
913 /* Loop through all files in the directory (DT node). */
914 while ((entry = readdir(dir))) {
915 /* We only care about compatible props or subnodes. */
916 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
917 !strcmp(entry->d_name, "compatible")))
918 continue;
919
920 /* Assemble the file name (on the stack, for speed). */
921 size_t plen = strlen(parent);
922 char *name = alloca(plen + strlen(entry->d_name) + 2);
923
924 strcpy(name, parent);
925 name[plen] = '/';
926 strcpy(name + plen + 1, entry->d_name);
927
928 /* If it's a subnode, recurse. */
929 if (entry->d_type & DT_DIR) {
930 ret = dt_find_compat(name, compat, addr_cells_ptr,
931 size_cells_ptr);
932
933 /* There is only one matching node to find, abort. */
934 if (ret) {
935 /* Gather cells values on the way up. */
936 dt_update_cells(parent, addr_cells_ptr,
937 size_cells_ptr);
938 break;
939 }
940 continue;
941 }
942
943 /* If it's a compatible string, see if it's the right one. */
944 int fd = open(name, O_RDONLY);
945 int clen = strlen(compat);
946 char *buffer = alloca(clen + 1);
947
948 if (fd < 0) {
949 perror(name);
950 continue;
951 }
952
953 if (read(fd, buffer, clen + 1) < 0) {
954 perror(name);
955 close(fd);
956 continue;
957 }
958 close(fd);
959
960 if (!strcmp(compat, buffer)) {
961 /* Initialize these to "unset" for the way up. */
962 *addr_cells_ptr = *size_cells_ptr = -1;
963
964 /* Can't leave string on the stack or we'll lose it! */
965 ret = strdup(parent);
966 break;
967 }
968 }
969
970 closedir(dir);
971 return ret;
972}
973#endif /* __arm__ */
974
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700975int main(int argc, char** argv)
976{
Stefan Reinauer19f87562013-01-07 13:37:12 -0800977 int print_defaults = 1;
978 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800979 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800980 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700981 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800982 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800983
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800984 int opt, option_index = 0;
985 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800986 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800987 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -0800988 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -0800989 {"timestamps", 0, 0, 't'},
Stefan Reinauera9c83612013-07-16 17:47:35 -0700990 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800991 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800992 {"version", 0, 0, 'v'},
993 {"help", 0, 0, 'h'},
994 {0, 0, 0, 0}
995 };
Stefan Reinauera9c83612013-07-16 17:47:35 -0700996 while ((opt = getopt_long(argc, argv, "cCltxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800997 long_options, &option_index)) != EOF) {
998 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800999 case 'c':
1000 print_console = 1;
1001 print_defaults = 0;
1002 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001003 case 'C':
1004 print_coverage = 1;
1005 print_defaults = 0;
1006 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001007 case 'l':
1008 print_list = 1;
1009 print_defaults = 0;
1010 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001011 case 'x':
1012 print_hexdump = 1;
1013 print_defaults = 0;
1014 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001015 case 't':
1016 print_timestamps = 1;
1017 print_defaults = 0;
1018 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001019 case 'V':
1020 verbose = 1;
1021 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001022 case 'v':
1023 print_version();
1024 exit(0);
1025 break;
1026 case 'h':
1027 case '?':
1028 default:
1029 print_usage(argv[0]);
1030 exit(0);
1031 break;
1032 }
1033 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001034
Julius Werner337de4c2014-06-16 23:02:03 -07001035 mem_fd = open("/dev/mem", O_RDONLY, 0);
1036 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001037 fprintf(stderr, "Failed to gain memory access: %s\n",
1038 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001039 return 1;
1040 }
1041
Stefan Reinauer7f681502013-06-19 15:39:09 -07001042#ifdef __arm__
Julius Werner337de4c2014-06-16 23:02:03 -07001043 int addr_cells, size_cells;
1044 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1045 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001046
Julius Werner337de4c2014-06-16 23:02:03 -07001047 if (!coreboot_node) {
1048 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001049 return 1;
1050 }
1051
Julius Werner337de4c2014-06-16 23:02:03 -07001052 if (addr_cells < 0) {
1053 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1054 addr_cells = 1;
1055 }
1056
1057 int nlen = strlen(coreboot_node);
1058 char *reg = alloca(nlen + sizeof("/reg"));
1059
1060 strcpy(reg, coreboot_node);
1061 strcpy(reg + nlen, "/reg");
1062 free(coreboot_node);
1063
1064 int fd = open(reg, O_RDONLY);
1065 if (fd < 0) {
1066 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001067 return 1;
1068 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001069
Julius Werner337de4c2014-06-16 23:02:03 -07001070 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001071 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1072 u8 *dtbuffer = alloca(size_to_read);
1073 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001074 perror(reg);
1075 return 1;
1076 }
1077 close(fd);
1078
1079 /* No variable-length byte swap function anywhere in C... how sad. */
1080 u64 baseaddr = 0;
1081 for (i = 0; i < addr_cells * 4; i++) {
1082 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001083 baseaddr |= *dtbuffer;
1084 dtbuffer++;
1085 }
1086 u64 cb_table_size = 0;
1087 for (i = 0; i < size_cells * 4; i++) {
1088 cb_table_size <<= 8;
1089 cb_table_size |= *dtbuffer;
1090 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001091 }
1092
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001093 parse_cbtable(baseaddr, cb_table_size);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001094#else
1095 int j;
1096 static const int possible_base_addresses[] = { 0, 0xf0000 };
1097
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001098 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001099 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001100 if (parse_cbtable(possible_base_addresses[j], MAP_BYTES))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001101 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001102 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001103#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001104
Stefan Reinauer19f87562013-01-07 13:37:12 -08001105 if (print_console)
1106 dump_console();
1107
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001108 if (print_coverage)
1109 dump_coverage();
1110
Stefan Reinauerc0199072013-01-07 16:26:10 -08001111 if (print_list)
1112 dump_cbmem_toc();
1113
Stefan Reinauera9c83612013-07-16 17:47:35 -07001114 if (print_hexdump)
1115 dump_cbmem_hex();
1116
Stefan Reinauer19f87562013-01-07 13:37:12 -08001117 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001118 dump_timestamps();
1119
Julius Werner337de4c2014-06-16 23:02:03 -07001120 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001121 return 0;
1122}