Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 4 | * Copyright 2012 Google Inc. |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 5 | * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 6 | * |
| 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 Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 18 | * Foundation, Inc. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Nico Huber | 8e4bb928 | 2013-05-26 18:17:54 +0200 | [diff] [blame] | 21 | #include <inttypes.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 26 | #include <inttypes.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 27 | #include <getopt.h> |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 28 | #include <dirent.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 31 | #include <ctype.h> |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 32 | #include <arpa/inet.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/mman.h> |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 36 | #include <libgen.h> |
| 37 | #include <assert.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 38 | #include <commonlib/cbmem_id.h> |
| 39 | #include <commonlib/timestamp_serialized.h> |
| 40 | #include <commonlib/coreboot_tables.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 41 | |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 42 | #ifdef __OpenBSD__ |
| 43 | #include <sys/param.h> |
| 44 | #include <sys/sysctl.h> |
| 45 | #endif |
| 46 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 47 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 48 | #define MAP_BYTES (1024*1024) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 49 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 50 | typedef uint8_t u8; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 51 | typedef uint16_t u16; |
| 52 | typedef uint32_t u32; |
| 53 | typedef uint64_t u64; |
| 54 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 55 | #define CBMEM_VERSION "1.1" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 56 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 57 | /* verbose output? */ |
| 58 | static int verbose = 0; |
| 59 | #define debug(x...) if(verbose) printf(x) |
| 60 | |
| 61 | /* File handle used to access /dev/mem */ |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 62 | static int mem_fd; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 63 | |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 64 | /* IMD root pointer location */ |
| 65 | static uint64_t rootptr = 0; |
| 66 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 67 | /* |
| 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 | */ |
| 71 | static 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 Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 87 | * 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 89 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 90 | static void *mapped_virtual; |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 91 | static size_t mapped_size; |
| 92 | |
| 93 | static inline size_t size_to_mib(size_t sz) |
| 94 | { |
| 95 | return sz >> 20; |
| 96 | } |
| 97 | |
| 98 | static void unmap_memory(void) |
| 99 | { |
| 100 | if (mapped_virtual == NULL) { |
| 101 | fprintf(stderr, "Error unmapping memory\n"); |
| 102 | return; |
| 103 | } |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 104 | 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 Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 107 | } else { |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 108 | debug("Unmapping %zuMB of virtual memory at %p.\n", |
| 109 | size_to_mib(mapped_size), mapped_virtual); |
| 110 | } |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 111 | munmap(mapped_virtual, mapped_size); |
| 112 | mapped_virtual = NULL; |
| 113 | mapped_size = 0; |
| 114 | } |
| 115 | |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 116 | static void *map_memory_size(u64 physical, size_t size, uint8_t abort_on_failure) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 117 | { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 118 | void *v; |
| 119 | off_t p; |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 120 | u64 page = getpagesize(); |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 121 | size_t padding; |
| 122 | |
| 123 | if (mapped_virtual != NULL) |
| 124 | unmap_memory(); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 125 | |
| 126 | /* Mapped memory must be aligned to page size */ |
| 127 | p = physical & ~(page - 1); |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 128 | padding = physical & (page-1); |
| 129 | size += padding; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 130 | |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 131 | 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 Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 134 | } else { |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 135 | 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 Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 138 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 139 | v = mmap(NULL, size, PROT_READ, MAP_SHARED, mem_fd, p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 140 | |
| 141 | if (v == MAP_FAILED) { |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 142 | /* 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 Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 155 | 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 162 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 163 | |
| 164 | /* Remember what we actually mapped ... */ |
| 165 | mapped_virtual = v; |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 166 | mapped_size = size; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 167 | |
| 168 | /* ... but return address to the physical memory that was requested */ |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 169 | if (padding) |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 170 | debug(" ... padding virtual address with 0x%zx bytes.\n", |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 171 | padding); |
| 172 | v += padding; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 173 | |
| 174 | return v; |
| 175 | } |
| 176 | |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 177 | static void *map_memory(u64 physical) |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 178 | { |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 179 | return map_memory_size(physical, MAP_BYTES, 1); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 183 | * 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 186 | * |
| 187 | * Returns pointer to a memory buffer containg the timestamp table or zero if |
| 188 | * none found. |
| 189 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 190 | |
| 191 | static struct lb_cbmem_ref timestamps; |
| 192 | static struct lb_cbmem_ref console; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 193 | static struct lb_memory_range cbmem; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 194 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 195 | /* 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 | */ |
| 202 | static 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 Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 211 | debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr); |
| 212 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 216 | static int parse_cbtable(u64 address, size_t table_size, uint8_t abort_on_failure) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 217 | { |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 218 | int i, found, ret = 0; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 219 | void *buf; |
| 220 | |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 221 | debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n", |
| 222 | address, table_size); |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 223 | buf = map_memory_size(address, table_size, abort_on_failure); |
| 224 | if (!buf) |
| 225 | return -2; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 226 | |
| 227 | /* look at every 16 bytes within 4K of the base */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 228 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 229 | for (i = 0; i < 0x1000; i += 0x10) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 230 | struct lb_header *lbh; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 231 | struct lb_record* lbr_p; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 232 | void *lbtable; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 233 | int j; |
| 234 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 235 | 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 239 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 240 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 241 | lbtable = buf + i + lbh->header_bytes; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 242 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 243 | if (ipchcksum(lbtable, lbh->table_bytes) != |
| 244 | lbh->table_checksum) { |
| 245 | debug("Signature found, but wrong checksum.\n"); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 246 | continue; |
| 247 | } |
| 248 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 249 | found = 1; |
| 250 | debug("Found!\n"); |
| 251 | |
| 252 | for (j = 0; j < lbh->table_bytes; j += lbr_p->size) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 253 | lbr_p = (struct lb_record*) ((char *)lbtable + j); |
| 254 | debug(" coreboot table entry 0x%02x\n", lbr_p->tag); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 255 | switch (lbr_p->tag) { |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 256 | 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 Menzel | 747c07f | 2014-10-17 13:46:12 +0200 | [diff] [blame] | 261 | while ((char *)&memory->map[i] < ((char *)lbr_p |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 262 | + 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 272 | case LB_TAG_TIMESTAMPS: { |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 273 | debug(" Found timestamp table.\n"); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 274 | timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 275 | continue; |
| 276 | } |
| 277 | case LB_TAG_CBMEM_CONSOLE: { |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 278 | debug(" Found cbmem console.\n"); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 279 | console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 280 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 281 | } |
| 282 | case LB_TAG_FORWARD: { |
| 283 | /* |
| 284 | * This is a forwarding entry - repeat the |
| 285 | * search at the new address. |
| 286 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 287 | struct lb_forward lbf_p = |
| 288 | *(struct lb_forward *) lbr_p; |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 289 | debug(" Found forwarding entry.\n"); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 290 | unmap_memory(); |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 291 | 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 302 | } |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 309 | unmap_memory(); |
| 310 | |
| 311 | return found; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 314 | #if defined(linux) && (defined(__i386__) || defined(__x86_64__)) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 315 | /* |
| 316 | * read CPU frequency from a sysfs file, return an frequency in Kilohertz as |
| 317 | * an int or exit on any error. |
| 318 | */ |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 319 | static unsigned long arch_tick_frequency(void) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 320 | { |
| 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 Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 332 | fprintf(stderr, "Could not open %s: %s\n", |
| 333 | freq_file, strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 334 | 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 Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 353 | #elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__)) |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 354 | static unsigned long arch_tick_frequency(void) |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 355 | { |
| 356 | int mib[2] = { CTL_HW, HW_CPUSPEED }; |
| 357 | static int value = 0; |
| 358 | size_t value_len = sizeof(value); |
| 359 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 360 | /* Return 1 MHz when sysctl fails. */ |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 361 | if ((value == 0) && (sysctl(mib, 2, &value, &value_len, NULL, 0) == -1)) |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 362 | return 1; |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 363 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 364 | return value; |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 365 | } |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 366 | #else |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 367 | static unsigned long arch_tick_frequency(void) |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 368 | { |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 369 | /* 1 MHz = 1us. */ |
| 370 | return 1; |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 371 | } |
| 372 | #endif |
| 373 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 374 | static unsigned long tick_freq_mhz; |
| 375 | |
| 376 | static 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 | |
| 392 | u64 arch_convert_raw_ts_entry(u64 ts) |
| 393 | { |
| 394 | return ts / tick_freq_mhz; |
| 395 | } |
| 396 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 397 | /* |
| 398 | * Print an integer in 'normalized' form - with commas separating every three |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 399 | * decimal orders. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 400 | */ |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 401 | static void print_norm(u64 v) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 402 | { |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 403 | if (v >= 1000) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 404 | /* print the higher order sections first */ |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 405 | print_norm(v / 1000); |
| 406 | printf(",%3.3u", (u32)(v % 1000)); |
| 407 | } else { |
| 408 | printf("%u", (u32)(v % 1000)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 409 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 412 | enum 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 Nematbakhsh | 5ece96a | 2015-10-27 13:53:02 -0700 | [diff] [blame^] | 425 | TS_VB_EC_VBOOT_DONE = 1030, |
| 426 | |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 427 | TS_CROSSYSTEM_DATA = 1100, |
| 428 | TS_START_KERNEL = 1101 |
| 429 | }; |
| 430 | |
| 431 | static const struct timestamp_id_to_name { |
| 432 | u32 id; |
| 433 | const char *name; |
| 434 | } timestamp_ids[] = { |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 435 | /* Marker to report base_time. */ |
| 436 | { 0, "1st timestamp" }, |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 437 | { 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 Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 443 | { TS_START_COPYRAM, "starting to load ramstage" }, |
| 444 | { TS_END_COPYRAM, "finished loading ramstage" }, |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 445 | { TS_START_RAMSTAGE, "start of ramstage" }, |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 446 | { 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 Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 452 | { 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 Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 462 | |
| 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 Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 474 | { 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 Nematbakhsh | 5ece96a | 2015-10-27 13:53:02 -0700 | [diff] [blame^] | 481 | { TS_VB_EC_VBOOT_DONE, "finished EC verification" }, |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 482 | { TS_CROSSYSTEM_DATA, "crossystem data" }, |
Lee Leahy | b817908 | 2015-02-24 11:30:38 -0800 | [diff] [blame] | 483 | { 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 Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 497 | }; |
| 498 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 499 | static const char *timestamp_name(uint32_t id) |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 500 | { |
| 501 | int i; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 502 | |
| 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 | |
| 510 | static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp, |
| 511 | uint64_t prev_stamp) |
| 512 | { |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 513 | const char *name; |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 514 | uint64_t step_time; |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 515 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 516 | 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 | |
| 529 | uint64_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 Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 535 | |
| 536 | printf("%4d:", id); |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 537 | printf("%-50s", name); |
| 538 | print_norm(arch_convert_raw_ts_entry(stamp)); |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 539 | step_time = arch_convert_raw_ts_entry(stamp - prev_stamp); |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 540 | if (prev_stamp) { |
| 541 | printf(" ("); |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 542 | print_norm(step_time); |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 543 | printf(")"); |
| 544 | } |
| 545 | printf("\n"); |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 546 | |
| 547 | return step_time; |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 550 | /* dump the timestamp table */ |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 551 | static void dump_timestamps(int mach_readable) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 552 | { |
| 553 | int i; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 554 | struct timestamp_table *tst_p; |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 555 | size_t size; |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 556 | uint64_t prev_stamp; |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 557 | uint64_t total_time; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 558 | |
| 559 | if (timestamps.tag != LB_TAG_TIMESTAMPS) { |
| 560 | fprintf(stderr, "No timestamps found in coreboot table.\n"); |
| 561 | return; |
| 562 | } |
| 563 | |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 564 | size = sizeof(*tst_p); |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 565 | tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 566 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 567 | timestamp_set_tick_freq(tst_p->tick_freq_mhz); |
| 568 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 569 | if (!mach_readable) |
| 570 | printf("%d entries total:\n\n", tst_p->num_entries); |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 571 | size += tst_p->num_entries * sizeof(tst_p->entries[0]); |
| 572 | |
| 573 | unmap_memory(); |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 574 | tst_p = map_memory_size((unsigned long)timestamps.cbmem_addr, size, 1); |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 575 | |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 576 | /* Report the base time within the table. */ |
| 577 | prev_stamp = 0; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 578 | 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 Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 583 | prev_stamp = tst_p->base_time; |
| 584 | |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 585 | total_time = 0; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 586 | for (i = 0; i < tst_p->num_entries; i++) { |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 587 | 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 Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 592 | 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 Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 598 | stamp, prev_stamp); |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 599 | prev_stamp = stamp; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 600 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 601 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 602 | if (!mach_readable) { |
| 603 | printf("\nTotal Time: "); |
| 604 | print_norm(total_time); |
| 605 | printf("\n"); |
| 606 | } |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 607 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 608 | unmap_memory(); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 611 | /* dump the cbmem console */ |
| 612 | static void dump_console(void) |
| 613 | { |
| 614 | void *console_p; |
| 615 | char *console_c; |
| 616 | uint32_t size; |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 617 | uint32_t cursor; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 618 | |
| 619 | if (console.tag != LB_TAG_CBMEM_CONSOLE) { |
| 620 | fprintf(stderr, "No console found in coreboot table.\n"); |
| 621 | return; |
| 622 | } |
| 623 | |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 624 | console_p = map_memory_size((unsigned long)console.cbmem_addr, |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 625 | 2 * sizeof(uint32_t), 1); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 626 | /* 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 Black | 06b13a3 | 2013-08-09 00:40:06 -0700 | [diff] [blame] | 632 | size = ((uint32_t *)console_p)[0]; |
| 633 | cursor = ((uint32_t *)console_p)[1]; |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 634 | /* 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 Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 639 | console_c = malloc(size + 1); |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 640 | unmap_memory(); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 641 | if (!console_c) { |
| 642 | fprintf(stderr, "Not enough memory for console.\n"); |
| 643 | exit(1); |
| 644 | } |
| 645 | |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 646 | console_p = map_memory_size((unsigned long)console.cbmem_addr, |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 647 | size + sizeof(size) + sizeof(cursor), 1); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 648 | memcpy(console_c, console_p + 8, size); |
| 649 | console_c[size] = 0; |
Gabe Black | 06b13a3 | 2013-08-09 00:40:06 -0700 | [diff] [blame] | 650 | console_c[cursor] = 0; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 651 | |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 652 | printf("%s\n", console_c); |
| 653 | if (size < cursor) |
| 654 | printf("%d %s lost\n", cursor - size, |
| 655 | (cursor - size) == 1 ? "byte":"bytes"); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 656 | |
| 657 | free(console_c); |
| 658 | |
| 659 | unmap_memory(); |
| 660 | } |
| 661 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 662 | static void hexdump(unsigned long memory, int length) |
| 663 | { |
| 664 | int i; |
| 665 | uint8_t *m; |
| 666 | int all_zero = 0; |
| 667 | |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 668 | m = map_memory_size((intptr_t)memory, length, 1); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 669 | |
| 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 | |
| 703 | static 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 Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 716 | #define CBMEM_POINTER_MAGIC 0xc0389481 |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 717 | #define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC) |
| 718 | |
| 719 | struct cbmem_root_pointer { |
| 720 | uint32_t magic; |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 721 | /* Relative to upper limit/offset. */ |
| 722 | int32_t root_offset; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 723 | } __attribute__((packed)); |
| 724 | |
| 725 | struct dynamic_cbmem_entry { |
| 726 | uint32_t magic; |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 727 | int32_t start_offset; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 728 | uint32_t size; |
| 729 | uint32_t id; |
| 730 | } __attribute__((packed)); |
| 731 | |
| 732 | struct cbmem_root { |
| 733 | uint32_t max_entries; |
| 734 | uint32_t num_entries; |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 735 | uint32_t flags; |
| 736 | uint32_t entry_align; |
| 737 | int32_t max_offset; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 738 | struct dynamic_cbmem_entry entries[0]; |
| 739 | } __attribute__((packed)); |
| 740 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 741 | #define CBMEM_MAGIC 0x434f5245 |
| 742 | #define MAX_CBMEM_ENTRIES 16 |
| 743 | |
| 744 | struct cbmem_entry { |
| 745 | uint32_t magic; |
| 746 | uint32_t id; |
| 747 | uint64_t base; |
| 748 | uint64_t size; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 749 | } __attribute__((packed)); |
| 750 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 751 | struct cbmem_id_to_name { |
| 752 | uint32_t id; |
| 753 | const char *name; |
| 754 | }; |
Vadim Bendebury | 8b143c5 | 2014-05-14 10:12:55 -0700 | [diff] [blame] | 755 | static const struct cbmem_id_to_name cbmem_ids[] = { CBMEM_ID_TO_NAME_TABLE }; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 756 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 757 | void 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 | |
| 779 | static 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 | |
| 794 | static void dump_dynamic_cbmem_toc(struct cbmem_root *root) |
| 795 | { |
| 796 | int i; |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 797 | 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 Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 799 | |
| 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 Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 807 | rootptr + root->entries[i].start_offset, root->entries[i].size); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 811 | static void dump_cbmem_toc(void) |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 812 | { |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 813 | uint64_t start; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 814 | void *cbmem_area; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 815 | struct cbmem_entry *entries; |
| 816 | |
| 817 | if (cbmem.type != LB_MEM_TABLE) { |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 818 | fprintf(stderr, "No coreboot CBMEM area found!\n"); |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
| 822 | start = unpack_lb64(cbmem.start); |
| 823 | |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 824 | cbmem_area = map_memory_size(start, unpack_lb64(cbmem.size), 1); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 825 | entries = (struct cbmem_entry *)cbmem_area; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 826 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 827 | if (entries[0].magic == CBMEM_MAGIC) { |
| 828 | dump_static_cbmem_toc(entries); |
| 829 | } else { |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 830 | 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 Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 835 | map_memory_size(rootptr, sizeof(*r), 1); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 836 | if (r->magic == CBMEM_POINTER_MAGIC) { |
| 837 | struct cbmem_root *root; |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 838 | uint64_t rootaddr = rootptr + r->root_offset; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 839 | unmap_memory(); |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 840 | root = map_memory_size(rootaddr, ROOT_MIN_SIZE, 1); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 841 | dump_dynamic_cbmem_toc(root); |
| 842 | } else |
| 843 | fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n"); |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 844 | } |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 845 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 846 | unmap_memory(); |
| 847 | } |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 848 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 849 | #define COVERAGE_MAGIC 0x584d4153 |
| 850 | struct 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 | |
| 859 | static 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 | |
| 876 | static 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 | |
| 953 | static void print_version(void) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 954 | { |
| 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 Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 969 | static void print_usage(const char *name) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 970 | { |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 971 | printf("usage: %s [-cCltTxVvh?]\n", name); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 972 | printf("\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 973 | " -c | --console: print cbmem console\n" |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 974 | " -C | --coverage: dump coverage information\n" |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 975 | " -l | --list: print cbmem table of contents\n" |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 976 | " -x | --hexdump: print hexdump of cbmem area\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 977 | " -t | --timestamps: print timestamp information\n" |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 978 | " -T | --parseable-timestamps: print parseable timestamps\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 979 | " -V | --verbose: verbose (debugging) output\n" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 980 | " -v | --version: print the version\n" |
| 981 | " -h | --help: print this help\n" |
| 982 | "\n"); |
| 983 | exit(1); |
| 984 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 985 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 986 | #ifdef __arm__ |
| 987 | static 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 | |
| 1027 | static 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1101 | int main(int argc, char** argv) |
| 1102 | { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1103 | int print_defaults = 1; |
| 1104 | int print_console = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1105 | int print_coverage = 0; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1106 | int print_list = 0; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1107 | int print_hexdump = 0; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1108 | int print_timestamps = 0; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1109 | int machine_readable_timestamps = 0; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1110 | |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1111 | int opt, option_index = 0; |
| 1112 | static struct option long_options[] = { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1113 | {"console", 0, 0, 'c'}, |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1114 | {"coverage", 0, 0, 'C'}, |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1115 | {"list", 0, 0, 'l'}, |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1116 | {"timestamps", 0, 0, 't'}, |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1117 | {"parseable-timestamps", 0, 0, 'T'}, |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1118 | {"hexdump", 0, 0, 'x'}, |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1119 | {"verbose", 0, 0, 'V'}, |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1120 | {"version", 0, 0, 'v'}, |
| 1121 | {"help", 0, 0, 'h'}, |
| 1122 | {0, 0, 0, 0} |
| 1123 | }; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1124 | while ((opt = getopt_long(argc, argv, "cCltTxVvh?", |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1125 | long_options, &option_index)) != EOF) { |
| 1126 | switch (opt) { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1127 | case 'c': |
| 1128 | print_console = 1; |
| 1129 | print_defaults = 0; |
| 1130 | break; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1131 | case 'C': |
| 1132 | print_coverage = 1; |
| 1133 | print_defaults = 0; |
| 1134 | break; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1135 | case 'l': |
| 1136 | print_list = 1; |
| 1137 | print_defaults = 0; |
| 1138 | break; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1139 | case 'x': |
| 1140 | print_hexdump = 1; |
| 1141 | print_defaults = 0; |
| 1142 | break; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1143 | case 't': |
| 1144 | print_timestamps = 1; |
| 1145 | print_defaults = 0; |
| 1146 | break; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1147 | case 'T': |
| 1148 | print_timestamps = 1; |
| 1149 | machine_readable_timestamps = 1; |
| 1150 | print_defaults = 0; |
| 1151 | break; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1152 | case 'V': |
| 1153 | verbose = 1; |
| 1154 | break; |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1155 | 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 Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1167 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1168 | mem_fd = open("/dev/mem", O_RDONLY, 0); |
| 1169 | if (mem_fd < 0) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1170 | fprintf(stderr, "Failed to gain memory access: %s\n", |
| 1171 | strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1172 | return 1; |
| 1173 | } |
| 1174 | |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1175 | #ifdef __arm__ |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1176 | int addr_cells, size_cells; |
| 1177 | char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot", |
| 1178 | &addr_cells, &size_cells); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1179 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1180 | if (!coreboot_node) { |
| 1181 | fprintf(stderr, "Could not find 'coreboot' compatible node!\n"); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1182 | return 1; |
| 1183 | } |
| 1184 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1185 | 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 Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1200 | return 1; |
| 1201 | } |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1202 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1203 | int i; |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 1204 | 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 Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1207 | 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 Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 1216 | 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 Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 1226 | parse_cbtable(baseaddr, cb_table_size, 1); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1227 | #else |
| 1228 | int j; |
| 1229 | static const int possible_base_addresses[] = { 0, 0xf0000 }; |
| 1230 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1231 | /* Find and parse coreboot table */ |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1232 | for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) { |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 1233 | if (parse_cbtable(possible_base_addresses[j], MAP_BYTES, 1)) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1234 | break; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1235 | } |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1236 | #endif |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1237 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1238 | if (print_console) |
| 1239 | dump_console(); |
| 1240 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1241 | if (print_coverage) |
| 1242 | dump_coverage(); |
| 1243 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1244 | if (print_list) |
| 1245 | dump_cbmem_toc(); |
| 1246 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1247 | if (print_hexdump) |
| 1248 | dump_cbmem_hex(); |
| 1249 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1250 | if (print_defaults || print_timestamps) |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1251 | dump_timestamps(machine_readable_timestamps); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1252 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1253 | close(mem_fd); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |