blob: fbd96209a964ebfd14c527ae46ad0526b5d3d123 [file] [log] [blame]
Bill Richardson60bcbe32010-09-09 14:53:56 -07001/*
Bill Richardson20807b62013-04-09 10:15:26 -07002 * Copyright (c) 2013 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"
Bill Richardson20807b62013-04-09 10:15:26 -070019#include "futility.h"
Che-Liang Chiou305e9e52011-02-17 17:56:16 +080020
Bill Richardson9429f882012-07-26 13:10:59 -070021enum { FMT_NORMAL, FMT_PRETTY, FMT_FLASHROM, FMT_HUMAN };
Bill Richardson77c02432011-07-29 13:05:58 -070022
Bill Richardson60bcbe32010-09-09 14:53:56 -070023/* global variables */
24static int opt_extract = 0;
Bill Richardson77c02432011-07-29 13:05:58 -070025static int opt_format = FMT_NORMAL;
Bill Richardsona4090b52012-12-11 10:04:28 -080026static int opt_overlap = 0;
Bill Richardson9429f882012-07-26 13:10:59 -070027static char *progname;
28static void *base_of_rom;
Bill Richardsonae98bf02012-08-17 16:16:50 -070029static int opt_gaps = 0;
Bill Richardson60bcbe32010-09-09 14:53:56 -070030
31
32/* Return 0 if successful */
Bill Richardson9429f882012-07-26 13:10:59 -070033static int dump_fmap(const void *ptr, int argc, char *argv[])
34{
Bill Richardsonf4729192012-05-02 23:30:16 -070035 int i, retval = 0;
Bill Richardson60bcbe32010-09-09 14:53:56 -070036 char buf[80]; // DWR: magic number
Bill Richardson9429f882012-07-26 13:10:59 -070037 const FmapHeader *fmh = (const FmapHeader*)ptr;
38 const FmapAreaHeader *ah = (const FmapAreaHeader*)(ptr + sizeof(FmapHeader));
Bill Richardson60bcbe32010-09-09 14:53:56 -070039
Bill Richardson77c02432011-07-29 13:05:58 -070040 if (FMT_NORMAL == opt_format) {
Bill Richardson9a2c3b22011-06-13 12:45:24 -070041 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 Richardson60bcbe32010-09-09 14:53:56 -070051
Bill Richardson9429f882012-07-26 13:10:59 -070052 for (i = 0; i < fmh->fmap_nareas; i++, ah++) {
Bill Richardson77c02432011-07-29 13:05:58 -070053 snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name);
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080054
55 if (argc) {
56 int j, found=0;
Bill Richardson9429f882012-07-26 13:10:59 -070057 for (j = 0; j < argc; j++)
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080058 if (!strcmp(argv[j], buf)) {
Bill Richardson9429f882012-07-26 13:10:59 -070059 found = 1;
60 break;
61 }
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080062 if (!found) {
63 continue;
64 }
65 }
66
Bill Richardson9429f882012-07-26 13:10:59 -070067 switch (opt_format) {
Bill Richardson77c02432011-07-29 13:05:58 -070068 case FMT_PRETTY:
Bill Richardson9a2c3b22011-06-13 12:45:24 -070069 printf("%s %d %d\n", buf, ah->area_offset, ah->area_size);
Bill Richardson77c02432011-07-29 13:05:58 -070070 break;
71 case FMT_FLASHROM:
72 if (ah->area_size)
Louis Yung-Chieh Lo9462e3e2011-08-24 12:53:43 +080073 printf("0x%08x:0x%08x %s\n", ah->area_offset,
Bill Richardson9429f882012-07-26 13:10:59 -070074 ah->area_offset + ah->area_size - 1, buf);
Bill Richardson77c02432011-07-29 13:05:58 -070075 break;
76 default:
Bill Richardson9a2c3b22011-06-13 12:45:24 -070077 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 Glassabd06d12013-07-15 17:03:30 -060082
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 Richardson60bcbe32010-09-09 14:53:56 -0700106 }
107
108 return retval;
109}
110
111
Bill Richardsonae98bf02012-08-17 16:16:50 -0700112/****************************************************************************/
113/* Stuff for human-readable form */
114
115typedef struct dup_s {
116 char *name;
117 struct dup_s *next;
118} dupe_t;
119
120typedef 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
131static node_t *all_nodes;
132
133static void sort_nodes(int num, node_t *ary[])
Bill Richardson9429f882012-07-26 13:10:59 -0700134{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700135 int i, j;
136 node_t *tmp;
Bill Richardson9429f882012-07-26 13:10:59 -0700137
Bill Richardsonae98bf02012-08-17 16:16:50 -0700138 /* 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 Richardson9429f882012-07-26 13:10:59 -0700147 }
Bill Richardson9429f882012-07-26 13:10:59 -0700148}
149
150
Bill Richardsonae98bf02012-08-17 16:16:50 -0700151static void line(int indent, char *name,
152 uint32_t start, uint32_t end, uint32_t size, char *append)
Bill Richardson9429f882012-07-26 13:10:59 -0700153{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700154 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 Richardson9429f882012-07-26 13:10:59 -0700160
Bill Richardsonae98bf02012-08-17 16:16:50 -0700161static int gapcount;
162static 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
172static 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 Richardson9429f882012-07-26 13:10:59 -0700190 }
191}
192
Bill Richardsonae98bf02012-08-17 16:16:50 -0700193static int overlaps(int i, int j)
Bill Richardson9429f882012-07-26 13:10:59 -0700194{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700195 node_t *a = all_nodes + i;
196 node_t *b = all_nodes + j;
Bill Richardson9429f882012-07-26 13:10:59 -0700197
Bill Richardsonae98bf02012-08-17 16:16:50 -0700198 return ((a->start < b->start) && (b->start < a->end) &&
199 (b->start < a->end) && (a->end < b->end));
200}
Bill Richardson9429f882012-07-26 13:10:59 -0700201
Bill Richardsonae98bf02012-08-17 16:16:50 -0700202static int encloses(int i, int j)
203{
204 node_t *a = all_nodes + i;
205 node_t *b = all_nodes + j;
Bill Richardson9429f882012-07-26 13:10:59 -0700206
Bill Richardsonae98bf02012-08-17 16:16:50 -0700207 return ((a->start <= b->start) &&
208 (a->end >= b->end));
209}
210
211static 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
220static 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
233static 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
250static 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 Richardson9429f882012-07-26 13:10:59 -0700301 }
302
Bill Richardsonae98bf02012-08-17 16:16:50 -0700303 /* 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 Richardsona4090b52012-12-11 10:04:28 -0800319 if (opt_overlap < 2) {
320 printf("Use more -h args to ignore this error\n");
321 errorcnt++;
322 }
Bill Richardsonae98bf02012-08-17 16:16:50 -0700323 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 Richardson9429f882012-07-26 13:10:59 -0700329 }
Bill Richardsonae98bf02012-08-17 16:16:50 -0700330 if (errorcnt)
331 return 1;
Bill Richardson9429f882012-07-26 13:10:59 -0700332
Bill Richardsonae98bf02012-08-17 16:16:50 -0700333 /* 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 Richardson9429f882012-07-26 13:10:59 -0700347
348 return 0;
349}
350
Bill Richardsonae98bf02012-08-17 16:16:50 -0700351/* End of human-reable stuff */
352/****************************************************************************/
Bill Richardson9429f882012-07-26 13:10:59 -0700353
Bill Richardson20807b62013-04-09 10:15:26 -0700354static int do_dump_fmap(int argc, char *argv[])
Bill Richardson9429f882012-07-26 13:10:59 -0700355{
Bill Richardson60bcbe32010-09-09 14:53:56 -0700356 int c;
357 int errorcnt = 0;
358 struct stat sb;
359 int fd;
Bill Richardson9429f882012-07-26 13:10:59 -0700360 const char *fmap;
Bill Richardson60bcbe32010-09-09 14:53:56 -0700361 int retval = 1;
362
363 progname = strrchr(argv[0], '/');
364 if (progname)
365 progname++;
366 else
367 progname = argv[0];
368
Bill Richardson9429f882012-07-26 13:10:59 -0700369 opterr = 0; /* quiet, you */
Bill Richardsonae98bf02012-08-17 16:16:50 -0700370 while ((c = getopt(argc, argv, ":xpfhH")) != -1) {
Bill Richardson9429f882012-07-26 13:10:59 -0700371 switch (c) {
Bill Richardson60bcbe32010-09-09 14:53:56 -0700372 case 'x':
373 opt_extract = 1;
374 break;
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700375 case 'p':
Bill Richardson77c02432011-07-29 13:05:58 -0700376 opt_format = FMT_PRETTY;
377 break;
378 case 'f':
379 opt_format = FMT_FLASHROM;
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700380 break;
Bill Richardsonae98bf02012-08-17 16:16:50 -0700381 case 'H':
382 opt_gaps = 1;
383 /* fallthrough */
Bill Richardson9429f882012-07-26 13:10:59 -0700384 case 'h':
385 opt_format = FMT_HUMAN;
Bill Richardsona4090b52012-12-11 10:04:28 -0800386 opt_overlap++;
Bill Richardson9429f882012-07-26 13:10:59 -0700387 break;
Bill Richardson60bcbe32010-09-09 14:53:56 -0700388 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 Richardson9429f882012-07-26 13:10:59 -0700406 "\nUsage: %s [-x] [-p|-f|-h] FLASHIMAGE [NAME...]\n\n"
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700407 "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 Richardsone7e8ecd2012-03-01 11:05:29 -0800409 "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 Richardson9429f882012-07-26 13:10:59 -0700412 "\n"
413 "The -h option shows the whole FMAP in human-readable form.\n"
Bill Richardsonae98bf02012-08-17 16:16:50 -0700414 " Use -H to also display any gaps.\n"
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700415 "\n",
416 progname);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700417 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 Richardson60bcbe32010-09-09 14:53:56 -0700436
Bill Richardson9429f882012-07-26 13:10:59 -0700437 base_of_rom = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Che-Liang Chiou305e9e52011-02-17 17:56:16 +0800438 if (base_of_rom == (char*)-1) {
Bill Richardson60bcbe32010-09-09 14:53:56 -0700439 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 Chiou305e9e52011-02-17 17:56:16 +0800448 fmap = FmapFind((char*) base_of_rom, sb.st_size);
449 if (fmap) {
Bill Richardson9429f882012-07-26 13:10:59 -0700450 switch (opt_format) {
451 case FMT_HUMAN:
452 retval = human_fmap((void *)fmap);
453 break;
454 case FMT_NORMAL:
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700455 printf("hit at 0x%08x\n", (uint32_t) (fmap - (char*) base_of_rom));
Bill Richardson9429f882012-07-26 13:10:59 -0700456 /* fallthrough */
457 default:
458 retval = dump_fmap(fmap, argc-optind-1, argv+optind+1);
459 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700460 }
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 Richardson20807b62013-04-09 10:15:26 -0700472
473DECLARE_FUTIL_COMMAND(dump_fmap, do_dump_fmap,
474 "Display FMAP contents from a firmware image");