Skip to contentSkip to conversation

This series adds support for file renaming to EFI_FILE_PROTOCOL.SetInfo().

PATCH In review Gabriel Dalimonte 3 participants 13 messages Feb 18, 2025 – Mar 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover This series adds support for file renaming to EFI_FILE_PROTOCOL.SetInfo().

This series adds support for file renaming to EFI_FILE_PROTOCOL.SetInfo(). One of the use cases for renaming in EFI is to facilitate boot loader boot counting.

No existing filesystems in U-Boot currently include file renaming, resulting in support for renaming at the filesystem level and a concrete implementation for the FAT filesystem.

Changes in v2:
- update create_link() docstring
- remove ATTR_ARCH being implicitly set in create_link()
- cleanup log message and verbosity in fs_rename()
- update fs_close() docstring with reference to fs_rename()
- clarify fs_rename() behaviour in docstring
- change fatrename to mv (updating semantics to match mv)
- add mv command docs
- add FAT_RENAME Kconfig symbol
- update rename tests for mv command semantics
- update rename test docs on whether mv should succeed or fail
- remove all (fat) prefixes from commands used in rename tests
- clarify docs in check_path_prefix()
- stack allocate fat_itr in check_path_prefix()
- log on observed filesystem corruption in check_path_prefix()
- change 'break's to 'goto' in check_path_prefix()
- normalize added logging to use log_debug()
- correct error code returned when destination directory is not empty
- remove setting ATTR_ARCH on parent dirs during renames
- skip updating timestamps if no RTC present
- make fat_itr stack allocated in update_parent_dir_props
- update parent dir props in create_link and delete_dentry_link
- simplify freeing of new_file_name and new_path

Gabriel Dalimonte (6):
fs: fat: factor out dentry link create/delete
fs: add rename infrastructure
fs: fat: add rename
fs: fat: update parent dirs metadata on dentry create/delete
efi_loader: move path out of file_handle
efi_loader: support file rename in SetInfo()

 cmd/fs.c                                |  14 +
 doc/usage/cmd/mv.rst                    |  61 +++
 fs/fat/Kconfig                          |   7 +
 fs/fat/fat_write.c                      | 476 +++++++++++++++++++++---
 fs/fs.c                                 |  95 +++++
 include/fat.h                           |   1 +
 include/fs.h                            |  16 +-
 lib/efi_loader/Kconfig                  |   1 +
 lib/efi_loader/efi_file.c               |  58 ++-
 test/py/tests/test_fs/conftest.py       | 121 ++++++
 test/py/tests/test_fs/fstest_helpers.py |   2 +
 test/py/tests/test_fs/test_rename.py    | 372 ++++++++++++++++++
 12 files changed, 1157 insertions(+), 67 deletions(-)
 create mode 100644 doc/usage/cmd/mv.rst
 create mode 100644 test/py/tests/test_fs/test_rename.py
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/6 fs: fat: factor out dentry link create/delete

The create_link() code was previously duplicated in two existing functions. The two functions will be used in a future commit to achieve renaming.

---

Changes in v2:
- update create_link() docstring
- remove ATTR_ARCH being implicitly set in create_link()

---
 fs/fat/fat_write.c | 121 ++++++++++++++++++++++++---------------------
 1 file changed, 65 insertions(+), 56 deletions(-)
fs/fat/fat_write.c +65 -56
index ea877ee9171..86366d03853 100644
@@ -1215,6 +1215,43 @@ static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
 	memcpy(&dentptr->nameext, shortname, SHORT_NAME_SIZE);
 }
 
+/**
+ * create_link() - inserts a directory entry for a file or directory
+ *
+ * @itr:	directory iterator
+ * @basename:	file name
+ * @clust:	cluster number the new directory entry should point to. Use 0
+ * if no cluster is assigned yet
+ * @size:	file size
+ * @attr:	file attributes
+ * Return:	0 for success
+ */
+static int create_link(fat_itr *itr, char *basename, __u32 clust, __u32 size,
+		       __u8 attr)
+{
+	char shortname[SHORT_NAME_SIZE];
+	int ndent;
+	int ret;
+
+	/* Check if long name is needed */
+	ndent = set_name(itr, basename, shortname);
+	if (ndent < 0)
+		return ndent;
+	ret = fat_find_empty_dentries(itr, ndent);
+	if (ret)
+		return ret;
+	if (ndent > 1) {
+		/* Set long name entries */
+		ret = fill_dir_slot(itr, basename, shortname);
+		if (ret)
+			return ret;
+	}
+
+	fill_dentry(itr->fsdata, itr->dent, shortname, clust, size, attr);
+
+	return 0;
+}
+
 /**
  * find_directory_entry() - find a directory entry by filename
  *
@@ -1420,35 +1457,15 @@ int file_fat_write_at(const char *filename, loff_t pos, void *buffer,
 		/* Update change date */
 		dentry_set_time(retdent);
 	} else {
-		/* Create a new file */
-		char shortname[SHORT_NAME_SIZE];
-		int ndent;
-
 		if (pos) {
 			/* No hole allowed */
 			ret = -EINVAL;
 			goto exit;
 		}
 
-		/* Check if long name is needed */
-		ndent = set_name(itr, basename, shortname);
-		if (ndent < 0) {
-			ret = ndent;
-			goto exit;
-		}
-		ret = fat_find_empty_dentries(itr, ndent);
+		ret = create_link(itr, basename, 0, size, ATTR_ARCH);
 		if (ret)
 			goto exit;
-		if (ndent > 1) {
-			/* Set long name entries */
-			ret = fill_dir_slot(itr, basename, shortname);
-			if (ret)
-				goto exit;
-		}
-
-		/* Set short name entry */
-		fill_dentry(itr->fsdata, itr->dent, shortname, 0, size,
-			    ATTR_ARCH);
 
 		retdent = itr->dent;
 	}
