blob: 69ffbaf4e375205019ad9fbb96515086aabe4fb1 [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.
Timothy Pearsonbea71402015-09-05 18:07:17 -05005 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070019 */
20
Nico Huber8e4bb9282013-05-26 18:17:54 +020021#include <inttypes.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080025#include <unistd.h>
Stefan Reinauer8c594772013-04-19 14:22:29 -070026#include <inttypes.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080027#include <getopt.h>
Julius Werner337de4c2014-06-16 23:02:03 -070028#include <dirent.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080029#include <errno.h>
30#include <fcntl.h>
Stefan Reinauera9c83612013-07-16 17:47:35 -070031#include <ctype.h>
Stefan Reinauer7f681502013-06-19 15:39:09 -070032#include <arpa/inet.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080033#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/mman.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080036#include <libgen.h>
37#include <assert.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050038#include <commonlib/cbmem_id.h>
39#include <commonlib/timestamp_serialized.h>
40#include <commonlib/coreboot_tables.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070041
Patrick Georgid00f1802015-07-13 16:53:50 +020042#ifdef __OpenBSD__
43#include <sys/param.h>
44#include <sys/sysctl.h>
45#endif
46
Stefan Reinauer05cbce62013-01-03 14:30:33 -080047#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
48#define MAP_BYTES (1024*1024)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070049
Julius Werner337de4c2014-06-16 23:02:03 -070050typedef uint8_t u8;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070051typedef uint16_t u16;
52typedef uint32_t u32;
53typedef uint64_t u64;
54
Stefan Reinauera9c83612013-07-16 17:47:35 -070055#define CBMEM_VERSION "1.1"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080056
Stefan Reinauer05cbce62013-01-03 14:30:33 -080057/* verbose output? */
58static int verbose = 0;
59#define debug(x...) if(verbose) printf(x)
60
61/* File handle used to access /dev/mem */
Julius Werner337de4c2014-06-16 23:02:03 -070062static int mem_fd;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070063
Timothy Pearsondf699d52015-05-16 14:55:54 -050064/* IMD root pointer location */
65static uint64_t rootptr = 0;
66
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070067/*
68 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
69 * the buffer length is odd last byte is excluded from the calculation
70 */
71static u16 ipchcksum(const void *addr, unsigned size)
72{
73 const u16 *p = addr;
74 unsigned i, n = size / 2; /* don't expect odd sized blocks */
75 u32 sum = 0;
76
77 for (i = 0; i < n; i++)
78 sum += p[i];
79
80 sum = (sum >> 16) + (sum & 0xffff);
81 sum += (sum >> 16);
82 sum = ~sum & 0xffff;
83 return (u16) sum;
84}
85
86/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080087 * Functions to map / unmap physical memory into virtual address space. These
88 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070089 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080090static void *mapped_virtual;
Aaron Durbinab180d82014-03-31 11:59:58 -050091static size_t mapped_size;
92
93static inline size_t size_to_mib(size_t sz)
94{
95 return sz >> 20;
96}
97
98static void unmap_memory(void)
99{
100 if (mapped_virtual == NULL) {
101 fprintf(stderr, "Error unmapping memory\n");
102 return;
103 }
Timothy Pearsondf699d52015-05-16 14:55:54 -0500104 if (size_to_mib(mapped_size) == 0) {
105 debug("Unmapping %zuMB of virtual memory at %p.\n",
106 size_to_mib(mapped_size), mapped_virtual);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500107 } else {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500108 debug("Unmapping %zuMB of virtual memory at %p.\n",
109 size_to_mib(mapped_size), mapped_virtual);
110 }
Aaron Durbinab180d82014-03-31 11:59:58 -0500111 munmap(mapped_virtual, mapped_size);
112 mapped_virtual = NULL;
113 mapped_size = 0;
114}
115
Timothy Pearsonbea71402015-09-05 18:07:17 -0500116static void *map_memory_size(u64 physical, size_t size, uint8_t abort_on_failure)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700117{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800118 void *v;
119 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -0700120 u64 page = getpagesize();
Aaron Durbinab180d82014-03-31 11:59:58 -0500121 size_t padding;
122
123 if (mapped_virtual != NULL)
124 unmap_memory();
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800125
126 /* Mapped memory must be aligned to page size */
127 p = physical & ~(page - 1);
Aaron Durbinab180d82014-03-31 11:59:58 -0500128 padding = physical & (page-1);
129 size += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800130
Timothy Pearsondf699d52015-05-16 14:55:54 -0500131 if (size_to_mib(size) == 0) {
132 debug("Mapping %zuB of physical memory at 0x%jx (requested 0x%jx).\n",
133 size, (intmax_t)p, (intmax_t)physical);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500134 } else {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500135 debug("Mapping %zuMB of physical memory at 0x%jx (requested 0x%jx).\n",
136 size_to_mib(size), (intmax_t)p, (intmax_t)physical);
137 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800138
Julius Werner337de4c2014-06-16 23:02:03 -0700139 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800140
141 if (v == MAP_FAILED) {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500142 /* The mapped area may have overrun the upper cbmem boundary when trying to
143 * align to the page size. Try growing down instead of up...
144 */
145 p -= page;
146 padding += page;
147 size &= ~(page - 1);
148 size = size + (page - 1);
149 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
150 debug(" ... failed. Mapping %zuB of physical memory at 0x%jx.\n",
151 size, (intmax_t)p);
152 }
153
154 if (v == MAP_FAILED) {
Timothy Pearsonbea71402015-09-05 18:07:17 -0500155 if (abort_on_failure) {
156 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
157 strerror(errno));
158 exit(1);
159 } else {
160 return 0;
161 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700162 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800163
164 /* Remember what we actually mapped ... */
165 mapped_virtual = v;
Aaron Durbinab180d82014-03-31 11:59:58 -0500166 mapped_size = size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800167
168 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700169 if (padding)
Aaron Durbinab180d82014-03-31 11:59:58 -0500170 debug(" ... padding virtual address with 0x%zx bytes.\n",
Stefan Reinauera9c83612013-07-16 17:47:35 -0700171 padding);
172 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800173
174 return v;
175}
176
Aaron Durbinab180d82014-03-31 11:59:58 -0500177static void *map_memory(u64 physical)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800178{
Timothy Pearsonbea71402015-09-05 18:07:17 -0500179 return map_memory_size(physical, MAP_BYTES, 1);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700180}
181
182/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800183 * Try finding the timestamp table and coreboot cbmem console starting from the
184 * passed in memory offset. Could be called recursively in case a forwarding
185 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700186 *
187 * Returns pointer to a memory buffer containg the timestamp table or zero if
188 * none found.
189 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800190
191static struct lb_cbmem_ref timestamps;
192static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800193static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800194
Stefan Reinauer8c594772013-04-19 14:22:29 -0700195/* This is a work-around for a nasty problem introduced by initially having
196 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
197 * on 64bit x86 systems because coreboot is 32bit on those systems.
198 * When the problem was found, it was corrected, but there are a lot of
199 * systems out there with a firmware that does not produce the right
200 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
201 */
202static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
203{
204 struct lb_cbmem_ref ret;
205
206 ret = *cbmem_ref;
207
208 if (cbmem_ref->size < sizeof(*cbmem_ref))
209 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
210
Stefan Reinauera9c83612013-07-16 17:47:35 -0700211 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
212
Stefan Reinauer8c594772013-04-19 14:22:29 -0700213 return ret;
214}
215
Timothy Pearsonbea71402015-09-05 18:07:17 -0500216static int parse_cbtable(u64 address, size_t table_size, uint8_t abort_on_failure)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700217{
Timothy Pearsonbea71402015-09-05 18:07:17 -0500218 int i, found, ret = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800219 void *buf;
220
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500221 debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n",
222 address, table_size);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500223 buf = map_memory_size(address, table_size, abort_on_failure);
224 if (!buf)
225 return -2;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700226
227 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800228
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700229 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800230 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700231 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800232 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700233 int j;
234
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800235 lbh = (struct lb_header *)(buf + i);
236 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
237 !lbh->header_bytes ||
238 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700239 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700240 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800241 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700242
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800243 if (ipchcksum(lbtable, lbh->table_bytes) !=
244 lbh->table_checksum) {
245 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700246 continue;
247 }
248
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800249 found = 1;
250 debug("Found!\n");
251
252 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800253 lbr_p = (struct lb_record*) ((char *)lbtable + j);
254 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700255 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800256 case LB_TAG_MEMORY: {
257 int i = 0;
258 debug(" Found memory map.\n");
259 struct lb_memory *memory =
260 (struct lb_memory *)lbr_p;
Paul Menzel747c07f2014-10-17 13:46:12 +0200261 while ((char *)&memory->map[i] < ((char *)lbr_p
Stefan Reinauerc0199072013-01-07 16:26:10 -0800262 + lbr_p->size)) {
263 if (memory->map[i].type == LB_MEM_TABLE) {
264 debug(" LB_MEM_TABLE found.\n");
265 /* The last one found is CBMEM */
266 cbmem = memory->map[i];
267 }
268 i++;
269 }
270 continue;
271 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700272 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800273 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700274 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800275 continue;
276 }
277 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800278 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700279 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800280 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700281 }
282 case LB_TAG_FORWARD: {
283 /*
284 * This is a forwarding entry - repeat the
285 * search at the new address.
286 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800287 struct lb_forward lbf_p =
288 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800289 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800290 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500291 ret = parse_cbtable(lbf_p.forward, table_size, 0);
292 if (ret == -2) {
293 /* try again with a smaller memory mapping request */
294 ret = parse_cbtable(lbf_p.forward, table_size / 2, 1);
295 if (ret == -2)
296 exit(1);
297 else
298 return ret;
299 } else {
300 return ret;
301 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700302 }
303 default:
304 break;
305 }
306
307 }
308 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800309 unmap_memory();
310
311 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700312}
313
Patrick Georgid00f1802015-07-13 16:53:50 +0200314#if defined(linux) && (defined(__i386__) || defined(__x86_64__))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700315/*
316 * read CPU frequency from a sysfs file, return an frequency in Kilohertz as
317 * an int or exit on any error.
318 */
Aaron Durbinc49014e2015-08-30 21:19:55 -0500319static unsigned long arch_tick_frequency(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700320{
321 FILE *cpuf;
322 char freqs[100];
323 int size;
324 char *endp;
325 u64 rv;
326
327 const char* freq_file =
328 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
329
330 cpuf = fopen(freq_file, "r");
331 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800332 fprintf(stderr, "Could not open %s: %s\n",
333 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700334 exit(1);
335 }
336
337 memset(freqs, 0, sizeof(freqs));
338 size = fread(freqs, 1, sizeof(freqs), cpuf);
339 if (!size || (size == sizeof(freqs))) {
340 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
341 size, freq_file);
342 exit(1);
343 }
344 fclose(cpuf);
345 rv = strtoull(freqs, &endp, 10);
346
347 if (*endp == '\0' || *endp == '\n')
348 return rv;
349 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
350 freqs, freq_file);
351 exit(1);
352}
Patrick Georgid00f1802015-07-13 16:53:50 +0200353#elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__))
Aaron Durbinc49014e2015-08-30 21:19:55 -0500354static unsigned long arch_tick_frequency(void)
Patrick Georgid00f1802015-07-13 16:53:50 +0200355{
356 int mib[2] = { CTL_HW, HW_CPUSPEED };
357 static int value = 0;
358 size_t value_len = sizeof(value);
359
Aaron Durbinc49014e2015-08-30 21:19:55 -0500360 /* Return 1 MHz when sysctl fails. */
Patrick Georgid00f1802015-07-13 16:53:50 +0200361 if ((value == 0) && (sysctl(mib, 2, &value, &value_len, NULL, 0) == -1))
Aaron Durbinc49014e2015-08-30 21:19:55 -0500362 return 1;
Patrick Georgid00f1802015-07-13 16:53:50 +0200363
Aaron Durbinc49014e2015-08-30 21:19:55 -0500364 return value;
Patrick Georgid00f1802015-07-13 16:53:50 +0200365}
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700366#else
Aaron Durbinc49014e2015-08-30 21:19:55 -0500367static unsigned long arch_tick_frequency(void)
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700368{
Aaron Durbinc49014e2015-08-30 21:19:55 -0500369 /* 1 MHz = 1us. */
370 return 1;
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700371}
372#endif
373
Aaron Durbinc49014e2015-08-30 21:19:55 -0500374static unsigned long tick_freq_mhz;
375
376static void timestamp_set_tick_freq(unsigned long table_tick_freq_mhz)
377{
378 tick_freq_mhz = table_tick_freq_mhz;
379
380 /* Honor table frequency. */
381 if (tick_freq_mhz)
382 return;
383
384 tick_freq_mhz = arch_tick_frequency();
385
386 if (!tick_freq_mhz) {
387 fprintf(stderr, "Cannot determine timestamp tick frequency.\n");
388 exit(1);
389 }
390}
391
392u64 arch_convert_raw_ts_entry(u64 ts)
393{
394 return ts / tick_freq_mhz;
395}
396
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700397/*
398 * Print an integer in 'normalized' form - with commas separating every three
Julius Wernera7d92442014-12-02 20:51:19 -0800399 * decimal orders.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700400 */
Julius Wernera7d92442014-12-02 20:51:19 -0800401static void print_norm(u64 v)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700402{
Julius Wernera7d92442014-12-02 20:51:19 -0800403 if (v >= 1000) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700404 /* print the higher order sections first */
Julius Wernera7d92442014-12-02 20:51:19 -0800405 print_norm(v / 1000);
406 printf(",%3.3u", (u32)(v % 1000));
407 } else {
408 printf("%u", (u32)(v % 1000));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700409 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700410}
411
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700412enum additional_timestamp_id {
413 // Depthcharge entry IDs start at 1000.
414 TS_DC_START = 1000,
415
416 TS_RO_PARAMS_INIT = 1001,
417 TS_RO_VB_INIT = 1002,
418 TS_RO_VB_SELECT_FIRMWARE = 1003,
419 TS_RO_VB_SELECT_AND_LOAD_KERNEL = 1004,
420
421 TS_RW_VB_SELECT_AND_LOAD_KERNEL = 1010,
422
423 TS_VB_SELECT_AND_LOAD_KERNEL = 1020,
424
425 TS_CROSSYSTEM_DATA = 1100,
426 TS_START_KERNEL = 1101
427};
428
429static const struct timestamp_id_to_name {
430 u32 id;
431 const char *name;
432} timestamp_ids[] = {
Aaron Durbin31540fb2015-07-11 12:44:10 -0500433 /* Marker to report base_time. */
434 { 0, "1st timestamp" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700435 { TS_START_ROMSTAGE, "start of rom stage" },
436 { TS_BEFORE_INITRAM, "before ram initialization" },
437 { TS_AFTER_INITRAM, "after ram initialization" },
438 { TS_END_ROMSTAGE, "end of romstage" },
439 { TS_START_VBOOT, "start of verified boot" },
440 { TS_END_VBOOT, "end of verified boot" },
Julius Wernera7d92442014-12-02 20:51:19 -0800441 { TS_START_COPYRAM, "starting to load ramstage" },
442 { TS_END_COPYRAM, "finished loading ramstage" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700443 { TS_START_RAMSTAGE, "start of ramstage" },
Julius Wernera7d92442014-12-02 20:51:19 -0800444 { TS_START_BOOTBLOCK, "start of bootblock" },
445 { TS_END_BOOTBLOCK, "end of bootblock" },
446 { TS_START_COPYROM, "starting to load romstage" },
447 { TS_END_COPYROM, "finished loading romstage" },
448 { TS_START_ULZMA, "starting LZMA decompress (ignore for x86)" },
449 { TS_END_ULZMA, "finished LZMA decompress (ignore for x86)" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700450 { TS_DEVICE_ENUMERATE, "device enumeration" },
451 { TS_DEVICE_CONFIGURE, "device configuration" },
452 { TS_DEVICE_ENABLE, "device enable" },
453 { TS_DEVICE_INITIALIZE, "device initialization" },
454 { TS_DEVICE_DONE, "device setup done" },
455 { TS_CBMEM_POST, "cbmem post" },
456 { TS_WRITE_TABLES, "write tables" },
457 { TS_LOAD_PAYLOAD, "load payload" },
458 { TS_ACPI_WAKE_JUMP, "ACPI wake jump" },
459 { TS_SELFBOOT_JUMP, "selfboot jump" },
Julius Wernera7d92442014-12-02 20:51:19 -0800460
461 { TS_START_COPYVER, "starting to load verstage" },
462 { TS_END_COPYVER, "finished loading verstage" },
463 { TS_START_TPMINIT, "starting to initialize TPM" },
464 { TS_END_TPMINIT, "finished TPM initialization" },
465 { TS_START_VERIFY_SLOT, "starting to verify keyblock/preamble (RSA)" },
466 { TS_END_VERIFY_SLOT, "finished verifying keyblock/preamble (RSA)" },
467 { TS_START_HASH_BODY, "starting to verify body (load+SHA2+RSA) " },
468 { TS_DONE_LOADING, "finished loading body (ignore for x86)" },
469 { TS_DONE_HASHING, "finished calculating body hash (SHA2)" },
470 { TS_END_HASH_BODY, "finished verifying body signature (RSA)" },
471
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700472 { TS_DC_START, "depthcharge start" },
473 { TS_RO_PARAMS_INIT, "RO parameter init" },
474 { TS_RO_VB_INIT, "RO vboot init" },
475 { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" },
476 { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" },
477 { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" },
478 { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" },
479 { TS_CROSSYSTEM_DATA, "crossystem data" },
Lee Leahyb8179082015-02-24 11:30:38 -0800480 { TS_START_KERNEL, "start kernel" },
481
482 /* FSP related timestamps */
483 { TS_FSP_MEMORY_INIT_START, "calling FspMemoryInit" },
484 { TS_FSP_MEMORY_INIT_END, "returning from FspMemoryInit" },
485 { TS_FSP_TEMP_RAM_EXIT_START, "calling FspTempRamExit" },
486 { TS_FSP_TEMP_RAM_EXIT_END, "returning from FspTempRamExit" },
487 { TS_FSP_SILICON_INIT_START, "calling FspSiliconInit" },
488 { TS_FSP_SILICON_INIT_END, "returning from FspSiliconInit" },
489 { TS_FSP_BEFORE_ENUMERATE, "calling FspNotify(AfterPciEnumeration)" },
490 { TS_FSP_AFTER_ENUMERATE,
491 "returning from FspNotify(AfterPciEnumeration)" },
492 { TS_FSP_BEFORE_FINALIZE, "calling FspNotify(ReadyToBoot)" },
493 { TS_FSP_AFTER_FINALIZE, "returning from FspNotify(ReadyToBoot)" }
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700494};
495
Aaron Durbinfbff3012015-08-30 22:00:12 -0500496static const char *timestamp_name(uint32_t id)
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700497{
498 int i;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500499
500 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
501 if (timestamp_ids[i].id == id)
502 return timestamp_ids[i].name;
503 }
504 return "<unknown>";
505}
506
507static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp,
508 uint64_t prev_stamp)
509{
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700510 const char *name;
Aaron Durbin799bf782015-08-06 13:52:08 -0500511 uint64_t step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700512
Aaron Durbinfbff3012015-08-30 22:00:12 -0500513 name = timestamp_name(id);
514
515 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
516
517 /* ID<tab>absolute time<tab>relative time<tab>description */
518 printf("%d\t", id);
519 printf("%llu\t", (long long)arch_convert_raw_ts_entry(stamp));
520 printf("%llu\t", (long long)step_time);
521 printf("%s\n", name);
522
523 return step_time;
524}
525
526uint64_t timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
527{
528 const char *name;
529 uint64_t step_time;
530
531 name = timestamp_name(id);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700532
533 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800534 printf("%-50s", name);
535 print_norm(arch_convert_raw_ts_entry(stamp));
Aaron Durbin799bf782015-08-06 13:52:08 -0500536 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700537 if (prev_stamp) {
538 printf(" (");
Aaron Durbin799bf782015-08-06 13:52:08 -0500539 print_norm(step_time);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700540 printf(")");
541 }
542 printf("\n");
Aaron Durbin799bf782015-08-06 13:52:08 -0500543
544 return step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700545}
546
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700547/* dump the timestamp table */
Aaron Durbinfbff3012015-08-30 22:00:12 -0500548static void dump_timestamps(int mach_readable)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700549{
550 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800551 struct timestamp_table *tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500552 size_t size;
Aaron Durbin31540fb2015-07-11 12:44:10 -0500553 uint64_t prev_stamp;
Aaron Durbin799bf782015-08-06 13:52:08 -0500554 uint64_t total_time;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800555
556 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
557 fprintf(stderr, "No timestamps found in coreboot table.\n");
558 return;
559 }
560
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500561 size = sizeof(*tst_p);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500562 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700563
Aaron Durbinc49014e2015-08-30 21:19:55 -0500564 timestamp_set_tick_freq(tst_p->tick_freq_mhz);
565
Aaron Durbinfbff3012015-08-30 22:00:12 -0500566 if (!mach_readable)
567 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500568 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
569
570 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500571 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500572
Aaron Durbin31540fb2015-07-11 12:44:10 -0500573 /* Report the base time within the table. */
574 prev_stamp = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500575 if (mach_readable)
576 timestamp_print_parseable_entry(0, tst_p->base_time,
577 prev_stamp);
578 else
579 timestamp_print_entry(0, tst_p->base_time, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500580 prev_stamp = tst_p->base_time;
581
Aaron Durbin799bf782015-08-06 13:52:08 -0500582 total_time = 0;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700583 for (i = 0; i < tst_p->num_entries; i++) {
Aaron Durbin31540fb2015-07-11 12:44:10 -0500584 uint64_t stamp;
585 const struct timestamp_entry *tse = &tst_p->entries[i];
586
587 /* Make all timestamps absolute. */
588 stamp = tse->entry_stamp + tst_p->base_time;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500589 if (mach_readable)
590 total_time +=
591 timestamp_print_parseable_entry(tse->entry_id,
592 stamp, prev_stamp);
593 else
594 total_time += timestamp_print_entry(tse->entry_id,
Aaron Durbin799bf782015-08-06 13:52:08 -0500595 stamp, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500596 prev_stamp = stamp;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700597 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800598
Aaron Durbinfbff3012015-08-30 22:00:12 -0500599 if (!mach_readable) {
600 printf("\nTotal Time: ");
601 print_norm(total_time);
602 printf("\n");
603 }
Aaron Durbin799bf782015-08-06 13:52:08 -0500604
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800605 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700606}
607
Stefan Reinauer19f87562013-01-07 13:37:12 -0800608/* dump the cbmem console */
609static void dump_console(void)
610{
611 void *console_p;
612 char *console_c;
613 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100614 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800615
616 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
617 fprintf(stderr, "No console found in coreboot table.\n");
618 return;
619 }
620
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500621 console_p = map_memory_size((unsigned long)console.cbmem_addr,
Timothy Pearsonbea71402015-09-05 18:07:17 -0500622 2 * sizeof(uint32_t), 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800623 /* The in-memory format of the console area is:
624 * u32 size
625 * u32 cursor
626 * char console[size]
627 * Hence we have to add 8 to get to the actual console string.
628 */
Gabe Black06b13a32013-08-09 00:40:06 -0700629 size = ((uint32_t *)console_p)[0];
630 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100631 /* Cursor continues to go on even after no more data fits in
632 * the buffer but the data is dropped in this case.
633 */
634 if (size > cursor)
635 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800636 console_c = malloc(size + 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500637 unmap_memory();
Stefan Reinauer19f87562013-01-07 13:37:12 -0800638 if (!console_c) {
639 fprintf(stderr, "Not enough memory for console.\n");
640 exit(1);
641 }
642
Aaron Durbinab180d82014-03-31 11:59:58 -0500643 console_p = map_memory_size((unsigned long)console.cbmem_addr,
Timothy Pearsonbea71402015-09-05 18:07:17 -0500644 size + sizeof(size) + sizeof(cursor), 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800645 memcpy(console_c, console_p + 8, size);
646 console_c[size] = 0;
Gabe Black06b13a32013-08-09 00:40:06 -0700647 console_c[cursor] = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800648
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100649 printf("%s\n", console_c);
650 if (size < cursor)
651 printf("%d %s lost\n", cursor - size,
652 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800653
654 free(console_c);
655
656 unmap_memory();
657}
658
Stefan Reinauera9c83612013-07-16 17:47:35 -0700659static void hexdump(unsigned long memory, int length)
660{
661 int i;
662 uint8_t *m;
663 int all_zero = 0;
664
Timothy Pearsonbea71402015-09-05 18:07:17 -0500665 m = map_memory_size((intptr_t)memory, length, 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700666
667 if (length > MAP_BYTES) {
668 printf("Truncating hex dump from %d to %d bytes\n\n",
669 length, MAP_BYTES);
670 length = MAP_BYTES;
671 }
672
673 for (i = 0; i < length; i += 16) {
674 int j;
675
676 all_zero++;
677 for (j = 0; j < 16; j++) {
678 if(m[i+j] != 0) {
679 all_zero = 0;
680 break;
681 }
682 }
683
684 if (all_zero < 2) {
685 printf("%08lx:", memory + i);
686 for (j = 0; j < 16; j++)
687 printf(" %02x", m[i+j]);
688 printf(" ");
689 for (j = 0; j < 16; j++)
690 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
691 printf("\n");
692 } else if (all_zero == 2) {
693 printf("...\n");
694 }
695 }
696
697 unmap_memory();
698}
699
700static void dump_cbmem_hex(void)
701{
702 if (cbmem.type != LB_MEM_TABLE) {
703 fprintf(stderr, "No coreboot CBMEM area found!\n");
704 return;
705 }
706
707 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
708}
709
710/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
711#define DYN_CBMEM_ALIGN_SIZE (4096)
712#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
Timothy Pearsondf699d52015-05-16 14:55:54 -0500713#define CBMEM_POINTER_MAGIC 0xc0389481
Stefan Reinauera9c83612013-07-16 17:47:35 -0700714#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
715
716struct cbmem_root_pointer {
717 uint32_t magic;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500718 /* Relative to upper limit/offset. */
719 int32_t root_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700720} __attribute__((packed));
721
722struct dynamic_cbmem_entry {
723 uint32_t magic;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500724 int32_t start_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700725 uint32_t size;
726 uint32_t id;
727} __attribute__((packed));
728
729struct cbmem_root {
730 uint32_t max_entries;
731 uint32_t num_entries;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500732 uint32_t flags;
733 uint32_t entry_align;
734 int32_t max_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700735 struct dynamic_cbmem_entry entries[0];
736} __attribute__((packed));
737
Stefan Reinauerc0199072013-01-07 16:26:10 -0800738#define CBMEM_MAGIC 0x434f5245
739#define MAX_CBMEM_ENTRIES 16
740
741struct cbmem_entry {
742 uint32_t magic;
743 uint32_t id;
744 uint64_t base;
745 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700746} __attribute__((packed));
747
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600748struct cbmem_id_to_name {
749 uint32_t id;
750 const char *name;
751};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700752static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800753
Stefan Reinauera9c83612013-07-16 17:47:35 -0700754void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
755{
756 int i;
757 const char *name;
758
759 name = NULL;
760 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
761 if (cbmem_ids[i].id == id) {
762 name = cbmem_ids[i].name;
763 break;
764 }
765 }
766
767 printf("%2d. ", n);
768 if (name == NULL)
769 printf("%08x ", id);
770 else
771 printf("%s", name);
772 printf(" %08" PRIx64 " ", base);
773 printf(" %08" PRIx64 "\n", size);
774}
775
776static void dump_static_cbmem_toc(struct cbmem_entry *entries)
777{
778 int i;
779
780 printf("CBMEM table of contents:\n");
781 printf(" ID START LENGTH\n");
782
783 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
784 if (entries[i].magic != CBMEM_MAGIC)
785 break;
786 cbmem_print_entry(i, entries[i].id,
787 entries[i].base, entries[i].size);
788 }
789}
790
791static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
792{
793 int i;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500794 debug("CBMEM: max_entries=%d num_entries=%d flags=0x%x, entry_align=0x%x, max_offset=%d\n\n",
795 root->max_entries, root->num_entries, root->flags, root->entry_align, root->max_offset);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700796
797 printf("CBMEM table of contents:\n");
798 printf(" ID START LENGTH\n");
799
800 for (i = 0; i < root->num_entries; i++) {
801 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
802 break;
803 cbmem_print_entry(i, root->entries[i].id,
Timothy Pearsondf699d52015-05-16 14:55:54 -0500804 rootptr + root->entries[i].start_offset, root->entries[i].size);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700805 }
806}
807
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800808static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800809{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800810 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700811 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800812 struct cbmem_entry *entries;
813
814 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700815 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800816 return;
817 }
818
819 start = unpack_lb64(cbmem.start);
820
Timothy Pearsonbea71402015-09-05 18:07:17 -0500821 cbmem_area = map_memory_size(start, unpack_lb64(cbmem.size), 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700822 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800823
Stefan Reinauera9c83612013-07-16 17:47:35 -0700824 if (entries[0].magic == CBMEM_MAGIC) {
825 dump_static_cbmem_toc(entries);
826 } else {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700827 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
828 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
829 rootptr -= sizeof(struct cbmem_root_pointer);
830 unmap_memory();
831 struct cbmem_root_pointer *r =
Timothy Pearsonbea71402015-09-05 18:07:17 -0500832 map_memory_size(rootptr, sizeof(*r), 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700833 if (r->magic == CBMEM_POINTER_MAGIC) {
834 struct cbmem_root *root;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500835 uint64_t rootaddr = rootptr + r->root_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700836 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500837 root = map_memory_size(rootaddr, ROOT_MIN_SIZE, 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700838 dump_dynamic_cbmem_toc(root);
839 } else
840 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800841 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700842
Stefan Reinauerc0199072013-01-07 16:26:10 -0800843 unmap_memory();
844}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800845
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800846#define COVERAGE_MAGIC 0x584d4153
847struct file {
848 uint32_t magic;
849 uint32_t next;
850 uint32_t filename;
851 uint32_t data;
852 int offset;
853 int len;
854};
855
856static int mkpath(char *path, mode_t mode)
857{
858 assert (path && *path);
859 char *p;
860 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
861 *p = '\0';
862 if (mkdir(path, mode) == -1) {
863 if (errno != EEXIST) {
864 *p = '/';
865 return -1;
866 }
867 }
868 *p = '/';
869 }
870 return 0;
871}
872
873static void dump_coverage(void)
874{
875 int i, found = 0;
876 uint64_t start;
877 struct cbmem_entry *entries;
878 void *coverage;
879 unsigned long phys_offset;
880#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
881
882 if (cbmem.type != LB_MEM_TABLE) {
883 fprintf(stderr, "No coreboot table area found!\n");
884 return;
885 }
886
887 start = unpack_lb64(cbmem.start);
888
889 entries = (struct cbmem_entry *)map_memory(start);
890
891 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
892 if (entries[i].magic != CBMEM_MAGIC)
893 break;
894 if (entries[i].id == CBMEM_ID_COVERAGE) {
895 found = 1;
896 break;
897 }
898 }
899
900 if (!found) {
901 unmap_memory();
902 fprintf(stderr, "No coverage information found in"
903 " CBMEM area.\n");
904 return;
905 }
906
907 start = entries[i].base;
908 unmap_memory();
909 /* Map coverage area */
910 coverage = map_memory(start);
911 phys_offset = (unsigned long)coverage - (unsigned long)start;
912
913 printf("Dumping coverage data...\n");
914
915 struct file *file = (struct file *)coverage;
916 while (file && file->magic == COVERAGE_MAGIC) {
917 FILE *f;
918 char *filename;
919
920 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
921 filename = strdup((char *)phys_to_virt(file->filename));
922 if (mkpath(filename, 0755) == -1) {
923 perror("Directory for coverage data could "
924 "not be created");
925 exit(1);
926 }
927 f = fopen(filename, "wb");
928 if (!f) {
929 printf("Could not open %s: %s\n",
930 filename, strerror(errno));
931 exit(1);
932 }
933 if (fwrite((void *)phys_to_virt(file->data),
934 file->len, 1, f) != 1) {
935 printf("Could not write to %s: %s\n",
936 filename, strerror(errno));
937 exit(1);
938 }
939 fclose(f);
940 free(filename);
941
942 if (file->next)
943 file = (struct file *)phys_to_virt(file->next);
944 else
945 file = NULL;
946 }
947 unmap_memory();
948}
949
950static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800951{
952 printf("cbmem v%s -- ", CBMEM_VERSION);
953 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
954 printf(
955 "This program is free software: you can redistribute it and/or modify\n"
956 "it under the terms of the GNU General Public License as published by\n"
957 "the Free Software Foundation, version 2 of the License.\n\n"
958 "This program is distributed in the hope that it will be useful,\n"
959 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
960 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
961 "GNU General Public License for more details.\n\n"
962 "You should have received a copy of the GNU General Public License\n"
963 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
964}
965
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800966static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800967{
Aaron Durbinfbff3012015-08-30 22:00:12 -0500968 printf("usage: %s [-cCltTxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800969 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800970 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800971 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800972 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700973 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800974 " -t | --timestamps: print timestamp information\n"
Aaron Durbinfbff3012015-08-30 22:00:12 -0500975 " -T | --parseable-timestamps: print parseable timestamps\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800976 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800977 " -v | --version: print the version\n"
978 " -h | --help: print this help\n"
979 "\n");
980 exit(1);
981}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700982
Julius Werner337de4c2014-06-16 23:02:03 -0700983#ifdef __arm__
984static void dt_update_cells(const char *name, int *addr_cells_ptr,
985 int *size_cells_ptr)
986{
987 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
988 return;
989
990 int buffer;
991 size_t nlen = strlen(name);
992 char *prop = alloca(nlen + sizeof("/#address-cells"));
993 strcpy(prop, name);
994
995 if (*addr_cells_ptr < 0) {
996 strcpy(prop + nlen, "/#address-cells");
997 int fd = open(prop, O_RDONLY);
998 if (fd < 0 && errno != ENOENT) {
999 perror(prop);
1000 } else if (fd >= 0) {
1001 if (read(fd, &buffer, sizeof(int)) < 0)
1002 perror(prop);
1003 else
1004 *addr_cells_ptr = ntohl(buffer);
1005 close(fd);
1006 }
1007 }
1008
1009 if (*size_cells_ptr < 0) {
1010 strcpy(prop + nlen, "/#size-cells");
1011 int fd = open(prop, O_RDONLY);
1012 if (fd < 0 && errno != ENOENT) {
1013 perror(prop);
1014 } else if (fd >= 0) {
1015 if (read(fd, &buffer, sizeof(int)) < 0)
1016 perror(prop);
1017 else
1018 *size_cells_ptr = ntohl(buffer);
1019 close(fd);
1020 }
1021 }
1022}
1023
1024static char *dt_find_compat(const char *parent, const char *compat,
1025 int *addr_cells_ptr, int *size_cells_ptr)
1026{
1027 char *ret = NULL;
1028 struct dirent *entry;
1029 DIR *dir;
1030
1031 if (!(dir = opendir(parent))) {
1032 perror(parent);
1033 return NULL;
1034 }
1035
1036 /* Loop through all files in the directory (DT node). */
1037 while ((entry = readdir(dir))) {
1038 /* We only care about compatible props or subnodes. */
1039 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
1040 !strcmp(entry->d_name, "compatible")))
1041 continue;
1042
1043 /* Assemble the file name (on the stack, for speed). */
1044 size_t plen = strlen(parent);
1045 char *name = alloca(plen + strlen(entry->d_name) + 2);
1046
1047 strcpy(name, parent);
1048 name[plen] = '/';
1049 strcpy(name + plen + 1, entry->d_name);
1050
1051 /* If it's a subnode, recurse. */
1052 if (entry->d_type & DT_DIR) {
1053 ret = dt_find_compat(name, compat, addr_cells_ptr,
1054 size_cells_ptr);
1055
1056 /* There is only one matching node to find, abort. */
1057 if (ret) {
1058 /* Gather cells values on the way up. */
1059 dt_update_cells(parent, addr_cells_ptr,
1060 size_cells_ptr);
1061 break;
1062 }
1063 continue;
1064 }
1065
1066 /* If it's a compatible string, see if it's the right one. */
1067 int fd = open(name, O_RDONLY);
1068 int clen = strlen(compat);
1069 char *buffer = alloca(clen + 1);
1070
1071 if (fd < 0) {
1072 perror(name);
1073 continue;
1074 }
1075
1076 if (read(fd, buffer, clen + 1) < 0) {
1077 perror(name);
1078 close(fd);
1079 continue;
1080 }
1081 close(fd);
1082
1083 if (!strcmp(compat, buffer)) {
1084 /* Initialize these to "unset" for the way up. */
1085 *addr_cells_ptr = *size_cells_ptr = -1;
1086
1087 /* Can't leave string on the stack or we'll lose it! */
1088 ret = strdup(parent);
1089 break;
1090 }
1091 }
1092
1093 closedir(dir);
1094 return ret;
1095}
1096#endif /* __arm__ */
1097
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001098int main(int argc, char** argv)
1099{
Stefan Reinauer19f87562013-01-07 13:37:12 -08001100 int print_defaults = 1;
1101 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001102 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001103 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001104 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001105 int print_timestamps = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001106 int machine_readable_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001107
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001108 int opt, option_index = 0;
1109 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001110 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001111 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -08001112 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -08001113 {"timestamps", 0, 0, 't'},
Aaron Durbinfbff3012015-08-30 22:00:12 -05001114 {"parseable-timestamps", 0, 0, 'T'},
Stefan Reinauera9c83612013-07-16 17:47:35 -07001115 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001116 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001117 {"version", 0, 0, 'v'},
1118 {"help", 0, 0, 'h'},
1119 {0, 0, 0, 0}
1120 };
Aaron Durbinfbff3012015-08-30 22:00:12 -05001121 while ((opt = getopt_long(argc, argv, "cCltTxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001122 long_options, &option_index)) != EOF) {
1123 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001124 case 'c':
1125 print_console = 1;
1126 print_defaults = 0;
1127 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001128 case 'C':
1129 print_coverage = 1;
1130 print_defaults = 0;
1131 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001132 case 'l':
1133 print_list = 1;
1134 print_defaults = 0;
1135 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001136 case 'x':
1137 print_hexdump = 1;
1138 print_defaults = 0;
1139 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001140 case 't':
1141 print_timestamps = 1;
1142 print_defaults = 0;
1143 break;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001144 case 'T':
1145 print_timestamps = 1;
1146 machine_readable_timestamps = 1;
1147 print_defaults = 0;
1148 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001149 case 'V':
1150 verbose = 1;
1151 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001152 case 'v':
1153 print_version();
1154 exit(0);
1155 break;
1156 case 'h':
1157 case '?':
1158 default:
1159 print_usage(argv[0]);
1160 exit(0);
1161 break;
1162 }
1163 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001164
Julius Werner337de4c2014-06-16 23:02:03 -07001165 mem_fd = open("/dev/mem", O_RDONLY, 0);
1166 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001167 fprintf(stderr, "Failed to gain memory access: %s\n",
1168 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001169 return 1;
1170 }
1171
Stefan Reinauer7f681502013-06-19 15:39:09 -07001172#ifdef __arm__
Julius Werner337de4c2014-06-16 23:02:03 -07001173 int addr_cells, size_cells;
1174 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1175 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001176
Julius Werner337de4c2014-06-16 23:02:03 -07001177 if (!coreboot_node) {
1178 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001179 return 1;
1180 }
1181
Julius Werner337de4c2014-06-16 23:02:03 -07001182 if (addr_cells < 0) {
1183 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1184 addr_cells = 1;
1185 }
1186
1187 int nlen = strlen(coreboot_node);
1188 char *reg = alloca(nlen + sizeof("/reg"));
1189
1190 strcpy(reg, coreboot_node);
1191 strcpy(reg + nlen, "/reg");
1192 free(coreboot_node);
1193
1194 int fd = open(reg, O_RDONLY);
1195 if (fd < 0) {
1196 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001197 return 1;
1198 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001199
Julius Werner337de4c2014-06-16 23:02:03 -07001200 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001201 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1202 u8 *dtbuffer = alloca(size_to_read);
1203 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001204 perror(reg);
1205 return 1;
1206 }
1207 close(fd);
1208
1209 /* No variable-length byte swap function anywhere in C... how sad. */
1210 u64 baseaddr = 0;
1211 for (i = 0; i < addr_cells * 4; i++) {
1212 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001213 baseaddr |= *dtbuffer;
1214 dtbuffer++;
1215 }
1216 u64 cb_table_size = 0;
1217 for (i = 0; i < size_cells * 4; i++) {
1218 cb_table_size <<= 8;
1219 cb_table_size |= *dtbuffer;
1220 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001221 }
1222
Timothy Pearsonbea71402015-09-05 18:07:17 -05001223 parse_cbtable(baseaddr, cb_table_size, 1);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001224#else
1225 int j;
1226 static const int possible_base_addresses[] = { 0, 0xf0000 };
1227
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001228 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001229 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Timothy Pearsonbea71402015-09-05 18:07:17 -05001230 if (parse_cbtable(possible_base_addresses[j], MAP_BYTES, 1))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001231 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001232 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001233#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001234
Stefan Reinauer19f87562013-01-07 13:37:12 -08001235 if (print_console)
1236 dump_console();
1237
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001238 if (print_coverage)
1239 dump_coverage();
1240
Stefan Reinauerc0199072013-01-07 16:26:10 -08001241 if (print_list)
1242 dump_cbmem_toc();
1243
Stefan Reinauera9c83612013-07-16 17:47:35 -07001244 if (print_hexdump)
1245 dump_cbmem_hex();
1246
Stefan Reinauer19f87562013-01-07 13:37:12 -08001247 if (print_defaults || print_timestamps)
Aaron Durbinfbff3012015-08-30 22:00:12 -05001248 dump_timestamps(machine_readable_timestamps);
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001249
Julius Werner337de4c2014-06-16 23:02:03 -07001250 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001251 return 0;
1252}