1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * on chip filesystem support
7 * Change Logs:
8 * Date Author Notes
9 * 2021-06-27 Chenyingchun first version
10 */
11 #include "board.h"
12 #include <rtthread.h>
13 #include <rtdevice.h>
14
15 #ifdef BSP_USING_ON_CHIP_FS
16
17 #ifndef RT_USING_FAL
18 #error "if you want to use on chip filesystem, you need to enable FAL package()"
19 #endif
20
21 #ifndef RT_USING_DFS
22 #error "if you want to use on chip filesystem, you need to enable DFS componment"
23 #endif
24
25 #ifndef BSP_USING_ON_CHIP_FLASH
26 #error "if you want to use on chip filesystem, you need to enable on-chip flash"
27 #endif
28
29 #ifndef RT_USING_MTD_NOR
30 #error "if you want to use on chip filesystem, you need to enable mtd nor"
31 #endif
32
33 #ifndef PKG_USING_LITTLEFS
34 #error "if you want to use on chip filesystem, you need to enable littlefs"
35 #endif
36
37 #include "fal.h"
38 #include <dfs_file.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <sys/stat.h>
42 #include <sys/statfs.h>
43
44 #define LOG_TAG "drv.fs"
45 #define DBG_LVL DBG_LOG
46 #include <rtdbg.h>
47
48 #define FS_PARTITION_NAME ON_CHIP_PARTION_NAME
49
50 /**
51 * @brief on chip filesystem init
52 * @param void
53 * @retval 0: filesystem init success, -1: filesystem init failed
54 */
55
on_chip_fs_init(void)56 static int on_chip_fs_init(void)
57 {
58 int result = 0;
59
60 fal_init();
61
62 struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
63
64 if (flash_dev == NULL)
65 {
66 LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
67 result = -1;
68 goto err;
69 }
70 else
71 {
72 LOG_D("Create a block device on the %s partition of flash successful.", FS_PARTITION_NAME);
73 }
74
75 if (rt_device_find(FS_PARTITION_NAME) != RT_NULL)
76 {
77 int mkfs_res = dfs_mkfs("lfs", FS_PARTITION_NAME);
78
79 if (mkfs_res != 0)
80 {
81 LOG_E("dfs_mkfs error, errno = %d", rt_get_errno());
82 result = -1;
83 goto err;
84 }
85
86 if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
87 {
88 LOG_D("onchip elm filesystem mount to '/'");
89 }
90 else
91 {
92 LOG_E("onchip elm filesystem mount to '/' failed!");
93 result = -1;
94 goto err;
95 }
96 }
97 else
98 {
99 LOG_E("find filesystem portion failed");
100 }
101 err:
102 return result;
103 }
104
105 INIT_ENV_EXPORT(on_chip_fs_init);
106
107 #endif /* BSP_USING_ON_CHIP_FS */
108