Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 The ChromiumOS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
Nico Huber | 8e4bb928 | 2013-05-26 18:17:54 +0200 | [diff] [blame] | 20 | #include <inttypes.h> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame^] | 25 | #include <inttypes.h> |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 26 | #include <getopt.h> |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 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> |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 34 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 35 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 36 | #define MAP_BYTES (1024*1024) |
| 37 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 38 | #include "boot/coreboot_tables.h" |
| 39 | |
| 40 | typedef uint16_t u16; |
| 41 | typedef uint32_t u32; |
| 42 | typedef uint64_t u64; |
| 43 | |
| 44 | #include "cbmem.h" |
| 45 | #include "timestamp.h" |
| 46 | |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 47 | #define CBMEM_VERSION "1.0" |
| 48 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 49 | /* verbose output? */ |
| 50 | static int verbose = 0; |
| 51 | #define debug(x...) if(verbose) printf(x) |
| 52 | |
| 53 | /* File handle used to access /dev/mem */ |
| 54 | static int fd; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * calculate ip checksum (16 bit quantities) on a passed in buffer. In case |
| 58 | * the buffer length is odd last byte is excluded from the calculation |
| 59 | */ |
| 60 | static u16 ipchcksum(const void *addr, unsigned size) |
| 61 | { |
| 62 | const u16 *p = addr; |
| 63 | unsigned i, n = size / 2; /* don't expect odd sized blocks */ |
| 64 | u32 sum = 0; |
| 65 | |
| 66 | for (i = 0; i < n; i++) |
| 67 | sum += p[i]; |
| 68 | |
| 69 | sum = (sum >> 16) + (sum & 0xffff); |
| 70 | sum += (sum >> 16); |
| 71 | sum = ~sum & 0xffff; |
| 72 | return (u16) sum; |
| 73 | } |
| 74 | |
| 75 | /* |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 76 | * Functions to map / unmap physical memory into virtual address space. These |
| 77 | * 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] | 78 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 79 | static void *mapped_virtual; |
| 80 | static void *map_memory(u64 physical) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 81 | { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 82 | void *v; |
| 83 | off_t p; |
| 84 | int page = getpagesize(); |
| 85 | |
| 86 | /* Mapped memory must be aligned to page size */ |
| 87 | p = physical & ~(page - 1); |
| 88 | |
Paul Menzel | 8d9ffd9 | 2013-04-13 18:25:56 +0200 | [diff] [blame] | 89 | debug("Mapping 1MB of physical memory at 0x%jx.\n", (intmax_t)p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 90 | |
| 91 | v = mmap(NULL, MAP_BYTES, PROT_READ, MAP_SHARED, fd, p); |
| 92 | |
| 93 | if (v == MAP_FAILED) { |
| 94 | fprintf(stderr, "Failed to mmap /dev/mem: %s\n", |
| 95 | strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 96 | exit(1); |
| 97 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 98 | |
| 99 | /* Remember what we actually mapped ... */ |
| 100 | mapped_virtual = v; |
| 101 | |
| 102 | /* ... but return address to the physical memory that was requested */ |
| 103 | v += physical & (page-1); |
| 104 | |
| 105 | return v; |
| 106 | } |
| 107 | |
| 108 | static void unmap_memory(void) |
| 109 | { |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 110 | if (mapped_virtual == NULL) { |
| 111 | fprintf(stderr, "Error unmapping memory\n"); |
| 112 | return; |
| 113 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 114 | debug("Unmapping 1MB of virtual memory at %p.\n", mapped_virtual); |
| 115 | munmap(mapped_virtual, MAP_BYTES); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 116 | mapped_virtual = NULL; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 120 | * Try finding the timestamp table and coreboot cbmem console starting from the |
| 121 | * passed in memory offset. Could be called recursively in case a forwarding |
| 122 | * entry is found. |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 123 | * |
| 124 | * Returns pointer to a memory buffer containg the timestamp table or zero if |
| 125 | * none found. |
| 126 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 127 | |
| 128 | static struct lb_cbmem_ref timestamps; |
| 129 | static struct lb_cbmem_ref console; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 130 | static struct lb_memory_range cbmem; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 131 | |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame^] | 132 | /* This is a work-around for a nasty problem introduced by initially having |
| 133 | * pointer sized entries in the lb_cbmem_ref structures. This caused problems |
| 134 | * on 64bit x86 systems because coreboot is 32bit on those systems. |
| 135 | * When the problem was found, it was corrected, but there are a lot of |
| 136 | * systems out there with a firmware that does not produce the right |
| 137 | * lb_cbmem_ref structure. Hence we try to autocorrect this issue here. |
| 138 | */ |
| 139 | static struct lb_cbmem_ref parse_cbmem_ref(struct lb_cbmem_ref *cbmem_ref) |
| 140 | { |
| 141 | struct lb_cbmem_ref ret; |
| 142 | |
| 143 | ret = *cbmem_ref; |
| 144 | |
| 145 | if (cbmem_ref->size < sizeof(*cbmem_ref)) |
| 146 | ret.cbmem_addr = (uint32_t)ret.cbmem_addr; |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 151 | static int parse_cbtable(u64 address) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 152 | { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 153 | int i, found = 0; |
| 154 | void *buf; |
| 155 | |
Nico Huber | 8e4bb928 | 2013-05-26 18:17:54 +0200 | [diff] [blame] | 156 | debug("Looking for coreboot table at %" PRIx64 "\n", address); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 157 | buf = map_memory(address); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 158 | |
| 159 | /* look at every 16 bytes within 4K of the base */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 160 | |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 161 | for (i = 0; i < 0x1000; i += 0x10) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 162 | struct lb_header *lbh; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 163 | struct lb_record* lbr_p; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 164 | void *lbtable; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 165 | int j; |
| 166 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 167 | lbh = (struct lb_header *)(buf + i); |
| 168 | if (memcmp(lbh->signature, "LBIO", sizeof(lbh->signature)) || |
| 169 | !lbh->header_bytes || |
| 170 | ipchcksum(lbh, sizeof(*lbh))) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 171 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 172 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 173 | lbtable = buf + i + lbh->header_bytes; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 174 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 175 | if (ipchcksum(lbtable, lbh->table_bytes) != |
| 176 | lbh->table_checksum) { |
| 177 | debug("Signature found, but wrong checksum.\n"); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 178 | continue; |
| 179 | } |
| 180 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 181 | found = 1; |
| 182 | debug("Found!\n"); |
| 183 | |
| 184 | for (j = 0; j < lbh->table_bytes; j += lbr_p->size) { |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 185 | /* look for the timestamp table */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 186 | lbr_p = (struct lb_record*) ((char *)lbtable + j); |
| 187 | debug(" coreboot table entry 0x%02x\n", lbr_p->tag); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 188 | switch (lbr_p->tag) { |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 189 | case LB_TAG_MEMORY: { |
| 190 | int i = 0; |
| 191 | debug(" Found memory map.\n"); |
| 192 | struct lb_memory *memory = |
| 193 | (struct lb_memory *)lbr_p; |
| 194 | while ((char *)&memory->map[i] < ((char *)lbtable |
| 195 | + lbr_p->size)) { |
| 196 | if (memory->map[i].type == LB_MEM_TABLE) { |
| 197 | debug(" LB_MEM_TABLE found.\n"); |
| 198 | /* The last one found is CBMEM */ |
| 199 | cbmem = memory->map[i]; |
| 200 | } |
| 201 | i++; |
| 202 | } |
| 203 | continue; |
| 204 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 205 | case LB_TAG_TIMESTAMPS: { |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 206 | debug(" Found timestamp table.\n"); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame^] | 207 | timestamps = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 208 | continue; |
| 209 | } |
| 210 | case LB_TAG_CBMEM_CONSOLE: { |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 211 | debug(" Found cbmem console.\n"); |
Stefan Reinauer | 8c59477 | 2013-04-19 14:22:29 -0700 | [diff] [blame^] | 212 | console = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 213 | continue; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 214 | } |
| 215 | case LB_TAG_FORWARD: { |
| 216 | /* |
| 217 | * This is a forwarding entry - repeat the |
| 218 | * search at the new address. |
| 219 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 220 | struct lb_forward lbf_p = |
| 221 | *(struct lb_forward *) lbr_p; |
Stefan Reinauer | d7144dc | 2013-01-07 15:25:37 -0800 | [diff] [blame] | 222 | debug(" Found forwarding entry.\n"); |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 223 | unmap_memory(); |
| 224 | return parse_cbtable(lbf_p.forward); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 225 | } |
| 226 | default: |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 232 | unmap_memory(); |
| 233 | |
| 234 | return found; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
| 238 | * read CPU frequency from a sysfs file, return an frequency in Kilohertz as |
| 239 | * an int or exit on any error. |
| 240 | */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 241 | static u64 get_cpu_freq_KHz(void) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 242 | { |
| 243 | FILE *cpuf; |
| 244 | char freqs[100]; |
| 245 | int size; |
| 246 | char *endp; |
| 247 | u64 rv; |
| 248 | |
| 249 | const char* freq_file = |
| 250 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 251 | |
| 252 | cpuf = fopen(freq_file, "r"); |
| 253 | if (!cpuf) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 254 | fprintf(stderr, "Could not open %s: %s\n", |
| 255 | freq_file, strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 256 | exit(1); |
| 257 | } |
| 258 | |
| 259 | memset(freqs, 0, sizeof(freqs)); |
| 260 | size = fread(freqs, 1, sizeof(freqs), cpuf); |
| 261 | if (!size || (size == sizeof(freqs))) { |
| 262 | fprintf(stderr, "Wrong number of bytes(%d) read from %s\n", |
| 263 | size, freq_file); |
| 264 | exit(1); |
| 265 | } |
| 266 | fclose(cpuf); |
| 267 | rv = strtoull(freqs, &endp, 10); |
| 268 | |
| 269 | if (*endp == '\0' || *endp == '\n') |
| 270 | return rv; |
| 271 | fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n", |
| 272 | freqs, freq_file); |
| 273 | exit(1); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Print an integer in 'normalized' form - with commas separating every three |
| 278 | * decimal orders. The 'comma' parameter indicates if a comma is needed after |
| 279 | * the value is printed. |
| 280 | */ |
| 281 | static void print_norm(u64 v, int comma) |
| 282 | { |
| 283 | int first_triple = 1; |
| 284 | |
| 285 | if (v > 1000) { |
| 286 | /* print the higher order sections first */ |
| 287 | print_norm(v / 1000, 1); |
| 288 | first_triple = 0; |
| 289 | } |
| 290 | if (first_triple) |
| 291 | printf("%d", (u32)(v % 1000)); |
| 292 | else |
| 293 | printf("%3.3d", (u32)(v % 1000)); |
| 294 | if (comma) |
| 295 | printf(","); |
| 296 | } |
| 297 | |
| 298 | /* dump the timestamp table */ |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 299 | static void dump_timestamps(void) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 300 | { |
| 301 | int i; |
| 302 | u64 cpu_freq_MHz = get_cpu_freq_KHz() / 1000; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 303 | struct timestamp_table *tst_p; |
| 304 | |
| 305 | if (timestamps.tag != LB_TAG_TIMESTAMPS) { |
| 306 | fprintf(stderr, "No timestamps found in coreboot table.\n"); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | tst_p = (struct timestamp_table *) |
| 311 | map_memory((unsigned long)timestamps.cbmem_addr); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 312 | |
| 313 | printf("%d entries total:\n\n", tst_p->num_entries); |
| 314 | for (i = 0; i < tst_p->num_entries; i++) { |
| 315 | const struct timestamp_entry *tse_p = tst_p->entries + i; |
| 316 | |
| 317 | printf("%4d:", tse_p->entry_id); |
| 318 | print_norm(tse_p->entry_stamp / cpu_freq_MHz, 0); |
| 319 | if (i) { |
| 320 | printf(" ("); |
| 321 | print_norm((tse_p->entry_stamp - |
| 322 | tse_p[-1].entry_stamp) / |
| 323 | cpu_freq_MHz, 0); |
| 324 | printf(")"); |
| 325 | } |
| 326 | printf("\n"); |
| 327 | } |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 328 | |
| 329 | unmap_memory(); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 332 | /* dump the cbmem console */ |
| 333 | static void dump_console(void) |
| 334 | { |
| 335 | void *console_p; |
| 336 | char *console_c; |
| 337 | uint32_t size; |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 338 | uint32_t cursor; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 339 | |
| 340 | if (console.tag != LB_TAG_CBMEM_CONSOLE) { |
| 341 | fprintf(stderr, "No console found in coreboot table.\n"); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | console_p = map_memory((unsigned long)console.cbmem_addr); |
| 346 | /* The in-memory format of the console area is: |
| 347 | * u32 size |
| 348 | * u32 cursor |
| 349 | * char console[size] |
| 350 | * Hence we have to add 8 to get to the actual console string. |
| 351 | */ |
| 352 | size = *(uint32_t *)console_p; |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 353 | cursor = *(uint32_t *) (console_p + 4); |
| 354 | /* Cursor continues to go on even after no more data fits in |
| 355 | * the buffer but the data is dropped in this case. |
| 356 | */ |
| 357 | if (size > cursor) |
| 358 | size = cursor; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 359 | console_c = malloc(size + 1); |
| 360 | if (!console_c) { |
| 361 | fprintf(stderr, "Not enough memory for console.\n"); |
| 362 | exit(1); |
| 363 | } |
| 364 | |
| 365 | memcpy(console_c, console_p + 8, size); |
| 366 | console_c[size] = 0; |
| 367 | |
Vladimir Serbinenko | f4a0d01 | 2013-03-30 12:15:12 +0100 | [diff] [blame] | 368 | printf("%s\n", console_c); |
| 369 | if (size < cursor) |
| 370 | printf("%d %s lost\n", cursor - size, |
| 371 | (cursor - size) == 1 ? "byte":"bytes"); |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 372 | |
| 373 | free(console_c); |
| 374 | |
| 375 | unmap_memory(); |
| 376 | } |
| 377 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 378 | #define CBMEM_MAGIC 0x434f5245 |
| 379 | #define MAX_CBMEM_ENTRIES 16 |
| 380 | |
| 381 | struct cbmem_entry { |
| 382 | uint32_t magic; |
| 383 | uint32_t id; |
| 384 | uint64_t base; |
| 385 | uint64_t size; |
| 386 | }; |
| 387 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 388 | static void dump_cbmem_toc(void) |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 389 | { |
| 390 | int i; |
| 391 | uint64_t start; |
| 392 | struct cbmem_entry *entries; |
| 393 | |
| 394 | if (cbmem.type != LB_MEM_TABLE) { |
| 395 | fprintf(stderr, "No coreboot table area found!\n"); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | start = unpack_lb64(cbmem.start); |
| 400 | |
| 401 | entries = (struct cbmem_entry *)map_memory(start); |
| 402 | |
| 403 | printf("CBMEM table of contents:\n"); |
| 404 | printf(" ID START LENGTH\n"); |
| 405 | for (i=0; i<MAX_CBMEM_ENTRIES; i++) { |
| 406 | if (entries[i].magic != CBMEM_MAGIC) |
| 407 | break; |
| 408 | |
| 409 | printf("%2d. ", i); |
| 410 | switch (entries[i].id) { |
| 411 | case CBMEM_ID_FREESPACE: printf("FREE SPACE "); break; |
| 412 | case CBMEM_ID_GDT: printf("GDT "); break; |
| 413 | case CBMEM_ID_ACPI: printf("ACPI "); break; |
| 414 | case CBMEM_ID_ACPI_GNVS: printf("ACPI GNVS "); break; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 415 | case CBMEM_ID_CBTABLE: printf("COREBOOT "); break; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 416 | case CBMEM_ID_PIRQ: printf("IRQ TABLE "); break; |
| 417 | case CBMEM_ID_MPTABLE: printf("SMP TABLE "); break; |
| 418 | case CBMEM_ID_RESUME: printf("ACPI RESUME "); break; |
| 419 | case CBMEM_ID_RESUME_SCRATCH: printf("ACPI SCRATCH"); break; |
| 420 | case CBMEM_ID_SMBIOS: printf("SMBIOS "); break; |
| 421 | case CBMEM_ID_TIMESTAMP: printf("TIME STAMP "); break; |
| 422 | case CBMEM_ID_MRCDATA: printf("MRC DATA "); break; |
| 423 | case CBMEM_ID_CONSOLE: printf("CONSOLE "); break; |
| 424 | case CBMEM_ID_ELOG: printf("ELOG "); break; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 425 | case CBMEM_ID_COVERAGE: printf("COVERAGE "); break; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 426 | default: printf("%08x ", |
| 427 | entries[i].id); break; |
| 428 | } |
| 429 | printf(" 0x%08jx 0x%08jx\n", (uintmax_t)entries[i].base, |
| 430 | (uintmax_t)entries[i].size); |
| 431 | } |
| 432 | unmap_memory(); |
| 433 | } |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 434 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 435 | #define COVERAGE_MAGIC 0x584d4153 |
| 436 | struct file { |
| 437 | uint32_t magic; |
| 438 | uint32_t next; |
| 439 | uint32_t filename; |
| 440 | uint32_t data; |
| 441 | int offset; |
| 442 | int len; |
| 443 | }; |
| 444 | |
| 445 | static int mkpath(char *path, mode_t mode) |
| 446 | { |
| 447 | assert (path && *path); |
| 448 | char *p; |
| 449 | for (p = strchr(path+1, '/'); p; p = strchr(p + 1, '/')) { |
| 450 | *p = '\0'; |
| 451 | if (mkdir(path, mode) == -1) { |
| 452 | if (errno != EEXIST) { |
| 453 | *p = '/'; |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | *p = '/'; |
| 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static void dump_coverage(void) |
| 463 | { |
| 464 | int i, found = 0; |
| 465 | uint64_t start; |
| 466 | struct cbmem_entry *entries; |
| 467 | void *coverage; |
| 468 | unsigned long phys_offset; |
| 469 | #define phys_to_virt(x) ((void *)(unsigned long)(x) + phys_offset) |
| 470 | |
| 471 | if (cbmem.type != LB_MEM_TABLE) { |
| 472 | fprintf(stderr, "No coreboot table area found!\n"); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | start = unpack_lb64(cbmem.start); |
| 477 | |
| 478 | entries = (struct cbmem_entry *)map_memory(start); |
| 479 | |
| 480 | for (i=0; i<MAX_CBMEM_ENTRIES; i++) { |
| 481 | if (entries[i].magic != CBMEM_MAGIC) |
| 482 | break; |
| 483 | if (entries[i].id == CBMEM_ID_COVERAGE) { |
| 484 | found = 1; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (!found) { |
| 490 | unmap_memory(); |
| 491 | fprintf(stderr, "No coverage information found in" |
| 492 | " CBMEM area.\n"); |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | start = entries[i].base; |
| 497 | unmap_memory(); |
| 498 | /* Map coverage area */ |
| 499 | coverage = map_memory(start); |
| 500 | phys_offset = (unsigned long)coverage - (unsigned long)start; |
| 501 | |
| 502 | printf("Dumping coverage data...\n"); |
| 503 | |
| 504 | struct file *file = (struct file *)coverage; |
| 505 | while (file && file->magic == COVERAGE_MAGIC) { |
| 506 | FILE *f; |
| 507 | char *filename; |
| 508 | |
| 509 | debug(" -> %s\n", (char *)phys_to_virt(file->filename)); |
| 510 | filename = strdup((char *)phys_to_virt(file->filename)); |
| 511 | if (mkpath(filename, 0755) == -1) { |
| 512 | perror("Directory for coverage data could " |
| 513 | "not be created"); |
| 514 | exit(1); |
| 515 | } |
| 516 | f = fopen(filename, "wb"); |
| 517 | if (!f) { |
| 518 | printf("Could not open %s: %s\n", |
| 519 | filename, strerror(errno)); |
| 520 | exit(1); |
| 521 | } |
| 522 | if (fwrite((void *)phys_to_virt(file->data), |
| 523 | file->len, 1, f) != 1) { |
| 524 | printf("Could not write to %s: %s\n", |
| 525 | filename, strerror(errno)); |
| 526 | exit(1); |
| 527 | } |
| 528 | fclose(f); |
| 529 | free(filename); |
| 530 | |
| 531 | if (file->next) |
| 532 | file = (struct file *)phys_to_virt(file->next); |
| 533 | else |
| 534 | file = NULL; |
| 535 | } |
| 536 | unmap_memory(); |
| 537 | } |
| 538 | |
| 539 | static void print_version(void) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 540 | { |
| 541 | printf("cbmem v%s -- ", CBMEM_VERSION); |
| 542 | printf("Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.\n\n"); |
| 543 | printf( |
| 544 | "This program is free software: you can redistribute it and/or modify\n" |
| 545 | "it under the terms of the GNU General Public License as published by\n" |
| 546 | "the Free Software Foundation, version 2 of the License.\n\n" |
| 547 | "This program is distributed in the hope that it will be useful,\n" |
| 548 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 549 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
| 550 | "GNU General Public License for more details.\n\n" |
| 551 | "You should have received a copy of the GNU General Public License\n" |
| 552 | "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n"); |
| 553 | } |
| 554 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 555 | static void print_usage(const char *name) |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 556 | { |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 557 | printf("usage: %s [-cCltVvh?]\n", name); |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 558 | printf("\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 559 | " -c | --console: print cbmem console\n" |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 560 | " -C | --coverage: dump coverage information\n" |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 561 | " -l | --list: print cbmem table of contents\n" |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 562 | " -t | --timestamps: print timestamp information\n" |
| 563 | " -V | --verbose: verbose (debugging) output\n" |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 564 | " -v | --version: print the version\n" |
| 565 | " -h | --help: print this help\n" |
| 566 | "\n"); |
| 567 | exit(1); |
| 568 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 569 | |
| 570 | int main(int argc, char** argv) |
| 571 | { |
| 572 | int j; |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 573 | static const int possible_base_addresses[] = { 0, 0xf0000 }; |
| 574 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 575 | int print_defaults = 1; |
| 576 | int print_console = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 577 | int print_coverage = 0; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 578 | int print_list = 0; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 579 | int print_timestamps = 0; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 580 | |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 581 | int opt, option_index = 0; |
| 582 | static struct option long_options[] = { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 583 | {"console", 0, 0, 'c'}, |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 584 | {"coverage", 0, 0, 'C'}, |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 585 | {"list", 0, 0, 'l'}, |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 586 | {"timestamps", 0, 0, 't'}, |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 587 | {"verbose", 0, 0, 'V'}, |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 588 | {"version", 0, 0, 'v'}, |
| 589 | {"help", 0, 0, 'h'}, |
| 590 | {0, 0, 0, 0} |
| 591 | }; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 592 | while ((opt = getopt_long(argc, argv, "cCltVvh?", |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 593 | long_options, &option_index)) != EOF) { |
| 594 | switch (opt) { |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 595 | case 'c': |
| 596 | print_console = 1; |
| 597 | print_defaults = 0; |
| 598 | break; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 599 | case 'C': |
| 600 | print_coverage = 1; |
| 601 | print_defaults = 0; |
| 602 | break; |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 603 | case 'l': |
| 604 | print_list = 1; |
| 605 | print_defaults = 0; |
| 606 | break; |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 607 | case 't': |
| 608 | print_timestamps = 1; |
| 609 | print_defaults = 0; |
| 610 | break; |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 611 | case 'V': |
| 612 | verbose = 1; |
| 613 | break; |
Stefan Reinauer | 1e0e556 | 2013-01-02 15:43:56 -0800 | [diff] [blame] | 614 | case 'v': |
| 615 | print_version(); |
| 616 | exit(0); |
| 617 | break; |
| 618 | case 'h': |
| 619 | case '?': |
| 620 | default: |
| 621 | print_usage(argv[0]); |
| 622 | exit(0); |
| 623 | break; |
| 624 | } |
| 625 | } |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 626 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 627 | fd = open("/dev/mem", O_RDONLY, 0); |
| 628 | if (fd < 0) { |
| 629 | fprintf(stderr, "Failed to gain memory access: %s\n", |
| 630 | strerror(errno)); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 631 | return 1; |
| 632 | } |
| 633 | |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 634 | /* Find and parse coreboot table */ |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 635 | for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) { |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 636 | if (parse_cbtable(possible_base_addresses[j])) |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 637 | break; |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 640 | if (print_console) |
| 641 | dump_console(); |
| 642 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 643 | if (print_coverage) |
| 644 | dump_coverage(); |
| 645 | |
Stefan Reinauer | c019907 | 2013-01-07 16:26:10 -0800 | [diff] [blame] | 646 | if (print_list) |
| 647 | dump_cbmem_toc(); |
| 648 | |
Stefan Reinauer | 19f8756 | 2013-01-07 13:37:12 -0800 | [diff] [blame] | 649 | if (print_defaults || print_timestamps) |
Stefan Reinauer | 05cbce6 | 2013-01-03 14:30:33 -0800 | [diff] [blame] | 650 | dump_timestamps(); |
| 651 | |
| 652 | close(fd); |
Vadim Bendebury | 6d18fd0 | 2012-09-27 19:24:07 -0700 | [diff] [blame] | 653 | return 0; |
| 654 | } |