|
| 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 | + * 2023-02-21 GuEe-GUI first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <rtthread.h> |
| 12 | +#include <rtdevice.h> |
| 13 | + |
| 14 | +#define DBG_TAG "rtdm.dfs" |
| 15 | +#define DBG_LVL DBG_INFO |
| 16 | +#include <rtdbg.h> |
| 17 | + |
| 18 | +#include <stdlib.h> |
| 19 | +#include <dfs_fs.h> |
| 20 | +#ifdef RT_USING_FINSH |
| 21 | +#include <msh.h> |
| 22 | +#endif |
| 23 | +#include <ioremap.h> |
| 24 | + |
| 25 | +#ifdef RT_USING_OFW |
| 26 | +#define bootargs_select rt_ofw_bootargs_select |
| 27 | +#define memregion_request rt_fdt_commit_memregion_request |
| 28 | +#else |
| 29 | +#error Platform have not kernel parameters select interfaces! |
| 30 | +#endif |
| 31 | + |
| 32 | +static int rootfs_mnt_init(void) |
| 33 | +{ |
| 34 | + rt_err_t err = -RT_ERROR; |
| 35 | + void *fsdata = RT_NULL; |
| 36 | + const char *cromfs_type = "crom"; |
| 37 | + const char *dev = bootargs_select("root=", 0); |
| 38 | + const char *fstype = bootargs_select("rootfstype=", 0); |
| 39 | + const char *rw = bootargs_select("rw", 0); |
| 40 | + |
| 41 | + if ((!dev || !fstype)) |
| 42 | + { |
| 43 | + const char *name = "initrd"; |
| 44 | + rt_size_t mem_region_nr; |
| 45 | + rt_region_t *mem_region; |
| 46 | + rt_uint64_t initrd_start = 0, initrd_end = 0; |
| 47 | + |
| 48 | + if (!memregion_request(&mem_region, &mem_region_nr, RT_TRUE)) |
| 49 | + { |
| 50 | + while (mem_region_nr-- > 0) |
| 51 | + { |
| 52 | + if (mem_region->name == name || !rt_strcmp(mem_region->name, name)) |
| 53 | + { |
| 54 | + initrd_start = mem_region->start; |
| 55 | + initrd_end = mem_region->end; |
| 56 | + |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + mem_region++; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (initrd_start && initrd_end) |
| 65 | + { |
| 66 | + size_t initrd_size = initrd_end - initrd_start; |
| 67 | + |
| 68 | + if ((fsdata = rt_ioremap_cached((void *)initrd_start, initrd_size))) |
| 69 | + { |
| 70 | + fstype = cromfs_type; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (fstype != cromfs_type && dev) |
| 76 | + { |
| 77 | + rt_tick_t timeout = 0; |
| 78 | + const char *rootwait, *rootdelay = RT_NULL; |
| 79 | + |
| 80 | + rootwait = bootargs_select("rootwait", 0); |
| 81 | + |
| 82 | + /* Maybe it is undefined or 'rootwaitABC' */ |
| 83 | + if (!rootwait || *rootwait) |
| 84 | + { |
| 85 | + rootdelay = bootargs_select("rootdelay=", 0); |
| 86 | + |
| 87 | + if (rootdelay) |
| 88 | + { |
| 89 | + timeout = rt_tick_from_millisecond(atoi(rootdelay)); |
| 90 | + } |
| 91 | + |
| 92 | + rootwait = RT_NULL; |
| 93 | + } |
| 94 | + |
| 95 | + /* |
| 96 | + * Delays in boot flow is a terrible behavior in RTOS, but the RT-Thread |
| 97 | + * SDIO framework init the devices in a task that we need to wait for |
| 98 | + * SDIO devices to init complete... |
| 99 | + * |
| 100 | + * WHAT THE F*CK PROBLEMS WILL HAPPENED? |
| 101 | + * |
| 102 | + * Your main PE, applications, services that depend on the root FS and |
| 103 | + * the multi cores setup, init will delay, too... |
| 104 | + * |
| 105 | + * So, you can try to link this function to `INIT_APP_EXPORT` even later |
| 106 | + * and remove the delays if you want to optimize the boot time and mount |
| 107 | + * the FS auto. |
| 108 | + */ |
| 109 | + for (; rootdelay || rootwait; --timeout) |
| 110 | + { |
| 111 | + if (!rootwait && timeout == 0) |
| 112 | + { |
| 113 | + LOG_E("Wait for /dev/%s init time out", dev); |
| 114 | + |
| 115 | + /* |
| 116 | + * We don't return at once because the device driver may init OK |
| 117 | + * when we break from this point, might as well give it another |
| 118 | + * try. |
| 119 | + */ |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + if (rt_device_find(dev)) |
| 124 | + { |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + rt_thread_mdelay(1); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (fstype) |
| 133 | + { |
| 134 | + if (!(err = dfs_mount(dev, "/", fstype, rw ? 0 : ~0, fsdata))) |
| 135 | + { |
| 136 | + LOG_I("Mount root %s%s type=%s %s", |
| 137 | + (dev && *dev) ? "on /dev/" : "", |
| 138 | + (dev && *dev) ? dev : "\b", |
| 139 | + fstype, "done"); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + LOG_W("Mount root %s%s type=%s %s", |
| 144 | + (dev && *dev) ? "on /dev/" : "", |
| 145 | + (dev && *dev) ? dev : "\b", |
| 146 | + fstype, "fail"); |
| 147 | + |
| 148 | + if (fstype == cromfs_type) |
| 149 | + { |
| 150 | + rt_iounmap(fsdata); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return 0; |
| 156 | +} |
| 157 | +INIT_ENV_EXPORT(rootfs_mnt_init); |
| 158 | + |
| 159 | +static int fstab_mnt_init(void) |
| 160 | +{ |
| 161 | + mkdir("/mnt", 0755); |
| 162 | + |
| 163 | +#ifdef RT_USING_FINSH |
| 164 | + /* Try mount by table */ |
| 165 | + msh_exec_script("fstab.sh", 16); |
| 166 | +#endif |
| 167 | + |
| 168 | + LOG_I("File system initialization done!\n"); |
| 169 | + |
| 170 | + return 0; |
| 171 | +} |
| 172 | +INIT_FS_EXPORT(fstab_mnt_init); |
0 commit comments