2021-05-12 18:49:31 +08:00
|
|
|
/**************************************************************************//**
|
|
|
|
*
|
|
|
|
* @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-12-12 Wayne First version
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
2022-12-29 15:15:13 +08:00
|
|
|
#if defined(RT_USING_DFS)
|
|
|
|
|
2021-05-12 18:49:31 +08:00
|
|
|
#define LOG_TAG "mnt"
|
|
|
|
#define DBG_ENABLE
|
|
|
|
#define DBG_SECTION_NAME "mnt"
|
|
|
|
#define DBG_LEVEL DBG_ERROR
|
|
|
|
#define DBG_COLOR
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2022-01-26 15:09:29 +08:00
|
|
|
#include <dfs_fs.h>
|
|
|
|
#include <dfs_file.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
2021-05-12 18:49:31 +08:00
|
|
|
|
2022-03-15 09:48:05 +08:00
|
|
|
#if defined(RT_USING_FAL)
|
2021-05-12 18:49:31 +08:00
|
|
|
#include <fal.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PKG_USING_RAMDISK)
|
|
|
|
#define RAMDISK_NAME "ramdisk0"
|
|
|
|
#define RAMDISK_UDC "ramdisk1"
|
|
|
|
#define MOUNT_POINT_RAMDISK0 "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(BOARD_USING_STORAGE_SPIFLASH)
|
|
|
|
#define PARTITION_NAME_FILESYSTEM "filesystem"
|
|
|
|
#define MOUNT_POINT_SPIFLASH0 "/mnt/"PARTITION_NAME_FILESYSTEM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_DFS_MNTTABLE
|
|
|
|
|
|
|
|
/*
|
|
|
|
const char *device_name;
|
|
|
|
const char *path;
|
|
|
|
const char *filesystemtype;
|
|
|
|
unsigned long rwflag;
|
|
|
|
const void *data;
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct dfs_mount_tbl mount_table[] =
|
|
|
|
{
|
|
|
|
#if defined(PKG_USING_RAMDISK)
|
|
|
|
{ RAMDISK_UDC, "/mnt/ram_usbd", "elm", 0, RT_NULL },
|
|
|
|
#endif
|
2022-06-03 10:01:15 +08:00
|
|
|
|
|
|
|
#if defined(BOARD_USING_STORAGE_SPIFLASH)
|
|
|
|
{ PARTITION_NAME_FILESYSTEM, "/mnt/filesystem", "elm", 0, RT_NULL },
|
|
|
|
#endif
|
|
|
|
#if defined(PKG_USING_DFS_YAFFS)
|
|
|
|
{ "nand1", "/mnt/fminand", "yaffs", 0, RT_NULL },
|
|
|
|
#endif
|
|
|
|
|
2021-05-12 18:49:31 +08:00
|
|
|
{0},
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(PKG_USING_RAMDISK)
|
|
|
|
|
|
|
|
extern rt_err_t ramdisk_init(const char *dev_name, rt_uint8_t *disk_addr, rt_size_t block_size, rt_size_t num_block);
|
|
|
|
int ramdisk_device_init(void)
|
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
|
|
|
/* Create a 8MB RAMDISK */
|
|
|
|
result = ramdisk_init(RAMDISK_NAME, NULL, 512, 2 * 4096);
|
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
|
|
/* Create a 4MB RAMDISK */
|
|
|
|
result = ramdisk_init(RAMDISK_UDC, NULL, 512, 2 * 4096);
|
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(ramdisk_device_init);
|
|
|
|
|
|
|
|
/* Recursive mkdir */
|
|
|
|
static int mkdir_p(const char *dir, const mode_t mode)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *tmp = NULL;
|
|
|
|
char *p = NULL;
|
|
|
|
struct stat sb;
|
|
|
|
rt_size_t len;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
goto exit_mkdir_p;
|
|
|
|
|
|
|
|
/* Copy path */
|
|
|
|
/* Get the string length */
|
|
|
|
len = strlen(dir);
|
|
|
|
tmp = rt_strdup(dir);
|
|
|
|
|
|
|
|
/* Remove trailing slash */
|
|
|
|
if (tmp[len - 1] == '/')
|
|
|
|
{
|
|
|
|
tmp[len - 1] = '\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if path exists and is a directory */
|
|
|
|
if (stat(tmp, &sb) == 0)
|
|
|
|
{
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
goto exit_mkdir_p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recursive mkdir */
|
|
|
|
for (p = tmp + 1; p - tmp <= len; p++)
|
|
|
|
{
|
|
|
|
if ((*p == '/') || (p - tmp == len))
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
/* Test path */
|
|
|
|
if (stat(tmp, &sb) != 0)
|
|
|
|
{
|
|
|
|
/* Path does not exist - create directory */
|
|
|
|
if (mkdir(tmp, mode) < 0)
|
|
|
|
{
|
|
|
|
goto exit_mkdir_p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!S_ISDIR(sb.st_mode))
|
|
|
|
{
|
|
|
|
/* Not a directory */
|
|
|
|
goto exit_mkdir_p;
|
|
|
|
}
|
|
|
|
if (p - tmp != len)
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit_mkdir_p:
|
|
|
|
|
|
|
|
if (tmp)
|
|
|
|
rt_free(tmp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
#if defined(PKG_USING_DFS_YAFFS) && defined(RT_USING_DFS_MNTTABLE)
|
|
|
|
#include "yaffs_guts.h"
|
2022-12-29 15:15:13 +08:00
|
|
|
int yaffs_dev_init(void)
|
2022-06-03 10:01:15 +08:00
|
|
|
{
|
|
|
|
int i;
|
2022-12-29 15:15:13 +08:00
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
for (i = 0; i < sizeof(mount_table) / sizeof(struct dfs_mount_tbl); i++)
|
|
|
|
{
|
|
|
|
if (mount_table[i].filesystemtype && !rt_strcmp(mount_table[i].filesystemtype, "yaffs"))
|
|
|
|
{
|
|
|
|
struct rt_mtd_nand_device *psMtdNandDev = RT_MTD_NAND_DEVICE(rt_device_find(mount_table[i].device_name));
|
|
|
|
if (psMtdNandDev)
|
|
|
|
{
|
2022-12-29 15:15:13 +08:00
|
|
|
LOG_I("yaffs start [%s].", mount_table[i].device_name);
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
yaffs_start_up(psMtdNandDev, (const char *)mount_table[i].path);
|
2022-12-29 15:15:13 +08:00
|
|
|
|
|
|
|
LOG_I("dfs mount [%s].", mount_table[i].device_name);
|
|
|
|
if (dfs_mount(mount_table[i].device_name,
|
|
|
|
mount_table[i].path,
|
|
|
|
mount_table[i].filesystemtype,
|
|
|
|
mount_table[i].rwflag,
|
|
|
|
mount_table[i].data) != 0)
|
|
|
|
{
|
|
|
|
LOG_E("mount fs[%s] on %s failed.", mount_table[i].filesystemtype, mount_table[i].path);
|
|
|
|
}
|
2022-06-03 10:01:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 15:15:13 +08:00
|
|
|
|
|
|
|
return 0;
|
2022-06-03 10:01:15 +08:00
|
|
|
}
|
2022-12-29 15:15:13 +08:00
|
|
|
INIT_APP_EXPORT(yaffs_dev_init);
|
2022-06-03 10:01:15 +08:00
|
|
|
#endif
|
|
|
|
|
2021-05-12 18:49:31 +08:00
|
|
|
/* Initialize the filesystem */
|
|
|
|
int filesystem_init(void)
|
|
|
|
{
|
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
#if defined(RT_USING_FAL)
|
|
|
|
extern int fal_init_check(void);
|
|
|
|
if (!fal_init_check())
|
|
|
|
fal_init();
|
|
|
|
#endif
|
|
|
|
|
2021-05-12 18:49:31 +08:00
|
|
|
// ramdisk as root
|
|
|
|
if (!rt_device_find(RAMDISK_NAME))
|
|
|
|
{
|
|
|
|
LOG_E("cannot find %s device", RAMDISK_NAME);
|
2022-06-03 10:01:15 +08:00
|
|
|
result = -RT_ERROR;
|
2021-05-12 18:49:31 +08:00
|
|
|
goto exit_filesystem_init;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Format these ramdisk */
|
|
|
|
result = (rt_err_t)dfs_mkfs("elm", RAMDISK_NAME);
|
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
|
|
|
|
/* mount ramdisk0 as root directory */
|
|
|
|
if (dfs_mount(RAMDISK_NAME, "/", "elm", 0, RT_NULL) == 0)
|
|
|
|
{
|
|
|
|
LOG_I("ramdisk mounted on \"/\".");
|
|
|
|
|
|
|
|
/* now you can create dir dynamically. */
|
|
|
|
mkdir_p("/mnt", 0x777);
|
|
|
|
mkdir_p("/cache", 0x777);
|
|
|
|
mkdir_p("/download", 0x777);
|
|
|
|
mkdir_p("/mnt/ram_usbd", 0x777);
|
2022-06-03 10:01:15 +08:00
|
|
|
mkdir_p("/mnt/filesystem", 0x777);
|
|
|
|
mkdir_p("/mnt/fminand", 0x777);
|
2021-05-12 18:49:31 +08:00
|
|
|
#if defined(RT_USBH_MSTORAGE) && defined(UDISK_MOUNTPOINT)
|
|
|
|
mkdir_p(UDISK_MOUNTPOINT, 0x777);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_E("root folder creation failed!\n");
|
|
|
|
goto exit_filesystem_init;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rt_device_find(RAMDISK_UDC))
|
|
|
|
{
|
|
|
|
LOG_E("cannot find %s device", RAMDISK_UDC);
|
|
|
|
goto exit_filesystem_init;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Format these ramdisk */
|
|
|
|
result = (rt_err_t)dfs_mkfs("elm", RAMDISK_UDC);
|
|
|
|
RT_ASSERT(result == RT_EOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(BOARD_USING_STORAGE_SPIFLASH)
|
|
|
|
{
|
2022-06-03 10:01:15 +08:00
|
|
|
struct rt_device *psNorFlash = fal_blk_device_create(PARTITION_NAME_FILESYSTEM);
|
|
|
|
if (!psNorFlash)
|
|
|
|
{
|
|
|
|
rt_kprintf("Failed to create block device for %s.\n", PARTITION_NAME_FILESYSTEM);
|
|
|
|
}
|
2021-05-12 18:49:31 +08:00
|
|
|
}
|
2022-06-03 10:01:15 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
exit_filesystem_init:
|
2021-05-12 18:49:31 +08:00
|
|
|
|
2022-06-03 10:01:15 +08:00
|
|
|
return -result;
|
2021-05-12 18:49:31 +08:00
|
|
|
}
|
2022-06-03 10:01:15 +08:00
|
|
|
INIT_ENV_EXPORT(filesystem_init);
|
2021-05-12 18:49:31 +08:00
|
|
|
#endif
|
2022-12-29 15:15:13 +08:00
|
|
|
#endif
|