From 8b33da03f313c07da50b565ff737d53e0f1d8ccf Mon Sep 17 00:00:00 2001 From: Dmytro Semenets Date: Thu, 23 Mar 2023 17:38:41 +0200 Subject: [PATCH] fs: shell: littlefs: add support of block devices For flash device, littlefs should be located at the partition. Current implementation of the fs shell is hardcoded for the flash device and the compilation fails if STORAGE_PARTITION isn't defined in the device tree. Add options for block dev support. This allows to mount littlefs on the block device from the shell, and also removes the compile time dependency on STORAGE_PARTITION, if CONFIG_FS_LITTLEFS_BLK_DEV is set. Fix mounting of littlefs blk device. lfs_mount stucks in case when the read/prog buffer less than SDMMC block size, because it can cause memory corrupt, for example, look into function 'card_read_blocks' how it writes data to read buffer in case when buffer isn't aligned. With default config we have 32 bytes in the read buffer but trying to write 'block_size' to it and get memory corrupt, the same issue will be in case when the read buffer is aligned. This is an incremental improvement, ideally, someone should implement selection of the storage dev from the shell args. Co-authored-by: Mykola Kvach Signed-off-by: Dmytro Semenets --- subsys/fs/shell.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/subsys/fs/shell.c b/subsys/fs/shell.c index 2912334f7bf..28c7fbd4ff2 100644 --- a/subsys/fs/shell.c +++ b/subsys/fs/shell.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,33 @@ static struct fs_mount_t fatfs_mnt = { /* LITTLEFS */ #ifdef CONFIG_FILE_SYSTEM_LITTLEFS #include + +/* TODO: Implement dynamic storage dev selection */ +#ifdef CONFIG_FS_LITTLEFS_BLK_DEV + +#if defined(CONFIG_DISK_DRIVER_SDMMC) +#define DISK_NAME CONFIG_SDMMC_VOLUME_NAME +#elif defined(CONFIG_DISK_DRIVER_MMC) +#define DISK_NAME CONFIG_MMC_VOLUME_NAME +#else +#error "No disk device defined, is your board supported?" +#endif + +FS_LITTLEFS_DECLARE_CUSTOM_CONFIG( + lfs_data, + CONFIG_SDHC_BUFFER_ALIGNMENT, + SDMMC_DEFAULT_BLOCK_SIZE, + SDMMC_DEFAULT_BLOCK_SIZE, + SDMMC_DEFAULT_BLOCK_SIZE, + 2 * SDMMC_DEFAULT_BLOCK_SIZE); + +static struct fs_mount_t littlefs_mnt = { + .type = FS_LITTLEFS, + .fs_data = &lfs_data, + .flags = FS_MOUNT_FLAG_USE_DISK_ACCESS, + .storage_dev = DISK_NAME, +}; +#else #include FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(lfs_data); @@ -43,6 +71,7 @@ static struct fs_mount_t littlefs_mnt = { .storage_dev = (void *)STORAGE_PARTITION_ID, }; #endif +#endif #define BUF_CNT 64