blob: 4d14d8b835a3d94f36110e8ee666903e4c9c510d [file] [log] [blame]
Bill Richardson60bcbe32010-09-09 14:53:56 -07001/*
Bill Richardson9429f882012-07-26 13:10:59 -07002 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Bill Richardson60bcbe32010-09-09 14:53:56 -07003 * 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 Chiou305e9e52011-02-17 17:56:16 +080018#include "fmap.h"
19
Bill Richardson9429f882012-07-26 13:10:59 -070020enum { FMT_NORMAL, FMT_PRETTY, FMT_FLASHROM, FMT_HUMAN };
Bill Richardson77c02432011-07-29 13:05:58 -070021
Bill Richardson60bcbe32010-09-09 14:53:56 -070022/* global variables */
23static int opt_extract = 0;
Bill Richardson77c02432011-07-29 13:05:58 -070024static int opt_format = FMT_NORMAL;
Bill Richardson9429f882012-07-26 13:10:59 -070025static char *progname;
26static void *base_of_rom;
Bill Richardsonae98bf02012-08-17 16:16:50 -070027static int opt_gaps = 0;
Bill Richardson60bcbe32010-09-09 14:53:56 -070028
29
30/* Return 0 if successful */
Bill Richardson9429f882012-07-26 13:10:59 -070031static int dump_fmap(const void *ptr, int argc, char *argv[])
32{
Bill Richardsonf4729192012-05-02 23:30:16 -070033 int i, retval = 0;
Bill Richardson60bcbe32010-09-09 14:53:56 -070034 char buf[80]; // DWR: magic number
Bill Richardson9429f882012-07-26 13:10:59 -070035 const FmapHeader *fmh = (const FmapHeader*)ptr;
36 const FmapAreaHeader *ah = (const FmapAreaHeader*)(ptr + sizeof(FmapHeader));
Bill Richardson60bcbe32010-09-09 14:53:56 -070037
Bill Richardson77c02432011-07-29 13:05:58 -070038 if (FMT_NORMAL == opt_format) {
Bill Richardson9a2c3b22011-06-13 12:45:24 -070039 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 Richardson60bcbe32010-09-09 14:53:56 -070049
Bill Richardson9429f882012-07-26 13:10:59 -070050 for (i = 0; i < fmh->fmap_nareas; i++, ah++) {
Bill Richardson77c02432011-07-29 13:05:58 -070051 snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name);
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080052
53 if (argc) {
54 int j, found=0;
Bill Richardson9429f882012-07-26 13:10:59 -070055 for (j = 0; j < argc; j++)
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080056 if (!strcmp(argv[j], buf)) {
Bill Richardson9429f882012-07-26 13:10:59 -070057 found = 1;
58 break;
59 }
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080060 if (!found) {
61 continue;
62 }
63 }
64
Bill Richardson9429f882012-07-26 13:10:59 -070065 switch (opt_format) {
Bill Richardson77c02432011-07-29 13:05:58 -070066 case FMT_PRETTY:
Bill Richardson9a2c3b22011-06-13 12:45:24 -070067 printf("%s %d %d\n", buf, ah->area_offset, ah->area_size);
Bill Richardson77c02432011-07-29 13:05:58 -070068 break;
69 case FMT_FLASHROM:
70 if (ah->area_size)
Louis Yung-Chieh Lo9462e3e2011-08-24 12:53:43 +080071 printf("0x%08x:0x%08x %s\n", ah->area_offset,
Bill Richardson9429f882012-07-26 13:10:59 -070072 ah->area_offset + ah->area_size - 1, buf);
Bill Richardson77c02432011-07-29 13:05:58 -070073 break;
74 default:
Bill Richardson9a2c3b22011-06-13 12:45:24 -070075 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 Richardson60bcbe32010-09-09 14:53:56 -070080
81 if (opt_extract) {
Bill Richardson9429f882012-07-26 13:10:59 -070082 char *s;
83 for (s = buf; *s; s++)
Bill Richardson60bcbe32010-09-09 14:53:56 -070084 if (*s == ' ')
Bill Richardson9a2c3b22011-06-13 12:45:24 -070085 *s = '_';
Bill Richardson9429f882012-07-26 13:10:59 -070086 FILE *fp = fopen(buf,"wb");
Bill Richardson60bcbe32010-09-09 14:53:56 -070087 if (!fp) {
88 fprintf(stderr, "%s: can't open %s: %s\n",
89 progname, buf, strerror(errno));
90 retval = 1;
91 } else {
Bill Richardsonccdaa472011-03-03 18:08:18 -080092 if (ah->area_size &&
93 1 != fwrite(base_of_rom + ah->area_offset, ah->area_size, 1, fp)) {
Bill Richardson60bcbe32010-09-09 14:53:56 -070094 fprintf(stderr, "%s: can't write %s: %s\n",
95 progname, buf, strerror(errno));
96 retval = 1;
97 } else {
Bill Richardson77c02432011-07-29 13:05:58 -070098 if (FMT_NORMAL == opt_format)
Bill Richardson9a2c3b22011-06-13 12:45:24 -070099 printf("saved as \"%s\"\n", buf);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700100 }
101 fclose(fp);
102 }
103 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700104 }
105
106 return retval;
107}
108
109
Bill Richardsonae98bf02012-08-17 16:16:50 -0700110/****************************************************************************/
111/* Stuff for human-readable form */
112
113typedef struct dup_s {
114 char *name;
115 struct dup_s *next;
116} dupe_t;
117
118typedef 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
129static node_t *all_nodes;
130
131static void sort_nodes(int num, node_t *ary[])
Bill Richardson9429f882012-07-26 13:10:59 -0700132{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700133 int i, j;
134 node_t *tmp;
Bill Richardson9429f882012-07-26 13:10:59 -0700135
Bill Richardsonae98bf02012-08-17 16:16:50 -0700136 /* 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 Richardson9429f882012-07-26 13:10:59 -0700145 }
Bill Richardson9429f882012-07-26 13:10:59 -0700146}
147
148
Bill Richardsonae98bf02012-08-17 16:16:50 -0700149static void line(int indent, char *name,
150 uint32_t start, uint32_t end, uint32_t size, char *append)
Bill Richardson9429f882012-07-26 13:10:59 -0700151{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700152 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 Richardson9429f882012-07-26 13:10:59 -0700158
Bill Richardsonae98bf02012-08-17 16:16:50 -0700159static int gapcount;
160static 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
170static 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 Richardson9429f882012-07-26 13:10:59 -0700188 }
189}
190
Bill Richardsonae98bf02012-08-17 16:16:50 -0700191static int overlaps(int i, int j)
Bill Richardson9429f882012-07-26 13:10:59 -0700192{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700193 node_t *a = all_nodes + i;
194 node_t *b = all_nodes + j;
Bill Richardson9429f882012-07-26 13:10:59 -0700195
Bill Richardsonae98bf02012-08-17 16:16:50 -0700196 return ((a->start < b->start) && (b->start < a->end) &&
197 (b->start < a->end) && (a->end < b->end));
198}
Bill Richardson9429f882012-07-26 13:10:59 -0700199
Bill Richardsonae98bf02012-08-17 16:16:50 -0700200static int encloses(int i, int j)
201{
202 node_t *a = all_nodes + i;
203 node_t *b = all_nodes + j;
Bill Richardson9429f882012-07-26 13:10:59 -0700204
Bill Richardsonae98bf02012-08-17 16:16:50 -0700205 return ((a->start <= b->start) &&
206 (a->end >= b->end));
207}
208
209static 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
218static 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
231static 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
248static 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 Richardson9429f882012-07-26 13:10:59 -0700299 }
300
Bill Richardsonae98bf02012-08-17 16:16:50 -0700301 /* 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 Richardson9429f882012-07-26 13:10:59 -0700324 }
Bill Richardsonae98bf02012-08-17 16:16:50 -0700325 if (errorcnt)
326 return 1;
Bill Richardson9429f882012-07-26 13:10:59 -0700327
Bill Richardsonae98bf02012-08-17 16:16:50 -0700328 /* 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 Richardson9429f882012-07-26 13:10:59 -0700342
343 return 0;
344}
345
Bill Richardsonae98bf02012-08-17 16:16:50 -0700346/* End of human-reable stuff */
347/****************************************************************************/
Bill Richardson9429f882012-07-26 13:10:59 -0700348
349int main(int argc, char *argv[])
350{
Bill Richardson60bcbe32010-09-09 14:53:56 -0700351 int c;
352 int errorcnt = 0;
353 struct stat sb;
354 int fd;
Bill Richardson9429f882012-07-26 13:10:59 -0700355 const char *fmap;
Bill Richardson60bcbe32010-09-09 14:53:56 -0700356 int retval = 1;
357
358 progname = strrchr(argv[0], '/');
359 if (progname)
360 progname++;
361 else
362 progname = argv[0];
363
Bill Richardson9429f882012-07-26 13:10:59 -0700364 opterr = 0; /* quiet, you */
Bill Richardsonae98bf02012-08-17 16:16:50 -0700365 while ((c = getopt(argc, argv, ":xpfhH")) != -1) {
Bill Richardson9429f882012-07-26 13:10:59 -0700366 switch (c) {
Bill Richardson60bcbe32010-09-09 14:53:56 -0700367 case 'x':
368 opt_extract = 1;
369 break;
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700370 case 'p':
Bill Richardson77c02432011-07-29 13:05:58 -0700371 opt_format = FMT_PRETTY;
372 break;
373 case 'f':
374 opt_format = FMT_FLASHROM;
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700375 break;
Bill Richardsonae98bf02012-08-17 16:16:50 -0700376 case 'H':
377 opt_gaps = 1;
378 /* fallthrough */
Bill Richardson9429f882012-07-26 13:10:59 -0700379 case 'h':
380 opt_format = FMT_HUMAN;
381 break;
Bill Richardson60bcbe32010-09-09 14:53:56 -0700382 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 Richardson9429f882012-07-26 13:10:59 -0700400 "\nUsage: %s [-x] [-p|-f|-h] FLASHIMAGE [NAME...]\n\n"
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700401 "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 Richardsone7e8ecd2012-03-01 11:05:29 -0800403 "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 Richardson9429f882012-07-26 13:10:59 -0700406 "\n"
407 "The -h option shows the whole FMAP in human-readable form.\n"
Bill Richardsonae98bf02012-08-17 16:16:50 -0700408 " Use -H to also display any gaps.\n"
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700409 "\n",
410 progname);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700411 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 Richardson77c02432011-07-29 13:05:58 -0700430 if (FMT_NORMAL == opt_format)
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700431 printf("opened %s\n", argv[optind]);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700432
Bill Richardson9429f882012-07-26 13:10:59 -0700433 base_of_rom = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Che-Liang Chiou305e9e52011-02-17 17:56:16 +0800434 if (base_of_rom == (char*)-1) {
Bill Richardson60bcbe32010-09-09 14:53:56 -0700435 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 Chiou305e9e52011-02-17 17:56:16 +0800444 fmap = FmapFind((char*) base_of_rom, sb.st_size);
445 if (fmap) {
Bill Richardson9429f882012-07-26 13:10:59 -0700446 switch (opt_format) {
447 case FMT_HUMAN:
448 retval = human_fmap((void *)fmap);
449 break;
450 case FMT_NORMAL:
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700451 printf("hit at 0x%08x\n", (uint32_t) (fmap - (char*) base_of_rom));
Bill Richardson9429f882012-07-26 13:10:59 -0700452 /* fallthrough */
453 default:
454 retval = dump_fmap(fmap, argc-optind-1, argv+optind+1);
455 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700456 }
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}