tools: mkimage: use common ALIGN to do the size align
[oweals/u-boot.git] / tools / dumpimage.c
index 0228e183abd30f0c70161f7cbc48af00f21c3a23..e5481435a7644add00810f7d6a7548762d2cb79a 100644 (file)
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Based on mkimage.c.
  *
  * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include "dumpimage.h"
 
 static void usage(void);
 
-/* image_type_params linked list to maintain registered image types supports */
-static struct image_type_params *dumpimage_tparams;
-
 /* parameters initialized by core will be used by the image type code */
 static struct image_tool_params params = {
        .type = IH_TYPE_KERNEL,
 };
 
-/**
- * dumpimage_register() - register respective image generation/list support
- *
- * the input struct image_type_params is checked and appended to the link
- * list, if the input structure is already registered, issue an error
- *
- * @tparams: Image type parameters
- */
-static void dumpimage_register(struct image_type_params *tparams)
-{
-       struct image_type_params **tp;
-
-       if (!tparams) {
-               fprintf(stderr, "%s: %s: Null input\n", params.cmdname,
-                       __func__);
-               exit(EXIT_FAILURE);
-       }
-
-       /* scan the linked list, check for registry and point the last one */
-       for (tp = &dumpimage_tparams; *tp != NULL; tp = &(*tp)->next) {
-               if (!strcmp((*tp)->name, tparams->name)) {
-                       fprintf(stderr, "%s: %s already registered\n",
-                               params.cmdname, tparams->name);
-                       return;
-               }
-       }
-
-       /* add input struct entry at the end of link list */
-       *tp = tparams;
-       /* mark input entry as last entry in the link list */
-       tparams->next = NULL;
-
-       debug("Registered %s\n", tparams->name);
-}
-
 /*
- * dumpimage_extract_datafile -
+ * dumpimage_extract_subimage -
  *
  * It scans all registered image types,
  * verifies image_header for each supported image type
@@ -66,29 +27,36 @@ static void dumpimage_register(struct image_type_params *tparams)
  * returns negative if input image format does not match with any of
  * supported image types
  */
