blob: e294c20578f4e66c3938801215b13b4dd309f890 [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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070018 */
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
Timothy Pearsondf699d52015-05-16 14:55:54 -050061/* IMD root pointer location */
62static uint64_t rootptr = 0;
63
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070064/*
65 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
66 * the buffer length is odd last byte is excluded from the calculation
67 */
68static u16 ipchcksum(const void *addr, unsigned size)
69{
70 const u16 *p = addr;
71 unsigned i, n = size / 2; /* don't expect odd sized blocks */
72 u32 sum = 0;
73
74 for (i = 0; i < n; i++)
75 sum += p[i];
76
77 sum = (sum >> 16) + (sum & 0xffff);
78 sum += (sum >> 16);
79 sum = ~sum & 0xffff;
80 return (u16) sum;
81}
82
83/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080084 * Functions to map / unmap physical memory into virtual address space. These
85 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070086 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080087static void *mapped_virtual;
Aaron Durbinab180d82014-03-31 11:59:58 -050088static size_t mapped_size;
89
90static inline size_t size_to_mib(size_t sz)
91{
92 return sz >> 20;
93}
94
95static void unmap_memory(void)
96{
97 if (mapped_virtual == NULL) {
98 fprintf(stderr, "Error unmapping memory\n");
99 return;
100 }
Timothy Pearsondf699d52015-05-16 14:55:54 -0500101 if (size_to_mib(mapped_size) == 0) {
102 debug("Unmapping %zuMB of virtual memory at %p.\n",
103 size_to_mib(mapped_size), mapped_virtual);
104 }
105 else {
106 debug("Unmapping %zuMB of virtual memory at %p.\n",
107 size_to_mib(mapped_size), mapped_virtual);
108 }
Aaron Durbinab180d82014-03-31 11:59:58 -0500109 munmap(mapped_virtual, mapped_size);
110 mapped_virtual = NULL;
111 mapped_size = 0;
112}
113
114static void *map_memory_size(u64 physical, size_t size)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700115{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800116 void *v;
117 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -0700118 u64 page = getpagesize();
Aaron Durbinab180d82014-03-31 11:59:58 -0500119 size_t padding;
120
121 if (mapped_virtual != NULL)
122 unmap_memory();
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800123
124 /* Mapped memory must be aligned to page size */
125 p = physical & ~(page - 1);
Aaron Durbinab180d82014-03-31 11:59:58 -0500126 padding = physical & (page-1);
127 size += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800128
Timothy Pearsondf699d52015-05-16 14:55:54 -0500129 if (size_to_mib(size) == 0) {
130 debug("Mapping %zuB of physical memory at 0x%jx (requested 0x%jx).\n",
131 size, (intmax_t)p, (intmax_t)physical);
132 }
133 else {
134 debug("Mapping %zuMB of physical memory at 0x%jx (requested 0x%jx).\n",
135 size_to_mib(size), (intmax_t)p, (intmax_t)physical);
136 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800137
Julius Werner337de4c2014-06-16 23:02:03 -0700138 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800139
140 if (v == MAP_FAILED) {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500141 /* The mapped area may have overrun the upper cbmem boundary when trying to
142 * align to the page size. Try growing down instead of up...
143 */
144 p -= page;
145 padding += page;
146 size &= ~(page - 1);
147 size = size + (page - 1);
148 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
149 debug(" ... failed. Mapping %zuB of physical memory at 0x%jx.\n",
150 size, (intmax_t)p);
151 }
152
153 if (v == MAP_FAILED) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800154 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
155 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700156 exit(1);
157 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800158
159 /* Remember what we actually mapped ... */
160 mapped_virtual = v;
Aaron Durbinab180d82014-03-31 11:59:58 -0500161 mapped_size = size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800162
163 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700164 if (padding)
Aaron Durbinab180d82014-03-31 11:59:58 -0500165 debug(" ... padding virtual address with 0x%zx bytes.\n",
Stefan Reinauera9c83612013-07-16 17:47:35 -0700166 padding);
167 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800168
169 return v;
170}
171
Aaron Durbinab180d82014-03-31 11:59:58 -0500172static void *map_memory(u64 physical)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800173{
Aaron Durbinab180d82014-03-31 11:59:58 -0500174 return map_memory_size(physical, MAP_BYTES);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700175}
176
177/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800178 * Try finding the timestamp table and coreboot cbmem console starting from the
179 * passed in memory offset. Could be called recursively in case a forwarding
180 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700181 *
182 * Returns pointer to a memory buffer containg the timestamp table or zero if
183 * none found.
184 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800185
186static struct lb_cbmem_ref timestamps;
187static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800188static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800189
Stefan Reinauer8c594772013-04-19 14:22:29 -0700190/* This is a work-around for a nasty problem introduced by initially having
191 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
192 * on 64bit x86 systems because coreboot is 32bit on those systems.
193 * When the problem was found, it was corrected, but there are a lot of
194 * systems out there with a firmware that does not produce the right
195 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
196 */
197static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
198{
199 struct lb_cbmem_ref ret;
200
201 ret = *cbmem_ref;
202
203 if (cbmem_ref->size < sizeof(*cbmem_ref))
204 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
205
Stefan Reinauera9c83612013-07-16 17:47:35 -0700206 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
207
Stefan Reinauer8c594772013-04-19 14:22:29 -0700208 return ret;
209}
210
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500211static int parse_cbtable(u64 address, size_t table_size)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700212{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800213 int i, found = 0;
214 void *buf;
215
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500216 debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n",
217 address, table_size);
218 buf = map_memory_size(address, table_size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700219
220 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800221
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700222 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800223 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700224 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800225 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700226 int j;
227
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800228 lbh = (struct lb_header *)(buf + i);
229 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
230 !lbh->header_bytes ||
231 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700232 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700233 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800234 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700235
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800236 if (ipchcksum(lbtable, lbh->table_bytes) !=
237 lbh->table_checksum) {
238 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700239 continue;
240 }
241
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800242 found = 1;
243 debug("Found!\n");
244
245 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800246 lbr_p = (struct lb_record*) ((char *)lbtable + j);
247 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700248 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800249 case LB_TAG_MEMORY: {
250 int i = 0;
251 debug(" Found memory map.\n");
252 struct lb_memory *memory =
253 (struct lb_memory *)lbr_p;
Paul Menzel747c07f2014-10-17 13:46:12 +0200254 while ((char *)&memory->map[i] < ((char *)lbr_p
Stefan Reinauerc0199072013-01-07 16:26:10 -0800255 + lbr_p->size)) {
256 if (memory->map[i].type == LB_MEM_TABLE) {
257 debug(" LB_MEM_TABLE found.\n");
258 /* The last one found is CBMEM */
259 cbmem = memory->map[i];
260 }
261 i++;
262 }
263 continue;
264 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700265 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800266 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700267 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800268 continue;
269 }
270 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800271 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700272 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800273 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700274 }
275 case LB_TAG_FORWARD: {
276 /*
277 * This is a forwarding entry - repeat the
278 * search at the new address.
279 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800280 struct lb_forward lbf_p =
281 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800282 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800283 unmap_memory();
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500284 return parse_cbtable(lbf_p.forward, table_size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700285 }
286 default:
287 break;
288 }
289
290 }
291 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800292 unmap_memory();
293
294 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700295}
296
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700297#if defined(__i386__) || defined(__x86_64__)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700298/*
299 * read CPU frequency from a sysfs file, return an frequency in Kilohertz as
300 * an int or exit on any error.
301 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800302static u64 get_cpu_freq_KHz(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700303{
304 FILE *cpuf;
305 char freqs[100];
306 int size;
307 char *endp;
308 u64 rv;
309
310 const char* freq_file =
311 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
312
313 cpuf = fopen(freq_file, "r");
314 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800315 fprintf(stderr, "Could not open %s: %s\n",
316 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700317 exit(1);
318 }
319
320 memset(freqs, 0, sizeof(freqs));
321 size = fread(freqs, 1, sizeof(freqs), cpuf);
322 if (!size || (size == sizeof(freqs))) {
323 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
324 size, freq_file);
325 exit(1);
326 }
327 fclose(cpuf);
328 rv = strtoull(freqs, &endp, 10);
329
330 if (*endp == '\0' || *endp == '\n')
331 return rv;
332 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
333 freqs, freq_file);
334 exit(1);
335}
336
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700337/* On x86 platforms timestamps are stored
338 * in CPU cycles (from rdtsc). Hence the
339 * timestamp divider is the CPU frequency
340 * in MHz.
341 */
342u64 arch_convert_raw_ts_entry(u64 ts)
343{
344 static u64 cpu_freq_mhz = 0;
345
346 if (!cpu_freq_mhz)
347 cpu_freq_mhz = get_cpu_freq_KHz() / 1000;
348
349 return ts / cpu_freq_mhz;
350}
351
352#else
353
354/* On non-x86 platforms the timestamp entries
355 * are not in clock cycles but in usecs
356 */
357u64 arch_convert_raw_ts_entry(u64 ts)
358{
359 return ts;
360}
361#endif
362
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700363/*
364 * Print an integer in 'normalized' form - with commas separating every three
Julius Wernera7d92442014-12-02 20:51:19 -0800365 * decimal orders.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700366 */
Julius Wernera7d92442014-12-02 20:51:19 -0800367static void print_norm(u64 v)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700368{
Julius Wernera7d92442014-12-02 20:51:19 -0800369 if (v >= 1000) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700370 /* print the higher order sections first */
Julius Wernera7d92442014-12-02 20:51:19 -0800371 print_norm(v / 1000);
372 printf(",%3.3u", (u32)(v % 1000));
373 } else {
374 printf("%u", (u32)(v % 1000));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700375 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700376}
377
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700378enum additional_timestamp_id {
379 // Depthcharge entry IDs start at 1000.
380 TS_DC_START = 1000,
381
382 TS_RO_PARAMS_INIT = 1001,
383 TS_RO_VB_INIT = 1002,
384 TS_RO_VB_SELECT_FIRMWARE = 1003,
385 TS_RO_VB_SELECT_AND_LOAD_KERNEL = 1004,
386
387 TS_RW_VB_SELECT_AND_LOAD_KERNEL = 1010,
388
389 TS_VB_SELECT_AND_LOAD_KERNEL = 1020,
390
391 TS_CROSSYSTEM_DATA = 1100,
392 TS_START_KERNEL = 1101
393};
394
395static const struct timestamp_id_to_name {
396 u32 id;
397 const char *name;
398} timestamp_ids[] = {
399 { TS_START_ROMSTAGE, "start of rom stage" },
400 { TS_BEFORE_INITRAM, "before ram initialization" },
401 { TS_AFTER_INITRAM, "after ram initialization" },
402 { TS_END_ROMSTAGE, "end of romstage" },
403 { TS_START_VBOOT, "start of verified boot" },
404 { TS_END_VBOOT, "end of verified boot" },
Julius Wernera7d92442014-12-02 20:51:19 -0800405 { TS_START_COPYRAM, "starting to load ramstage" },
406 { TS_END_COPYRAM, "finished loading ramstage" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700407 { TS_START_RAMSTAGE, "start of ramstage" },
Julius Wernera7d92442014-12-02 20:51:19 -0800408 { TS_START_BOOTBLOCK, "start of bootblock" },
409 { TS_END_BOOTBLOCK, "end of bootblock" },
410 { TS_START_COPYROM, "starting to load romstage" },
411 { TS_END_COPYROM, "finished loading romstage" },
412 { TS_START_ULZMA, "starting LZMA decompress (ignore for x86)" },
413 { TS_END_ULZMA, "finished LZMA decompress (ignore for x86)" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700414 { TS_DEVICE_ENUMERATE, "device enumeration" },
415 { TS_DEVICE_CONFIGURE, "device configuration" },
416 { TS_DEVICE_ENABLE, "device enable" },
417 { TS_DEVICE_INITIALIZE, "device initialization" },
418 { TS_DEVICE_DONE, "device setup done" },
419 { TS_CBMEM_POST, "cbmem post" },
420 { TS_WRITE_TABLES, "write tables" },
421 { TS_LOAD_PAYLOAD, "load payload" },
422 { TS_ACPI_WAKE_JUMP, "ACPI wake jump" },
423 { TS_SELFBOOT_JUMP, "selfboot jump" },
Julius Wernera7d92442014-12-02 20:51:19 -0800424
425 { TS_START_COPYVER, "starting to load verstage" },
426 { TS_END_COPYVER, "finished loading verstage" },
427 { TS_START_TPMINIT, "starting to initialize TPM" },
428 { TS_END_TPMINIT, "finished TPM initialization" },
429 { TS_START_VERIFY_SLOT, "starting to verify keyblock/preamble (RSA)" },
430 { TS_END_VERIFY_SLOT, "finished verifying keyblock/preamble (RSA)" },
431 { TS_START_HASH_BODY, "starting to verify body (load+SHA2+RSA) " },
432 { TS_DONE_LOADING, "finished loading body (ignore for x86)" },
433 { TS_DONE_HASHING, "finished calculating body hash (SHA2)" },
434 { TS_END_HASH_BODY, "finished verifying body signature (RSA)" },
435
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700436 { TS_DC_START, "depthcharge start" },
437 { TS_RO_PARAMS_INIT, "RO parameter init" },
438 { TS_RO_VB_INIT, "RO vboot init" },
439 { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" },
440 { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" },
441 { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" },
442 { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" },
443 { TS_CROSSYSTEM_DATA, "crossystem data" },
Lee Leahyb8179082015-02-24 11:30:38 -0800444 { TS_START_KERNEL, "start kernel" },
445
446 /* FSP related timestamps */
447 { TS_FSP_MEMORY_INIT_START, "calling FspMemoryInit" },
448 { TS_FSP_MEMORY_INIT_END, "returning from FspMemoryInit" },
449 { TS_FSP_TEMP_RAM_EXIT_START, "calling FspTempRamExit" },
450 { TS_FSP_TEMP_RAM_EXIT_END, "returning from FspTempRamExit" },
451 { TS_FSP_SILICON_INIT_START, "calling FspSiliconInit" },
452 { TS_FSP_SILICON_INIT_END, "returning from FspSiliconInit" },
453 { TS_FSP_BEFORE_ENUMERATE, "calling FspNotify(AfterPciEnumeration)" },
454 { TS_FSP_AFTER_ENUMERATE,
455 "returning from FspNotify(AfterPciEnumeration)" },
456 { TS_FSP_BEFORE_FINALIZE, "calling FspNotify(ReadyToBoot)" },
457 { TS_FSP_AFTER_FINALIZE, "returning from FspNotify(ReadyToBoot)" }
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700458};
459
460void timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
461{
462 int i;
463 const char *name;
464
465 name = "<unknown>";
466 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
467 if (timestamp_ids[i].id == id) {
468 name = timestamp_ids[i].name;
469 break;
470 }
471 }
472
473 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800474 printf("%-50s", name);
475 print_norm(arch_convert_raw_ts_entry(stamp));
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700476 if (prev_stamp) {
477 printf(" (");
Julius Wernera7d92442014-12-02 20:51:19 -0800478 print_norm(arch_convert_raw_ts_entry(stamp - prev_stamp));
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700479 printf(")");
480 }
481 printf("\n");
482}
483
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700484/* dump the timestamp table */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800485static void dump_timestamps(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700486{
487 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800488 struct timestamp_table *tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500489 size_t size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800490
491 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
492 fprintf(stderr, "No timestamps found in coreboot table.\n");
493 return;
494 }
495
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500496 size = sizeof(*tst_p);
497 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700498
499 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500500 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
501
502 unmap_memory();
503 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size);
504
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700505 for (i = 0; i < tst_p->num_entries; i++) {
506 const struct timestamp_entry *tse_p = tst_p->entries + i;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700507 timestamp_print_entry(tse_p->entry_id, tse_p->entry_stamp,
508 i ? tse_p[-1].entry_stamp : 0);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700509 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800510
511 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700512}
513
Stefan Reinauer19f87562013-01-07 13:37:12 -0800514/* dump the cbmem console */
515static void dump_console(void)
516{
517 void *console_p;
518 char *console_c;
519 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100520 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800521
522 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
523 fprintf(stderr, "No console found in coreboot table.\n");
524 return;
525 }
526
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500527 console_p = map_memory_size((unsigned long)console.cbmem_addr,
528 2 * sizeof(uint32_t));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800529 /* The in-memory format of the console area is:
530 * u32 size
531 * u32 cursor
532 * char console[size]
533 * Hence we have to add 8 to get to the actual console string.
534 */
Gabe Black06b13a32013-08-09 00:40:06 -0700535 size = ((uint32_t *)console_p)[0];
536 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100537 /* Cursor continues to go on even after no more data fits in
538 * the buffer but the data is dropped in this case.
539 */
540 if (size > cursor)
541 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800542 console_c = malloc(size + 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500543 unmap_memory();
Stefan Reinauer19f87562013-01-07 13:37:12 -0800544 if (!console_c) {
545 fprintf(stderr, "Not enough memory for console.\n");
546 exit(1);
547 }
548
Aaron Durbinab180d82014-03-31 11:59:58 -0500549 console_p = map_memory_size((unsigned long)console.cbmem_addr,
550 size + sizeof(size) + sizeof(cursor));
Stefan Reinauer19f87562013-01-07 13:37:12 -0800551 memcpy(console_c, console_p + 8, size);
552 console_c[size] = 0;
Gabe Black06b13a32013-08-09 00:40:06 -0700553 console_c[cursor] = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800554
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100555 printf("%s\n", console_c);
556 if (size < cursor)
557 printf("%d %s lost\n", cursor - size,
558 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800559
560 free(console_c);
561
562 unmap_memory();
563}
564
Stefan Reinauera9c83612013-07-16 17:47:35 -0700565static void hexdump(unsigned long memory, int length)
566{
567 int i;
568 uint8_t *m;
569 int all_zero = 0;
570
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500571 m = map_memory_size((intptr_t)memory, length);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700572
573 if (length > MAP_BYTES) {
574 printf("Truncating hex dump from %d to %d bytes\n\n",
575 length, MAP_BYTES);
576 length = MAP_BYTES;
577 }
578
579 for (i = 0; i < length; i += 16) {
580 int j;
581
582 all_zero++;
583 for (j = 0; j < 16; j++) {
584 if(m[i+j] != 0) {
585 all_zero = 0;
586 break;
587 }
588 }
589
590 if (all_zero < 2) {
591 printf("%08lx:", memory + i);
592 for (j = 0; j < 16; j++)
593 printf(" %02x", m[i+j]);
594 printf(" ");
595 for (j = 0; j < 16; j++)
596 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
597 printf("\n");
598 } else if (all_zero == 2) {
599 printf("...\n");
600 }
601 }
602
603 unmap_memory();
604}
605
606static void dump_cbmem_hex(void)
607{
608 if (cbmem.type != LB_MEM_TABLE) {
609 fprintf(stderr, "No coreboot CBMEM area found!\n");
610 return;
611 }
612
613 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
614}
615
616/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
617#define DYN_CBMEM_ALIGN_SIZE (4096)
618#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
Timothy Pearsondf699d52015-05-16 14:55:54 -0500619#define CBMEM_POINTER_MAGIC 0xc0389481
Stefan Reinauera9c83612013-07-16 17:47:35 -0700620#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
621
622struct cbmem_root_pointer {
623 uint32_t magic;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500624 /* Relative to upper limit/offset. */
625 int32_t root_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700626} __attribute__((packed));
627
628struct dynamic_cbmem_entry {
629 uint32_t magic;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500630 int32_t start_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700631 uint32_t size;
632 uint32_t id;
633} __attribute__((packed));
634
635struct cbmem_root {
636 uint32_t max_entries;
637 uint32_t num_entries;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500638 uint32_t flags;
639 uint32_t entry_align;
640 int32_t max_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700641 struct dynamic_cbmem_entry entries[0];
642} __attribute__((packed));
643
Stefan Reinauerc0199072013-01-07 16:26:10 -0800644#define CBMEM_MAGIC 0x434f5245
645#define MAX_CBMEM_ENTRIES 16
646
647struct cbmem_entry {
648 uint32_t magic;
649 uint32_t id;
650 uint64_t base;
651 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700652} __attribute__((packed));
653
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600654struct cbmem_id_to_name {
655 uint32_t id;
656 const char *name;
657};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700658static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800659
Stefan Reinauera9c83612013-07-16 17:47:35 -0700660void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
661{
662 int i;
663 const char *name;
664
665 name = NULL;
666 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
667 if (cbmem_ids[i].id == id) {
668 name = cbmem_ids[i].name;
669 break;
670 }
671 }
672
673 printf("%2d. ", n);
674 if (name == NULL)
675 printf("%08x ", id);
676 else
677 printf("%s", name);
678 printf(" %08" PRIx64 " ", base);
679 printf(" %08" PRIx64 "\n", size);
680}
681
682static void dump_static_cbmem_toc(struct cbmem_entry *entries)
683{
684 int i;
685
686 printf("CBMEM table of contents:\n");
687 printf(" ID START LENGTH\n");
688
689 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
690 if (entries[i].magic != CBMEM_MAGIC)
691 break;
692 cbmem_print_entry(i, entries[i].id,
693 entries[i].base, entries[i].size);
694 }
695}
696
697static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
698{
699 int i;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500700 debug("CBMEM: max_entries=%d num_entries=%d flags=0x%x, entry_align=0x%x, max_offset=%d\n\n",
701 root->max_entries, root->num_entries, root->flags, root->entry_align, root->max_offset);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700702
703 printf("CBMEM table of contents:\n");
704 printf(" ID START LENGTH\n");
705
706 for (i = 0; i < root->num_entries; i++) {
707 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
708 break;
709 cbmem_print_entry(i, root->entries[i].id,
Timothy Pearsondf699d52015-05-16 14:55:54 -0500710 rootptr + root->entries[i].start_offset, root->entries[i].size);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700711 }
712}
713
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800714static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800715{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800716 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700717 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800718 struct cbmem_entry *entries;
719
720 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700721 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800722 return;
723 }
724
725 start = unpack_lb64(cbmem.start);
726
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500727 cbmem_area = map_memory_size(start, unpack_lb64(cbmem.size));
Stefan Reinauera9c83612013-07-16 17:47:35 -0700728 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800729
Stefan Reinauera9c83612013-07-16 17:47:35 -0700730 if (entries[0].magic == CBMEM_MAGIC) {
731 dump_static_cbmem_toc(entries);
732 } else {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700733 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
734 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
735 rootptr -= sizeof(struct cbmem_root_pointer);
736 unmap_memory();
737 struct cbmem_root_pointer *r =
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500738 map_memory_size(rootptr, sizeof(*r));
Stefan Reinauera9c83612013-07-16 17:47:35 -0700739 if (r->magic == CBMEM_POINTER_MAGIC) {
740 struct cbmem_root *root;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500741 uint64_t rootaddr = rootptr + r->root_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700742 unmap_memory();
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500743 root = map_memory_size(rootaddr, ROOT_MIN_SIZE);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700744 dump_dynamic_cbmem_toc(root);
745 } else
746 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800747 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700748
Stefan Reinauerc0199072013-01-07 16:26:10 -0800749 unmap_memory();
750}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800751
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800752#define COVERAGE_MAGIC 0x584d4153
753struct file {
754 uint32_t magic;
755 uint32_t next;
756 uint32_t filename;
757 uint32_t data;
758 int offset;
759 int len;
760};
761
762static int mkpath(char *path, mode_t mode)
763{
764 assert (path && *path);
765 char *p;
766 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
767 *p = '\0';
768 if (mkdir(path, mode) == -1) {
769 if (errno != EEXIST) {
770 *p = '/';
771 return -1;
772 }
773 }
774 *p = '/';
775 }
776 return 0;
777}
778
779static void dump_coverage(void)
780{
781 int i, found = 0;
782 uint64_t start;
783 struct cbmem_entry *entries;
784 void *coverage;
785 unsigned long phys_offset;
786#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
787
788 if (cbmem.type != LB_MEM_TABLE) {
789 fprintf(stderr, "No coreboot table area found!\n");
790 return;
791 }
792
793 start = unpack_lb64(cbmem.start);
794
795 entries = (struct cbmem_entry *)map_memory(start);
796
797 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
798 if (entries[i].magic != CBMEM_MAGIC)
799 break;
800 if (entries[i].id == CBMEM_ID_COVERAGE) {
801 found = 1;
802 break;
803 }
804 }
805
806 if (!found) {
807 unmap_memory();
808 fprintf(stderr, "No coverage information found in"
809 " CBMEM area.\n");
810 return;
811 }
812
813 start = entries[i].base;
814 unmap_memory();
815 /* Map coverage area */
816 coverage = map_memory(start);
817 phys_offset = (unsigned long)coverage - (unsigned long)start;
818
819 printf("Dumping coverage data...\n");
820
821 struct file *file = (struct file *)coverage;
822 while (file && file->magic == COVERAGE_MAGIC) {
823 FILE *f;
824 char *filename;
825
826 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
827 filename = strdup((char *)phys_to_virt(file->filename));
828 if (mkpath(filename, 0755) == -1) {
829 perror("Directory for coverage data could "
830 "not be created");
831 exit(1);
832 }
833 f = fopen(filename, "wb");
834 if (!f) {
835 printf("Could not open %s: %s\n",
836 filename, strerror(errno));
837 exit(1);
838 }
839 if (fwrite((void *)phys_to_virt(file->data),
840 file->len, 1, f) != 1) {
841 printf("Could not write to %s: %s\n",
842 filename, strerror(errno));
843 exit(1);
844 }
845 fclose(f);
846 free(filename);
847
848 if (file->next)
849 file = (struct file *)phys_to_virt(file->next);
850 else
851 file = NULL;
852 }
853 unmap_memory();
854}
855
856static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800857{
858 printf("cbmem v%s -- ", CBMEM_VERSION);
859 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
860 printf(
861 "This program is free software: you can redistribute it and/or modify\n"
862 "it under the terms of the GNU General Public License as published by\n"
863 "the Free Software Foundation, version 2 of the License.\n\n"
864 "This program is distributed in the hope that it will be useful,\n"
865 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
866 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
867 "GNU General Public License for more details.\n\n"
868 "You should have received a copy of the GNU General Public License\n"
869 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
870}
871
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800872static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800873{
Stefan Reinauera9c83612013-07-16 17:47:35 -0700874 printf("usage: %s [-cCltxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800875 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800876 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800877 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800878 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700879 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800880 " -t | --timestamps: print timestamp information\n"
881 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800882 " -v | --version: print the version\n"
883 " -h | --help: print this help\n"
884 "\n");
885 exit(1);
886}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700887
Julius Werner337de4c2014-06-16 23:02:03 -0700888#ifdef __arm__
889static void dt_update_cells(const char *name, int *addr_cells_ptr,
890 int *size_cells_ptr)
891{
892 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
893 return;
894
895 int buffer;
896 size_t nlen = strlen(name);
897 char *prop = alloca(nlen + sizeof("/#address-cells"));
898 strcpy(prop, name);
899
900 if (*addr_cells_ptr < 0) {
901 strcpy(prop + nlen, "/#address-cells");
902 int fd = open(prop, O_RDONLY);
903 if (fd < 0 && errno != ENOENT) {
904 perror(prop);
905 } else if (fd >= 0) {
906 if (read(fd, &buffer, sizeof(int)) < 0)
907 perror(prop);
908 else
909 *addr_cells_ptr = ntohl(buffer);
910 close(fd);
911 }
912 }
913
914 if (*size_cells_ptr < 0) {
915 strcpy(prop + nlen, "/#size-cells");
916 int fd = open(prop, O_RDONLY);
917 if (fd < 0 && errno != ENOENT) {
918 perror(prop);
919 } else if (fd >= 0) {
920 if (read(fd, &buffer, sizeof(int)) < 0)
921 perror(prop);
922 else
923 *size_cells_ptr = ntohl(buffer);
924 close(fd);
925 }
926 }
927}
928
929static char *dt_find_compat(const char *parent, const char *compat,
930 int *addr_cells_ptr, int *size_cells_ptr)
931{
932 char *ret = NULL;
933 struct dirent *entry;
934 DIR *dir;
935
936 if (!(dir = opendir(parent))) {
937 perror(parent);
938 return NULL;
939 }
940
941 /* Loop through all files in the directory (DT node). */
942 while ((entry = readdir(dir))) {
943 /* We only care about compatible props or subnodes. */
944 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
945 !strcmp(entry->d_name, "compatible")))
946 continue;
947
948 /* Assemble the file name (on the stack, for speed). */
949 size_t plen = strlen(parent);
950 char *name = alloca(plen + strlen(entry->d_name) + 2);
951
952 strcpy(name, parent);
953 name[plen] = '/';
954 strcpy(name + plen + 1, entry->d_name);
955
956 /* If it's a subnode, recurse. */
957 if (entry->d_type & DT_DIR) {
958 ret = dt_find_compat(name, compat, addr_cells_ptr,
959 size_cells_ptr);
960
961 /* There is only one matching node to find, abort. */
962 if (ret) {
963 /* Gather cells values on the way up. */
964 dt_update_cells(parent, addr_cells_ptr,
965 size_cells_ptr);
966 break;
967 }
968 continue;
969 }
970
971 /* If it's a compatible string, see if it's the right one. */
972 int fd = open(name, O_RDONLY);
973 int clen = strlen(compat);
974 char *buffer = alloca(clen + 1);
975
976 if (fd < 0) {
977 perror(name);
978 continue;
979 }
980
981 if (read(fd, buffer, clen + 1) < 0) {
982 perror(name);
983 close(fd);
984 continue;
985 }
986 close(fd);
987
988 if (!strcmp(compat, buffer)) {
989 /* Initialize these to "unset" for the way up. */
990 *addr_cells_ptr = *size_cells_ptr = -1;
991
992 /* Can't leave string on the stack or we'll lose it! */
993 ret = strdup(parent);
994 break;
995 }
996 }
997
998 closedir(dir);
999 return ret;
1000}
1001#endif /* __arm__ */
1002
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001003int main(int argc, char** argv)
1004{
Stefan Reinauer19f87562013-01-07 13:37:12 -08001005 int print_defaults = 1;
1006 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001007 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001008 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001009 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001010 int print_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001011
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001012 int opt, option_index = 0;
1013 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001014 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001015 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -08001016 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -08001017 {"timestamps", 0, 0, 't'},
Stefan Reinauera9c83612013-07-16 17:47:35 -07001018 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001019 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001020 {"version", 0, 0, 'v'},
1021 {"help", 0, 0, 'h'},
1022 {0, 0, 0, 0}
1023 };
Stefan Reinauera9c83612013-07-16 17:47:35 -07001024 while ((opt = getopt_long(argc, argv, "cCltxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001025 long_options, &option_index)) != EOF) {
1026 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001027 case 'c':
1028 print_console = 1;
1029 print_defaults = 0;
1030 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001031 case 'C':
1032 print_coverage = 1;
1033 print_defaults = 0;
1034 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001035 case 'l':
1036 print_list = 1;
1037 print_defaults = 0;
1038 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001039 case 'x':
1040 print_hexdump = 1;
1041 print_defaults = 0;
1042 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001043 case 't':
1044 print_timestamps = 1;
1045 print_defaults = 0;
1046 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001047 case 'V':
1048 verbose = 1;
1049 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001050 case 'v':
1051 print_version();
1052 exit(0);
1053 break;
1054 case 'h':
1055 case '?':
1056 default:
1057 print_usage(argv[0]);
1058 exit(0);
1059 break;
1060 }
1061 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001062
Julius Werner337de4c2014-06-16 23:02:03 -07001063 mem_fd = open("/dev/mem", O_RDONLY, 0);
1064 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001065 fprintf(stderr, "Failed to gain memory access: %s\n",
1066 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001067 return 1;
1068 }
1069
Stefan Reinauer7f681502013-06-19 15:39:09 -07001070#ifdef __arm__
Julius Werner337de4c2014-06-16 23:02:03 -07001071 int addr_cells, size_cells;
1072 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1073 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001074
Julius Werner337de4c2014-06-16 23:02:03 -07001075 if (!coreboot_node) {
1076 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001077 return 1;
1078 }
1079
Julius Werner337de4c2014-06-16 23:02:03 -07001080 if (addr_cells < 0) {
1081 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1082 addr_cells = 1;
1083 }
1084
1085 int nlen = strlen(coreboot_node);
1086 char *reg = alloca(nlen + sizeof("/reg"));
1087
1088 strcpy(reg, coreboot_node);
1089 strcpy(reg + nlen, "/reg");
1090 free(coreboot_node);
1091
1092 int fd = open(reg, O_RDONLY);
1093 if (fd < 0) {
1094 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001095 return 1;
1096 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001097
Julius Werner337de4c2014-06-16 23:02:03 -07001098 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001099 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1100 u8 *dtbuffer = alloca(size_to_read);
1101 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001102 perror(reg);
1103 return 1;
1104 }
1105 close(fd);
1106
1107 /* No variable-length byte swap function anywhere in C... how sad. */
1108 u64 baseaddr = 0;
1109 for (i = 0; i < addr_cells * 4; i++) {
1110 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001111 baseaddr |= *dtbuffer;
1112 dtbuffer++;
1113 }
1114 u64 cb_table_size = 0;
1115 for (i = 0; i < size_cells * 4; i++) {
1116 cb_table_size <<= 8;
1117 cb_table_size |= *dtbuffer;
1118 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001119 }
1120
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001121 parse_cbtable(baseaddr, cb_table_size);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001122#else
1123 int j;
1124 static const int possible_base_addresses[] = { 0, 0xf0000 };
1125
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001126 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001127 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001128 if (parse_cbtable(possible_base_addresses[j], MAP_BYTES))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001129 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001130 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001131#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001132
Stefan Reinauer19f87562013-01-07 13:37:12 -08001133 if (print_console)
1134 dump_console();
1135
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001136 if (print_coverage)
1137 dump_coverage();
1138
Stefan Reinauerc0199072013-01-07 16:26:10 -08001139 if (print_list)
1140 dump_cbmem_toc();
1141
Stefan Reinauera9c83612013-07-16 17:47:35 -07001142 if (print_hexdump)
1143 dump_cbmem_hex();
1144
Stefan Reinauer19f87562013-01-07 13:37:12 -08001145 if (print_defaults || print_timestamps)
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001146 dump_timestamps();
1147
Julius Werner337de4c2014-06-16 23:02:03 -07001148 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001149 return 0;
1150}