Skip to content

Commit 1ea0267

Browse files
kica-zde-nordic
authored andcommitted
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: Carlo Kirchmeier <carlo.kirchmeier@zuehlke.com> Co-authored-by: Carlo Kirchmeier <carlo.kirchmeier@zuehlke.com> Co-authored-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent df5ffc9 commit 1ea0267

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (C) 2025 Endress+Hauser AG
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
Description of pre-defined fatfs file systems.
6+
7+
compatible: "zephyr,fstab,fatfs"
8+
9+
include: zephyr,fstab-common.yaml

dts/bindings/fs/zephyr,fstab,littlefs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ include: "zephyr,fstab-common.yaml"
1111
properties:
1212
# num-files and num-dirs are not filesystem-specific.
1313

14+
partition:
15+
type: phandle
16+
required: true
17+
description: |
18+
A reference to the file system's partition.
19+
1420
read-size:
1521
type: int
1622
required: true

dts/bindings/fs/zephyr,fstab-common.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ properties:
1212
description: |
1313
The absolute path used as the file system mount point.
1414
15-
partition:
16-
type: phandle
17-
required: true
18-
description: |
19-
A reference to the file system's partition.
20-
2115
automount:
2216
type: boolean
2317
description: |

subsys/fs/Kconfig.fatfs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ config FS_FATFS_CUSTOM_MOUNT_POINTS
299299
be used for mounting fatfs filesystems anymore.
300300
depends on FS_FATFS_CUSTOM_MOUNT_POINT_COUNT > 0
301301

302+
DT_COMPAT_ZEPHYR_FSTAB_FATFS := zephyr,fstab,fatfs
303+
config FS_FATFS_FSTAB_AUTOMOUNT
304+
bool "Support for fstab auto-mounting"
305+
depends on $(dt_compat_enabled,$(DT_COMPAT_ZEPHYR_FSTAB_FATFS))
306+
help
307+
This option enables fatfs support for the devicetree fstab auto-mounting
308+
functionality.
309+
302310
endmenu
303311

304312
endif # FAT_FILESYSTEM_ELM

subsys/fs/fat_fs.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,43 @@ static const struct fs_file_system_t fatfs_fs = {
557557
#endif
558558
};
559559

560+
#define DT_DRV_COMPAT zephyr_fstab_fatfs
561+
562+
#define DEFINE_FS(inst) \
563+
BUILD_ASSERT(DT_INST_PROP(inst, disk_access), "FATFS needs disk-access"); \
564+
BUILD_ASSERT(!DT_INST_PROP(inst, read_only), \
565+
"READ_ONLY not supported for individual instances see FS_FATFS_READ_ONLY"); \
566+
BUILD_ASSERT(!DT_INST_PROP(inst, no_format), \
567+
"NO_FORMAT not supported for individual instanzes FS_FATFS_MKFS"); \
568+
static FATFS fs_data_##inst; \
569+
struct fs_mount_t FS_FSTAB_ENTRY(DT_DRV_INST(inst)) = { \
570+
.type = FS_FATFS, \
571+
.mnt_point = DT_INST_PROP(inst, mount_point), \
572+
.fs_data = &fs_data_##inst, \
573+
.storage_dev = NULL, \
574+
.flags = FSTAB_ENTRY_DT_MOUNT_FLAGS(DT_DRV_INST(inst)), \
575+
};
576+
577+
DT_INST_FOREACH_STATUS_OKAY(DEFINE_FS);
578+
579+
#ifdef CONFIG_FS_FATFS_FSTAB_AUTOMOUNT
580+
#define REFERENCE_MOUNT(inst) (&FS_FSTAB_ENTRY(DT_DRV_INST(inst))),
581+
582+
static void automount_if_enabled(struct fs_mount_t *mountp)
583+
{
584+
int ret = 0;
585+
586+
if ((mountp->flags & FS_MOUNT_FLAG_AUTOMOUNT) != 0) {
587+
ret = fs_mount(mountp);
588+
if (ret < 0) {
589+
LOG_ERR("Error mounting filesystem: at %s: %d", mountp->mnt_point, ret);
590+
} else {
591+
LOG_DBG("FATFS Filesystem \"%s\" initialized", mountp->mnt_point);
592+
}
593+
}
594+
}
595+
#endif /* CONFIG_FS_FATFS_FSTAB_AUTOMOUNT */
596+
560597
#if CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT
561598
const char *VolumeStr[CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT];
562599
#endif /* CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT */
@@ -581,7 +618,21 @@ static int fatfs_init(void)
581618
}
582619
}
583620
#endif /* CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT */
584-
return fs_register(FS_FATFS, &fatfs_fs);
621+
int rc = fs_register(FS_FATFS, &fatfs_fs);
622+
623+
#ifdef CONFIG_FS_FATFS_FSTAB_AUTOMOUNT
624+
if (rc == 0) {
625+
struct fs_mount_t *partitions[] = {DT_INST_FOREACH_STATUS_OKAY(REFERENCE_MOUNT)};
626+
627+
for (size_t i = 0; i < ARRAY_SIZE(partitions); i++) {
628+
struct fs_mount_t *mpi = partitions[i];
629+
630+
automount_if_enabled(mpi);
631+
}
632+
}
633+
#endif /* CONFIG_FS_FATFS_FSTAB_AUTOMOUNT */
634+
635+
return rc;
585636
}
586637

587638
SYS_INIT(fatfs_init, POST_KERNEL, CONFIG_FILE_SYSTEM_INIT_PRIORITY);

0 commit comments

Comments
 (0)