mkimage: Split out file-opening into its own function

Add a new open_image() function to handle the initial opening of the
output file.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2025-07-31 13:41:21 +12:00
parent f684570439
commit 359baec777

View File

@@ -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;