diff options
author | Simon Glass <sjg@chromium.org> | 2014-06-02 22:04:52 -0600 |
---|---|---|
committer | Tom Rini <trini@ti.com> | 2014-06-11 16:25:46 -0400 |
commit | ef0af64b1c45b8ee28db12c16c4ee881ee328e44 (patch) | |
tree | ae0d2333556164104f9c12f561221b1d3541ed01 | |
parent | 4f427a421fcba92b0325907fe79464c9791e85d5 (diff) | |
download | u-boot-imx-ef0af64b1c45b8ee28db12c16c4ee881ee328e44.zip u-boot-imx-ef0af64b1c45b8ee28db12c16c4ee881ee328e44.tar.gz u-boot-imx-ef0af64b1c45b8ee28db12c16c4ee881ee328e44.tar.bz2 |
Improve error handling in fit_common
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>
-rw-r--r-- | tools/fit_check_sign.c | 4 | ||||
-rw-r--r-- | tools/fit_common.c | 28 | ||||
-rw-r--r-- | tools/fit_common.h | 14 | ||||
-rw-r--r-- | 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 817773d..357650d 100644 --- a/tools/fit_check_sign.c +++ b/tools/fit_check_sign.c @@ -62,10 +62,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); |