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