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