Commit 1ea0267a authored by Carlo Kirchmeier's avatar Carlo Kirchmeier Committed by Benjamin Cabé
Browse files

fs: enable fstab devicetree integration for fatfs

In order to use fstab with fatfs in addition to
littlefs a new driver fstab,fatfs was introduced
containing the required logic to detect and if
needed automount fatfs devices specified in fstab.
Additionally as the partition phandle is not needed
by fatfs and currently is specific to littlefs it
was moved from the common dts binding into the
littlefs binding.

Signed-off-by: default avatarCarlo Kirchmeier <carlo.kirchmeier@zuehlke.com>
Co-authored-by: default avatarCarlo Kirchmeier <carlo.kirchmeier@zuehlke.com>
Co-authored-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent df5ffc91
Loading
Loading
Loading
Loading
+9 −0
Changes for dts/bindings/fs/zephyr,fstab,fatfs.yaml: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Copyright (C) 2025 Endress+Hauser AG
# SPDX-License-Identifier: Apache-2.0

description: |
    Description of pre-defined fatfs file systems.

compatible: "zephyr,fstab,fatfs"

include: zephyr,fstab-common.yaml
+6 −0
Changes for dts/bindings/fs/zephyr,fstab,littlefs.yaml: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -11,6 +11,12 @@ include: "zephyr,fstab-common.yaml"
properties:
  # num-files and num-dirs are not filesystem-specific.

  partition:
    type: phandle
    required: true
    description: |
      A reference to the file system's partition.

  read-size:
    type: int
    required: true
+0 −6
Changes for dts/bindings/fs/zephyr,fstab-common.yaml: 0 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -12,12 +12,6 @@ properties:
    description: |
      The absolute path used as the file system mount point.

  partition:
    type: phandle
    required: true
    description: |
      A reference to the file system's partition.

  automount:
    type: boolean
    description: |
+8 −0
Changes for subsys/fs/Kconfig.fatfs: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -299,6 +299,14 @@ config FS_FATFS_CUSTOM_MOUNT_POINTS
	   be used for mounting fatfs filesystems anymore.
	depends on FS_FATFS_CUSTOM_MOUNT_POINT_COUNT > 0

DT_COMPAT_ZEPHYR_FSTAB_FATFS := zephyr,fstab,fatfs
config FS_FATFS_FSTAB_AUTOMOUNT
	bool "Support for fstab auto-mounting"
	depends on $(dt_compat_enabled,$(DT_COMPAT_ZEPHYR_FSTAB_FATFS))
	help
	  This option enables fatfs support for the devicetree fstab auto-mounting
	  functionality.

endmenu

endif # FAT_FILESYSTEM_ELM
+52 −1
Changes for subsys/fs/fat_fs.c: 52 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -557,6 +557,43 @@ static const struct fs_file_system_t fatfs_fs = {
#endif
};

#define DT_DRV_COMPAT zephyr_fstab_fatfs

#define DEFINE_FS(inst)                                                                            \
	BUILD_ASSERT(DT_INST_PROP(inst, disk_access), "FATFS needs disk-access");                  \
	BUILD_ASSERT(!DT_INST_PROP(inst, read_only),                                               \
		     "READ_ONLY not supported for individual instances see FS_FATFS_READ_ONLY");   \
	BUILD_ASSERT(!DT_INST_PROP(inst, no_format),                                               \
		     "NO_FORMAT not supported for individual instanzes FS_FATFS_MKFS");            \
	static FATFS fs_data_##inst;                                                               \
	struct fs_mount_t FS_FSTAB_ENTRY(DT_DRV_INST(inst)) = {                                    \
		.type = FS_FATFS,                                                                  \
		.mnt_point = DT_INST_PROP(inst, mount_point),                                      \
		.fs_data = &fs_data_##inst,                                                        \
		.storage_dev = NULL,                                                               \
		.flags = FSTAB_ENTRY_DT_MOUNT_FLAGS(DT_DRV_INST(inst)),                            \
	};

DT_INST_FOREACH_STATUS_OKAY(DEFINE_FS);

#ifdef CONFIG_FS_FATFS_FSTAB_AUTOMOUNT
#define REFERENCE_MOUNT(inst) (&FS_FSTAB_ENTRY(DT_DRV_INST(inst))),

static void automount_if_enabled(struct fs_mount_t *mountp)
{
	int ret = 0;

	if ((mountp->flags & FS_MOUNT_FLAG_AUTOMOUNT) != 0) {
		ret = fs_mount(mountp);
		if (ret < 0) {
			LOG_ERR("Error mounting filesystem: at %s: %d", mountp->mnt_point, ret);
		} else {
			LOG_DBG("FATFS Filesystem \"%s\" initialized", mountp->mnt_point);
		}
	}
}
#endif /* CONFIG_FS_FATFS_FSTAB_AUTOMOUNT */

#if CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT
const char *VolumeStr[CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT];
#endif /* CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT */
@@ -581,7 +618,21 @@ static int fatfs_init(void)
		}
	}
#endif /* CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT */
	return fs_register(FS_FATFS, &fatfs_fs);
	int rc = fs_register(FS_FATFS, &fatfs_fs);

#ifdef CONFIG_FS_FATFS_FSTAB_AUTOMOUNT
	if (rc == 0) {
		struct fs_mount_t *partitions[] = {DT_INST_FOREACH_STATUS_OKAY(REFERENCE_MOUNT)};

		for (size_t i = 0; i < ARRAY_SIZE(partitions); i++) {
			struct fs_mount_t *mpi = partitions[i];

			automount_if_enabled(mpi);
		}
	}
#endif /* CONFIG_FS_FATFS_FSTAB_AUTOMOUNT */

	return rc;
}

SYS_INIT(fatfs_init, POST_KERNEL, CONFIG_FILE_SYSTEM_INIT_PRIORITY);