[PATCH 6/6] fastboot: check for file existence before flashing/erasing

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Apr 28 05:52:18 PDT 2025


We used to have an init time check for whether exported files exist or
not, but this went away and was replaced by a check in getvar.

If a client omits getvar and directly calls flash for a non-existent
file, barebox will create it automatically, thereby turning every
partition as if it had the create flag set.

Add an explicit check for whether a file exists into flash/erase
and proceed only if it does or the create flag is set.

The optional flag needs not to be checked here as it's only relevant
when enumerating available partitions.

Fixes: 6a191155be4e ("fastboot: evaluate fastboot partitions when used")
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/fastboot.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/common/fastboot.c b/common/fastboot.c
index e51a6b2027ec..56bc4e82c4fe 100644
--- a/common/fastboot.c
+++ b/common/fastboot.c
@@ -94,6 +94,20 @@ static loff_t fb_file_getsize(struct file_list_entry *fentry)
 	return ret ? ret : s.st_size;
 }
 
+static int fb_file_available(struct file_list_entry *fentry)
+{
+	loff_t size;
+
+	size = fb_file_getsize(fentry);
+	if (size >= 0)
+		return 1;
+
+	if (fentry->flags & FILE_LIST_FLAG_CREATE)
+		return 0;
+
+	return size;
+}
+
 static int fastboot_add_partition_variables(struct fastboot *fb, struct list_head *list,
 		struct file_list_entry *fentry)
 {
@@ -682,13 +696,24 @@ static void cb_flash(struct fastboot *fb, const char *cmd)
 		goto out;
 	}
 
-	/* Check if board-code registered a vendor-specific handler */
+	/* Check if board-code registered a vendor-specific handler
+	 * We intentionally do this before the fb_file_available check
+	 * to afford board core more flexibility.
+	 */
 	if (fb->cmd_flash) {
 		ret = fb->cmd_flash(fb, fentry, fb->tempname, fb->download_size);
 		if (ret != FASTBOOT_CMD_FALLTHROUGH)
 			goto out;
 	}
 
+	ret = fb_file_available(fentry);
+	if (ret < 0) {
+		fastboot_tx_print(fb, FASTBOOT_MSG_FAIL,
+				  "file %s doesn't exist", fentry->filename);
+		ret = -ENOENT;
+		goto out;
+	}
+
 	filename = fentry->filename;
 
 	if (filetype == filetype_android_sparse) {
@@ -801,6 +826,13 @@ static void cb_erase(struct fastboot *fb, const char *cmd)
 		return;
 	}
 
+	ret = fb_file_available(fentry);
+	if (ret < 0) {
+		fastboot_tx_print(fb, FASTBOOT_MSG_FAIL,
+				  "file %s doesn't exist", fentry->filename);
+		return;
+	}
+
 	fd = open(filename, O_RDWR);
 	if (fd < 0)
 		fastboot_tx_print(fb, FASTBOOT_MSG_FAIL, strerror(-fd));
-- 
2.39.5




More information about the barebox mailing list