blob: 9a7ba775a8c41c851a1e32ff8b3348f02a73cd56 [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
Shawn Nematbakhsh5ece96a2015-10-27 13:53:02 -0700425 TS_VB_EC_VBOOT_DONE = 1030,
426
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700427 TS_CROSSYSTEM_DATA = 1100,
428 TS_START_KERNEL = 1101
429};
430
431static const struct timestamp_id_to_name {
432 u32 id;
433 const char *name;
434} timestamp_ids[] = {
Aaron Durbin31540fb2015-07-11 12:44:10 -0500435 /* Marker to report base_time. */
436 { 0, "1st timestamp" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700437 { TS_START_ROMSTAGE, "start of rom stage" },
438 { TS_BEFORE_INITRAM, "before ram initialization" },
439 { TS_AFTER_INITRAM, "after ram initialization" },
440 { TS_END_ROMSTAGE, "end of romstage" },
441 { TS_START_VBOOT, "start of verified boot" },
442 { TS_END_VBOOT, "end of verified boot" },
Julius Wernera7d92442014-12-02 20:51:19 -0800443 { TS_START_COPYRAM, "starting to load ramstage" },
444 { TS_END_COPYRAM, "finished loading ramstage" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700445 { TS_START_RAMSTAGE, "start of ramstage" },
Julius Wernera7d92442014-12-02 20:51:19 -0800446 { TS_START_BOOTBLOCK, "start of bootblock" },
447 { TS_END_BOOTBLOCK, "end of bootblock" },
448 { TS_START_COPYROM, "starting to load romstage" },
449 { TS_END_COPYROM, "finished loading romstage" },
450 { TS_START_ULZMA, "starting LZMA decompress (ignore for x86)" },
451 { TS_END_ULZMA, "finished LZMA decompress (ignore for x86)" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700452 { TS_DEVICE_ENUMERATE, "device enumeration" },
453 { TS_DEVICE_CONFIGURE, "device configuration" },
454 { TS_DEVICE_ENABLE, "device enable" },
455 { TS_DEVICE_INITIALIZE, "device initialization" },
456 { TS_DEVICE_DONE, "device setup done" },
457 { TS_CBMEM_POST, "cbmem post" },
458 { TS_WRITE_TABLES, "write tables" },
459 { TS_LOAD_PAYLOAD, "load payload" },
460 { TS_ACPI_WAKE_JUMP, "ACPI wake jump" },
461 { TS_SELFBOOT_JUMP, "selfboot jump" },
Julius Wernera7d92442014-12-02 20:51:19 -0800462
463 { TS_START_COPYVER, "starting to load verstage" },
464 { TS_END_COPYVER, "finished loading verstage" },
465 { TS_START_TPMINIT, "starting to initialize TPM" },
466 { TS_END_TPMINIT, "finished TPM initialization" },
467 { TS_START_VERIFY_SLOT, "starting to verify keyblock/preamble (RSA)" },
468 { TS_END_VERIFY_SLOT, "finished verifying keyblock/preamble (RSA)" },
469 { TS_START_HASH_BODY, "starting to verify body (load+SHA2+RSA) " },
470 { TS_DONE_LOADING, "finished loading body (ignore for x86)" },
471 { TS_DONE_HASHING, "finished calculating body hash (SHA2)" },
472 { TS_END_HASH_BODY, "finished verifying body signature (RSA)" },
473
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700474 { TS_DC_START, "depthcharge start" },
475 { TS_RO_PARAMS_INIT, "RO parameter init" },
476 { TS_RO_VB_INIT, "RO vboot init" },
477 { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" },
478 { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" },
479 { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" },
480 { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" },
Shawn Nematbakhsh5ece96a2015-10-27 13:53:02 -0700481 { TS_VB_EC_VBOOT_DONE, "finished EC verification" },
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700482 { TS_CROSSYSTEM_DATA, "crossystem data" },
Lee Leahyb8179082015-02-24 11:30:38 -0800483 { TS_START_KERNEL, "start kernel" },
484
485 /* FSP related timestamps */
486 { TS_FSP_MEMORY_INIT_START, "calling FspMemoryInit" },
487 { TS_FSP_MEMORY_INIT_END, "returning from FspMemoryInit" },
488 { TS_FSP_TEMP_RAM_EXIT_START, "calling FspTempRamExit" },
489 { TS_FSP_TEMP_RAM_EXIT_END, "returning from FspTempRamExit" },
490 { TS_FSP_SILICON_INIT_START, "calling FspSiliconInit" },
491 { TS_FSP_SILICON_INIT_END, "returning from FspSiliconInit" },
492 { TS_FSP_BEFORE_ENUMERATE, "calling FspNotify(AfterPciEnumeration)" },
493 { TS_FSP_AFTER_ENUMERATE,
494 "returning from FspNotify(AfterPciEnumeration)" },
495 { TS_FSP_BEFORE_FINALIZE, "calling FspNotify(ReadyToBoot)" },
496 { TS_FSP_AFTER_FINALIZE, "returning from FspNotify(ReadyToBoot)" }
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700497};
498
Aaron Durbinfbff3012015-08-30 22:00:12 -0500499static const char *timestamp_name(uint32_t id)
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700500{
501 int i;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500502
503 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
504 if (timestamp_ids[i].id == id)
505 return timestamp_ids[i].name;
506 }
507 return "<unknown>";
508}
509
510static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp,
511 uint64_t prev_stamp)
512{
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700513 const char *name;
Aaron Durbin799bf782015-08-06 13:52:08 -0500514 uint64_t step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700515
Aaron Durbinfbff3012015-08-30 22:00:12 -0500516 name = timestamp_name(id);
517
518 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
519
520 /* ID<tab>absolute time<tab>relative time<tab>description */
521 printf("%d\t", id);
522 printf("%llu\t", (long long)arch_convert_raw_ts_entry(stamp));
523 printf("%llu\t", (long long)step_time);
524 printf("%s\n", name);
525
526 return step_time;
527}
528
529uint64_t timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
530{
531 const char *name;
532 uint64_t step_time;
533
534 name = timestamp_name(id);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700535
536 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800537 printf("%-50s", name);
538 print_norm(arch_convert_raw_ts_entry(stamp));
Aaron Durbin799bf782015-08-06 13:52:08 -0500539 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700540 if (prev_stamp) {
541 printf(" (");
Aaron Durbin799bf782015-08-06 13:52:08 -0500542 print_norm(step_time);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700543 printf(")");
544 }
545 printf("\n");
Aaron Durbin799bf782015-08-06 13:52:08 -0500546
547 return step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700548}
549
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700550/* dump the timestamp table */
Aaron Durbinfbff3012015-08-30 22:00:12 -0500551static void dump_timestamps(int mach_readable)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700552{
553 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800554 struct timestamp_table *tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500555 size_t size;
Aaron Durbin31540fb2015-07-11 12:44:10 -0500556 uint64_t prev_stamp;
Aaron Durbin799bf782015-08-06 13:52:08 -0500557 uint64_t total_time;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800558
559 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
560 fprintf(stderr, "No timestamps found in coreboot table.\n");
561 return;
562 }
563
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500564 size = sizeof(*tst_p);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500565 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700566
Aaron Durbinc49014e2015-08-30 21:19:55 -0500567 timestamp_set_tick_freq(tst_p->tick_freq_mhz);
568
Aaron Durbinfbff3012015-08-30 22:00:12 -0500569 if (!mach_readable)
570 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500571 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
572
573 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500574 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500575
Aaron Durbin31540fb2015-07-11 12:44:10 -0500576 /* Report the base time within the table. */
577 prev_stamp = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500578 if (mach_readable)
579 timestamp_print_parseable_entry(0, tst_p->base_time,
580 prev_stamp);
581 else
582 timestamp_print_entry(0, tst_p->base_time, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500583 prev_stamp = tst_p->base_time;
584
Aaron Durbin799bf782015-08-06 13:52:08 -0500585 total_time = 0;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700586 for (i = 0; i < tst_p->num_entries; i++) {
Aaron Durbin31540fb2015-07-11 12:44:10 -0500587 uint64_t stamp;
588 const struct timestamp_entry *tse = &tst_p->entries[i];
589
590 /* Make all timestamps absolute. */
591 stamp = tse->entry_stamp + tst_p->base_time;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500592 if (mach_readable)
593 total_time +=
594 timestamp_print_parseable_entry(tse->entry_id,
595 stamp, prev_stamp);
596 else
597 total_time += timestamp_print_entry(tse->entry_id,
Aaron Durbin799bf782015-08-06 13:52:08 -0500598 stamp, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500599 prev_stamp = stamp;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700600 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800601
Aaron Durbinfbff3012015-08-30 22:00:12 -0500602 if (!mach_readable) {
603 printf("\nTotal Time: ");
604 print_norm(total_time);
605 printf("\n");
606 }
Aaron Durbin799bf782015-08-06 13:52:08 -0500607
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800608 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700609}
610
Stefan Reinauer19f87562013-01-07 13:37:12 -0800611/* dump the cbmem console */
612static void dump_console(void)
613{
614 void *console_p;
615 char *console_c;
616 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100617 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800618
619 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
620 fprintf(stderr, "No console found in coreboot table.\n");
621 return;
622 }
623
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500624 console_p = map_memory_size((unsigned long)console.cbmem_addr,
Timothy Pearsonbea71402015-09-05 18:07:17 -0500625 2 * sizeof(uint32_t), 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800626 /* The in-memory format of the console area is:
627 * u32 size
628 * u32 cursor
629 * char console[size]
630 * Hence we have to add 8 to get to the actual console string.
631 */
Gabe Black06b13a32013-08-09 00:40:06 -0700632 size = ((uint32_t *)console_p)[0];
633 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100634 /* Cursor continues to go on even after no more data fits in
635 * the buffer but the data is dropped in this case.
636 */
637 if (size > cursor)
638 size = cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800639 console_c = malloc(size + 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500640 unmap_memory();
Stefan Reinauer19f87562013-01-07 13:37:12 -0800641 if (!console_c) {
642 fprintf(stderr, "Not enough memory for console.\n");
643 exit(1);
644 }
645
Aaron Durbinab180d82014-03-31 11:59:58 -0500646 console_p = map_memory_size((unsigned long)console.cbmem_addr,
Timothy Pearsonbea71402015-09-05 18:07:17 -0500647 size + sizeof(size) + sizeof(cursor), 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800648 memcpy(console_c, console_p + 8, size);
649 console_c[size] = 0;
Gabe Black06b13a32013-08-09 00:40:06 -0700650 console_c[cursor] = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800651
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100652 printf("%s\n", console_c);
653 if (size < cursor)
654 printf("%d %s lost\n", cursor - size,
655 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800656
657 free(console_c);
658
659 unmap_memory();
660}
661
Stefan Reinauera9c83612013-07-16 17:47:35 -0700662static void hexdump(unsigned long memory, int length)
663{
664 int i;
665 uint8_t *m;
666 int all_zero = 0;
667
Timothy Pearsonbea71402015-09-05 18:07:17 -0500668 m = map_memory_size((intptr_t)memory, length, 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700669
670 if (length > MAP_BYTES) {
671 printf("Truncating hex dump from %d to %d bytes\n\n",
672 length, MAP_BYTES);
673 length = MAP_BYTES;
674 }
675
676 for (i = 0; i < length; i += 16) {
677 int j;
678
679 all_zero++;
680 for (j = 0; j < 16; j++) {
681 if(m[i+j] != 0) {
682 all_zero = 0;
683 break;
684 }
685 }
686
687 if (all_zero < 2) {
688 printf("%08lx:", memory + i);
689 for (j = 0; j < 16; j++)
690 printf(" %02x", m[i+j]);
691 printf(" ");
692 for (j = 0; j < 16; j++)
693 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
694 printf("\n");
695 } else if (all_zero == 2) {
696 printf("...\n");
697 }
698 }
699
700 unmap_memory();
701}
702
703static void dump_cbmem_hex(void)
704{
705 if (cbmem.type != LB_MEM_TABLE) {
706 fprintf(stderr, "No coreboot CBMEM area found!\n");
707 return;
708 }
709
710 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
711}
712
713/* The root region is at least DYN_CBMEM_ALIGN_SIZE . */
714#define DYN_CBMEM_ALIGN_SIZE (4096)
715#define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE
Timothy Pearsondf699d52015-05-16 14:55:54 -0500716#define CBMEM_POINTER_MAGIC 0xc0389481
Stefan Reinauera9c83612013-07-16 17:47:35 -0700717#define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC)
718
719struct cbmem_root_pointer {
720 uint32_t magic;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500721 /* Relative to upper limit/offset. */
722 int32_t root_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700723} __attribute__((packed));
724
725struct dynamic_cbmem_entry {
726 uint32_t magic;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500727 int32_t start_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700728 uint32_t size;
729 uint32_t id;
730} __attribute__((packed));
731
732struct cbmem_root {
733 uint32_t max_entries;
734 uint32_t num_entries;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500735 uint32_t flags;
736 uint32_t entry_align;
737 int32_t max_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700738 struct dynamic_cbmem_entry entries[0];
739} __attribute__((packed));
740
Stefan Reinauerc0199072013-01-07 16:26:10 -0800741#define CBMEM_MAGIC 0x434f5245
742#define MAX_CBMEM_ENTRIES 16
743
744struct cbmem_entry {
745 uint32_t magic;
746 uint32_t id;
747 uint64_t base;
748 uint64_t size;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700749} __attribute__((packed));
750
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600751struct cbmem_id_to_name {
752 uint32_t id;
753 const char *name;
754};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700755static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800756
Stefan Reinauera9c83612013-07-16 17:47:35 -0700757void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
758{
759 int i;
760 const char *name;
761
762 name = NULL;
763 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
764 if (cbmem_ids[i].id == id) {
765 name = cbmem_ids[i].name;
766 break;
767 }
768 }
769
770 printf("%2d. ", n);
771 if (name == NULL)
772 printf("%08x ", id);
773 else
774 printf("%s", name);
775 printf(" %08" PRIx64 " ", base);
776 printf(" %08" PRIx64 "\n", size);
777}
778
779static void dump_static_cbmem_toc(struct cbmem_entry *entries)
780{
781 int i;
782
783 printf("CBMEM table of contents:\n");
784 printf(" ID START LENGTH\n");
785
786 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
787 if (entries[i].magic != CBMEM_MAGIC)
788 break;
789 cbmem_print_entry(i, entries[i].id,
790 entries[i].base, entries[i].size);
791 }
792}
793
794static void dump_dynamic_cbmem_toc(struct cbmem_root *root)
795{
796 int i;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500797 debug("CBMEM: max_entries=%d num_entries=%d flags=0x%x, entry_align=0x%x, max_offset=%d\n\n",
798 root->max_entries, root->num_entries, root->flags, root->entry_align, root->max_offset);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700799
800 printf("CBMEM table of contents:\n");
801 printf(" ID START LENGTH\n");
802
803 for (i = 0; i < root->num_entries; i++) {
804 if(root->entries[i].magic != CBMEM_ENTRY_MAGIC)
805 break;
806 cbmem_print_entry(i, root->entries[i].id,
Timothy Pearsondf699d52015-05-16 14:55:54 -0500807 rootptr + root->entries[i].start_offset, root->entries[i].size);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700808 }
809}
810
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800811static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800812{
Stefan Reinauerc0199072013-01-07 16:26:10 -0800813 uint64_t start;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700814 void *cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800815 struct cbmem_entry *entries;
816
817 if (cbmem.type != LB_MEM_TABLE) {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700818 fprintf(stderr, "No coreboot CBMEM area found!\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800819 return;
820 }
821
822 start = unpack_lb64(cbmem.start);
823
Timothy Pearsonbea71402015-09-05 18:07:17 -0500824 cbmem_area = map_memory_size(start, unpack_lb64(cbmem.size), 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700825 entries = (struct cbmem_entry *)cbmem_area;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800826
Stefan Reinauera9c83612013-07-16 17:47:35 -0700827 if (entries[0].magic == CBMEM_MAGIC) {
828 dump_static_cbmem_toc(entries);
829 } else {
Stefan Reinauera9c83612013-07-16 17:47:35 -0700830 rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size);
831 rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1);
832 rootptr -= sizeof(struct cbmem_root_pointer);
833 unmap_memory();
834 struct cbmem_root_pointer *r =
Timothy Pearsonbea71402015-09-05 18:07:17 -0500835 map_memory_size(rootptr, sizeof(*r), 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700836 if (r->magic == CBMEM_POINTER_MAGIC) {
837 struct cbmem_root *root;
Timothy Pearsondf699d52015-05-16 14:55:54 -0500838 uint64_t rootaddr = rootptr + r->root_offset;
Stefan Reinauera9c83612013-07-16 17:47:35 -0700839 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500840 root = map_memory_size(rootaddr, ROOT_MIN_SIZE, 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700841 dump_dynamic_cbmem_toc(root);
842 } else
843 fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n");
Stefan Reinauerc0199072013-01-07 16:26:10 -0800844 }
Stefan Reinauera9c83612013-07-16 17:47:35 -0700845
Stefan Reinauerc0199072013-01-07 16:26:10 -0800846 unmap_memory();
847}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800848
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800849#define COVERAGE_MAGIC 0x584d4153
850struct file {
851 uint32_t magic;
852 uint32_t next;
853 uint32_t filename;
854 uint32_t data;
855 int offset;
856 int len;
857};
858
859static int mkpath(char *path, mode_t mode)
860{
861 assert (path && *path);
862 char *p;
863 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
864 *p = '\0';
865 if (mkdir(path, mode) == -1) {
866 if (errno != EEXIST) {
867 *p = '/';
868 return -1;
869 }
870 }
871 *p = '/';
872 }
873 return 0;
874}
875
876static void dump_coverage(void)
877{
878 int i, found = 0;
879 uint64_t start;
880 struct cbmem_entry *entries;
881 void *coverage;
882 unsigned long phys_offset;
883#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
884
885 if (cbmem.type != LB_MEM_TABLE) {
886 fprintf(stderr, "No coreboot table area found!\n");
887 return;
888 }
889
890 start = unpack_lb64(cbmem.start);
891
892 entries = (struct cbmem_entry *)map_memory(start);
893
894 for (i=0; i<MAX_CBMEM_ENTRIES; i++) {
895 if (entries[i].magic != CBMEM_MAGIC)
896 break;
897 if (entries[i].id == CBMEM_ID_COVERAGE) {
898 found = 1;
899 break;
900 }
901 }
902
903 if (!found) {
904 unmap_memory();
905 fprintf(stderr, "No coverage information found in"
906 " CBMEM area.\n");
907 return;
908 }
909
910 start = entries[i].base;
911 unmap_memory();
912 /* Map coverage area */
913 coverage = map_memory(start);
914 phys_offset = (unsigned long)coverage - (unsigned long)start;
915
916 printf("Dumping coverage data...\n");
917
918 struct file *file = (struct file *)coverage;
919 while (file && file->magic == COVERAGE_MAGIC) {
920 FILE *f;
921 char *filename;
922
923 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
924 filename = strdup((char *)phys_to_virt(file->filename));
925 if (mkpath(filename, 0755) == -1) {
926 perror("Directory for coverage data could "
927 "not be created");
928 exit(1);
929 }
930 f = fopen(filename, "wb");
931 if (!f) {
932 printf("Could not open %s: %s\n",
933 filename, strerror(errno));
934 exit(1);
935 }
936 if (fwrite((void *)phys_to_virt(file->data),
937 file->len, 1, f) != 1) {
938 printf("Could not write to %s: %s\n",
939 filename, strerror(errno));
940 exit(1);
941 }
942 fclose(f);
943 free(filename);
944
945 if (file->next)
946 file = (struct file *)phys_to_virt(file->next);
947 else
948 file = NULL;
949 }
950 unmap_memory();
951}
952
953static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800954{
955 printf("cbmem v%s -- ", CBMEM_VERSION);
956 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
957 printf(
958 "This program is free software: you can redistribute it and/or modify\n"
959 "it under the terms of the GNU General Public License as published by\n"
960 "the Free Software Foundation, version 2 of the License.\n\n"
961 "This program is distributed in the hope that it will be useful,\n"
962 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
963 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
964 "GNU General Public License for more details.\n\n"
965 "You should have received a copy of the GNU General Public License\n"
966 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
967}
968
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800969static void print_usage(const char *name)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800970{
Aaron Durbinfbff3012015-08-30 22:00:12 -0500971 printf("usage: %s [-cCltTxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800972 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800973 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800974 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800975 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700976 " -x | --hexdump: print hexdump of cbmem area\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800977 " -t | --timestamps: print timestamp information\n"
Aaron Durbinfbff3012015-08-30 22:00:12 -0500978 " -T | --parseable-timestamps: print parseable timestamps\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800979 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800980 " -v | --version: print the version\n"
981 " -h | --help: print this help\n"
982 "\n");
983 exit(1);
984}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700985
Julius Werner337de4c2014-06-16 23:02:03 -0700986#ifdef __arm__
987static void dt_update_cells(const char *name, int *addr_cells_ptr,
988 int *size_cells_ptr)
989{
990 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
991 return;
992
993 int buffer;
994 size_t nlen = strlen(name);
995 char *prop = alloca(nlen + sizeof("/#address-cells"));
996 strcpy(prop, name);
997
998 if (*addr_cells_ptr < 0) {
999 strcpy(prop + nlen, "/#address-cells");
1000 int fd = open(prop, O_RDONLY);
1001 if (fd < 0 && errno != ENOENT) {
1002 perror(prop);
1003 } else if (fd >= 0) {
1004 if (read(fd, &buffer, sizeof(int)) < 0)
1005 perror(prop);
1006 else
1007 *addr_cells_ptr = ntohl(buffer);
1008 close(fd);
1009 }
1010 }
1011
1012 if (*size_cells_ptr < 0) {
1013 strcpy(prop + nlen, "/#size-cells");
1014 int fd = open(prop, O_RDONLY);
1015 if (fd < 0 && errno != ENOENT) {
1016 perror(prop);
1017 } else if (fd >= 0) {
1018 if (read(fd, &buffer, sizeof(int)) < 0)
1019 perror(prop);
1020 else
1021 *size_cells_ptr = ntohl(buffer);
1022 close(fd);
1023 }
1024 }
1025}
1026
1027static char *dt_find_compat(const char *parent, const char *compat,
1028 int *addr_cells_ptr, int *size_cells_ptr)
1029{
1030 char *ret = NULL;
1031 struct dirent *entry;
1032 DIR *dir;
1033
1034 if (!(dir = opendir(parent))) {
1035 perror(parent);
1036 return NULL;
1037 }
1038
1039 /* Loop through all files in the directory (DT node). */
1040 while ((entry = readdir(dir))) {
1041 /* We only care about compatible props or subnodes. */
1042 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
1043 !strcmp(entry->d_name, "compatible")))
1044 continue;
1045
1046 /* Assemble the file name (on the stack, for speed). */
1047 size_t plen = strlen(parent);
1048 char *name = alloca(plen + strlen(entry->d_name) + 2);
1049
1050 strcpy(name, parent);
1051 name[plen] = '/';
1052 strcpy(name + plen + 1, entry->d_name);
1053
1054 /* If it's a subnode, recurse. */
1055 if (entry->d_type & DT_DIR) {
1056 ret = dt_find_compat(name, compat, addr_cells_ptr,
1057 size_cells_ptr);
1058
1059 /* There is only one matching node to find, abort. */
1060 if (ret) {
1061 /* Gather cells values on the way up. */
1062 dt_update_cells(parent, addr_cells_ptr,
1063 size_cells_ptr);
1064 break;
1065 }
1066 continue;
1067 }
1068
1069 /* If it's a compatible string, see if it's the right one. */
1070 int fd = open(name, O_RDONLY);
1071 int clen = strlen(compat);
1072 char *buffer = alloca(clen + 1);
1073
1074 if (fd < 0) {
1075 perror(name);
1076 continue;
1077 }
1078
1079 if (read(fd, buffer, clen + 1) < 0) {
1080 perror(name);
1081 close(fd);
1082 continue;
1083 }
1084 close(fd);
1085
1086 if (!strcmp(compat, buffer)) {
1087 /* Initialize these to "unset" for the way up. */
1088 *addr_cells_ptr = *size_cells_ptr = -1;
1089
1090 /* Can't leave string on the stack or we'll lose it! */
1091 ret = strdup(parent);
1092 break;
1093 }
1094 }
1095
1096 closedir(dir);
1097 return ret;
1098}
1099#endif /* __arm__ */
1100
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001101int main(int argc, char** argv)
1102{
Stefan Reinauer19f87562013-01-07 13:37:12 -08001103 int print_defaults = 1;
1104 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001105 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001106 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001107 int print_hexdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001108 int print_timestamps = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001109 int machine_readable_timestamps = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001110
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001111 int opt, option_index = 0;
1112 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001113 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001114 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -08001115 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -08001116 {"timestamps", 0, 0, 't'},
Aaron Durbinfbff3012015-08-30 22:00:12 -05001117 {"parseable-timestamps", 0, 0, 'T'},
Stefan Reinauera9c83612013-07-16 17:47:35 -07001118 {"hexdump", 0, 0, 'x'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001119 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001120 {"version", 0, 0, 'v'},
1121 {"help", 0, 0, 'h'},
1122 {0, 0, 0, 0}
1123 };
Aaron Durbinfbff3012015-08-30 22:00:12 -05001124 while ((opt = getopt_long(argc, argv, "cCltTxVvh?",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001125 long_options, &option_index)) != EOF) {
1126 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001127 case 'c':
1128 print_console = 1;
1129 print_defaults = 0;
1130 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001131 case 'C':
1132 print_coverage = 1;
1133 print_defaults = 0;
1134 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001135 case 'l':
1136 print_list = 1;
1137 print_defaults = 0;
1138 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001139 case 'x':
1140 print_hexdump = 1;
1141 print_defaults = 0;
1142 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001143 case 't':
1144 print_timestamps = 1;
1145 print_defaults = 0;
1146 break;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001147 case 'T':
1148 print_timestamps = 1;
1149 machine_readable_timestamps = 1;
1150 print_defaults = 0;
1151 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001152 case 'V':
1153 verbose = 1;
1154 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001155 case 'v':
1156 print_version();
1157 exit(0);
1158 break;
1159 case 'h':
1160 case '?':
1161 default:
1162 print_usage(argv[0]);
1163 exit(0);
1164 break;
1165 }
1166 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001167
Julius Werner337de4c2014-06-16 23:02:03 -07001168 mem_fd = open("/dev/mem", O_RDONLY, 0);
1169 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001170 fprintf(stderr, "Failed to gain memory access: %s\n",
1171 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001172 return 1;
1173 }
1174
Stefan Reinauer7f681502013-06-19 15:39:09 -07001175#ifdef __arm__
Julius Werner337de4c2014-06-16 23:02:03 -07001176 int addr_cells, size_cells;
1177 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1178 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001179
Julius Werner337de4c2014-06-16 23:02:03 -07001180 if (!coreboot_node) {
1181 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001182 return 1;
1183 }
1184
Julius Werner337de4c2014-06-16 23:02:03 -07001185 if (addr_cells < 0) {
1186 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1187 addr_cells = 1;
1188 }
1189
1190 int nlen = strlen(coreboot_node);
1191 char *reg = alloca(nlen + sizeof("/reg"));
1192
1193 strcpy(reg, coreboot_node);
1194 strcpy(reg + nlen, "/reg");
1195 free(coreboot_node);
1196
1197 int fd = open(reg, O_RDONLY);
1198 if (fd < 0) {
1199 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001200 return 1;
1201 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001202
Julius Werner337de4c2014-06-16 23:02:03 -07001203 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001204 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1205 u8 *dtbuffer = alloca(size_to_read);
1206 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001207 perror(reg);
1208 return 1;
1209 }
1210 close(fd);
1211
1212 /* No variable-length byte swap function anywhere in C... how sad. */
1213 u64 baseaddr = 0;
1214 for (i = 0; i < addr_cells * 4; i++) {
1215 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001216 baseaddr |= *dtbuffer;
1217 dtbuffer++;
1218 }
1219 u64 cb_table_size = 0;
1220 for (i = 0; i < size_cells * 4; i++) {
1221 cb_table_size <<= 8;
1222 cb_table_size |= *dtbuffer;
1223 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001224 }
1225
Timothy Pearsonbea71402015-09-05 18:07:17 -05001226 parse_cbtable(baseaddr, cb_table_size, 1);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001227#else
1228 int j;
1229 static const int possible_base_addresses[] = { 0, 0xf0000 };
1230
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001231 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001232 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Timothy Pearsonbea71402015-09-05 18:07:17 -05001233 if (parse_cbtable(possible_base_addresses[j], MAP_BYTES, 1))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001234 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001235 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001236#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001237
Stefan Reinauer19f87562013-01-07 13:37:12 -08001238 if (print_console)
1239 dump_console();
1240
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001241 if (print_coverage)
1242 dump_coverage();
1243
Stefan Reinauerc0199072013-01-07 16:26:10 -08001244 if (print_list)
1245 dump_cbmem_toc();
1246
Stefan Reinauera9c83612013-07-16 17:47:35 -07001247 if (print_hexdump)
1248 dump_cbmem_hex();
1249
Stefan Reinauer19f87562013-01-07 13:37:12 -08001250 if (print_defaults || print_timestamps)
Aaron Durbinfbff3012015-08-30 22:00:12 -05001251 dump_timestamps(machine_readable_timestamps);
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001252
Julius Werner337de4c2014-06-16 23:02:03 -07001253 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001254 return 0;
1255}