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 = {
16 .type = IH_TYPE_KERNEL,
20 * dumpimage_extract_subimage -
22 * It scans all registered image types,
23 * verifies image_header for each supported image type
24 * if verification is successful, it extracts the desired file,
25 * indexed by pflag, from the image
27 * returns negative if input image format does not match with any of
28 * supported image types
30 static int dumpimage_extract_subimage(struct image_type_params *tparams,
31 void *ptr, struct stat *sbuf)
35 if (tparams->verify_header) {
36 retval = tparams->verify_header((unsigned char *)ptr,
37 sbuf->st_size, ¶ms);
41 * Extract the file from the image
42 * if verify is successful
44 if (tparams->extract_subimage) {
45 retval = tparams->extract_subimage(ptr, ¶ms);
48 "%s: extract_subimage undefined for %s\n",
49 params.cmdname, tparams->name);
57 int main(int argc, char **argv)
63 int retval = EXIT_SUCCESS;
64 struct image_type_params *tparams = NULL;
66 params.cmdname = *argv;
68 while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
74 params.outfile = optarg;
78 params.type = genimg_get_type_id(optarg);
79 if (params.type < 0) {
80 fprintf(stderr, "%s: Invalid type\n",
86 params.pflag = strtoul(optarg, &ptr, 10);
89 "%s: invalid file position %s\n",
90 params.cmdname, *argv);
95 printf("dumpimage version %s\n", PLAIN_VERSION);
108 if (optind >= argc) {
109 fprintf(stderr, "%s: image file missing\n", params.cmdname);
113 params.imagefile = argv[optind];
115 /* set tparams as per input type_id */
116 tparams = imagetool_get_type(params.type);
117 if (tparams == NULL) {
118 fprintf(stderr, "%s: unsupported type: %s\n",
119 params.cmdname, genimg_get_type_name(params.type));
124 * check the passed arguments parameters meets the requirements
125 * as per image type to be generated/listed
127 if (tparams->check_params) {
128 if (tparams->check_params(¶ms)) {
129 fprintf(stderr, "%s: Parameter check failed\n",
135 if (!params.lflag && !params.outfile) {
136 fprintf(stderr, "%s: No output file provided\n",
141 ifd = open(params.imagefile, O_RDONLY|O_BINARY);
143 fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
144 params.imagefile, strerror(errno));
148 if (fstat(ifd, &sbuf) < 0) {
149 fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
150 params.imagefile, strerror(errno));
154 if ((uint32_t)sbuf.st_size < tparams->header_size) {
155 fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
156 params.cmdname, params.imagefile);
160 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
161 if (ptr == MAP_FAILED) {
162 fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
163 params.imagefile, strerror(errno));
168 * Both calls bellow scan through dumpimage registry for all
169 * supported image types and verify the input image file
174 * Extract the data files from within the matched
175 * image type. Returns the error code if not matched
177 retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
180 * Print the image information for matched image type
181 * Returns the error code if not matched
183 retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
187 (void)munmap((void *)ptr, sbuf.st_size);
193 static void usage(void)
195 fprintf(stderr, "Usage: %s -l image\n"
196 " -l ==> list image header information\n",
199 " %s [-T type] [-p position] [-o outfile] image\n"
200 " -T ==> declare image type as 'type'\n"
201 " -p ==> 'position' (starting at 0) of the component to extract from image\n"
202 " -o ==> extract component to file 'outfile'\n",
205 " %s -h ==> print usage information and exit\n",
208 " %s -V ==> print version information and exit\n",