Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 2 | |
Nico Huber | 8e4bb928 | 2013-05-26 18:17:54 +0200 | [diff] [blame] | 3 | #include <inttypes.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | #include <getopt.h> |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 9 | #include <dirent.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 12 | #include <ctype.h> |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 13 | #include <arpa/inet.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <sys/mman.h> |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 17 | #include <libgen.h> |
| 18 | #include <assert.h> |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 19 | #include <regex.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 20 | #include <commonlib/cbmem_id.h> |
| 21 | #include <commonlib/timestamp_serialized.h> |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 22 | #include <commonlib/tcpa_log_serialized.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 23 | #include <commonlib/coreboot_tables.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 24 | |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 25 | #ifdef __OpenBSD__ |
| 26 | #include <sys/param.h> |
| 27 | #include <sys/sysctl.h> |
| 28 | #endif |
| 29 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 30 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 31 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 32 | typedef uint8_t u8; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 33 | typedef uint16_t u16; |
| 34 | typedef uint32_t u32; |
| 35 | typedef uint64_t u64; |
| 36 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 37 | /* Return < 0 on error, 0 on success. */ |
| 38 | static int parse_cbtable(u64 address, size_t table_size); |
| 39 | |
| 40 | struct mapping { |
| 41 | void *virt; |
| 42 | size_t offset; |
| 43 | size_t virt_size; |
| 44 | unsigned long long phys; |
| 45 | size_t size; |
| 46 | }; |
| 47 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 48 | #define CBMEM_VERSION "1.1" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 49 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 50 | /* verbose output? */ |
| 51 | static int verbose = 0; |
| 52 | #define debug(x...) if(verbose) printf(x) |
| 53 | |
| 54 | /* File handle used to access /dev/mem */ |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 55 | static int mem_fd; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 56 | static struct mapping lbtable_mapping; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 57 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 58 | static void die(const char *msg) |
| 59 | { |
| 60 | if (msg) |
| 61 | fputs(msg, stderr); |
| 62 | exit(1); |
| 63 | } |
| 64 | |
| 65 | static unsigned long long system_page_size(void) |
| 66 | { |
| 67 | static unsigned long long page_size; |
| 68 | |
| 69 | if (!page_size) |
| 70 | page_size = getpagesize(); |
| 71 | |
| 72 | return page_size; |
| 73 | } |
| 74 | |
| 75 | static inline size_t size_to_mib(size_t sz) |
| 76 | { |
| 77 | return sz >> 20; |
| 78 | } |
| 79 | |
| 80 | /* Return mapping of physical address requested. */ |
| 81 | static const void *mapping_virt(const struct mapping *mapping) |
| 82 | { |
| 83 | const char *v = mapping->virt; |
| 84 | |
| 85 | if (v == NULL) |
| 86 | return NULL; |
| 87 | |
| 88 | return v + mapping->offset; |
| 89 | } |
| 90 | |
Aaron Durbin | cf20c90 | 2017-09-28 17:52:59 -0600 | [diff] [blame] | 91 | /* Returns virtual address on success, NULL on error. mapping is filled in. */ |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 92 | static const void *map_memory(struct mapping *mapping, unsigned long long phys, |
| 93 | size_t sz) |
| 94 | { |
| 95 | void *v; |
| 96 | unsigned long long page_size; |
| 97 | |
| 98 | page_size = system_page_size(); |
| 99 | |
| 100 | mapping->virt = NULL; |
| 101 | mapping->offset = phys % page_size; |
| 102 | mapping->virt_size = sz + mapping->offset; |
| 103 | mapping->size = sz; |
| 104 | mapping->phys = phys; |
| 105 | |
| 106 | if (size_to_mib(mapping->virt_size) == 0) { |
| 107 | debug("Mapping %zuB of physical memory at 0x%llx (requested 0x%llx).\n", |
| 108 | mapping->virt_size, phys - mapping->offset, phys); |
| 109 | } else { |
| 110 | debug("Mapping %zuMB of physical memory at 0x%llx (requested 0x%llx).\n", |
| 111 | size_to_mib(mapping->virt_size), phys - mapping->offset, |
| 112 | phys); |
| 113 | } |
| 114 | |
| 115 | v = mmap(NULL, mapping->virt_size, PROT_READ, MAP_SHARED, mem_fd, |
| 116 | phys - mapping->offset); |
| 117 | |
| 118 | if (v == MAP_FAILED) { |
| 119 | debug("Mapping failed %zuB of physical memory at 0x%llx.\n", |
| 120 | mapping->virt_size, phys - mapping->offset); |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | mapping->virt = v; |
| 125 | |
| 126 | if (mapping->offset != 0) |
| 127 | debug(" ... padding virtual address with 0x%zx bytes.\n", |
| 128 | mapping->offset); |
| 129 | |
| 130 | return mapping_virt(mapping); |
| 131 | } |
| 132 | |
| 133 | /* Returns 0 on success, < 0 on error. mapping is cleared if successful. */ |
| 134 | static int unmap_memory(struct mapping *mapping) |
| 135 | { |
| 136 | if (mapping->virt == NULL) |
| 137 | return -1; |
| 138 | |
| 139 | munmap(mapping->virt, mapping->virt_size); |
| 140 | mapping->virt = NULL; |
| 141 | mapping->offset = 0; |
| 142 | mapping->virt_size = 0; |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* Return size of physical address mapping requested. */ |
| 148 | static size_t mapping_size(const struct mapping *mapping) |
| 149 | { |
| 150 | if (mapping->virt == NULL) |
| 151 | return 0; |
| 152 | |
| 153 | return mapping->size; |
| 154 | } |
Timothy Pearson | df699d5 | 2015-05-16 14:55:54 -0500 | [diff] [blame] | 155 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 156 | /* |
Julius Werner | 127a79e | 2017-02-03 12:50:03 -0800 | [diff] [blame] | 157 | * Some architectures map /dev/mem memory in a way that doesn't support |
| 158 | * unaligned accesses. Most normal libc memcpy()s aren't safe to use in this |
| 159 | * case, so build our own which makes sure to never do unaligned accesses on |
| 160 | * *src (*dest is fine since we never map /dev/mem for writing). |
| 161 | */ |
| 162 | static void *aligned_memcpy(void *dest, const void *src, size_t n) |
| 163 | { |
| 164 | u8 *d = dest; |
| 165 | const volatile u8 *s = src; /* volatile to prevent optimization */ |
| 166 | |
| 167 | while ((uintptr_t)s & (sizeof(size_t) - 1)) { |
| 168 | if (n-- == 0) |
| 169 | return dest; |
| 170 | *d++ = *s++; |
| 171 | } |
| 172 | |
| 173 | while (n >= sizeof(size_t)) { |
| 174 | *(size_t *)d = *(const volatile size_t *)s; |
| 175 | d += sizeof(size_t); |
| 176 | s += sizeof(size_t); |
| 177 | n -= sizeof(size_t); |
| 178 | } |
| 179 | |
| 180 | while (n-- > 0) |
| 181 | *d++ = *s++; |
| 182 | |
| 183 | return dest; |
| 184 | } |
| 185 | |
| 186 | /* |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 187 | * calculate ip checksum (16 bit quantities) on a passed in buffer. In case |
| 188 | * the buffer length is odd last byte is excluded from the calculation |
| 189 | */ |
| 190 | static u16 ipchcksum(const void *addr, unsigned size) |
| 191 | { |
| 192 | const u16 *p = addr; |
| 193 | unsigned i, n = size / 2; /* don't expect odd sized blocks */ |
| 194 | u32 sum = 0; |
| 195 | |
| 196 | for (i = 0; i < n; i++) |
| 197 | sum += p[i]; |
| 198 | |
| 199 | sum = (sum >> 16) + (sum & 0xffff); |
| 200 | sum += (sum >> 16); |
| 201 | sum = ~sum & 0xffff; |
| 202 | return (u16) sum; |
| 203 | } |
| 204 | |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 205 | /* Find the first cbmem entry filling in the details. */ |
| 206 | static int find_cbmem_entry(uint32_t id, uint64_t *addr, size_t *size) |
| 207 | { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 208 | const uint8_t *table; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 209 | size_t offset; |
| 210 | int ret = -1; |
| 211 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 212 | table = mapping_virt(&lbtable_mapping); |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 213 | |
| 214 | if (table == NULL) |
| 215 | return -1; |
| 216 | |
| 217 | offset = 0; |
| 218 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 219 | while (offset < mapping_size(&lbtable_mapping)) { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 220 | const struct lb_record *lbr; |
| 221 | const struct lb_cbmem_entry *lbe; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 222 | |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 223 | lbr = (const void *)(table + offset); |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 224 | offset += lbr->size; |
| 225 | |
| 226 | if (lbr->tag != LB_TAG_CBMEM_ENTRY) |
| 227 | continue; |
| 228 | |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 229 | lbe = (const void *)lbr; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 230 | if (lbe->id != id) |
| 231 | continue; |
| 232 | |
| 233 | *addr = lbe->address; |
| 234 | *size = lbe->entry_size; |
| 235 | ret = 0; |
| 236 | break; |
| 237 | } |
| 238 | |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 239 | return ret; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 243 | * Try finding the timestamp table and coreboot cbmem console starting from the |
| 244 | * passed in memory offset. Could be called recursively in case a forwarding |
| 245 | * entry is found. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 246 | * |
Patrick Georgi | 220c209 | 2020-01-30 12:58:08 +0100 | [diff] [blame] | 247 | * Returns pointer to a memory buffer containing the timestamp table or zero if |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 248 | * none found. |
| 249 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 250 | |
| 251 | static struct lb_cbmem_ref timestamps; |
| 252 | static struct lb_cbmem_ref console; |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 253 | static struct lb_cbmem_ref tcpa_log; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 254 | static struct lb_memory_range cbmem; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 255 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 256 | /* This is a work-around for a nasty problem introduced by initially having |
| 257 | * pointer sized entries in the lb_cbmem_ref structures. This caused problems |
| 258 | * on 64bit x86 systems because coreboot is 32bit on those systems. |
| 259 | * When the problem was found, it was corrected, but there are a lot of |
| 260 | * systems out there with a firmware that does not produce the right |
| 261 | * lb_cbmem_ref structure. Hence we try to autocorrect this issue here. |
| 262 | */ |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 263 | static struct lb_cbmem_ref parse_cbmem_ref(const struct lb_cbmem_ref *cbmem_ref) |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 264 | { |
| 265 | struct lb_cbmem_ref ret; |
| 266 | |
Julius Werner | e3c2391 | 2018-11-26 15:57:59 -0800 | [diff] [blame] | 267 | aligned_memcpy(&ret, cbmem_ref, sizeof(ret)); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 268 | |
| 269 | if (cbmem_ref->size < sizeof(*cbmem_ref)) |
| 270 | ret.cbmem_addr = (uint32_t)ret.cbmem_addr; |
| 271 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 272 | debug(" cbmem_addr = %" PRIx64 "\n", ret.cbmem_addr); |
| 273 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame] | 274 | return ret; |
| 275 | } |
| 276 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 277 | static void parse_memory_tags(const struct lb_memory *mem) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 278 | { |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 279 | int num_entries; |
| 280 | int i; |
| 281 | |
| 282 | /* Peel off the header size and calculate the number of entries. */ |
| 283 | num_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]); |
| 284 | |
| 285 | for (i = 0; i < num_entries; i++) { |
| 286 | if (mem->map[i].type != LB_MEM_TABLE) |
| 287 | continue; |
| 288 | debug(" LB_MEM_TABLE found.\n"); |
| 289 | /* The last one found is CBMEM */ |
Aaron Durbin | eb72228 | 2019-01-31 09:40:26 -0700 | [diff] [blame] | 290 | aligned_memcpy(&cbmem, &mem->map[i], sizeof(cbmem)); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /* Return < 0 on error, 0 on success, 1 if forwarding table entry found. */ |
| 295 | static int parse_cbtable_entries(const struct mapping *table_mapping) |
| 296 | { |
| 297 | size_t i; |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 298 | const struct lb_record *lbr_p; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 299 | size_t table_size = mapping_size(table_mapping); |
| 300 | const void *lbtable = mapping_virt(table_mapping); |
| 301 | int forwarding_table_found = 0; |
| 302 | |
| 303 | for (i = 0; i < table_size; i += lbr_p->size) { |
| 304 | lbr_p = lbtable + i; |
| 305 | debug(" coreboot table entry 0x%02x\n", lbr_p->tag); |
| 306 | switch (lbr_p->tag) { |
| 307 | case LB_TAG_MEMORY: |
| 308 | debug(" Found memory map.\n"); |
| 309 | parse_memory_tags(lbtable + i); |
| 310 | continue; |
| 311 | case LB_TAG_TIMESTAMPS: { |
| 312 | debug(" Found timestamp table.\n"); |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 313 | timestamps = |
| 314 | parse_cbmem_ref((struct lb_cbmem_ref *)lbr_p); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 315 | continue; |
| 316 | } |
| 317 | case LB_TAG_CBMEM_CONSOLE: { |
| 318 | debug(" Found cbmem console.\n"); |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 319 | console = parse_cbmem_ref((struct lb_cbmem_ref *)lbr_p); |
| 320 | continue; |
| 321 | } |
| 322 | case LB_TAG_TCPA_LOG: { |
| 323 | debug(" Found tcpa log table.\n"); |
| 324 | tcpa_log = |
| 325 | parse_cbmem_ref((struct lb_cbmem_ref *)lbr_p); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 326 | continue; |
| 327 | } |
| 328 | case LB_TAG_FORWARD: { |
| 329 | int ret; |
| 330 | /* |
| 331 | * This is a forwarding entry - repeat the |
| 332 | * search at the new address. |
| 333 | */ |
| 334 | struct lb_forward lbf_p = |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 335 | *(const struct lb_forward *)lbr_p; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 336 | debug(" Found forwarding entry.\n"); |
| 337 | ret = parse_cbtable(lbf_p.forward, 0); |
| 338 | |
| 339 | /* Assume the forwarding entry is valid. If this fails |
| 340 | * then there's a total failure. */ |
| 341 | if (ret < 0) |
| 342 | return -1; |
| 343 | forwarding_table_found = 1; |
| 344 | } |
| 345 | default: |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return forwarding_table_found; |
| 351 | } |
| 352 | |
| 353 | /* Return < 0 on error, 0 on success. */ |
| 354 | static int parse_cbtable(u64 address, size_t table_size) |
| 355 | { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 356 | const void *buf; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 357 | struct mapping header_mapping; |
| 358 | size_t req_size; |
| 359 | size_t i; |
| 360 | |
| 361 | req_size = table_size; |
| 362 | /* Default to 4 KiB search space. */ |
| 363 | if (req_size == 0) |
| 364 | req_size = 4 * 1024; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 365 | |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 366 | debug("Looking for coreboot table at %" PRIx64 " %zd bytes.\n", |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 367 | address, req_size); |
| 368 | |
| 369 | buf = map_memory(&header_mapping, address, req_size); |
| 370 | |
Timothy Pearson | bea7140 | 2015-09-05 18:07:17 -0500 | [diff] [blame] | 371 | if (!buf) |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 372 | return -1; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 373 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 374 | /* look at every 16 bytes */ |
Aaron Durbin | cf20c90 | 2017-09-28 17:52:59 -0600 | [diff] [blame] | 375 | for (i = 0; i <= req_size - sizeof(struct lb_header); i += 16) { |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 376 | int ret; |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 377 | const struct lb_header *lbh; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 378 | struct mapping table_mapping; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 379 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 380 | lbh = buf + i; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 381 | if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) || |
| 382 | !lbh->header_bytes || |
| 383 | ipchcksum(lbh, sizeof(*lbh))) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 384 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 387 | /* Map in the whole table to parse. */ |
| 388 | if (!map_memory(&table_mapping, address + i + lbh->header_bytes, |
| 389 | lbh->table_bytes)) { |
| 390 | debug("Couldn't map in table\n"); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 391 | continue; |
| 392 | } |
| 393 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 394 | if (ipchcksum(mapping_virt(&table_mapping), lbh->table_bytes) != |
| 395 | lbh->table_checksum) { |
| 396 | debug("Signature found, but wrong checksum.\n"); |
| 397 | unmap_memory(&table_mapping); |
| 398 | continue; |
| 399 | } |
| 400 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 401 | debug("Found!\n"); |
| 402 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 403 | ret = parse_cbtable_entries(&table_mapping); |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 404 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 405 | /* Table parsing failed. */ |
| 406 | if (ret < 0) { |
| 407 | unmap_memory(&table_mapping); |
| 408 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 409 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 410 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 411 | /* Succeeded in parsing the table. Header not needed anymore. */ |
| 412 | unmap_memory(&header_mapping); |
| 413 | |
| 414 | /* |
| 415 | * Table parsing succeeded. If forwarding table not found update |
| 416 | * coreboot table mapping for future use. |
| 417 | */ |
| 418 | if (ret == 0) |
| 419 | lbtable_mapping = table_mapping; |
| 420 | else |
| 421 | unmap_memory(&table_mapping); |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | unmap_memory(&header_mapping); |
| 427 | |
| 428 | return -1; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 431 | #if defined(linux) && (defined(__i386__) || defined(__x86_64__)) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 432 | /* |
Aaron Durbin | 08e920e | 2016-03-12 08:41:34 +0100 | [diff] [blame] | 433 | * read CPU frequency from a sysfs file, return an frequency in Megahertz as |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 434 | * an int or exit on any error. |
| 435 | */ |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 436 | static unsigned long arch_tick_frequency(void) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 437 | { |
| 438 | FILE *cpuf; |
| 439 | char freqs[100]; |
| 440 | int size; |
| 441 | char *endp; |
| 442 | u64 rv; |
| 443 | |
| 444 | const char* freq_file = |
| 445 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 446 | |
| 447 | cpuf = fopen(freq_file, "r"); |
| 448 | if (!cpuf) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 449 | fprintf(stderr, "Could not open %s: %s\n", |
| 450 | freq_file, strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 451 | exit(1); |
| 452 | } |
| 453 | |
| 454 | memset(freqs, 0, sizeof(freqs)); |
| 455 | size = fread(freqs, 1, sizeof(freqs), cpuf); |
| 456 | if (!size || (size == sizeof(freqs))) { |
| 457 | fprintf(stderr, "Wrong number of bytes(%d) read from %s\n", |
| 458 | size, freq_file); |
| 459 | exit(1); |
| 460 | } |
| 461 | fclose(cpuf); |
| 462 | rv = strtoull(freqs, &endp, 10); |
| 463 | |
| 464 | if (*endp == '\0' || *endp == '\n') |
Aaron Durbin | 08e920e | 2016-03-12 08:41:34 +0100 | [diff] [blame] | 465 | /* cpuinfo_max_freq is in kHz. Convert it to MHz. */ |
| 466 | return rv / 1000; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 467 | fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n", |
| 468 | freqs, freq_file); |
| 469 | exit(1); |
| 470 | } |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 471 | #elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__)) |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 472 | static unsigned long arch_tick_frequency(void) |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 473 | { |
| 474 | int mib[2] = { CTL_HW, HW_CPUSPEED }; |
| 475 | static int value = 0; |
| 476 | size_t value_len = sizeof(value); |
| 477 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 478 | /* Return 1 MHz when sysctl fails. */ |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 479 | if ((value == 0) && (sysctl(mib, 2, &value, &value_len, NULL, 0) == -1)) |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 480 | return 1; |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 481 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 482 | return value; |
Patrick Georgi | d00f180 | 2015-07-13 16:53:50 +0200 | [diff] [blame] | 483 | } |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 484 | #else |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 485 | static unsigned long arch_tick_frequency(void) |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 486 | { |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 487 | /* 1 MHz = 1us. */ |
| 488 | return 1; |
Stefan Reinauer | d8ef9e9 | 2013-07-31 15:44:37 -0700 | [diff] [blame] | 489 | } |
| 490 | #endif |
| 491 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 492 | static unsigned long tick_freq_mhz; |
| 493 | |
| 494 | static void timestamp_set_tick_freq(unsigned long table_tick_freq_mhz) |
| 495 | { |
| 496 | tick_freq_mhz = table_tick_freq_mhz; |
| 497 | |
Martin Roth | ac9d6b8 | 2017-12-11 13:27:56 -0700 | [diff] [blame] | 498 | /* Honor table frequency if present. */ |
| 499 | if (!tick_freq_mhz) |
| 500 | tick_freq_mhz = arch_tick_frequency(); |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 501 | |
| 502 | if (!tick_freq_mhz) { |
| 503 | fprintf(stderr, "Cannot determine timestamp tick frequency.\n"); |
| 504 | exit(1); |
| 505 | } |
Martin Roth | ac9d6b8 | 2017-12-11 13:27:56 -0700 | [diff] [blame] | 506 | |
| 507 | debug("Timestamp tick frequency: %ld MHz\n", tick_freq_mhz); |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Jacob Garber | 79a2f47 | 2019-06-27 17:23:25 -0600 | [diff] [blame] | 510 | static u64 arch_convert_raw_ts_entry(u64 ts) |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 511 | { |
| 512 | return ts / tick_freq_mhz; |
| 513 | } |
| 514 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 515 | /* |
| 516 | * Print an integer in 'normalized' form - with commas separating every three |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 517 | * decimal orders. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 518 | */ |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 519 | static void print_norm(u64 v) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 520 | { |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 521 | if (v >= 1000) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 522 | /* print the higher order sections first */ |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 523 | print_norm(v / 1000); |
| 524 | printf(",%3.3u", (u32)(v % 1000)); |
| 525 | } else { |
| 526 | printf("%u", (u32)(v % 1000)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 527 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 530 | static const char *timestamp_name(uint32_t id) |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 531 | { |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 532 | for (size_t i = 0; i < ARRAY_SIZE(timestamp_ids); i++) { |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 533 | if (timestamp_ids[i].id == id) |
| 534 | return timestamp_ids[i].name; |
| 535 | } |
| 536 | return "<unknown>"; |
| 537 | } |
| 538 | |
| 539 | static uint64_t timestamp_print_parseable_entry(uint32_t id, uint64_t stamp, |
| 540 | uint64_t prev_stamp) |
| 541 | { |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 542 | const char *name; |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 543 | uint64_t step_time; |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 544 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 545 | name = timestamp_name(id); |
| 546 | |
| 547 | step_time = arch_convert_raw_ts_entry(stamp - prev_stamp); |
| 548 | |
| 549 | /* ID<tab>absolute time<tab>relative time<tab>description */ |
| 550 | printf("%d\t", id); |
| 551 | printf("%llu\t", (long long)arch_convert_raw_ts_entry(stamp)); |
| 552 | printf("%llu\t", (long long)step_time); |
| 553 | printf("%s\n", name); |
| 554 | |
| 555 | return step_time; |
| 556 | } |
| 557 | |
Jacob Garber | 79a2f47 | 2019-06-27 17:23:25 -0600 | [diff] [blame] | 558 | static uint64_t timestamp_print_entry(uint32_t id, uint64_t stamp, uint64_t prev_stamp) |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 559 | { |
| 560 | const char *name; |
| 561 | uint64_t step_time; |
| 562 | |
| 563 | name = timestamp_name(id); |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 564 | |
| 565 | printf("%4d:", id); |
Julius Werner | a7d9244 | 2014-12-02 20:51:19 -0800 | [diff] [blame] | 566 | printf("%-50s", name); |
| 567 | print_norm(arch_convert_raw_ts_entry(stamp)); |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 568 | step_time = arch_convert_raw_ts_entry(stamp - prev_stamp); |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 569 | if (prev_stamp) { |
| 570 | printf(" ("); |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 571 | print_norm(step_time); |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 572 | printf(")"); |
| 573 | } |
| 574 | printf("\n"); |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 575 | |
| 576 | return step_time; |
Stefan Reinauer | 0db924d | 2013-08-09 11:06:11 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 579 | static int compare_timestamp_entries(const void *a, const void *b) |
| 580 | { |
| 581 | const struct timestamp_entry *tse_a = (struct timestamp_entry *)a; |
| 582 | const struct timestamp_entry *tse_b = (struct timestamp_entry *)b; |
| 583 | |
Furquan Shaikh | 8f56732 | 2018-05-17 15:08:03 -0700 | [diff] [blame] | 584 | if (tse_a->entry_stamp > tse_b->entry_stamp) |
| 585 | return 1; |
| 586 | else if (tse_a->entry_stamp < tse_b->entry_stamp) |
| 587 | return -1; |
| 588 | |
| 589 | return 0; |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 590 | } |
| 591 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 592 | /* dump the timestamp table */ |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 593 | static void dump_timestamps(int mach_readable) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 594 | { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 595 | const struct timestamp_table *tst_p; |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 596 | struct timestamp_table *sorted_tst_p; |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 597 | size_t size; |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 598 | uint64_t prev_stamp; |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 599 | uint64_t total_time; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 600 | struct mapping timestamp_mapping; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 601 | |
| 602 | if (timestamps.tag != LB_TAG_TIMESTAMPS) { |
| 603 | fprintf(stderr, "No timestamps found in coreboot table.\n"); |
| 604 | return; |
| 605 | } |
| 606 | |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 607 | size = sizeof(*tst_p); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 608 | tst_p = map_memory(×tamp_mapping, timestamps.cbmem_addr, size); |
| 609 | if (!tst_p) |
| 610 | die("Unable to map timestamp header\n"); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 611 | |
Aaron Durbin | c49014e | 2015-08-30 21:19:55 -0500 | [diff] [blame] | 612 | timestamp_set_tick_freq(tst_p->tick_freq_mhz); |
| 613 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 614 | if (!mach_readable) |
| 615 | printf("%d entries total:\n\n", tst_p->num_entries); |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 616 | size += tst_p->num_entries * sizeof(tst_p->entries[0]); |
| 617 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 618 | unmap_memory(×tamp_mapping); |
| 619 | |
| 620 | tst_p = map_memory(×tamp_mapping, timestamps.cbmem_addr, size); |
| 621 | if (!tst_p) |
| 622 | die("Unable to map full timestamp table\n"); |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 623 | |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 624 | /* Report the base time within the table. */ |
| 625 | prev_stamp = 0; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 626 | if (mach_readable) |
| 627 | timestamp_print_parseable_entry(0, tst_p->base_time, |
| 628 | prev_stamp); |
| 629 | else |
| 630 | timestamp_print_entry(0, tst_p->base_time, prev_stamp); |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 631 | prev_stamp = tst_p->base_time; |
| 632 | |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 633 | sorted_tst_p = malloc(size); |
| 634 | if (!sorted_tst_p) |
| 635 | die("Failed to allocate memory"); |
Julius Werner | e3c2391 | 2018-11-26 15:57:59 -0800 | [diff] [blame] | 636 | aligned_memcpy(sorted_tst_p, tst_p, size); |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 637 | |
| 638 | qsort(&sorted_tst_p->entries[0], sorted_tst_p->num_entries, |
| 639 | sizeof(struct timestamp_entry), compare_timestamp_entries); |
| 640 | |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 641 | total_time = 0; |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 642 | for (uint32_t i = 0; i < sorted_tst_p->num_entries; i++) { |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 643 | uint64_t stamp; |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 644 | const struct timestamp_entry *tse = &sorted_tst_p->entries[i]; |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 645 | |
| 646 | /* Make all timestamps absolute. */ |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 647 | stamp = tse->entry_stamp + sorted_tst_p->base_time; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 648 | if (mach_readable) |
| 649 | total_time += |
| 650 | timestamp_print_parseable_entry(tse->entry_id, |
| 651 | stamp, prev_stamp); |
| 652 | else |
| 653 | total_time += timestamp_print_entry(tse->entry_id, |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 654 | stamp, prev_stamp); |
Aaron Durbin | 31540fb | 2015-07-11 12:44:10 -0500 | [diff] [blame] | 655 | prev_stamp = stamp; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 656 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 657 | |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 658 | if (!mach_readable) { |
| 659 | printf("\nTotal Time: "); |
| 660 | print_norm(total_time); |
| 661 | printf("\n"); |
| 662 | } |
Aaron Durbin | 799bf78 | 2015-08-06 13:52:08 -0500 | [diff] [blame] | 663 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 664 | unmap_memory(×tamp_mapping); |
Raul E Rangel | d4fec68 | 2018-05-11 11:08:07 -0600 | [diff] [blame] | 665 | free(sorted_tst_p); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 668 | /* dump the tcpa log table */ |
| 669 | static void dump_tcpa_log(void) |
| 670 | { |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 671 | const struct tcpa_table *tclt_p; |
| 672 | size_t size; |
| 673 | struct mapping tcpa_mapping; |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 674 | |
| 675 | if (tcpa_log.tag != LB_TAG_TCPA_LOG) { |
| 676 | fprintf(stderr, "No tcpa log found in coreboot table.\n"); |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | size = sizeof(*tclt_p); |
| 681 | tclt_p = map_memory(&tcpa_mapping, tcpa_log.cbmem_addr, size); |
| 682 | if (!tclt_p) |
| 683 | die("Unable to map tcpa log header\n"); |
| 684 | |
| 685 | size += tclt_p->num_entries * sizeof(tclt_p->entries[0]); |
| 686 | |
| 687 | unmap_memory(&tcpa_mapping); |
| 688 | |
| 689 | tclt_p = map_memory(&tcpa_mapping, tcpa_log.cbmem_addr, size); |
| 690 | if (!tclt_p) |
| 691 | die("Unable to map full tcpa log table\n"); |
| 692 | |
| 693 | printf("coreboot TCPA log:\n\n"); |
| 694 | |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 695 | for (uint16_t i = 0; i < tclt_p->num_entries; i++) { |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 696 | const struct tcpa_entry *tce = &tclt_p->entries[i]; |
| 697 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 698 | printf(" PCR-%u ", tce->pcr); |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 699 | |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 700 | for (uint32_t j = 0; j < tce->digest_length; j++) |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 701 | printf("%02x", tce->digest[j]); |
| 702 | |
| 703 | printf(" %s [%s]\n", tce->digest_type, tce->name); |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | unmap_memory(&tcpa_mapping); |
| 707 | } |
| 708 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 709 | struct cbmem_console { |
| 710 | u32 size; |
| 711 | u32 cursor; |
| 712 | u8 body[0]; |
| 713 | } __attribute__ ((__packed__)); |
| 714 | |
| 715 | #define CBMC_CURSOR_MASK ((1 << 28) - 1) |
| 716 | #define CBMC_OVERFLOW (1 << 31) |
| 717 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 718 | /* dump the cbmem console */ |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 719 | static void dump_console(int one_boot_only) |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 720 | { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 721 | const struct cbmem_console *console_p; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 722 | char *console_c; |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 723 | size_t size, cursor; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 724 | struct mapping console_mapping; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 725 | |
| 726 | if (console.tag != LB_TAG_CBMEM_CONSOLE) { |
| 727 | fprintf(stderr, "No console found in coreboot table.\n"); |
| 728 | return; |
| 729 | } |
| 730 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 731 | size = sizeof(*console_p); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 732 | console_p = map_memory(&console_mapping, console.cbmem_addr, size); |
| 733 | if (!console_p) |
| 734 | die("Unable to map console object.\n"); |
| 735 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 736 | cursor = console_p->cursor & CBMC_CURSOR_MASK; |
| 737 | if (!(console_p->cursor & CBMC_OVERFLOW) && cursor < console_p->size) |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 738 | size = cursor; |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 739 | else |
| 740 | size = console_p->size; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 741 | unmap_memory(&console_mapping); |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 742 | |
| 743 | console_c = malloc(size + 1); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 744 | if (!console_c) { |
| 745 | fprintf(stderr, "Not enough memory for console.\n"); |
| 746 | exit(1); |
| 747 | } |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 748 | console_c[size] = '\0'; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 749 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 750 | console_p = map_memory(&console_mapping, console.cbmem_addr, |
| 751 | size + sizeof(*console_p)); |
| 752 | |
| 753 | if (!console_p) |
| 754 | die("Unable to map full console object.\n"); |
| 755 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 756 | if (console_p->cursor & CBMC_OVERFLOW) { |
| 757 | if (cursor >= size) { |
| 758 | printf("cbmem: ERROR: CBMEM console struct is illegal, " |
| 759 | "output may be corrupt or out of order!\n\n"); |
| 760 | cursor = 0; |
| 761 | } |
| 762 | aligned_memcpy(console_c, console_p->body + cursor, |
| 763 | size - cursor); |
| 764 | aligned_memcpy(console_c + size - cursor, |
| 765 | console_p->body, cursor); |
| 766 | } else { |
| 767 | aligned_memcpy(console_c, console_p->body, size); |
| 768 | } |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 769 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 770 | /* Slight memory corruption may occur between reboots and give us a few |
| 771 | unprintable characters like '\0'. Replace them with '?' on output. */ |
| 772 | for (cursor = 0; cursor < size; cursor++) |
| 773 | if (!isprint(console_c[cursor]) && !isspace(console_c[cursor])) |
| 774 | console_c[cursor] = '?'; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 775 | |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 776 | /* We detect the last boot by looking for a bootblock, romstage or |
| 777 | ramstage banner, in that order (to account for platforms without |
| 778 | CONFIG_BOOTBLOCK_CONSOLE and/or CONFIG_EARLY_CONSOLE). Once we find |
| 779 | a banner, store the last match for that stage in cursor and stop. */ |
| 780 | cursor = 0; |
| 781 | if (one_boot_only) { |
You-Cheng Syu | 1430b39 | 2019-07-01 16:40:25 +0800 | [diff] [blame] | 782 | #define BANNER_REGEX(stage) \ |
| 783 | "\n\ncoreboot-[^\n]* " stage " starting.*\\.\\.\\.\n" |
Julius Werner | be3aa04 | 2017-06-12 17:35:15 -0700 | [diff] [blame] | 784 | #define OVERFLOW_REGEX(stage) "\n\\*\\*\\* Pre-CBMEM " stage " console overflow" |
Kangheui Won | 4b5c8b5 | 2020-10-07 14:29:38 +1100 | [diff] [blame] | 785 | const char *regex[] = { BANNER_REGEX("verstage-before-bootblock"), |
| 786 | BANNER_REGEX("bootblock"), |
You-Cheng Syu | 1430b39 | 2019-07-01 16:40:25 +0800 | [diff] [blame] | 787 | BANNER_REGEX("verstage"), |
Julius Werner | d906bb6 | 2017-05-16 13:54:18 -0700 | [diff] [blame] | 788 | OVERFLOW_REGEX("romstage"), |
Furquan Shaikh | 35972de | 2018-02-16 16:12:02 -0800 | [diff] [blame] | 789 | BANNER_REGEX("romstage"), |
| 790 | OVERFLOW_REGEX("ramstage"), |
| 791 | BANNER_REGEX("ramstage") }; |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 792 | |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 793 | for (size_t i = 0; !cursor && i < ARRAY_SIZE(regex); i++) { |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 794 | regex_t re; |
| 795 | regmatch_t match; |
| 796 | assert(!regcomp(&re, regex[i], 0)); |
| 797 | |
| 798 | /* Keep looking for matches so we find the last one. */ |
| 799 | while (!regexec(&re, console_c + cursor, 1, &match, 0)) |
| 800 | cursor += match.rm_so + 1; |
| 801 | regfree(&re); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | puts(console_c + cursor); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 806 | free(console_c); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 807 | unmap_memory(&console_mapping); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 810 | static void hexdump(unsigned long memory, int length) |
| 811 | { |
| 812 | int i; |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 813 | const uint8_t *m; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 814 | int all_zero = 0; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 815 | struct mapping hexdump_mapping; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 816 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 817 | m = map_memory(&hexdump_mapping, memory, length); |
| 818 | if (!m) |
| 819 | die("Unable to map hexdump memory.\n"); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 820 | |
| 821 | for (i = 0; i < length; i += 16) { |
| 822 | int j; |
| 823 | |
| 824 | all_zero++; |
| 825 | for (j = 0; j < 16; j++) { |
| 826 | if(m[i+j] != 0) { |
| 827 | all_zero = 0; |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (all_zero < 2) { |
| 833 | printf("%08lx:", memory + i); |
| 834 | for (j = 0; j < 16; j++) |
| 835 | printf(" %02x", m[i+j]); |
| 836 | printf(" "); |
| 837 | for (j = 0; j < 16; j++) |
| 838 | printf("%c", isprint(m[i+j]) ? m[i+j] : '.'); |
| 839 | printf("\n"); |
| 840 | } else if (all_zero == 2) { |
| 841 | printf("...\n"); |
| 842 | } |
| 843 | } |
| 844 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 845 | unmap_memory(&hexdump_mapping); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static void dump_cbmem_hex(void) |
| 849 | { |
| 850 | if (cbmem.type != LB_MEM_TABLE) { |
| 851 | fprintf(stderr, "No coreboot CBMEM area found!\n"); |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | hexdump(unpack_lb64(cbmem.start), unpack_lb64(cbmem.size)); |
| 856 | } |
| 857 | |
Jacob Garber | 79a2f47 | 2019-06-27 17:23:25 -0600 | [diff] [blame] | 858 | static void rawdump(uint64_t base, uint64_t size) |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 859 | { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 860 | const uint8_t *m; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 861 | struct mapping dump_mapping; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 862 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 863 | m = map_memory(&dump_mapping, base, size); |
| 864 | if (!m) |
| 865 | die("Unable to map rawdump memory\n"); |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 866 | |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 867 | for (uint64_t i = 0 ; i < size; i++) |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 868 | printf("%c", m[i]); |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 869 | |
| 870 | unmap_memory(&dump_mapping); |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | static void dump_cbmem_raw(unsigned int id) |
| 874 | { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 875 | const uint8_t *table; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 876 | size_t offset; |
| 877 | uint64_t base = 0; |
| 878 | uint64_t size = 0; |
| 879 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 880 | table = mapping_virt(&lbtable_mapping); |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 881 | |
| 882 | if (table == NULL) |
| 883 | return; |
| 884 | |
| 885 | offset = 0; |
| 886 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 887 | while (offset < mapping_size(&lbtable_mapping)) { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 888 | const struct lb_record *lbr; |
| 889 | const struct lb_cbmem_entry *lbe; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 890 | |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 891 | lbr = (const void *)(table + offset); |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 892 | offset += lbr->size; |
| 893 | |
| 894 | if (lbr->tag != LB_TAG_CBMEM_ENTRY) |
| 895 | continue; |
| 896 | |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 897 | lbe = (const void *)lbr; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 898 | if (lbe->id == id) { |
| 899 | debug("found id for raw dump %0x", lbe->id); |
| 900 | base = lbe->address; |
| 901 | size = lbe->entry_size; |
| 902 | break; |
| 903 | } |
| 904 | } |
| 905 | |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 906 | if (!base) |
| 907 | fprintf(stderr, "id %0x not found in cbtable\n", id); |
| 908 | else |
| 909 | rawdump(base, size); |
| 910 | } |
| 911 | |
Aaron Durbin | 0dff57d | 2015-03-05 21:18:33 -0600 | [diff] [blame] | 912 | struct cbmem_id_to_name { |
| 913 | uint32_t id; |
| 914 | const char *name; |
| 915 | }; |
Vadim Bendebury | 8b143c5 | 2014-05-14 10:12:55 -0700 | [diff] [blame] | 916 | 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] | 917 | |
Kyösti Mälkki | eab5c12 | 2017-09-04 11:10:17 +0300 | [diff] [blame] | 918 | #define MAX_STAGEx 10 |
Jacob Garber | 79a2f47 | 2019-06-27 17:23:25 -0600 | [diff] [blame] | 919 | static void cbmem_print_entry(int n, uint32_t id, uint64_t base, uint64_t size) |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 920 | { |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 921 | const char *name; |
Kyösti Mälkki | eab5c12 | 2017-09-04 11:10:17 +0300 | [diff] [blame] | 922 | char stage_x[20]; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 923 | |
| 924 | name = NULL; |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 925 | for (size_t i = 0; i < ARRAY_SIZE(cbmem_ids); i++) { |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 926 | if (cbmem_ids[i].id == id) { |
| 927 | name = cbmem_ids[i].name; |
| 928 | break; |
| 929 | } |
Kyösti Mälkki | eab5c12 | 2017-09-04 11:10:17 +0300 | [diff] [blame] | 930 | if (id >= CBMEM_ID_STAGEx_META && |
| 931 | id < CBMEM_ID_STAGEx_META + MAX_STAGEx) { |
| 932 | snprintf(stage_x, sizeof(stage_x), "STAGE%d META", |
| 933 | (id - CBMEM_ID_STAGEx_META)); |
| 934 | name = stage_x; |
| 935 | } |
| 936 | if (id >= CBMEM_ID_STAGEx_CACHE && |
| 937 | id < CBMEM_ID_STAGEx_CACHE + MAX_STAGEx) { |
| 938 | snprintf(stage_x, sizeof(stage_x), "STAGE%d $ ", |
| 939 | (id - CBMEM_ID_STAGEx_CACHE)); |
| 940 | name = stage_x; |
| 941 | } |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | printf("%2d. ", n); |
| 945 | if (name == NULL) |
Kyösti Mälkki | eab5c12 | 2017-09-04 11:10:17 +0300 | [diff] [blame] | 946 | printf("\t\t%08x", id); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 947 | else |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 948 | printf("%s\t%08x", name, id); |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 949 | printf(" %08" PRIx64 " ", base); |
| 950 | printf(" %08" PRIx64 "\n", size); |
| 951 | } |
| 952 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 953 | static void dump_cbmem_toc(void) |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 954 | { |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 955 | int i; |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 956 | const uint8_t *table; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 957 | size_t offset; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 958 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 959 | table = mapping_virt(&lbtable_mapping); |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 960 | |
| 961 | if (table == NULL) |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 962 | return; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 963 | |
| 964 | printf("CBMEM table of contents:\n"); |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 965 | printf(" NAME ID START LENGTH\n"); |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 966 | |
| 967 | i = 0; |
| 968 | offset = 0; |
| 969 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 970 | while (offset < mapping_size(&lbtable_mapping)) { |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 971 | const struct lb_record *lbr; |
| 972 | const struct lb_cbmem_entry *lbe; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 973 | |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 974 | lbr = (const void *)(table + offset); |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 975 | offset += lbr->size; |
| 976 | |
| 977 | if (lbr->tag != LB_TAG_CBMEM_ENTRY) |
| 978 | continue; |
| 979 | |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 980 | lbe = (const void *)lbr; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 981 | cbmem_print_entry(i, lbe->id, lbe->address, lbe->entry_size); |
| 982 | i++; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 983 | } |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 984 | } |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 985 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 986 | #define COVERAGE_MAGIC 0x584d4153 |
| 987 | struct file { |
| 988 | uint32_t magic; |
| 989 | uint32_t next; |
| 990 | uint32_t filename; |
| 991 | uint32_t data; |
| 992 | int offset; |
| 993 | int len; |
| 994 | }; |
| 995 | |
| 996 | static int mkpath(char *path, mode_t mode) |
| 997 | { |
| 998 | assert (path && *path); |
| 999 | char *p; |
| 1000 | for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) { |
| 1001 | *p = '\0'; |
| 1002 | if (mkdir(path, mode) == -1) { |
| 1003 | if (errno != EEXIST) { |
| 1004 | *p = '/'; |
| 1005 | return -1; |
| 1006 | } |
| 1007 | } |
| 1008 | *p = '/'; |
| 1009 | } |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static void dump_coverage(void) |
| 1014 | { |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1015 | uint64_t start; |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 1016 | size_t size; |
Aaron Durbin | f2a3822 | 2017-09-26 17:44:28 -0600 | [diff] [blame] | 1017 | const void *coverage; |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1018 | struct mapping coverage_mapping; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1019 | unsigned long phys_offset; |
| 1020 | #define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset) |
| 1021 | |
Aaron Durbin | 09c0c11 | 2015-09-30 12:33:01 -0500 | [diff] [blame] | 1022 | if (find_cbmem_entry(CBMEM_ID_COVERAGE, &start, &size)) { |
| 1023 | fprintf(stderr, "No coverage information found\n"); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1024 | return; |
| 1025 | } |
| 1026 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1027 | /* Map coverage area */ |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1028 | coverage = map_memory(&coverage_mapping, start, size); |
| 1029 | if (!coverage) |
| 1030 | die("Unable to map coverage area.\n"); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1031 | phys_offset = (unsigned long)coverage - (unsigned long)start; |
| 1032 | |
| 1033 | printf("Dumping coverage data...\n"); |
| 1034 | |
| 1035 | struct file *file = (struct file *)coverage; |
| 1036 | while (file && file->magic == COVERAGE_MAGIC) { |
| 1037 | FILE *f; |
| 1038 | char *filename; |
| 1039 | |
| 1040 | debug(" -> %s\n", (char *)phys_to_virt(file->filename)); |
| 1041 | filename = strdup((char *)phys_to_virt(file->filename)); |
| 1042 | if (mkpath(filename, 0755) == -1) { |
| 1043 | perror("Directory for coverage data could " |
| 1044 | "not be created"); |
| 1045 | exit(1); |
| 1046 | } |
| 1047 | f = fopen(filename, "wb"); |
| 1048 | if (!f) { |
| 1049 | printf("Could not open %s: %s\n", |
| 1050 | filename, strerror(errno)); |
| 1051 | exit(1); |
| 1052 | } |
| 1053 | if (fwrite((void *)phys_to_virt(file->data), |
| 1054 | file->len, 1, f) != 1) { |
| 1055 | printf("Could not write to %s: %s\n", |
| 1056 | filename, strerror(errno)); |
| 1057 | exit(1); |
| 1058 | } |
| 1059 | fclose(f); |
| 1060 | free(filename); |
| 1061 | |
| 1062 | if (file->next) |
| 1063 | file = (struct file *)phys_to_virt(file->next); |
| 1064 | else |
| 1065 | file = NULL; |
| 1066 | } |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1067 | unmap_memory(&coverage_mapping); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static void print_version(void) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1071 | { |
| 1072 | printf("cbmem v%s -- ", CBMEM_VERSION); |
| 1073 | printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n"); |
| 1074 | printf( |
| 1075 | "This program is free software: you can redistribute it and/or modify\n" |
| 1076 | "it under the terms of the GNU General Public License as published by\n" |
| 1077 | "the Free Software Foundation, version 2 of the License.\n\n" |
| 1078 | "This program is distributed in the hope that it will be useful,\n" |
| 1079 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 1080 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
Martin Roth | eb20e60 | 2016-01-12 13:30:50 -0700 | [diff] [blame] | 1081 | "GNU General Public License for more details.\n\n"); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Martin Roth | 8448ac4 | 2016-09-11 15:43:22 -0600 | [diff] [blame] | 1084 | static void print_usage(const char *name, int exit_code) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1085 | { |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1086 | printf("usage: %s [-cCltTLxVvh?]\n", name); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1087 | printf("\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1088 | " -c | --console: print cbmem console\n" |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 1089 | " -1 | --oneboot: print cbmem console for last boot only\n" |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1090 | " -C | --coverage: dump coverage information\n" |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1091 | " -l | --list: print cbmem table of contents\n" |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1092 | " -x | --hexdump: print hexdump of cbmem area\n" |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 1093 | " -r | --rawdump ID: print rawdump of specific ID (in hex) of cbtable\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1094 | " -t | --timestamps: print timestamp information\n" |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1095 | " -T | --parseable-timestamps: print parseable timestamps\n" |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1096 | " -L | --tcpa-log print TCPA log\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1097 | " -V | --verbose: verbose (debugging) output\n" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1098 | " -v | --version: print the version\n" |
| 1099 | " -h | --help: print this help\n" |
| 1100 | "\n"); |
Martin Roth | 8448ac4 | 2016-09-11 15:43:22 -0600 | [diff] [blame] | 1101 | exit(exit_code); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1102 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1103 | |
Adam Kallai | 66c2250 | 2018-11-30 11:31:50 +0100 | [diff] [blame] | 1104 | #if defined(__arm__) || defined(__aarch64__) |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1105 | static void dt_update_cells(const char *name, int *addr_cells_ptr, |
| 1106 | int *size_cells_ptr) |
| 1107 | { |
| 1108 | if (*addr_cells_ptr >= 0 && *size_cells_ptr >= 0) |
| 1109 | return; |
| 1110 | |
| 1111 | int buffer; |
| 1112 | size_t nlen = strlen(name); |
| 1113 | char *prop = alloca(nlen + sizeof("/#address-cells")); |
| 1114 | strcpy(prop, name); |
| 1115 | |
| 1116 | if (*addr_cells_ptr < 0) { |
| 1117 | strcpy(prop + nlen, "/#address-cells"); |
| 1118 | int fd = open(prop, O_RDONLY); |
| 1119 | if (fd < 0 && errno != ENOENT) { |
| 1120 | perror(prop); |
| 1121 | } else if (fd >= 0) { |
| 1122 | if (read(fd, &buffer, sizeof(int)) < 0) |
| 1123 | perror(prop); |
| 1124 | else |
| 1125 | *addr_cells_ptr = ntohl(buffer); |
| 1126 | close(fd); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | if (*size_cells_ptr < 0) { |
| 1131 | strcpy(prop + nlen, "/#size-cells"); |
| 1132 | int fd = open(prop, O_RDONLY); |
| 1133 | if (fd < 0 && errno != ENOENT) { |
| 1134 | perror(prop); |
| 1135 | } else if (fd >= 0) { |
| 1136 | if (read(fd, &buffer, sizeof(int)) < 0) |
| 1137 | perror(prop); |
| 1138 | else |
| 1139 | *size_cells_ptr = ntohl(buffer); |
| 1140 | close(fd); |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | static char *dt_find_compat(const char *parent, const char *compat, |
| 1146 | int *addr_cells_ptr, int *size_cells_ptr) |
| 1147 | { |
| 1148 | char *ret = NULL; |
| 1149 | struct dirent *entry; |
| 1150 | DIR *dir; |
| 1151 | |
| 1152 | if (!(dir = opendir(parent))) { |
| 1153 | perror(parent); |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
| 1157 | /* Loop through all files in the directory (DT node). */ |
| 1158 | while ((entry = readdir(dir))) { |
| 1159 | /* We only care about compatible props or subnodes. */ |
| 1160 | if (entry->d_name[0] == '.' || !((entry->d_type & DT_DIR) || |
| 1161 | !strcmp(entry->d_name, "compatible"))) |
| 1162 | continue; |
| 1163 | |
| 1164 | /* Assemble the file name (on the stack, for speed). */ |
| 1165 | size_t plen = strlen(parent); |
| 1166 | char *name = alloca(plen + strlen(entry->d_name) + 2); |
| 1167 | |
| 1168 | strcpy(name, parent); |
| 1169 | name[plen] = '/'; |
| 1170 | strcpy(name + plen + 1, entry->d_name); |
| 1171 | |
| 1172 | /* If it's a subnode, recurse. */ |
| 1173 | if (entry->d_type & DT_DIR) { |
| 1174 | ret = dt_find_compat(name, compat, addr_cells_ptr, |
| 1175 | size_cells_ptr); |
| 1176 | |
| 1177 | /* There is only one matching node to find, abort. */ |
| 1178 | if (ret) { |
| 1179 | /* Gather cells values on the way up. */ |
| 1180 | dt_update_cells(parent, addr_cells_ptr, |
| 1181 | size_cells_ptr); |
| 1182 | break; |
| 1183 | } |
| 1184 | continue; |
| 1185 | } |
| 1186 | |
| 1187 | /* If it's a compatible string, see if it's the right one. */ |
| 1188 | int fd = open(name, O_RDONLY); |
| 1189 | int clen = strlen(compat); |
| 1190 | char *buffer = alloca(clen + 1); |
| 1191 | |
| 1192 | if (fd < 0) { |
| 1193 | perror(name); |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | if (read(fd, buffer, clen + 1) < 0) { |
| 1198 | perror(name); |
| 1199 | close(fd); |
| 1200 | continue; |
| 1201 | } |
| 1202 | close(fd); |
| 1203 | |
| 1204 | if (!strcmp(compat, buffer)) { |
| 1205 | /* Initialize these to "unset" for the way up. */ |
| 1206 | *addr_cells_ptr = *size_cells_ptr = -1; |
| 1207 | |
| 1208 | /* Can't leave string on the stack or we'll lose it! */ |
| 1209 | ret = strdup(parent); |
| 1210 | break; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | closedir(dir); |
| 1215 | return ret; |
| 1216 | } |
Adam Kallai | 66c2250 | 2018-11-30 11:31:50 +0100 | [diff] [blame] | 1217 | #endif /* defined(__arm__) || defined(__aarch64__) */ |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1218 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1219 | int main(int argc, char** argv) |
| 1220 | { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1221 | int print_defaults = 1; |
| 1222 | int print_console = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1223 | int print_coverage = 0; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1224 | int print_list = 0; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1225 | int print_hexdump = 0; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 1226 | int print_rawdump = 0; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1227 | int print_timestamps = 0; |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1228 | int print_tcpa_log = 0; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1229 | int machine_readable_timestamps = 0; |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 1230 | int one_boot_only = 0; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 1231 | unsigned int rawdump_id = 0; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1232 | |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1233 | int opt, option_index = 0; |
| 1234 | static struct option long_options[] = { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1235 | {"console", 0, 0, 'c'}, |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 1236 | {"oneboot", 0, 0, '1'}, |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1237 | {"coverage", 0, 0, 'C'}, |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1238 | {"list", 0, 0, 'l'}, |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1239 | {"tcpa-log", 0, 0, 'L'}, |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1240 | {"timestamps", 0, 0, 't'}, |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1241 | {"parseable-timestamps", 0, 0, 'T'}, |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1242 | {"hexdump", 0, 0, 'x'}, |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 1243 | {"rawdump", required_argument, 0, 'r'}, |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1244 | {"verbose", 0, 0, 'V'}, |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1245 | {"version", 0, 0, 'v'}, |
| 1246 | {"help", 0, 0, 'h'}, |
| 1247 | {0, 0, 0, 0} |
| 1248 | }; |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1249 | while ((opt = getopt_long(argc, argv, "c1CltTLxVvh?r:", |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1250 | long_options, &option_index)) != EOF) { |
| 1251 | switch (opt) { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1252 | case 'c': |
| 1253 | print_console = 1; |
| 1254 | print_defaults = 0; |
| 1255 | break; |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 1256 | case '1': |
| 1257 | print_console = 1; |
| 1258 | one_boot_only = 1; |
| 1259 | print_defaults = 0; |
| 1260 | break; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1261 | case 'C': |
| 1262 | print_coverage = 1; |
| 1263 | print_defaults = 0; |
| 1264 | break; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1265 | case 'l': |
| 1266 | print_list = 1; |
| 1267 | print_defaults = 0; |
| 1268 | break; |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1269 | case 'L': |
| 1270 | print_tcpa_log = 1; |
| 1271 | print_defaults = 0; |
| 1272 | break; |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1273 | case 'x': |
| 1274 | print_hexdump = 1; |
| 1275 | print_defaults = 0; |
| 1276 | break; |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 1277 | case 'r': |
| 1278 | print_rawdump = 1; |
| 1279 | print_defaults = 0; |
| 1280 | rawdump_id = strtoul(optarg, NULL, 16); |
| 1281 | break; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1282 | case 't': |
| 1283 | print_timestamps = 1; |
| 1284 | print_defaults = 0; |
| 1285 | break; |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1286 | case 'T': |
| 1287 | print_timestamps = 1; |
| 1288 | machine_readable_timestamps = 1; |
| 1289 | print_defaults = 0; |
| 1290 | break; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1291 | case 'V': |
| 1292 | verbose = 1; |
| 1293 | break; |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1294 | case 'v': |
| 1295 | print_version(); |
| 1296 | exit(0); |
| 1297 | break; |
| 1298 | case 'h': |
Martin Roth | 8448ac4 | 2016-09-11 15:43:22 -0600 | [diff] [blame] | 1299 | print_usage(argv[0], 0); |
| 1300 | break; |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1301 | case '?': |
| 1302 | default: |
Martin Roth | 8448ac4 | 2016-09-11 15:43:22 -0600 | [diff] [blame] | 1303 | print_usage(argv[0], 1); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 1304 | break; |
| 1305 | } |
| 1306 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1307 | |
Arthur Heymans | 8cd17ea | 2018-08-01 17:21:32 +0200 | [diff] [blame] | 1308 | if (optind < argc) { |
| 1309 | fprintf(stderr, "Error: Extra parameter found.\n"); |
| 1310 | print_usage(argv[0], 1); |
| 1311 | } |
| 1312 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1313 | mem_fd = open("/dev/mem", O_RDONLY, 0); |
| 1314 | if (mem_fd < 0) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1315 | fprintf(stderr, "Failed to gain memory access: %s\n", |
| 1316 | strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1317 | return 1; |
| 1318 | } |
| 1319 | |
Adam Kallai | 66c2250 | 2018-11-30 11:31:50 +0100 | [diff] [blame] | 1320 | #if defined(__arm__) || defined(__aarch64__) |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1321 | int addr_cells, size_cells; |
| 1322 | char *coreboot_node = dt_find_compat("/proc/device-tree", "coreboot", |
| 1323 | &addr_cells, &size_cells); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1324 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1325 | if (!coreboot_node) { |
| 1326 | fprintf(stderr, "Could not find 'coreboot' compatible node!\n"); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1327 | return 1; |
| 1328 | } |
| 1329 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1330 | if (addr_cells < 0) { |
| 1331 | fprintf(stderr, "Warning: no #address-cells node in tree!\n"); |
| 1332 | addr_cells = 1; |
| 1333 | } |
| 1334 | |
| 1335 | int nlen = strlen(coreboot_node); |
| 1336 | char *reg = alloca(nlen + sizeof("/reg")); |
| 1337 | |
| 1338 | strcpy(reg, coreboot_node); |
| 1339 | strcpy(reg + nlen, "/reg"); |
| 1340 | free(coreboot_node); |
| 1341 | |
| 1342 | int fd = open(reg, O_RDONLY); |
| 1343 | if (fd < 0) { |
| 1344 | perror(reg); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1345 | return 1; |
| 1346 | } |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1347 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1348 | int i; |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 1349 | size_t size_to_read = addr_cells * 4 + size_cells * 4; |
| 1350 | u8 *dtbuffer = alloca(size_to_read); |
| 1351 | if (read(fd, dtbuffer, size_to_read) < 0) { |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1352 | perror(reg); |
| 1353 | return 1; |
| 1354 | } |
| 1355 | close(fd); |
| 1356 | |
| 1357 | /* No variable-length byte swap function anywhere in C... how sad. */ |
| 1358 | u64 baseaddr = 0; |
| 1359 | for (i = 0; i < addr_cells * 4; i++) { |
| 1360 | baseaddr <<= 8; |
Aaron Durbin | b58f9e3 | 2014-10-07 14:56:35 -0500 | [diff] [blame] | 1361 | baseaddr |= *dtbuffer; |
| 1362 | dtbuffer++; |
| 1363 | } |
| 1364 | u64 cb_table_size = 0; |
| 1365 | for (i = 0; i < size_cells * 4; i++) { |
| 1366 | cb_table_size <<= 8; |
| 1367 | cb_table_size |= *dtbuffer; |
| 1368 | dtbuffer++; |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1369 | } |
| 1370 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1371 | parse_cbtable(baseaddr, cb_table_size); |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1372 | #else |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1373 | unsigned long long possible_base_addresses[] = { 0, 0xf0000 }; |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1374 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1375 | /* Find and parse coreboot table */ |
Jacob Garber | 414d5d8 | 2019-06-27 16:02:32 -0600 | [diff] [blame] | 1376 | for (size_t j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) { |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1377 | if (!parse_cbtable(possible_base_addresses[j], 0)) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1378 | break; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1379 | } |
Stefan Reinauer | 7f68150 | 2013-06-19 15:39:09 -0700 | [diff] [blame] | 1380 | #endif |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1381 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1382 | if (mapping_virt(&lbtable_mapping) == NULL) |
| 1383 | die("Table not found.\n"); |
| 1384 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1385 | if (print_console) |
Julius Werner | b7b64a9 | 2017-04-28 16:31:46 -0700 | [diff] [blame] | 1386 | dump_console(one_boot_only); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1387 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 1388 | if (print_coverage) |
| 1389 | dump_coverage(); |
| 1390 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 1391 | if (print_list) |
| 1392 | dump_cbmem_toc(); |
| 1393 | |
Stefan Reinauer | a9c8361 | 2013-07-16 17:47:35 -0700 | [diff] [blame] | 1394 | if (print_hexdump) |
| 1395 | dump_cbmem_hex(); |
| 1396 | |
Pratik Prajapati | c29e57d | 2015-09-03 12:58:44 -0700 | [diff] [blame] | 1397 | if (print_rawdump) |
| 1398 | dump_cbmem_raw(rawdump_id); |
| 1399 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 1400 | if (print_defaults || print_timestamps) |
Aaron Durbin | fbff301 | 2015-08-30 22:00:12 -0500 | [diff] [blame] | 1401 | dump_timestamps(machine_readable_timestamps); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 1402 | |
Philipp Deppenwiese | fa1f6ff | 2018-05-13 12:42:42 +0200 | [diff] [blame] | 1403 | if (print_tcpa_log) |
| 1404 | dump_tcpa_log(); |
| 1405 | |
Aaron Durbin | 46300aa | 2017-09-26 16:22:38 -0600 | [diff] [blame] | 1406 | unmap_memory(&lbtable_mapping); |
| 1407 | |
Julius Werner | 337de4c | 2014-06-16 23:02:03 -0700 | [diff] [blame] | 1408 | close(mem_fd); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1409 | return 0; |
| 1410 | } |