4 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 static void usage(void);
15 /* parameters initialized by core will be used by the image type code */
16 static struct image_tool_params params = {
17 .type = IH_TYPE_KERNEL,
21 * dumpimage_extract_subimage -
23 * It scans all registered image types,
24 * verifies image_header for each supported image type
25 * if verification is successful, it extracts the desired file,
26 * indexed by pflag, from the image
28 * returns negative if input image format does not match with any of
29 * supported image types
31 static int dumpimage_extract_subimage(struct image_type_params *tparams,
32 void *ptr, struct stat *sbuf)
36 if (tparams->verify_header) {
37 retval = tparams->verify_header((unsigned char *)ptr,
38 sbuf->st_size, ¶ms);
42 * Extract the file from the image
43 * if verify is successful
45 if (tparams->extract_subimage) {
46 retval = tparams->extract_subimage(ptr, ¶ms);
49 "%s: extract_subimage undefined for %s\n",
50 params.cmdname, tparams->name);
58 int main(int argc, char **argv)
65 struct image_type_params *tparams = NULL;
67 params.cmdname = *argv;
69 while ((opt = getopt(argc, argv, "li:o:T:p:V")) != -1) {
75 params.imagefile = optarg;
79 params.outfile = optarg;
82 params.type = genimg_get_type_id(optarg);
83 if (params.type < 0) {
88 params.pflag = strtoul(optarg, &ptr, 10);
91 "%s: invalid file position %s\n",
92 params.cmdname, *argv);
97 printf("dumpimage version %s\n", PLAIN_VERSION);
108 /* set tparams as per input type_id */
109 tparams = imagetool_get_type(params.type);
110 if (tparams == NULL) {
111 fprintf(stderr, "%s: unsupported type: %s\n",
112 params.cmdname, genimg_get_type_name(params.type));
117 * check the passed arguments parameters meets the requirements
118 * as per image type to be generated/listed
120 if (tparams->check_params) {
121 if (tparams->check_params(¶ms))
126 params.datafile = argv[optind];
128 params.imagefile = argv[optind];
130 params.outfile = params.datafile;
132 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
134 fprintf(stderr, "%s: Can't open \"%s\": %s\n",
135 params.cmdname, params.imagefile,
140 if (params.lflag || params.iflag) {
141 if (fstat(ifd, &sbuf) < 0) {
142 fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
143 params.cmdname, params.imagefile,
148 if ((uint32_t)sbuf.st_size < tparams->header_size) {
150 "%s: Bad size: \"%s\" is not valid image\n",
151 params.cmdname, params.imagefile);
155 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
156 if (ptr == MAP_FAILED) {
157 fprintf(stderr, "%s: Can't read \"%s\": %s\n",
158 params.cmdname, params.imagefile,
164 * Both calls bellow scan through dumpimage registry for all
165 * supported image types and verify the input image file
170 * Extract the data files from within the matched
171 * image type. Returns the error code if not matched
173 retval = dumpimage_extract_subimage(tparams, ptr,
177 * Print the image information for matched image type
178 * Returns the error code if not matched
180 retval = imagetool_verify_print_header(ptr, &sbuf,
184 (void)munmap((void *)ptr, sbuf.st_size);
195 static void usage(void)
197 fprintf(stderr, "Usage: %s -l image\n"
198 " -l ==> list image header information\n",
201 " %s -i image -T type [-p position] [-o outfile] data_file\n"
202 " -i ==> extract from the 'image' a specific 'data_file'\n"
203 " -T ==> set image type to 'type'\n"
204 " -p ==> 'position' (starting at 0) of the 'data_file' inside the 'image'\n",
207 " %s -V ==> print version information and exit\n",