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