blob: 7b434d874c68da3491b55d293dba69eb2d02c3da [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.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070015 */
16
Nico Huber8e4bb9282013-05-26 18:17:54 +020017#include <inttypes.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080021#include <unistd.h>
Stefan Reinauer8c594772013-04-19 14:22:29 -070022#include <inttypes.h>
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080023#include <getopt.h>
Julius Werner337de4c2014-06-16 23:02:03 -070024#include <dirent.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080025#include <errno.h>
26#include <fcntl.h>
Stefan Reinauera9c83612013-07-16 17:47:35 -070027#include <ctype.h>
Stefan Reinauer7f681502013-06-19 15:39:09 -070028#include <arpa/inet.h>
Stefan Reinauer05cbce62013-01-03 14:30:33 -080029#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/mman.h>
Stefan Reinauerd37ab452012-12-18 16:23:28 -080032#include <libgen.h>
33#include <assert.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050034#include <commonlib/cbmem_id.h>
35#include <commonlib/timestamp_serialized.h>
36#include <commonlib/coreboot_tables.h>
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070037
Patrick Georgid00f1802015-07-13 16:53:50 +020038#ifdef __OpenBSD__
39#include <sys/param.h>
40#include <sys/sysctl.h>
41#endif
42
Stefan Reinauer05cbce62013-01-03 14:30:33 -080043#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
44#define MAP_BYTES (1024*1024)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070045
Julius Werner337de4c2014-06-16 23:02:03 -070046typedef uint8_t u8;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070047typedef uint16_t u16;
48typedef uint32_t u32;
49typedef uint64_t u64;
50
Stefan Reinauera9c83612013-07-16 17:47:35 -070051#define CBMEM_VERSION "1.1"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -080052
Stefan Reinauer05cbce62013-01-03 14:30:33 -080053/* verbose output? */
54static int verbose = 0;
55#define debug(x...) if(verbose) printf(x)
56
57/* File handle used to access /dev/mem */
Julius Werner337de4c2014-06-16 23:02:03 -070058static int mem_fd;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070059
Aaron Durbin09c0c112015-09-30 12:33:01 -050060static uint64_t lbtable_address;
61static size_t lbtable_size;
Timothy Pearsondf699d52015-05-16 14:55:54 -050062
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070063/*
64 * calculate ip checksum (16 bit quantities) on a passed in buffer. In case
65 * the buffer length is odd last byte is excluded from the calculation
66 */
67static u16 ipchcksum(const void *addr, unsigned size)
68{
69 const u16 *p = addr;
70 unsigned i, n = size / 2; /* don't expect odd sized blocks */
71 u32 sum = 0;
72
73 for (i = 0; i < n; i++)
74 sum += p[i];
75
76 sum = (sum >> 16) + (sum & 0xffff);
77 sum += (sum >> 16);
78 sum = ~sum & 0xffff;
79 return (u16) sum;
80}
81
82/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -080083 * Functions to map / unmap physical memory into virtual address space. These
84 * functions always maps 1MB at a time and can only map one area at once.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -070085 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -080086static void *mapped_virtual;
Aaron Durbinab180d82014-03-31 11:59:58 -050087static size_t mapped_size;
88
89static inline size_t size_to_mib(size_t sz)
90{
91 return sz >> 20;
92}
93
94static void unmap_memory(void)
95{
96 if (mapped_virtual == NULL) {
97 fprintf(stderr, "Error unmapping memory\n");
98 return;
99 }
Timothy Pearsondf699d52015-05-16 14:55:54 -0500100 if (size_to_mib(mapped_size) == 0) {
101 debug("Unmapping %zuMB of virtual memory at %p.\n",
102 size_to_mib(mapped_size), mapped_virtual);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500103 } else {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500104 debug("Unmapping %zuMB of virtual memory at %p.\n",
105 size_to_mib(mapped_size), mapped_virtual);
106 }
Aaron Durbinab180d82014-03-31 11:59:58 -0500107 munmap(mapped_virtual, mapped_size);
108 mapped_virtual = NULL;
109 mapped_size = 0;
110}
111
Timothy Pearsonbea71402015-09-05 18:07:17 -0500112static void *map_memory_size(u64 physical, size_t size, uint8_t abort_on_failure)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700113{
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800114 void *v;
115 off_t p;
Stefan Reinauer7f681502013-06-19 15:39:09 -0700116 u64 page = getpagesize();
Aaron Durbinab180d82014-03-31 11:59:58 -0500117 size_t padding;
118
119 if (mapped_virtual != NULL)
120 unmap_memory();
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800121
122 /* Mapped memory must be aligned to page size */
123 p = physical & ~(page - 1);
Aaron Durbinab180d82014-03-31 11:59:58 -0500124 padding = physical & (page-1);
125 size += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800126
Timothy Pearsondf699d52015-05-16 14:55:54 -0500127 if (size_to_mib(size) == 0) {
128 debug("Mapping %zuB of physical memory at 0x%jx (requested 0x%jx).\n",
129 size, (intmax_t)p, (intmax_t)physical);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500130 } else {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500131 debug("Mapping %zuMB of physical memory at 0x%jx (requested 0x%jx).\n",
132 size_to_mib(size), (intmax_t)p, (intmax_t)physical);
133 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800134
Julius Werner337de4c2014-06-16 23:02:03 -0700135 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800136
137 if (v == MAP_FAILED) {
Timothy Pearsondf699d52015-05-16 14:55:54 -0500138 /* The mapped area may have overrun the upper cbmem boundary when trying to
139 * align to the page size. Try growing down instead of up...
140 */
141 p -= page;
142 padding += page;
143 size &= ~(page - 1);
144 size = size + (page - 1);
145 v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p);
146 debug(" ... failed. Mapping %zuB of physical memory at 0x%jx.\n",
147 size, (intmax_t)p);
148 }
149
150 if (v == MAP_FAILED) {
Timothy Pearsonbea71402015-09-05 18:07:17 -0500151 if (abort_on_failure) {
152 fprintf(stderr, "Failed to mmap /dev/mem: %s\n",
153 strerror(errno));
154 exit(1);
155 } else {
156 return 0;
157 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700158 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800159
160 /* Remember what we actually mapped ... */
161 mapped_virtual = v;
Aaron Durbinab180d82014-03-31 11:59:58 -0500162 mapped_size = size;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800163
164 /* ... but return address to the physical memory that was requested */
Stefan Reinauera9c83612013-07-16 17:47:35 -0700165 if (padding)
Aaron Durbinab180d82014-03-31 11:59:58 -0500166 debug(" ... padding virtual address with 0x%zx bytes.\n",
Stefan Reinauera9c83612013-07-16 17:47:35 -0700167 padding);
168 v += padding;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800169
170 return v;
171}
172
Aaron Durbin09c0c112015-09-30 12:33:01 -0500173static void *map_lbtable(void)
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800174{
Aaron Durbin09c0c112015-09-30 12:33:01 -0500175 if (lbtable_address == 0 || lbtable_size == 0) {
176 fprintf(stderr, "No coreboot table area found!\n");
177 return NULL;
178 }
179
180 return map_memory_size(lbtable_address, lbtable_size, 1);
181}
182
183static void unmap_lbtable(void)
184{
185 unmap_memory();
186}
187
188/* Find the first cbmem entry filling in the details. */
189static int find_cbmem_entry(uint32_t id, uint64_t *addr, size_t *size)
190{
191 uint8_t *table;
192 size_t offset;
193 int ret = -1;
194
195 table = map_lbtable();
196
197 if (table == NULL)
198 return -1;
199
200 offset = 0;
201
202 while (offset < lbtable_size) {
203 struct lb_record *lbr;
204 struct lb_cbmem_entry *lbe;
205
206 lbr = (void *)(table + offset);
207 offset += lbr->size;
208
209 if (lbr->tag != LB_TAG_CBMEM_ENTRY)
210 continue;
211
212 lbe = (void *)lbr;
213 if (lbe->id != id)
214 continue;
215
216 *addr = lbe->address;
217 *size = lbe->entry_size;
218 ret = 0;
219 break;
220 }
221
222 unmap_lbtable();
223 return ret;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700224}
225
226/*
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800227 * Try finding the timestamp table and coreboot cbmem console starting from the
228 * passed in memory offset. Could be called recursively in case a forwarding
229 * entry is found.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700230 *
231 * Returns pointer to a memory buffer containg the timestamp table or zero if
232 * none found.
233 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800234
235static struct lb_cbmem_ref timestamps;
236static struct lb_cbmem_ref console;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800237static struct lb_memory_range cbmem;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800238
Stefan Reinauer8c594772013-04-19 14:22:29 -0700239/* This is a work-around for a nasty problem introduced by initially having
240 * pointer sized entries in the lb_cbmem_ref structures. This caused problems
241 * on 64bit x86 systems because coreboot is 32bit on those systems.
242 * When the problem was found, it was corrected, but there are a lot of
243 * systems out there with a firmware that does not produce the right
244 * lb_cbmem_ref structure. Hence we try to autocorrect this issue here.
245 */
246static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref)
247{
248 struct lb_cbmem_ref ret;
249
250 ret = *cbmem_ref;
251
252 if (cbmem_ref->size < sizeof(*cbmem_ref))
253 ret.cbmem_addr = (uint32_t)ret.cbmem_addr;
254
Stefan Reinauera9c83612013-07-16 17:47:35 -0700255 debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr);
256
Stefan Reinauer8c594772013-04-19 14:22:29 -0700257 return ret;
258}
259
Timothy Pearsonbea71402015-09-05 18:07:17 -0500260static int parse_cbtable(u64 address, size_t table_size, uint8_t abort_on_failure)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700261{
Patrick Georgibe8f0fa2016-07-29 21:55:44 +0200262 int i, found = 0, ret = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800263 void *buf;
264
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500265 debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n",
266 address, table_size);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500267 buf = map_memory_size(address, table_size, abort_on_failure);
268 if (!buf)
269 return -2;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700270
271 /* look at every 16 bytes within 4K of the base */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800272
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700273 for (i = 0; i < 0x1000; i += 0x10) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800274 struct lb_header *lbh;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700275 struct lb_record* lbr_p;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800276 void *lbtable;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700277 int j;
278
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800279 lbh = (struct lb_header *)(buf + i);
280 if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) ||
281 !lbh->header_bytes ||
282 ipchcksum(lbh, sizeof(*lbh))) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700283 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700284 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800285 lbtable = buf + i + lbh->header_bytes;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700286
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800287 if (ipchcksum(lbtable, lbh->table_bytes) !=
288 lbh->table_checksum) {
289 debug("Signature found, but wrong checksum.\n");
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700290 continue;
291 }
292
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800293 found = 1;
294 debug("Found!\n");
295
Aaron Durbin09c0c112015-09-30 12:33:01 -0500296 /* Keep reference to lbtable. */
297 lbtable_address = address;
298 lbtable_address += ((uint8_t *)lbtable - (uint8_t *)lbh);
299 lbtable_size = lbh->table_bytes;
300
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800301 for (j = 0; j < lbh->table_bytes; j += lbr_p->size) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800302 lbr_p = (struct lb_record*) ((char *)lbtable + j);
303 debug(" coreboot table entry 0x%02x\n", lbr_p->tag);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700304 switch (lbr_p->tag) {
Stefan Reinauerc0199072013-01-07 16:26:10 -0800305 case LB_TAG_MEMORY: {
306 int i = 0;
307 debug(" Found memory map.\n");
308 struct lb_memory *memory =
309 (struct lb_memory *)lbr_p;
Paul Menzel747c07f2014-10-17 13:46:12 +0200310 while ((char *)&memory->map[i] < ((char *)lbr_p
Stefan Reinauerc0199072013-01-07 16:26:10 -0800311 + lbr_p->size)) {
312 if (memory->map[i].type == LB_MEM_TABLE) {
313 debug(" LB_MEM_TABLE found.\n");
314 /* The last one found is CBMEM */
315 cbmem = memory->map[i];
316 }
317 i++;
318 }
319 continue;
320 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700321 case LB_TAG_TIMESTAMPS: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800322 debug(" Found timestamp table.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700323 timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800324 continue;
325 }
326 case LB_TAG_CBMEM_CONSOLE: {
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800327 debug(" Found cbmem console.\n");
Stefan Reinauer8c594772013-04-19 14:22:29 -0700328 console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p);
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800329 continue;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700330 }
331 case LB_TAG_FORWARD: {
332 /*
333 * This is a forwarding entry - repeat the
334 * search at the new address.
335 */
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800336 struct lb_forward lbf_p =
337 *(struct lb_forward *) lbr_p;
Stefan Reinauerd7144dc2013-01-07 15:25:37 -0800338 debug(" Found forwarding entry.\n");
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800339 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500340 ret = parse_cbtable(lbf_p.forward, table_size, 0);
341 if (ret == -2) {
342 /* try again with a smaller memory mapping request */
343 ret = parse_cbtable(lbf_p.forward, table_size / 2, 1);
344 if (ret == -2)
345 exit(1);
346 else
347 return ret;
348 } else {
349 return ret;
350 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700351 }
352 default:
353 break;
354 }
355
356 }
357 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800358 unmap_memory();
359
360 return found;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700361}
362
Patrick Georgid00f1802015-07-13 16:53:50 +0200363#if defined(linux) && (defined(__i386__) || defined(__x86_64__))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700364/*
Aaron Durbin08e920e2016-03-12 08:41:34 +0100365 * read CPU frequency from a sysfs file, return an frequency in Megahertz as
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700366 * an int or exit on any error.
367 */
Aaron Durbinc49014e2015-08-30 21:19:55 -0500368static unsigned long arch_tick_frequency(void)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700369{
370 FILE *cpuf;
371 char freqs[100];
372 int size;
373 char *endp;
374 u64 rv;
375
376 const char* freq_file =
377 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
378
379 cpuf = fopen(freq_file, "r");
380 if (!cpuf) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800381 fprintf(stderr, "Could not open %s: %s\n",
382 freq_file, strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700383 exit(1);
384 }
385
386 memset(freqs, 0, sizeof(freqs));
387 size = fread(freqs, 1, sizeof(freqs), cpuf);
388 if (!size || (size == sizeof(freqs))) {
389 fprintf(stderr, "Wrong number of bytes(%d) read from %s\n",
390 size, freq_file);
391 exit(1);
392 }
393 fclose(cpuf);
394 rv = strtoull(freqs, &endp, 10);
395
396 if (*endp == '\0' || *endp == '\n')
Aaron Durbin08e920e2016-03-12 08:41:34 +0100397 /* cpuinfo_max_freq is in kHz. Convert it to MHz. */
398 return rv / 1000;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700399 fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n",
400 freqs, freq_file);
401 exit(1);
402}
Patrick Georgid00f1802015-07-13 16:53:50 +0200403#elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__))
Aaron Durbinc49014e2015-08-30 21:19:55 -0500404static unsigned long arch_tick_frequency(void)
Patrick Georgid00f1802015-07-13 16:53:50 +0200405{
406 int mib[2] = { CTL_HW, HW_CPUSPEED };
407 static int value = 0;
408 size_t value_len = sizeof(value);
409
Aaron Durbinc49014e2015-08-30 21:19:55 -0500410 /* Return 1 MHz when sysctl fails. */
Patrick Georgid00f1802015-07-13 16:53:50 +0200411 if ((value == 0) && (sysctl(mib, 2, &value, &value_len, NULL, 0) == -1))
Aaron Durbinc49014e2015-08-30 21:19:55 -0500412 return 1;
Patrick Georgid00f1802015-07-13 16:53:50 +0200413
Aaron Durbinc49014e2015-08-30 21:19:55 -0500414 return value;
Patrick Georgid00f1802015-07-13 16:53:50 +0200415}
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700416#else
Aaron Durbinc49014e2015-08-30 21:19:55 -0500417static unsigned long arch_tick_frequency(void)
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700418{
Aaron Durbinc49014e2015-08-30 21:19:55 -0500419 /* 1 MHz = 1us. */
420 return 1;
Stefan Reinauerd8ef9e92013-07-31 15:44:37 -0700421}
422#endif
423
Aaron Durbinc49014e2015-08-30 21:19:55 -0500424static unsigned long tick_freq_mhz;
425
426static void timestamp_set_tick_freq(unsigned long table_tick_freq_mhz)
427{
428 tick_freq_mhz = table_tick_freq_mhz;
429
430 /* Honor table frequency. */
431 if (tick_freq_mhz)
432 return;
433
434 tick_freq_mhz = arch_tick_frequency();
435
436 if (!tick_freq_mhz) {
437 fprintf(stderr, "Cannot determine timestamp tick frequency.\n");
438 exit(1);
439 }
440}
441
442u64 arch_convert_raw_ts_entry(u64 ts)
443{
444 return ts / tick_freq_mhz;
445}
446
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700447/*
448 * Print an integer in 'normalized' form - with commas separating every three
Julius Wernera7d92442014-12-02 20:51:19 -0800449 * decimal orders.
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700450 */
Julius Wernera7d92442014-12-02 20:51:19 -0800451static void print_norm(u64 v)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700452{
Julius Wernera7d92442014-12-02 20:51:19 -0800453 if (v >= 1000) {
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700454 /* print the higher order sections first */
Julius Wernera7d92442014-12-02 20:51:19 -0800455 print_norm(v / 1000);
456 printf(",%3.3u", (u32)(v % 1000));
457 } else {
458 printf("%u", (u32)(v % 1000));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700459 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700460}
461
Aaron Durbinfbff3012015-08-30 22:00:12 -0500462static const char *timestamp_name(uint32_t id)
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700463{
464 int i;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500465
466 for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) {
467 if (timestamp_ids[i].id == id)
468 return timestamp_ids[i].name;
469 }
470 return "<unknown>";
471}
472
473static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp,
474 uint64_t prev_stamp)
475{
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700476 const char *name;
Aaron Durbin799bf782015-08-06 13:52:08 -0500477 uint64_t step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700478
Aaron Durbinfbff3012015-08-30 22:00:12 -0500479 name = timestamp_name(id);
480
481 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
482
483 /* ID<tab>absolute time<tab>relative time<tab>description */
484 printf("%d\t", id);
485 printf("%llu\t", (long long)arch_convert_raw_ts_entry(stamp));
486 printf("%llu\t", (long long)step_time);
487 printf("%s\n", name);
488
489 return step_time;
490}
491
492uint64_t timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp)
493{
494 const char *name;
495 uint64_t step_time;
496
497 name = timestamp_name(id);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700498
499 printf("%4d:", id);
Julius Wernera7d92442014-12-02 20:51:19 -0800500 printf("%-50s", name);
501 print_norm(arch_convert_raw_ts_entry(stamp));
Aaron Durbin799bf782015-08-06 13:52:08 -0500502 step_time = arch_convert_raw_ts_entry(stamp - prev_stamp);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700503 if (prev_stamp) {
504 printf(" (");
Aaron Durbin799bf782015-08-06 13:52:08 -0500505 print_norm(step_time);
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700506 printf(")");
507 }
508 printf("\n");
Aaron Durbin799bf782015-08-06 13:52:08 -0500509
510 return step_time;
Stefan Reinauer0db924d2013-08-09 11:06:11 -0700511}
512
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700513/* dump the timestamp table */
Aaron Durbinfbff3012015-08-30 22:00:12 -0500514static void dump_timestamps(int mach_readable)
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700515{
516 int i;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800517 struct timestamp_table *tst_p;
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500518 size_t size;
Aaron Durbin31540fb2015-07-11 12:44:10 -0500519 uint64_t prev_stamp;
Aaron Durbin799bf782015-08-06 13:52:08 -0500520 uint64_t total_time;
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800521
522 if (timestamps.tag != LB_TAG_TIMESTAMPS) {
523 fprintf(stderr, "No timestamps found in coreboot table.\n");
524 return;
525 }
526
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500527 size = sizeof(*tst_p);
Timothy Pearsonbea71402015-09-05 18:07:17 -0500528 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700529
Aaron Durbinc49014e2015-08-30 21:19:55 -0500530 timestamp_set_tick_freq(tst_p->tick_freq_mhz);
531
Aaron Durbinfbff3012015-08-30 22:00:12 -0500532 if (!mach_readable)
533 printf("%d entries total:\n\n", tst_p->num_entries);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500534 size += tst_p->num_entries * sizeof(tst_p->entries[0]);
535
536 unmap_memory();
Timothy Pearsonbea71402015-09-05 18:07:17 -0500537 tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500538
Aaron Durbin31540fb2015-07-11 12:44:10 -0500539 /* Report the base time within the table. */
540 prev_stamp = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500541 if (mach_readable)
542 timestamp_print_parseable_entry(0, tst_p->base_time,
543 prev_stamp);
544 else
545 timestamp_print_entry(0, tst_p->base_time, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500546 prev_stamp = tst_p->base_time;
547
Aaron Durbin799bf782015-08-06 13:52:08 -0500548 total_time = 0;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700549 for (i = 0; i < tst_p->num_entries; i++) {
Aaron Durbin31540fb2015-07-11 12:44:10 -0500550 uint64_t stamp;
551 const struct timestamp_entry *tse = &tst_p->entries[i];
552
553 /* Make all timestamps absolute. */
554 stamp = tse->entry_stamp + tst_p->base_time;
Aaron Durbinfbff3012015-08-30 22:00:12 -0500555 if (mach_readable)
556 total_time +=
557 timestamp_print_parseable_entry(tse->entry_id,
558 stamp, prev_stamp);
559 else
560 total_time += timestamp_print_entry(tse->entry_id,
Aaron Durbin799bf782015-08-06 13:52:08 -0500561 stamp, prev_stamp);
Aaron Durbin31540fb2015-07-11 12:44:10 -0500562 prev_stamp = stamp;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700563 }
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800564
Aaron Durbinfbff3012015-08-30 22:00:12 -0500565 if (!mach_readable) {
566 printf("\nTotal Time: ");
567 print_norm(total_time);
568 printf("\n");
569 }
Aaron Durbin799bf782015-08-06 13:52:08 -0500570
Stefan Reinauer05cbce62013-01-03 14:30:33 -0800571 unmap_memory();
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700572}
573
Stefan Reinauer19f87562013-01-07 13:37:12 -0800574/* dump the cbmem console */
575static void dump_console(void)
576{
577 void *console_p;
578 char *console_c;
579 uint32_t size;
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100580 uint32_t cursor;
Stefan Reinauer19f87562013-01-07 13:37:12 -0800581
582 if (console.tag != LB_TAG_CBMEM_CONSOLE) {
583 fprintf(stderr, "No console found in coreboot table.\n");
584 return;
585 }
586
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500587 console_p = map_memory_size((unsigned long)console.cbmem_addr,
Timothy Pearsonbea71402015-09-05 18:07:17 -0500588 2 * sizeof(uint32_t), 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800589 /* The in-memory format of the console area is:
590 * u32 size
591 * u32 cursor
592 * char console[size]
593 * Hence we have to add 8 to get to the actual console string.
594 */
Gabe Black06b13a32013-08-09 00:40:06 -0700595 size = ((uint32_t *)console_p)[0];
596 cursor = ((uint32_t *)console_p)[1];
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100597 /* Cursor continues to go on even after no more data fits in
598 * the buffer but the data is dropped in this case.
599 */
600 if (size > cursor)
601 size = cursor;
Aaron Durbine740f482015-06-17 16:22:00 +0200602 console_c = calloc(1, size + 1);
Aaron Durbinb58f9e32014-10-07 14:56:35 -0500603 unmap_memory();
Stefan Reinauer19f87562013-01-07 13:37:12 -0800604 if (!console_c) {
605 fprintf(stderr, "Not enough memory for console.\n");
606 exit(1);
607 }
608
Aaron Durbinab180d82014-03-31 11:59:58 -0500609 console_p = map_memory_size((unsigned long)console.cbmem_addr,
Timothy Pearsonbea71402015-09-05 18:07:17 -0500610 size + sizeof(size) + sizeof(cursor), 1);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800611 memcpy(console_c, console_p + 8, size);
Stefan Reinauer19f87562013-01-07 13:37:12 -0800612
Vladimir Serbinenkof4a0d012013-03-30 12:15:12 +0100613 printf("%s\n", console_c);
614 if (size < cursor)
615 printf("%d %s lost\n", cursor - size,
616 (cursor - size) == 1 ? "byte":"bytes");
Stefan Reinauer19f87562013-01-07 13:37:12 -0800617
618 free(console_c);
619
620 unmap_memory();
621}
622
Stefan Reinauera9c83612013-07-16 17:47:35 -0700623static void hexdump(unsigned long memory, int length)
624{
625 int i;
626 uint8_t *m;
627 int all_zero = 0;
628
Timothy Pearsonbea71402015-09-05 18:07:17 -0500629 m = map_memory_size((intptr_t)memory, length, 1);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700630
631 if (length > MAP_BYTES) {
632 printf("Truncating hex dump from %d to %d bytes\n\n",
633 length, MAP_BYTES);
634 length = MAP_BYTES;
635 }
636
637 for (i = 0; i < length; i += 16) {
638 int j;
639
640 all_zero++;
641 for (j = 0; j < 16; j++) {
642 if(m[i+j] != 0) {
643 all_zero = 0;
644 break;
645 }
646 }
647
648 if (all_zero < 2) {
649 printf("%08lx:", memory + i);
650 for (j = 0; j < 16; j++)
651 printf(" %02x", m[i+j]);
652 printf(" ");
653 for (j = 0; j < 16; j++)
654 printf("%c", isprint(m[i+j]) ? m[i+j] : '.');
655 printf("\n");
656 } else if (all_zero == 2) {
657 printf("...\n");
658 }
659 }
660
661 unmap_memory();
662}
663
664static void dump_cbmem_hex(void)
665{
666 if (cbmem.type != LB_MEM_TABLE) {
667 fprintf(stderr, "No coreboot CBMEM area found!\n");
668 return;
669 }
670
671 hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size));
672}
673
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700674void rawdump(uint64_t base, uint64_t size)
675{
676 int i;
677 uint8_t *m;
678
679 m = map_memory_size((intptr_t)base, size, 1);
680 if (!m) {
681 fprintf(stderr, "Failed to map memory");
682 return;
683 }
684
685 for (i = 0 ; i < size; i++)
686 printf("%c", m[i]);
687 unmap_memory();
688}
689
690static void dump_cbmem_raw(unsigned int id)
691{
692 uint8_t *table;
693 size_t offset;
694 uint64_t base = 0;
695 uint64_t size = 0;
696
697 table = map_lbtable();
698
699 if (table == NULL)
700 return;
701
702 offset = 0;
703
704 while (offset < lbtable_size) {
705 struct lb_record *lbr;
706 struct lb_cbmem_entry *lbe;
707
708 lbr = (void *)(table + offset);
709 offset += lbr->size;
710
711 if (lbr->tag != LB_TAG_CBMEM_ENTRY)
712 continue;
713
714 lbe = (void *)lbr;
715 if (lbe->id == id) {
716 debug("found id for raw dump %0x", lbe->id);
717 base = lbe->address;
718 size = lbe->entry_size;
719 break;
720 }
721 }
722
723 unmap_lbtable();
724
725 if (!base)
726 fprintf(stderr, "id %0x not found in cbtable\n", id);
727 else
728 rawdump(base, size);
729}
730
Aaron Durbin0dff57d2015-03-05 21:18:33 -0600731struct cbmem_id_to_name {
732 uint32_t id;
733 const char *name;
734};
Vadim Bendebury8b143c52014-05-14 10:12:55 -0700735static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE };
Stefan Reinauerc0199072013-01-07 16:26:10 -0800736
Stefan Reinauera9c83612013-07-16 17:47:35 -0700737void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size)
738{
739 int i;
740 const char *name;
741
742 name = NULL;
743 for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) {
744 if (cbmem_ids[i].id == id) {
745 name = cbmem_ids[i].name;
746 break;
747 }
748 }
749
750 printf("%2d. ", n);
751 if (name == NULL)
752 printf("%08x ", id);
753 else
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700754 printf("%s\t%08x", name, id);
Stefan Reinauera9c83612013-07-16 17:47:35 -0700755 printf(" %08" PRIx64 " ", base);
756 printf(" %08" PRIx64 "\n", size);
757}
758
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800759static void dump_cbmem_toc(void)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800760{
Aaron Durbin09c0c112015-09-30 12:33:01 -0500761 int i;
762 uint8_t *table;
763 size_t offset;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800764
Aaron Durbin09c0c112015-09-30 12:33:01 -0500765 table = map_lbtable();
766
767 if (table == NULL)
Stefan Reinauerc0199072013-01-07 16:26:10 -0800768 return;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500769
770 printf("CBMEM table of contents:\n");
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700771 printf(" NAME ID START LENGTH\n");
Aaron Durbin09c0c112015-09-30 12:33:01 -0500772
773 i = 0;
774 offset = 0;
775
776 while (offset < lbtable_size) {
777 struct lb_record *lbr;
778 struct lb_cbmem_entry *lbe;
779
780 lbr = (void *)(table + offset);
781 offset += lbr->size;
782
783 if (lbr->tag != LB_TAG_CBMEM_ENTRY)
784 continue;
785
786 lbe = (void *)lbr;
787 cbmem_print_entry(i, lbe->id, lbe->address, lbe->entry_size);
788 i++;
Stefan Reinauerc0199072013-01-07 16:26:10 -0800789 }
790
Aaron Durbin09c0c112015-09-30 12:33:01 -0500791 unmap_lbtable();
Stefan Reinauerc0199072013-01-07 16:26:10 -0800792}
Stefan Reinauer19f87562013-01-07 13:37:12 -0800793
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800794#define COVERAGE_MAGIC 0x584d4153
795struct file {
796 uint32_t magic;
797 uint32_t next;
798 uint32_t filename;
799 uint32_t data;
800 int offset;
801 int len;
802};
803
804static int mkpath(char *path, mode_t mode)
805{
806 assert (path && *path);
807 char *p;
808 for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) {
809 *p = '\0';
810 if (mkdir(path, mode) == -1) {
811 if (errno != EEXIST) {
812 *p = '/';
813 return -1;
814 }
815 }
816 *p = '/';
817 }
818 return 0;
819}
820
821static void dump_coverage(void)
822{
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800823 uint64_t start;
Aaron Durbin09c0c112015-09-30 12:33:01 -0500824 size_t size;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800825 void *coverage;
826 unsigned long phys_offset;
827#define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset)
828
Aaron Durbin09c0c112015-09-30 12:33:01 -0500829 if (find_cbmem_entry(CBMEM_ID_COVERAGE, &start, &size)) {
830 fprintf(stderr, "No coverage information found\n");
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800831 return;
832 }
833
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800834 /* Map coverage area */
Aaron Durbin09c0c112015-09-30 12:33:01 -0500835 coverage = map_memory_size(start, size, 1);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800836 phys_offset = (unsigned long)coverage - (unsigned long)start;
837
838 printf("Dumping coverage data...\n");
839
840 struct file *file = (struct file *)coverage;
841 while (file && file->magic == COVERAGE_MAGIC) {
842 FILE *f;
843 char *filename;
844
845 debug(" -> %s\n", (char *)phys_to_virt(file->filename));
846 filename = strdup((char *)phys_to_virt(file->filename));
847 if (mkpath(filename, 0755) == -1) {
848 perror("Directory for coverage data could "
849 "not be created");
850 exit(1);
851 }
852 f = fopen(filename, "wb");
853 if (!f) {
854 printf("Could not open %s: %s\n",
855 filename, strerror(errno));
856 exit(1);
857 }
858 if (fwrite((void *)phys_to_virt(file->data),
859 file->len, 1, f) != 1) {
860 printf("Could not write to %s: %s\n",
861 filename, strerror(errno));
862 exit(1);
863 }
864 fclose(f);
865 free(filename);
866
867 if (file->next)
868 file = (struct file *)phys_to_virt(file->next);
869 else
870 file = NULL;
871 }
872 unmap_memory();
873}
874
875static void print_version(void)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800876{
877 printf("cbmem v%s -- ", CBMEM_VERSION);
878 printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n");
879 printf(
880 "This program is free software: you can redistribute it and/or modify\n"
881 "it under the terms of the GNU General Public License as published by\n"
882 "the Free Software Foundation, version 2 of the License.\n\n"
883 "This program is distributed in the hope that it will be useful,\n"
884 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
885 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
Martin Rotheb20e602016-01-12 13:30:50 -0700886 "GNU General Public License for more details.\n\n");
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800887}
888
Martin Roth8448ac42016-09-11 15:43:22 -0600889static void print_usage(const char *name, int exit_code)
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800890{
Aaron Durbinfbff3012015-08-30 22:00:12 -0500891 printf("usage: %s [-cCltTxVvh?]\n", name);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800892 printf("\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800893 " -c | --console: print cbmem console\n"
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800894 " -C | --coverage: dump coverage information\n"
Stefan Reinauerc0199072013-01-07 16:26:10 -0800895 " -l | --list: print cbmem table of contents\n"
Stefan Reinauera9c83612013-07-16 17:47:35 -0700896 " -x | --hexdump: print hexdump of cbmem area\n"
Pratik Prajapatic29e57d2015-09-03 12:58:44 -0700897 " -r | --rawdump ID: print rawdump of specific ID (in hex) of cbtable\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800898 " -t | --timestamps: print timestamp information\n"
Aaron Durbinfbff3012015-08-30 22:00:12 -0500899 " -T | --parseable-timestamps: print parseable timestamps\n"
Stefan Reinauer19f87562013-01-07 13:37:12 -0800900 " -V | --verbose: verbose (debugging) output\n"
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800901 " -v | --version: print the version\n"
902 " -h | --help: print this help\n"
903 "\n");
Martin Roth8448ac42016-09-11 15:43:22 -0600904 exit(exit_code);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -0800905}
Vadim Bendebury6d18fd02012-09-27 19:24:07 -0700906
Julius Werner337de4c2014-06-16 23:02:03 -0700907#ifdef __arm__
908static void dt_update_cells(const char *name, int *addr_cells_ptr,
909 int *size_cells_ptr)
910{
911 if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0)
912 return;
913
914 int buffer;
915 size_t nlen = strlen(name);
916 char *prop = alloca(nlen + sizeof("/#address-cells"));
917 strcpy(prop, name);
918
919 if (*addr_cells_ptr < 0) {
920 strcpy(prop + nlen, "/#address-cells");
921 int fd = open(prop, O_RDONLY);
922 if (fd < 0 && errno != ENOENT) {
923 perror(prop);
924 } else if (fd >= 0) {
925 if (read(fd, &buffer, sizeof(int)) < 0)
926 perror(prop);
927 else
928 *addr_cells_ptr = ntohl(buffer);
929 close(fd);
930 }
931 }
932
933 if (*size_cells_ptr < 0) {
934 strcpy(prop + nlen, "/#size-cells");
935 int fd = open(prop, O_RDONLY);
936 if (fd < 0 && errno != ENOENT) {
937 perror(prop);
938 } else if (fd >= 0) {
939 if (read(fd, &buffer, sizeof(int)) < 0)
940 perror(prop);
941 else
942 *size_cells_ptr = ntohl(buffer);
943 close(fd);
944 }
945 }
946}
947
948static char *dt_find_compat(const char *parent, const char *compat,
949 int *addr_cells_ptr, int *size_cells_ptr)
950{
951 char *ret = NULL;
952 struct dirent *entry;
953 DIR *dir;
954
955 if (!(dir = opendir(parent))) {
956 perror(parent);
957 return NULL;
958 }
959
960 /* Loop through all files in the directory (DT node). */
961 while ((entry = readdir(dir))) {
962 /* We only care about compatible props or subnodes. */
963 if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) ||
964 !strcmp(entry->d_name, "compatible")))
965 continue;
966
967 /* Assemble the file name (on the stack, for speed). */
968 size_t plen = strlen(parent);
969 char *name = alloca(plen + strlen(entry->d_name) + 2);
970
971 strcpy(name, parent);
972 name[plen] = '/';
973 strcpy(name + plen + 1, entry->d_name);
974
975 /* If it's a subnode, recurse. */
976 if (entry->d_type & DT_DIR) {
977 ret = dt_find_compat(name, compat, addr_cells_ptr,
978 size_cells_ptr);
979
980 /* There is only one matching node to find, abort. */
981 if (ret) {
982 /* Gather cells values on the way up. */
983 dt_update_cells(parent, addr_cells_ptr,
984 size_cells_ptr);
985 break;
986 }
987 continue;
988 }
989
990 /* If it's a compatible string, see if it's the right one. */
991 int fd = open(name, O_RDONLY);
992 int clen = strlen(compat);
993 char *buffer = alloca(clen + 1);
994
995 if (fd < 0) {
996 perror(name);
997 continue;
998 }
999
1000 if (read(fd, buffer, clen + 1) < 0) {
1001 perror(name);
1002 close(fd);
1003 continue;
1004 }
1005 close(fd);
1006
1007 if (!strcmp(compat, buffer)) {
1008 /* Initialize these to "unset" for the way up. */
1009 *addr_cells_ptr = *size_cells_ptr = -1;
1010
1011 /* Can't leave string on the stack or we'll lose it! */
1012 ret = strdup(parent);
1013 break;
1014 }
1015 }
1016
1017 closedir(dir);
1018 return ret;
1019}
1020#endif /* __arm__ */
1021
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001022int main(int argc, char** argv)
1023{
Stefan Reinauer19f87562013-01-07 13:37:12 -08001024 int print_defaults = 1;
1025 int print_console = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001026 int print_coverage = 0;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001027 int print_list = 0;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001028 int print_hexdump = 0;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001029 int print_rawdump = 0;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001030 int print_timestamps = 0;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001031 int machine_readable_timestamps = 0;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001032 unsigned int rawdump_id = 0;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001033
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001034 int opt, option_index = 0;
1035 static struct option long_options[] = {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001036 {"console", 0, 0, 'c'},
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001037 {"coverage", 0, 0, 'C'},
Stefan Reinauerc0199072013-01-07 16:26:10 -08001038 {"list", 0, 0, 'l'},
Stefan Reinauer19f87562013-01-07 13:37:12 -08001039 {"timestamps", 0, 0, 't'},
Aaron Durbinfbff3012015-08-30 22:00:12 -05001040 {"parseable-timestamps", 0, 0, 'T'},
Stefan Reinauera9c83612013-07-16 17:47:35 -07001041 {"hexdump", 0, 0, 'x'},
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001042 {"rawdump", required_argument, 0, 'r'},
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001043 {"verbose", 0, 0, 'V'},
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001044 {"version", 0, 0, 'v'},
1045 {"help", 0, 0, 'h'},
1046 {0, 0, 0, 0}
1047 };
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001048 while ((opt = getopt_long(argc, argv, "cCltTxVvh?r:",
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001049 long_options, &option_index)) != EOF) {
1050 switch (opt) {
Stefan Reinauer19f87562013-01-07 13:37:12 -08001051 case 'c':
1052 print_console = 1;
1053 print_defaults = 0;
1054 break;
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001055 case 'C':
1056 print_coverage = 1;
1057 print_defaults = 0;
1058 break;
Stefan Reinauerc0199072013-01-07 16:26:10 -08001059 case 'l':
1060 print_list = 1;
1061 print_defaults = 0;
1062 break;
Stefan Reinauera9c83612013-07-16 17:47:35 -07001063 case 'x':
1064 print_hexdump = 1;
1065 print_defaults = 0;
1066 break;
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001067 case 'r':
1068 print_rawdump = 1;
1069 print_defaults = 0;
1070 rawdump_id = strtoul(optarg, NULL, 16);
1071 break;
Stefan Reinauer19f87562013-01-07 13:37:12 -08001072 case 't':
1073 print_timestamps = 1;
1074 print_defaults = 0;
1075 break;
Aaron Durbinfbff3012015-08-30 22:00:12 -05001076 case 'T':
1077 print_timestamps = 1;
1078 machine_readable_timestamps = 1;
1079 print_defaults = 0;
1080 break;
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001081 case 'V':
1082 verbose = 1;
1083 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001084 case 'v':
1085 print_version();
1086 exit(0);
1087 break;
1088 case 'h':
Martin Roth8448ac42016-09-11 15:43:22 -06001089 print_usage(argv[0], 0);
1090 break;
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001091 case '?':
1092 default:
Martin Roth8448ac42016-09-11 15:43:22 -06001093 print_usage(argv[0], 1);
Stefan Reinauer1e0e5562013-01-02 15:43:56 -08001094 break;
1095 }
1096 }
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001097
Julius Werner337de4c2014-06-16 23:02:03 -07001098 mem_fd = open("/dev/mem", O_RDONLY, 0);
1099 if (mem_fd < 0) {
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001100 fprintf(stderr, "Failed to gain memory access: %s\n",
1101 strerror(errno));
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001102 return 1;
1103 }
1104
Stefan Reinauer7f681502013-06-19 15:39:09 -07001105#ifdef __arm__
Julius Werner337de4c2014-06-16 23:02:03 -07001106 int addr_cells, size_cells;
1107 char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot",
1108 &addr_cells, &size_cells);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001109
Julius Werner337de4c2014-06-16 23:02:03 -07001110 if (!coreboot_node) {
1111 fprintf(stderr, "Could not find 'coreboot' compatible node!\n");
Stefan Reinauer7f681502013-06-19 15:39:09 -07001112 return 1;
1113 }
1114
Julius Werner337de4c2014-06-16 23:02:03 -07001115 if (addr_cells < 0) {
1116 fprintf(stderr, "Warning: no #address-cells node in tree!\n");
1117 addr_cells = 1;
1118 }
1119
1120 int nlen = strlen(coreboot_node);
1121 char *reg = alloca(nlen + sizeof("/reg"));
1122
1123 strcpy(reg, coreboot_node);
1124 strcpy(reg + nlen, "/reg");
1125 free(coreboot_node);
1126
1127 int fd = open(reg, O_RDONLY);
1128 if (fd < 0) {
1129 perror(reg);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001130 return 1;
1131 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001132
Julius Werner337de4c2014-06-16 23:02:03 -07001133 int i;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001134 size_t size_to_read = addr_cells * 4 + size_cells * 4;
1135 u8 *dtbuffer = alloca(size_to_read);
1136 if (read(fd, dtbuffer, size_to_read) < 0) {
Julius Werner337de4c2014-06-16 23:02:03 -07001137 perror(reg);
1138 return 1;
1139 }
1140 close(fd);
1141
1142 /* No variable-length byte swap function anywhere in C... how sad. */
1143 u64 baseaddr = 0;
1144 for (i = 0; i < addr_cells * 4; i++) {
1145 baseaddr <<= 8;
Aaron Durbinb58f9e32014-10-07 14:56:35 -05001146 baseaddr |= *dtbuffer;
1147 dtbuffer++;
1148 }
1149 u64 cb_table_size = 0;
1150 for (i = 0; i < size_cells * 4; i++) {
1151 cb_table_size <<= 8;
1152 cb_table_size |= *dtbuffer;
1153 dtbuffer++;
Julius Werner337de4c2014-06-16 23:02:03 -07001154 }
1155
Timothy Pearsonbea71402015-09-05 18:07:17 -05001156 parse_cbtable(baseaddr, cb_table_size, 1);
Stefan Reinauer7f681502013-06-19 15:39:09 -07001157#else
1158 int j;
1159 static const int possible_base_addresses[] = { 0, 0xf0000 };
1160
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001161 /* Find and parse coreboot table */
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001162 for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) {
Timothy Pearsonbea71402015-09-05 18:07:17 -05001163 if (parse_cbtable(possible_base_addresses[j], MAP_BYTES, 1))
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001164 break;
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001165 }
Stefan Reinauer7f681502013-06-19 15:39:09 -07001166#endif
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001167
Stefan Reinauer19f87562013-01-07 13:37:12 -08001168 if (print_console)
1169 dump_console();
1170
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001171 if (print_coverage)
1172 dump_coverage();
1173
Stefan Reinauerc0199072013-01-07 16:26:10 -08001174 if (print_list)
1175 dump_cbmem_toc();
1176
Stefan Reinauera9c83612013-07-16 17:47:35 -07001177 if (print_hexdump)
1178 dump_cbmem_hex();
1179
Pratik Prajapatic29e57d2015-09-03 12:58:44 -07001180 if (print_rawdump)
1181 dump_cbmem_raw(rawdump_id);
1182
Stefan Reinauer19f87562013-01-07 13:37:12 -08001183 if (print_defaults || print_timestamps)
Aaron Durbinfbff3012015-08-30 22:00:12 -05001184 dump_timestamps(machine_readable_timestamps);
Stefan Reinauer05cbce62013-01-03 14:30:33 -08001185
Julius Werner337de4c2014-06-16 23:02:03 -07001186 close(mem_fd);
Vadim Bendebury6d18fd02012-09-27 19:24:07 -07001187 return 0;
1188}