mnt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2023-04-27 huanghe first version
  11. * 2023-07-14 liqiaozhong add SD file sys mount func
  12. *
  13. */
  14. #include <rtthread.h>
  15. #if defined(RT_USING_DFS)
  16. #include <rtdbg.h>
  17. #include <dfs_fs.h>
  18. #include <dfs_file.h>
  19. static int ram_disk_mount(const char *mount_point)
  20. {
  21. #ifdef RT_USING_DFS_RAMFS
  22. extern struct dfs_ramfs *dfs_ramfs_create(rt_uint8_t *pool, rt_size_t size);
  23. rt_uint8_t *pool = RT_NULL;
  24. rt_size_t size = 8 * 1024 * 1024;
  25. rt_err_t err = RT_EOK;
  26. pool = rt_malloc(size);
  27. if (pool == RT_NULL)
  28. {
  29. LOG_E("Malloc fail!");
  30. }
  31. err = dfs_mount(RT_NULL, mount_point, "ram", 0, (const void *)dfs_ramfs_create(pool, size));
  32. if (err == RT_EOK)
  33. {
  34. LOG_I("RAM file system initializated!");
  35. }
  36. else
  37. {
  38. LOG_E("RAM file system initializate failed!, err = %d", err);
  39. }
  40. #endif
  41. return RT_EOK;
  42. }
  43. #ifdef BSP_USING_SDCARD_FATFS
  44. extern void fsdif_change(void);
  45. static int sd_disk_try_mount(char *device_name, char *mount_point, char *fs_type_name, int mkfs_count)
  46. {
  47. struct statfs fs_stat;
  48. int rc = 0;
  49. LOG_I("mount(\"%s\",\"%s\",\"%s\");", device_name, mount_point, fs_type_name);
  50. if (rt_device_find(device_name) == NULL)
  51. {
  52. LOG_I("%s not find!!!", device_name);
  53. return -RT_EIO;
  54. }
  55. mkdir(mount_point, 0);
  56. _remount:
  57. rc = dfs_mount(device_name, mount_point, fs_type_name, 0, 0);
  58. if (rc == 0)
  59. {
  60. LOG_I("mounted %s on %s", device_name, mount_point);
  61. if (dfs_statfs(mount_point, &fs_stat) >= 0)
  62. {
  63. LOG_I("%s size:%d, total: %d, free: %d", mount_point,
  64. fs_stat.f_bsize, fs_stat.f_blocks, fs_stat.f_bfree);
  65. }
  66. }
  67. else
  68. {
  69. if (mkfs_count > 0)
  70. {
  71. /* LOG_I("[%s]try mkfs -t %s %s ", mkfs_count, fs_type_name, device_name);
  72. dfs_mkfs(fs_type_name, device_name); */
  73. mkfs_count--;
  74. LOG_E("%s is not in %s, please format first !!!", device_name, fs_type_name);
  75. goto _remount;
  76. }
  77. LOG_I("mount failed :%d ", rc);
  78. return -RT_EIO;
  79. }
  80. return RT_EOK;
  81. }
  82. static void sd_filesytem_task_entry(void *parameter)
  83. {
  84. int result;
  85. LOG_D("sdio host change: %d", change);
  86. mmcsd_wait_cd_changed(0); /* clear */
  87. fsdif_change(); /* send cd change to host */
  88. /* block until plug/unplug event happens */
  89. result = mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  90. if (result == MMCSD_HOST_PLUGED)
  91. {
  92. rt_kprintf("mmcsd change pluged \n");
  93. /* mount sdcard partition as / */
  94. if (RT_EOK == sd_disk_try_mount(BSP_USING_SDCARD_PARTITION, "/", "elm", 0))
  95. {
  96. ram_disk_mount("/ram"); /* mount ramdisk if configured */
  97. }
  98. }
  99. }
  100. int filesystem_mount(void)
  101. {
  102. rt_thread_t tid;
  103. tid = rt_thread_create("sd_filesytem", sd_filesytem_task_entry,
  104. RT_NULL,
  105. 4096,
  106. RT_THREAD_PRIORITY_MAX - 2, 20);
  107. if (tid != RT_NULL)
  108. {
  109. rt_thread_startup(tid);
  110. }
  111. else
  112. {
  113. LOG_E("create sd mount task error!");
  114. }
  115. return RT_EOK;
  116. }
  117. INIT_APP_EXPORT(filesystem_mount);
  118. #else
  119. static int filesystem_mount(void)
  120. {
  121. return ram_disk_mount("/"); /* mount ramdisk as / */
  122. }
  123. INIT_ENV_EXPORT(filesystem_mount);
  124. #endif // #ifdef BSP_USING_SDCARD_FATFS
  125. #endif // #if defined(RT_USING_DFS)