2012-01-01 21:37:21 +08:00
|
|
|
/*
|
2022-08-03 00:09:49 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2012-01-01 21:37:21 +08:00
|
|
|
*
|
2018-10-15 01:46:07 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2012-01-01 21:37:21 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs.h>
|
|
|
|
#include <dfs_fs.h>
|
2017-10-25 14:21:49 +08:00
|
|
|
#include <dfs_file.h>
|
|
|
|
|
2012-01-01 21:37:21 +08:00
|
|
|
#include "dfs_romfs.h"
|
|
|
|
|
|
|
|
int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
|
|
|
|
{
|
2022-04-08 20:03:43 +08:00
|
|
|
struct romfs_dirent *root_dirent;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (data == NULL)
|
|
|
|
return -EIO;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-04-08 20:03:43 +08:00
|
|
|
root_dirent = (struct romfs_dirent *)data;
|
|
|
|
fs->data = root_dirent;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_unmount(struct dfs_filesystem *fs)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_romfs_ioctl(struct dfs_file *file, int cmd, void *args)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
int ret = RT_EOK;
|
|
|
|
struct romfs_dirent *dirent;
|
|
|
|
|
|
|
|
dirent = (struct romfs_dirent *)file->vnode->data;
|
|
|
|
RT_ASSERT(dirent != NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_FIOGETADDR:
|
|
|
|
{
|
|
|
|
*(rt_ubase_t*)args = (rt_ubase_t)dirent->data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RT_FIOFTRUNCATE:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ret = -RT_EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2014-11-03 12:10:25 +08:00
|
|
|
rt_inline int check_dirent(struct romfs_dirent *dirent)
|
|
|
|
{
|
2014-11-03 17:32:50 +08:00
|
|
|
if ((dirent->type != ROMFS_DIRENT_FILE && dirent->type != ROMFS_DIRENT_DIR)
|
2022-08-03 00:09:49 +08:00
|
|
|
|| dirent->size == ~0U)
|
2014-11-03 12:10:25 +08:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-08 20:03:43 +08:00
|
|
|
struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_size_t index, found;
|
|
|
|
const char *subpath, *subpath_end;
|
|
|
|
struct romfs_dirent *dirent;
|
|
|
|
rt_size_t dirent_size;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2014-11-03 12:10:25 +08:00
|
|
|
/* Check the root_dirent. */
|
2022-04-08 20:03:43 +08:00
|
|
|
if (check_dirent(root_dirent) != 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (path[0] == '/' && path[1] == '\0')
|
|
|
|
{
|
2022-04-08 20:03:43 +08:00
|
|
|
*size = root_dirent->size;
|
|
|
|
return root_dirent;
|
2017-10-15 22:44:53 +08:00
|
|
|
}
|
|
|
|
|
2022-04-08 20:03:43 +08:00
|
|
|
/* goto root directory entries */
|
|
|
|
dirent = (struct romfs_dirent *)root_dirent->data;
|
|
|
|
dirent_size = root_dirent->size;
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
/* get the end position of this subpath */
|
|
|
|
subpath_end = path;
|
|
|
|
/* skip /// */
|
|
|
|
while (*subpath_end && *subpath_end == '/')
|
|
|
|
subpath_end ++;
|
|
|
|
subpath = subpath_end;
|
|
|
|
while ((*subpath_end != '/') && *subpath_end)
|
|
|
|
subpath_end ++;
|
|
|
|
|
|
|
|
while (dirent != NULL)
|
|
|
|
{
|
|
|
|
found = 0;
|
|
|
|
|
|
|
|
/* search in folder */
|
|
|
|
for (index = 0; index < dirent_size; index ++)
|
|
|
|
{
|
2014-11-03 12:10:25 +08:00
|
|
|
if (check_dirent(&dirent[index]) != 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
return NULL;
|
2022-08-03 00:09:49 +08:00
|
|
|
if (rt_strlen(dirent[index].name) == (rt_size_t)(subpath_end - subpath) &&
|
2022-04-08 20:03:43 +08:00
|
|
|
rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
|
|
|
dirent_size = dirent[index].size;
|
|
|
|
|
|
|
|
/* skip /// */
|
|
|
|
while (*subpath_end && *subpath_end == '/')
|
|
|
|
subpath_end ++;
|
|
|
|
subpath = subpath_end;
|
|
|
|
while ((*subpath_end != '/') && *subpath_end)
|
|
|
|
subpath_end ++;
|
|
|
|
|
|
|
|
if (!(*subpath))
|
|
|
|
{
|
|
|
|
*size = dirent_size;
|
|
|
|
return &dirent[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirent[index].type == ROMFS_DIRENT_DIR)
|
|
|
|
{
|
|
|
|
/* enter directory */
|
2022-04-08 20:03:43 +08:00
|
|
|
dirent = (struct romfs_dirent *)dirent[index].data;
|
2017-10-15 22:44:53 +08:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* return file dirent */
|
|
|
|
return &dirent[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
break; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
return NULL;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
ssize_t dfs_romfs_read(struct dfs_file *file, void *buf, size_t count)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_size_t length;
|
|
|
|
struct romfs_dirent *dirent;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = (struct romfs_dirent *)file->vnode->data;
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(dirent != NULL);
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2014-11-03 12:10:25 +08:00
|
|
|
if (check_dirent(dirent) != 0)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2014-11-03 12:10:25 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (count < file->vnode->size - file->pos)
|
2017-10-15 22:44:53 +08:00
|
|
|
length = count;
|
|
|
|
else
|
2022-12-03 12:07:44 +08:00
|
|
|
length = file->vnode->size - file->pos;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (length > 0)
|
2022-04-08 20:03:43 +08:00
|
|
|
rt_memcpy(buf, &(dirent->data[file->pos]), length);
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
/* update file current position */
|
|
|
|
file->pos += length;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return length;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2023-07-24 09:12:35 +08:00
|
|
|
off_t dfs_romfs_lseek(struct dfs_file *file, off_t offset)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
if (offset <= file->vnode->size)
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
|
|
|
file->pos = offset;
|
|
|
|
return file->pos;
|
|
|
|
}
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_romfs_close(struct dfs_file *file)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2022-12-03 12:07:44 +08:00
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
file->vnode->data = NULL;
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_romfs_open(struct dfs_file *file)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_size_t size;
|
|
|
|
struct romfs_dirent *dirent;
|
2022-04-08 20:03:43 +08:00
|
|
|
struct romfs_dirent *root_dirent;
|
|
|
|
struct dfs_filesystem *fs;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RT_ASSERT(file->vnode->ref_count > 0);
|
|
|
|
if (file->vnode->ref_count > 1)
|
|
|
|
{
|
|
|
|
if (file->vnode->type == FT_DIRECTORY
|
|
|
|
&& !(file->flags & O_DIRECTORY))
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
file->pos = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs = file->vnode->fs;
|
2022-04-08 20:03:43 +08:00
|
|
|
root_dirent = (struct romfs_dirent *)fs->data;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-04-08 20:03:43 +08:00
|
|
|
if (check_dirent(root_dirent) != 0)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = dfs_romfs_lookup(root_dirent, file->vnode->path, &size);
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
|
|
|
|
/* entry is a directory file type */
|
|
|
|
if (dirent->type == ROMFS_DIRENT_DIR)
|
|
|
|
{
|
|
|
|
if (!(file->flags & O_DIRECTORY))
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
file->vnode->type = FT_DIRECTORY;
|
2017-10-15 22:44:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* entry is a file, but open it as a directory */
|
|
|
|
if (file->flags & O_DIRECTORY)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
|
|
|
file->vnode->type = FT_REGULAR;
|
2017-10-15 22:44:53 +08:00
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
file->vnode->data = dirent;
|
|
|
|
file->vnode->size = size;
|
2017-10-15 22:44:53 +08:00
|
|
|
file->pos = 0;
|
|
|
|
|
|
|
|
return RT_EOK;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
|
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
rt_size_t size;
|
|
|
|
struct romfs_dirent *dirent;
|
2022-04-08 20:03:43 +08:00
|
|
|
struct romfs_dirent *root_dirent;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-04-08 20:03:43 +08:00
|
|
|
root_dirent = (struct romfs_dirent *)fs->data;
|
|
|
|
dirent = dfs_romfs_lookup(root_dirent, path, &size);
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent == NULL)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -ENOENT;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
st->st_dev = 0;
|
|
|
|
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
|
2019-04-03 18:09:52 +08:00
|
|
|
S_IWUSR | S_IWGRP | S_IWOTH;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
if (dirent->type == ROMFS_DIRENT_DIR)
|
|
|
|
{
|
|
|
|
st->st_mode &= ~S_IFREG;
|
|
|
|
st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
|
|
|
|
}
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
st->st_size = dirent->size;
|
|
|
|
st->st_mtime = 0;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return RT_EOK;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 22:25:51 +08:00
|
|
|
int dfs_romfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2022-04-08 20:03:43 +08:00
|
|
|
rt_size_t index;
|
2022-09-20 22:31:11 +08:00
|
|
|
rt_size_t len;
|
2017-10-15 22:44:53 +08:00
|
|
|
const char *name;
|
|
|
|
struct dirent *d;
|
|
|
|
struct romfs_dirent *dirent, *sub_dirent;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
dirent = (struct romfs_dirent *)file->vnode->data;
|
2014-11-03 12:10:25 +08:00
|
|
|
if (check_dirent(dirent) != 0)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EIO;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2017-10-15 22:44:53 +08:00
|
|
|
RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
/* enter directory */
|
2022-04-08 20:03:43 +08:00
|
|
|
dirent = (struct romfs_dirent *)dirent->data;
|
2014-11-03 12:10:25 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
/* make integer count */
|
|
|
|
count = (count / sizeof(struct dirent));
|
|
|
|
if (count == 0)
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
return -EINVAL;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
2014-11-03 12:10:25 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
index = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
for (index = 0; index < count && file->pos < file->vnode->size; index++)
|
2017-10-15 22:44:53 +08:00
|
|
|
{
|
|
|
|
d = dirp + index;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
sub_dirent = &dirent[file->pos];
|
2022-04-08 20:03:43 +08:00
|
|
|
name = sub_dirent->name;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
/* fill dirent */
|
|
|
|
if (sub_dirent->type == ROMFS_DIRENT_DIR)
|
|
|
|
d->d_type = DT_DIR;
|
|
|
|
else
|
|
|
|
d->d_type = DT_REG;
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2022-09-20 22:31:11 +08:00
|
|
|
len = rt_strlen(name);
|
|
|
|
RT_ASSERT(len <= RT_UINT8_MAX);
|
|
|
|
d->d_namlen = (rt_uint8_t)len;
|
2017-10-15 22:44:53 +08:00
|
|
|
d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
|
2023-11-28 17:16:21 +08:00
|
|
|
rt_strncpy(d->d_name, name, DIRENT_NAME_MAX);
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
/* move to next position */
|
|
|
|
++ file->pos;
|
|
|
|
}
|
2012-01-01 21:37:21 +08:00
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
return index * sizeof(struct dirent);
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
|
|
|
|
2017-10-15 22:44:53 +08:00
|
|
|
static const struct dfs_file_ops _rom_fops =
|
2012-01-01 21:37:21 +08:00
|
|
|
{
|
2017-10-15 22:44:53 +08:00
|
|
|
dfs_romfs_open,
|
|
|
|
dfs_romfs_close,
|
|
|
|
dfs_romfs_ioctl,
|
|
|
|
dfs_romfs_read,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
dfs_romfs_lseek,
|
|
|
|
dfs_romfs_getdents,
|
2022-08-03 00:09:49 +08:00
|
|
|
NULL,
|
2017-10-15 22:44:53 +08:00
|
|
|
};
|
|
|
|
static const struct dfs_filesystem_ops _romfs =
|
|
|
|
{
|
|
|
|
"rom",
|
|
|
|
DFS_FS_FLAG_DEFAULT,
|
|
|
|
&_rom_fops,
|
|
|
|
|
|
|
|
dfs_romfs_mount,
|
|
|
|
dfs_romfs_unmount,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
dfs_romfs_stat,
|
|
|
|
NULL,
|
2012-01-01 21:37:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int dfs_romfs_init(void)
|
|
|
|
{
|
|
|
|
/* register rom file system */
|
|
|
|
dfs_register(&_romfs);
|
2017-10-15 22:44:53 +08:00
|
|
|
return 0;
|
2012-01-01 21:37:21 +08:00
|
|
|
}
|
2017-11-03 11:44:37 +08:00
|
|
|
INIT_COMPONENT_EXPORT(dfs_romfs_init);
|
2012-01-01 21:37:21 +08:00
|
|
|
|