Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: enable custom mount points for fatfs #84194

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/fatfs/zephyr_fatfs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@
#define FF_USE_FIND 1
#endif /* defined(CONFIG_FS_FATFS_EXTRA_NATIVE_API) */

/*
* When custom mount points are activated we need to
* undef FF_VOLUME_STRS in order to be able to provide
* our own VolumeStr array.
*/
#if defined(CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS)
#if !defined(CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT)
#error Mount point count needs to be defined
#endif /* !defined(CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT) */
#undef FF_VOLUMES
#define FF_VOLUMES CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT
#undef FF_VOLUME_STRS
#endif /* defined(CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS) */

/*
* Options provided below have been added to ELM FAT source code to
* support Zephyr specific features, and are not part of ffconf.h.
Expand Down
51 changes: 26 additions & 25 deletions modules/fatfs/zfs_diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@
* under name zfs_diskio.c.
*/
#include <ff.h>
#include <diskio.h> /* FatFs lower layer API */
#include <diskio.h> /* FatFs lower layer API */
#include <zfs_diskio.h> /* Zephyr specific FatFS API */
#include <zephyr/storage/disk_access.h>

static const char * const pdrv_str[] = {FF_VOLUME_STRS};
#ifdef CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS
#define PDRV_STR_ARRAY VolumeStr
#else
static const char *const pdrv_str[] = {FF_VOLUME_STRS};
#define PDRV_STR_ARRAY pdrv_str
#endif /* CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS */

