Commit a804b5ce2d84dca6f9e145003e588876adf2c71f
Committed by
Tom Rini
1 parent
f86ed6a8d5
Exists in
master
and in
50 other branches
Add dumpimage, a tool to extract data from U-Boot images
Given a multi-file image created through the mkimage's -d option: $ mkimage -A x86 -O linux -T multi -n x86 -d vmlinuz:initrd.img:System.map \ multi.img Image Name: x86 Created: Thu Jul 25 10:29:13 2013 Image Type: Intel x86 Linux Multi-File Image (gzip compressed) Data Size: 13722956 Bytes = 13401.32 kB = 13.09 MB Load Address: 00000000 Entry Point: 00000000 Contents: Image 0: 4040128 Bytes = 3945.44 kB = 3.85 MB Image 1: 7991719 Bytes = 7804.41 kB = 7.62 MB Image 2: 1691092 Bytes = 1651.46 kB = 1.61 MB It is possible to perform the innverse operation -- extracting any file from the image -- by using the dumpimage's -i option: $ dumpimage -i multi.img -p 2 System.map Although it's feasible to retrieve "data files" from image through scripting, the requirement to embed tools such 'dd', 'awk' and 'sed' for this sole purpose is cumbersome and unreliable -- once you must keep track of file sizes inside the image. Furthermore, extracting data files using "dumpimage" tool is faster than through scripting. Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> Signed-off-by: Simon Glass <sjg@chromium.org>
Showing 8 changed files with 444 additions and 0 deletions Side-by-side Diff
Makefile
... | ... | @@ -797,6 +797,7 @@ |
797 | 797 | $(obj)tools/envcrc \ |
798 | 798 | $(obj)tools/gdb/{astest,gdbcont,gdbsend} \ |
799 | 799 | $(obj)tools/gen_eth_addr $(obj)tools/img2srec \ |
800 | + $(obj)tools/dump{env,}image \ | |
800 | 801 | $(obj)tools/mk{env,}image $(obj)tools/mpc86x_clk \ |
801 | 802 | $(obj)tools/mk{$(BOARD),}spl \ |
802 | 803 | $(obj)tools/mxsboot \ |
README
... | ... | @@ -5252,6 +5252,15 @@ |
5252 | 5252 | Load Address: 0x00000000 |
5253 | 5253 | Entry Point: 0x00000000 |
5254 | 5254 | |
5255 | +The "dumpimage" is a tool to disassemble images built by mkimage. Its "-i" | |
5256 | +option performs the converse operation of the mkimage's second form (the "-d" | |
5257 | +option). Given an image built by mkimage, the dumpimage extracts a "data file" | |
5258 | +from the image: | |
5259 | + | |
5260 | + tools/dumpimage -i image -p position data_file | |
5261 | + -i ==> extract from the 'image' a specific 'data_file', \ | |
5262 | + indexed by 'position' | |
5263 | + | |
5255 | 5264 | |
5256 | 5265 | Installing a Linux Image: |
5257 | 5266 | ------------------------- |
tools/.gitignore
tools/Makefile
... | ... | @@ -50,6 +50,7 @@ |
50 | 50 | BIN_FILES-$(CONFIG_CMD_NET) += gen_eth_addr$(SFX) |
51 | 51 | BIN_FILES-$(CONFIG_CMD_LOADS) += img2srec$(SFX) |
52 | 52 | BIN_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes$(SFX) |
53 | +BIN_FILES-y += dumpimage$(SFX) | |
53 | 54 | BIN_FILES-y += mkenvimage$(SFX) |
54 | 55 | BIN_FILES-y += mkimage$(SFX) |
55 | 56 | BIN_FILES-$(CONFIG_EXYNOS5250) += mk$(BOARD)spl$(SFX) |
... | ... | @@ -72,6 +73,7 @@ |
72 | 73 | # Source files located in the tools directory |
73 | 74 | NOPED_OBJ_FILES-y += aisimage.o |
74 | 75 | NOPED_OBJ_FILES-y += default_image.o |
76 | +NOPED_OBJ_FILES-y += dumpimage.o | |
75 | 77 | NOPED_OBJ_FILES-y += fit_image.o |
76 | 78 | NOPED_OBJ_FILES-y += image-host.o |
77 | 79 | NOPED_OBJ_FILES-y += imximage.o |
... | ... | @@ -198,6 +200,30 @@ |
198 | 200 | |
199 | 201 | $(obj)xway-swap-bytes$(SFX): $(obj)xway-swap-bytes.o |
200 | 202 | $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ |
203 | + $(HOSTSTRIP) $@ | |
204 | + | |
205 | +$(obj)dumpimage$(SFX): $(obj)aisimage.o \ | |
206 | + $(FIT_SIG_OBJS) \ | |
207 | + $(obj)crc32.o \ | |
208 | + $(obj)default_image.o \ | |
209 | + $(obj)fit_image.o \ | |
210 | + $(obj)image-fit.o \ | |
211 | + $(obj)image.o \ | |
212 | + $(obj)image-host.o \ | |
213 | + $(obj)imagetool.o \ | |
214 | + $(obj)imximage.o \ | |
215 | + $(obj)kwbimage.o \ | |
216 | + $(obj)dumpimage.o \ | |
217 | + $(obj)md5.o \ | |
218 | + $(obj)mxsimage.o \ | |
219 | + $(obj)omapimage.o \ | |
220 | + $(obj)os_support.o \ | |
221 | + $(obj)pblimage.o \ | |
222 | + $(obj)sha1.o \ | |
223 | + $(obj)ublimage.o \ | |
224 | + $(LIBFDT_OBJS) \ | |
225 | + $(RSA_OBJS) | |
226 | + $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTLIBS) | |
201 | 227 | $(HOSTSTRIP) $@ |
202 | 228 | |
203 | 229 | $(obj)mkenvimage$(SFX): $(obj)crc32.o $(obj)mkenvimage.o \ |
tools/default_image.c
... | ... | @@ -117,6 +117,62 @@ |
117 | 117 | image_set_hcrc(hdr, checksum); |
118 | 118 | } |
119 | 119 | |
120 | +static int image_save_datafile(struct image_tool_params *params, | |
121 | + ulong file_data, ulong file_len) | |
122 | +{ | |
123 | + int dfd; | |
124 | + const char *datafile = params->outfile; | |
125 | + | |
126 | + dfd = open(datafile, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, | |
127 | + S_IRUSR | S_IWUSR); | |
128 | + if (dfd < 0) { | |
129 | + fprintf(stderr, "%s: Can't open \"%s\": %s\n", | |
130 | + params->cmdname, datafile, strerror(errno)); | |
131 | + return -1; | |
132 | + } | |
133 | + | |
134 | + if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) { | |
135 | + fprintf(stderr, "%s: Write error on \"%s\": %s\n", | |
136 | + params->cmdname, datafile, strerror(errno)); | |
137 | + close(dfd); | |
138 | + return -1; | |
139 | + } | |
140 | + | |
141 | + close(dfd); | |
142 | + | |
143 | + return 0; | |
144 | +} | |
145 | + | |
146 | +static int image_extract_datafile(void *ptr, struct image_tool_params *params) | |
147 | +{ | |
148 | + const image_header_t *hdr = (const image_header_t *)ptr; | |
149 | + ulong file_data; | |
150 | + ulong file_len; | |
151 | + | |
152 | + if (image_check_type(hdr, IH_TYPE_MULTI)) { | |
153 | + ulong idx = params->pflag; | |
154 | + ulong count; | |
155 | + | |
156 | + /* get the number of data files present in the image */ | |
157 | + count = image_multi_count(hdr); | |
158 | + | |
159 | + /* retrieve the "data file" at the idx position */ | |
160 | + image_multi_getimg(hdr, idx, &file_data, &file_len); | |
161 | + | |
162 | + if ((file_len == 0) || (idx >= count)) { | |
163 | + fprintf(stderr, "%s: No such data file %ld in \"%s\"\n", | |
164 | + params->cmdname, idx, params->imagefile); | |
165 | + return -1; | |
166 | + } | |
167 | + } else { | |
168 | + file_data = image_get_data(hdr); | |
169 | + file_len = image_get_size(hdr); | |
170 | + } | |
171 | + | |
172 | + /* save the "data file" into the file system */ | |
173 | + return image_save_datafile(params, file_data, file_len); | |
174 | +} | |
175 | + | |
120 | 176 | /* |
121 | 177 | * Default image type parameters definition |
122 | 178 | */ |
... | ... | @@ -128,6 +184,7 @@ |
128 | 184 | .verify_header = image_verify_header, |
129 | 185 | .print_header = image_print_contents, |
130 | 186 | .set_header = image_set_header, |
187 | + .extract_datafile = image_extract_datafile, | |
131 | 188 | .check_params = image_check_params, |
132 | 189 | }; |
133 | 190 |
tools/dumpimage.c
1 | +/* | |
2 | + * Based on mkimage.c. | |
3 | + * | |
4 | + * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> | |
5 | + * | |
6 | + * SPDX-License-Identifier: GPL-2.0+ | |
7 | + */ | |
8 | + | |
9 | +#include "dumpimage.h" | |
10 | +#include <image.h> | |
11 | +#include <version.h> | |
12 | + | |
13 | +static void usage(void); | |
14 | + | |
15 | +/* image_type_params linked list to maintain registered image types supports */ | |
16 | +static struct image_type_params *dumpimage_tparams; | |
17 | + | |
18 | +/* parameters initialized by core will be used by the image type code */ | |
19 | +static struct image_tool_params params = { | |
20 | + .type = IH_TYPE_KERNEL, | |
21 | +}; | |
22 | + | |
23 | +/** | |
24 | + * dumpimage_register() - register respective image generation/list support | |
25 | + * | |
26 | + * the input struct image_type_params is checked and appended to the link | |
27 | + * list, if the input structure is already registered, issue an error | |
28 | + * | |
29 | + * @tparams: Image type parameters | |
30 | + */ | |
31 | +static void dumpimage_register(struct image_type_params *tparams) | |
32 | +{ | |
33 | + struct image_type_params **tp; | |
34 | + | |
35 | + if (!tparams) { | |
36 | + fprintf(stderr, "%s: %s: Null input\n", params.cmdname, | |
37 | + __func__); | |
38 | + exit(EXIT_FAILURE); | |
39 | + } | |
40 | + | |
41 | + /* scan the linked list, check for registry and point the last one */ | |
42 | + for (tp = &dumpimage_tparams; *tp != NULL; tp = &(*tp)->next) { | |
43 | + if (!strcmp((*tp)->name, tparams->name)) { | |
44 | + fprintf(stderr, "%s: %s already registered\n", | |
45 | + params.cmdname, tparams->name); | |
46 | + return; | |
47 | + } | |
48 | + } | |
49 | + | |
50 | + /* add input struct entry at the end of link list */ | |
51 | + *tp = tparams; | |
52 | + /* mark input entry as last entry in the link list */ | |
53 | + tparams->next = NULL; | |
54 | + | |
55 | + debug("Registered %s\n", tparams->name); | |
56 | +} | |
57 | + | |
58 | +/** | |
59 | + * dumpimage_get_type() - find the image type params for a given image type | |
60 | + * | |
61 | + * Scan all registered image types and check the input type_id for each | |
62 | + * supported image type | |
63 | + * | |
64 | + * @return respective image_type_params pointer. If the input type is not | |
65 | + * supported by any of registered image types, returns NULL | |
66 | + */ | |
67 | +static struct image_type_params *dumpimage_get_type(int type) | |
68 | +{ | |
69 | + struct image_type_params *curr; | |
70 | + | |
71 | + for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) { | |
72 | + if (curr->check_image_type) { | |
73 | + if (!curr->check_image_type(type)) | |
74 | + return curr; | |
75 | + } | |
76 | + } | |
77 | + return NULL; | |
78 | +} | |
79 | + | |
80 | +/* | |
81 | + * dumpimage_verify_print_header() - verifies the image header | |
82 | + * | |
83 | + * Scan registered image types and verify the image_header for each | |
84 | + * supported image type. If verification is successful, this prints | |
85 | + * the respective header. | |
86 | + * | |
87 | + * @return 0 on success, negative if input image format does not match with | |
88 | + * any of supported image types | |
89 | + */ | |
90 | +static int dumpimage_verify_print_header(void *ptr, struct stat *sbuf) | |
91 | +{ | |
92 | + int retval = -1; | |
93 | + struct image_type_params *curr; | |
94 | + | |
95 | + for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) { | |
96 | + if (curr->verify_header) { | |
97 | + retval = curr->verify_header((unsigned char *)ptr, | |
98 | + sbuf->st_size, ¶ms); | |
99 | + if (retval != 0) | |
100 | + continue; | |
101 | + /* | |
102 | + * Print the image information if verify is | |
103 | + * successful | |
104 | + */ | |
105 | + if (curr->print_header) { | |
106 | + curr->print_header(ptr); | |
107 | + } else { | |
108 | + fprintf(stderr, | |
109 | + "%s: print_header undefined for %s\n", | |
110 | + params.cmdname, curr->name); | |
111 | + } | |
112 | + break; | |
113 | + } | |
114 | + } | |
115 | + | |
116 | + return retval; | |
117 | +} | |
118 | + | |
119 | +/* | |
120 | + * dumpimage_extract_datafile - | |
121 | + * | |
122 | + * It scans all registered image types, | |
123 | + * verifies image_header for each supported image type | |
124 | + * if verification is successful, it extracts the desired file, | |
125 | + * indexed by pflag, from the image | |
126 | + * | |
127 | + * returns negative if input image format does not match with any of | |
128 | + * supported image types | |
129 | + */ | |
130 | +static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf) | |
131 | +{ | |
132 | + int retval = -1; | |
133 | + struct image_type_params *curr; | |
134 | + | |
135 | + for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) { | |
136 | + if (curr->verify_header) { | |
137 | + retval = curr->verify_header((unsigned char *)ptr, | |
138 | + sbuf->st_size, ¶ms); | |
139 | + if (retval != 0) | |
140 | + continue; | |
141 | + /* | |
142 | + * Extract the file from the image | |
143 | + * if verify is successful | |
144 | + */ | |
145 | + if (curr->extract_datafile) { | |
146 | + curr->extract_datafile(ptr, ¶ms); | |
147 | + } else { | |
148 | + fprintf(stderr, | |
149 | + "%s: extract_datafile undefined for %s\n", | |
150 | + params.cmdname, curr->name); | |
151 | + break; | |
152 | + } | |
153 | + } | |
154 | + } | |
155 | + | |
156 | + return retval; | |
157 | +} | |
158 | + | |
159 | +int main(int argc, char **argv) | |
160 | +{ | |
161 | + int opt; | |
162 | + int ifd = -1; | |
163 | + struct stat sbuf; | |
164 | + char *ptr; | |
165 | + int retval = 0; | |
166 | + struct image_type_params *tparams = NULL; | |
167 | + | |
168 | + /* Init all image generation/list support */ | |
169 | + register_image_tool(dumpimage_register); | |
170 | + | |
171 | + params.cmdname = *argv; | |
172 | + | |
173 | + while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) { | |
174 | + switch (opt) { | |
175 | + case 'l': | |
176 | + params.lflag = 1; | |
177 | + break; | |
178 | + case 'i': | |
179 | + params.imagefile = optarg; | |
180 | + params.iflag = 1; | |
181 | + break; | |
182 | + case 'o': | |
183 | + params.outfile = optarg; | |
184 | + break; | |
185 | + case 'p': | |
186 | + params.pflag = strtoul(optarg, &ptr, 10); | |
187 | + if (*ptr) { | |
188 | + fprintf(stderr, | |
189 | + "%s: invalid file position %s\n", | |
190 | + params.cmdname, *argv); | |
191 | + exit(EXIT_FAILURE); | |
192 | + } | |
193 | + break; | |
194 | + case 'V': | |
195 | + printf("dumpimage version %s\n", PLAIN_VERSION); | |
196 | + exit(EXIT_SUCCESS); | |
197 | + default: | |
198 | + usage(); | |
199 | + } | |
200 | + } | |
201 | + | |
202 | + if (optind >= argc) | |
203 | + usage(); | |
204 | + | |
205 | + /* set tparams as per input type_id */ | |
206 | + tparams = dumpimage_get_type(params.type); | |
207 | + if (tparams == NULL) { | |
208 | + fprintf(stderr, "%s: unsupported type %s\n", | |
209 | + params.cmdname, genimg_get_type_name(params.type)); | |
210 | + exit(EXIT_FAILURE); | |
211 | + } | |
212 | + | |
213 | + /* | |
214 | + * check the passed arguments parameters meets the requirements | |
215 | + * as per image type to be generated/listed | |
216 | + */ | |
217 | + if (tparams->check_params) { | |
218 | + if (tparams->check_params(¶ms)) | |
219 | + usage(); | |
220 | + } | |
221 | + | |
222 | + if (params.iflag) | |
223 | + params.datafile = argv[optind]; | |
224 | + else | |
225 | + params.imagefile = argv[optind]; | |
226 | + if (!params.outfile) | |
227 | + params.outfile = params.datafile; | |
228 | + | |
229 | + ifd = open(params.imagefile, O_RDONLY|O_BINARY); | |
230 | + if (ifd < 0) { | |
231 | + fprintf(stderr, "%s: Can't open \"%s\": %s\n", | |
232 | + params.cmdname, params.imagefile, | |
233 | + strerror(errno)); | |
234 | + exit(EXIT_FAILURE); | |
235 | + } | |
236 | + | |
237 | + if (params.lflag || params.iflag) { | |
238 | + if (fstat(ifd, &sbuf) < 0) { | |
239 | + fprintf(stderr, "%s: Can't stat \"%s\": %s\n", | |
240 | + params.cmdname, params.imagefile, | |
241 | + strerror(errno)); | |
242 | + exit(EXIT_FAILURE); | |
243 | + } | |
244 | + | |
245 | + if ((unsigned)sbuf.st_size < tparams->header_size) { | |
246 | + fprintf(stderr, | |
247 | + "%s: Bad size: \"%s\" is not valid image\n", | |
248 | + params.cmdname, params.imagefile); | |
249 | + exit(EXIT_FAILURE); | |
250 | + } | |
251 | + | |
252 | + ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); | |
253 | + if (ptr == MAP_FAILED) { | |
254 | + fprintf(stderr, "%s: Can't read \"%s\": %s\n", | |
255 | + params.cmdname, params.imagefile, | |
256 | + strerror(errno)); | |
257 | + exit(EXIT_FAILURE); | |
258 | + } | |
259 | + | |
260 | + /* | |
261 | + * Both calls bellow scan through dumpimage registry for all | |
262 | + * supported image types and verify the input image file | |
263 | + * header for match | |
264 | + */ | |
265 | + if (params.iflag) { | |
266 | + /* | |
267 | + * Extract the data files from within the matched | |
268 | + * image type. Returns the error code if not matched | |
269 | + */ | |
270 | + retval = dumpimage_extract_datafile(ptr, &sbuf); | |
271 | + } else { | |
272 | + /* | |
273 | + * Print the image information for matched image type | |
274 | + * Returns the error code if not matched | |
275 | + */ | |
276 | + retval = dumpimage_verify_print_header(ptr, &sbuf); | |
277 | + } | |
278 | + | |
279 | + (void)munmap((void *)ptr, sbuf.st_size); | |
280 | + (void)close(ifd); | |
281 | + | |
282 | + return retval; | |
283 | + } | |
284 | + | |
285 | + (void)close(ifd); | |
286 | + | |
287 | + return EXIT_SUCCESS; | |
288 | +} | |
289 | + | |
290 | +static void usage(void) | |
291 | +{ | |
292 | + fprintf(stderr, "Usage: %s -l image\n" | |
293 | + " -l ==> list image header information\n", | |
294 | + params.cmdname); | |
295 | + fprintf(stderr, | |
296 | + " %s -i image [-p position] [-o outfile] data_file\n" | |
297 | + " -i ==> extract from the 'image' a specific 'data_file'" | |
298 | + ", indexed by 'position' (starting at 0)\n", | |
299 | + params.cmdname); | |
300 | + fprintf(stderr, | |
301 | + " %s -V ==> print version information and exit\n", | |
302 | + params.cmdname); | |
303 | + | |
304 | + exit(EXIT_FAILURE); | |
305 | +} |
tools/dumpimage.h
1 | +/* | |
2 | + * Based on mkimage.c. | |
3 | + * | |
4 | + * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> | |
5 | + * | |
6 | + * SPDX-License-Identifier: GPL-2.0+ | |
7 | + */ | |
8 | + | |
9 | +#ifndef _DUMPIMAGE_H_ | |
10 | +#define _DUMPIMAGE_H_ | |
11 | + | |
12 | +#include "os_support.h" | |
13 | +#include <errno.h> | |
14 | +#include <fcntl.h> | |
15 | +#include <stdio.h> | |
16 | +#include <stdlib.h> | |
17 | +#include <string.h> | |
18 | +#include <sys/stat.h> | |
19 | +#include <time.h> | |
20 | +#include <unistd.h> | |
21 | +#include <sha1.h> | |
22 | +#include "fdt_host.h" | |
23 | +#include "imagetool.h" | |
24 | + | |
25 | +#undef DUMPIMAGE_DEBUG | |
26 | + | |
27 | +#ifdef DUMPIMAGE_DEBUG | |
28 | +#define debug(fmt, args...) printf(fmt, ##args) | |
29 | +#else | |
30 | +#define debug(fmt, args...) | |
31 | +#endif /* DUMPIMAGE_DEBUG */ | |
32 | + | |
33 | +#endif /* _DUMPIMAGE_H_ */ |
tools/imagetool.h
... | ... | @@ -34,7 +34,9 @@ |
34 | 34 | int dflag; |
35 | 35 | int eflag; |
36 | 36 | int fflag; |
37 | + int iflag; | |
37 | 38 | int lflag; |
39 | + int pflag; | |
38 | 40 | int vflag; |
39 | 41 | int xflag; |
40 | 42 | int skipcpy; |
... | ... | @@ -50,6 +52,7 @@ |
50 | 52 | char *datafile; |
51 | 53 | char *imagefile; |
52 | 54 | char *cmdname; |
55 | + const char *outfile; /* Output filename */ | |
53 | 56 | const char *keydir; /* Directory holding private keys */ |
54 | 57 | const char *keydest; /* Destination .dtb for public key */ |
55 | 58 | const char *comment; /* Comment to add to signature node */ |
... | ... | @@ -96,6 +99,15 @@ |
96 | 99 | */ |
97 | 100 | void (*set_header) (void *, struct stat *, int, |
98 | 101 | struct image_tool_params *); |
102 | + /* | |
103 | + * This function is used by the command to retrieve a data file from | |
104 | + * the image (i.e. dumpimage -i <image> -p <position> <data_file>). | |
105 | + * Thus the code to extract a file from an image must be put here. | |
106 | + * | |
107 | + * Returns 0 if the file was successfully retrieved from the image, | |
108 | + * or a negative value on error. | |
109 | + */ | |
110 | + int (*extract_datafile) (void *, struct image_tool_params *); | |
99 | 111 | /* |
100 | 112 | * Some image generation support for ex (default image type) supports |
101 | 113 | * more than one type_ids, this callback function is used to check |