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