2017-08-08 21:12:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Codecoup
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <zephyr/types.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <flash.h>
|
|
|
|
#include <fs.h>
|
2018-12-02 22:58:22 +08:00
|
|
|
#include <crc.h>
|
2017-08-08 21:12:55 +08:00
|
|
|
#include <misc/__assert.h>
|
|
|
|
#include <misc/printk.h>
|
|
|
|
#include <nffs/os.h>
|
2018-04-17 00:44:48 +08:00
|
|
|
#include <nffs/nffs.h>
|
2017-08-08 21:12:55 +08:00
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
#define NFFS_MAX_FILE_NAME 256
|
|
|
|
|
2017-08-08 21:12:55 +08:00
|
|
|
/*
|
|
|
|
* NFFS code keeps fs state in RAM but access to these structures is not
|
|
|
|
* thread-safe - we need global lock for each fs operation to guarantee two
|
|
|
|
* threads won't modify NFFS at the same time.
|
|
|
|
*/
|
|
|
|
static struct k_mutex nffs_lock;
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
/*
|
|
|
|
* TODO: Get rid of global flash_dev which limits
|
|
|
|
* system to have multiple instances of NFFS.
|
|
|
|
*/
|
2017-08-08 21:12:55 +08:00
|
|
|
static struct device *flash_dev;
|
|
|
|
|
2018-04-07 01:20:02 +08:00
|
|
|
/* nffs flash area descriptors */
|
|
|
|
static struct nffs_area_desc descs[CONFIG_NFFS_FILESYSTEM_MAX_AREAS + 1];
|
|
|
|
|
2017-08-08 21:12:55 +08:00
|
|
|
K_MEM_SLAB_DEFINE(nffs_file_pool, sizeof(struct nffs_file),
|
|
|
|
CONFIG_FS_NFFS_NUM_FILES, 4);
|
|
|
|
K_MEM_SLAB_DEFINE(nffs_dir_pool, sizeof(struct nffs_dir),
|
|
|
|
CONFIG_FS_NFFS_NUM_DIRS, 4);
|
|
|
|
K_MEM_SLAB_DEFINE(nffs_inode_entry_pool, sizeof(struct nffs_inode_entry),
|
|
|
|
CONFIG_FS_NFFS_NUM_INODES, 4);
|
|
|
|
K_MEM_SLAB_DEFINE(nffs_block_entry_pool, sizeof(struct nffs_hash_entry),
|
|
|
|
CONFIG_FS_NFFS_NUM_BLOCKS, 4);
|
|
|
|
K_MEM_SLAB_DEFINE(nffs_cache_inode_pool, sizeof(struct nffs_cache_inode),
|
|
|
|
CONFIG_FS_NFFS_NUM_CACHE_INODES, 4);
|
|
|
|
K_MEM_SLAB_DEFINE(nffs_cache_block_pool, sizeof(struct nffs_cache_block),
|
|
|
|
CONFIG_FS_NFFS_NUM_CACHE_BLOCKS, 4);
|
|
|
|
|
|
|
|
static int translate_error(int error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case FS_EOK:
|
|
|
|
return 0;
|
|
|
|
case FS_ECORRUPT:
|
|
|
|
case FS_EHW:
|
|
|
|
return -EIO;
|
|
|
|
case FS_EOFFSET:
|
|
|
|
case FS_EINVAL:
|
|
|
|
return -EINVAL;
|
|
|
|
case FS_ENOMEM:
|
|
|
|
return -ENOMEM;
|
|
|
|
case FS_ENOENT:
|
|
|
|
return -ENOENT;
|
|
|
|
case FS_EEMPTY:
|
|
|
|
return -ENODEV;
|
|
|
|
case FS_EFULL:
|
|
|
|
return -ENOSPC;
|
|
|
|
case FS_EUNEXP:
|
|
|
|
case FS_EOS:
|
|
|
|
return -EIO;
|
|
|
|
case FS_EEXIST:
|
|
|
|
return -EEXIST;
|
|
|
|
case FS_EACCESS:
|
|
|
|
return -EACCES;
|
|
|
|
case FS_EUNINIT:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nffs_os_mempool_init(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Just reinitialize slabs here - this is what original implementation
|
|
|
|
* does. We assume all references to previously allocated blocks, if
|
|
|
|
* any, are invalidated in NFFS code already.
|
|
|
|
*/
|
|
|
|
|
|
|
|
k_mem_slab_init(&nffs_file_pool, _k_mem_slab_buf_nffs_file_pool,
|
|
|
|
sizeof(struct nffs_file),
|
|
|
|
CONFIG_FS_NFFS_NUM_FILES);
|
|
|
|
k_mem_slab_init(&nffs_dir_pool, _k_mem_slab_buf_nffs_dir_pool,
|
|
|
|
sizeof(struct nffs_dir),
|
|
|
|
CONFIG_FS_NFFS_NUM_DIRS);
|
|
|
|
k_mem_slab_init(&nffs_inode_entry_pool,
|
|
|
|
_k_mem_slab_buf_nffs_inode_entry_pool,
|
|
|
|
sizeof(struct nffs_inode_entry),
|
|
|
|
CONFIG_FS_NFFS_NUM_INODES);
|
|
|
|
k_mem_slab_init(&nffs_block_entry_pool,
|
|
|
|
_k_mem_slab_buf_nffs_block_entry_pool,
|
|
|
|
sizeof(struct nffs_hash_entry),
|
|
|
|
CONFIG_FS_NFFS_NUM_BLOCKS);
|
|
|
|
k_mem_slab_init(&nffs_cache_inode_pool,
|
|
|
|
_k_mem_slab_buf_nffs_cache_inode_pool,
|
|
|
|
sizeof(struct nffs_cache_inode),
|
|
|
|
CONFIG_FS_NFFS_NUM_CACHE_INODES);
|
|
|
|
k_mem_slab_init(&nffs_cache_block_pool,
|
|
|
|
_k_mem_slab_buf_nffs_cache_block_pool,
|
|
|
|
sizeof(struct nffs_cache_block),
|
|
|
|
CONFIG_FS_NFFS_NUM_CACHE_BLOCKS);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *nffs_os_mempool_get(nffs_os_mempool_t *pool)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
rc = k_mem_slab_alloc(pool, &ptr, K_NO_WAIT);
|
|
|
|
if (rc) {
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nffs_os_mempool_free(nffs_os_mempool_t *pool, void *block)
|
|
|
|
{
|
|
|
|
k_mem_slab_free(pool, &block);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:49:49 +08:00
|
|
|
int nffs_os_flash_read(uint8_t id, uint32_t address, void *dst,
|
|
|
|
uint32_t num_bytes)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = flash_read(flash_dev, address, dst, num_bytes);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:49:49 +08:00
|
|
|
int nffs_os_flash_write(uint8_t id, uint32_t address, const void *src,
|
|
|
|
uint32_t num_bytes)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = flash_write_protection_set(flash_dev, false);
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = flash_write(flash_dev, address, src, num_bytes);
|
|
|
|
|
|
|
|
/* Ignore errors here - this does not affect write operation */
|
|
|
|
(void) flash_write_protection_set(flash_dev, true);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:49:49 +08:00
|
|
|
int nffs_os_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = flash_write_protection_set(flash_dev, false);
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = flash_erase(flash_dev, address, num_bytes);
|
|
|
|
|
|
|
|
/* Ignore errors here - this does not affect erase operation */
|
|
|
|
(void) flash_write_protection_set(flash_dev, true);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:49:49 +08:00
|
|
|
int nffs_os_flash_info(uint8_t id, uint32_t sector, uint32_t *address,
|
|
|
|
uint32_t *size)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
struct flash_pages_info pi;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = flash_get_page_info_by_idx(flash_dev, sector, &pi);
|
|
|
|
__ASSERT(rc == 0, "Failed to obtain flash page data");
|
|
|
|
|
|
|
|
*address = pi.start_offset;
|
|
|
|
*size = pi.size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-22 04:49:49 +08:00
|
|
|
uint16_t nffs_os_crc16_ccitt(uint16_t initial_crc, const void *buf, int len,
|
|
|
|
int final)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
return crc16(buf, len, 0x1021, initial_crc, final);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inode_to_dirent(struct nffs_inode_entry *inode,
|
|
|
|
struct fs_dirent *entry)
|
|
|
|
{
|
|
|
|
u8_t name_len;
|
2017-12-22 04:49:49 +08:00
|
|
|
uint32_t size;
|
2017-08-08 21:12:55 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = nffs_inode_read_filename(inode, sizeof(entry->name), entry->name,
|
|
|
|
&name_len);
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nffs_hash_id_is_dir(inode->nie_hash_entry.nhe_id)) {
|
|
|
|
entry->type = FS_DIR_ENTRY_DIR;
|
|
|
|
entry->size = 0;
|
|
|
|
} else {
|
|
|
|
entry->type = FS_DIR_ENTRY_FILE;
|
|
|
|
nffs_inode_data_len(inode, &size);
|
|
|
|
entry->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_open(struct fs_file_t *zfp, const char *file_name)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2018-04-17 00:44:48 +08:00
|
|
|
int rc, match_len;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
zfp->filep = NULL;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
if (!nffs_misc_ready()) {
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
match_len = strlen(zfp->mp->mnt_point);
|
|
|
|
rc = nffs_file_open((struct nffs_file **)&zfp->filep, &file_name[match_len],
|
2017-08-08 21:12:55 +08:00
|
|
|
FS_ACCESS_READ | FS_ACCESS_WRITE);
|
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_close(struct fs_file_t *zfp)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_file_close(zfp->filep);
|
2017-08-08 21:12:55 +08:00
|
|
|
if (!rc) {
|
2018-04-17 00:44:48 +08:00
|
|
|
zfp->filep = NULL;
|
2017-08-08 21:12:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_unlink(struct fs_mount_t *mountp, const char *path)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2018-04-17 00:44:48 +08:00
|
|
|
int rc, match_len;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
match_len = strlen(mountp->mnt_point);
|
|
|
|
rc = nffs_path_unlink(&path[match_len]);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static ssize_t nffs_read(struct fs_file_t *zfp, void *ptr, size_t size)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2017-12-22 04:49:49 +08:00
|
|
|
uint32_t br;
|
2017-08-08 21:12:55 +08:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_file_read(zfp->filep, size, ptr, &br);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return br;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static ssize_t nffs_write(struct fs_file_t *zfp, const void *ptr, size_t size)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_write_to_file(zfp->filep, ptr, size);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need to assume all bytes were written */
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_seek(struct fs_file_t *zfp, off_t offset, int whence)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2017-12-22 04:49:49 +08:00
|
|
|
uint32_t len;
|
2017-08-08 21:12:55 +08:00
|
|
|
u32_t pos;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
|
|
|
switch (whence) {
|
|
|
|
case FS_SEEK_SET:
|
|
|
|
pos = offset;
|
|
|
|
break;
|
|
|
|
case FS_SEEK_CUR:
|
2018-04-17 00:44:48 +08:00
|
|
|
pos = ((struct nffs_file *)zfp->filep)->nf_offset + offset;
|
2017-08-08 21:12:55 +08:00
|
|
|
break;
|
|
|
|
case FS_SEEK_END:
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_inode_data_len(((struct nffs_file *)zfp->filep)->nf_inode_entry, &len);
|
2017-08-08 21:12:55 +08:00
|
|
|
if (rc) {
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
pos = len + offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_file_seek(zfp->filep, pos);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static off_t nffs_tell(struct fs_file_t *zfp)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
u32_t offset;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
if (!zfp->filep) {
|
2017-08-08 21:12:55 +08:00
|
|
|
k_mutex_unlock(&nffs_lock);
|
2018-04-14 02:58:17 +08:00
|
|
|
return -EIO;
|
2017-08-08 21:12:55 +08:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
offset = ((struct nffs_file *)zfp->filep)->nf_offset;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_truncate(struct fs_file_t *zfp, off_t length)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* There is no API in NFFS to truncate opened file. For now we return
|
|
|
|
* ENOTSUP, but this should be revisited if truncation is implemented
|
|
|
|
* in NFFS at some point.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_sync(struct fs_file_t *zfp)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Files are written to flash immediately so we do not need to support
|
|
|
|
* sync call - just return success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_mkdir(struct fs_mount_t *mountp, const char *path)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2018-04-17 00:44:48 +08:00
|
|
|
int rc, match_len;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
|
|
|
if (!nffs_misc_ready()) {
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
match_len = strlen(mountp->mnt_point);
|
|
|
|
rc = nffs_path_new_dir(&path[match_len], NULL);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_opendir(struct fs_dir_t *zdp, const char *path)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2018-04-17 00:44:48 +08:00
|
|
|
int rc, match_len;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
zdp->dirp = NULL;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
if (!nffs_misc_ready()) {
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
match_len = strlen(zdp->mp->mnt_point);
|
|
|
|
rc = nffs_dir_open(&path[match_len], (struct nffs_dir **)&zdp->dirp);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_readdir(struct fs_dir_t *zdp, struct fs_dirent *entry)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
struct nffs_dirent *dirent;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_dir_read(zdp->dirp, &dirent);
|
2017-08-08 21:12:55 +08:00
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
rc = inode_to_dirent(dirent->nde_inode_entry, entry);
|
|
|
|
break;
|
|
|
|
case FS_ENOENT:
|
|
|
|
entry->name[0] = 0;
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_closedir(struct fs_dir_t *zdp)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
rc = nffs_dir_close(zdp->dirp);
|
2017-08-08 21:12:55 +08:00
|
|
|
if (!rc) {
|
2018-04-17 00:44:48 +08:00
|
|
|
zdp->dirp = NULL;
|
2017-08-08 21:12:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_stat(struct fs_mount_t *mountp,
|
|
|
|
const char *path, struct fs_dirent *entry)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
struct nffs_path_parser parser;
|
|
|
|
struct nffs_inode_entry *parent;
|
|
|
|
struct nffs_inode_entry *inode;
|
2018-04-17 00:44:48 +08:00
|
|
|
int rc, match_len;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
match_len = strlen(mountp->mnt_point);
|
|
|
|
nffs_path_parser_new(&parser, &path[match_len]);
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
rc = nffs_path_find(&parser, &inode, &parent);
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = inode_to_dirent(inode, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_statvfs(struct fs_mount_t *mountp,
|
|
|
|
const char *path, struct fs_statvfs *stat)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* There is not API to retrieve such data in NFFS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-02-27 19:26:14 +08:00
|
|
|
static int nffs_rename(struct fs_mount_t *mountp, const char *from,
|
|
|
|
const char *to)
|
|
|
|
{
|
2018-04-17 00:44:48 +08:00
|
|
|
int rc, match_len;
|
2018-02-27 19:26:14 +08:00
|
|
|
|
|
|
|
k_mutex_lock(&nffs_lock, K_FOREVER);
|
|
|
|
|
|
|
|
if (!nffs_misc_ready()) {
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:44:48 +08:00
|
|
|
match_len = strlen(mountp->mnt_point);
|
|
|
|
rc = nffs_path_rename(&from[match_len], &to[match_len]);
|
2018-02-27 19:26:14 +08:00
|
|
|
|
|
|
|
k_mutex_unlock(&nffs_lock);
|
|
|
|
|
|
|
|
return translate_error(rc);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
static int nffs_mount(struct fs_mount_t *mountp)
|
2017-08-08 21:12:55 +08:00
|
|
|
{
|
2018-02-22 16:02:57 +08:00
|
|
|
struct nffs_flash_desc *flash_desc =
|
|
|
|
(struct nffs_flash_desc *)mountp->fs_data;
|
2017-08-08 21:12:55 +08:00
|
|
|
int cnt;
|
|
|
|
int rc;
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
/* Set flash device */
|
2018-04-07 01:20:02 +08:00
|
|
|
flash_dev = (struct device *)mountp->storage_dev;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
/* Set flash descriptor fields */
|
|
|
|
flash_desc->id = 0;
|
|
|
|
flash_desc->sector_count = flash_get_page_count(flash_dev);
|
2018-04-19 22:55:53 +08:00
|
|
|
flash_desc->area_offset = FLASH_AREA_STORAGE_OFFSET;
|
|
|
|
flash_desc->area_size = FLASH_AREA_STORAGE_SIZE;
|
2017-08-08 21:12:55 +08:00
|
|
|
|
|
|
|
rc = nffs_misc_reset();
|
|
|
|
if (rc) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
cnt = CONFIG_NFFS_FILESYSTEM_MAX_AREAS;
|
2018-02-22 16:02:57 +08:00
|
|
|
rc = nffs_misc_desc_from_flash_area(flash_desc, &cnt, descs);
|
2017-08-08 21:12:55 +08:00
|
|
|
if (rc) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = nffs_restore_full(descs);
|
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case FS_ECORRUPT:
|
|
|
|
rc = nffs_format_full(descs);
|
|
|
|
if (rc) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:57 +08:00
|
|
|
/* File system interface */
|
|
|
|
static struct fs_file_system_t nffs_fs = {
|
|
|
|
.open = nffs_open,
|
|
|
|
.close = nffs_close,
|
|
|
|
.read = nffs_read,
|
|
|
|
.write = nffs_write,
|
|
|
|
.lseek = nffs_seek,
|
|
|
|
.tell = nffs_tell,
|
|
|
|
.truncate = nffs_truncate,
|
|
|
|
.sync = nffs_sync,
|
|
|
|
.opendir = nffs_opendir,
|
|
|
|
.readdir = nffs_readdir,
|
|
|
|
.closedir = nffs_closedir,
|
|
|
|
.mount = nffs_mount,
|
|
|
|
.unlink = nffs_unlink,
|
2018-02-27 19:26:14 +08:00
|
|
|
.rename = nffs_rename,
|
2018-02-22 16:02:57 +08:00
|
|
|
.mkdir = nffs_mkdir,
|
|
|
|
.stat = nffs_stat,
|
|
|
|
.statvfs = nffs_statvfs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int nffs_init(struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
k_mutex_init(&nffs_lock);
|
|
|
|
|
|
|
|
return fs_register(FS_NFFS, &nffs_fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(nffs_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|