@@ -1564,6 +1581,31 @@ static int delete_long_name(fat_itr *itr)
 	return 0;
 }
 
+/**
+ * delete_dentry_link() - deletes a directory entry, but not the cluster chain
+ * it points to
+ *
+ * @itr:	the first directory entry (if a longname) to remove
+ * Return:	0 for success
+ */
+static int delete_dentry_link(fat_itr *itr)
+{
+	itr->dent = itr->dent_start;
+	itr->remaining = itr->dent_rem;
+	/* Delete long name */
+	if ((itr->dent->attr & ATTR_VFAT) == ATTR_VFAT &&
+	    (itr->dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
+		int ret;
+
+		ret = delete_long_name(itr);
+		if (ret)
+			return ret;
+	}
+	/* Delete short name */
+	delete_single_dentry(itr);
+	return flush_dir(itr);
+}
+
 /**
  * delete_dentry_long() - remove directory entry
  *
@@ -1589,21 +1631,7 @@ static int delete_dentry_long(fat_itr *itr)
 		if (ret)
 			return ret;
 	}
-	itr->dent = itr->dent_start;
-	itr->remaining = itr->dent_rem;
-	dent = itr->dent_start;
-	/* Delete long name */
-	if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
-	    (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
-		int ret;
-
-		ret = delete_long_name(itr);
-		if (ret)
-			return ret;
-	}
-	/* Delete short name */
-	delete_single_dentry(itr);
-	return flush_dir(itr);
+	return delete_dentry_link(itr);
 }
 
 int fat_unlink(const char *filename)
@@ -1725,9 +1753,6 @@ int fat_mkdir(const char *dirname)
 		ret = -EEXIST;
 		goto exit;
 	} else {
-		char shortname[SHORT_NAME_SIZE];
-		int ndent;
-
 		if (itr->is_root) {
 			/* root dir cannot have "." or ".." */
 			if (!strcmp(l_dirname, ".") ||
@@ -1737,25 +1762,9 @@ int fat_mkdir(const char *dirname)
 			}
 		}
 
-		/* Check if long name is needed */
-		ndent = set_name(itr, basename, shortname);
-		if (ndent < 0) {
-			ret = ndent;
-			goto exit;
-		}
-		ret = fat_find_empty_dentries(itr, ndent);
+		ret = create_link(itr, basename, 0, 0, ATTR_DIR | ATTR_ARCH);
 		if (ret)
 			goto exit;
-		if (ndent > 1) {
-			/* Set long name entries */
-			ret = fill_dir_slot(itr, basename, shortname);
-			if (ret)
-				goto exit;
-		}
-
-		/* Set attribute as archive for regular file */
-		fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0,
-			    ATTR_DIR | ATTR_ARCH);
 
 		retdent = itr->dent;
 	}
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/6 fs: add rename infrastructure

In reply to Gabriel Dalimonte

The selection for *rename as the name for the rename/move operation derives from the POSIX specification where they name the function rename/renameat. [1] This aligns with Linux where the syscalls for renaming/moving also use the rename/renameat naming.

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html

---

Changes in v2:
- cleanup log message and verbosity in fs_rename()
- update fs_close() docstring with reference to fs_rename()
- clarify fs_rename() behaviour in docstring

---
 fs/fs.c      | 32 ++++++++++++++++++++++++++++++++
 include/fs.h | 14 +++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
fs/fs.c +32 -0
index 99ddcc5e37b..fdff83719b1 100644
@@ -143,6 +143,12 @@ static inline int fs_mkdir_unsupported(const char *dirname)
 	return -1;
 }
 
