blob: 7007354b34a6dce0fc244fcbf0715365595bb828 [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" },
416 { TS_START_KERNEL, "start kernel" }
417};
418
419void timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
420{
421 int i;
422 const char *name;
423
424 name = "<unknown>";
425 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
426 if (timestamp_ids[i].id == id) {
427 name = timestamp_ids[i].name;
428 break;
429 }
430 }
431
432 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800433 printf("%-50s", name);
434 print_norm(arch_convert_raw_ts_entry(stamp));
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700435 if (prev_stamp) {
436 printf(" (");
Julius Wernera7d92442014-12-02 20:51:19 -0800437 print_norm(arch_convert_raw_ts_entry(stamp - prev_stamp));
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700438 printf(")");
439 }
440 printf("\n");
441}
442
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700443/* dump the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800444static void dump_timestamps(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700445{
446 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800447 struct timestamp_table *tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500448 size_t size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800449
450 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
451 fprintf(stderr, "No timestamps found in coreboot table.\n");
452 return;
453 }
454
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500455 size = sizeof(*tst_p);
456 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700457
458 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500459 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
460
461 unmap_memory();
462 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size);
463
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700464 for (i = 0; i < tst_p->num_entries; i++) {
465 const struct timestamp_entry *tse_p = tst_p->entries + i;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700466 timestamp_print_entry(tse_p->entry_id, tse_p->entry_stamp,
467 i ? tse_p[-1].entry_stamp : 0);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700468 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800469
470 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700471}
472
Stefan Reinauer19f87562013-01-07 13:37:12 -0800473/* dump the cbmem console */
474static void dump_console(void)
475{
476 void *console_p;
477 char *console_c;
478 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100479 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800480
481 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
482 fprintf(stderr, "No console found in coreboot table.\n");
483 return;
484 }
485
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500486 console_p = map_memory_size((unsigned long)console.cbmem_addr,
487 2 * sizeof(uint32_t));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800488 /* The in-memory format of the console area is:
489 * u32 size
490 * u32 cursor
491 * char console[size]
492 * Hence we have to add 8 to get to the actual console string.
493 */
Gabe Black06b13a32013-08-09 00:40:06 -0700494 size = ((uint32_t *)console_p)[0];
495 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100496 /* Cursor continues to go on even after no more data fits in
497 * the buffer but the data is dropped in this case.
498 */
499 if (size > cursor)
500 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800501 console_c = malloc(size + 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500502 unmap_memory();
Stefan Reinauer19f87562013-01-07 13:37:12 -0800503 if (!console_c) {
504 fprintf(stderr, "Not enough memory for console.\n");
505 exit(1);
506 }
507
Aaron Durbinab180d82014-03-31 11:59:58 -0500508 console_p = map_memory_size((unsigned long)console.cbmem_addr,
509 size + sizeof(size) + sizeof(cursor));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800510 memcpy(console_c, console_p + 8, size);
511 console_c[size] = 0;
Gabe Black06b13a32013-08-09 00:40:06 -0700512 console_c[cursor] = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800513
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100514 printf("%s\n", console_c);
515 if (size < cursor)
516 printf("%d %s lost\n", cursor - size,
517 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800518
519 free(console_c);
520
521 unmap_memory();
522}
523
Stefan Reinauera9c83612013-07-16 17:47:35 -0700524static void hexdump(unsigned long memory, int length)
525{
526 int i;
527 uint8_t *m;
528 int all_zero = 0;
529
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500530 m = map_memory_size((intptr_t)memory, length);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700531
532 if (length > MAP_BYTES) {
533 printf("Truncating hex dump from %d to %d bytes\n\n",
534 length, MAP_BYTES);
535 length = MAP_BYTES;
536 }
537
538 for (i = 0; i < length; i += 16) {
539 int j;
540
541 all_zero++;
542 for (j = 0; j < 16; j++) {
543 if(m[i+j] != 0) {
544 all_zero = 0;
545 break;
546 }
547 }
548
549 if (all_zero < 2) {
550 printf("%08lx:", memory + i);
551 for (j = 0; j < 16; j++)
552 printf(" %02x", m[i+j]);
553 printf(" ");
554 for (j = 0; j < 16; j++)
555 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
556 printf("\n");
557 } else if (all_zero == 2) {
558 printf("...\n");
559 }
560 }
561
562 unmap_memory();
563}
564
565static void dump_cbmem_hex(void)
566{
567 if (cbmem.type != LB_MEM_TABLE) {
568 fprintf(stderr, "No coreboot CBMEM area found!\n");
569 return;
570 }
571
572 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
573}
574
575/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
576#define DYN_CBMEM_ALIGN_SIZE (4096)
577#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
578#define CBMEM_POINTER_MAGIC 0xc0389479
579#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
580
581struct cbmem_root_pointer {
582 uint32_t magic;
583 uint32_t root;
584} __attribute__((packed));
585
586struct dynamic_cbmem_entry {
587 uint32_t magic;
588 uint32_t start;
589 uint32_t size;
590 uint32_t id;
591} __attribute__((packed));
592
593struct cbmem_root {
594 uint32_t max_entries;
595 uint32_t num_entries;
596 uint32_t locked;
597 uint32_t size;
598 struct dynamic_cbmem_entry entries[0];
599} __attribute__((packed));
600
Stefan Reinauerc0199072013-01-07 16:26:10 -0800601#define CBMEM_MAGIC 0x434f5245
602#define MAX_CBMEM_ENTRIES 16
603
604struct cbmem_entry {
605 uint32_t magic;
606 uint32_t id;
607 uint64_t base;
608 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700609} __attribute__((packed));
610
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600611struct cbmem_id_to_name {
612 uint32_t id;
613 const char *name;
614};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700615static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800616
Stefan Reinauera9c83612013-07-16 17:47:35 -0700617void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
618{
619 int i;
620 const char *name;
621
622 name = NULL;
623 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
624 if (cbmem_ids[i].id == id) {
625 name = cbmem_ids[i].name;
626 break;
627 }
628 }
629
630 printf("%2d. ", n);
631 if (name == NULL)
632 printf("%08x ", id);
633 else
634 printf("%s", name);
635 printf(" %08" PRIx64 " ", base);
636 printf(" %08" PRIx64 "\n", size);
637}
638
639static void dump_static_cbmem_toc(struct cbmem_entry *entries)
640{
641 int i;
642
643 printf("CBMEM table of contents:\n");
644 printf(" ID START LENGTH\n");
645
646 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
647 if (entries[i].magic != CBMEM_MAGIC)
648 break;
649 cbmem_print_entry(i, entries[i].id,
650 entries[i].base, entries[i].size);
651 }
652}
653
654static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
655{
656 int i;
657 debug("CBMEM: max_entries=%d num_entries=%d locked=0x%x, size=%d\n\n",
658 root->max_entries, root->num_entries, root->locked, root->size);
659
660 printf("CBMEM table of contents:\n");
661 printf(" ID START LENGTH\n");
662
663 for (i = 0; i < root->num_entries; i++) {
664 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
665 break;
666 cbmem_print_entry(i, root->entries[i].id,
667 root->entries[i].start, root->entries[i].size);
668 }
669}
670
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800671static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800672{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800673 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700674 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800675 struct cbmem_entry *entries;
676
677 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700678 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800679 return;
680 }
681
682 start = unpack_lb64(cbmem.start);
683
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500684 cbmem_area = map_memory_size(start, unpack_lb64(cbmem.size));
Stefan Reinauera9c83612013-07-16 17:47:35 -0700685 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800686
Stefan Reinauera9c83612013-07-16 17:47:35 -0700687 if (entries[0].magic == CBMEM_MAGIC) {
688 dump_static_cbmem_toc(entries);
689 } else {
690 uint64_t rootptr;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800691
Stefan Reinauera9c83612013-07-16 17:47:35 -0700692 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
693 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
694 rootptr -= sizeof(struct cbmem_root_pointer);
695 unmap_memory();
696 struct cbmem_root_pointer *r =
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500697 map_memory_size(rootptr, sizeof(*r));
Stefan Reinauera9c83612013-07-16 17:47:35 -0700698 if (r->magic == CBMEM_POINTER_MAGIC) {
699 struct cbmem_root *root;
700 uint64_t rootaddr = r->root;
701 unmap_memory();
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500702 root = map_memory_size(rootaddr, ROOT_MIN_SIZE);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700703 dump_dynamic_cbmem_toc(root);
704 } else
705 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800706 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700707
Stefan Reinauerc0199072013-01-07 16:26:10 -0800708 unmap_memory();
709}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800710
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800711#define COVERAGE_MAGIC 0x584d4153
712struct file {
713 uint32_t magic;
714 uint32_t next;
715 uint32_t filename;
716 uint32_t data;
717 int offset;
718 int len;
719};
720
721static int mkpath(char *path, mode_t mode)
722{
723 assert (path && *path);
724 char *p;
725 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
726 *p = '\0';
727 if (mkdir(path, mode) == -1) {
728 if (errno != EEXIST) {
729 *p = '/';
730 return -1;
731 }
732 }
733 *p = '/';
734 }
735 return 0;
736}
737
738static void dump_coverage(void)
739{
740 int i, found = 0;
741 uint64_t start;
742 struct cbmem_entry *entries;
743 void *coverage;
744 unsigned long phys_offset;
745#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
746
747 if (cbmem.type != LB_MEM_TABLE) {
748 fprintf(stderr, "No coreboot table area found!\n");
749 return;
750 }
751
752 start = unpack_lb64(cbmem.start);
753
754 entries = (struct cbmem_entry *)map_memory(start);
755
756 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
757 if (entries[i].magic != CBMEM_MAGIC)
758 break;
759 if (entries[i].id == CBMEM_ID_COVERAGE) {
760 found = 1;
761 break;
762 }
763 }
764
765 if (!found) {
766 unmap_memory();
767 fprintf(stderr, "No coverage information found in"
768 " CBMEM area.\n");
769 return;
770 }
771
772 start = entries[i].base;
773 unmap_memory();
774 /* Map coverage area */
775 coverage = map_memory(start);
776 phys_offset = (unsigned long)coverage - (unsigned long)start;
777
778 printf("Dumping coverage data...\n");
779
780 struct file *file = (struct file *)coverage;
781 while (file && file->magic == COVERAGE_MAGIC) {
782 FILE *f;
783 char *filename;
784
785 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
786 filename = strdup((char *)phys_to_virt(file->filename));
787 if (mkpath(filename, 0755) == -1) {
788 perror("Directory for coverage data could "
789 "not be created");
790 exit(1);
791 }
792 f = fopen(filename, "wb");
793 if (!f) {
794 printf("Could not open %s: %s\n",
795 filename, strerror(errno));
796 exit(1);
797 }
798 if (fwrite((void *)phys_to_virt(file->data),
799 file->len, 1, f) != 1) {
800 printf("Could not write to %s: %s\n",
801 filename, strerror(errno));
802 exit(1);
803 }
804 fclose(f);
805 free(filename);
806
807 if (file->next)
808 file = (struct file *)phys_to_virt(file->next);
809 else
810 file = NULL;
811 }
812 unmap_memory();
813}
814
815static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800816{
817 printf("cbmem v%s -- ", CBMEM_VERSION);
818 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
819 printf(
820 "This program is free software: you can redistribute it and/or modify\n"
821 "it under the terms of the GNU General Public License as published by\n"
822 "the Free Software Foundation, version 2 of the License.\n\n"
823 "This program is distributed in the hope that it will be useful,\n"
824 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
825 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
826 "GNU General Public License for more details.\n\n"
827 "You should have received a copy of the GNU General Public License\n"
828 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
829}
830
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800831static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800832{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700833 printf("usage: %s [-cCltxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800834 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800835 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800836 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800837 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700838 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800839 " -t | --timestamps: print timestamp information\n"
840 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800841 " -v | --version: print the version\n"
842 " -h | --help: print this help\n"
843 "\n");
844 exit(1);
845}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700846
Julius Werner337de4c2014-06-16 23:02:03 -0700847#ifdef __arm__
848static void dt_update_cells(const char *name, int *addr_cells_ptr,
849 int *size_cells_ptr)
850{
851 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
852 return;
853
854 int buffer;
855 size_t nlen = strlen(name);
856 char *prop = alloca(nlen + sizeof("/#address-cells"));
857 strcpy(prop, name);
858
859 if (*addr_cells_ptr < 0) {
860 strcpy(prop + nlen, "/#address-cells");
861 int fd = open(prop, O_RDONLY);
862 if (fd < 0 && errno != ENOENT) {
863 perror(prop);
864 } else if (fd >= 0) {
865 if (read(fd, &buffer, sizeof(int)) < 0)
866 perror(prop);
867 else
868 *addr_cells_ptr = ntohl(buffer);
869 close(fd);
870 }
871 }
872
873 if (*size_cells_ptr < 0) {
874 strcpy(prop + nlen, "/#size-cells");
875 int fd = open(prop, O_RDONLY);
876 if (fd < 0 && errno != ENOENT) {
877 perror(prop);
878 } else if (fd >= 0) {
879 if (read(fd, &buffer, sizeof(int)) < 0)
880 perror(prop);
881 else
882 *size_cells_ptr = ntohl(buffer);
883 close(fd);
884 }
885 }
886}
887
888static char *dt_find_compat(const char *parent, const char *compat,
889 int *addr_cells_ptr, int *size_cells_ptr)
890{
891 char *ret = NULL;
892 struct dirent *entry;
893 DIR *dir;
894
895 if (!(dir = opendir(parent))) {
896 perror(parent);
897 return NULL;
898 }
899
900 /* Loop through all files in the directory (DT node). */
901 while ((entry = readdir(dir))) {
902 /* We only care about compatible props or subnodes. */
903 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
904 !strcmp(entry->d_name, "compatible")))
905 continue;
906
907 /* Assemble the file name (on the stack, for speed). */
908 size_t plen = strlen(parent);
909 char *name = alloca(plen + strlen(entry->d_name) + 2);
910
911 strcpy(name, parent);
912 name[plen] = '/';
913 strcpy(name + plen + 1, entry->d_name);
914
915 /* If it's a subnode, recurse. */
916 if (entry->d_type & DT_DIR) {
917 ret = dt_find_compat(name, compat, addr_cells_ptr,
918 size_cells_ptr);
919
920 /* There is only one matching node to find, abort. */
921 if (ret) {
922 /* Gather cells values on the way up. */
923 dt_update_cells(parent, addr_cells_ptr,
924 size_cells_ptr);
925 break;
926 }
927 continue;
928 }
929
930 /* If it's a compatible string, see if it's the right one. */
931 int fd = open(name, O_RDONLY);
932 int clen = strlen(compat);
933 char *buffer = alloca(clen + 1);
934
935 if (fd < 0) {
936 perror(name);
937 continue;
938 }
939
940 if (read(fd, buffer, clen + 1) < 0) {
941 perror(name);
942 close(fd);
943 continue;
944 }
945 close(fd);
946
947 if (!strcmp(compat, buffer)) {
948 /* Initialize these to "unset" for the way up. */
949 *addr_cells_ptr = *size_cells_ptr = -1;
950
951 /* Can't leave string on the stack or we'll lose it! */
952 ret = strdup(parent);
953 break;
954 }
955 }
956
957 closedir(dir);
958 return ret;
959}
960#endif /* __arm__ */
961
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700962int main(int argc, char** argv)
963{
Stefan Reinauer19f87562013-01-07 13:37:12 -0800964 int print_defaults = 1;
965 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800966 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800967 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700968 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800969 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800970
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800971 int opt, option_index = 0;
972 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800973 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800974 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -0800975 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -0800976 {"timestamps", 0, 0, 't'},
Stefan Reinauera9c83612013-07-16 17:47:35 -0700977 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800978 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800979 {"version", 0, 0, 'v'},
980 {"help", 0, 0, 'h'},
981 {0, 0, 0, 0}
982 };
Stefan Reinauera9c83612013-07-16 17:47:35 -0700983 while ((opt = getopt_long(argc, argv, "cCltxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800984 long_options, &option_index)) != EOF) {
985 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -0800986 case 'c':
987 print_console = 1;
988 print_defaults = 0;
989 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800990 case 'C':
991 print_coverage = 1;
992 print_defaults = 0;
993 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800994 case 'l':
995 print_list = 1;
996 print_defaults = 0;
997 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700998 case 'x':
999 print_hexdump = 1;
1000 print_defaults = 0;
1001 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001002 case 't':
1003 print_timestamps = 1;
1004 print_defaults = 0;
1005 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001006 case 'V':
1007 verbose = 1;
1008 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001009 case 'v':
1010 print_version();
1011 exit(0);
1012 break;
1013 case 'h':
1014 case '?':
1015 default:
1016 print_usage(argv[0]);
1017 exit(0);
1018 break;
1019 }
1020 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001021
Julius Werner337de4c2014-06-16 23:02:03 -07001022 mem_fd = open("/dev/mem", O_RDONLY, 0);
1023 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001024 fprintf(stderr, "Failed to gain memory access: %s\n",
1025 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001026 return 1;
1027 }
1028
Stefan Reinauer7f681502013-06-19 15:39:09 -07001029#ifdef __arm__
Julius Werner337de4c2014-06-16 23:02:03 -07001030 int addr_cells, size_cells;
1031 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1032 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001033
Julius Werner337de4c2014-06-16 23:02:03 -07001034 if (!coreboot_node) {
1035 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001036 return 1;
1037 }
1038
Julius Werner337de4c2014-06-16 23:02:03 -07001039 if (addr_cells < 0) {
1040 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1041 addr_cells = 1;
1042 }
1043
1044 int nlen = strlen(coreboot_node);
1045 char *reg = alloca(nlen + sizeof("/reg"));
1046
1047 strcpy(reg, coreboot_node);
1048 strcpy(reg + nlen, "/reg");
1049 free(coreboot_node);
1050
1051 int fd = open(reg, O_RDONLY);
1052 if (fd < 0) {
1053 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001054 return 1;
1055 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001056
Julius Werner337de4c2014-06-16 23:02:03 -07001057 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001058 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1059 u8 *dtbuffer = alloca(size_to_read);
1060 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001061 perror(reg);
1062 return 1;
1063 }
1064 close(fd);
1065
1066 /* No variable-length byte swap function anywhere in C... how sad. */
1067 u64 baseaddr = 0;
1068 for (i = 0; i < addr_cells * 4; i++) {
1069 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001070 baseaddr |= *dtbuffer;
1071 dtbuffer++;
1072 }
1073 u64 cb_table_size = 0;
1074 for (i = 0; i < size_cells * 4; i++) {
1075 cb_table_size <<= 8;
1076 cb_table_size |= *dtbuffer;
1077 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001078 }
1079
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001080 parse_cbtable(baseaddr, cb_table_size);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001081#else
1082 int j;
1083 static const int possible_base_addresses[] = { 0, 0xf0000 };
1084
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001085 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001086 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001087 if (parse_cbtable(possible_base_addresses[j], MAP_BYTES))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001088 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001089 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001090#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001091
Stefan Reinauer19f87562013-01-07 13:37:12 -08001092 if (print_console)
1093 dump_console();
1094
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001095 if (print_coverage)
1096 dump_coverage();
1097
Stefan Reinauerc0199072013-01-07 16:26:10 -08001098 if (print_list)
1099 dump_cbmem_toc();
1100
Stefan Reinauera9c83612013-07-16 17:47:35 -07001101 if (print_hexdump)
1102 dump_cbmem_hex();
1103
Stefan Reinauer19f87562013-01-07 13:37:12 -08001104 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001105 dump_timestamps();
1106
Julius Werner337de4c2014-06-16 23:02:03 -07001107 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001108 return 0;
1109}