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