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_extract_datafile -
26 * It scans all registered image types,
27 * verifies image_header for each supported image type
28 * if verification is successful, it extracts the desired file,
29 * indexed by pflag, from the image
31 * returns negative if input image format does not match with any of
32 * supported image types
34 static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf)
37 struct image_type_params *curr;
38 struct image_type_params *start = ll_entry_start(
39 struct image_type_params, image_type);
40 struct image_type_params *end = ll_entry_end(
41 struct image_type_params, image_type);
43 for (curr = start; curr != end; curr++) {
44 if (curr->verify_header) {
45 retval = curr->verify_header((unsigned char *)ptr,
46 sbuf->st_size, ¶ms);
50 * Extract the file from the image
51 * if verify is successful
53 if (curr->extract_datafile) {
54 curr->extract_datafile(ptr, ¶ms);
57 "%s: extract_datafile undefined for %s\n",
58 params.cmdname, curr->name);
67 int main(int argc, char **argv)
74 struct image_type_params *tparams = NULL;
76 params.cmdname = *argv;
78 while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) {
84 params.imagefile = optarg;
88 params.outfile = optarg;
91 params.pflag = strtoul(optarg, &ptr, 10);
94 "%s: invalid file position %s\n",
95 params.cmdname, *argv);
100 printf("dumpimage version %s\n", PLAIN_VERSION);
110 /* set tparams as per input type_id */
111 tparams = imagetool_get_type(params.type);
112 if (tparams == NULL) {
113 fprintf(stderr, "%s: unsupported type %s\n",
114 params.cmdname, genimg_get_type_name(params.type));
119 * check the passed arguments parameters meets the requirements
120 * as per image type to be generated/listed
122 if (tparams->check_params) {
123 if (tparams->check_params(¶ms))
128 params.datafile = argv[optind];
130 params.imagefile = argv[optind];
132 params.outfile = params.datafile;
134 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
136 fprintf(stderr, "%s: Can't open \"%s\": %s\n",
137 params.cmdname, params.imagefile,
142 if (params.lflag || params.iflag) {
143 if (fstat(ifd, &sbuf) < 0) {
144 fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
145 params.cmdname, params.imagefile,
150 if ((unsigned)sbuf.st_size < tparams->header_size) {
152 "%s: Bad size: \"%s\" is not valid image\n",
153 params.cmdname, params.imagefile);
157 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
158 if (ptr == MAP_FAILED) {
159 fprintf(stderr, "%s: Can't read \"%s\": %s\n",
160 params.cmdname, params.imagefile,
166 * Both calls bellow scan through dumpimage registry for all
167 * supported image types and verify the input image file
172 * Extract the data files from within the matched
173 * image type. Returns the error code if not matched
175 retval = dumpimage_extract_datafile(ptr, &sbuf);
178 * Print the image information for matched image type
179 * Returns the error code if not matched
181 retval = imagetool_verify_print_header(ptr, &sbuf,
185 (void)munmap((void *)ptr, sbuf.st_size);
196 static void usage(void)
198 fprintf(stderr, "Usage: %s -l image\n"
199 " -l ==> list image header information\n",
202 " %s -i image [-p position] [-o outfile] data_file\n"
203 " -i ==> extract from the 'image' a specific 'data_file'"
204 ", indexed by 'position' (starting at 0)\n",
207 " %s -V ==> print version information and exit\n",