Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 1 | /* |
Bill Richardson | 20807b6 | 2013-04-09 10:15:26 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <inttypes.h> |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <unistd.h> |
| 17 | |
Che-Liang Chiou | 305e9e5 | 2011-02-17 17:56:16 +0800 | [diff] [blame] | 18 | #include "fmap.h" |
Bill Richardson | 20807b6 | 2013-04-09 10:15:26 -0700 | [diff] [blame] | 19 | #include "futility.h" |
Che-Liang Chiou | 305e9e5 | 2011-02-17 17:56:16 +0800 | [diff] [blame] | 20 | |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 21 | enum { FMT_NORMAL, FMT_PRETTY, FMT_FLASHROM, FMT_HUMAN }; |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 22 | |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 23 | /* global variables */ |
| 24 | static int opt_extract = 0; |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 25 | static int opt_format = FMT_NORMAL; |
Bill Richardson | a4090b5 | 2012-12-11 10:04:28 -0800 | [diff] [blame] | 26 | static int opt_overlap = 0; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 27 | static char *progname; |
| 28 | static void *base_of_rom; |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 29 | static int opt_gaps = 0; |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /* Return 0 if successful */ |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 33 | static int dump_fmap(const void *ptr, int argc, char *argv[]) |
| 34 | { |
Bill Richardson | f472919 | 2012-05-02 23:30:16 -0700 | [diff] [blame] | 35 | int i, retval = 0; |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 36 | char buf[80]; // DWR: magic number |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 37 | const FmapHeader *fmh = (const FmapHeader*)ptr; |
| 38 | const FmapAreaHeader *ah = (const FmapAreaHeader*)(ptr + sizeof(FmapHeader)); |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 39 | |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 40 | if (FMT_NORMAL == opt_format) { |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 41 | snprintf(buf, FMAP_SIGNATURE_SIZE+1, "%s", fmh->fmap_signature); |
| 42 | printf("fmap_signature %s\n", buf); |
| 43 | printf("fmap_version: %d.%d\n", |
| 44 | fmh->fmap_ver_major, fmh->fmap_ver_minor); |
| 45 | printf("fmap_base: 0x%" PRIx64 "\n", fmh->fmap_base); |
| 46 | printf("fmap_size: 0x%08x (%d)\n", fmh->fmap_size, fmh->fmap_size); |
| 47 | snprintf(buf, FMAP_NAMELEN+1, "%s", fmh->fmap_name); |
| 48 | printf("fmap_name: %s\n", buf); |
| 49 | printf("fmap_nareas: %d\n", fmh->fmap_nareas); |
| 50 | } |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 51 | |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 52 | for (i = 0; i < fmh->fmap_nareas; i++, ah++) { |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 53 | snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name); |
Bill Richardson | e7e8ecd | 2012-03-01 11:05:29 -0800 | [diff] [blame] | 54 | |
| 55 | if (argc) { |
| 56 | int j, found=0; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 57 | for (j = 0; j < argc; j++) |
Bill Richardson | e7e8ecd | 2012-03-01 11:05:29 -0800 | [diff] [blame] | 58 | if (!strcmp(argv[j], buf)) { |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 59 | found = 1; |
| 60 | break; |
| 61 | } |
Bill Richardson | e7e8ecd | 2012-03-01 11:05:29 -0800 | [diff] [blame] | 62 | if (!found) { |
| 63 | continue; |
| 64 | } |
| 65 | } |
| 66 | |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 67 | switch (opt_format) { |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 68 | case FMT_PRETTY: |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 69 | printf("%s %d %d\n", buf, ah->area_offset, ah->area_size); |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 70 | break; |
| 71 | case FMT_FLASHROM: |
| 72 | if (ah->area_size) |
Louis Yung-Chieh Lo | 9462e3e | 2011-08-24 12:53:43 +0800 | [diff] [blame] | 73 | printf("0x%08x:0x%08x %s\n", ah->area_offset, |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 74 | ah->area_offset + ah->area_size - 1, buf); |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 75 | break; |
| 76 | default: |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 77 | printf("area: %d\n", i+1); |
| 78 | printf("area_offset: 0x%08x\n", ah->area_offset); |
| 79 | printf("area_size: 0x%08x (%d)\n", ah->area_size, ah->area_size); |
| 80 | printf("area_name: %s\n", buf); |
| 81 | } |
Simon Glass | abd06d1 | 2013-07-15 17:03:30 -0600 | [diff] [blame] | 82 | |
| 83 | if (opt_extract) { |
| 84 | char *s; |
| 85 | for (s = buf; *s; s++) |
| 86 | if (*s == ' ') |
| 87 | *s = '_'; |
| 88 | FILE *fp = fopen(buf,"wb"); |
| 89 | if (!fp) { |
| 90 | fprintf(stderr, "%s: can't open %s: %s\n", |
| 91 | progname, buf, strerror(errno)); |
| 92 | retval = 1; |
| 93 | } else { |
| 94 | if (ah->area_size && |
| 95 | 1 != fwrite(base_of_rom + ah->area_offset, ah->area_size, 1, fp)) { |
| 96 | fprintf(stderr, "%s: can't write %s: %s\n", |
| 97 | progname, buf, strerror(errno)); |
| 98 | retval = 1; |
| 99 | } else { |
| 100 | if (FMT_NORMAL == opt_format) |
| 101 | printf("saved as \"%s\"\n", buf); |
| 102 | } |
| 103 | fclose(fp); |
| 104 | } |
| 105 | } |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | return retval; |
| 109 | } |
| 110 | |
| 111 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 112 | /****************************************************************************/ |
| 113 | /* Stuff for human-readable form */ |
| 114 | |
| 115 | typedef struct dup_s { |
| 116 | char *name; |
| 117 | struct dup_s *next; |
| 118 | } dupe_t; |
| 119 | |
| 120 | typedef struct node_s { |
| 121 | char *name; |
| 122 | uint32_t start; |
| 123 | uint32_t size; |
| 124 | uint32_t end; |
| 125 | struct node_s *parent; |
| 126 | int num_children; |
| 127 | struct node_s **child; |
| 128 | dupe_t *alias; |
| 129 | } node_t; |
| 130 | |
| 131 | static node_t *all_nodes; |
| 132 | |
| 133 | static void sort_nodes(int num, node_t *ary[]) |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 134 | { |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 135 | int i, j; |
| 136 | node_t *tmp; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 137 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 138 | /* bubble-sort is quick enough with only a few entries */ |
| 139 | for (i = 0; i < num; i++) { |
| 140 | for (j = i + 1; j < num; j++) { |
| 141 | if (ary[j]->start > ary[i]->start) { |
| 142 | tmp = ary[i]; |
| 143 | ary[i] = ary[j]; |
| 144 | ary[j] = tmp; |
| 145 | } |
| 146 | } |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 147 | } |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 151 | static void line(int indent, char *name, |
| 152 | uint32_t start, uint32_t end, uint32_t size, char *append) |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 153 | { |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 154 | int i; |
| 155 | for (i = 0; i < indent; i++) |
| 156 | printf(" "); |
| 157 | printf("%-25s %08x %08x %08x%s\n", name, start, end, size, |
| 158 | append ? append : ""); |
| 159 | } |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 160 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 161 | static int gapcount; |
| 162 | static void empty(int indent, uint32_t start, uint32_t end, char *name) |
| 163 | { |
| 164 | char buf[80]; |
| 165 | if (opt_gaps) { |
| 166 | sprintf(buf, " // gap in %s", name); |
| 167 | line(indent + 1, "", start, end, end - start, buf); |
| 168 | } |
| 169 | gapcount++; |
| 170 | } |
| 171 | |
| 172 | static void show(node_t *p, int indent, int show_first) |
| 173 | { |
| 174 | int i; |
| 175 | dupe_t *alias; |
| 176 | if (show_first) { |
| 177 | line(indent, p->name, p->start, p->end, p->size, 0); |
| 178 | for (alias = p->alias; alias; alias = alias->next) |
| 179 | line(indent, alias->name, p->start, p->end, p->size, " // DUPLICATE"); |
| 180 | } |
| 181 | sort_nodes(p->num_children, p->child); |
| 182 | for (i = 0; i < p->num_children; i++) { |
| 183 | if (i == 0 && p->end != p->child[i]->end) |
| 184 | empty(indent, p->child[i]->end, p->end, p->name); |
| 185 | show(p->child[i], indent + show_first, 1); |
| 186 | if (i < p->num_children - 1 && p->child[i]->start != p->child[i+1]->end) |
| 187 | empty(indent, p->child[i+1]->end, p->child[i]->start, p->name); |
| 188 | if (i == p->num_children - 1 && p->child[i]->start != p->start) |
| 189 | empty(indent, p->start, p->child[i]->start, p->name); |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 193 | static int overlaps(int i, int j) |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 194 | { |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 195 | node_t *a = all_nodes + i; |
| 196 | node_t *b = all_nodes + j; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 197 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 198 | return ((a->start < b->start) && (b->start < a->end) && |
| 199 | (b->start < a->end) && (a->end < b->end)); |
| 200 | } |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 201 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 202 | static int encloses(int i, int j) |
| 203 | { |
| 204 | node_t *a = all_nodes + i; |
| 205 | node_t *b = all_nodes + j; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 206 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 207 | return ((a->start <= b->start) && |
| 208 | (a->end >= b->end)); |
| 209 | } |
| 210 | |
| 211 | static int duplicates(int i, int j) |
| 212 | { |
| 213 | node_t *a = all_nodes + i; |
| 214 | node_t *b = all_nodes + j; |
| 215 | |
| 216 | return ((a->start == b->start) && |
| 217 | (a->end == b->end)); |
| 218 | } |
| 219 | |
| 220 | static void add_dupe(int i, int j, int numnodes) |
| 221 | { |
| 222 | int k; |
| 223 | dupe_t *alias; |
| 224 | |
| 225 | alias = (dupe_t *)malloc(sizeof(dupe_t)); |
| 226 | alias->name = all_nodes[j].name; |
| 227 | alias->next = all_nodes[i].alias; |
| 228 | all_nodes[i].alias = alias; |
| 229 | for (k = j; k < numnodes; k++ ) |
| 230 | all_nodes[k] = all_nodes[k + 1]; |
| 231 | } |
| 232 | |
| 233 | static void add_child(node_t *p, int n) |
| 234 | { |
| 235 | int i; |
| 236 | if (p->num_children && !p->child) { |
| 237 | p->child = (struct node_s **)calloc(p->num_children, sizeof(node_t *)); |
| 238 | if (!p->child) { |
| 239 | perror("calloc failed"); |
| 240 | exit(1); |
| 241 | } |
| 242 | } |
| 243 | for (i = 0; i < p->num_children; i++) |
| 244 | if (!p->child[i]) { |
| 245 | p->child[i] = all_nodes + n; |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | static int human_fmap(void *p) |
| 251 | { |
| 252 | FmapHeader *fmh; |
| 253 | FmapAreaHeader *ah; |
| 254 | int i, j, errorcnt=0; |
| 255 | int numnodes; |
| 256 | |
| 257 | fmh = (FmapHeader *)p; |
| 258 | ah = (FmapAreaHeader *)(fmh + 1); |
| 259 | |
| 260 | /* The challenge here is to generate a directed graph from the |
| 261 | * arbitrarily-ordered FMAP entries, and then to prune it until it's as |
| 262 | * simple (and deep) as possible. Overlapping regions are not allowed. |
| 263 | * Duplicate regions are okay, but may require special handling. */ |
| 264 | |
| 265 | /* Convert the FMAP info into our format. */ |
| 266 | numnodes = fmh->fmap_nareas; |
| 267 | |
| 268 | /* plus one for the all-enclosing "root" */ |
| 269 | all_nodes = (node_t *)calloc(numnodes+1, sizeof(node_t)); |
| 270 | if (!all_nodes) { |
| 271 | perror("calloc failed"); |
| 272 | exit(1); |
| 273 | } |
| 274 | for (i = 0; i < numnodes; i++) { |
| 275 | char buf[FMAP_NAMELEN+1]; |
| 276 | strncpy(buf, ah[i].area_name, FMAP_NAMELEN); |
| 277 | buf[FMAP_NAMELEN] = '\0'; |
| 278 | if (!(all_nodes[i].name = strdup(buf))) { |
| 279 | perror("strdup failed"); |
| 280 | exit(1); |
| 281 | } |
| 282 | all_nodes[i].start = ah[i].area_offset; |
| 283 | all_nodes[i].size = ah[i].area_size; |
| 284 | all_nodes[i].end = ah[i].area_offset + ah[i].area_size; |
| 285 | } |
| 286 | /* Now add the root node */ |
| 287 | all_nodes[numnodes].name = strdup("-entire flash-"); |
| 288 | all_nodes[numnodes].start = fmh->fmap_base; |
| 289 | all_nodes[numnodes].size = fmh->fmap_size; |
| 290 | all_nodes[numnodes].end = fmh->fmap_base + fmh->fmap_size; |
| 291 | |
| 292 | |
| 293 | /* First, coalesce any duplicates */ |
| 294 | for (i = 0; i < numnodes; i++) { |
| 295 | for (j = i + 1; j < numnodes; j++) { |
| 296 | if (duplicates(i, j)) { |
| 297 | add_dupe(i, j, numnodes); |
| 298 | numnodes--; |
| 299 | } |
| 300 | } |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 303 | /* Each node should have at most one parent, which is the smallest enclosing |
| 304 | * node. Duplicate nodes "enclose" each other, but if there's already a |
| 305 | * relationship in one direction, we won't create another. */ |
| 306 | for (i = 0; i < numnodes; i++) { |
| 307 | /* Find the smallest parent, which might be the root node. */ |
| 308 | int k = numnodes; |
| 309 | for (j = 0; j < numnodes; j++) { /* full O(N^2), not triangular */ |
| 310 | if (i == j) |
| 311 | continue; |
| 312 | if (overlaps(i, j)) { |
| 313 | printf("ERROR: %s and %s overlap\n", |
| 314 | all_nodes[i].name, all_nodes[j].name); |
| 315 | printf(" %s: 0x%x - 0x%x\n", all_nodes[i].name, |
| 316 | all_nodes[i].start, all_nodes[i].end); |
| 317 | printf(" %s: 0x%x - 0x%x\n", all_nodes[j].name, |
| 318 | all_nodes[j].start, all_nodes[j].end); |
Bill Richardson | a4090b5 | 2012-12-11 10:04:28 -0800 | [diff] [blame] | 319 | if (opt_overlap < 2) { |
| 320 | printf("Use more -h args to ignore this error\n"); |
| 321 | errorcnt++; |
| 322 | } |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 323 | continue; |
| 324 | } |
| 325 | if (encloses(j, i) && all_nodes[j].size < all_nodes[k].size) |
| 326 | k = j; |
| 327 | } |
| 328 | all_nodes[i].parent = all_nodes + k; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 329 | } |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 330 | if (errorcnt) |
| 331 | return 1; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 332 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 333 | /* Force those deadbeat parents to recognize their children */ |
| 334 | for (i = 0; i < numnodes; i++) /* how many */ |
| 335 | if (all_nodes[i].parent) |
| 336 | all_nodes[i].parent->num_children++; |
| 337 | for (i = 0; i < numnodes; i++) /* here they are */ |
| 338 | if (all_nodes[i].parent) |
| 339 | add_child(all_nodes[i].parent, i); |
| 340 | |
| 341 | /* Ready to go */ |
| 342 | printf("# name start end size\n"); |
| 343 | show(all_nodes + numnodes, 0, opt_gaps); |
| 344 | |
| 345 | if (gapcount && !opt_gaps) |
| 346 | printf("\nWARNING: unused regions found. Use -H to see them\n"); |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 351 | /* End of human-reable stuff */ |
| 352 | /****************************************************************************/ |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 353 | |
Bill Richardson | 20807b6 | 2013-04-09 10:15:26 -0700 | [diff] [blame] | 354 | static int do_dump_fmap(int argc, char *argv[]) |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 355 | { |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 356 | int c; |
| 357 | int errorcnt = 0; |
| 358 | struct stat sb; |
| 359 | int fd; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 360 | const char *fmap; |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 361 | int retval = 1; |
| 362 | |
| 363 | progname = strrchr(argv[0], '/'); |
| 364 | if (progname) |
| 365 | progname++; |
| 366 | else |
| 367 | progname = argv[0]; |
| 368 | |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 369 | opterr = 0; /* quiet, you */ |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 370 | while ((c = getopt(argc, argv, ":xpfhH")) != -1) { |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 371 | switch (c) { |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 372 | case 'x': |
| 373 | opt_extract = 1; |
| 374 | break; |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 375 | case 'p': |
Bill Richardson | 77c0243 | 2011-07-29 13:05:58 -0700 | [diff] [blame] | 376 | opt_format = FMT_PRETTY; |
| 377 | break; |
| 378 | case 'f': |
| 379 | opt_format = FMT_FLASHROM; |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 380 | break; |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 381 | case 'H': |
| 382 | opt_gaps = 1; |
| 383 | /* fallthrough */ |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 384 | case 'h': |
| 385 | opt_format = FMT_HUMAN; |
Bill Richardson | a4090b5 | 2012-12-11 10:04:28 -0800 | [diff] [blame] | 386 | opt_overlap++; |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 387 | break; |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 388 | case '?': |
| 389 | fprintf(stderr, "%s: unrecognized switch: -%c\n", |
| 390 | progname, optopt); |
| 391 | errorcnt++; |
| 392 | break; |
| 393 | case ':': |
| 394 | fprintf(stderr, "%s: missing argument to -%c\n", |
| 395 | progname, optopt); |
| 396 | errorcnt++; |
| 397 | break; |
| 398 | default: |
| 399 | errorcnt++; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (errorcnt || optind >= argc) { |
| 405 | fprintf(stderr, |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 406 | "\nUsage: %s [-x] [-p|-f|-h] FLASHIMAGE [NAME...]\n\n" |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 407 | "Display (and extract with -x) the FMAP components from a BIOS image.\n" |
| 408 | "The -p option makes the output easier to parse by scripts.\n" |
Bill Richardson | e7e8ecd | 2012-03-01 11:05:29 -0800 | [diff] [blame] | 409 | "The -f option emits the FMAP in the format used by flashrom.\n" |
| 410 | "\n" |
| 411 | "Specify one or more NAMEs to only print sections that exactly match.\n" |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 412 | "\n" |
| 413 | "The -h option shows the whole FMAP in human-readable form.\n" |
Bill Richardson | ae98bf0 | 2012-08-17 16:16:50 -0700 | [diff] [blame] | 414 | " Use -H to also display any gaps.\n" |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 415 | "\n", |
| 416 | progname); |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 417 | return 1; |
| 418 | } |
| 419 | |
| 420 | if (0 != stat(argv[optind], &sb)) { |
| 421 | fprintf(stderr, "%s: can't stat %s: %s\n", |
| 422 | progname, |
| 423 | argv[optind], |
| 424 | strerror(errno)); |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | fd = open(argv[optind], O_RDONLY); |
| 429 | if (fd < 0) { |
| 430 | fprintf(stderr, "%s: can't open %s: %s\n", |
| 431 | progname, |
| 432 | argv[optind], |
| 433 | strerror(errno)); |
| 434 | return 1; |
| 435 | } |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 436 | |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 437 | base_of_rom = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
Che-Liang Chiou | 305e9e5 | 2011-02-17 17:56:16 +0800 | [diff] [blame] | 438 | if (base_of_rom == (char*)-1) { |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 439 | fprintf(stderr, "%s: can't mmap %s: %s\n", |
| 440 | progname, |
| 441 | argv[optind], |
| 442 | strerror(errno)); |
| 443 | close(fd); |
| 444 | return 1; |
| 445 | } |
| 446 | close(fd); /* done with this now */ |
| 447 | |
Che-Liang Chiou | 305e9e5 | 2011-02-17 17:56:16 +0800 | [diff] [blame] | 448 | fmap = FmapFind((char*) base_of_rom, sb.st_size); |
| 449 | if (fmap) { |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 450 | switch (opt_format) { |
| 451 | case FMT_HUMAN: |
| 452 | retval = human_fmap((void *)fmap); |
| 453 | break; |
| 454 | case FMT_NORMAL: |
Bill Richardson | 9a2c3b2 | 2011-06-13 12:45:24 -0700 | [diff] [blame] | 455 | printf("hit at 0x%08x\n", (uint32_t) (fmap - (char*) base_of_rom)); |
Bill Richardson | 9429f88 | 2012-07-26 13:10:59 -0700 | [diff] [blame] | 456 | /* fallthrough */ |
| 457 | default: |
| 458 | retval = dump_fmap(fmap, argc-optind-1, argv+optind+1); |
| 459 | } |
Bill Richardson | 60bcbe3 | 2010-09-09 14:53:56 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (0 != munmap(base_of_rom, sb.st_size)) { |
| 463 | fprintf(stderr, "%s: can't munmap %s: %s\n", |
| 464 | progname, |
| 465 | argv[optind], |
| 466 | strerror(errno)); |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | return retval; |
| 471 | } |
Bill Richardson | 20807b6 | 2013-04-09 10:15:26 -0700 | [diff] [blame] | 472 | |
| 473 | DECLARE_FUTIL_COMMAND(dump_fmap, do_dump_fmap, |
| 474 | "Display FMAP contents from a firmware image"); |