2023-05-11 10:25:21 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Email: opensource_embedded@phytium.com.cn
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2023-04-27 huanghe first version
|
2023-08-02 13:27:09 +08:00
|
|
|
* 2023-07-14 liqiaozhong add SD file sys mount func
|
2023-05-11 10:25:21 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <rtthread.h>
|
2023-08-02 13:27:09 +08:00
|
|
|
#if defined(RT_USING_DFS)
|
|
|
|
#include <rtdbg.h>
|
2023-05-11 10:25:21 +08:00
|
|
|
|
|
|
|
#include <dfs_fs.h>
|
|
|
|
#include <dfs_file.h>
|
2023-08-02 13:27:09 +08:00
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
static int ram_disk_mount(const char *mount_point)
|
2023-05-11 10:25:21 +08:00
|
|
|
{
|
2023-08-02 13:27:09 +08:00
|
|
|
#ifdef RT_USING_DFS_RAMFS
|
|
|
|
extern struct dfs_ramfs *dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size);
|
|
|
|
|
|
|
|
rt_uint8_t *pool = RT_NULL;
|
|
|
|
rt_size_t size = 8 * 1024 * 1024;
|
2023-11-21 17:42:23 +08:00
|
|
|
rt_err_t err = RT_EOK;
|
2023-08-02 13:27:09 +08:00
|
|
|
|
|
|
|
pool = rt_malloc(size);
|
|
|
|
if (pool == RT_NULL)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2023-08-02 13:27:09 +08:00
|
|
|
LOG_E("Malloc fail!");
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
err = dfs_mount(RT_NULL, mount_point, "ram", 0, (const void *)dfs_ramfs_create(pool, size));
|
|
|
|
if (err == RT_EOK)
|
|
|
|
{
|
2023-08-02 13:27:09 +08:00
|
|
|
LOG_I("RAM file system initializated!");
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
else
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
LOG_E("RAM file system initializate failed!, err = %d", err);
|
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
#endif
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2023-05-11 10:25:21 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
#ifdef BSP_USING_SDCARD_FATFS
|
|
|
|
extern void fsdif_change(void);
|
|
|
|
static int sd_disk_try_mount(char *device_name, char *mount_point, char *fs_type_name, int mkfs_count)
|
2023-08-02 13:27:09 +08:00
|
|
|
{
|
2023-11-21 17:42:23 +08:00
|
|
|
struct statfs fs_stat;
|
|
|
|
int rc = 0;
|
2023-08-02 13:27:09 +08:00
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
LOG_I("mount(\"%s\",\"%s\",\"%s\");", device_name, mount_point, fs_type_name);
|
2023-08-02 13:27:09 +08:00
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
if (rt_device_find(device_name) == NULL)
|
|
|
|
{
|
|
|
|
LOG_I("%s not find!!!", device_name);
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
mkdir(mount_point, 0);
|
|
|
|
_remount:
|
|
|
|
rc = dfs_mount(device_name, mount_point, fs_type_name, 0, 0);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
LOG_I("mounted %s on %s", device_name, mount_point);
|
|
|
|
if (dfs_statfs(mount_point, &fs_stat) >= 0)
|
|
|
|
{
|
|
|
|
LOG_I("%s size:%d, total: %d, free: %d", mount_point,
|
|
|
|
fs_stat.f_bsize, fs_stat.f_blocks, fs_stat.f_bfree);
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
else
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
if (mkfs_count > 0)
|
|
|
|
{
|
|
|
|
/* LOG_I("[%s]try mkfs -t %s %s ", mkfs_count, fs_type_name, device_name);
|
|
|
|
dfs_mkfs(fs_type_name, device_name); */
|
|
|
|
mkfs_count--;
|
|
|
|
LOG_E("%s is not in %s, please format first !!!", device_name, fs_type_name);
|
|
|
|
goto _remount;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_I("mount failed :%d ", rc);
|
|
|
|
return -RT_EIO;
|
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
static void sd_filesytem_task_entry(void *parameter)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
LOG_D("sdio host change: %d", change);
|
|
|
|
mmcsd_wait_cd_changed(0); /* clear */
|
|
|
|
fsdif_change(); /* send cd change to host */
|
|
|
|
|
|
|
|
/* block until plug/unplug event happens */
|
|
|
|
result = mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
|
|
|
|
if (result == MMCSD_HOST_PLUGED)
|
|
|
|
{
|
|
|
|
rt_kprintf("mmcsd change pluged \n");
|
|
|
|
/* mount sdcard partition as / */
|
|
|
|
if (RT_EOK == sd_disk_try_mount(BSP_USING_SDCARD_PARTITION, "/", "elm", 0))
|
|
|
|
{
|
|
|
|
ram_disk_mount("/ram"); /* mount ramdisk if configured */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int filesystem_mount(void)
|
|
|
|
{
|
|
|
|
rt_thread_t tid;
|
|
|
|
tid = rt_thread_create("sd_filesytem", sd_filesytem_task_entry,
|
|
|
|
RT_NULL,
|
|
|
|
4096,
|
|
|
|
RT_THREAD_PRIORITY_MAX - 2, 20);
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_E("create sd mount task error!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
INIT_APP_EXPORT(filesystem_mount);
|
|
|
|
|
|
|
|
#else
|
|
|
|
static int filesystem_mount(void)
|
|
|
|
{
|
|
|
|
return ram_disk_mount("/"); /* mount ramdisk as / */
|
|
|
|
}
|
2023-08-02 13:27:09 +08:00
|
|
|
INIT_ENV_EXPORT(filesystem_mount);
|
|
|
|
#endif // #ifdef BSP_USING_SDCARD_FATFS
|
|
|
|
#endif // #if defined(RT_USING_DFS)
|