2011-10-08 21:25:21 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2011-10-08 21:25:21 +08:00
|
|
|
*
|
2018-10-15 01:46:07 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-10-08 21:25:21 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2008-02-22 QiuYi The first version.
|
|
|
|
* 2011-10-08 Bernard fixed the block size in statfs.
|
2011-11-23 06:32:56 +08:00
|
|
|
* 2011-11-23 Bernard fixed the rename issue.
|
2012-07-26 00:41:30 +08:00
|
|
|
* 2012-07-26 aozima implement ff_memalloc and ff_memfree.
|
2012-12-19 09:57:43 +08:00
|
|
|
* 2012-12-19 Bernard fixed the O_APPEND and lseek issue.
|
2013-03-01 12:23:28 +08:00
|
|
|
* 2013-03-01 aozima fixed the stat(st_mtime) issue.
|
2014-01-26 22:43:12 +08:00
|
|
|
* 2014-01-26 Bernard Check the sector size before mount.
|
2017-02-13 22:33:15 +08:00
|
|
|
* 2017-02-13 Hichard Update Fatfs version to 0.12b, support exFAT.
|
2017-04-11 09:33:25 +08:00
|
|
|
* 2017-04-11 Bernard fix the st_blksize issue.
|
2017-05-26 21:02:59 +08:00
|
|
|
* 2017-05-26 Urey fix f_mount error when mount more fats
|
2011-10-08 21:25:21 +08:00
|
|
|
*/
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2011-10-08 21:25:21 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include "ffconf.h"
|
|
|
|
#include "ff.h"
|
2013-03-02 18:56:07 +08:00
|
|
|
#include <string.h>
|
2021-02-07 21:08:41 +08:00
|
|
|
#include <sys/time.h>
|
2011-10-08 21:25:21 +08:00
|
|
|
|
|
|
|
/* ELM FatFs provide a DIR struct */
|
|
|
|
#define HAVE_DIR_STRUCTURE
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
#include <dfs.h>
|
2011-10-08 21:25:21 +08:00
|
|
|
#include <dfs_fs.h>
|
2023-06-10 21:35:25 +08:00
|
|
|
#include <dfs_dentry.h>
|
2017-10-15 22:44:53 +08:00
|
|
|
#include <dfs_file.h>
|
2023-06-10 21:35:25 +08:00
|
|
|
#include <dfs_mnt.h>
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
#include "dfs_pcache.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static int dfs_elm_free_vnode(struct dfs_vnode *vnode);
|
|
|
|
|
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
static ssize_t dfs_elm_page_read(struct dfs_file *file, struct dfs_page *page);
|
|
|
|
static ssize_t dfs_elm_page_write(struct dfs_page *page);
|
|
|
|
|
|
|
|
static struct dfs_aspace_ops dfs_elm_aspace_ops =
|
|
|
|
{
|
|
|
|
.read = dfs_elm_page_read,
|
|
|
|
.write = dfs_elm_page_write,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-03-25 07:34:00 +08:00
|
|
|
#undef SS
|
|
|
|
#if FF_MAX_SS == FF_MIN_SS
|
|
|
|
#define SS(fs) ((UINT)FF_MAX_SS) /* Fixed sector size */
|
|
|
|
#else
|
|
|
|
#define SS(fs) ((fs)->ssize) /* Variable sector size */
|
|
|
|
#endif
|
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
static rt_device_t disk[FF_VOLUMES] = {0};
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_elm_unmount(struct dfs_mnt *mnt);
|
|
|
|
|
2011-10-08 21:25:21 +08:00
|
|
|
static int elm_result_to_dfs(FRESULT result)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
int status = RT_EOK;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case FR_OK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FR_NO_FILE:
|
|
|
|
case FR_NO_PATH:
|
|
|
|
case FR_NO_FILESYSTEM:
|
2017-10-15 22:44:53 +08:00
|
|
|
status = -ENOENT;
|
2013-01-08 19:52:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FR_INVALID_NAME:
|
2017-10-15 22:44:53 +08:00
|
|
|
status = -EINVAL;
|
2013-01-08 19:52:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FR_EXIST:
|
|
|
|
case FR_INVALID_OBJECT:
|
2017-10-15 22:44:53 +08:00
|
|
|
status = -EEXIST;
|
2013-01-08 19:52:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FR_DISK_ERR:
|
|
|
|
case FR_NOT_READY:
|
|
|
|
case FR_INT_ERR:
|
2017-10-15 22:44:53 +08:00
|
|
|
status = -EIO;
|
2013-01-08 19:52:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FR_WRITE_PROTECTED:
|
|
|
|
case FR_DENIED:
|
2017-10-15 22:44:53 +08:00
|
|
|
status = -EROFS;
|
2013-01-08 19:52:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FR_MKFS_ABORTED:
|
2017-10-15 22:44:53 +08:00
|
|
|
status = -EINVAL;
|
2013-01-08 19:52:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 18:58:45 +08:00
|
|
|
/* results:
|
2013-01-08 19:52:00 +08:00
|
|
|
* -1, no space to install fatfs driver
|
|
|
|
* >= 0, there is an space to install fatfs driver
|
2013-01-08 18:58:45 +08:00
|
|
|
*/
|
|
|
|
static int get_disk(rt_device_t id)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 18:58:45 +08:00
|
|
|
int index;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
for (index = 0; index < FF_VOLUMES; index ++)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
if (disk[index] == id)
|
2013-01-08 18:58:45 +08:00
|
|
|
return index;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 18:58:45 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
static int dfs_elm_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void *data)
|
2013-01-08 18:58:45 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FATFS *fat;
|
|
|
|
FRESULT result;
|
|
|
|
int index;
|
2017-04-11 09:33:25 +08:00
|
|
|
struct rt_device_blk_geometry geometry;
|
2020-06-12 10:34:35 +08:00
|
|
|
char logic_nbr[3] = {'0',':', 0};
|
2013-01-08 18:58:45 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
/* open device, but do not check the status of device */
|
|
|
|
if (mnt->dev_id == RT_NULL
|
|
|
|
|| rt_device_open(mnt->dev_id, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* get an empty position */
|
|
|
|
index = get_disk(RT_NULL);
|
|
|
|
if (index == -1)
|
2023-06-10 21:35:25 +08:00
|
|
|
{
|
|
|
|
rt_device_close(mnt->dev_id);
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2023-06-10 21:35:25 +08:00
|
|
|
}
|
2017-05-26 21:02:59 +08:00
|
|
|
logic_nbr[0] = '0' + index;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* save device */
|
2023-06-10 21:35:25 +08:00
|
|
|
disk[index] = mnt->dev_id;
|
2017-04-11 09:33:25 +08:00
|
|
|
/* check sector size */
|
2023-06-10 21:35:25 +08:00
|
|
|
if (rt_device_control(mnt->dev_id, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry) == RT_EOK)
|
2017-04-11 09:33:25 +08:00
|
|
|
{
|
2020-06-12 10:34:35 +08:00
|
|
|
if (geometry.bytes_per_sector > FF_MAX_SS)
|
2017-04-11 09:33:25 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("The sector size of device is greater than the sector size of FAT.\n");
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_device_close(mnt->dev_id);
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2017-04-11 09:33:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
fat = (FATFS *)rt_malloc(sizeof(FATFS));
|
|
|
|
if (fat == RT_NULL)
|
|
|
|
{
|
2013-01-08 18:58:45 +08:00
|
|
|
disk[index] = RT_NULL;
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_device_close(mnt->dev_id);
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mount fatfs, always 0 logic driver */
|
2019-04-03 18:09:52 +08:00
|
|
|
result = f_mount(fat, (const TCHAR *)logic_nbr, 1);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result == FR_OK)
|
|
|
|
{
|
|
|
|
char drive[8];
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
rt_snprintf(drive, sizeof(drive), "%d:/", index);
|
|
|
|
dir = (DIR *)rt_malloc(sizeof(DIR));
|
|
|
|
if (dir == RT_NULL)
|
2013-01-22 23:47:32 +08:00
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
f_mount(RT_NULL, (const TCHAR *)logic_nbr, 1);
|
2013-01-22 23:47:32 +08:00
|
|
|
disk[index] = RT_NULL;
|
|
|
|
rt_free(fat);
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_device_close(mnt->dev_id);
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-22 23:47:32 +08:00
|
|
|
}
|
2013-01-08 19:52:00 +08:00
|
|
|
|
|
|
|
/* open the root directory to test whether the fatfs is valid */
|
|
|
|
result = f_opendir(dir, drive);
|
|
|
|
if (result != FR_OK)
|
2013-01-08 18:58:45 +08:00
|
|
|
goto __err;
|
2012-08-08 23:46:48 +08:00
|
|
|
|
2013-01-08 18:58:45 +08:00
|
|
|
/* mount succeed! */
|
2023-06-10 21:35:25 +08:00
|
|
|
mnt->data = fat;
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(dir);
|
2023-06-10 21:35:25 +08:00
|
|
|
return RT_EOK;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 18:58:45 +08:00
|
|
|
__err:
|
2019-04-03 18:09:52 +08:00
|
|
|
f_mount(RT_NULL, (const TCHAR *)logic_nbr, 1);
|
2013-01-08 18:58:45 +08:00
|
|
|
disk[index] = RT_NULL;
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(fat);
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_device_close(mnt->dev_id);
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_elm_unmount(struct dfs_mnt *mnt)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FATFS *fat;
|
|
|
|
FRESULT result;
|
|
|
|
int index;
|
2020-06-12 10:34:35 +08:00
|
|
|
char logic_nbr[3] = {'0',':', 0};
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
fat = (FATFS *)mnt->data;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(fat != RT_NULL);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* find the device index and then umount it */
|
2023-06-10 21:35:25 +08:00
|
|
|
index = get_disk(mnt->dev_id);
|
2013-01-08 18:58:45 +08:00
|
|
|
if (index == -1) /* not found */
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2018-01-24 15:13:42 +08:00
|
|
|
logic_nbr[0] = '0' + index;
|
2020-06-12 10:34:35 +08:00
|
|
|
result = f_mount(RT_NULL, logic_nbr, (BYTE)0);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result != FR_OK)
|
|
|
|
return elm_result_to_dfs(result);
|
2013-01-08 18:58:45 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
mnt->data = RT_NULL;
|
2013-01-08 19:52:00 +08:00
|
|
|
disk[index] = RT_NULL;
|
|
|
|
rt_free(fat);
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_device_close(mnt->dev_id);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
int dfs_elm_mkfs(rt_device_t dev_id, const char *fs_name)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 18:58:45 +08:00
|
|
|
#define FSM_STATUS_INIT 0
|
|
|
|
#define FSM_STATUS_USE_TEMP_DRIVER 1
|
2013-03-01 12:34:01 +08:00
|
|
|
FATFS *fat = RT_NULL;
|
2017-02-13 22:33:15 +08:00
|
|
|
BYTE *work;
|
2013-01-08 18:58:45 +08:00
|
|
|
int flag;
|
2013-01-08 19:52:00 +08:00
|
|
|
FRESULT result;
|
2013-01-08 18:58:45 +08:00
|
|
|
int index;
|
2020-06-12 10:34:35 +08:00
|
|
|
char logic_nbr[3] = {'0',':', 0};
|
|
|
|
MKFS_PARM opt;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
work = rt_malloc(FF_MAX_SS);
|
|
|
|
if(RT_NULL == work) {
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2017-02-13 22:33:15 +08:00
|
|
|
}
|
2017-04-11 09:33:25 +08:00
|
|
|
|
2013-01-08 18:58:45 +08:00
|
|
|
if (dev_id == RT_NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
|
|
|
rt_free(work); /* release memory */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* if the device is already mounted, then just do mkfs to the drv,
|
2013-01-08 18:58:45 +08:00
|
|
|
* while if it is not mounted yet, then find an empty drive to do mkfs
|
|
|
|
*/
|
|
|
|
|
|
|
|
flag = FSM_STATUS_INIT;
|
|
|
|
index = get_disk(dev_id);
|
|
|
|
if (index == -1)
|
|
|
|
{
|
|
|
|
/* not found the device id */
|
|
|
|
index = get_disk(RT_NULL);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (index == -1)
|
|
|
|
{
|
2013-01-08 18:58:45 +08:00
|
|
|
/* no space to store an temp driver */
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_kprintf("sorry, there is no space to do mkfs! \n");
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_free(work); /* release memory */
|
|
|
|
return -ENOSPC;
|
2013-01-08 18:58:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-18 20:09:19 +08:00
|
|
|
fat = (FATFS *)rt_malloc(sizeof(FATFS));
|
2013-01-08 18:58:45 +08:00
|
|
|
if (fat == RT_NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
|
|
|
rt_free(work); /* release memory */
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2013-01-08 18:58:45 +08:00
|
|
|
|
|
|
|
flag = FSM_STATUS_USE_TEMP_DRIVER;
|
|
|
|
|
|
|
|
disk[index] = dev_id;
|
2013-07-29 14:43:52 +08:00
|
|
|
/* try to open device */
|
|
|
|
rt_device_open(dev_id, RT_DEVICE_OFLAG_RDWR);
|
2013-01-08 18:58:45 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* just fill the FatFs[vol] in ff.c, or mkfs will failded!
|
|
|
|
* consider this condition: you just umount the elm fat,
|
|
|
|
* then the space in FatFs[index] is released, and now do mkfs
|
|
|
|
* on the disk, you will get a failure. so we need f_mount here,
|
2013-01-08 18:58:45 +08:00
|
|
|
* just fill the FatFS[index] in elm fatfs to make mkfs work.
|
|
|
|
*/
|
2018-03-18 17:22:22 +08:00
|
|
|
logic_nbr[0] = '0' + index;
|
|
|
|
f_mount(fat, logic_nbr, (BYTE)index);
|
2013-01-08 18:58:45 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-28 16:34:51 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
logic_nbr[0] = '0' + index;
|
|
|
|
}
|
2013-01-08 18:58:45 +08:00
|
|
|
|
2017-02-13 22:33:15 +08:00
|
|
|
/* [IN] Logical drive number */
|
|
|
|
/* [IN] Format options */
|
|
|
|
/* [-] Working buffer */
|
|
|
|
/* [IN] Size of working buffer */
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&opt, 0, sizeof(opt));
|
2020-06-12 10:34:35 +08:00
|
|
|
opt.fmt = FM_ANY|FM_SFD;
|
|
|
|
result = f_mkfs(logic_nbr, &opt, work, FF_MAX_SS);
|
|
|
|
rt_free(work); work = RT_NULL;
|
2013-01-08 18:58:45 +08:00
|
|
|
|
|
|
|
/* check flag status, we need clear the temp driver stored in disk[] */
|
|
|
|
if (flag == FSM_STATUS_USE_TEMP_DRIVER)
|
|
|
|
{
|
|
|
|
rt_free(fat);
|
2019-04-03 18:09:52 +08:00
|
|
|
f_mount(RT_NULL, logic_nbr, (BYTE)index);
|
2013-01-08 18:58:45 +08:00
|
|
|
disk[index] = RT_NULL;
|
2013-07-29 14:43:52 +08:00
|
|
|
/* close device */
|
|
|
|
rt_device_close(dev_id);
|
2013-01-08 18:58:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result != FR_OK)
|
|
|
|
{
|
2020-06-12 10:34:35 +08:00
|
|
|
rt_kprintf("format error, result=%d\n", result);
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_elm_statfs(struct dfs_mnt *mnt, struct statfs *buf)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FATFS *f;
|
|
|
|
FRESULT res;
|
|
|
|
char driver[4];
|
|
|
|
DWORD fre_clust, fre_sect, tot_sect;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
RT_ASSERT(mnt != RT_NULL);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(buf != RT_NULL);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
f = (FATFS *)mnt->data;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
rt_snprintf(driver, sizeof(driver), "%d:", f->pdrv);
|
2013-01-08 19:52:00 +08:00
|
|
|
res = f_getfree(driver, &fre_clust, &f);
|
|
|
|
if (res)
|
|
|
|
return elm_result_to_dfs(res);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* Get total sectors and free sectors */
|
|
|
|
tot_sect = (f->n_fatent - 2) * f->csize;
|
|
|
|
fre_sect = fre_clust * f->csize;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
buf->f_bfree = fre_sect;
|
|
|
|
buf->f_blocks = tot_sect;
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_MAX_SS != 512
|
2013-01-08 19:52:00 +08:00
|
|
|
buf->f_bsize = f->ssize;
|
2011-10-08 21:25:21 +08:00
|
|
|
#else
|
|
|
|
buf->f_bsize = 512;
|
|
|
|
#endif
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return 0;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_elm_open(struct dfs_file *file)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FIL *fd;
|
|
|
|
BYTE mode;
|
|
|
|
FRESULT result;
|
|
|
|
char *drivers_fn;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
#if (FF_VOLUMES > 1)
|
2013-01-08 19:52:00 +08:00
|
|
|
int vol;
|
2023-06-10 21:35:25 +08:00
|
|
|
struct dfs_mnt *mnt = file->vnode->mnt;
|
2013-01-08 19:52:00 +08:00
|
|
|
extern int elm_get_vol(FATFS * fat);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
2023-10-17 13:07:59 +08:00
|
|
|
if (file->vnode->data)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
if (file->vnode->type == FT_DIRECTORY
|
|
|
|
&& !(file->flags & O_DIRECTORY))
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
if (mnt == NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
/* add path for ELM FatFS driver support */
|
2023-06-10 21:35:25 +08:00
|
|
|
vol = elm_get_vol((FATFS *)mnt->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (vol < 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2019-06-18 20:09:19 +08:00
|
|
|
drivers_fn = (char *)rt_malloc(256);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (drivers_fn == RT_NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->dentry->pathname);
|
2011-10-08 21:25:21 +08:00
|
|
|
#else
|
2023-06-10 21:35:25 +08:00
|
|
|
drivers_fn = file->dentry->pathname;
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_DIRECTORY)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
DIR *dir;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_CREAT)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
result = f_mkdir(drivers_fn);
|
|
|
|
if (result != FR_OK)
|
|
|
|
{
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open directory */
|
|
|
|
dir = (DIR *)rt_malloc(sizeof(DIR));
|
|
|
|
if (dir == RT_NULL)
|
|
|
|
{
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_opendir(dir, drivers_fn);
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result != FR_OK)
|
|
|
|
{
|
|
|
|
rt_free(dir);
|
|
|
|
return elm_result_to_dfs(result);
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
file->vnode->data = dir;
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_init(&file->vnode->lock, file->dentry->pathname, RT_IPC_FLAG_PRIO);
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode = FA_READ;
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_WRONLY)
|
2013-01-08 19:52:00 +08:00
|
|
|
mode |= FA_WRITE;
|
2017-10-15 22:44:53 +08:00
|
|
|
if ((file->flags & O_ACCMODE) & O_RDWR)
|
2013-01-08 19:52:00 +08:00
|
|
|
mode |= FA_WRITE;
|
|
|
|
/* Opens the file, if it is existing. If not, a new file is created. */
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_CREAT)
|
2013-01-08 19:52:00 +08:00
|
|
|
mode |= FA_OPEN_ALWAYS;
|
|
|
|
/* Creates a new file. If the file is existing, it is truncated and overwritten. */
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_TRUNC)
|
2013-01-08 19:52:00 +08:00
|
|
|
mode |= FA_CREATE_ALWAYS;
|
|
|
|
/* Creates a new file. The function fails if the file is already existing. */
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_EXCL)
|
2013-01-08 19:52:00 +08:00
|
|
|
mode |= FA_CREATE_NEW;
|
|
|
|
|
|
|
|
/* allocate a fd */
|
|
|
|
fd = (FIL *)rt_malloc(sizeof(FIL));
|
|
|
|
if (fd == RT_NULL)
|
|
|
|
{
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2012-09-17 10:33:18 +08:00
|
|
|
#endif
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_open(fd, drivers_fn, mode);
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result == FR_OK)
|
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos = fd->fptr;
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->size = f_size(fd);
|
|
|
|
file->vnode->type = FT_REGULAR;
|
2023-06-10 21:35:25 +08:00
|
|
|
file->vnode->data = fd;
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_init(&file->vnode->lock, file->dentry->pathname, RT_IPC_FLAG_PRIO);
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (file->flags & O_APPEND)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
/* seek to the end of file */
|
2017-02-13 22:33:15 +08:00
|
|
|
f_lseek(fd, f_size(fd));
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos = fd->fptr;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* open failed, return */
|
|
|
|
rt_free(fd);
|
|
|
|
return elm_result_to_dfs(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_elm_close(struct dfs_file *file)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FRESULT result;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-01-08 19:52:00 +08:00
|
|
|
result = FR_OK;
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
DIR *dir = RT_NULL;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
dir = (DIR *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(dir != RT_NULL);
|
|
|
|
|
|
|
|
/* release memory */
|
|
|
|
rt_free(dir);
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
else if (file->vnode->type == FT_REGULAR)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
FIL *fd = RT_NULL;
|
2023-08-02 13:59:43 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
fd = (FIL *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(fd != RT_NULL);
|
|
|
|
|
2023-08-02 13:59:43 +08:00
|
|
|
f_close(fd);
|
|
|
|
/* release memory */
|
|
|
|
rt_free(fd);
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
file->vnode->data = RT_NULL;
|
|
|
|
rt_mutex_detach(&file->vnode->lock);
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_elm_ioctl(struct dfs_file *file, int cmd, void *args)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2019-09-26 10:22:33 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_FIOFTRUNCATE:
|
|
|
|
{
|
|
|
|
FIL *fd;
|
|
|
|
FSIZE_t fptr, length;
|
|
|
|
FRESULT result = FR_OK;
|
2023-06-10 21:35:25 +08:00
|
|
|
fd = (FIL *)(file->vnode->data);
|
2019-09-26 10:22:33 +08:00
|
|
|
RT_ASSERT(fd != RT_NULL);
|
|
|
|
|
|
|
|
/* save file read/write point */
|
|
|
|
fptr = fd->fptr;
|
|
|
|
length = *(off_t*)args;
|
|
|
|
if (length <= fd->obj.objsize)
|
|
|
|
{
|
|
|
|
fd->fptr = length;
|
|
|
|
result = f_truncate(fd);
|
2019-10-08 09:11:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-26 10:22:33 +08:00
|
|
|
result = f_lseek(fd, length);
|
|
|
|
}
|
|
|
|
/* restore file read/write point */
|
|
|
|
fd->fptr = fptr;
|
|
|
|
return elm_result_to_dfs(result);
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
case F_GETLK:
|
|
|
|
return 0;
|
|
|
|
case F_SETLK:
|
|
|
|
return 0;
|
2019-09-26 10:22:33 +08:00
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOSYS;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
ssize_t dfs_elm_read(struct dfs_file *file, void *buf, size_t len, off_t *pos)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FIL *fd;
|
2023-10-17 13:07:59 +08:00
|
|
|
FRESULT result = FR_OK;
|
2013-01-08 19:52:00 +08:00
|
|
|
UINT byte_read;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EISDIR;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
if (file->vnode->size > *pos)
|
|
|
|
{
|
|
|
|
fd = (FIL *)(file->vnode->data);
|
|
|
|
RT_ASSERT(fd != RT_NULL);
|
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
f_lseek(fd, *pos);
|
|
|
|
result = f_read(fd, buf, len, &byte_read);
|
|
|
|
/* update position */
|
|
|
|
*pos = fd->fptr;
|
|
|
|
rt_mutex_release(&file->vnode->lock);
|
|
|
|
if (result == FR_OK)
|
|
|
|
return byte_read;
|
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
ssize_t dfs_elm_write(struct dfs_file *file, const void *buf, size_t len, off_t *pos)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FIL *fd;
|
|
|
|
FRESULT result;
|
|
|
|
UINT byte_write;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_DIRECTORY)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EISDIR;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
fd = (FIL *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(fd != RT_NULL);
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
f_lseek(fd, *pos);
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_write(fd, buf, len, &byte_write);
|
|
|
|
/* update position and file size */
|
2023-08-02 13:59:43 +08:00
|
|
|
*pos = fd->fptr;
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->size = f_size(fd);
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result == FR_OK)
|
|
|
|
return byte_write;
|
|
|
|
|
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_elm_flush(struct dfs_file *file)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FIL *fd;
|
|
|
|
FRESULT result;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
fd = (FIL *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(fd != RT_NULL);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_sync(fd);
|
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
off_t dfs_elm_lseek(struct dfs_file *file, off_t offset, int wherece)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FRESULT result = FR_OK;
|
2023-08-02 13:59:43 +08:00
|
|
|
|
|
|
|
switch (wherece)
|
|
|
|
{
|
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset += file->fpos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
offset += file->vnode->size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->vnode->type == FT_REGULAR)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
FIL *fd;
|
|
|
|
|
|
|
|
/* regular file type */
|
2023-06-10 21:35:25 +08:00
|
|
|
fd = (FIL *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(fd != RT_NULL);
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_lseek(fd, offset);
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result == FR_OK)
|
|
|
|
{
|
|
|
|
/* return current position */
|
|
|
|
return fd->fptr;
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
else if (file->vnode->type == FT_DIRECTORY)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
/* which is a directory */
|
2022-12-03 12:07:44 +08:00
|
|
|
DIR *dir = RT_NULL;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
dir = (DIR *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(dir != RT_NULL);
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_seekdir(dir, offset / sizeof(struct dirent));
|
2023-10-17 13:07:59 +08:00
|
|
|
rt_mutex_release(&file->vnode->lock);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result == FR_OK)
|
|
|
|
{
|
|
|
|
/* update file position */
|
2023-08-02 13:59:43 +08:00
|
|
|
return offset;
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_elm_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
DIR *dir;
|
|
|
|
FILINFO fno;
|
|
|
|
FRESULT result;
|
|
|
|
rt_uint32_t index;
|
|
|
|
struct dirent *d;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
dir = (DIR *)(file->vnode->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
RT_ASSERT(dir != RT_NULL);
|
|
|
|
|
|
|
|
/* make integer count */
|
|
|
|
count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
|
|
|
|
if (count == 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
index = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
char *fn;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
d = dirp + index;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_readdir(dir, &fno);
|
|
|
|
if (result != FR_OK || fno.fname[0] == 0)
|
|
|
|
break;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_USE_LFN
|
2017-02-13 22:33:15 +08:00
|
|
|
fn = *fno.fname ? fno.fname : fno.altname;
|
2011-10-08 21:25:21 +08:00
|
|
|
#else
|
2013-01-08 19:52:00 +08:00
|
|
|
fn = fno.fname;
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
d->d_type = DT_UNKNOWN;
|
2013-01-08 19:52:00 +08:00
|
|
|
if (fno.fattrib & AM_DIR)
|
2017-10-15 22:44:53 +08:00
|
|
|
d->d_type = DT_DIR;
|
2013-01-08 19:52:00 +08:00
|
|
|
else
|
2017-10-15 22:44:53 +08:00
|
|
|
d->d_type = DT_REG;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
d->d_namlen = (rt_uint8_t)rt_strlen(fn);
|
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
2023-11-28 17:16:21 +08:00
|
|
|
rt_strncpy(d->d_name, fn, DIRENT_NAME_MAX);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
index ++;
|
|
|
|
if (index * sizeof(struct dirent) >= count)
|
|
|
|
break;
|
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
if (index == 0)
|
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
file->fpos += index * sizeof(struct dirent);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return index * sizeof(struct dirent);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_elm_unlink(struct dfs_dentry *dentry)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FRESULT result;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
int vol;
|
|
|
|
char *drivers_fn;
|
|
|
|
extern int elm_get_vol(FATFS * fat);
|
|
|
|
|
|
|
|
/* add path for ELM FatFS driver support */
|
2023-06-10 21:35:25 +08:00
|
|
|
vol = elm_get_vol((FATFS *)dentry->mnt->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (vol < 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2019-06-18 20:09:19 +08:00
|
|
|
drivers_fn = (char *)rt_malloc(256);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (drivers_fn == RT_NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_snprintf(drivers_fn, 256, "%d:%s", vol, dentry->pathname);
|
2011-10-08 21:25:21 +08:00
|
|
|
#else
|
2013-01-08 19:52:00 +08:00
|
|
|
const char *drivers_fn;
|
|
|
|
drivers_fn = path;
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_unlink(drivers_fn);
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_elm_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *new_dentry)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
FRESULT result;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
char *drivers_oldfn;
|
|
|
|
const char *drivers_newfn;
|
|
|
|
int vol;
|
|
|
|
extern int elm_get_vol(FATFS * fat);
|
|
|
|
|
|
|
|
/* add path for ELM FatFS driver support */
|
2023-06-10 21:35:25 +08:00
|
|
|
vol = elm_get_vol((FATFS *)old_dentry->mnt->data);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (vol < 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2019-06-18 20:09:19 +08:00
|
|
|
drivers_oldfn = (char *)rt_malloc(256);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (drivers_oldfn == RT_NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2023-06-10 21:35:25 +08:00
|
|
|
drivers_newfn = new_dentry->pathname;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, old_dentry->pathname);
|
2011-10-08 21:25:21 +08:00
|
|
|
#else
|
2013-01-08 19:52:00 +08:00
|
|
|
const char *drivers_oldfn, *drivers_newfn;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
drivers_oldfn = old_dentry->pathname;
|
|
|
|
drivers_newfn = new_dentry->pathname;
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_rename(drivers_oldfn, drivers_newfn);
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_oldfn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
int dfs_elm_stat(struct dfs_dentry *dentry, struct stat *st)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
FATFS *fat;
|
2013-01-08 19:52:00 +08:00
|
|
|
FILINFO file_info;
|
|
|
|
FRESULT result;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
fat = (FATFS *)dentry->mnt->data;
|
2023-03-25 07:34:00 +08:00
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
int vol;
|
|
|
|
char *drivers_fn;
|
|
|
|
extern int elm_get_vol(FATFS * fat);
|
|
|
|
|
|
|
|
/* add path for ELM FatFS driver support */
|
2023-06-10 21:35:25 +08:00
|
|
|
vol = elm_get_vol(fat);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (vol < 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2019-06-18 20:09:19 +08:00
|
|
|
drivers_fn = (char *)rt_malloc(256);
|
2013-01-08 19:52:00 +08:00
|
|
|
if (drivers_fn == RT_NULL)
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOMEM;
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
rt_snprintf(drivers_fn, 256, "%d:%s", vol, dentry->pathname);
|
2011-10-08 21:25:21 +08:00
|
|
|
#else
|
2013-01-08 19:52:00 +08:00
|
|
|
const char *drivers_fn;
|
2023-06-10 21:35:25 +08:00
|
|
|
drivers_fn = dentry->pathname;
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = f_stat(drivers_fn, &file_info);
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_VOLUMES > 1
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_free(drivers_fn);
|
2011-10-08 21:25:21 +08:00
|
|
|
#endif
|
2013-01-08 19:52:00 +08:00
|
|
|
if (result == FR_OK)
|
|
|
|
{
|
|
|
|
/* convert to dfs stat structure */
|
2023-06-10 21:35:25 +08:00
|
|
|
st->st_dev = (dev_t)(size_t)(dentry->mnt->dev_id);
|
|
|
|
st->st_ino = (ino_t)dfs_dentry_full_path_crc32(dentry);
|
2013-01-08 19:52:00 +08:00
|
|
|
|
|
|
|
if (file_info.fattrib & AM_DIR)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
st->st_mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st->st_mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
if (file_info.fattrib & AM_RDO)
|
2017-10-15 22:44:53 +08:00
|
|
|
st->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
|
2013-01-08 19:52:00 +08:00
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
if (S_IFDIR & st->st_mode)
|
|
|
|
{
|
|
|
|
st->st_size = file_info.fsize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
st->st_size = (dentry->vnode && dentry->vnode->aspace) ? dentry->vnode->size : file_info.fsize;
|
|
|
|
#else
|
|
|
|
st->st_size = file_info.fsize;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
st->st_blksize = fat->csize * SS(fat);
|
2023-03-25 07:34:00 +08:00
|
|
|
if (file_info.fattrib & AM_ARC)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
st->st_blocks = st->st_size ? ((st->st_size - 1) / SS(fat) / fat->csize + 1) : 0;
|
2023-03-25 07:34:00 +08:00
|
|
|
st->st_blocks *= (st->st_blksize / 512); // man say st_blocks is number of 512B blocks allocated
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
st->st_blocks = fat->csize;
|
2023-03-25 07:34:00 +08:00
|
|
|
}
|
2013-03-01 12:23:28 +08:00
|
|
|
/* get st_mtime. */
|
|
|
|
{
|
|
|
|
struct tm tm_file;
|
|
|
|
int year, mon, day, hour, min, sec;
|
|
|
|
WORD tmp;
|
|
|
|
|
|
|
|
tmp = file_info.fdate;
|
|
|
|
day = tmp & 0x1F; /* bit[4:0] Day(1..31) */
|
|
|
|
tmp >>= 5;
|
|
|
|
mon = tmp & 0x0F; /* bit[8:5] Month(1..12) */
|
|
|
|
tmp >>= 4;
|
|
|
|
year = (tmp & 0x7F) + 1980; /* bit[15:9] Year origin from 1980(0..127) */
|
|
|
|
|
|
|
|
tmp = file_info.ftime;
|
|
|
|
sec = (tmp & 0x1F) * 2; /* bit[4:0] Second/2(0..29) */
|
|
|
|
tmp >>= 5;
|
|
|
|
min = tmp & 0x3F; /* bit[10:5] Minute(0..59) */
|
|
|
|
tmp >>= 6;
|
|
|
|
hour = tmp & 0x1F; /* bit[15:11] Hour(0..23) */
|
|
|
|
|
2021-12-31 08:38:20 +08:00
|
|
|
rt_memset(&tm_file, 0, sizeof(tm_file));
|
2013-03-01 12:23:28 +08:00
|
|
|
tm_file.tm_year = year - 1900; /* Years since 1900 */
|
|
|
|
tm_file.tm_mon = mon - 1; /* Months *since* january: 0-11 */
|
|
|
|
tm_file.tm_mday = day; /* Day of the month: 1-31 */
|
|
|
|
tm_file.tm_hour = hour; /* Hours since midnight: 0-23 */
|
|
|
|
tm_file.tm_min = min; /* Minutes: 0-59 */
|
|
|
|
tm_file.tm_sec = sec; /* Seconds: 0-59 */
|
|
|
|
|
2021-02-07 21:08:41 +08:00
|
|
|
st->st_mtime = timegm(&tm_file);
|
2013-03-01 12:23:28 +08:00
|
|
|
} /* get st_mtime. */
|
2013-01-08 19:52:00 +08:00
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return elm_result_to_dfs(result);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
static struct dfs_vnode *dfs_elm_lookup(struct dfs_dentry *dentry)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
struct dfs_vnode *vnode = RT_NULL;
|
|
|
|
|
|
|
|
if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dfs_elm_stat(dentry, &st) != 0)
|
|
|
|
{
|
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
vnode = dfs_vnode_create();
|
|
|
|
if (vnode)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mnt = dentry->mnt;
|
|
|
|
vnode->size = st.st_size;
|
|
|
|
vnode->data = NULL;
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_REGULAR;
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_elm_aspace_ops);
|
|
|
|
#endif
|
2023-06-10 21:35:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dfs_vnode *dfs_elm_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
|
|
|
|
{
|
|
|
|
struct dfs_vnode *vnode = RT_NULL;
|
|
|
|
|
|
|
|
if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vnode = dfs_vnode_create();
|
|
|
|
if (vnode)
|
|
|
|
{
|
|
|
|
if (type == FT_DIRECTORY)
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
/* fat directory force mode 0555 */
|
|
|
|
vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_DIRECTORY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-17 13:07:59 +08:00
|
|
|
/* fat REGULAR file mode force mode 0777 */
|
|
|
|
vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
|
2023-06-10 21:35:25 +08:00
|
|
|
vnode->type = FT_REGULAR;
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_elm_aspace_ops);
|
|
|
|
#endif
|
2023-06-10 21:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
vnode->mnt = dentry->mnt;
|
|
|
|
vnode->data = NULL;
|
|
|
|
vnode->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dfs_elm_free_vnode(struct dfs_vnode *vnode)
|
|
|
|
{
|
|
|
|
/* nothing to be freed */
|
|
|
|
if (vnode && vnode->ref_count <= 1)
|
|
|
|
{
|
|
|
|
vnode->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
#ifdef RT_USING_PAGECACHE
|
|
|
|
static ssize_t dfs_elm_page_read(struct dfs_file *file, struct dfs_page *page)
|
|
|
|
{
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
if (page->page)
|
|
|
|
{
|
|
|
|
off_t fpos = page->fpos;
|
|
|
|
ret = dfs_elm_read(file, page->page, page->size, &fpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t dfs_elm_page_write(struct dfs_page *page)
|
|
|
|
{
|
|
|
|
FIL *fd;
|
|
|
|
FRESULT result;
|
|
|
|
UINT byte_write;
|
|
|
|
|
|
|
|
if (page->aspace->vnode->type == FT_DIRECTORY)
|
|
|
|
{
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = (FIL *)(page->aspace->vnode->data);
|
|
|
|
RT_ASSERT(fd != RT_NULL);
|
|
|
|
rt_mutex_take(&page->aspace->vnode->lock, RT_WAITING_FOREVER);
|
|
|
|
f_lseek(fd, page->fpos);
|
|
|
|
result = f_write(fd, page->page, page->len, &byte_write);
|
|
|
|
rt_mutex_release(&page->aspace->vnode->lock);
|
|
|
|
if (result == FR_OK)
|
|
|
|
{
|
|
|
|
return byte_write;
|
|
|
|
}
|
|
|
|
|
|
|
|
return elm_result_to_dfs(result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static const struct dfs_file_ops dfs_elm_fops =
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2023-06-10 21:35:25 +08:00
|
|
|
.open = dfs_elm_open,
|
|
|
|
.close = dfs_elm_close,
|
|
|
|
.ioctl = dfs_elm_ioctl,
|
|
|
|
.read = dfs_elm_read,
|
|
|
|
.write = dfs_elm_write,
|
|
|
|
.flush = dfs_elm_flush,
|
|
|
|
.lseek = dfs_elm_lseek,
|
|
|
|
.getdents = dfs_elm_getdents,
|
2017-10-15 22:44:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dfs_filesystem_ops dfs_elm =
|
|
|
|
{
|
|
|
|
"elm",
|
2023-06-10 21:35:25 +08:00
|
|
|
FS_NEED_DEVICE,
|
2017-10-15 22:44:53 +08:00
|
|
|
&dfs_elm_fops,
|
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
.mount = dfs_elm_mount,
|
|
|
|
.umount = dfs_elm_unmount,
|
|
|
|
.mkfs = dfs_elm_mkfs,
|
|
|
|
.statfs = dfs_elm_statfs,
|
|
|
|
|
|
|
|
.unlink = dfs_elm_unlink,
|
|
|
|
.stat = dfs_elm_stat,
|
|
|
|
.rename = dfs_elm_rename,
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2023-06-10 21:35:25 +08:00
|
|
|
.lookup = dfs_elm_lookup,
|
|
|
|
.create_vnode = dfs_elm_create_vnode,
|
|
|
|
.free_vnode = dfs_elm_free_vnode
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct dfs_filesystem_type _elmfs =
|
|
|
|
{
|
|
|
|
.fs_ops = &dfs_elm,
|
2011-10-08 21:25:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int elm_init(void)
|
|
|
|
{
|
|
|
|
/* register fatfs file system */
|
2023-06-10 21:35:25 +08:00
|
|
|
dfs_register(&_elmfs);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return 0;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
2017-11-03 11:44:37 +08:00
|
|
|
INIT_COMPONENT_EXPORT(elm_init);
|
2011-10-08 21:25:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* RT-Thread Device Interface for ELM FatFs
|
|
|
|
*/
|
|
|
|
#include "diskio.h"
|
|
|
|
|
2011-12-31 12:05:40 +08:00
|
|
|
/* Initialize a Drive */
|
2011-12-23 10:11:55 +08:00
|
|
|
DSTATUS disk_initialize(BYTE drv)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
return 0;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return Disk Status */
|
2011-12-23 10:11:55 +08:00
|
|
|
DSTATUS disk_status(BYTE drv)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
return 0;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read Sector(s) */
|
2019-04-03 18:09:52 +08:00
|
|
|
DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, UINT count)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_size_t result;
|
|
|
|
rt_device_t device = disk[drv];
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = rt_device_read(device, sector, buff, count);
|
|
|
|
if (result == count)
|
|
|
|
{
|
|
|
|
return RES_OK;
|
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return RES_ERROR;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write Sector(s) */
|
2019-04-03 18:09:52 +08:00
|
|
|
DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, UINT count)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_size_t result;
|
|
|
|
rt_device_t device = disk[drv];
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
result = rt_device_write(device, sector, buff, count);
|
|
|
|
if (result == count)
|
|
|
|
{
|
|
|
|
return RES_OK;
|
|
|
|
}
|
2011-10-08 21:25:21 +08:00
|
|
|
|
2013-01-08 19:52:00 +08:00
|
|
|
return RES_ERROR;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Miscellaneous Functions */
|
2011-12-23 10:11:55 +08:00
|
|
|
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2013-01-08 19:52:00 +08:00
|
|
|
rt_device_t device = disk[drv];
|
|
|
|
|
|
|
|
if (device == RT_NULL)
|
|
|
|
return RES_ERROR;
|
|
|
|
|
|
|
|
if (ctrl == GET_SECTOR_COUNT)
|
|
|
|
{
|
|
|
|
struct rt_device_blk_geometry geometry;
|
|
|
|
|
|
|
|
rt_memset(&geometry, 0, sizeof(geometry));
|
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
|
|
|
|
|
|
|
|
*(DWORD *)buff = geometry.sector_count;
|
|
|
|
if (geometry.sector_count == 0)
|
|
|
|
return RES_ERROR;
|
|
|
|
}
|
|
|
|
else if (ctrl == GET_SECTOR_SIZE)
|
|
|
|
{
|
|
|
|
struct rt_device_blk_geometry geometry;
|
|
|
|
|
|
|
|
rt_memset(&geometry, 0, sizeof(geometry));
|
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
|
|
|
|
|
|
|
|
*(WORD *)buff = (WORD)(geometry.bytes_per_sector);
|
|
|
|
}
|
|
|
|
else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) */
|
|
|
|
{
|
|
|
|
struct rt_device_blk_geometry geometry;
|
|
|
|
|
|
|
|
rt_memset(&geometry, 0, sizeof(geometry));
|
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
|
|
|
|
|
|
|
|
*(DWORD *)buff = geometry.block_size / geometry.bytes_per_sector;
|
|
|
|
}
|
|
|
|
else if (ctrl == CTRL_SYNC)
|
|
|
|
{
|
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_BLK_SYNC, RT_NULL);
|
|
|
|
}
|
2017-02-13 22:33:15 +08:00
|
|
|
else if (ctrl == CTRL_TRIM)
|
2013-01-08 19:52:00 +08:00
|
|
|
{
|
|
|
|
rt_device_control(device, RT_DEVICE_CTRL_BLK_ERASE, buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RES_OK;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2017-04-17 07:13:15 +08:00
|
|
|
DWORD get_fattime(void)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2018-06-11 19:56:13 +08:00
|
|
|
DWORD fat_time = 0;
|
2017-04-17 07:13:15 +08:00
|
|
|
time_t now;
|
|
|
|
struct tm tm_now;
|
|
|
|
|
|
|
|
now = time(RT_NULL);
|
2022-06-22 13:41:06 +08:00
|
|
|
gmtime_r(&now, &tm_now);
|
2017-04-17 07:13:15 +08:00
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
fat_time = (DWORD)(tm_now.tm_year - 80) << 25 |
|
|
|
|
(DWORD)(tm_now.tm_mon + 1) << 21 |
|
|
|
|
(DWORD)tm_now.tm_mday << 16 |
|
|
|
|
(DWORD)tm_now.tm_hour << 11 |
|
|
|
|
(DWORD)tm_now.tm_min << 5 |
|
|
|
|
(DWORD)tm_now.tm_sec / 2 ;
|
2017-04-17 07:13:15 +08:00
|
|
|
|
|
|
|
return fat_time;
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_FS_REENTRANT
|
2023-10-26 17:26:48 +08:00
|
|
|
|
|
|
|
static rt_mutex_t Mutex[FF_VOLUMES + 1];
|
|
|
|
|
|
|
|
int ff_mutex_create (int vol)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
|
|
|
char name[8];
|
|
|
|
rt_mutex_t mutex;
|
|
|
|
|
2023-10-26 17:26:48 +08:00
|
|
|
rt_snprintf(name, sizeof(name), "fat%d", vol);
|
2021-11-18 04:57:15 +08:00
|
|
|
mutex = rt_mutex_create(name, RT_IPC_FLAG_PRIO);
|
2011-10-08 21:25:21 +08:00
|
|
|
if (mutex != RT_NULL)
|
|
|
|
{
|
2023-10-26 17:26:48 +08:00
|
|
|
Mutex[vol] = mutex;
|
2011-10-08 21:25:21 +08:00
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
2023-10-26 17:26:48 +08:00
|
|
|
void ff_mutex_delete (int vol)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2023-10-26 17:26:48 +08:00
|
|
|
if (Mutex[vol] != RT_NULL)
|
|
|
|
rt_mutex_delete(Mutex[vol]);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
2023-10-26 17:26:48 +08:00
|
|
|
int ff_mutex_take (int vol)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2023-10-26 17:26:48 +08:00
|
|
|
if (rt_mutex_take(Mutex[vol], FF_FS_TIMEOUT) == RT_EOK)
|
2013-01-08 19:52:00 +08:00
|
|
|
return RT_TRUE;
|
2011-10-08 21:25:21 +08:00
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
2023-10-26 17:26:48 +08:00
|
|
|
void ff_mutex_give (int vol)
|
2011-10-08 21:25:21 +08:00
|
|
|
{
|
2023-10-26 17:26:48 +08:00
|
|
|
rt_mutex_release(Mutex[vol]);
|
2011-10-08 21:25:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-07-26 00:41:30 +08:00
|
|
|
|
|
|
|
/* Memory functions */
|
2020-06-12 10:34:35 +08:00
|
|
|
#if FF_USE_LFN == 3
|
2012-07-26 00:41:30 +08:00
|
|
|
/* Allocate memory block */
|
2013-01-08 19:52:00 +08:00
|
|
|
void *ff_memalloc(UINT size)
|
2012-07-26 00:41:30 +08:00
|
|
|
{
|
|
|
|
return rt_malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free memory block */
|
2013-01-08 19:52:00 +08:00
|
|
|
void ff_memfree(void *mem)
|
2012-07-26 00:41:30 +08:00
|
|
|
{
|
|
|
|
rt_free(mem);
|
|
|
|
}
|
2020-06-12 10:34:35 +08:00
|
|
|
#endif /* FF_USE_LFN == 3 */
|
2012-07-26 00:41:30 +08:00
|
|
|
|