All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 09/14] Improve error handling in fit_common
Date: Mon,  2 Jun 2014 22:04:52 -0600	[thread overview]
Message-ID: <1401768297-7198-10-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1401768297-7198-1-git-send-email-sjg@chromium.org>

Make the error handling common, and make sure the file is always closed
on error. Rename the parameter to be more description and add comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v3:
- Add new patch to improve error handling in fit_common

Changes in v2: None

 tools/fit_check_sign.c |  4 ++--
 tools/fit_common.c     | 28 ++++++++++++++--------------
 tools/fit_common.h     | 14 ++++++++++++--
 tools/fit_info.c       |  2 +-
 4 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/tools/fit_check_sign.c b/tools/fit_check_sign.c
index d6d9340..e6df610 100644
--- a/tools/fit_check_sign.c
+++ b/tools/fit_check_sign.c
@@ -61,10 +61,10 @@ int main(int argc, char **argv)
 			break;
 	}
 
-	ffd = mmap_fdt(cmdname, fdtfile, &fit_blob, &fsbuf, 0);
+	ffd = mmap_fdt(cmdname, fdtfile, &fit_blob, &fsbuf, false);
 	if (ffd < 0)
 		return EXIT_FAILURE;
-	kfd = mmap_fdt(cmdname, keyfile, &key_blob, &ksbuf, 0);
+	kfd = mmap_fdt(cmdname, keyfile, &key_blob, &ksbuf, false);
 	if (ffd < 0)
 		return EXIT_FAILURE;
 
diff --git a/tools/fit_common.c b/tools/fit_common.c
index ee1767b..286f357 100644
--- a/tools/fit_common.c
+++ b/tools/fit_common.c
@@ -38,8 +38,8 @@ int fit_check_image_types(uint8_t type)
 		return EXIT_FAILURE;
 }
 
-int mmap_fdt(char *cmdname, const char *fname, void **blobp,
-		struct stat *sbuf, int useunlink)
+int mmap_fdt(const char *cmdname, const char *fname, void **blobp,
+	     struct stat *sbuf, bool delete_on_error)
 {
 	void *ptr;
 	int fd;
@@ -50,17 +50,13 @@ int mmap_fdt(char *cmdname, const char *fname, void **blobp,
 	if (fd < 0) {
 		fprintf(stderr, "%s: Can't open %s: %s\n",
 			cmdname, fname, strerror(errno));
-		if (useunlink)
-			unlink(fname);
-		return -1;
+		goto err;
 	}
 
 	if (fstat(fd, sbuf) < 0) {
 		fprintf(stderr, "%s: Can't stat %s: %s\n",
 			cmdname, fname, strerror(errno));
-		if (useunlink)
-			unlink(fname);
-		return -1;
+		goto err;
 	}
 
 	errno = 0;
@@ -68,19 +64,23 @@ int mmap_fdt(char *cmdname, const char *fname, void **blobp,
 	if ((ptr == MAP_FAILED) || (errno != 0)) {
 		fprintf(stderr, "%s: Can't read %s: %s\n",
 			cmdname, fname, strerror(errno));
-		if (useunlink)
-			unlink(fname);
-		return -1;
+		goto err;
 	}
 
 	/* check if ptr has a valid blob */
 	if (fdt_check_header(ptr)) {
 		fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
-		if (useunlink)
-			unlink(fname);
-		return -1;
+		goto err;
 	}
 
 	*blobp = ptr;
 	return fd;
+
+err:
+	if (fd >= 0)
+		close(fd);
+	if (delete_on_error)
+		unlink(fname);
+
+	return -1;
 }
diff --git a/tools/fit_common.h b/tools/fit_common.h
index adf4404..adcee6d 100644
--- a/tools/fit_common.h
+++ b/tools/fit_common.h
@@ -16,7 +16,17 @@ int fit_verify_header(unsigned char *ptr, int image_size,
 
 int fit_check_image_types(uint8_t type);
 
-int mmap_fdt(char *cmdname, const char *fname, void **blobp,
-		struct stat *sbuf, int useunlink);
+/**
+ * Map an FDT into memory, optionally increasing its size
+ *
+ * @cmdname:	Tool name (for displaying with error messages)
+ * @fname:	Filename containing FDT
+ * @blobp:	Returns pointer to FDT blob
+ * @sbuf:	File status information is stored here
+ * @delete_on_error:	true to delete the file if we get an error
+ * @return 0 if OK, -1 on error.
+ */
+int mmap_fdt(const char *cmdname, const char *fname, void **blobp,
+	     struct stat *sbuf, bool delete_on_error);
 
 #endif /* _FIT_COMMON_H_ */
diff --git a/tools/fit_info.c b/tools/fit_info.c
index 50f3c8e..9442ff1 100644
--- a/tools/fit_info.c
+++ b/tools/fit_info.c
@@ -68,7 +68,7 @@ int main(int argc, char **argv)
 			break;
 		}
 
-	ffd = mmap_fdt(cmdname, fdtfile, &fit_blob, &fsbuf, 0);
+	ffd = mmap_fdt(cmdname, fdtfile, &fit_blob, &fsbuf, false);
 
 	if (ffd < 0) {
 		printf("Could not open %s\n", fdtfile);
-- 
1.9.1.423.g4596e3a

  parent reply	other threads:[~2014-06-03  4:04 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-03  4:04 [U-Boot] [PATCH v3 0/14] Minor improvements to secure boot and enable on beaglebone Simon Glass
2014-06-03  4:04 ` [U-Boot] [PATCH v3 01/14] ti: am335x: Fix the U-Boot binary output Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 02/14] cm_t335: " Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 03/14] mx31ads: " Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 04/14] Check that u-boot.bin size looks correct Simon Glass
2014-06-03  4:04 ` [U-Boot] [PATCH v3 05/14] am33xx/omap: Allow cache enable for all Sitara/OMAP Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 06/14] hash: Export the function to show a hash Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 07/14] fdt: Add DEV_TREE_BIN option to specify a device tree binary file Simon Glass
2014-06-10  5:59   ` Masahiro Yamada
2014-06-11 22:18     ` Tom Rini
2014-06-12  4:44       ` Simon Glass
2014-06-11 22:25     ` Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 08/14] fdt: Update functions which write to an FDT to return -ENOSPC Simon Glass
2014-06-11 22:17   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` Simon Glass [this message]
2014-06-11 22:18   ` [U-Boot] [U-Boot, v3, 09/14] Improve error handling in fit_common Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 10/14] mkimage: Automatically make space in FDT when full Simon Glass
2014-06-11 22:18   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 11/14] arm: ti: Increase malloc size to 16MB for armv7 boards Simon Glass
2014-06-11 22:18   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 12/14] am33xx/omap: Enable CONFIG_OF_CONTROL Simon Glass
2014-06-11 22:18   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 13/14] am33xx/omap: Enable FIT support Simon Glass
2014-06-11 22:18   ` [U-Boot] [U-Boot,v3,13/14] " Tom Rini
2014-06-03  4:04 ` [U-Boot] [PATCH v3 14/14] am33xx/omap: Add a new board to enable verified boot Simon Glass
2014-06-11 22:18   ` [U-Boot] [U-Boot, v3, " Tom Rini
2014-06-11 22:18 ` [U-Boot] [PATCH v3 0/14] Minor improvements to secure boot and enable on beaglebone Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1401768297-7198-10-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.