1) elm fatfs can do mkfs without mounting first, 2) use new mkfs 3)change linebreak in dfs_uffs.c to unix

This commit is contained in:
prife 2013-01-08 20:43:52 +08:00
commit b3cf278502
5 changed files with 584 additions and 528 deletions

View File

@ -75,38 +75,50 @@ static int elm_result_to_dfs(FRESULT result)
return status; return status;
} }
/* results:
* -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;
for (index = 0; index < _VOLUMES; index ++)
{
if (disk[index] == id)
return index;
}
return -1;
}
int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data) int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{ {
FATFS *fat; FATFS *fat;
FRESULT result; FRESULT result;
BYTE index; int index;
/* handle RT-Thread device routine */ /* get an empty position */
for (index = 0; index < _VOLUMES; index ++) index = get_disk(RT_NULL);
{ if (index == -1)
if (disk[index] == RT_NULL)
{
break;
}
}
if (index == _VOLUMES)
return -DFS_STATUS_ENOSPC; return -DFS_STATUS_ENOSPC;
/* get device */ /* save device */
disk[index] = fs->dev_id; disk[index] = fs->dev_id;
fat = (FATFS *)rt_malloc(sizeof(FATFS)); fat = (FATFS *)rt_malloc(sizeof(FATFS));
if (fat == RT_NULL) if (fat == RT_NULL)
{ {
disk[index] = RT_NULL;
return -1; return -1;
} }
/* mount fatfs, always 0 logic driver */ /* mount fatfs, always 0 logic driver */
result = f_mount(index, fat); result = f_mount((BYTE)index, fat);
if (result == FR_OK) if (result == FR_OK)
{ {
char drive[8]; char drive[8];
DIR * dir; DIR *dir;
rt_snprintf(drive, sizeof(drive), "%d:/", index); rt_snprintf(drive, sizeof(drive), "%d:/", index);
dir = (DIR *)rt_malloc(sizeof(DIR)); dir = (DIR *)rt_malloc(sizeof(DIR));
@ -116,68 +128,106 @@ int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *d
/* open the root directory to test whether the fatfs is valid */ /* open the root directory to test whether the fatfs is valid */
result = f_opendir(dir, drive); result = f_opendir(dir, drive);
if (result != FR_OK) if (result != FR_OK)
{ goto __err;
rt_free(dir);
return elm_result_to_dfs(result);
}
rt_free(dir);
/* mount succeed! */
fs->data = fat; fs->data = fat;
rt_free(dir);
return 0;
} }
else
{ __err:
disk[index] = RT_NULL;
rt_free(fat); rt_free(fat);
return elm_result_to_dfs(result); return elm_result_to_dfs(result);
}
return 0;
} }
int dfs_elm_unmount(struct dfs_filesystem *fs) int dfs_elm_unmount(struct dfs_filesystem *fs)
{ {
FATFS *fat; FATFS *fat;
FRESULT result; FRESULT result;
BYTE index; int index;
fat = (FATFS *)fs->data; fat = (FATFS *)fs->data;
RT_ASSERT(fat != RT_NULL); RT_ASSERT(fat != RT_NULL);
/* find the device index and then umount it */ /* find the device index and then umount it */
for (index = 0; index < _VOLUMES; index ++) index = get_disk(fs->dev_id);
{ if (index == -1) /* not found */
if (disk[index] == fs->dev_id) return -DFS_STATUS_ENOENT;
{
result = f_mount(index, RT_NULL); result = f_mount((BYTE)index, RT_NULL);
if (result != FR_OK)
return elm_result_to_dfs(result);
if (result == FR_OK)
{
fs->data = RT_NULL; fs->data = RT_NULL;
disk[index] = RT_NULL; disk[index] = RT_NULL;
rt_free(fat); rt_free(fat);
return DFS_STATUS_OK;
}
}
}
return -DFS_STATUS_ENOENT; return DFS_STATUS_OK;
} }
int dfs_elm_mkfs(const char *device_name) int dfs_elm_mkfs(rt_device_t dev_id)
{ {
BYTE drv; #define FSM_STATUS_INIT 0
rt_device_t dev; #define FSM_STATUS_USE_TEMP_DRIVER 1
FATFS *fat;
int flag;
FRESULT result; FRESULT result;
int index;
/* find device name */ if (dev_id == RT_NULL)
for (drv = 0; drv < _VOLUMES; drv ++) return -DFS_STATUS_EINVAL;
/* 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)
{ {
dev = disk[drv]; /* not found the device id */
if (dev != RT_NULL && rt_strncmp(dev->parent.name, device_name, RT_NAME_MAX) == 0) index = get_disk(RT_NULL);
if (index == -1)
{ {
/* no space to store an temp driver */
rt_kprintf("sorry, there is no space to do mkfs! \n");
return -DFS_STATUS_ENOSPC;
}
else
{
fat = rt_malloc(sizeof(FATFS));
if (fat == RT_NULL)
return -DFS_STATUS_ENOMEM;
flag = FSM_STATUS_USE_TEMP_DRIVER;
disk[index] = dev_id;
/* 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.
*/
f_mount((BYTE)index, fat);
}
}
/* 1: no partition table */ /* 1: no partition table */
/* 0: auto selection of cluster size */ /* 0: auto selection of cluster size */
result = f_mkfs(drv, 1, 0); result = f_mkfs((BYTE)index, 1, 0);
/* check flag status, we need clear the temp driver stored in disk[] */
if (flag == FSM_STATUS_USE_TEMP_DRIVER)
{
rt_free(fat);
f_mount((BYTE)index, RT_NULL);
disk[index] = RT_NULL;
}
if (result != FR_OK) if (result != FR_OK)
{ {
rt_kprintf("format error\n"); rt_kprintf("format error\n");
@ -185,12 +235,6 @@ int dfs_elm_mkfs(const char *device_name)
} }
return DFS_STATUS_OK; return DFS_STATUS_OK;
}
}
/* can't find device driver */
rt_kprintf("can not find device driver: %s\n", device_name);
return -DFS_STATUS_EIO;
} }
int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf) int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
@ -234,7 +278,7 @@ int dfs_elm_open(struct dfs_fd *file)
#if (_VOLUMES > 1) #if (_VOLUMES > 1)
int vol; int vol;
extern int elm_get_vol(FATFS *fat); extern int elm_get_vol(FATFS * fat);
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)file->fs->data); vol = elm_get_vol((FATFS *)file->fs->data);
@ -513,7 +557,7 @@ int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count
break; break;
#if _USE_LFN #if _USE_LFN
fn = *fno.lfname? fno.lfname : fno.fname; fn = *fno.lfname ? fno.lfname : fno.fname;
#else #else
fn = fno.fname; fn = fno.fname;
#endif #endif
@ -552,7 +596,7 @@ int dfs_elm_unlink(struct dfs_filesystem *fs, const char *path)
#if _VOLUMES > 1 #if _VOLUMES > 1
int vol; int vol;
char *drivers_fn; char *drivers_fn;
extern int elm_get_vol(FATFS *fat); extern int elm_get_vol(FATFS * fat);
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data); vol = elm_get_vol((FATFS *)fs->data);
@ -583,7 +627,7 @@ int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *n
char *drivers_oldfn; char *drivers_oldfn;
const char *drivers_newfn; const char *drivers_newfn;
int vol; int vol;
extern int elm_get_vol(FATFS *fat); extern int elm_get_vol(FATFS * fat);
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data); vol = elm_get_vol((FATFS *)fs->data);
@ -618,7 +662,7 @@ int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
#if _VOLUMES > 1 #if _VOLUMES > 1
int vol; int vol;
char *drivers_fn; char *drivers_fn;
extern int elm_get_vol(FATFS *fat); extern int elm_get_vol(FATFS * fat);
/* add path for ELM FatFS driver support */ /* add path for ELM FatFS driver support */
vol = elm_get_vol((FATFS *)fs->data); vol = elm_get_vol((FATFS *)fs->data);
@ -783,7 +827,7 @@ DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
rt_memset(&geometry, 0, sizeof(geometry)); rt_memset(&geometry, 0, sizeof(geometry));
rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry); rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
*(DWORD *)buff = geometry.block_size/geometry.bytes_per_sector; *(DWORD *)buff = geometry.block_size / geometry.bytes_per_sector;
} }
else if (ctrl == CTRL_SYNC) else if (ctrl == CTRL_SYNC)
{ {
@ -821,6 +865,7 @@ int ff_cre_syncobj(BYTE drv, _SYNC_t *m)
int ff_del_syncobj(_SYNC_t m) int ff_del_syncobj(_SYNC_t m)
{ {
if (m != RT_NULL)
rt_mutex_delete(m); rt_mutex_delete(m);
return RT_TRUE; return RT_TRUE;
@ -844,13 +889,13 @@ void ff_rel_grant(_SYNC_t m)
/* Memory functions */ /* Memory functions */
#if _USE_LFN == 3 #if _USE_LFN == 3
/* Allocate memory block */ /* Allocate memory block */
void* ff_memalloc (UINT size) void *ff_memalloc(UINT size)
{ {
return rt_malloc(size); return rt_malloc(size);
} }
/* Free memory block */ /* Free memory block */
void ff_memfree (void* mem) void ff_memfree(void *mem)
{ {
rt_free(mem); rt_free(mem);
} }

View File

@ -222,7 +222,7 @@ static int dfs_jffs2_unmount(struct dfs_filesystem* fs)
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
} }
static int dfs_jffs2_mkfs(const char* device_name) static int dfs_jffs2_mkfs(rt_device_t dev_id)
{ {
/* just erase all blocks on this nand partition */ /* just erase all blocks on this nand partition */
return -DFS_STATUS_ENOSYS; return -DFS_STATUS_ENOSYS;

View File

@ -207,7 +207,7 @@ static int dfs_uffs_unmount(struct dfs_filesystem* fs)
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
} }
static int dfs_uffs_mkfs(const char* device_name) static int dfs_uffs_mkfs(rt_device_t dev_id)
{ {
rt_base_t index; rt_base_t index;
rt_uint32_t block; rt_uint32_t block;
@ -216,15 +216,13 @@ static int dfs_uffs_mkfs(const char* device_name)
/*1. find the device index */ /*1. find the device index */
for (index = 0; index < UFFS_DEVICE_MAX; index++) for (index = 0; index < UFFS_DEVICE_MAX; index++)
{ {
if (rt_strncmp(nand_part[index].dev->parent.parent.name, if (nand_part[index].dev == (struct rt_mtd_nand_device *)dev_id)
device_name, RT_NAME_MAX) == 0)
break; break;
} }
if (index == UFFS_DEVICE_MAX) if (index == UFFS_DEVICE_MAX)
{ {
/* can't find device driver */ /* can't find device driver */
rt_kprintf("can not find device driver: %s\n", device_name);
return -DFS_STATUS_ENOENT; return -DFS_STATUS_ENOENT;
} }

View File

@ -35,7 +35,7 @@ struct dfs_filesystem_operation
int (*unmount) (struct dfs_filesystem *fs); int (*unmount) (struct dfs_filesystem *fs);
/* make a file system */ /* make a file system */
int (*mkfs) (const char *device_name); int (*mkfs) (rt_device_t devid);
int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf); int (*statfs) (struct dfs_filesystem *fs, struct statfs *buf);
int (*open) (struct dfs_fd *fd); int (*open) (struct dfs_fd *fd);

View File

@ -424,6 +424,19 @@ err1:
int dfs_mkfs(const char *fs_name, const char *device_name) int dfs_mkfs(const char *fs_name, const char *device_name)
{ {
int index; int index;
rt_device_t dev_id;
/* check device name, and it should not be NULL */
if (device_name == RT_NULL)
dev_id = RT_NULL;
else
dev_id = rt_device_find(device_name);
if (dev_id == RT_NULL)
{
rt_set_errno(-DFS_STATUS_ENODEV);
return -1;
}
/* lock file system */ /* lock file system */
dfs_lock(); dfs_lock();
@ -438,7 +451,7 @@ int dfs_mkfs(const char *fs_name, const char *device_name)
dfs_unlock(); dfs_unlock();
if (ops->mkfs != RT_NULL) if (ops->mkfs != RT_NULL)
return ops->mkfs(device_name); return ops->mkfs(dev_id);
break; break;
} }