115 lines
2.4 KiB
C
115 lines
2.4 KiB
C
#include <rtthread.h>
|
|
#include <rtdevice.h>
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include "fal.h"
|
|
|
|
#if defined(RT_USING_DFS)
|
|
#include <dfs_fs.h>
|
|
#endif
|
|
|
|
#define DBG_TAG "falcfg"
|
|
#define DBG_LVL DBG_INFO
|
|
#include <rtdbg.h>
|
|
|
|
#if defined(RT_USING_DFS) && defined(RT_USING_MTD_NOR)
|
|
|
|
static const char g_fal_part_name[] = "config";
|
|
static const char g_fal_mount_path[] = "/";
|
|
static const char g_fal_fs_name[] = "lfs";
|
|
|
|
static rt_bool_t fal_flashfs_partition_is_blank(void)
|
|
{
|
|
uint8_t probe[256];
|
|
const struct fal_partition *part;
|
|
rt_size_t i;
|
|
|
|
part = fal_partition_find(g_fal_part_name);
|
|
if (!part)
|
|
{
|
|
return RT_FALSE;
|
|
}
|
|
|
|
if (fal_partition_read(part, 0, probe, sizeof(probe)) != sizeof(probe))
|
|
{
|
|
return RT_FALSE;
|
|
}
|
|
|
|
for (i = 0; i < sizeof(probe); ++i)
|
|
{
|
|
if (probe[i] != 0xFF)
|
|
{
|
|
return RT_FALSE;
|
|
}
|
|
}
|
|
|
|
return RT_TRUE;
|
|
}
|
|
|
|
static int fal_flashfs_mount(void)
|
|
{
|
|
(void)mkdir(g_fal_mount_path, 0);
|
|
|
|
if (dfs_mount(g_fal_part_name, g_fal_mount_path, g_fal_fs_name, 0, 0) == 0)
|
|
{
|
|
LOG_I("mounted %s at %s as %s",
|
|
g_fal_part_name,
|
|
g_fal_mount_path,
|
|
g_fal_fs_name);
|
|
return RT_EOK;
|
|
}
|
|
|
|
return -RT_ERROR;
|
|
}
|
|
|
|
int fal_flashfs_mount_init(void)
|
|
{
|
|
rt_device_t dev;
|
|
|
|
if (fal_init() <= 0)
|
|
{
|
|
LOG_E("fal init failed");
|
|
return -RT_ERROR;
|
|
}
|
|
|
|
dev = rt_device_find(g_fal_part_name);
|
|
if (!dev)
|
|
{
|
|
dev = fal_mtd_nor_device_create(g_fal_part_name);
|
|
if (!dev)
|
|
{
|
|
LOG_E("create MTD NOR device %s failed", g_fal_part_name);
|
|
return -RT_ERROR;
|
|
}
|
|
}
|
|
|
|
LOG_I("using partition %s as MTD NOR device for %s", g_fal_part_name, g_fal_fs_name);
|
|
|
|
if (fal_flashfs_mount() == RT_EOK)
|
|
{
|
|
return RT_EOK;
|
|
}
|
|
|
|
if (fal_flashfs_partition_is_blank())
|
|
{
|
|
LOG_W("partition %s is blank, formatting as %s", g_fal_part_name, g_fal_fs_name);
|
|
if ((dfs_mkfs(g_fal_fs_name, g_fal_part_name) == 0) &&
|
|
(fal_flashfs_mount() == RT_EOK))
|
|
{
|
|
return RT_EOK;
|
|
}
|
|
}
|
|
|
|
LOG_W("mount %s failed; you can try manually: mkfs -t %s %s",
|
|
g_fal_mount_path,
|
|
g_fal_fs_name,
|
|
g_fal_part_name);
|
|
return RT_EOK;
|
|
}
|
|
INIT_ENV_EXPORT(fal_flashfs_mount_init);
|
|
|
|
#endif /* RT_USING_DFS && RT_USING_MTD_NOR */
|