
Add a new open_image() function to handle the initial opening of the output file. Signed-off-by: Simon Glass <sjg@chromium.org> --- tools/mkimage.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/tools/mkimage.c b/tools/mkimage.c index 5edc2123b40..d1dcc5a6930 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -641,6 +641,33 @@ static int process_fit(struct imgtool *itl, struct imgtool_funcs *tfuncs) return 0; } +/** + * open_image() - Open the image file to create/update + * + * @itl: Image-tool info + * + * Return: file handle if OK, or -ve on error + */ +static int open_image(struct imgtool *itl) +{ + int ifd; + + if (itl->lflag || itl->fflag) { + ifd = open(itl->imagefile, O_RDONLY | O_BINARY); + } else { + ifd = open(itl->imagefile, + O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); + } + + if (ifd < 0) { + fprintf(stderr, "%s: Can't open %s: %s\n", + itl->cmdname, itl->imagefile, strerror(errno)); + return -ENOENT; + } + + return ifd; +} + /** * run_mkimage() - Run the mkimage tool * @@ -665,19 +692,9 @@ static int run_mkimage(struct imgtool *itl) if (itl->fflag && process_fit(itl, tparams)) return EXIT_FAILURE; - if (itl->lflag || itl->fflag) { - ifd = open(itl->imagefile, O_RDONLY | O_BINARY); - } else { - ifd = open(itl->imagefile, - O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0666); - } - - if (ifd < 0) { - fprintf (stderr, "%s: Can't open %s: %s\n", - itl->cmdname, itl->imagefile, - strerror(errno)); + ifd = open_image(itl); + if (ifd < 0) return EXIT_FAILURE; - } if (itl->lflag || itl->fflag) { uint64_t size; -- 2.43.0