blob: 28fd2f2de59bc46e51c8c7c86153b22dd402ff86 [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 Richardsona4090b52012-12-11 10:04:28 -080025static int opt_overlap = 0;
Bill Richardson9429f882012-07-26 13:10:59 -070026static char *progname;
27static void *base_of_rom;
Bill Richardsonae98bf02012-08-17 16:16:50 -070028static int opt_gaps = 0;
Bill Richardson60bcbe32010-09-09 14:53:56 -070029
30
31/* Return 0 if successful */
Bill Richardson9429f882012-07-26 13:10:59 -070032static int dump_fmap(const void *ptr, int argc, char *argv[])
33{
Bill Richardsonf4729192012-05-02 23:30:16 -070034 int i, retval = 0;
Bill Richardson60bcbe32010-09-09 14:53:56 -070035 char buf[80]; // DWR: magic number
Bill Richardson9429f882012-07-26 13:10:59 -070036 const FmapHeader *fmh = (const FmapHeader*)ptr;
37 const FmapAreaHeader *ah = (const FmapAreaHeader*)(ptr + sizeof(FmapHeader));
Bill Richardson60bcbe32010-09-09 14:53:56 -070038
Bill Richardson77c02432011-07-29 13:05:58 -070039 if (FMT_NORMAL == opt_format) {
Bill Richardson9a2c3b22011-06-13 12:45:24 -070040 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 Richardson60bcbe32010-09-09 14:53:56 -070050
Bill Richardson9429f882012-07-26 13:10:59 -070051 for (i = 0; i < fmh->fmap_nareas; i++, ah++) {
Bill Richardson77c02432011-07-29 13:05:58 -070052 snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name);
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080053
54 if (argc) {
55 int j, found=0;
Bill Richardson9429f882012-07-26 13:10:59 -070056 for (j = 0; j < argc; j++)
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080057 if (!strcmp(argv[j], buf)) {
Bill Richardson9429f882012-07-26 13:10:59 -070058 found = 1;
59 break;
60 }
Bill Richardsone7e8ecd2012-03-01 11:05:29 -080061 if (!found) {
62 continue;
63 }
64 }
65
Bill Richardson9429f882012-07-26 13:10:59 -070066 switch (opt_format) {
Bill Richardson77c02432011-07-29 13:05:58 -070067 case FMT_PRETTY:
Bill Richardson9a2c3b22011-06-13 12:45:24 -070068 printf("%s %d %d\n", buf, ah->area_offset, ah->area_size);
Bill Richardson77c02432011-07-29 13:05:58 -070069 break;
70 case FMT_FLASHROM:
71 if (ah->area_size)
Louis Yung-Chieh Lo9462e3e2011-08-24 12:53:43 +080072 printf("0x%08x:0x%08x %s\n", ah->area_offset,
Bill Richardson9429f882012-07-26 13:10:59 -070073 ah->area_offset + ah->area_size - 1, buf);
Bill Richardson77c02432011-07-29 13:05:58 -070074 break;
75 default:
Bill Richardson9a2c3b22011-06-13 12:45:24 -070076 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 Richardson60bcbe32010-09-09 14:53:56 -070081
82 if (opt_extract) {
Bill Richardson9429f882012-07-26 13:10:59 -070083 char *s;
84 for (s = buf; *s; s++)
Bill Richardson60bcbe32010-09-09 14:53:56 -070085 if (*s == ' ')
Bill Richardson9a2c3b22011-06-13 12:45:24 -070086 *s = '_';
Bill Richardson9429f882012-07-26 13:10:59 -070087 FILE *fp = fopen(buf,"wb");
Bill Richardson60bcbe32010-09-09 14:53:56 -070088 if (!fp) {
89 fprintf(stderr, "%s: can't open %s: %s\n",
90 progname, buf, strerror(errno));
91 retval = 1;
92 } else {
Bill Richardsonccdaa472011-03-03 18:08:18 -080093 if (ah->area_size &&
94 1 != fwrite(base_of_rom + ah->area_offset, ah->area_size, 1, fp)) {
Bill Richardson60bcbe32010-09-09 14:53:56 -070095 fprintf(stderr, "%s: can't write %s: %s\n",
96 progname, buf, strerror(errno));
97 retval = 1;
98 } else {
Bill Richardson77c02432011-07-29 13:05:58 -070099 if (FMT_NORMAL == opt_format)
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700100 printf("saved as \"%s\"\n", buf);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700101 }
102 fclose(fp);
103 }
104 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700105 }
106
107 return retval;
108}
109
110
Bill Richardsonae98bf02012-08-17 16:16:50 -0700111/****************************************************************************/
112/* Stuff for human-readable form */
113
114typedef struct dup_s {
115 char *name;
116 struct dup_s *next;
117} dupe_t;
118
119typedef 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
130static node_t *all_nodes;
131
132static void sort_nodes(int num, node_t *ary[])
Bill Richardson9429f882012-07-26 13:10:59 -0700133{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700134 int i, j;
135 node_t *tmp;
Bill Richardson9429f882012-07-26 13:10:59 -0700136
Bill Richardsonae98bf02012-08-17 16:16:50 -0700137 /* 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 Richardson9429f882012-07-26 13:10:59 -0700146 }
Bill Richardson9429f882012-07-26 13:10:59 -0700147}
148
149
Bill Richardsonae98bf02012-08-17 16:16:50 -0700150static void line(int indent, char *name,
151 uint32_t start, uint32_t end, uint32_t size, char *append)
Bill Richardson9429f882012-07-26 13:10:59 -0700152{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700153 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 Richardson9429f882012-07-26 13:10:59 -0700159
Bill Richardsonae98bf02012-08-17 16:16:50 -0700160static int gapcount;
161static 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
171static 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 Richardson9429f882012-07-26 13:10:59 -0700189 }
190}
191
Bill Richardsonae98bf02012-08-17 16:16:50 -0700192static int overlaps(int i, int j)
Bill Richardson9429f882012-07-26 13:10:59 -0700193{
Bill Richardsonae98bf02012-08-17 16:16:50 -0700194 node_t *a = all_nodes + i;
195 node_t *b = all_nodes + j;
Bill Richardson9429f882012-07-26 13:10:59 -0700196
Bill Richardsonae98bf02012-08-17 16:16:50 -0700197 return ((a->start < b->start) && (b->start < a->end) &&
198 (b->start < a->end) && (a->end < b->end));
199}
Bill Richardson9429f882012-07-26 13:10:59 -0700200
Bill Richardsonae98bf02012-08-17 16:16:50 -0700201static int encloses(int i, int j)
202{
203 node_t *a = all_nodes + i;
204 node_t *b = all_nodes + j;
Bill Richardson9429f882012-07-26 13:10:59 -0700205
Bill Richardsonae98bf02012-08-17 16:16:50 -0700206 return ((a->start <= b->start) &&
207 (a->end >= b->end));
208}
209
210static 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
219static 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
232static 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
249static 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 Richardson9429f882012-07-26 13:10:59 -0700300 }
301
Bill Richardsonae98bf02012-08-17 16:16:50 -0700302 /* 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 Richardsona4090b52012-12-11 10:04:28 -0800318 if (opt_overlap < 2) {
319 printf("Use more -h args to ignore this error\n");
320 errorcnt++;
321 }
Bill Richardsonae98bf02012-08-17 16:16:50 -0700322 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 Richardson9429f882012-07-26 13:10:59 -0700328 }
Bill Richardsonae98bf02012-08-17 16:16:50 -0700329 if (errorcnt)
330 return 1;
Bill Richardson9429f882012-07-26 13:10:59 -0700331
Bill Richardsonae98bf02012-08-17 16:16:50 -0700332 /* 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 Richardson9429f882012-07-26 13:10:59 -0700346
347 return 0;
348}
349
Bill Richardsonae98bf02012-08-17 16:16:50 -0700350/* End of human-reable stuff */
351/****************************************************************************/
Bill Richardson9429f882012-07-26 13:10:59 -0700352
353int main(int argc, char *argv[])
354{
Bill Richardson60bcbe32010-09-09 14:53:56 -0700355 int c;
356 int errorcnt = 0;
357 struct stat sb;
358 int fd;
Bill Richardson9429f882012-07-26 13:10:59 -0700359 const char *fmap;
Bill Richardson60bcbe32010-09-09 14:53:56 -0700360 int retval = 1;
361
362 progname = strrchr(argv[0], '/');
363 if (progname)
364 progname++;
365 else
366 progname = argv[0];
367
Bill Richardson9429f882012-07-26 13:10:59 -0700368 opterr = 0; /* quiet, you */
Bill Richardsonae98bf02012-08-17 16:16:50 -0700369 while ((c = getopt(argc, argv, ":xpfhH")) != -1) {
Bill Richardson9429f882012-07-26 13:10:59 -0700370 switch (c) {
Bill Richardson60bcbe32010-09-09 14:53:56 -0700371 case 'x':
372 opt_extract = 1;
373 break;
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700374 case 'p':
Bill Richardson77c02432011-07-29 13:05:58 -0700375 opt_format = FMT_PRETTY;
376 break;
377 case 'f':
378 opt_format = FMT_FLASHROM;
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700379 break;
Bill Richardsonae98bf02012-08-17 16:16:50 -0700380 case 'H':
381 opt_gaps = 1;
382 /* fallthrough */
Bill Richardson9429f882012-07-26 13:10:59 -0700383 case 'h':
384 opt_format = FMT_HUMAN;
Bill Richardsona4090b52012-12-11 10:04:28 -0800385 opt_overlap++;
Bill Richardson9429f882012-07-26 13:10:59 -0700386 break;
Bill Richardson60bcbe32010-09-09 14:53:56 -0700387 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 Richardson9429f882012-07-26 13:10:59 -0700405 "\nUsage: %s [-x] [-p|-f|-h] FLASHIMAGE [NAME...]\n\n"
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700406 "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 Richardsone7e8ecd2012-03-01 11:05:29 -0800408 "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 Richardson9429f882012-07-26 13:10:59 -0700411 "\n"
412 "The -h option shows the whole FMAP in human-readable form.\n"
Bill Richardsonae98bf02012-08-17 16:16:50 -0700413 " Use -H to also display any gaps.\n"
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700414 "\n",
415 progname);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700416 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 Richardson77c02432011-07-29 13:05:58 -0700435 if (FMT_NORMAL == opt_format)
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700436 printf("opened %s\n", argv[optind]);
Bill Richardson60bcbe32010-09-09 14:53:56 -0700437
Bill Richardson9429f882012-07-26 13:10:59 -0700438 base_of_rom = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Che-Liang Chiou305e9e52011-02-17 17:56:16 +0800439 if (base_of_rom == (char*)-1) {
Bill Richardson60bcbe32010-09-09 14:53:56 -0700440 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 Chiou305e9e52011-02-17 17:56:16 +0800449 fmap = FmapFind((char*) base_of_rom, sb.st_size);
450 if (fmap) {
Bill Richardson9429f882012-07-26 13:10:59 -0700451 switch (opt_format) {
452 case FMT_HUMAN:
453 retval = human_fmap((void *)fmap);
454 break;
455 case FMT_NORMAL:
Bill Richardson9a2c3b22011-06-13 12:45:24 -0700456 printf("hit at 0x%08x\n", (uint32_t) (fmap - (char*) base_of_rom));
Bill Richardson9429f882012-07-26 13:10:59 -0700457 /* fallthrough */
458 default:
459 retval = dump_fmap(fmap, argc-optind-1, argv+optind+1);
460 }
Bill Richardson60bcbe32010-09-09 14:53:56 -0700461 }
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}