1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2024-06-18     Shell        add cromfs support
9  */
10 
11 #define DBG_TAG "app.filesystem"
12 #define DBG_LVL DBG_LOG
13 #include <rtdbg.h>
14 
15 #include <dfs_cromfs.h>
16 #include <dfs_posix.h>
17 #include <dfs_fs.h>
18 #include <ioremap.h>
19 #include <mmu.h>
20 #include <rtthread.h>
21 
22 #include <unistd.h>
23 
24 struct _mount_table
25 {
26     char *dev_name;
27     char *mount_point;
28     char *fs_name;
29     long rwflag;
30     void *data;
31 };
32 
33 struct _mount_table _mount_table[] = {
34     [0] = {NULL, "/", "crom", 0, 0},
35 
36 };
37 
_wait_device_ready(const char * devname)38 static int _wait_device_ready(const char* devname)
39 {
40     int k;
41 
42     for(k = 0; k < 10; k++)
43     {
44         if (rt_device_find(devname) != RT_NULL)
45         {
46             return 1;
47         }
48         rt_thread_mdelay(50);
49     }
50 
51     return 0;
52 }
53 
mnt_init(void)54 int mnt_init(void)
55 {
56     int i;
57     uint32_t crom_data_len = 0;
58     uint32_t length;
59 
60     _mount_table[0].data = cromfs_get_partition_data(&length);
61     crom_data_len = length;
62 
63     if (_mount_table[0].data && (crom_data_len > 0))
64     {
65         for (i = 0; i < sizeof(_mount_table) / sizeof(_mount_table[0]); i++)
66         {
67             if (_mount_table[i].dev_name && !_wait_device_ready(_mount_table[i].dev_name))
68             {
69                 LOG_E("device %s find timeout", _mount_table[i].dev_name);
70                 continue;
71             }
72 
73             if (dfs_mount(_mount_table[i].dev_name, _mount_table[i].mount_point,
74                         _mount_table[i].fs_name, _mount_table[i].rwflag, _mount_table[i].data) != 0)
75             {
76                 LOG_E("Dir %s %s mount failed!", _mount_table[i].mount_point,
77                     _mount_table[i].dev_name ? _mount_table[i].dev_name : _mount_table[i].fs_name);
78             }
79             else
80             {
81                 LOG_I("Dir %s %s mount ok!", _mount_table[i].mount_point,
82                     _mount_table[i].dev_name ? _mount_table[i].dev_name : _mount_table[i].fs_name);
83             }
84         }
85     }
86     else
87     {
88         LOG_E("No mount data found!");
89     }
90 
91     mkdir("/dev/shm", 0777);
92 
93     if (dfs_mount(NULL, "/dev/shm", "tmp", 0, 0) != 0)
94     {
95         LOG_E("Dir %s %s mount failed!", "/dev/shm", "tmp");
96     }
97     else
98     {
99         LOG_I("Dir %s %s mount ok!", "/dev/shm", "tmp");
100     }
101 
102     LOG_I("file system initialization done!\n");
103 
104     return 0;
105 }
106 INIT_ENV_EXPORT(mnt_init);
107