rt-thread/components/dfs/filesystems/elmfat/dfs_elm.c

1009 lines
23 KiB
C
Raw Normal View History

/*
* File : dfs_elm.c
* This file is part of Device File System in RT-Thread RTOS
* COPYRIGHT (C) 2008-2011, RT-Thread Development Team
*
2013-06-26 22:30:40 +08:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* 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 Bernard fixed the rename issue.
* 2012-07-26 aozima implement ff_memalloc and ff_memfree.
* 2012-12-19 Bernard fixed the O_APPEND and lseek issue.
* 2013-03-01 aozima fixed the stat(st_mtime) issue.
* 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 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
*/
2013-01-08 19:52:00 +08:00
#include <rtthread.h>
#include "ffconf.h"
#include "ff.h"
#include <string.h>
#include <time.h>
/* ELM FatFs provide a DIR struct */
#define HAVE_DIR_STRUCTURE
#include <dfs_fs.h>
#include <dfs_file.h>
2017-05-26 21:05:10 +08:00
static rt_device_t disk[_VOLUMES] = {0};
static int elm_result_to_dfs(FRESULT result)
{
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:
status = -ENOENT;
2013-01-08 19:52:00 +08:00
break;
case FR_INVALID_NAME:
status = -EINVAL;
2013-01-08 19:52:00 +08:00
break;
case FR_EXIST:
case FR_INVALID_OBJECT:
status = -EEXIST;
2013-01-08 19:52:00 +08:00
break;
case FR_DISK_ERR:
case FR_NOT_READY:
case FR_INT_ERR:
status = -EIO;
2013-01-08 19:52:00 +08:00
break;
case FR_WRITE_PROTECTED:
case FR_DENIED:
status = -EROFS;
2013-01-08 19:52:00 +08:00
break;
case FR_MKFS_ABORTED:
status = -EINVAL;
2013-01-08 19:52:00 +08:00
break;
default:
status = -1;
break;
}
return status;
}
/* 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
*/
static int get_disk(rt_device_t id)
{
int index;
2013-01-08 19:52:00 +08:00
for (index = 0; index < _VOLUMES; index ++)
{
if (disk[index] == id)
return index;
2013-01-08 19:52:00 +08:00
}
return -1;
}
int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
2013-01-08 19:52:00 +08:00
FATFS *fat;
FRESULT result;
int index;
struct rt_device_blk_geometry geometry;
2017-05-26 21:02:59 +08:00
char logic_nbr[2] = {'0',':'};
2013-01-08 19:52:00 +08:00
/* get an empty position */
index = get_disk(RT_NULL);
if (index == -1)
return -ENOENT;
2017-05-26 21:02:59 +08:00
logic_nbr[0] = '0' + index;
2013-01-08 19:52:00 +08:00
/* save device */
disk[index] = fs->dev_id;
/* check sector size */
if (rt_device_control(fs->dev_id, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry) == RT_EOK)
{
if (geometry.bytes_per_sector > _MAX_SS)
{
rt_kprintf("The sector size of device is greater than the sector size of FAT.\n");
return -EINVAL;
}
}
2013-01-08 19:52:00 +08:00
fat = (FATFS *)rt_malloc(sizeof(FATFS));
if (fat == RT_NULL)
{
disk[index] = RT_NULL;
return -ENOMEM;
2013-01-08 19:52:00 +08:00
}
/* mount fatfs, always 0 logic driver */
2017-06-15 14:36:59 +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)
{
2017-06-15 14:36:59 +08:00
f_mount(RT_NULL, (const TCHAR*)logic_nbr, 1);
disk[index] = RT_NULL;
rt_free(fat);
return -ENOMEM;
}
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)
goto __err;
/* mount succeed! */
2013-01-08 19:52:00 +08:00
fs->data = fat;
rt_free(dir);
return 0;
}
__err:
2017-05-26 21:02:59 +08:00
f_mount(RT_NULL, (const TCHAR*)logic_nbr, 1);
disk[index] = RT_NULL;
2013-01-08 19:52:00 +08:00
rt_free(fat);
return elm_result_to_dfs(result);
}
int dfs_elm_unmount(struct dfs_filesystem *fs)
{
2013-01-08 19:52:00 +08:00
FATFS *fat;
FRESULT result;
int index;
char logic_nbr[2] = {'0',':'};
2013-01-08 19:52:00 +08:00
fat = (FATFS *)fs->data;
2013-01-08 19:52:00 +08:00
RT_ASSERT(fat != RT_NULL);
2013-01-08 19:52:00 +08:00
/* find the device index and then umount it */
index = get_disk(fs->dev_id);
if (index == -1) /* not found */
return -ENOENT;
logic_nbr[0] = '0' + index;
result = f_mount(RT_NULL, logic_nbr, (BYTE)1);
2013-01-08 19:52:00 +08:00
if (result != FR_OK)
return elm_result_to_dfs(result);
2013-01-08 19:52:00 +08:00
fs->data = RT_NULL;
disk[index] = RT_NULL;
rt_free(fat);
return RT_EOK;
}
int dfs_elm_mkfs(rt_device_t dev_id)
{
#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;
int flag;
2013-01-08 19:52:00 +08:00
FRESULT result;
int index;
2018-03-18 17:22:22 +08:00
char logic_nbr[2] = {'0',':'};
2017-02-13 22:33:15 +08:00
work = rt_malloc(_MAX_SS);
if(RT_NULL == work) {
return -ENOMEM;
2017-02-13 22:33:15 +08:00
}
if (dev_id == RT_NULL)
{
rt_free(work); /* release memory */
return -EINVAL;
}
2013-01-08 19:52:00 +08:00
/* if the device is already mounted, then just do mkfs to the drv,
* 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)
{
/* 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");
rt_free(work); /* release memory */
return -ENOSPC;
}
else
{
fat = rt_malloc(sizeof(FATFS));
if (fat == RT_NULL)
{
rt_free(work); /* release memory */
return -ENOMEM;
}
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 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,
* 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);
}
}
2017-02-13 22:33:15 +08:00
/* [IN] Logical drive number */
/* [IN] Format options */
/* [IN] Size of the allocation unit */
/* [-] Working buffer */
/* [IN] Size of working buffer */
2018-03-18 17:22:22 +08:00
result = f_mkfs(logic_nbr, FM_ANY, 0, work, _MAX_SS);
rt_free(work); work = RT_NULL;
/* check flag status, we need clear the temp driver stored in disk[] */
if (flag == FSM_STATUS_USE_TEMP_DRIVER)
{
rt_free(fat);
2018-03-18 17:22:22 +08:00
f_mount(RT_NULL, logic_nbr,(BYTE)index);
disk[index] = RT_NULL;
2013-07-29 14:43:52 +08:00
/* close device */
rt_device_close(dev_id);
}
2013-01-08 19:52:00 +08:00
if (result != FR_OK)
{
rt_kprintf("format error\n");
return elm_result_to_dfs(result);
}
return RT_EOK;
}
int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
{
2013-01-08 19:52:00 +08:00
FATFS *f;
FRESULT res;
char driver[4];
DWORD fre_clust, fre_sect, tot_sect;
2013-01-08 19:52:00 +08:00
RT_ASSERT(fs != RT_NULL);
RT_ASSERT(buf != RT_NULL);
2013-01-08 19:52:00 +08:00
f = (FATFS *)fs->data;
2013-01-08 19:52:00 +08:00
rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
res = f_getfree(driver, &fre_clust, &f);
if (res)
return elm_result_to_dfs(res);
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;
2013-01-08 19:52:00 +08:00
buf->f_bfree = fre_sect;
buf->f_blocks = tot_sect;
#if _MAX_SS != 512
2013-01-08 19:52:00 +08:00
buf->f_bsize = f->ssize;
#else
buf->f_bsize = 512;
#endif
2013-01-08 19:52:00 +08:00
return 0;
}
int dfs_elm_open(struct dfs_fd *file)
{
2013-01-08 19:52:00 +08:00
FIL *fd;
BYTE mode;
FRESULT result;
char *drivers_fn;
#if (_VOLUMES > 1)
2013-01-08 19:52:00 +08:00
int vol;
struct dfs_filesystem *fs = (struct dfs_filesystem *)file->data;
2013-01-08 19:52:00 +08:00
extern int elm_get_vol(FATFS * fat);
if (fs == NULL)
return -ENOENT;
2013-01-08 19:52:00 +08:00
/* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data);
2013-01-08 19:52:00 +08:00
if (vol < 0)
return -ENOENT;
2013-01-08 19:52:00 +08:00
drivers_fn = rt_malloc(256);
if (drivers_fn == RT_NULL)
return -ENOMEM;
2013-01-08 19:52:00 +08:00
rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path);
#else
2013-01-08 19:52:00 +08:00
drivers_fn = file->path;
#endif
if (file->flags & O_DIRECTORY)
2013-01-08 19:52:00 +08:00
{
DIR *dir;
if (file->flags & O_CREAT)
2013-01-08 19:52:00 +08:00
{
result = f_mkdir(drivers_fn);
if (result != FR_OK)
{
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#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)
{
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#endif
return -ENOMEM;
2013-01-08 19:52:00 +08:00
}
2013-01-08 19:52:00 +08:00
result = f_opendir(dir, drivers_fn);
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#endif
2013-01-08 19:52:00 +08:00
if (result != FR_OK)
{
rt_free(dir);
return elm_result_to_dfs(result);
}
file->data = dir;
return RT_EOK;
2013-01-08 19:52:00 +08:00
}
else
{
mode = FA_READ;
if (file->flags & O_WRONLY)
2013-01-08 19:52:00 +08:00
mode |= FA_WRITE;
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. */
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. */
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. */
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)
{
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#endif
return -ENOMEM;
2013-01-08 19:52:00 +08:00
}
2013-01-08 19:52:00 +08:00
result = f_open(fd, drivers_fn, mode);
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#endif
2013-01-08 19:52:00 +08:00
if (result == FR_OK)
{
file->pos = fd->fptr;
2017-02-13 22:33:15 +08:00
file->size = f_size(fd);
2013-01-08 19:52:00 +08:00
file->data = fd;
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));
2013-01-08 19:52:00 +08:00
file->pos = fd->fptr;
}
}
else
{
/* open failed, return */
rt_free(fd);
return elm_result_to_dfs(result);
}
}
return RT_EOK;
}
int dfs_elm_close(struct dfs_fd *file)
{
2013-01-08 19:52:00 +08:00
FRESULT result;
result = FR_OK;
if (file->type == FT_DIRECTORY)
{
DIR *dir;
dir = (DIR *)(file->data);
RT_ASSERT(dir != RT_NULL);
/* release memory */
rt_free(dir);
}
else if (file->type == FT_REGULAR)
{
FIL *fd;
fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL);
result = f_close(fd);
if (result == FR_OK)
{
/* release memory */
rt_free(fd);
}
}
return elm_result_to_dfs(result);
}
2013-01-08 19:52:00 +08:00
int dfs_elm_ioctl(struct dfs_fd *file, int cmd, void *args)
{
return -ENOSYS;
}
int dfs_elm_read(struct dfs_fd *file, void *buf, size_t len)
{
2013-01-08 19:52:00 +08:00
FIL *fd;
FRESULT result;
UINT byte_read;
2013-01-08 19:52:00 +08:00
if (file->type == FT_DIRECTORY)
{
return -EISDIR;
2013-01-08 19:52:00 +08:00
}
2013-01-08 19:52:00 +08:00
fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL);
2013-01-08 19:52:00 +08:00
result = f_read(fd, buf, len, &byte_read);
/* update position */
file->pos = fd->fptr;
if (result == FR_OK)
return byte_read;
2013-01-08 19:52:00 +08:00
return elm_result_to_dfs(result);
}
int dfs_elm_write(struct dfs_fd *file, const void *buf, size_t len)
{
2013-01-08 19:52:00 +08:00
FIL *fd;
FRESULT result;
UINT byte_write;
if (file->type == FT_DIRECTORY)
{
return -EISDIR;
2013-01-08 19:52:00 +08:00
}
fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL);
result = f_write(fd, buf, len, &byte_write);
/* update position and file size */
file->pos = fd->fptr;
2017-02-13 22:33:15 +08:00
file->size = f_size(fd);
2013-01-08 19:52:00 +08:00
if (result == FR_OK)
return byte_write;
return elm_result_to_dfs(result);
}
int dfs_elm_flush(struct dfs_fd *file)
{
2013-01-08 19:52:00 +08:00
FIL *fd;
FRESULT result;
2013-01-08 19:52:00 +08:00
fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL);
2013-01-08 19:52:00 +08:00
result = f_sync(fd);
return elm_result_to_dfs(result);
}
int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
{
2013-01-08 19:52:00 +08:00
FRESULT result = FR_OK;
if (file->type == FT_REGULAR)
{
FIL *fd;
/* regular file type */
fd = (FIL *)(file->data);
RT_ASSERT(fd != RT_NULL);
result = f_lseek(fd, offset);
if (result == FR_OK)
{
/* return current position */
file->pos = fd->fptr;
return fd->fptr;
}
}
else if (file->type == FT_DIRECTORY)
{
/* which is a directory */
DIR *dir;
dir = (DIR *)(file->data);
RT_ASSERT(dir != RT_NULL);
result = f_seekdir(dir, offset / sizeof(struct dirent));
if (result == FR_OK)
{
/* update file position */
file->pos = offset;
return file->pos;
}
}
return elm_result_to_dfs(result);
}
int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
{
2013-01-08 19:52:00 +08:00
DIR *dir;
FILINFO fno;
FRESULT result;
rt_uint32_t index;
struct dirent *d;
dir = (DIR *)(file->data);
RT_ASSERT(dir != RT_NULL);
/* make integer count */
count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
if (count == 0)
return -EINVAL;
2013-01-08 19:52:00 +08:00
index = 0;
while (1)
{
char *fn;
2013-01-08 19:52:00 +08:00
d = dirp + index;
2013-01-08 19:52:00 +08:00
result = f_readdir(dir, &fno);
if (result != FR_OK || fno.fname[0] == 0)
break;
#if _USE_LFN
2017-02-13 22:33:15 +08:00
fn = *fno.fname ? fno.fname : fno.altname;
#else
2013-01-08 19:52:00 +08:00
fn = fno.fname;
#endif
d->d_type = DT_UNKNOWN;
2013-01-08 19:52:00 +08:00
if (fno.fattrib & AM_DIR)
d->d_type = DT_DIR;
2013-01-08 19:52:00 +08:00
else
d->d_type = DT_REG;
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);
rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);
2013-01-08 19:52:00 +08:00
index ++;
if (index * sizeof(struct dirent) >= count)
break;
}
2013-01-08 19:52:00 +08:00
if (index == 0)
return elm_result_to_dfs(result);
2013-01-08 19:52:00 +08:00
file->pos += index * sizeof(struct dirent);
2013-01-08 19:52:00 +08:00
return index * sizeof(struct dirent);
}
int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
{
2013-01-08 19:52:00 +08:00
FRESULT result;
#if _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 */
vol = elm_get_vol((FATFS *)fs->data);
if (vol < 0)
return -ENOENT;
2013-01-08 19:52:00 +08:00
drivers_fn = rt_malloc(256);
if (drivers_fn == RT_NULL)
return -ENOMEM;
2013-01-08 19:52:00 +08:00
rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
#else
2013-01-08 19:52:00 +08:00
const char *drivers_fn;
drivers_fn = path;
#endif
2013-01-08 19:52:00 +08:00
result = f_unlink(drivers_fn);
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#endif
2013-01-08 19:52:00 +08:00
return elm_result_to_dfs(result);
}
int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath)
{
2013-01-08 19:52:00 +08:00
FRESULT result;
#if _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 */
vol = elm_get_vol((FATFS *)fs->data);
if (vol < 0)
return -ENOENT;
2013-01-08 19:52:00 +08:00
drivers_oldfn = rt_malloc(256);
if (drivers_oldfn == RT_NULL)
return -ENOMEM;
2013-01-08 19:52:00 +08:00
drivers_newfn = newpath;
rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath);
#else
2013-01-08 19:52:00 +08:00
const char *drivers_oldfn, *drivers_newfn;
2013-01-08 19:52:00 +08:00
drivers_oldfn = oldpath;
drivers_newfn = newpath;
#endif
2013-01-08 19:52:00 +08:00
result = f_rename(drivers_oldfn, drivers_newfn);
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_oldfn);
#endif
2013-01-08 19:52:00 +08:00
return elm_result_to_dfs(result);
}
int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{
2013-01-08 19:52:00 +08:00
FILINFO file_info;
FRESULT result;
#if _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 */
vol = elm_get_vol((FATFS *)fs->data);
if (vol < 0)
return -ENOENT;
2013-01-08 19:52:00 +08:00
drivers_fn = rt_malloc(256);
if (drivers_fn == RT_NULL)
return -ENOMEM;
2013-01-08 19:52:00 +08:00
rt_snprintf(drivers_fn, 256, "%d:%s", vol, path);
#else
2013-01-08 19:52:00 +08:00
const char *drivers_fn;
drivers_fn = path;
#endif
2013-01-08 19:52:00 +08:00
result = f_stat(drivers_fn, &file_info);
#if _VOLUMES > 1
2013-01-08 19:52:00 +08:00
rt_free(drivers_fn);
#endif
2013-01-08 19:52:00 +08:00
if (result == FR_OK)
{
/* convert to dfs stat structure */
st->st_dev = 0;
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP | S_IWOTH;
2013-01-08 19:52:00 +08:00
if (file_info.fattrib & AM_DIR)
{
st->st_mode &= ~S_IFREG;
st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
2013-01-08 19:52:00 +08:00
}
if (file_info.fattrib & AM_RDO)
st->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
2013-01-08 19:52:00 +08:00
st->st_size = file_info.fsize;
/* 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) */
memset(&tm_file, 0, sizeof(tm_file));
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 */
st->st_mtime = mktime(&tm_file);
} /* get st_mtime. */
2013-01-08 19:52:00 +08:00
}
2013-01-08 19:52:00 +08:00
return elm_result_to_dfs(result);
}
static const struct dfs_file_ops dfs_elm_fops =
{
2013-01-08 19:52:00 +08:00
dfs_elm_open,
dfs_elm_close,
dfs_elm_ioctl,
dfs_elm_read,
dfs_elm_write,
dfs_elm_flush,
dfs_elm_lseek,
dfs_elm_getdents,
RT_NULL, /* poll interface */
};
static const struct dfs_filesystem_ops dfs_elm =
{
"elm",
DFS_FS_FLAG_DEFAULT,
&dfs_elm_fops,
dfs_elm_mount,
dfs_elm_unmount,
dfs_elm_mkfs,
dfs_elm_statfs,
2013-01-08 19:52:00 +08:00
dfs_elm_unlink,
dfs_elm_stat,
dfs_elm_rename,
};
int elm_init(void)
{
/* register fatfs file system */
dfs_register(&dfs_elm);
2013-01-08 19:52:00 +08:00
return 0;
}
INIT_COMPONENT_EXPORT(elm_init);
/*
* RT-Thread Device Interface for ELM FatFs
*/
#include "diskio.h"
/* Initialize a Drive */
DSTATUS disk_initialize(BYTE drv)
{
2013-01-08 19:52:00 +08:00
return 0;
}
/* Return Disk Status */
DSTATUS disk_status(BYTE drv)
{
2013-01-08 19:52:00 +08:00
return 0;
}
/* Read Sector(s) */
2017-02-13 22:33:15 +08:00
DRESULT disk_read (BYTE drv, BYTE* buff, DWORD sector, UINT count)
{
2013-01-08 19:52:00 +08:00
rt_size_t result;
rt_device_t device = disk[drv];
2013-01-08 19:52:00 +08:00
result = rt_device_read(device, sector, buff, count);
if (result == count)
{
return RES_OK;
}
2013-01-08 19:52:00 +08:00
return RES_ERROR;
}
/* Write Sector(s) */
2017-02-13 22:33:15 +08:00
DRESULT disk_write (BYTE drv, const BYTE* buff, DWORD sector, UINT count)
{
2013-01-08 19:52:00 +08:00
rt_size_t result;
rt_device_t device = disk[drv];
2013-01-08 19:52:00 +08:00
result = rt_device_write(device, sector, buff, count);
if (result == count)
{
return RES_OK;
}
2013-01-08 19:52:00 +08:00
return RES_ERROR;
}
/* Miscellaneous Functions */
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
{
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;
}
DWORD get_fattime(void)
{
time_t now;
struct tm *p_tm;
struct tm tm_now;
DWORD fat_time;
/* get current time */
now = time(RT_NULL);
/* lock scheduler. */
rt_enter_critical();
/* converts calendar time time into local time. */
p_tm = localtime(&now);
/* copy the statically located variable */
memcpy(&tm_now, p_tm, sizeof(struct tm));
/* unlock scheduler. */
rt_exit_critical();
2017-06-15 14:36:59 +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 ;
return fat_time;
}
#if _FS_REENTRANT
int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
{
char name[8];
rt_mutex_t mutex;
rt_snprintf(name, sizeof(name), "fat%d", drv);
mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
if (mutex != RT_NULL)
{
*m = mutex;
return RT_TRUE;
}
return RT_FALSE;
}
int ff_del_syncobj(_SYNC_t m)
{
if (m != RT_NULL)
rt_mutex_delete(m);
return RT_TRUE;
}
int ff_req_grant(_SYNC_t m)
{
if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
2013-01-08 19:52:00 +08:00
return RT_TRUE;
return RT_FALSE;
}
void ff_rel_grant(_SYNC_t m)
{
2013-01-08 19:52:00 +08:00
rt_mutex_release(m);
}
#endif
/* Memory functions */
#if _USE_LFN == 3
/* Allocate memory block */
2013-01-08 19:52:00 +08:00
void *ff_memalloc(UINT size)
{
return rt_malloc(size);
}
/* Free memory block */
2013-01-08 19:52:00 +08:00
void ff_memfree(void *mem)
{
rt_free(mem);
}
#endif /* _USE_LFN == 3 */