[U-Boot] [PATCH 3/5] tools: dumpimage: Simplify internal logic

Martyn Welch martyn.welch at collabora.com
Sat Jan 26 02:31:52 UTC 2019


There are 3 supported modes of operation:

1) Show version
2) List image contents
3) Extract image component

Option (1) terminates early, so only options (2) and (3) remain. Remove
redundant check for these modes.

Signed-off-by: Martyn Welch <martyn.welch at collabora.com>
---

 tools/dumpimage.c | 85 +++++++++++++++++++++--------------------------
 1 file changed, 37 insertions(+), 48 deletions(-)

diff --git a/tools/dumpimage.c b/tools/dumpimage.c
index 1b92dec364..e17e979b04 100644
--- a/tools/dumpimage.c
+++ b/tools/dumpimage.c
@@ -60,7 +60,7 @@ 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;
@@ -135,65 +135,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)
-- 
2.20.1



More information about the U-Boot mailing list