]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/dumpimage.c
4 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 static void usage(void);
15 /* image_type_params linked list to maintain registered image types supports */
16 static struct image_type_params
*dumpimage_tparams
;
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
,
24 * dumpimage_register() - register respective image generation/list support
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
29 * @tparams: Image type parameters
31 static void dumpimage_register(struct image_type_params
*tparams
)
33 struct image_type_params
**tp
;
36 fprintf(stderr
, "%s: %s: Null input\n", params
.cmdname
,
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
);
50 /* add input struct entry at the end of link list */
52 /* mark input entry as last entry in the link list */
55 debug("Registered %s\n", tparams
->name
);
59 * dumpimage_get_type() - find the image type params for a given image type
61 * Scan all registered image types and check the input type_id for each
62 * supported image type
64 * @return respective image_type_params pointer. If the input type is not
65 * supported by any of registered image types, returns NULL
67 static struct image_type_params
*dumpimage_get_type(int type
)
69 struct image_type_params
*curr
;
71 for (curr
= dumpimage_tparams
; curr
!= NULL
; curr
= curr
->next
) {
72 if (curr
->check_image_type
) {
73 if (!curr
->check_image_type(type
))
81 * dumpimage_verify_print_header() - verifies the image header
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.
87 * @return 0 on success, negative if input image format does not match with
88 * any of supported image types
90 static int dumpimage_verify_print_header(void *ptr
, struct stat
*sbuf
)
93 struct image_type_params
*curr
;
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
);
102 * Print the image information if verify is
105 if (curr
->print_header
) {
106 curr
->print_header(ptr
);
109 "%s: print_header undefined for %s\n",
110 params
.cmdname
, curr
->name
);
120 * dumpimage_extract_datafile -
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
127 * returns negative if input image format does not match with any of
128 * supported image types
130 static int dumpimage_extract_datafile(void *ptr
, struct stat
*sbuf
)
133 struct image_type_params
*curr
;
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
);
142 * Extract the file from the image
143 * if verify is successful
145 if (curr
->extract_datafile
) {
146 curr
->extract_datafile(ptr
, ¶ms
);
149 "%s: extract_datafile undefined for %s\n",
150 params
.cmdname
, curr
->name
);
159 int main(int argc
, char **argv
)
166 struct image_type_params
*tparams
= NULL
;
168 /* Init all image generation/list support */
169 register_image_tool(dumpimage_register
);
171 params
.cmdname
= *argv
;
173 while ((opt
= getopt(argc
, argv
, "li:o:p:V")) != -1) {
179 params
.imagefile
= optarg
;
183 params
.outfile
= optarg
;
186 params
.pflag
= strtoul(optarg
, &ptr
, 10);
189 "%s: invalid file position %s\n",
190 params
.cmdname
, *argv
);
195 printf("dumpimage version %s\n", PLAIN_VERSION
);
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
));
214 * check the passed arguments parameters meets the requirements
215 * as per image type to be generated/listed
217 if (tparams
->check_params
) {
218 if (tparams
->check_params(¶ms
))
223 params
.datafile
= argv
[optind
];
225 params
.imagefile
= argv
[optind
];
227 params
.outfile
= params
.datafile
;
229 ifd
= open(params
.imagefile
, O_RDONLY
|O_BINARY
);
231 fprintf(stderr
, "%s: Can't open \"%s\": %s\n",
232 params
.cmdname
, params
.imagefile
,
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
,
245 if ((unsigned)sbuf
.st_size
< tparams
->header_size
) {
247 "%s: Bad size: \"%s\" is not valid image\n",
248 params
.cmdname
, params
.imagefile
);
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
,
261 * Both calls bellow scan through dumpimage registry for all
262 * supported image types and verify the input image file
267 * Extract the data files from within the matched
268 * image type. Returns the error code if not matched
270 retval
= dumpimage_extract_datafile(ptr
, &sbuf
);
273 * Print the image information for matched image type
274 * Returns the error code if not matched
276 retval
= dumpimage_verify_print_header(ptr
, &sbuf
);
279 (void)munmap((void *)ptr
, sbuf
.st_size
);
290 static void usage(void)
292 fprintf(stderr
, "Usage: %s -l image\n"
293 " -l ==> list image header information\n",
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",
301 " %s -V ==> print version information and exit\n",