/* Get Drive Status */
DSTATUS disk_status(BYTE pdrv)
{
__ASSERT(pdrv < ARRAY_SIZE(pdrv_str), "pdrv out-of-range\n");
__ASSERT(pdrv < ARRAY_SIZE(PDRV_STR_ARRAY), "pdrv out-of-range\n");

if (disk_access_status(pdrv_str[pdrv]) != 0) {
if (disk_access_status(PDRV_STR_ARRAY[pdrv]) != 0) {
return STA_NOINIT;
} else {
return RES_OK;
Expand All @@ -41,22 +46,21 @@ DSTATUS disk_initialize(BYTE pdrv)
/* Read Sector(s) */
DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count)
{
__ASSERT(pdrv < ARRAY_SIZE(pdrv_str), "pdrv out-of-range\n");
__ASSERT(pdrv < ARRAY_SIZE(PDRV_STR_ARRAY), "pdrv out-of-range\n");

if (disk_access_read(pdrv_str[pdrv], buff, sector, count) != 0) {
if (disk_access_read(PDRV_STR_ARRAY[pdrv], buff, sector, count) != 0) {
return RES_ERROR;
} else {
return RES_OK;
}

}

/* Write Sector(s) */
DRESULT disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count)
{
__ASSERT(pdrv < ARRAY_SIZE(pdrv_str), "pdrv out-of-range\n");
__ASSERT(pdrv < ARRAY_SIZE(PDRV_STR_ARRAY), "pdrv out-of-range\n");

if (disk_access_write(pdrv_str[pdrv], buff, sector, count) != 0) {
if (disk_access_write(PDRV_STR_ARRAY[pdrv], buff, sector, count) != 0) {
return RES_ERROR;
} else {
return RES_OK;
Expand All @@ -69,19 +73,18 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
int ret = RES_OK;
uint32_t sector_size = 0;

__ASSERT(pdrv < ARRAY_SIZE(pdrv_str), "pdrv out-of-range\n");
__ASSERT(pdrv < ARRAY_SIZE(PDRV_STR_ARRAY), "pdrv out-of-range\n");

switch (cmd) {
case CTRL_SYNC:
if (disk_access_ioctl(pdrv_str[pdrv],
DISK_IOCTL_CTRL_SYNC, buff) != 0) {
if (disk_access_ioctl(PDRV_STR_ARRAY[pdrv], DISK_IOCTL_CTRL_SYNC, buff) != 0) {
ret = RES_ERROR;
}
break;

case GET_SECTOR_COUNT:
if (disk_access_ioctl(pdrv_str[pdrv],
DISK_IOCTL_GET_SECTOR_COUNT, buff) != 0) {
if (disk_access_ioctl(PDRV_STR_ARRAY[pdrv], DISK_IOCTL_GET_SECTOR_COUNT, buff) !=
0) {
ret = RES_ERROR;
}
break;
Expand All @@ -91,18 +94,18 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
* 32-bit number while FatFS's GET_SECTOR_SIZE is supposed to
* return a 16-bit number.
*/
if ((disk_access_ioctl(pdrv_str[pdrv],
DISK_IOCTL_GET_SECTOR_SIZE, &sector_size) == 0) &&
(sector_size == (uint16_t)sector_size)) {
if ((disk_access_ioctl(PDRV_STR_ARRAY[pdrv], DISK_IOCTL_GET_SECTOR_SIZE,
&sector_size) == 0) &&
(sector_size == (uint16_t)sector_size)) {
*(uint16_t *)buff = (uint16_t)sector_size;
} else {
ret = RES_ERROR;
}
break;

case GET_BLOCK_SIZE:
if (disk_access_ioctl(pdrv_str[pdrv],
DISK_IOCTL_GET_ERASE_BLOCK_SZ, buff) != 0) {
if (disk_access_ioctl(PDRV_STR_ARRAY[pdrv], DISK_IOCTL_GET_ERASE_BLOCK_SZ, buff) !=
0) {
ret = RES_ERROR;
}
break;
Expand All @@ -113,16 +116,14 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
case CTRL_POWER:
if (((*(uint8_t *)buff)) == DISK_IOCTL_POWER_OFF) {
/* Power disk off */
if (disk_access_ioctl(pdrv_str[pdrv],
DISK_IOCTL_CTRL_DEINIT,
NULL) != 0) {
if (disk_access_ioctl(PDRV_STR_ARRAY[pdrv], DISK_IOCTL_CTRL_DEINIT, NULL) !=
0) {
ret = RES_ERROR;
}
} else {
/* Power disk on */
if (disk_access_ioctl(pdrv_str[pdrv],
DISK_IOCTL_CTRL_INIT,
NULL) != 0) {
if (disk_access_ioctl(PDRV_STR_ARRAY[pdrv], DISK_IOCTL_CTRL_INIT, NULL) !=
0) {
ret = STA_NOINIT;
}
}
Expand Down
21 changes: 21 additions & 0 deletions subsys/fs/Kconfig.fatfs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,27 @@ config FS_FATFS_MULTI_PARTITION
physical drive number and only an FAT volume found on the physical drive
will be mounted.

config FS_FATFS_CUSTOM_MOUNT_POINT_COUNT
int "Count of custom mount points"
range 1 10
help
This option has to be used in combination with FS_FATFS_CUSTOM_MOUNT_POINTS.
It specifies how many custom mount points are defined in the custom mount
point string.

config FS_FATFS_CUSTOM_MOUNT_POINTS
string "Support for custom mountpoints when using fatfs"
help
This option allows to specify custom mount points where fatfs filesystems
can be mounted. The option has to be defined as a comma separated list
with no whitespaces and no trailing comma.
Example: "RAM,SD,SD2,NAND".
It is also necessary to define the count of mount points specified in the
list with the option FS_FATFS_CUSTOM_MOUNT_POINT_COUNT.
If this option is active no mount points not defined within the list can
be used for mounting fatfs filesystems anymore.
depends on FS_FATFS_CUSTOM_MOUNT_POINT_COUNT > 0

endmenu

endif # FAT_FILESYSTEM_ELM
23 changes: 22 additions & 1 deletion subsys/fs/fat_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,28 @@ static const struct fs_file_system_t fatfs_fs = {

static int fatfs_init(void)
{

#ifdef CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS
static char mount_points[] = CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS;
int mount_point_count = 0;

VolumeStr[0] = mount_points;
for (int i = 0; i < strlen(CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS); i++) {
if (mount_points[i] == ',') {
mount_points[i] = 0;
if (i == strlen(CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS) - 1) {
LOG_WRN("Trailing comma in custom mount point list. Fixing...");
continue;
}
mount_point_count++;
if (mount_point_count > CONFIG_FS_FATFS_CUSTOM_MOUNT_POINT_COUNT - 1) {
LOG_ERR("Custom mount point count not sufficient for defined mount "
"points.");
return -1;
}
VolumeStr[mount_point_count] = &mount_points[i + 1];
}
}
#endif /* CONFIG_FS_FATFS_CUSTOM_MOUNT_POINTS */
return fs_register(FS_FATFS, &fatfs_fs);
}

Expand Down
Loading