mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #17341 from benpicco/vfs-mtd_cleanup
sys/vfs: add file-system auto-mount
This commit is contained in:
commit
be45400631
@ -20,8 +20,58 @@
|
||||
|
||||
#ifdef MODULE_MTD
|
||||
#include "mtd_native.h"
|
||||
|
||||
mtd_native_dev_t mtd0_dev = {
|
||||
.base = {
|
||||
.driver = &native_flash_driver,
|
||||
.sector_count = MTD_SECTOR_NUM,
|
||||
.pages_per_sector = MTD_SECTOR_SIZE / MTD_PAGE_SIZE,
|
||||
.page_size = MTD_PAGE_SIZE,
|
||||
},
|
||||
.fname = MTD_NATIVE_FILENAME,
|
||||
};
|
||||
|
||||
mtd_dev_t *mtd0 = &mtd0_dev.base;
|
||||
#endif
|
||||
|
||||
#ifdef MODULE_VFS
|
||||
#include "vfs.h"
|
||||
|
||||
/*
|
||||
* On `native` we define auto-mounts for every file system.
|
||||
*
|
||||
* A 'real' board would typically always use the same file system to avoid
|
||||
* data loss when re-formatting, but since `native` is for testing only we
|
||||
* provide all file system definitions here.
|
||||
*/
|
||||
|
||||
/* littlefs support */
|
||||
#if defined(MODULE_LITTLEFS)
|
||||
|
||||
#include "fs/littlefs_fs.h"
|
||||
VFS_AUTO_MOUNT(littlefs, VFS_MTD(mtd0_dev), "/", 0);
|
||||
|
||||
/* littlefs2 support */
|
||||
#elif defined(MODULE_LITTLEFS2)
|
||||
|
||||
#include "fs/littlefs2_fs.h"
|
||||
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(mtd0_dev), "/", 0);
|
||||
|
||||
/* spiffs support */
|
||||
#elif defined(MODULE_SPIFFS)
|
||||
|
||||
#include "fs/spiffs_fs.h"
|
||||
VFS_AUTO_MOUNT(spiffs, VFS_MTD(mtd0_dev), "/", 0);
|
||||
|
||||
/* FAT support */
|
||||
#elif defined(MODULE_FATFS_VFS)
|
||||
|
||||
#include "fs/fatfs.h"
|
||||
VFS_AUTO_MOUNT(fatfs, VFS_MTD(mtd0_dev), "/", 0);
|
||||
|
||||
#endif
|
||||
#endif /* MODULE_VFS */
|
||||
|
||||
/**
|
||||
* Nothing to initialize at the moment.
|
||||
* Turns the red LED on and the green LED off.
|
||||
@ -33,17 +83,3 @@ void board_init(void)
|
||||
|
||||
puts("RIOT native board initialized.");
|
||||
}
|
||||
|
||||
#ifdef MODULE_MTD
|
||||
static mtd_native_dev_t mtd0_dev = {
|
||||
.dev = {
|
||||
.driver = &native_flash_driver,
|
||||
.sector_count = MTD_SECTOR_NUM,
|
||||
.pages_per_sector = MTD_SECTOR_SIZE / MTD_PAGE_SIZE,
|
||||
.page_size = MTD_PAGE_SIZE,
|
||||
},
|
||||
.fname = MTD_NATIVE_FILENAME,
|
||||
};
|
||||
|
||||
mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd0_dev;
|
||||
#endif
|
||||
|
@ -28,7 +28,7 @@ extern "C" {
|
||||
|
||||
/** mtd native descriptor */
|
||||
typedef struct mtd_native_dev {
|
||||
mtd_dev_t dev; /**< mtd generic device */
|
||||
mtd_dev_t base; /**< mtd generic device */
|
||||
const char *fname; /**< filename to use for memory emulation */
|
||||
} mtd_native_dev_t;
|
||||
|
||||
|
@ -24,9 +24,6 @@
|
||||
#define MTD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#if MODULE_VFS
|
||||
#include "vfs.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -397,13 +394,6 @@ int mtd_erase_sector(mtd_dev_t *mtd, uint32_t sector, uint32_t num);
|
||||
*/
|
||||
int mtd_power(mtd_dev_t *mtd, enum mtd_power_state power);
|
||||
|
||||
#if defined(MODULE_VFS) || defined(DOXYGEN)
|
||||
/**
|
||||
* @brief MTD driver for VFS
|
||||
*/
|
||||
extern const vfs_file_ops_t mtd_vfs_ops;
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -30,6 +30,8 @@ USEMODULE += mtd
|
||||
|
||||
# Use VFS
|
||||
USEMODULE += vfs
|
||||
# the example demonstrates manual file system mounting, disable auto-mount
|
||||
DISABLE_MODULE += vfs_auto_mount
|
||||
|
||||
# Use a file system
|
||||
# USEMODULE += littlefs
|
||||
|
@ -45,8 +45,8 @@ static mtd_sdcard_t mtd_sdcard_dev = {
|
||||
.sd_card = &sdcard_spi_devs[0],
|
||||
.params = &sdcard_spi_params[0],
|
||||
};
|
||||
static mtd_dev_t *mtd0 = (mtd_dev_t*)&mtd_sdcard_dev;
|
||||
#define MTD_0 mtd0
|
||||
static mtd_dev_t *mtd_sdcard = (mtd_dev_t*)&mtd_sdcard_dev;
|
||||
#define MTD_0 mtd_sdcard
|
||||
#endif
|
||||
|
||||
/* Flash mount point */
|
||||
@ -111,9 +111,6 @@ static spiffs_desc_t fs_desc = {
|
||||
* this example focus on basic usage, i.e. entire memory used */
|
||||
static fatfs_desc_t fs_desc;
|
||||
|
||||
/* provide mtd devices for use within diskio layer of fatfs */
|
||||
mtd_dev_t *fatfs_mtd_devs[FF_VOLUMES];
|
||||
|
||||
/* fatfs driver will be used */
|
||||
#define FS_DRIVER fatfs_file_system
|
||||
#endif
|
||||
@ -311,12 +308,12 @@ static const shell_command_t shell_commands[] = {
|
||||
|
||||
int main(void)
|
||||
{
|
||||
#if defined(MTD_0) && (defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || defined(MODULE_LITTLEFS2))
|
||||
#if defined(MTD_0) && \
|
||||
(defined(MODULE_SPIFFS) || defined(MODULE_LITTLEFS) || \
|
||||
defined(MODULE_LITTLEFS2) || defined(MODULE_FATFS_VFS))
|
||||
/* spiffs and littlefs need a mtd pointer
|
||||
* by default the whole memory is used */
|
||||
fs_desc.dev = MTD_0;
|
||||
#elif defined(MTD_0) && defined(MODULE_FATFS_VFS)
|
||||
fatfs_mtd_devs[fs_desc.vol_idx] = MTD_0;
|
||||
#endif
|
||||
int res = vfs_mount(&const_mount);
|
||||
if (res < 0) {
|
||||
|
@ -214,6 +214,8 @@ PSEUDOMODULES += suit_transport_%
|
||||
PSEUDOMODULES += suit_storage_%
|
||||
PSEUDOMODULES += sys_bus_%
|
||||
PSEUDOMODULES += vdd_lc_filter_%
|
||||
PSEUDOMODULES += vfs_auto_format
|
||||
PSEUDOMODULES += vfs_auto_mount
|
||||
PSEUDOMODULES += wakaama_objects_%
|
||||
PSEUDOMODULES += wifi_enterprise
|
||||
PSEUDOMODULES += xtimer_on_ztimer
|
||||
|
@ -71,8 +71,18 @@ DSTATUS disk_initialize(BYTE pdrv)
|
||||
return STA_NOINIT;
|
||||
}
|
||||
|
||||
return (mtd_init(fatfs_mtd_devs[pdrv]) == 0) ? FATFS_DISKIO_DSTASTUS_OK
|
||||
: STA_NOINIT;
|
||||
if (mtd_init(fatfs_mtd_devs[pdrv])) {
|
||||
return STA_NOINIT;
|
||||
}
|
||||
|
||||
uint32_t sector_size = fatfs_mtd_devs[pdrv]->page_size
|
||||
* fatfs_mtd_devs[pdrv]->pages_per_sector;
|
||||
if (sector_size > FF_MAX_SS) {
|
||||
assert(0);
|
||||
return STA_NOINIT;
|
||||
}
|
||||
|
||||
return FATFS_DISKIO_DSTASTUS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,6 +35,8 @@
|
||||
static int fatfs_err_to_errno(int32_t err);
|
||||
static void _fatfs_time_to_timespec(WORD fdate, WORD ftime, time_t *time);
|
||||
|
||||
mtd_dev_t *fatfs_mtd_devs[FF_VOLUMES];
|
||||
|
||||
/**
|
||||
* @brief Concatenate drive number and path into the buffer provided by fs_desc
|
||||
*
|
||||
@ -46,6 +48,25 @@ static void _build_abs_path(fatfs_desc_t *fs_desc, const char *name)
|
||||
fs_desc->vol_idx, name);
|
||||
}
|
||||
|
||||
static int _init(vfs_mount_t *mountp)
|
||||
{
|
||||
fatfs_desc_t *fs_desc = mountp->private_data;
|
||||
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(fatfs_mtd_devs); ++i) {
|
||||
if (fatfs_mtd_devs[i] == fs_desc->dev) {
|
||||
/* already initialized */
|
||||
return 0;
|
||||
}
|
||||
if (fatfs_mtd_devs[i] == NULL) {
|
||||
fatfs_mtd_devs[i] = fs_desc->dev;
|
||||
fs_desc->vol_idx = i;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int _mount(vfs_mount_t *mountp)
|
||||
{
|
||||
/* if one of the lines below fail to compile you probably need to adjust
|
||||
@ -57,6 +78,11 @@ static int _mount(vfs_mount_t *mountp)
|
||||
|
||||
fatfs_desc_t *fs_desc = (fatfs_desc_t *)mountp->private_data;
|
||||
|
||||
if (_init(mountp)) {
|
||||
DEBUG("can't find free slot in fatfs_mtd_devs\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
_build_abs_path(fs_desc, "");
|
||||
|
||||
memset(&fs_desc->fat_fs, 0, sizeof(fs_desc->fat_fs));
|
||||
|
@ -499,6 +499,7 @@ ifneq (,$(filter vfs,$(USEMODULE)))
|
||||
ifeq (native, $(BOARD))
|
||||
USEMODULE += native_vfs
|
||||
endif
|
||||
DEFAULT_MODULE += vfs_auto_mount
|
||||
endif
|
||||
|
||||
ifneq (,$(filter sock_async_event,$(USEMODULE)))
|
||||
|
@ -154,6 +154,11 @@ void auto_init(void)
|
||||
extern void auto_init_devfs(void);
|
||||
auto_init_devfs();
|
||||
}
|
||||
if (IS_USED(MODULE_VFS_AUTO_MOUNT)) {
|
||||
LOG_DEBUG("Mounting filesystems.\n");
|
||||
extern void auto_init_vfs(void);
|
||||
auto_init_vfs();
|
||||
}
|
||||
if (IS_USED(MODULE_AUTO_INIT_GNRC_IPV6_NIB)) {
|
||||
LOG_DEBUG("Auto init gnrc_ipv6_nib.\n");
|
||||
extern void gnrc_ipv6_nib_init(void);
|
||||
|
@ -54,6 +54,7 @@ extern "C" {
|
||||
*/
|
||||
typedef struct fatfs_desc {
|
||||
FATFS fat_fs; /**< FatFs work area needed for each volume */
|
||||
mtd_dev_t *dev; /**< MTD device to use */
|
||||
uint8_t vol_idx; /**< low level device that is used by FatFs */
|
||||
|
||||
/** most FatFs file operations need an absolute path. This buffer provides
|
||||
|
@ -68,6 +68,8 @@
|
||||
|
||||
#include "sched.h"
|
||||
#include "clist.h"
|
||||
#include "mtd.h"
|
||||
#include "xfa.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@ -228,6 +230,36 @@ extern "C" {
|
||||
*/
|
||||
#define VFS_ANY_FD (-1)
|
||||
|
||||
/**
|
||||
* @brief Helper macro for VFS_AUTO_MOUNT
|
||||
*
|
||||
* @param[in] mtd MTD device to use for filesystem
|
||||
*/
|
||||
#define VFS_MTD(mtd) { .dev = &mtd.base }
|
||||
|
||||
/**
|
||||
* @brief Define an automatic mountpoint
|
||||
*
|
||||
* @param[in] type file system type
|
||||
* Can be littlefs, littlefs2, spiffs or fatfs
|
||||
*
|
||||
* Internally, file systems supporting this must name their
|
||||
* @ref vfs_file_system_t `${TYPE}_file_system`, and must use
|
||||
* a type named `${TYPE}_desc_t` for their private data
|
||||
* @param[in] mtd file system backed device configuration
|
||||
* @param[in] path Mount path
|
||||
* @param[in] idx Unique index of the mount point
|
||||
*/
|
||||
#define VFS_AUTO_MOUNT(type, mtd, path, idx) \
|
||||
static type ## _desc_t fs_desc_ ## idx = mtd; \
|
||||
\
|
||||
XFA(vfs_mountpoints_xfa, 0) \
|
||||
vfs_mount_t _mount_mtd_ ## idx = { \
|
||||
.fs = &type ## _file_system, \
|
||||
.mount_point = path, \
|
||||
.private_data = &fs_desc_ ## idx, \
|
||||
}
|
||||
|
||||
/* Forward declarations */
|
||||
/**
|
||||
* @brief struct @c vfs_file_ops typedef
|
||||
@ -250,6 +282,11 @@ typedef struct vfs_file_system_ops vfs_file_system_ops_t;
|
||||
/* not struct vfs_mount because of name collision with the function */
|
||||
typedef struct vfs_mount_struct vfs_mount_t;
|
||||
|
||||
/**
|
||||
* @brief MTD driver for VFS
|
||||
*/
|
||||
extern const vfs_file_ops_t mtd_vfs_ops;
|
||||
|
||||
/**
|
||||
* @brief A file system driver
|
||||
*/
|
||||
|
@ -9,3 +9,12 @@ config MODULE_VFS
|
||||
bool "Virtual File System (VFS)"
|
||||
depends on TEST_KCONFIG
|
||||
select MODULE_POSIX_HEADERS
|
||||
|
||||
config MODULE_VFS_AUTO_MOUNT
|
||||
bool "Automatically mount configured file systems"
|
||||
depends on MODULE_VFS
|
||||
default y
|
||||
|
||||
config MODULE_VFS_AUTO_FORMAT
|
||||
bool "Automatically format configured file systems if mount fails"
|
||||
depends on MODULE_VFS
|
||||
|
@ -41,6 +41,16 @@
|
||||
#define DEBUG_NOT_STDOUT(...)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Automatic mountpoints
|
||||
*/
|
||||
XFA_INIT(vfs_mount_t, vfs_mountpoints_xfa);
|
||||
|
||||
/**
|
||||
* @brief Number of automatic mountpoints
|
||||
*/
|
||||
#define MOUNTPOINTS_NUMOF XFA_LEN(vfs_mount_t, vfs_mountpoints_xfa)
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @brief Array of all currently open files
|
||||
@ -1037,4 +1047,27 @@ int vfs_sysop_stat_from_fstat(vfs_mount_t *mountp, const char *restrict path, st
|
||||
return err;
|
||||
}
|
||||
|
||||
void auto_init_vfs(void)
|
||||
{
|
||||
for (unsigned i = 0; i < MOUNTPOINTS_NUMOF; ++i) {
|
||||
DEBUG("vfs%u: mounting as '%s'\n", i, vfs_mountpoints_xfa[i].mount_point);
|
||||
int res = vfs_mount(&vfs_mountpoints_xfa[i]);
|
||||
if (res) {
|
||||
if (IS_ACTIVE(MODULE_VFS_AUTO_FORMAT)) {
|
||||
DEBUG("vfs%u: formatting…\n", i);
|
||||
res = vfs_format(&vfs_mountpoints_xfa[i]);
|
||||
if (res) {
|
||||
DEBUG("vfs%u: format: error %d\n", i, res);
|
||||
continue;
|
||||
}
|
||||
res = vfs_mount(&vfs_mountpoints_xfa[i]);
|
||||
}
|
||||
|
||||
if (res) {
|
||||
DEBUG("vfs%u mount: error %d\n", i, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
@ -54,9 +54,7 @@ static const char test_txt[] = "the test file content 123 abc";
|
||||
static const char test_txt2[] = "another text";
|
||||
static const char test_txt3[] = "hello world for vfs";
|
||||
|
||||
static fatfs_desc_t fatfs = {
|
||||
.vol_idx = 0
|
||||
};
|
||||
static fatfs_desc_t fatfs;
|
||||
|
||||
static vfs_mount_t _test_vfs_mount = {
|
||||
.mount_point = MNT_PATH,
|
||||
@ -64,9 +62,6 @@ static vfs_mount_t _test_vfs_mount = {
|
||||
.private_data = (void *)&fatfs,
|
||||
};
|
||||
|
||||
/* provide mtd devices for use within diskio layer of fatfs */
|
||||
mtd_dev_t *fatfs_mtd_devs[FF_VOLUMES];
|
||||
|
||||
#if defined(MODULE_MTD_NATIVE) || defined(MODULE_MTD_MCI)
|
||||
/* mtd devices are provided in the board's board_init.c*/
|
||||
extern mtd_dev_t *mtd0;
|
||||
@ -402,17 +397,16 @@ int main(void)
|
||||
mtd_sdcard_devs[i].base.driver = &mtd_sdcard_driver;
|
||||
mtd_sdcard_devs[i].sd_card = &sdcard_spi_devs[i];
|
||||
mtd_sdcard_devs[i].params = &sdcard_spi_params[i];
|
||||
fatfs_mtd_devs[i] = &mtd_sdcard_devs[i].base;
|
||||
mtd_init(&mtd_sdcard_devs[i].base);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MODULE_MTD_NATIVE) || defined(MODULE_MTD_MCI)
|
||||
fatfs_mtd_devs[fatfs.vol_idx] = mtd0;
|
||||
fatfs.dev = mtd0;
|
||||
#endif
|
||||
|
||||
#if defined(MODULE_MTD_SDCARD)
|
||||
fatfs_mtd_devs[fatfs.vol_idx] = mtd_sdcard;
|
||||
fatfs.dev = mtd_sdcard;
|
||||
#endif
|
||||
|
||||
printf("Tests for FatFs over VFS - test results will be printed "
|
||||
|
Loading…
Reference in New Issue
Block a user