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