1 // SPDX-License-Identifier: GPL-2.0+
5 * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
12 static void usage(void);
14 /* parameters initialized by core will be used by the image type code */
15 static struct image_tool_params params;
18 * dumpimage_extract_subimage -
20 * It scans all registered image types,
21 * verifies image_header for each supported image type
22 * if verification is successful, it extracts the desired file,
23 * indexed by pflag, from the image
25 * returns negative if input image format does not match with any of
26 * supported image types
28 static int dumpimage_extract_subimage(struct image_type_params *tparams,
29 void *ptr, struct stat *sbuf)
33 if (tparams->verify_header) {
34 retval = tparams->verify_header((unsigned char *)ptr,
35 sbuf->st_size, ¶ms);
37 fprintf(stderr, "%s: failed to verify header of %s\n",
38 params.cmdname, tparams->name);
43 * Extract the file from the image
44 * if verify is successful
46 if (tparams->extract_subimage) {
47 retval = tparams->extract_subimage(ptr, ¶ms);
49 fprintf(stderr, "%s: extract_subimage failed for %s\n",
50 params.cmdname, tparams->name);
55 "%s: extract_subimage undefined for %s\n",
56 params.cmdname, tparams->name);
64 int main(int argc, char **argv)
70 int retval = EXIT_SUCCESS;
71 struct image_type_params *tparams = NULL;
73 params.cmdname = *argv;
75 while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
81 params.outfile = optarg;
85 params.type = genimg_get_type_id(optarg);
86 if (params.type < 0) {
87 fprintf(stderr, "%s: Invalid type\n",
93 params.pflag = strtoul(optarg, &ptr, 10);
96 "%s: invalid file position %s\n",
97 params.cmdname, *argv);
102 printf("dumpimage version %s\n", PLAIN_VERSION);
111 if (argc < 2 || (params.iflag && params.lflag))
114 if (optind >= argc) {
115 fprintf(stderr, "%s: image file missing\n", params.cmdname);
119 params.imagefile = argv[optind];
121 /* set tparams as per input type_id */
122 tparams = imagetool_get_type(params.type);
123 if (!params.lflag && tparams == NULL) {
124 fprintf(stderr, "%s: unsupported type: %s\n",
125 params.cmdname, genimg_get_type_name(params.type));
130 * check the passed arguments parameters meets the requirements
131 * as per image type to be generated/listed
133 if (tparams && tparams->check_params) {
134 if (tparams->check_params(¶ms)) {
135 fprintf(stderr, "%s: Parameter check failed\n",
141 if (!params.lflag && !params.outfile) {
142 fprintf(stderr, "%s: No output file provided\n",
147 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
149 fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
150 params.imagefile, strerror(errno));
154 if (fstat(ifd, &sbuf) < 0) {
155 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
156 params.imagefile, strerror(errno));
160 if (tparams && (uint32_t)sbuf.st_size < tparams->header_size) {
161 fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
162 params.cmdname, params.imagefile);
166 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
167 if (ptr == MAP_FAILED) {
168 fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
169 params.imagefile, strerror(errno));
174 * Both calls bellow scan through dumpimage registry for all
175 * supported image types and verify the input image file
180 * Extract the data files from within the matched
181 * image type. Returns the error code if not matched
183 retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
185 fprintf(stderr, "%s: Can't extract subimage from %s\n",
186 params.cmdname, params.imagefile);
189 * Print the image information for matched image type
190 * Returns the error code if not matched
192 retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
196 (void)munmap((void *)ptr, sbuf.st_size);
202 static void usage(void)
204 fprintf(stderr, "Usage: %s [-T type] -l image\n"
205 " -l ==> list image header information\n"
206 " -T ==> parse image file as 'type'\n",
209 " %s [-T type] [-p position] [-o outfile] image\n"
210 " -T ==> declare image type as 'type'\n"
211 " -p ==> 'position' (starting at 0) of the component to extract from image\n"
212 " -o ==> extract component to file 'outfile'\n",
215 " %s -h ==> print usage information and exit\n",
218 " %s -V ==> print version information and exit\n",