-static int dumpimage_extract_datafile(void *ptr, struct stat *sbuf)
+static int dumpimage_extract_subimage(struct image_type_params *tparams,
+               void *ptr, struct stat *sbuf)
 {
        int retval = -1;
-       struct image_type_params *curr;
-
-       for (curr = dumpimage_tparams; curr != NULL; curr = curr->next) {
-               if (curr->verify_header) {
-                       retval = curr->verify_header((unsigned char *)ptr,
-                                                    sbuf->st_size, &params);
-                       if (retval != 0)
-                               continue;
-                       /*
-                        * Extract the file from the image
-                        * if verify is successful
-                        */
-                       if (curr->extract_datafile) {
-                               curr->extract_datafile(ptr, &params);
-                       } else {
-                               fprintf(stderr,
-                                       "%s: extract_datafile undefined for %s\n",
-                                       params.cmdname, curr->name);
-                       break;
+
+       if (tparams->verify_header) {
+               retval = tparams->verify_header((unsigned char *)ptr,
+                               sbuf->st_size, &params);
+               if (retval != 0) {
+                       fprintf(stderr, "%s: failed to verify header of %s\n",
+                               params.cmdname, tparams->name);
+                       return -1;
+               }
+
+               /*
+                * Extract the file from the image
+                * if verify is successful
+                */
+               if (tparams->extract_subimage) {
+                       retval = tparams->extract_subimage(ptr, &params);
+                       if (retval != 0) {
+                               fprintf(stderr, "%s: extract_subimage failed for %s\n",
+                                       params.cmdname, tparams->name);
+                               return -3;
                        }
+               } else {
+                       fprintf(stderr,
+                               "%s: extract_subimage undefined for %s\n",
+                               params.cmdname, tparams->name);
+                       return -2;
                }
        }
 
@@ -101,25 +69,27 @@ int main(int argc, char **argv)
        int ifd = -1;
        struct stat sbuf;
        char *ptr;
-       int retval = 0;
+       int retval = EXIT_SUCCESS;
        struct image_type_params *tparams = NULL;
 
-       /* Init all image generation/list support */
-       register_image_tool(dumpimage_register);
-
        params.cmdname = *argv;
 
-       while ((opt = getopt(argc, argv, "li:o:p:V")) != -1) {
+       while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
                switch (opt) {
                case 'l':
                        params.lflag = 1;
                        break;
-               case 'i':
-                       params.imagefile = optarg;
-                       params.iflag = 1;
-                       break;
                case 'o':
                        params.outfile = optarg;
+                       params.iflag = 1;
+                       break;
+               case 'T':
+                       params.type = genimg_get_type_id(optarg);
+                       if (params.type < 0) {
+                               fprintf(stderr, "%s: Invalid type\n",
+                                       params.cmdname);
+                               exit(EXIT_FAILURE);
+                       }
                        break;
                case 'p':
                        params.pflag = strtoul(optarg, &ptr, 10);
@@ -133,18 +103,27 @@ int main(int argc, char **argv)
                case 'V':
                        printf("dumpimage version %s\n", PLAIN_VERSION);
                        exit(EXIT_SUCCESS);
+               case 'h':
                default:
                        usage();
+                       break;
                }
        }
 
-       if (optind >= argc)
+       if (argc < 2)
                usage();
 
+       if (optind >= argc) {
+               fprintf(stderr, "%s: image file missing\n", params.cmdname);
+               exit(EXIT_FAILURE);
+       }
+
+       params.imagefile = argv[optind];
+
        /* set tparams as per input type_id */
-       tparams = imagetool_get_type(params.type, dumpimage_tparams);
+       tparams = imagetool_get_type(params.type);
        if (tparams == NULL) {
-               fprintf(stderr, "%s: unsupported type %s\n",
+               fprintf(stderr, "%s: unsupported type: %s\n",
                        params.cmdname, genimg_get_type_name(params.type));
                exit(EXIT_FAILURE);
        }
@@ -154,77 +133,72 @@ int main(int argc, char **argv)
         * as per image type to be generated/listed
         */
        if (tparams->check_params) {
-               if (tparams->check_params(&params))
-                       usage();
+               if (tparams->check_params(&params)) {
+                       fprintf(stderr, "%s: Parameter check failed\n",
+                               params.cmdname);
+                       exit(EXIT_FAILURE);
+               }
        }
 
-       if (params.iflag)
-               params.datafile = argv[optind];
-       else
-               params.imagefile = argv[optind];
-       if (!params.outfile)
-               params.outfile = params.datafile;
+       if (!params.lflag && !params.outfile) {
+               fprintf(stderr, "%s: No output file provided\n",
+                       params.cmdname);
+               exit(EXIT_FAILURE);
+       }
 
        ifd = open(params.imagefile, O_RDONLY|O_BINARY);
        if (ifd < 0) {
-               fprintf(stderr, "%s: Can't open \"%s\": %s\n",
-                       params.cmdname, params.imagefile,
-                       strerror(errno));
+               fprintf(stderr, "%s: Can't open \"%s\": %s\n", params.cmdname,
+                       params.imagefile, strerror(errno));
                exit(EXIT_FAILURE);
        }
 
-       if (params.lflag || params.iflag) {
-               if (fstat(ifd, &sbuf) < 0) {
-                       fprintf(stderr, "%s: Can't stat \"%s\": %s\n",
-                               params.cmdname, params.imagefile,
-                               strerror(errno));
-                       exit(EXIT_FAILURE);
-               }
+       if (fstat(ifd, &sbuf) < 0) {
+               fprintf(stderr, "%s: Can't stat \"%s\": %s\n", params.cmdname,
+                       params.imagefile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
-               if ((unsigned)sbuf.st_size < tparams->header_size) {
-                       fprintf(stderr,
-                               "%s: Bad size: \"%s\" is not valid image\n",
-                               params.cmdname, params.imagefile);
-                       exit(EXIT_FAILURE);
-               }
+       if ((uint32_t)sbuf.st_size < tparams->header_size) {
+               fprintf(stderr, "%s: Bad size: \"%s\" is not valid image\n",
+                       params.cmdname, params.imagefile);
+               exit(EXIT_FAILURE);
+       }
 
-               ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
-               if (ptr == MAP_FAILED) {
-                       fprintf(stderr, "%s: Can't read \"%s\": %s\n",
-                               params.cmdname, params.imagefile,
-                               strerror(errno));
-                       exit(EXIT_FAILURE);
-               }
+       ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
+       if (ptr == MAP_FAILED) {
+               fprintf(stderr, "%s: Can't read \"%s\": %s\n", params.cmdname,
+                       params.imagefile, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
 
+       /*
+        * Both calls bellow scan through dumpimage registry for all
+        * supported image types and verify the input image file
+        * header for match
+        */
+       if (params.iflag) {
                /*
-                * Both calls bellow scan through dumpimage registry for all
-                * supported image types and verify the input image file
-                * header for match
+                * Extract the data files from within the matched
+                * image type. Returns the error code if not matched
                 */
-               if (params.iflag) {
-                       /*
-                        * Extract the data files from within the matched
-                        * image type. Returns the error code if not matched
-                        */
-                       retval = dumpimage_extract_datafile(ptr, &sbuf);
-               } else {
-                       /*
-                        * Print the image information for matched image type
-                        * Returns the error code if not matched
-                        */
-                       retval = imagetool_verify_print_header(ptr, &sbuf,
-                                       tparams, &params);
-               }
-
-               (void)munmap((void *)ptr, sbuf.st_size);
-               (void)close(ifd);
-
-               return retval;
+               retval = dumpimage_extract_subimage(tparams, ptr, &sbuf);
+               if (retval)
+                       fprintf(stderr, "%s: Can't extract subimage from %s\n",
+                               params.cmdname, params.imagefile);
+       } else {
+               /*
+                * Print the image information for matched image type
+                * Returns the error code if not matched
+                */
+               retval = imagetool_verify_print_header(ptr, &sbuf, tparams,
+                                                      &params);
        }
 
+       (void)munmap((void *)ptr, sbuf.st_size);
        (void)close(ifd);
 
-       return EXIT_SUCCESS;
+       return retval;
 }
 
 static void usage(void)
@@ -233,13 +207,17 @@ static void usage(void)
                "          -l ==> list image header information\n",
                params.cmdname);
        fprintf(stderr,
-               "       %s -i image [-p position] [-o outfile] data_file\n"
-               "          -i ==> extract from the 'image' a specific 'data_file'"
-               ", indexed by 'position' (starting at 0)\n",
+               "       %s [-T type] [-p position] [-o outfile] image\n"
+               "          -T ==> declare image type as 'type'\n"
+               "          -p ==> 'position' (starting at 0) of the component to extract from image\n"
+               "          -o ==> extract component to file 'outfile'\n",
+               params.cmdname);
+       fprintf(stderr,
+               "       %s -h ==> print usage information and exit\n",
                params.cmdname);
        fprintf(stderr,
                "       %s -V ==> print version information and exit\n",
                params.cmdname);
 
-       exit(EXIT_FAILURE);
+       exit(EXIT_SUCCESS);
 }