blob: a0d24de4de2770101663fee6940a4b0253321c9c [file] [log] [blame]
Bill Richardson60bcbe32010-09-09 14:53:56 -07001/*
2 * Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
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
18/* global variables */
19static int opt_extract = 0;
20static char *progname;
21static void *base_of_rom;
22
23/* FMAP structs. See http://code.google.com/p/flashmap/wiki/FmapSpec */
24#define FMAP_SIGLEN 8
25#define FMAP_NAMELEN 32
26#define FMAP_SEARCH_STRIDE 4
27typedef struct _FmapHeader {
28 char fmap_signature[FMAP_SIGLEN]; /* avoiding endian issues */
29 uint8_t fmap_ver_major;
30 uint8_t fmap_ver_minor;
31 uint64_t fmap_base;
32 uint32_t fmap_size;
33 char fmap_name[FMAP_NAMELEN];
34 uint16_t fmap_nareas;
35} __attribute__((packed)) FmapHeader;
36
37typedef struct _AreaHeader {
38 uint32_t area_offset;
39 uint32_t area_size;
40 char area_name[FMAP_NAMELEN];
41 uint16_t area_flags;
42} __attribute__((packed)) AreaHeader;
43
44
45/* Return 0 if successful */
46static int dump_fmap(void *ptr) {
47 int i,retval = 0;
48 char buf[80]; // DWR: magic number
49 FmapHeader *fmh = (FmapHeader *)ptr;
50 AreaHeader *ah = (AreaHeader *)(ptr + sizeof(FmapHeader));
51
52 snprintf(buf, FMAP_SIGLEN+1, "%s", fmh->fmap_signature);
53 printf("fmap_signature %s\n", buf);
54 printf("fmap_version: %d.%d\n", fmh->fmap_ver_major, fmh->fmap_ver_minor);
55 printf("fmap_base: 0x%" PRIx64 "\n", fmh->fmap_base);
56 printf("fmap_size: 0x%08x (%d)\n", fmh->fmap_size, fmh->fmap_size);
57 snprintf(buf, FMAP_NAMELEN+1, "%s", fmh->fmap_name);
58 printf("fmap_name: %s\n", buf);
59 printf("fmap_nareas: %d\n", fmh->fmap_nareas);
60
61 for (i=0; i<fmh->fmap_nareas; i++) {
62 printf("area: %d\n", i+1);
63 printf("area_offset: 0x%08x\n", ah->area_offset);
64 printf("area_size: 0x%08x (%d)\n", ah->area_size, ah->area_size);
65 snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name);
66 printf("area_name: %s\n", buf);
67
68 if (opt_extract) {
69 char *s;
70 for (s=buf; *s; s++)
71 if (*s == ' ')
72 *s = '_';
73 FILE *fp = fopen(buf,"wb");
74 if (!fp) {
75 fprintf(stderr, "%s: can't open %s: %s\n",
76 progname, buf, strerror(errno));
77 retval = 1;
78 } else {
79 if (1 != fwrite(base_of_rom + ah->area_offset, ah->area_size, 1, fp)) {
80 fprintf(stderr, "%s: can't write %s: %s\n",
81 progname, buf, strerror(errno));
82 retval = 1;
83 } else {
84 printf("saved as \"%s\"\n", buf);
85 }
86 fclose(fp);
87 }
88 }
89
90 ah++;
91 }
92
93 return retval;
94}
95
96
97int main(int argc, char *argv[]) {
98 int c;
99 int errorcnt = 0;
100 struct stat sb;
101 int fd;
102 char *s;
103 size_t i;
104 int retval = 1;
105
106 progname = strrchr(argv[0], '/');
107 if (progname)
108 progname++;
109 else
110 progname = argv[0];
111
112 opterr = 0; /* quiet, you */
113 while ((c=getopt(argc, argv, ":x")) != -1) {
114 switch (c)
115 {
116 case 'x':
117 opt_extract = 1;
118 break;
119 case '?':
120 fprintf(stderr, "%s: unrecognized switch: -%c\n",
121 progname, optopt);
122 errorcnt++;
123 break;
124 case ':':
125 fprintf(stderr, "%s: missing argument to -%c\n",
126 progname, optopt);
127 errorcnt++;
128 break;
129 default:
130 errorcnt++;
131 break;
132 }
133 }
134
135 if (errorcnt || optind >= argc) {
136 fprintf(stderr,
137 "\nUsage: %s [-x] FLASHIMAGE\n\n"
138 "Display (and extract with -x) the FMAP components from a BIOS image"
139 "\n\n",
140 progname);
141 return 1;
142 }
143
144 if (0 != stat(argv[optind], &sb)) {
145 fprintf(stderr, "%s: can't stat %s: %s\n",
146 progname,
147 argv[optind],
148 strerror(errno));
149 return 1;
150 }
151
152 fd = open(argv[optind], O_RDONLY);
153 if (fd < 0) {
154 fprintf(stderr, "%s: can't open %s: %s\n",
155 progname,
156 argv[optind],
157 strerror(errno));
158 return 1;
159 }
160 printf("opened %s\n", argv[optind]);
161
162 base_of_rom = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
163 if (base_of_rom == (char *)-1) {
164 fprintf(stderr, "%s: can't mmap %s: %s\n",
165 progname,
166 argv[optind],
167 strerror(errno));
168 close(fd);
169 return 1;
170 }
171 close(fd); /* done with this now */
172
173 s = (char *)base_of_rom;
174 for (i=0; i<sb.st_size; i += FMAP_SEARCH_STRIDE) {
175 if (0 == strncmp(s, "__FMAP__", 8)) {
176 printf("hit at 0x%08x\n", (uint32_t)i);
177 retval = dump_fmap(s);
178 break;
179 }
180 s++;
181 }
182
183 if (0 != munmap(base_of_rom, sb.st_size)) {
184 fprintf(stderr, "%s: can't munmap %s: %s\n",
185 progname,
186 argv[optind],
187 strerror(errno));
188 return 1;
189 }
190
191 return retval;
192}