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