2013-01-08 20:30:57 +08:00
|
|
|
/*
|
2018-10-15 01:46:07 +08:00
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
2013-01-08 20:30:57 +08:00
|
|
|
*
|
2018-10-15 01:46:07 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-01-08 20:30:57 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2011-10-22 prife the first version
|
|
|
|
* 2012-03-28 prife use mtd device interface
|
|
|
|
* 2012-04-05 prife update uffs with official repo and use uffs_UnMount/Mount
|
2017-04-12 14:32:12 +08:00
|
|
|
* 2017-04-12 lizhen9880 fix the f_bsize and f_blocks issue in function dfs_uffs_statfs
|
2013-06-26 22:30:40 +08:00
|
|
|
*/
|
|
|
|
|
2013-01-08 20:30:57 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
#include <dfs_fs.h>
|
2017-10-25 16:23:02 +08:00
|
|
|
#include <dfs_file.h>
|
2013-01-08 20:30:57 +08:00
|
|
|
#include <rtdevice.h>
|
2017-10-25 16:23:02 +08:00
|
|
|
|
2013-01-08 20:30:57 +08:00
|
|
|
#include "dfs_uffs.h"
|
|
|
|
|
|
|
|
#include "uffs/uffs_fd.h" /* posix file api is here */
|
|
|
|
#include "uffs/uffs_mtb.h"
|
|
|
|
#include "uffs/uffs_mem.h"
|
|
|
|
#include "uffs/uffs_utils.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RT-Thread DFS Interface for uffs
|
|
|
|
*/
|
2017-10-25 16:23:02 +08:00
|
|
|
#define UFFS_DEVICE_MAX 2 /* the max partions on a nand deivce*/
|
|
|
|
#define UFFS_MOUNT_PATH_MAX 128 /* the mount point max length */
|
|
|
|
#define FILE_PATH_MAX 256 /* the longest file path */
|
2013-01-08 20:30:57 +08:00
|
|
|
|
|
|
|
struct _nand_dev
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
struct rt_mtd_nand_device *dev;
|
2017-10-25 16:23:02 +08:00
|
|
|
struct uffs_StorageAttrSt storage;
|
|
|
|
uffs_Device uffs_dev;
|
|
|
|
uffs_MountTable mount_table;
|
|
|
|
char mount_path[UFFS_MOUNT_PATH_MAX];
|
2019-04-03 18:09:52 +08:00
|
|
|
void *data; /* when uffs use static buf, it will save ptr here */
|
2013-01-08 20:30:57 +08:00
|
|
|
};
|
|
|
|
/* make sure the following struct var had been initilased to 0! */
|
|
|
|
static struct _nand_dev nand_part[UFFS_DEVICE_MAX] = {0};
|
|
|
|
|
|
|
|
static int uffs_result_to_dfs(int result)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int status = -1;
|
|
|
|
|
|
|
|
result = result < 0 ? -result : result;
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case UENOERR:/** no error */
|
|
|
|
break;
|
|
|
|
case UEACCES:/** Tried to open read-only file for writing, or files sharing mode
|
|
|
|
does not allow specified operations, or given path is directory */
|
|
|
|
status = -EINVAL;
|
|
|
|
break;/* no suitable */
|
|
|
|
case UEEXIST: /** _O_CREAT and _O_EXCL flags specified, but filename already exists */
|
|
|
|
status = -EEXIST;
|
|
|
|
break;
|
|
|
|
case UEINVAL: /** Invalid oflag or pmode argument */
|
|
|
|
status = -EINVAL;
|
|
|
|
break;
|
|
|
|
case UEMFILE: /** No more file handles available(too many open files) */
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
case UENOENT: /** file or path not found */
|
|
|
|
status = -ENOENT;
|
|
|
|
break;
|
|
|
|
case UETIME: /** can't set file time */
|
|
|
|
status = -1;
|
|
|
|
break;
|
|
|
|
case UEBADF: /** invalid file handle */
|
|
|
|
status = -EBADF;
|
|
|
|
break;
|
|
|
|
case UENOMEM:/** no enough memory */
|
|
|
|
status = -ENOSPC;
|
|
|
|
break;
|
|
|
|
case UEIOERR: /** I/O error from lower level flash operation */
|
|
|
|
status = -EIO;
|
|
|
|
break;
|
|
|
|
case UENOTDIR: /** Not a directory */
|
|
|
|
status = -ENOTDIR;
|
|
|
|
break;
|
|
|
|
case UEISDIR: /** Is a directory */
|
|
|
|
status = -EISDIR;
|
|
|
|
break;
|
|
|
|
case UEUNKNOWN_ERR:
|
|
|
|
default:
|
|
|
|
status = -1;
|
|
|
|
break; /* unknown error! */
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static URET _device_init(uffs_Device *dev)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
dev->attr->_private = NULL; // hook nand_chip data structure to attr->_private
|
|
|
|
dev->ops = (struct uffs_FlashOpsSt *)&nand_ops;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
return U_SUCC;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static URET _device_release(uffs_Device *dev)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
return U_SUCC;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int init_uffs_fs(
|
2019-04-03 18:09:52 +08:00
|
|
|
struct _nand_dev *nand_part)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
uffs_MountTable *mtb;
|
|
|
|
struct rt_mtd_nand_device *nand;
|
|
|
|
struct uffs_StorageAttrSt *flash_storage;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
|
|
|
mtb = &nand_part->mount_table;
|
|
|
|
nand = nand_part->dev;
|
|
|
|
flash_storage = &nand_part->storage;
|
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* setup nand storage attributes */
|
|
|
|
uffs_setup_storage(flash_storage, nand);
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* register mount table */
|
2019-04-03 18:09:52 +08:00
|
|
|
if (mtb->dev)
|
2017-10-25 16:23:02 +08:00
|
|
|
{
|
|
|
|
/* set memory allocator for uffs */
|
2013-01-08 20:30:57 +08:00
|
|
|
#if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
|
2017-10-25 16:23:02 +08:00
|
|
|
uffs_MemSetupSystemAllocator(&mtb->dev->mem);
|
2013-01-08 20:30:57 +08:00
|
|
|
#endif
|
2017-10-25 16:23:02 +08:00
|
|
|
/* setup device init/release entry */
|
|
|
|
mtb->dev->Init = _device_init;
|
|
|
|
mtb->dev->Release = _device_release;
|
|
|
|
mtb->dev->attr = flash_storage;
|
|
|
|
|
|
|
|
uffs_RegisterMountTable(mtb);
|
|
|
|
}
|
|
|
|
/* mount uffs partion on nand device */
|
|
|
|
return uffs_Mount(nand_part->mount_path) == U_SUCC ? 0 : -1;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dfs_uffs_mount(
|
2019-04-03 18:09:52 +08:00
|
|
|
struct dfs_filesystem *fs,
|
2013-01-08 20:30:57 +08:00
|
|
|
unsigned long rwflag,
|
2019-04-03 18:09:52 +08:00
|
|
|
const void *data)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
rt_base_t index;
|
2019-04-03 18:09:52 +08:00
|
|
|
uffs_MountTable *mount_part;
|
|
|
|
struct rt_mtd_nand_device *dev;
|
|
|
|
|
|
|
|
RT_ASSERT(rt_strlen(fs->path) < (UFFS_MOUNT_PATH_MAX - 1));
|
2017-10-25 16:23:02 +08:00
|
|
|
dev = RT_MTD_NAND_DEVICE(fs->dev_id);
|
|
|
|
|
|
|
|
/*1. find a empty entry in partition table */
|
|
|
|
for (index = 0; index < UFFS_DEVICE_MAX ; index ++)
|
|
|
|
{
|
|
|
|
if (nand_part[index].dev == RT_NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (index == UFFS_DEVICE_MAX)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
/*2. fill partition structure */
|
|
|
|
nand_part[index].dev = dev;
|
|
|
|
|
|
|
|
/* make a right mount path for uffs, end with '/' */
|
|
|
|
rt_snprintf(nand_part[index].mount_path, UFFS_MOUNT_PATH_MAX, "%s/", fs->path);
|
|
|
|
if (nand_part[index].mount_path[1] == '/')
|
|
|
|
nand_part[index].mount_path[1] = 0;
|
|
|
|
|
|
|
|
mount_part = &(nand_part[index].mount_table);
|
|
|
|
mount_part->mount = nand_part[index].mount_path;
|
|
|
|
mount_part->dev = &(nand_part[index].uffs_dev);
|
|
|
|
rt_memset(mount_part->dev, 0, sizeof(uffs_Device));//in order to make uffs happy.
|
|
|
|
mount_part->dev->_private = dev; /* save dev_id into uffs */
|
|
|
|
mount_part->start_block = dev->block_start;
|
|
|
|
mount_part->end_block = dev->block_end;
|
|
|
|
/*3. mount uffs */
|
|
|
|
if (init_uffs_fs(&nand_part[index]) < 0)
|
|
|
|
{
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
}
|
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_unmount(struct dfs_filesystem *fs)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
rt_base_t index;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
/* find the device index and then unmount it */
|
|
|
|
for (index = 0; index < UFFS_DEVICE_MAX; index++)
|
|
|
|
{
|
|
|
|
if (nand_part[index].dev == RT_MTD_NAND_DEVICE(fs->dev_id))
|
|
|
|
{
|
|
|
|
nand_part[index].dev = RT_NULL;
|
|
|
|
result = uffs_UnMount(nand_part[index].mount_path);
|
|
|
|
if (result != U_SUCC)
|
|
|
|
break;
|
|
|
|
|
|
|
|
result = uffs_UnRegisterMountTable(& nand_part[index].mount_table);
|
|
|
|
return (result == U_SUCC) ? RT_EOK : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dfs_uffs_mkfs(rt_device_t dev_id)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
rt_base_t index;
|
|
|
|
rt_uint32_t block;
|
2019-04-03 18:09:52 +08:00
|
|
|
struct rt_mtd_nand_device *mtd;
|
2017-10-25 16:23:02 +08:00
|
|
|
|
|
|
|
/*1. find the device index */
|
|
|
|
for (index = 0; index < UFFS_DEVICE_MAX; index++)
|
|
|
|
{
|
|
|
|
if (nand_part[index].dev == (struct rt_mtd_nand_device *)dev_id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == UFFS_DEVICE_MAX)
|
|
|
|
{
|
|
|
|
/* can't find device driver */
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*2. then unmount the partition */
|
|
|
|
uffs_Mount(nand_part[index].mount_path);
|
|
|
|
mtd = nand_part[index].dev;
|
|
|
|
|
|
|
|
/*3. erase all blocks on the partition */
|
|
|
|
block = mtd->block_start;
|
|
|
|
for (; block <= mtd->block_end; block++)
|
|
|
|
{
|
|
|
|
rt_mtd_nand_erase_block(mtd, block);
|
|
|
|
if (rt_mtd_nand_check_block(mtd, block) != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("found bad block %d\n", block);
|
|
|
|
rt_mtd_nand_mark_badblock(mtd, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*4. remount it */
|
|
|
|
if (init_uffs_fs(&nand_part[index]) < 0)
|
|
|
|
{
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_statfs(struct dfs_filesystem *fs,
|
|
|
|
struct statfs *buf)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
rt_base_t index;
|
2019-04-03 18:09:52 +08:00
|
|
|
struct rt_mtd_nand_device *mtd = RT_MTD_NAND_DEVICE(fs->dev_id);
|
2017-10-25 16:23:02 +08:00
|
|
|
|
|
|
|
RT_ASSERT(mtd != RT_NULL);
|
|
|
|
|
|
|
|
/* find the device index */
|
|
|
|
for (index = 0; index < UFFS_DEVICE_MAX; index++)
|
|
|
|
{
|
|
|
|
if (nand_part[index].dev == (void *)mtd)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (index == UFFS_DEVICE_MAX)
|
|
|
|
return -ENOENT;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
|
|
|
buf->f_bsize = mtd->page_size * mtd->pages_per_block;
|
2017-10-25 16:23:02 +08:00
|
|
|
buf->f_blocks = (mtd->block_end - mtd->block_start + 1);
|
2019-04-03 18:09:52 +08:00
|
|
|
buf->f_bfree = uffs_GetDeviceFree(&nand_part[index].uffs_dev) / buf->f_bsize ;
|
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_open(struct dfs_fd *file)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int fd;
|
|
|
|
int oflag, mode;
|
2019-04-03 18:09:52 +08:00
|
|
|
char *file_path;
|
2017-10-25 16:23:02 +08:00
|
|
|
|
|
|
|
oflag = file->flags;
|
|
|
|
if (oflag & O_DIRECTORY) /* operations about dir */
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
uffs_DIR *dir;
|
2017-10-25 16:23:02 +08:00
|
|
|
|
|
|
|
if (oflag & O_CREAT) /* create a dir*/
|
|
|
|
{
|
|
|
|
if (uffs_mkdir(file->path) < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
}
|
|
|
|
/* open dir */
|
|
|
|
file_path = rt_malloc(FILE_PATH_MAX);
|
2019-04-03 18:09:52 +08:00
|
|
|
if (file_path == RT_NULL)
|
|
|
|
return -ENOMEM;
|
2017-10-25 16:23:02 +08:00
|
|
|
|
|
|
|
if (file->path[0] == '/' && !(file->path[1] == 0))
|
|
|
|
rt_snprintf(file_path, FILE_PATH_MAX, "%s/", file->path);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file_path[0] = '/';
|
|
|
|
file_path[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = uffs_opendir(file_path);
|
|
|
|
|
|
|
|
if (dir == RT_NULL)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
rt_free(file_path);
|
2017-10-25 16:23:02 +08:00
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
}
|
|
|
|
/* save this pointer,will used by dfs_uffs_getdents*/
|
|
|
|
file->data = dir;
|
|
|
|
rt_free(file_path);
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
/* regular file operations */
|
|
|
|
/* int uffs_open(const char *name, int oflag, ...); what is this?
|
|
|
|
* uffs_open can open dir!! **/
|
|
|
|
mode = 0;
|
|
|
|
if (oflag & O_RDONLY) mode |= UO_RDONLY;
|
|
|
|
if (oflag & O_WRONLY) mode |= UO_WRONLY;
|
|
|
|
if (oflag & O_RDWR) mode |= UO_RDWR;
|
|
|
|
/* Opens the file, if it is existing. If not, a new file is created. */
|
|
|
|
if (oflag & O_CREAT) mode |= UO_CREATE;
|
|
|
|
/* Creates a new file. If the file is existing, it is truncated and overwritten. */
|
|
|
|
if (oflag & O_TRUNC) mode |= UO_TRUNC;
|
|
|
|
/* Creates a new file. The function fails if the file is already existing. */
|
|
|
|
if (oflag & O_EXCL) mode |= UO_EXCL;
|
|
|
|
|
|
|
|
fd = uffs_open(file->path, mode);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
}
|
|
|
|
|
2018-10-15 01:46:07 +08:00
|
|
|
/* save this pointer, it will be used when calling read(), write(),
|
2017-10-25 16:23:02 +08:00
|
|
|
* flush(), seek(), and will be free when calling close()*/
|
|
|
|
|
|
|
|
file->data = (void *)fd;
|
|
|
|
file->pos = uffs_seek(fd, 0, USEEK_CUR);
|
|
|
|
file->size = uffs_seek(fd, 0, USEEK_END);
|
|
|
|
uffs_seek(fd, file->pos, USEEK_SET);
|
|
|
|
|
|
|
|
if (oflag & O_APPEND)
|
|
|
|
{
|
|
|
|
file->pos = uffs_seek(fd, 0, USEEK_END);
|
|
|
|
}
|
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_close(struct dfs_fd *file)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int oflag;
|
|
|
|
int fd;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
oflag = file->flags;
|
|
|
|
if (oflag & O_DIRECTORY)
|
|
|
|
{
|
|
|
|
/* operations about dir */
|
|
|
|
if (uffs_closedir((uffs_DIR *)(file->data)) < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* regular file operations */
|
|
|
|
fd = (int)(file->data);
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
if (uffs_close(fd) == 0)
|
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_ioctl(struct dfs_fd *file, int cmd, void *args)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
return -ENOSYS;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_read(struct dfs_fd *file, void *buf, size_t len)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int fd;
|
|
|
|
int char_read;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
fd = (int)(file->data);
|
|
|
|
char_read = uffs_read(fd, buf, len);
|
|
|
|
if (char_read < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* update position */
|
|
|
|
file->pos = uffs_seek(fd, 0, USEEK_CUR);
|
|
|
|
return char_read;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_write(struct dfs_fd *file,
|
|
|
|
const void *buf,
|
|
|
|
size_t len)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int fd;
|
|
|
|
int char_write;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
fd = (int)(file->data);
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
char_write = uffs_write(fd, buf, len);
|
|
|
|
if (char_write < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* update position */
|
|
|
|
file->pos = uffs_seek(fd, 0, USEEK_CUR);
|
|
|
|
return char_write;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_flush(struct dfs_fd *file)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int fd;
|
|
|
|
int result;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
fd = (int)(file->data);
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
result = uffs_flush(fd);
|
2019-04-03 18:09:52 +08:00
|
|
|
if (result < 0)
|
2017-10-25 16:23:02 +08:00
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int uffs_seekdir(uffs_DIR *dir, long offset)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int i = 0;
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
while (i < offset)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
if (uffs_readdir(dir) == RT_NULL)
|
|
|
|
return -1;
|
|
|
|
i++;
|
2019-04-03 18:09:52 +08:00
|
|
|
}
|
2017-10-25 16:23:02 +08:00
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_seek(struct dfs_fd *file,
|
|
|
|
rt_off_t offset)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
/* set offset as current offset */
|
|
|
|
if (file->type == FT_DIRECTORY)
|
|
|
|
{
|
|
|
|
uffs_rewinddir((uffs_DIR *)(file->data));
|
2019-04-03 18:09:52 +08:00
|
|
|
result = uffs_seekdir((uffs_DIR *)(file->data), offset / sizeof(struct dirent));
|
2017-10-25 16:23:02 +08:00
|
|
|
if (result >= 0)
|
|
|
|
{
|
2019-04-03 18:09:52 +08:00
|
|
|
file->pos = offset;
|
2017-10-25 16:23:02 +08:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (file->type == FT_REGULAR)
|
|
|
|
{
|
|
|
|
result = uffs_seek((int)(file->data), offset, USEEK_SET);
|
2019-04-03 18:09:52 +08:00
|
|
|
if (result >= 0)
|
2017-10-25 16:23:02 +08:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return the size of struct dirent*/
|
|
|
|
static int dfs_uffs_getdents(
|
2019-04-03 18:09:52 +08:00
|
|
|
struct dfs_fd *file,
|
|
|
|
struct dirent *dirp,
|
2018-09-10 19:51:15 +08:00
|
|
|
uint32_t count)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
rt_uint32_t index;
|
2019-04-03 18:09:52 +08:00
|
|
|
char *file_path;
|
|
|
|
struct dirent *d;
|
|
|
|
uffs_DIR *dir;
|
|
|
|
struct uffs_dirent *uffs_d;
|
|
|
|
|
|
|
|
dir = (uffs_DIR *)(file->data);
|
2017-10-25 16:23:02 +08:00
|
|
|
RT_ASSERT(dir != RT_NULL);
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* round count, count is always 1 */
|
|
|
|
count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
|
|
|
|
if (count == 0) return -EINVAL;
|
|
|
|
|
|
|
|
/* allocate file name */
|
|
|
|
file_path = rt_malloc(FILE_PATH_MAX);
|
|
|
|
if (file_path == RT_NULL)
|
|
|
|
return -ENOMEM;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
index = 0;
|
|
|
|
/* usually, the while loop should only be looped only once! */
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
struct uffs_stat s;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
d = dirp + index;
|
|
|
|
|
|
|
|
uffs_d = uffs_readdir(dir);
|
|
|
|
if (uffs_d == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(file_path);
|
|
|
|
return (uffs_result_to_dfs(uffs_get_error()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file->path[0] == '/' && !(file->path[1] == 0))
|
|
|
|
rt_snprintf(file_path, FILE_PATH_MAX, "%s/%s", file->path, uffs_d->d_name);
|
|
|
|
else
|
|
|
|
rt_strncpy(file_path, uffs_d->d_name, FILE_PATH_MAX);
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
uffs_stat(file_path, &s);
|
|
|
|
switch (s.st_mode & US_IFMT) /* file type mark */
|
2017-10-25 16:23:02 +08:00
|
|
|
{
|
|
|
|
case US_IFREG: /* directory */
|
|
|
|
d->d_type = DT_REG;
|
|
|
|
break;
|
|
|
|
case US_IFDIR: /* regular file */
|
|
|
|
d->d_type = DT_DIR;
|
|
|
|
break;
|
|
|
|
case US_IFLNK: /* symbolic link */
|
|
|
|
case US_IREAD: /* read permission */
|
|
|
|
case US_IWRITE:/* write permission */
|
|
|
|
default:
|
|
|
|
d->d_type = DT_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the rest args of struct dirent* dirp */
|
|
|
|
d->d_namlen = rt_strlen(uffs_d->d_name);
|
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
|
|
|
rt_strncpy(d->d_name, uffs_d->d_name, rt_strlen(uffs_d->d_name) + 1);
|
|
|
|
|
|
|
|
index ++;
|
|
|
|
if (index * sizeof(struct dirent) >= count)
|
|
|
|
break;
|
|
|
|
}
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* free file name buf */
|
|
|
|
rt_free(file_path);
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
if (index == 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
|
|
|
|
file->pos += index * sizeof(struct dirent);
|
|
|
|
|
|
|
|
return index * sizeof(struct dirent);
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_unlink(struct dfs_filesystem *fs, const char *path)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int result;
|
|
|
|
struct uffs_stat s;
|
|
|
|
|
|
|
|
/* judge file type, dir is to be delete by uffs_rmdir, others by uffs_remove */
|
|
|
|
if (uffs_lstat(path, &s) < 0)
|
|
|
|
{
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
switch (s.st_mode & US_IFMT)
|
2017-10-25 16:23:02 +08:00
|
|
|
{
|
|
|
|
case US_IFREG:
|
|
|
|
result = uffs_remove(path);
|
|
|
|
break;
|
|
|
|
case US_IFDIR:
|
|
|
|
result = uffs_rmdir(path);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* unknown file type */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (result < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
|
|
|
|
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int dfs_uffs_rename(
|
2019-04-03 18:09:52 +08:00
|
|
|
struct dfs_filesystem *fs,
|
|
|
|
const char *oldpath,
|
|
|
|
const char *newpath)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int result;
|
2019-04-03 18:09:52 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
result = uffs_rename(oldpath, newpath);
|
|
|
|
if (result < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static int dfs_uffs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
int result;
|
|
|
|
struct uffs_stat s;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
result = uffs_stat(path, &s);
|
|
|
|
if (result < 0)
|
|
|
|
return uffs_result_to_dfs(uffs_get_error());
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
/* convert uffs stat to dfs stat structure */
|
|
|
|
/* FIXME, these field may not be the same */
|
|
|
|
st->st_dev = 0;
|
|
|
|
st->st_mode = s.st_mode;
|
|
|
|
st->st_size = s.st_size;
|
|
|
|
st->st_mtime = s.st_mtime;
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
return 0;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 18:09:52 +08:00
|
|
|
static const struct dfs_file_ops dfs_uffs_fops =
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
dfs_uffs_open,
|
|
|
|
dfs_uffs_close,
|
|
|
|
dfs_uffs_ioctl,
|
|
|
|
dfs_uffs_read,
|
|
|
|
dfs_uffs_write,
|
|
|
|
dfs_uffs_flush,
|
|
|
|
dfs_uffs_seek,
|
|
|
|
dfs_uffs_getdents,
|
2017-10-15 22:44:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dfs_filesystem_ops dfs_uffs_ops =
|
2013-01-08 20:30:57 +08:00
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
"uffs", /* file system type: uffs */
|
2013-01-08 20:30:57 +08:00
|
|
|
#if RTTHREAD_VERSION >= 10100
|
2017-10-25 16:23:02 +08:00
|
|
|
DFS_FS_FLAG_FULLPATH,
|
2013-01-08 20:30:57 +08:00
|
|
|
#else
|
|
|
|
#error "uffs can only work with rtthread whose version should >= 1.01\n"
|
|
|
|
#endif
|
2017-10-25 16:23:02 +08:00
|
|
|
&dfs_uffs_fops,
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
dfs_uffs_mount,
|
|
|
|
dfs_uffs_unmount,
|
|
|
|
dfs_uffs_mkfs,
|
|
|
|
dfs_uffs_statfs,
|
2013-01-08 20:30:57 +08:00
|
|
|
|
2017-10-25 16:23:02 +08:00
|
|
|
dfs_uffs_unlink,
|
|
|
|
dfs_uffs_stat,
|
|
|
|
dfs_uffs_rename,
|
2013-01-08 20:30:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int dfs_uffs_init(void)
|
|
|
|
{
|
2017-10-25 16:23:02 +08:00
|
|
|
/* register uffs file system */
|
|
|
|
dfs_register(&dfs_uffs_ops);
|
|
|
|
|
|
|
|
if (uffs_InitObjectBuf() == U_SUCC)
|
|
|
|
{
|
|
|
|
if (uffs_DirEntryBufInit() == U_SUCC)
|
|
|
|
{
|
|
|
|
uffs_InitGlobalFsLock();
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -RT_ERROR;
|
2013-01-08 20:30:57 +08:00
|
|
|
}
|
2017-11-03 11:44:37 +08:00
|
|
|
INIT_COMPONENT_EXPORT(dfs_uffs_init);
|
2017-10-25 16:23:02 +08:00
|
|
|
|