Merge tag 'efi-2020-01-rc2' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / tools / dumpimage.c
index 1b92dec364eb5770f8f08884bf0e8f633967b9c2..ee3d41dda4d4b2b0ed46f2b430c5d01ab95812d8 100644 (file)
@@ -60,12 +60,12 @@ 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;
 
        params.cmdname = *argv;
 
-       while ((opt = getopt(argc, argv, "lo:T:p:V")) != -1) {
+       while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
                switch (opt) {
                case 'l':
                        params.lflag = 1;
@@ -79,7 +79,7 @@ int main(int argc, char **argv)
                        if (params.type < 0) {
                                fprintf(stderr, "%s: Invalid type\n",
                                        params.cmdname);
-                               usage();
+                               exit(EXIT_FAILURE);
                        }
                        break;
                case 'p':
@@ -94,15 +94,20 @@ int main(int argc, char **argv)
                case 'V':
                        printf("dumpimage version %s\n", PLAIN_VERSION);
                        exit(EXIT_SUCCESS);
+               case 'h':
+                       usage();
                default:
                        usage();
                        break;
                }
        }
 
+       if (argc < 2)
+               usage();
+
        if (optind >= argc) {
                fprintf(stderr, "%s: image file missing\n", params.cmdname);
-               usage();
+               exit(EXIT_FAILURE);
        }
 
        params.imagefile = argv[optind];
@@ -123,7 +128,7 @@ int main(int argc, char **argv)
                if (tparams->check_params(&params)) {
                        fprintf(stderr, "%s: Parameter check failed\n",
                                params.cmdname);
-                       usage();
+                       exit(EXIT_FAILURE);
                }
        }
 
@@ -135,65 +140,54 @@ int main(int argc, char **argv)
 
        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 ((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);
-               }
+       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_subimage(tparams, 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);
+       } 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)
@@ -202,13 +196,17 @@ static void usage(void)
                "          -l ==> list image header information\n",
                params.cmdname);
        fprintf(stderr,
-               "       %s -T type [-p position] [-o outfile] image\n"
-               "          -T ==> set image type to 'type'\n"
-               "          -p ==> 'position' (starting at 0) of the component to extract from image\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);
 }