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. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Nico Huber | 8e4bb928 | 2013-05-26 18:17:54 +0200 | [diff] [blame] | 20 | #include <inttypes.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 26 | #include <getopt.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 29 | #include <ctype.h> |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 30 | #include <arpa/inet.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/mman.h> |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 34 | #include <libgen.h> |
| 35 | #include <assert.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 36 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 37 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 38 | #define MAP_BYTES (1024*1024) |
Kyösti Mälkki | b3594ab | 2014-06-18 00:43:35 +0300 | [diff] [blame^] | 39 | #define IS_ENABLED(x) (defined (x) && (x)) |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 40 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 41 | #include "boot/coreboot_tables.h" |
| 42 | |
| 43 | typedef uint16_t u16; |
| 44 | typedef uint32_t u32; |
| 45 | typedef uint64_t u64; |
| 46 | |
| 47 | #include "cbmem.h" |
| 48 | #include "timestamp.h" |
| 49 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 50 | #define CBMEM_VERSION "1.1" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 51 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 52 | /* verbose output? */ |
| 53 | static int verbose = 0; |
| 54 | #define debug(x...) if(verbose) printf(x) |
| 55 | |
| 56 | /* File handle used to access /dev/mem */ |
| 57 | static int fd; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * calculate ip checksum (16 bit quantities) on a passed in buffer. In case |
| 61 | * the buffer length is odd last byte is excluded from the calculation |
| 62 | */ |
| 63 | static u16 ipchcksum(const void *addr, unsigned size) |
| 64 | { |
| 65 | const u16 *p = addr; |
| 66 | unsigned i, n = size / 2; /* don't expect odd sized blocks */ |
| 67 | u32 sum = 0; |
| 68 | |
| 69 | for (i = 0; i < n; i++) |
| 70 | sum += p[i]; |
| 71 | |
| 72 | sum = (sum >> 16) + (sum & 0xffff); |
| 73 | sum += (sum >> 16); |
| 74 | sum = ~sum & 0xffff; |
| 75 | return (u16) sum; |
| 76 | } |
| 77 | |
| 78 | /* |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 79 | * Functions to map / unmap physical memory into virtual address space. These |
| 80 | * 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] | 81 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 82 | static void *mapped_virtual; |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 83 | static size_t mapped_size; |
| 84 | |
| 85 | static inline size_t size_to_mib(size_t sz) |
| 86 | { |
| 87 | return sz >> 20; |
| 88 | } |
| 89 | |
| 90 | static void unmap_memory(void) |
| 91 | { |
| 92 | if (mapped_virtual == NULL) { |
| 93 | fprintf(stderr, "Error unmapping memory\n"); |
| 94 | return; |
| 95 | } |
| 96 | debug("Unmapping %zuMB of virtual memory at %p.\n", |
| 97 | size_to_mib(mapped_size), mapped_virtual); |
| 98 | munmap(mapped_virtual, mapped_size); |
| 99 | mapped_virtual = NULL; |
| 100 | mapped_size = 0; |
| 101 | } |
| 102 | |
| 103 | static void *map_memory_size(u64 physical, size_t size) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 104 | { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 105 | void *v; |
| 106 | off_t p; |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 107 | u64 page = getpagesize(); |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 108 | size_t padding; |
| 109 | |
| 110 | if (mapped_virtual != NULL) |
| 111 | unmap_memory(); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 112 | |
| 113 | /* Mapped memory must be aligned to page size */ |
| 114 | p = physical & ~(page - 1); |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 115 | padding = physical & (page-1); |
| 116 | size += padding; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 117 | |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 118 | debug("Mapping %zuMB of physical memory at 0x%jx.\n", |
| 119 | size_to_mib(size), (intmax_t)p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 120 | |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 121 | v = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 122 | |
| 123 | if (v == MAP_FAILED) { |
| 124 | fprintf(stderr, "Failed to mmap /dev/mem: %s\n", |
| 125 | strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 126 | exit(1); |
| 127 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 128 | |
| 129 | /* Remember what we actually mapped ... */ |
| 130 | mapped_virtual = v; |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 131 | mapped_size = size; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 132 | |
| 133 | /* ... but return address to the physical memory that was requested */ |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 134 | if (padding) |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 135 | debug(" ... padding virtual address with 0x%zx bytes.\n", |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 136 | padding); |
| 137 | v += padding; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 138 | |
| 139 | return v; |
| 140 | } |
| 141 | |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 142 | static void *map_memory(u64 physical) |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 143 | { |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 144 | return map_memory_size(physical, MAP_BYTES); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 148 | * Try finding the timestamp table and coreboot cbmem console starting from the |
| 149 | * passed in memory offset. Could be called recursively in case a forwarding |
| 150 | * entry is found. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 151 | * |
| 152 | * Returns pointer to a memory buffer containg the timestamp table or zero if |
| 153 | * none found. |
| 154 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 155 | |
| 156 | static struct lb_cbmem_ref timestamps; |
| 157 | static struct lb_cbmem_ref console; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 158 | static struct lb_memory_range cbmem; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 159 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 160 | /* This is a work-around for a nasty problem introduced by initially having |
| 161 | * pointer sized entries in the lb_cbmem_ref structures. This caused problems |
| 162 | * on 64bit x86 systems because coreboot is 32bit on those systems. |
| 163 | * When the problem was found, it was corrected, but there are a lot of |
| 164 | * systems out there with a firmware that does not produce the right |
| 165 | * lb_cbmem_ref structure. Hence we try to autocorrect this issue here. |
| 166 | */ |
| 167 | static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref) |
| 168 | { |
| 169 | struct lb_cbmem_ref ret; |
| 170 | |
| 171 | ret = *cbmem_ref; |
| 172 | |
| 173 | if (cbmem_ref->size < sizeof(*cbmem_ref)) |
| 174 | ret.cbmem_addr = (uint32_t)ret.cbmem_addr; |
| 175 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 176 | debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr); |
| 177 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 178 | return ret; |
| 179 | } |
| 180 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 181 | static int parse_cbtable(u64 address) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 182 | { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 183 | int i, found = 0; |
| 184 | void *buf; |
| 185 | |
Nico Huber | 8e4bb928 | 2013-05-26 18:17:54 +0200 | [diff] [blame] | 186 | debug("Looking for coreboot table at %" PRIx64 "\n", address); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 187 | buf = map_memory(address); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 188 | |
| 189 | /* look at every 16 bytes within 4K of the base */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 190 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 191 | for (i = 0; i < 0x1000; i += 0x10) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 192 | struct lb_header *lbh; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 193 | struct lb_record* lbr_p; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 194 | void *lbtable; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 195 | int j; |
| 196 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 197 | lbh = (struct lb_header *)(buf + i); |
| 198 | if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) || |
| 199 | !lbh->header_bytes || |
| 200 | ipchcksum(lbh, sizeof(*lbh))) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 201 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 202 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 203 | lbtable = buf + i + lbh->header_bytes; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 204 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 205 | if (ipchcksum(lbtable, lbh->table_bytes) != |
| 206 | lbh->table_checksum) { |
| 207 | debug("Signature found, but wrong checksum.\n"); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 208 | continue; |
| 209 | } |
| 210 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 211 | found = 1; |
| 212 | debug("Found!\n"); |
| 213 | |
| 214 | for (j = 0; j < lbh->table_bytes; j += lbr_p->size) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 215 | /* look for the timestamp table */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 216 | lbr_p = (struct lb_record*) ((char *)lbtable + j); |
| 217 | debug(" coreboot table entry 0x%02x\n", lbr_p->tag); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 218 | switch (lbr_p->tag) { |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 219 | case LB_TAG_MEMORY: { |
| 220 | int i = 0; |
| 221 | debug(" Found memory map.\n"); |
| 222 | struct lb_memory *memory = |
| 223 | (struct lb_memory *)lbr_p; |
| 224 | while ((char *)&memory->map[i] < ((char *)lbtable |
| 225 | + lbr_p->size)) { |
| 226 | if (memory->map[i].type == LB_MEM_TABLE) { |
| 227 | debug(" LB_MEM_TABLE found.\n"); |
| 228 | /* The last one found is CBMEM */ |
| 229 | cbmem = memory->map[i]; |
| 230 | } |
| 231 | i++; |
| 232 | } |
| 233 | continue; |
| 234 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 235 | case LB_TAG_TIMESTAMPS: { |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 236 | debug(" Found timestamp table.\n"); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 237 | timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | case LB_TAG_CBMEM_CONSOLE: { |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 241 | debug(" Found cbmem console.\n"); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 242 | console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 243 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 244 | } |
| 245 | case LB_TAG_FORWARD: { |
| 246 | /* |
| 247 | * This is a forwarding entry - repeat the |
| 248 | * search at the new address. |
| 249 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 250 | struct lb_forward lbf_p = |
| 251 | *(struct lb_forward *) lbr_p; |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 252 | debug(" Found forwarding entry.\n"); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 253 | unmap_memory(); |
| 254 | return parse_cbtable(lbf_p.forward); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 255 | } |
| 256 | default: |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | } |
| 261 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 262 | unmap_memory(); |
| 263 | |
| 264 | return found; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 267 | #if defined(__i386__) || defined(__x86_64__) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 268 | /* |
| 269 | * read CPU frequency from a sysfs file, return an frequency in Kilohertz as |
| 270 | * an int or exit on any error. |
| 271 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 272 | static u64 get_cpu_freq_KHz(void) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 273 | { |
| 274 | FILE *cpuf; |
| 275 | char freqs[100]; |
| 276 | int size; |
| 277 | char *endp; |
| 278 | u64 rv; |
| 279 | |
| 280 | const char* freq_file = |
| 281 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 282 | |
| 283 | cpuf = fopen(freq_file, "r"); |
| 284 | if (!cpuf) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 285 | fprintf(stderr, "Could not open %s: %s\n", |
| 286 | freq_file, strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 287 | exit(1); |
| 288 | } |
| 289 | |
| 290 | memset(freqs, 0, sizeof(freqs)); |
| 291 | size = fread(freqs, 1, sizeof(freqs), cpuf); |
| 292 | if (!size || (size == sizeof(freqs))) { |
| 293 | fprintf(stderr, "Wrong number of bytes(%d) read from %s\n", |
| 294 | size, freq_file); |
| 295 | exit(1); |
| 296 | } |
| 297 | fclose(cpuf); |
| 298 | rv = strtoull(freqs, &endp, 10); |
| 299 | |
| 300 | if (*endp == '\0' || *endp == '\n') |
| 301 | return rv; |
| 302 | fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n", |
| 303 | freqs, freq_file); |
| 304 | exit(1); |
| 305 | } |
| 306 | |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 307 | /* On x86 platforms timestamps are stored |
| 308 | * in CPU cycles (from rdtsc). Hence the |
| 309 | * timestamp divider is the CPU frequency |
| 310 | * in MHz. |
| 311 | */ |
| 312 | u64 arch_convert_raw_ts_entry(u64 ts) |
| 313 | { |
| 314 | static u64 cpu_freq_mhz = 0; |
| 315 | |
| 316 | if (!cpu_freq_mhz) |
| 317 | cpu_freq_mhz = get_cpu_freq_KHz() / 1000; |
| 318 | |
| 319 | return ts / cpu_freq_mhz; |
| 320 | } |
| 321 | |
| 322 | #else |
| 323 | |
| 324 | /* On non-x86 platforms the timestamp entries |
| 325 | * are not in clock cycles but in usecs |
| 326 | */ |
| 327 | u64 arch_convert_raw_ts_entry(u64 ts) |
| 328 | { |
| 329 | return ts; |
| 330 | } |
| 331 | #endif |
| 332 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 333 | /* |
| 334 | * Print an integer in 'normalized' form - with commas separating every three |
| 335 | * decimal orders. The 'comma' parameter indicates if a comma is needed after |
| 336 | * the value is printed. |
| 337 | */ |
| 338 | static void print_norm(u64 v, int comma) |
| 339 | { |
| 340 | int first_triple = 1; |
| 341 | |
| 342 | if (v > 1000) { |
| 343 | /* print the higher order sections first */ |
| 344 | print_norm(v / 1000, 1); |
| 345 | first_triple = 0; |
| 346 | } |
| 347 | if (first_triple) |
| 348 | printf("%d", (u32)(v % 1000)); |
| 349 | else |
| 350 | printf("%3.3d", (u32)(v % 1000)); |
| 351 | if (comma) |
| 352 | printf(","); |
| 353 | } |
| 354 | |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 355 | enum additional_timestamp_id { |
| 356 | // Depthcharge entry IDs start at 1000. |
| 357 | TS_DC_START = 1000, |
| 358 | |
| 359 | TS_RO_PARAMS_INIT = 1001, |
| 360 | TS_RO_VB_INIT = 1002, |
| 361 | TS_RO_VB_SELECT_FIRMWARE = 1003, |
| 362 | TS_RO_VB_SELECT_AND_LOAD_KERNEL = 1004, |
| 363 | |
| 364 | TS_RW_VB_SELECT_AND_LOAD_KERNEL = 1010, |
| 365 | |
| 366 | TS_VB_SELECT_AND_LOAD_KERNEL = 1020, |
| 367 | |
| 368 | TS_CROSSYSTEM_DATA = 1100, |
| 369 | TS_START_KERNEL = 1101 |
| 370 | }; |
| 371 | |
| 372 | static const struct timestamp_id_to_name { |
| 373 | u32 id; |
| 374 | const char *name; |
| 375 | } timestamp_ids[] = { |
| 376 | { TS_START_ROMSTAGE, "start of rom stage" }, |
| 377 | { TS_BEFORE_INITRAM, "before ram initialization" }, |
| 378 | { TS_AFTER_INITRAM, "after ram initialization" }, |
| 379 | { TS_END_ROMSTAGE, "end of romstage" }, |
| 380 | { TS_START_VBOOT, "start of verified boot" }, |
| 381 | { TS_END_VBOOT, "end of verified boot" }, |
| 382 | { TS_START_COPYRAM, "start of copying ram stage" }, |
| 383 | { TS_END_COPYRAM, "end of copying ram stage" }, |
| 384 | { TS_START_RAMSTAGE, "start of ramstage" }, |
| 385 | { TS_DEVICE_ENUMERATE, "device enumeration" }, |
| 386 | { TS_DEVICE_CONFIGURE, "device configuration" }, |
| 387 | { TS_DEVICE_ENABLE, "device enable" }, |
| 388 | { TS_DEVICE_INITIALIZE, "device initialization" }, |
| 389 | { TS_DEVICE_DONE, "device setup done" }, |
| 390 | { TS_CBMEM_POST, "cbmem post" }, |
| 391 | { TS_WRITE_TABLES, "write tables" }, |
| 392 | { TS_LOAD_PAYLOAD, "load payload" }, |
| 393 | { TS_ACPI_WAKE_JUMP, "ACPI wake jump" }, |
| 394 | { TS_SELFBOOT_JUMP, "selfboot jump" }, |
| 395 | { TS_DC_START, "depthcharge start" }, |
| 396 | { TS_RO_PARAMS_INIT, "RO parameter init" }, |
| 397 | { TS_RO_VB_INIT, "RO vboot init" }, |
| 398 | { TS_RO_VB_SELECT_FIRMWARE, "RO vboot select firmware" }, |
| 399 | { TS_RO_VB_SELECT_AND_LOAD_KERNEL, "RO vboot select&load kernel" }, |
| 400 | { TS_RW_VB_SELECT_AND_LOAD_KERNEL, "RW vboot select&load kernel" }, |
| 401 | { TS_VB_SELECT_AND_LOAD_KERNEL, "vboot select&load kernel" }, |
| 402 | { TS_CROSSYSTEM_DATA, "crossystem data" }, |
| 403 | { TS_START_KERNEL, "start kernel" } |
| 404 | }; |
| 405 | |
| 406 | void timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp) |
| 407 | { |
| 408 | int i; |
| 409 | const char *name; |
| 410 | |
| 411 | name = "<unknown>"; |
| 412 | for (i = 0; i < ARRAY_SIZE(timestamp_ids); i++) { |
| 413 | if (timestamp_ids[i].id == id) { |
| 414 | name = timestamp_ids[i].name; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | printf("%4d:", id); |
| 420 | printf("%-30s", name); |
| 421 | print_norm(arch_convert_raw_ts_entry(stamp), 0); |
| 422 | if (prev_stamp) { |
| 423 | printf(" ("); |
| 424 | print_norm(arch_convert_raw_ts_entry(stamp |
| 425 | - prev_stamp), 0); |
| 426 | printf(")"); |
| 427 | } |
| 428 | printf("\n"); |
| 429 | } |
| 430 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 431 | /* dump the timestamp table */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 432 | static void dump_timestamps(void) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 433 | { |
| 434 | int i; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 435 | struct timestamp_table *tst_p; |
| 436 | |
| 437 | if (timestamps.tag != LB_TAG_TIMESTAMPS) { |
| 438 | fprintf(stderr, "No timestamps found in coreboot table.\n"); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | tst_p = (struct timestamp_table *) |
| 443 | map_memory((unsigned long)timestamps.cbmem_addr); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 444 | |
| 445 | printf("%d entries total:\n\n", tst_p->num_entries); |
| 446 | for (i = 0; i < tst_p->num_entries; i++) { |
| 447 | const struct timestamp_entry *tse_p = tst_p->entries + i; |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 448 | timestamp_print_entry(tse_p->entry_id, tse_p->entry_stamp, |
| 449 | i ? tse_p[-1].entry_stamp : 0); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 450 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 451 | |
| 452 | unmap_memory(); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 455 | /* dump the cbmem console */ |
| 456 | static void dump_console(void) |
| 457 | { |
| 458 | void *console_p; |
| 459 | char *console_c; |
| 460 | uint32_t size; |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 461 | uint32_t cursor; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 462 | |
| 463 | if (console.tag != LB_TAG_CBMEM_CONSOLE) { |
| 464 | fprintf(stderr, "No console found in coreboot table.\n"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | console_p = map_memory((unsigned long)console.cbmem_addr); |
| 469 | /* The in-memory format of the console area is: |
| 470 | * u32 size |
| 471 | * u32 cursor |
| 472 | * char console[size] |
| 473 | * Hence we have to add 8 to get to the actual console string. |
| 474 | */ |
| 475 | size = *(uint32_t *)console_p; |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 476 | cursor = *(uint32_t *) (console_p + 4); |
| 477 | /* Cursor continues to go on even after no more data fits in |
| 478 | * the buffer but the data is dropped in this case. |
| 479 | */ |
| 480 | if (size > cursor) |
| 481 | size = cursor; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 482 | console_c = malloc(size + 1); |
| 483 | if (!console_c) { |
| 484 | fprintf(stderr, "Not enough memory for console.\n"); |
| 485 | exit(1); |
| 486 | } |
| 487 | |
Aaron Durbin | ab180d8 | 2014-03-31 11:59:58 -0500 | [diff] [blame] | 488 | console_p = map_memory_size((unsigned long)console.cbmem_addr, |
| 489 | size + sizeof(size) + sizeof(cursor)); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 490 | memcpy(console_c, console_p + 8, size); |
| 491 | console_c[size] = 0; |
| 492 | |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 493 | printf("%s\n", console_c); |
| 494 | if (size < cursor) |
| 495 | printf("%d %s lost\n", cursor - size, |
| 496 | (cursor - size) == 1 ? "byte":"bytes"); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 497 | |
| 498 | free(console_c); |
| 499 | |
| 500 | unmap_memory(); |
| 501 | } |
| 502 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 503 | static void hexdump(unsigned long memory, int length) |
| 504 | { |
| 505 | int i; |
| 506 | uint8_t *m; |
| 507 | int all_zero = 0; |
| 508 | |
| 509 | m = map_memory((intptr_t)memory); |
| 510 | |
| 511 | if (length > MAP_BYTES) { |
| 512 | printf("Truncating hex dump from %d to %d bytes\n\n", |
| 513 | length, MAP_BYTES); |
| 514 | length = MAP_BYTES; |
| 515 | } |
| 516 | |
| 517 | for (i = 0; i < length; i += 16) { |
| 518 | int j; |
| 519 | |
| 520 | all_zero++; |
| 521 | for (j = 0; j < 16; j++) { |
| 522 | if(m[i+j] != 0) { |
| 523 | all_zero = 0; |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | if (all_zero < 2) { |
| 529 | printf("%08lx:", memory + i); |
| 530 | for (j = 0; j < 16; j++) |
| 531 | printf(" %02x", m[i+j]); |
| 532 | printf(" "); |
| 533 | for (j = 0; j < 16; j++) |
| 534 | printf("%c", isprint(m[i+j]) ? m[i+j] : '.'); |
| 535 | printf("\n"); |
| 536 | } else if (all_zero == 2) { |
| 537 | printf("...\n"); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | unmap_memory(); |
| 542 | } |
| 543 | |
| 544 | static void dump_cbmem_hex(void) |
| 545 | { |
| 546 | if (cbmem.type != LB_MEM_TABLE) { |
| 547 | fprintf(stderr, "No coreboot CBMEM area found!\n"); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size)); |
| 552 | } |
| 553 | |
| 554 | /* The root region is at least DYN_CBMEM_ALIGN_SIZE . */ |
| 555 | #define DYN_CBMEM_ALIGN_SIZE (4096) |
| 556 | #define ROOT_MIN_SIZE DYN_CBMEM_ALIGN_SIZE |
| 557 | #define CBMEM_POINTER_MAGIC 0xc0389479 |
| 558 | #define CBMEM_ENTRY_MAGIC ~(CBMEM_POINTER_MAGIC) |
| 559 | |
| 560 | struct cbmem_root_pointer { |
| 561 | uint32_t magic; |
| 562 | uint32_t root; |
| 563 | } __attribute__((packed)); |
| 564 | |
| 565 | struct dynamic_cbmem_entry { |
| 566 | uint32_t magic; |
| 567 | uint32_t start; |
| 568 | uint32_t size; |
| 569 | uint32_t id; |
| 570 | } __attribute__((packed)); |
| 571 | |
| 572 | struct cbmem_root { |
| 573 | uint32_t max_entries; |
| 574 | uint32_t num_entries; |
| 575 | uint32_t locked; |
| 576 | uint32_t size; |
| 577 | struct dynamic_cbmem_entry entries[0]; |
| 578 | } __attribute__((packed)); |
| 579 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 580 | #define CBMEM_MAGIC 0x434f5245 |
| 581 | #define MAX_CBMEM_ENTRIES 16 |
| 582 | |
| 583 | struct cbmem_entry { |
| 584 | uint32_t magic; |
| 585 | uint32_t id; |
| 586 | uint64_t base; |
| 587 | uint64_t size; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 588 | } __attribute__((packed)); |
| 589 | |
| 590 | static const struct cbmem_id_to_name { |
| 591 | u32 id; |
| 592 | const char *name; |
| 593 | } cbmem_ids[] = { |
| 594 | { CBMEM_ID_FREESPACE, "FREE SPACE " }, |
| 595 | { CBMEM_ID_GDT, "GDT " }, |
| 596 | { CBMEM_ID_ACPI, "ACPI " }, |
| 597 | { CBMEM_ID_CBTABLE, "COREBOOT " }, |
| 598 | { CBMEM_ID_PIRQ, "IRQ TABLE " }, |
| 599 | { CBMEM_ID_MPTABLE, "SMP TABLE " }, |
| 600 | { CBMEM_ID_RESUME, "ACPI RESUME" }, |
| 601 | { CBMEM_ID_RESUME_SCRATCH, "ACPISCRATCH" }, |
| 602 | { CBMEM_ID_ACPI_GNVS, "ACPI GNVS " }, |
| 603 | { CBMEM_ID_ACPI_GNVS_PTR, "GNVS PTR " }, |
| 604 | { CBMEM_ID_SMBIOS, "SMBIOS " }, |
| 605 | { CBMEM_ID_TIMESTAMP, "TIME STAMP " }, |
| 606 | { CBMEM_ID_MRCDATA, "MRC DATA " }, |
| 607 | { CBMEM_ID_CONSOLE, "CONSOLE " }, |
| 608 | { CBMEM_ID_ELOG, "ELOG " }, |
| 609 | { CBMEM_ID_COVERAGE, "COVERAGE " }, |
| 610 | { CBMEM_ID_ROMSTAGE_INFO, "ROMSTAGE " }, |
| 611 | { CBMEM_ID_ROMSTAGE_RAM_STACK, "ROMSTG STCK" }, |
| 612 | { CBMEM_ID_RAMSTAGE, "RAMSTAGE " }, |
| 613 | { CBMEM_ID_RAMSTAGE_CACHE, "RAMSTAGE $ " }, |
| 614 | { CBMEM_ID_ROOT, "CBMEM ROOT " }, |
| 615 | { CBMEM_ID_VBOOT_HANDOFF, "VBOOT " }, |
| 616 | { CBMEM_ID_CAR_GLOBALS, "CAR GLOBALS" }, |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 617 | }; |
| 618 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 619 | void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size) |
| 620 | { |
| 621 | int i; |
| 622 | const char *name; |
| 623 | |
| 624 | name = NULL; |
| 625 | for (i = 0; i < ARRAY_SIZE(cbmem_ids); i++) { |
| 626 | if (cbmem_ids[i].id == id) { |
| 627 | name = cbmem_ids[i].name; |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | printf("%2d. ", n); |
| 633 | if (name == NULL) |
| 634 | printf("%08x ", id); |
| 635 | else |
| 636 | printf("%s", name); |
| 637 | printf(" %08" PRIx64 " ", base); |
| 638 | printf(" %08" PRIx64 "\n", size); |
| 639 | } |
| 640 | |
| 641 | static void dump_static_cbmem_toc(struct cbmem_entry *entries) |
| 642 | { |
| 643 | int i; |
| 644 | |
| 645 | printf("CBMEM table of contents:\n"); |
| 646 | printf(" ID START LENGTH\n"); |
| 647 | |
| 648 | for (i=0; i<MAX_CBMEM_ENTRIES; i++) { |
| 649 | if (entries[i].magic != CBMEM_MAGIC) |
| 650 | break; |
| 651 | cbmem_print_entry(i, entries[i].id, |
| 652 | entries[i].base, entries[i].size); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | static void dump_dynamic_cbmem_toc(struct cbmem_root *root) |
| 657 | { |
| 658 | int i; |
| 659 | debug("CBMEM: max_entries=%d num_entries=%d locked=0x%x, size=%d\n\n", |
| 660 | root->max_entries, root->num_entries, root->locked, root->size); |
| 661 | |
| 662 | printf("CBMEM table of contents:\n"); |
| 663 | printf(" ID START LENGTH\n"); |
| 664 | |
| 665 | for (i = 0; i < root->num_entries; i++) { |
| 666 | if(root->entries[i].magic != CBMEM_ENTRY_MAGIC) |
| 667 | break; |
| 668 | cbmem_print_entry(i, root->entries[i].id, |
| 669 | root->entries[i].start, root->entries[i].size); |
| 670 | } |
| 671 | } |
| 672 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 673 | static void dump_cbmem_toc(void) |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 674 | { |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 675 | uint64_t start; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 676 | void *cbmem_area; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 677 | struct cbmem_entry *entries; |
| 678 | |
| 679 | if (cbmem.type != LB_MEM_TABLE) { |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 680 | fprintf(stderr, "No coreboot CBMEM area found!\n"); |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 681 | return; |
| 682 | } |
| 683 | |
| 684 | start = unpack_lb64(cbmem.start); |
| 685 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 686 | cbmem_area = map_memory(start); |
| 687 | entries = (struct cbmem_entry *)cbmem_area; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 688 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 689 | if (entries[0].magic == CBMEM_MAGIC) { |
| 690 | dump_static_cbmem_toc(entries); |
| 691 | } else { |
| 692 | uint64_t rootptr; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 693 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 694 | rootptr = unpack_lb64(cbmem.start) + unpack_lb64(cbmem.size); |
| 695 | rootptr &= ~(DYN_CBMEM_ALIGN_SIZE - 1); |
| 696 | rootptr -= sizeof(struct cbmem_root_pointer); |
| 697 | unmap_memory(); |
| 698 | struct cbmem_root_pointer *r = |
| 699 | (struct cbmem_root_pointer *)map_memory(rootptr); |
| 700 | if (r->magic == CBMEM_POINTER_MAGIC) { |
| 701 | struct cbmem_root *root; |
| 702 | uint64_t rootaddr = r->root; |
| 703 | unmap_memory(); |
| 704 | /* Note that this only works because our default mmap |
| 705 | * size is 1MiB which happens to be larger than the |
| 706 | * root entry size which is default to be 4KiB. |
| 707 | */ |
| 708 | root = (struct cbmem_root *)map_memory(rootaddr); |
| 709 | dump_dynamic_cbmem_toc(root); |
| 710 | } else |
| 711 | fprintf(stderr, "No valid coreboot CBMEM root pointer found.\n"); |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 712 | } |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 713 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 714 | unmap_memory(); |
| 715 | } |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 716 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 717 | #define COVERAGE_MAGIC 0x584d4153 |
| 718 | struct file { |
| 719 | uint32_t magic; |
| 720 | uint32_t next; |
| 721 | uint32_t filename; |
| 722 | uint32_t data; |
| 723 | int offset; |
| 724 | int len; |
| 725 | }; |
| 726 | |
| 727 | static int mkpath(char *path, mode_t mode) |
| 728 | { |
| 729 | assert (path && *path); |
| 730 | char *p; |
| 731 | for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) { |
| 732 | *p = '\0'; |
| 733 | if (mkdir(path, mode) == -1) { |
| 734 | if (errno != EEXIST) { |
| 735 | *p = '/'; |
| 736 | return -1; |
| 737 | } |
| 738 | } |
| 739 | *p = '/'; |
| 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static void dump_coverage(void) |
| 745 | { |
| 746 | int i, found = 0; |
| 747 | uint64_t start; |
| 748 | struct cbmem_entry *entries; |
| 749 | void *coverage; |
| 750 | unsigned long phys_offset; |
| 751 | #define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset) |
| 752 | |
| 753 | if (cbmem.type != LB_MEM_TABLE) { |
| 754 | fprintf(stderr, "No coreboot table area found!\n"); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | start = unpack_lb64(cbmem.start); |
| 759 | |
| 760 | entries = (struct cbmem_entry *)map_memory(start); |
| 761 | |
| 762 | for (i=0; i<MAX_CBMEM_ENTRIES; i++) { |
| 763 | if (entries[i].magic != CBMEM_MAGIC) |
| 764 | break; |
| 765 | if (entries[i].id == CBMEM_ID_COVERAGE) { |
| 766 | found = 1; |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (!found) { |
| 772 | unmap_memory(); |
| 773 | fprintf(stderr, "No coverage information found in" |
| 774 | " CBMEM area.\n"); |
| 775 | return; |
| 776 | } |
| 777 | |
| 778 | start = entries[i].base; |
| 779 | unmap_memory(); |
| 780 | /* Map coverage area */ |
| 781 | coverage = map_memory(start); |
| 782 | phys_offset = (unsigned long)coverage - (unsigned long)start; |
| 783 | |
| 784 | printf("Dumping coverage data...\n"); |
| 785 | |
| 786 | struct file *file = (struct file *)coverage; |
| 787 | while (file && file->magic == COVERAGE_MAGIC) { |
| 788 | FILE *f; |
| 789 | char *filename; |
| 790 | |
| 791 | debug(" -> %s\n", (char *)phys_to_virt(file->filename)); |
| 792 | filename = strdup((char *)phys_to_virt(file->filename)); |
| 793 | if (mkpath(filename, 0755) == -1) { |
| 794 | perror("Directory for coverage data could " |
| 795 | "not be created"); |
| 796 | exit(1); |
| 797 | } |
| 798 | f = fopen(filename, "wb"); |
| 799 | if (!f) { |
| 800 | printf("Could not open %s: %s\n", |
| 801 | filename, strerror(errno)); |
| 802 | exit(1); |
| 803 | } |
| 804 | if (fwrite((void *)phys_to_virt(file->data), |
| 805 | file->len, 1, f) != 1) { |
| 806 | printf("Could not write to %s: %s\n", |
| 807 | filename, strerror(errno)); |
| 808 | exit(1); |
| 809 | } |
| 810 | fclose(f); |
| 811 | free(filename); |
| 812 | |
| 813 | if (file->next) |
| 814 | file = (struct file *)phys_to_virt(file->next); |
| 815 | else |
| 816 | file = NULL; |
| 817 | } |
| 818 | unmap_memory(); |
| 819 | } |
| 820 | |
| 821 | static void print_version(void) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 822 | { |
| 823 | printf("cbmem v%s -- ", CBMEM_VERSION); |
| 824 | printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n"); |
| 825 | printf( |
| 826 | "This program is free software: you can redistribute it and/or modify\n" |
| 827 | "it under the terms of the GNU General Public License as published by\n" |
| 828 | "the Free Software Foundation, version 2 of the License.\n\n" |
| 829 | "This program is distributed in the hope that it will be useful,\n" |
| 830 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 831 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
| 832 | "GNU General Public License for more details.\n\n" |
| 833 | "You should have received a copy of the GNU General Public License\n" |
| 834 | "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n"); |
| 835 | } |
| 836 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 837 | static void print_usage(const char *name) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 838 | { |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 839 | printf("usage: %s [-cCltxVvh?]\n", name); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 840 | printf("\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 841 | " -c | --console: print cbmem console\n" |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 842 | " -C | --coverage: dump coverage information\n" |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 843 | " -l | --list: print cbmem table of contents\n" |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 844 | " -x | --hexdump: print hexdump of cbmem area\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 845 | " -t | --timestamps: print timestamp information\n" |
| 846 | " -V | --verbose: verbose (debugging) output\n" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 847 | " -v | --version: print the version\n" |
| 848 | " -h | --help: print this help\n" |
| 849 | "\n"); |
| 850 | exit(1); |
| 851 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 852 | |
| 853 | int main(int argc, char** argv) |
| 854 | { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 855 | int print_defaults = 1; |
| 856 | int print_console = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 857 | int print_coverage = 0; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 858 | int print_list = 0; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 859 | int print_hexdump = 0; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 860 | int print_timestamps = 0; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 861 | |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 862 | int opt, option_index = 0; |
| 863 | static struct option long_options[] = { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 864 | {"console", 0, 0, 'c'}, |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 865 | {"coverage", 0, 0, 'C'}, |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 866 | {"list", 0, 0, 'l'}, |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 867 | {"timestamps", 0, 0, 't'}, |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 868 | {"hexdump", 0, 0, 'x'}, |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 869 | {"verbose", 0, 0, 'V'}, |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 870 | {"version", 0, 0, 'v'}, |
| 871 | {"help", 0, 0, 'h'}, |
| 872 | {0, 0, 0, 0} |
| 873 | }; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 874 | while ((opt = getopt_long(argc, argv, "cCltxVvh?", |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 875 | long_options, &option_index)) != EOF) { |
| 876 | switch (opt) { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 877 | case 'c': |
| 878 | print_console = 1; |
| 879 | print_defaults = 0; |
| 880 | break; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 881 | case 'C': |
| 882 | print_coverage = 1; |
| 883 | print_defaults = 0; |
| 884 | break; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 885 | case 'l': |
| 886 | print_list = 1; |
| 887 | print_defaults = 0; |
| 888 | break; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 889 | case 'x': |
| 890 | print_hexdump = 1; |
| 891 | print_defaults = 0; |
| 892 | break; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 893 | case 't': |
| 894 | print_timestamps = 1; |
| 895 | print_defaults = 0; |
| 896 | break; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 897 | case 'V': |
| 898 | verbose = 1; |
| 899 | break; |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 900 | case 'v': |
| 901 | print_version(); |
| 902 | exit(0); |
| 903 | break; |
| 904 | case 'h': |
| 905 | case '?': |
| 906 | default: |
| 907 | print_usage(argv[0]); |
| 908 | exit(0); |
| 909 | break; |
| 910 | } |
| 911 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 912 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 913 | fd = open("/dev/mem", O_RDONLY, 0); |
| 914 | if (fd < 0) { |
| 915 | fprintf(stderr, "Failed to gain memory access: %s\n", |
| 916 | strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 917 | return 1; |
| 918 | } |
| 919 | |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 920 | #ifdef __arm__ |
| 921 | int dt_fd; |
| 922 | uint32_t cbtable_base; |
| 923 | |
| 924 | dt_fd = open("/proc/device-tree/firmware/coreboot/coreboot-table", |
| 925 | O_RDONLY, 0); |
| 926 | if (dt_fd < 0) { |
| 927 | fprintf(stderr, "Failed to open device tree node: %s\n", |
| 928 | strerror(errno)); |
| 929 | return 1; |
| 930 | } |
| 931 | |
| 932 | if (read(dt_fd, &cbtable_base, 4) != 4) { |
| 933 | fprintf(stderr, "Failed to read device tree node: %s\n", |
| 934 | strerror(errno)); |
| 935 | return 1; |
| 936 | } |
| 937 | close(dt_fd); |
| 938 | |
| 939 | parse_cbtable(ntohl(cbtable_base)); |
| 940 | #else |
| 941 | int j; |
| 942 | static const int possible_base_addresses[] = { 0, 0xf0000 }; |
| 943 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 944 | /* Find and parse coreboot table */ |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 945 | for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 946 | if (parse_cbtable(possible_base_addresses[j])) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 947 | break; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 948 | } |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 949 | #endif |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 950 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 951 | if (print_console) |
| 952 | dump_console(); |
| 953 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 954 | if (print_coverage) |
| 955 | dump_coverage(); |
| 956 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 957 | if (print_list) |
| 958 | dump_cbmem_toc(); |
| 959 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 960 | if (print_hexdump) |
| 961 | dump_cbmem_hex(); |
| 962 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 963 | if (print_defaults || print_timestamps) |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 964 | dump_timestamps(); |
| 965 | |
| 966 | close(fd); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 967 | return 0; |
| 968 | } |