+static inline int fs_rename_unsupported(const char *old_path,
+					const char *new_path)
+{
+	return -1;
+}
+
 struct fstype_info {
 	int fstype;
 	char *name;
@@ -183,6 +189,7 @@ struct fstype_info {
 	int (*unlink)(const char *filename);
 	int (*mkdir)(const char *dirname);
 	int (*ln)(const char *filename, const char *target);
+	int (*rename)(const char *old_path, const char *new_path);
 };
 
 static struct fstype_info fstypes[] = {
@@ -206,6 +213,7 @@ static struct fstype_info fstypes[] = {
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 #endif
+		.rename = fs_rename_unsupported,
 		.uuid = fat_uuid,
 		.opendir = fat_opendir,
 		.readdir = fat_readdir,
@@ -238,6 +246,7 @@ static struct fstype_info fstypes[] = {
 		.closedir = ext4fs_closedir,
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 #if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD)
@@ -257,6 +266,7 @@ static struct fstype_info fstypes[] = {
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 		.ln = fs_ln_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 #if CONFIG_IS_ENABLED(SEMIHOSTING)
@@ -276,6 +286,7 @@ static struct fstype_info fstypes[] = {
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 		.ln = fs_ln_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 #ifndef CONFIG_XPL_BUILD
@@ -296,6 +307,7 @@ static struct fstype_info fstypes[] = {
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 		.ln = fs_ln_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 #endif
@@ -317,6 +329,7 @@ static struct fstype_info fstypes[] = {
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 		.ln = fs_ln_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 #endif
@@ -339,6 +352,7 @@ static struct fstype_info fstypes[] = {
 		.ln = fs_ln_unsupported,
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 #if IS_ENABLED(CONFIG_FS_EROFS)
@@ -360,6 +374,7 @@ static struct fstype_info fstypes[] = {
 		.ln = fs_ln_unsupported,
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 #endif
 	{
@@ -378,6 +393,7 @@ static struct fstype_info fstypes[] = {
 		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 		.ln = fs_ln_unsupported,
+		.rename = fs_rename_unsupported,
 	},
 };
 
@@ -713,6 +729,22 @@ int fs_ln(const char *fname, const char *target)
 	return ret;
 }
 
+int fs_rename(const char *old_path, const char *new_path)
+{
+	struct fstype_info *info = fs_get_info(fs_type);
+	int ret;
+
+	ret = info->rename(old_path, new_path);
+
+	if (ret < 0) {
+		log_debug("Unable to rename %s -> %s\n", old_path, new_path);
+		ret = -1;
+	}
+	fs_close();
+
+	return ret;
+}
+
 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	    int fstype)
 {
include/fs.h +13 -1
index 2474880385d..5b272eb9f5e 100644
@@ -86,7 +86,7 @@ int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
  *
  * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
  * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
- * fs_unlink().
+ * fs_unlink(), fs_rename().
  */
 void fs_close(void);
 
@@ -270,6 +270,18 @@ int fs_unlink(const char *filename);
  */
 int fs_mkdir(const char *filename);
 
+/**
+ * fs_rename - rename/move a file or directory
+ *
+ * @old_path: existing path of the file/directory to rename
+ * @new_path: new path of the file/directory. If this points to an existing
+ * file or empty directory, the existing file/directory will be unlinked.
+ * If this points to a non-empty directory, the rename will fail.
+ *
+ * Return: 0 on success, -1 on error conditions
+ */
+int fs_rename(const char *old_path, const char *new_path);
+
 /*
  * Common implementation for various filesystem commands, optionally limited
  * to a specific filesystem type via the fstype parameter.
Signature · 1 line
2.34.1
Acked-by on v2 2/6
#3 lore raw

Re: [PATCH v2 2/6] fs: add rename infrastructure

Hi Gabriel,

Quoted 176 lines · Gabriel Dalimonte

The selection for *rename as the name for the rename/move operation derives from the POSIX specification where they name the function rename/renameat. [1] This aligns with Linux where the syscalls for renaming/moving also use the rename/renameat naming.

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html

---

Changes in v2:
- cleanup log message and verbosity in fs_rename()
- update fs_close() docstring with reference to fs_rename()
- clarify fs_rename() behaviour in docstring

---
 fs/fs.c      | 32 ++++++++++++++++++++++++++++++++
 include/fs.h | 14 +++++++++++++-
 2 files changed, 45 insertions(+), 1 deletion(-)
fs/fs.c +32 -0
index 99ddcc5e37b..fdff83719b1 100644
@@ -143,6 +143,12 @@ static inline int fs_mkdir_unsupported(const char *dirname)
        return -1;
 }
 
+static inline int fs_rename_unsupported(const char *old_path,
+                                       const char *new_path)
+{
+       return -1;
+}
+
 struct fstype_info {
        int fstype;
        char *name;
@@ -183,6 +189,7 @@ struct fstype_info {
        int (*unlink)(const char *filename);
        int (*mkdir)(const char *dirname);
        int (*ln)(const char *filename, const char *target);
+       int (*rename)(const char *old_path, const char *new_path);
 };
 
 static struct fstype_info fstypes[] = {
@@ -206,6 +213,7 @@ static struct fstype_info fstypes[] = {
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
 #endif
+               .rename = fs_rename_unsupported,
                .uuid = fat_uuid,
                .opendir = fat_opendir,
                .readdir = fat_readdir,
@@ -238,6 +246,7 @@ static struct fstype_info fstypes[] = {
                .closedir = ext4fs_closedir,
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
 #if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD)
@@ -257,6 +266,7 @@ static struct fstype_info fstypes[] = {
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
                .ln = fs_ln_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
 #if CONFIG_IS_ENABLED(SEMIHOSTING)
@@ -276,6 +286,7 @@ static struct fstype_info fstypes[] = {
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
                .ln = fs_ln_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
 #ifndef CONFIG_XPL_BUILD
@@ -296,6 +307,7 @@ static struct fstype_info fstypes[] = {
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
                .ln = fs_ln_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
 #endif
@@ -317,6 +329,7 @@ static struct fstype_info fstypes[] = {
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
                .ln = fs_ln_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
 #endif
@@ -339,6 +352,7 @@ static struct fstype_info fstypes[] = {
                .ln = fs_ln_unsupported,
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
 #if IS_ENABLED(CONFIG_FS_EROFS)
@@ -360,6 +374,7 @@ static struct fstype_info fstypes[] = {
                .ln = fs_ln_unsupported,
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
+               .rename = fs_rename_unsupported,
        },
 #endif
        {
@@ -378,6 +393,7 @@ static struct fstype_info fstypes[] = {
                .unlink = fs_unlink_unsupported,
                .mkdir = fs_mkdir_unsupported,
                .ln = fs_ln_unsupported,
+               .rename = fs_rename_unsupported,
        },
 };
 
@@ -713,6 +729,22 @@ int fs_ln(const char *fname, const char *target)
        return ret;
 }
 
+int fs_rename(const char *old_path, const char *new_path)
+{
+       struct fstype_info *info = fs_get_info(fs_type);
+       int ret;
+
+       ret = info->rename(old_path, new_path);
+
+       if (ret < 0) {
+               log_debug("Unable to rename %s -> %s\n", old_path, new_path);
+               ret = -1;
+       }
+       fs_close();
+
+       return ret;
+}
+
 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
            int fstype)
 {
include/fs.h +13 -2
index 2474880385d..5b272eb9f5e 100644
@@ -86,7 +86,7 @@ int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
  *
  * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
  * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
- * fs_unlink().
+ * fs_unlink(), fs_rename().
  */
 void fs_close(void);
 
@@ -270,6 +270,18 @@ int fs_unlink(const char *filename);
  */
 int fs_mkdir(const char *filename);
 
+/**
+ * fs_rename - rename/move a file or directory
+ *
+ * @old_path: existing path of the file/directory to rename
+ * @new_path: new path of the file/directory. If this points to an existing
+ * file or empty directory, the existing file/directory will be unlinked.
+ * If this points to a non-empty directory, the rename will fail.
+ *
+ * Return: 0 on success, -1 on error conditions
+ */
+int fs_rename(const char *old_path, const char *new_path);
+
 /*
  * Common implementation for various filesystem commands, optionally limited
  * to a specific filesystem type via the fstype parameter.
--
Quoted 2 lines

2.34.1

I am by no means an expert on our FS support but this looks reasonable

#4 lore raw

PATCH v2 3/6 fs: fat: add rename

In reply to Gabriel Dalimonte

The implementation roughly follows the POSIX specification for rename() [1]. The ordering of operations attempting to minimize the chance for data loss in unexpected circumstances.

The 'mv' command was implemented as a front end for the rename operation as that is what most users are likely familiar with in terms of behavior.

The 'FAT_RENAME' Kconfig option was added to prevent code size increase on size-oriented builds like SPL.

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html

---

Changes in v2:
- change fatrename to mv (updating semantics to match mv)
- add mv command docs
- add FAT_RENAME Kconfig symbol
- update rename tests for mv command semantics
- update rename test docs on whether mv should succeed or fail
- remove all (fat) prefixes from commands used in rename tests
- clarify docs in check_path_prefix()
- stack allocate fat_itr in check_path_prefix()
- log on observed filesystem corruption in check_path_prefix()
- change 'break's to 'goto' in check_path_prefix()
- normalize added logging to use log_debug()
- correct error code returned when destination directory is not empty

---
 cmd/fs.c                                |  14 +
 doc/usage/cmd/mv.rst                    |  61 ++++
 fs/fat/Kconfig                          |   7 +
 fs/fat/fat_write.c                      | 285 ++++++++++++++++++
 fs/fs.c                                 |  65 ++++-
 include/fat.h                           |   1 +
 include/fs.h                            |   2 +
 lib/efi_loader/Kconfig                  |   1 +
 test/py/tests/test_fs/conftest.py       | 121 ++++++++
 test/py/tests/test_fs/fstest_helpers.py |   2 +
 test/py/tests/test_fs/test_rename.py    | 372 ++++++++++++++++++++++++
 11 files changed, 930 insertions(+), 1 deletion(-)
 create mode 100644 doc/usage/cmd/mv.rst
 create mode 100644 test/py/tests/test_fs/test_rename.py
cmd/fs.c +14 -0
index 3d7e06d6f1e..3faf7627447 100644
@@ -110,3 +110,17 @@ U_BOOT_CMD(
 	fstypes, 1, 1, do_fstypes_wrapper,
 	"List supported filesystem types", ""
 );
+
+static int do_mv_wrapper(struct cmd_tbl *cmdtp, int flag, int argc,
+			 char *const argv[])
+{
+	return do_mv(cmdtp, flag, argc, argv, FS_TYPE_ANY);
+}
+
+U_BOOT_CMD(
+	mv,	5,	1,	do_mv_wrapper,
+	"rename/move a file/directory",
+	"<interface> [<dev[:part]>] <old_path> <new_path>\n"
+	"    - renames/moves a file/directory in 'dev' on 'interface' from\n"
+	"      'old_path' to 'new_path'"
+);
doc/usage/cmd/mv.rst new file +61 -0

61 lines not on this page · raw message

fs/fat/Kconfig +7 -0
index 9bb11eac9f7..19d52238713 100644
@@ -13,6 +13,13 @@ config FAT_WRITE
 	  This provides support for creating and writing new files to an
 	  existing FAT filesystem partition.
 
+config FAT_RENAME
+	bool "Enable filesystem rename support"
+	depends on FAT_WRITE
+	help
+	  This provides support for renaming and moving files within a
+	  FAT filesystem partition.
+
 config FS_FAT_MAX_CLUSTSIZE
 	int "Set maximum possible clustersize"
 	default 65536
fs/fat/fat_write.c +285 -0

294 lines not on this page · raw message

fs/fs.c +64 -1

82 lines not on this page · raw message

include/fat.h +1 -0
index 3dce99a23cf..ca97880de12 100644
@@ -206,6 +206,7 @@ int fat_opendir(const char *filename, struct fs_dir_stream **dirsp);
 int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
 void fat_closedir(struct fs_dir_stream *dirs);
 int fat_unlink(const char *filename);
+int fat_rename(const char *old_path, const char *new_path);
 int fat_mkdir(const char *dirname);
 void fat_close(void);
 void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes);
include/fs.h +2 -0
index 5b272eb9f5e..54449faf2e5 100644
@@ -302,6 +302,8 @@ int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	     int fstype);
 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	  int fstype);
+int do_mv(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+	  int fstype);
 
 /*
  * Determine the UUID of the specified filesystem and print it. Optionally it is
lib/efi_loader/Kconfig +1 -0
index d4f6b56afaa..6130af14337 100644
@@ -27,6 +27,7 @@ config EFI_LOADER
 	select REGEX
 	imply FAT
 	imply FAT_WRITE
+	imply FAT_RENAME
 	imply USB_KEYBOARD_FN_KEYS
 	imply VIDEO_ANSI
 	help
test/py/tests/test_fs/conftest.py +121 -0

151 lines not on this page · raw message

test/py/tests/test_fs/fstest_helpers.py +2 -0
index faec2982489..c1447b4d43e 100644
@@ -9,5 +9,7 @@ def assert_fs_integrity(fs_type, fs_img):
     try:
         if fs_type == 'ext4':
             check_call('fsck.ext4 -n -f %s' % fs_img, shell=True)
+        elif fs_type in ['fat12', 'fat16', 'fat32']:
+            check_call('fsck.fat -n %s' % fs_img, shell=True)
     except CalledProcessError:
         raise
test/py/tests/test_fs/test_rename.py new file +372 -0

372 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

PATCH v2 4/6 fs: fat: update parent dirs metadata on dentry create/delete

In reply to Gabriel Dalimonte

POSIX filesystem functions that create or remove directory entries contain text along the lines of "[function] shall mark for update the last data modification and last file status change timestamps of the parent directory of each file." [1][2][3] The common theme is these timestamp updates occur when a directory entry is added or removed. The create_link() and delete_dentry_link() functions have been changed to update the modification timestamp on the directory where the direntry change occurs. This differs slightly from Linux in the case of rename(), where Linux will not update new_path's parent directory's timestamp if it is replacing an existing file. (via vfat_add_entry [4])

The timestamps are not updated if the build configuration does not support RTCs. This is an effort to minimize introducing erratic timestamps where they would go from [current date] -> 2000-01-01 (error timestamp in the FAT driver). I would assume an unchanged timestamp would be more valuable than a default timestamp in these cases.

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html
[2] https://pubs.opengroup.org/onlinepubs/9799919799/functions/unlink.html
[3] https://pubs.opengroup.org/onlinepubs/9799919799/functions/open.html
[4] https://elixir.bootlin.com/linux/v6.12.6/source/fs/fat/namei_vfat.c#L682

---

Changes in v2:
- remove setting ATTR_ARCH on parent dirs during renames
- skip updating timestamps if no RTC present
- make fat_itr stack allocated in update_parent_dir_props
- update parent dir props in create_link and delete_dentry_link

---
 fs/fat/fat_write.c | 78 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 74 insertions(+), 4 deletions(-)
fs/fat/fat_write.c +74 -4
index d4952e259ff..0b924541187 100644
@@ -1237,6 +1237,64 @@ static int fat_itr_parent(fat_itr *itr)
 	return fat_itr_resolve(itr, "..", TYPE_DIR);
 }
 
+/**
+ * update_parent_dir_props - updates the modified time for the parent directory
+ *
+ * @dir_itr:	iterator positioned anywhere in a directory whose parent
+ * should be updated
+ * @Return:	0 for success, -errno otherwise
+ */
+static int update_parent_dir_props(fat_itr *dir_itr)
+{
+	int ret = 0;
+
+	fat_itr itr;
+	fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
+	__u32 target_clust = dir_itr->start_clust;
+
+	/* Short circuit if no RTC because it only updates timestamps */
+	if (!CONFIG_IS_ENABLED(DM_RTC))
+		return ret;
+
+	/* duplicate fsdata */
+	itr = *dir_itr;
+	fsdata = *itr.fsdata;
+
+	/* allocate local fat buffer */
+	fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
+	if (!fsdata.fatbuf) {
+		log_debug("Error: allocating memory\n");
+		ret = -ENOMEM;
+		return ret;
+	}
+
+	fsdata.fatbufnum = -1;
+	itr.fsdata = &fsdata;
+
+	if (!itr.is_root) {
+		ret = fat_itr_parent(&itr);
+		if (ret)
+			goto exit;
+
+		while (fat_itr_next(&itr)) {
+			if (START(itr.dent) == target_clust)
+				goto update;
+		}
+
+		/* dent not found */
+		ret = -EIO;
+		goto exit;
+update:
+		dentry_set_time(itr.dent);
+		ret = flush_dir(&itr);
+	}
+
+exit:
+	free(fsdata.fatbuf);
+
+	return ret;
+}
+
 /**
  * create_link() - inserts a directory entry for a file or directory
  *
@@ -1270,8 +1328,9 @@ static int create_link(fat_itr *itr, char *basename, __u32 clust, __u32 size,
 	}
 
 	fill_dentry(itr->fsdata, itr->dent, shortname, clust, size, attr);
+	ret = update_parent_dir_props(itr);
 
-	return 0;
+	return ret;
 }
 
 /**
@@ -1612,20 +1671,25 @@ static int delete_long_name(fat_itr *itr)
  */
 static int delete_dentry_link(fat_itr *itr)
 {
+	int ret;
+
 	itr->dent = itr->dent_start;
 	itr->remaining = itr->dent_rem;
 	/* Delete long name */
 	if ((itr->dent->attr & ATTR_VFAT) == ATTR_VFAT &&
 	    (itr->dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
-		int ret;
-
 		ret = delete_long_name(itr);
 		if (ret)
 			return ret;
 	}
 	/* Delete short name */
 	delete_single_dentry(itr);
-	return flush_dir(itr);
+
+	ret = flush_dir(itr);
+	if (ret)
+		return ret;
+
+	return update_parent_dir_props(itr);
 }
 
 /**
@@ -2047,6 +2111,10 @@ int fat_rename(const char *old_path, const char *new_path)
 		*new_itr->dent = *old_itr->dent;
 		new_itr->dent->nameext = new_name;
 		new_itr->dent->lcase = lcase;
+
+		ret = update_parent_dir_props(new_itr);
+		if (ret)
+			goto exit;
 	} else {
 		/* reset iterator to the start of the directory */
 		ret = fat_move_to_cluster(new_itr, new_itr->start_clust);
@@ -2089,6 +2157,8 @@ int fat_rename(const char *old_path, const char *new_path)
 		ret = flush_dir(new_itr);
 		if (ret)
 			goto exit;
+		/* restore directory location to update parent props below */
+		fat_itr_child(new_itr, new_itr);
 	}
 
 	/* refresh old in case write happened to the same block. */
Signature · 1 line
2.34.1
#6 lore raw

PATCH v2 5/6 efi_loader: move path out of file_handle

In reply to Gabriel Dalimonte

In order to support renaming via SetInfo(), path must allow for longer values than what was originally present when file_handle was allocated.

---

(no changes since v1)

 lib/efi_loader/efi_file.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/efi_loader/efi_file.c +9 -4
index 201fa5f8f3c..6b15c1f3d27 100644
@@ -40,7 +40,7 @@ struct file_handle {
 	struct fs_dir_stream *dirs;
 	struct fs_dirent *dent;
 
-	char path[0];
+	char *path;
 };
 #define to_fh(x) container_of(x, struct file_handle, base)
 
@@ -178,6 +178,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
 		u64 attributes)
 {
 	struct file_handle *fh;
+	char *path;
 	char f0[MAX_UTF8_PER_UTF16] = {0};
 	int plen = 0;
 	int flen = 0;
@@ -194,11 +195,13 @@ static struct efi_file_handle *file_open(struct file_system *fs,
 		plen = strlen(parent->path) + 1;
 	}
 
+	fh = calloc(1, sizeof(*fh));
 	/* +2 is for null and '/' */
-	fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
-	if (!fh)
-		return NULL;
+	path = calloc(1, plen + (flen * MAX_UTF8_PER_UTF16) + 2);
+	if (!fh || !path)
+		goto error;
 
+	fh->path = path;
 	fh->open_mode = open_mode;
 	fh->base = efi_file_handle_protocol;
 	fh->fs = fs;
@@ -245,6 +248,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
 	return &fh->base;
 
 error:
+	free(fh->path);
 	free(fh);
 	return NULL;
 }
@@ -368,6 +372,7 @@ out:
 static efi_status_t file_close(struct file_handle *fh)
 {
 	fs_closedir(fh->dirs);
+	free(fh->path);
 	free(fh);
 	return EFI_SUCCESS;
 }
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 5/6] efi_loader: move path out of file_handle

Hi Gabriel,

Quoted 34 lines · Gabriel Dalimonte

In order to support renaming via SetInfo(), path must allow for longer values than what was originally present when file_handle was allocated.

---

(no changes since v1)

 lib/efi_loader/efi_file.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/efi_loader/efi_file.c +2 -1
index 201fa5f8f3c..6b15c1f3d27 100644
@@ -40,7 +40,7 @@ struct file_handle {
        struct fs_dir_stream *dirs;
        struct fs_dirent *dent;
 
-       char path[0];
+       char *path;
 };
 #define to_fh(x) container_of(x, struct file_handle, base)
 
@@ -178,6 +178,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
                u64 attributes)
 {
        struct file_handle *fh;
+       char *path;
        char f0[MAX_UTF8_PER_UTF16] = {0};
        int plen = 0;
        int flen = 0;

lib/efi_loader/efi_file.c static struct efi_file_handle *file_open(struct file_system *fs, · line 200

Quoted 9 lines
lib/efi_loader/efi_file.c +2 -3
@@ -194,11 +195,13 @@ static struct efi_file_handle *file_open(struct file_system *fs,
                plen = strlen(parent->path) + 1;
        }
 
+       fh = calloc(1, sizeof(*fh));
        /* +2 is for null and '/' */
-       fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
-       if (!fh)
-               return NULL;
+       path = calloc(1, plen + (flen * MAX_UTF8_PER_UTF16) + 2);

I assume the new allocation gets freed in patch #6 right? Is there an easy way to include this change in the current patch? Otherwise, we'll end up not freeing memory on a specific sha commit. I can live with it if the change is too tricky though.

Quoted 24 lines
lib/efi_loader/efi_file.c +5 -1
+       if (!fh || !path)
+               goto error;
 
+       fh->path = path;
        fh->open_mode = open_mode;
        fh->base = efi_file_handle_protocol;
        fh->fs = fs;
@@ -245,6 +248,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
        return &fh->base;
 
 error:
+       free(fh->path);
        free(fh);
        return NULL;
 }
@@ -368,6 +372,7 @@ out:
 static efi_status_t file_close(struct file_handle *fh)
 {
        fs_closedir(fh->dirs);
+       free(fh->path);
        free(fh);
        return EFI_SUCCESS;
 }
--
Quoted 2 lines

2.34.1

Thanks
/Ilias

#8 lore raw

Re: [PATCH v2 5/6] efi_loader: move path out of file_handle

Hi Ilias,

Thanks for the review!

Quoted 42 lines · Ilias Apalodimas

Hi Gabriel,

Quoted 37 lines · Gabriel Dalimonte

In order to support renaming via SetInfo(), path must allow for longer values than what was originally present when file_handle was allocated.

---

(no changes since v1)

 lib/efi_loader/efi_file.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
lib/efi_loader/efi_file.c +2 -1
index 201fa5f8f3c..6b15c1f3d27 100644
@@ -40,7 +40,7 @@ struct file_handle {
        struct fs_dir_stream *dirs;
        struct fs_dirent *dent;
 
-       char path[0];
+       char *path;
 };
 #define to_fh(x) container_of(x, struct file_handle, base)
 
@@ -178,6 +178,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
                u64 attributes)
 {
        struct file_handle *fh;
+       char *path;
        char f0[MAX_UTF8_PER_UTF16] = {0};
        int plen = 0;
        int flen = 0;
@@ -194,11 +195,13 @@ static struct efi_file_handle *file_open(struct file_system *fs,
                plen = strlen(parent->path) + 1;
        }
 
Quoted 11 lines

lib/efi_loader/efi_file.c static struct efi_file_handle *file_open(struct file_system *fs, · line 200

Quoted 6 lines
lib/efi_loader/efi_file.c +2 -3
+       fh = calloc(1, sizeof(*fh));
        /* +2 is for null and '/' */
-       fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2);
-       if (!fh)
-               return NULL;
+       path = calloc(1, plen + (flen * MAX_UTF8_PER_UTF16) + 2);

I assume the new allocation gets freed in patch #6 right? Is there an easy way to include this change in the current patch? Otherwise, we'll end up not freeing memory on a specific sha commit. I can live with it if the change is too tricky though.

The path variable is assigned to fh->path

Quoted 5 lines

lib/efi_loader/efi_file.c static struct efi_file_handle *file_open(struct file_system *fs, · line 204

Quoted 5 lines
lib/efi_loader/efi_file.c +3 -0
+       if (!fh || !path)
+               goto error;
 
+       fh->path = path;

here. I used the intermediate path variable to collapse calloc failure handling
into a single conditional. If it is preferable to split the conditional and eliminate
path, I can make that change.

Quoted 17 lines
Quoted 17 lines
lib/efi_loader/efi_file.c +2 -0
        fh->open_mode = open_mode;
        fh->base = efi_file_handle_protocol;
        fh->fs = fs;
@@ -245,6 +248,7 @@ static struct efi_file_handle *file_open(struct file_system *fs,
        return &fh->base;
 
 error:
+       free(fh->path);
        free(fh);
        return NULL;
 }
@@ -368,6 +372,7 @@ out:
 static efi_status_t file_close(struct file_handle *fh)
 {
        fs_closedir(fh->dirs);
+       free(fh->path);
        free(fh);
Quoted 8 lines
Quoted 5 lines
lib/efi_loader/efi_file.c +0 -1
        return EFI_SUCCESS;
 }
--

2.34.1

Thanks
/Ilias

Thanks,
Gabriel

#9 lore raw

PATCH v2 6/6 efi_loader: support file rename in SetInfo()

In reply to Gabriel Dalimonte

Following the UEFI specification. The specification did not seem to delineate if file_name was explicitly a file name only, or could include paths to move the file to a different directory. The more generous interpretation of supporting paths was selected.

---

Changes in v2:
- simplify freeing of new_file_name and new_path

---
 lib/efi_loader/efi_file.c | 45 +++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)
lib/efi_loader/efi_file.c +39 -6
index 6b15c1f3d27..7d81da8f2d8 100644
@@ -954,6 +954,7 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
 {
 	struct file_handle *fh = to_fh(file);
 	efi_status_t ret = EFI_UNSUPPORTED;
+	char *new_file_name = NULL, *new_path = NULL;
 
 	EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
 
@@ -983,13 +984,43 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
 		pos = new_file_name;
 		utf16_utf8_strcpy(&pos, info->file_name);
 		if (strcmp(new_file_name, filename)) {
-			/* TODO: we do not support renaming */
-			EFI_PRINT("Renaming not supported\n");
-			free(new_file_name);
-			ret = EFI_ACCESS_DENIED;
-			goto out;
+			int dlen;
+			int rv;
+
+			if (set_blk_dev(fh)) {
+				ret = EFI_DEVICE_ERROR;
+				goto out;
+			}
+			dlen = filename - fh->path;
+			new_path = calloc(1, dlen + strlen(new_file_name) + 1);
+			if (!new_path) {
+				ret = EFI_OUT_OF_RESOURCES;
+				goto out;
+			}
+			memcpy(new_path, fh->path, dlen);
+			strcpy(new_path + dlen, new_file_name);
+			sanitize_path(new_path);
+			rv = fs_exists(new_path);
+			if (rv) {
+				ret = EFI_ACCESS_DENIED;
+				goto out;
+			}
+			/* fs_exists() calls fs_close(), so open file system again */
+			if (set_blk_dev(fh)) {
+				ret = EFI_DEVICE_ERROR;
+				goto out;
+			}
+			rv = fs_rename(fh->path, new_path);
+			if (rv) {
+				ret = EFI_ACCESS_DENIED;
+				goto out;
+			}
+			free(fh->path);
+			fh->path = new_path;
+			/* Prevent new_path from being freed on out */
+			new_path = NULL;
+			ret = EFI_SUCCESS;
 		}
-		free(new_file_name);
 		/* Check for truncation */
 		if (!fh->isdir) {
 			ret = efi_get_file_size(fh, &file_size);
@@ -1012,6 +1043,8 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
 		ret = EFI_UNSUPPORTED;
 	}
 out:
+	free(new_path);
+	free(new_file_name);
 	return EFI_EXIT(ret);
 }
 
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH v2 6/6] efi_loader: support file rename in SetInfo()

Hi Gabriel,

Quoted 64 lines · Gabriel Dalimonte

Following the UEFI specification. The specification did not seem to delineate if file_name was explicitly a file name only, or could include paths to move the file to a different directory. The more generous interpretation of supporting paths was selected.

---

Changes in v2:
- simplify freeing of new_file_name and new_path

---
 lib/efi_loader/efi_file.c | 45 +++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)
lib/efi_loader/efi_file.c +26 -5
index 6b15c1f3d27..7d81da8f2d8 100644
@@ -954,6 +954,7 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
 {
        struct file_handle *fh = to_fh(file);
        efi_status_t ret = EFI_UNSUPPORTED;
+       char *new_file_name = NULL, *new_path = NULL;
 
        EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
 
@@ -983,13 +984,43 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
                pos = new_file_name;
                utf16_utf8_strcpy(&pos, info->file_name);
                if (strcmp(new_file_name, filename)) {
-                       /* TODO: we do not support renaming */
-                       EFI_PRINT("Renaming not supported\n");
-                       free(new_file_name);
-                       ret = EFI_ACCESS_DENIED;
-                       goto out;
+                       int dlen;
+                       int rv;
+
+                       if (set_blk_dev(fh)) {
+                               ret = EFI_DEVICE_ERROR;
+                               goto out;
+                       }
+                       dlen = filename - fh->path;
+                       new_path = calloc(1, dlen + strlen(new_file_name) + 1);
+                       if (!new_path) {
+                               ret = EFI_OUT_OF_RESOURCES;
+                               goto out;
+                       }
+                       memcpy(new_path, fh->path, dlen);
+                       strcpy(new_path + dlen, new_file_name);
+                       sanitize_path(new_path);
+                       rv = fs_exists(new_path);
+                       if (rv) {
+                               ret = EFI_ACCESS_DENIED;
+                               goto out;
+                       }
+                       /* fs_exists() calls fs_close(), so open file system again */
+                       if (set_blk_dev(fh)) {
+                               ret = EFI_DEVICE_ERROR;
+                               goto out;

lib/efi_loader/efi_file.c static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, · line 1021

Quoted 10 lines
lib/efi_loader/efi_file.c +10 -0
+                       }
+                       rv = fs_rename(fh->path, new_path);
+                       if (rv) {
+                               ret = EFI_ACCESS_DENIED;
+                               goto out;
+                       }
+                       free(fh->path);
+                       fh->path = new_path;
+                       /* Prevent new_path from being freed on out */
+                       new_path = NULL;

fh->path is also NULL now. I am not sure where these are used but this seems wrong.

Quoted 16 lines
lib/efi_loader/efi_file.c +3 -2
+                       ret = EFI_SUCCESS;
                }
-               free(new_file_name);
                /* Check for truncation */
                if (!fh->isdir) {
                        ret = efi_get_file_size(fh, &file_size);
@@ -1012,6 +1043,8 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
                ret = EFI_UNSUPPORTED;
        }
 out:
+       free(new_path);
+       free(new_file_name);
        return EFI_EXIT(ret);
 }
 
--
Quoted 2 lines

2.34.1

Thanks
/Ilias

Reviewed-by
#11 lore raw

Re: [PATCH v2 6/6] efi_loader: support file rename in SetInfo()

Quoted 74 lines · Ilias Apalodimas

Hi Gabriel,

Quoted 69 lines · Gabriel Dalimonte

Following the UEFI specification. The specification did not seem to delineate if file_name was explicitly a file name only, or could include paths to move the file to a different directory. The more generous interpretation of supporting paths was selected.

---

Changes in v2:
- simplify freeing of new_file_name and new_path

---
 lib/efi_loader/efi_file.c | 45 +++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)
lib/efi_loader/efi_file.c +31 -5
index 6b15c1f3d27..7d81da8f2d8 100644
@@ -954,6 +954,7 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
 {
        struct file_handle *fh = to_fh(file);
        efi_status_t ret = EFI_UNSUPPORTED;
+       char *new_file_name = NULL, *new_path = NULL;
 
        EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer);
 
@@ -983,13 +984,43 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
                pos = new_file_name;
                utf16_utf8_strcpy(&pos, info->file_name);
                if (strcmp(new_file_name, filename)) {
-                       /* TODO: we do not support renaming */
-                       EFI_PRINT("Renaming not supported\n");
-                       free(new_file_name);
-                       ret = EFI_ACCESS_DENIED;
-                       goto out;
+                       int dlen;
+                       int rv;
+
+                       if (set_blk_dev(fh)) {
+                               ret = EFI_DEVICE_ERROR;
+                               goto out;
+                       }
+                       dlen = filename - fh->path;
+                       new_path = calloc(1, dlen + strlen(new_file_name) + 1);
+                       if (!new_path) {
+                               ret = EFI_OUT_OF_RESOURCES;
+                               goto out;
+                       }
+                       memcpy(new_path, fh->path, dlen);
+                       strcpy(new_path + dlen, new_file_name);
+                       sanitize_path(new_path);
+                       rv = fs_exists(new_path);
+                       if (rv) {
+                               ret = EFI_ACCESS_DENIED;
+                               goto out;
+                       }
+                       /* fs_exists() calls fs_close(), so open file system again */
+                       if (set_blk_dev(fh)) {
+                               ret = EFI_DEVICE_ERROR;
+                               goto out;
+                       }
+                       rv = fs_rename(fh->path, new_path);
+                       if (rv) {
+                               ret = EFI_ACCESS_DENIED;
+                               goto out;
Quoted 8 lines

lib/efi_loader/efi_file.c static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, · line 1021

Quoted 5 lines
lib/efi_loader/efi_file.c +5 -0
+                       }
+                       free(fh->path);
+                       fh->path = new_path;
+                       /* Prevent new_path from being freed on out */
+                       new_path = NULL;

fh->path is also NULL now. I am not sure where these are used but this seems wrong.

Right ignore me, this is comment is obviously wrong and the patch is fine

Quoted 16 lines
Quoted 16 lines
lib/efi_loader/efi_file.c +3 -1
+                       ret = EFI_SUCCESS;
                }
-               free(new_file_name);
                /* Check for truncation */
                if (!fh->isdir) {
                        ret = efi_get_file_size(fh, &file_size);
@@ -1012,6 +1043,8 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file,
                ret = EFI_UNSUPPORTED;
        }
 out:
+       free(new_path);
+       free(new_file_name);
        return EFI_EXIT(ret);
 }
 
Quoted 6 lines
Quoted 3 lines

--
2.34.1

Thanks
/Ilias

#12 lore raw
In reply to Gabriel Dalimonte
Quoted 9 lines · Gabriel Dalimonte

This series adds support for file renaming to EFI_FILE_PROTOCOL.SetInfo(). One of the use cases for renaming in EFI is to facilitate boot loader boot counting.

No existing filesystems in U-Boot currently include file renaming, resulting in support for renaming at the filesystem level and a concrete implementation for the FAT filesystem.

[...]

Applied to u-boot/next, thanks!

[1/6] fs: fat: factor out dentry link create/delete

      commit: 1742b8484e797c56c720c24fa7b923a6d7275cc6
[2/6] fs: add rename infrastructure
      commit: d9c149664fa7a0c2eabfc046dcf89637f655364b
[3/6] fs: fat: add rename
      commit: 06159a1465fc97d8d7b72b9bea39a396f6e7057c
[4/6] fs: fat: update parent dirs metadata on dentry create/delete
      commit: 879eee641b6ddd4cd8299cb14803766935536b3e
[5/6] efi_loader: move path out of file_handle
      commit: 8465ee528b84f242e403a3c6e67dab5b244bacc8
[6/6] efi_loader: support file rename in SetInfo()
      commit: 0165e1a8bd80ee91216e901064bfd4b0ca7f623a
Signature · 1 line
Tom