mnt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. pool = rt_malloc(size);
  26. if (pool == RT_NULL)
  27. {
  28. LOG_E("Malloc fail!");
  29. }
  30. if (dfs_mount(RT_NULL, mount_point, "ram", 0, (const void *)dfs_ramfs_create(pool, size)) == 0)
  31. {
  32. LOG_I("RAM file system initializated!");
  33. }
  34. else
  35. {
  36. LOG_E("RAM file system initializate failed!");
  37. }
  38. #endif
  39. return RT_EOK;
  40. }
  41. #ifdef BSP_USING_SDCARD_FATFS
  42. extern void fsdif_change(void);
  43. static int sd_disk_try_mount(char *device_name, char *mount_point, char *fs_type_name, int mkfs_count)
  44. {
  45. struct statfs fs_stat;
  46. int rc = 0;
  47. LOG_I("mount(\"%s\",\"%s\",\"%s\");", device_name, mount_point, fs_type_name);
  48. if (rt_device_find(device_name) == NULL)
  49. {
  50. LOG_I("%s not find!!!", device_name);
  51. return -RT_EIO;
  52. }
  53. mkdir(mount_point, 0);
  54. _remount:
  55. rc = dfs_mount(device_name, mount_point, fs_type_name, 0, 0);
  56. if (rc == 0)
  57. {
  58. LOG_I("mounted %s on %s", device_name, mount_point);
  59. if (dfs_statfs(mount_point, &fs_stat) >= 0)
  60. {
  61. LOG_I("%s size:%d, total: %d, free: %d", mount_point,
  62. fs_stat.f_bsize, fs_stat.f_blocks, fs_stat.f_bfree);
  63. }
  64. }
  65. else
  66. {
  67. if (mkfs_count > 0)
  68. {
  69. /* LOG_I("[%s]try mkfs -t %s %s ", mkfs_count, fs_type_name, device_name);
  70. dfs_mkfs(fs_type_name, device_name); */
  71. mkfs_count--;
  72. LOG_E("%s is not in %s, please format first !!!", device_name, fs_type_name);
  73. goto _remount;
  74. }
  75. LOG_I("mount failed :%d ", rc);
  76. return -RT_EIO;
  77. }
  78. return RT_EOK;
  79. }
  80. static void sd_filesytem_task_entry(void *parameter)
  81. {
  82. int result;
  83. LOG_D("sdio host change: %d", change);
  84. mmcsd_wait_cd_changed(0); /* clear */
  85. fsdif_change(); /* send cd change to host */
  86. /* block until plug/unplug event happens */
  87. result = mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
  88. if (result == MMCSD_HOST_PLUGED)
  89. {
  90. rt_kprintf("mmcsd change pluged \n");
  91. /* mount sdcard partition as / */
  92. if (RT_EOK == sd_disk_try_mount(BSP_USING_SDCARD_PARTITION, "/", "elm", 0))
  93. {
  94. ram_disk_mount("/ram"); /* mount ramdisk if configured */
  95. }
  96. }
  97. }
  98. int filesystem_mount(void)
  99. {
  100. rt_thread_t tid;
  101. tid = rt_thread_create("sd_filesytem", sd_filesytem_task_entry,
  102. RT_NULL,
  103. 4096,
  104. RT_THREAD_PRIORITY_MAX - 2, 20);
  105. if (tid != RT_NULL)
  106. {
  107. rt_thread_startup(tid);
  108. }
  109. else
  110. {
  111. LOG_E("create sd mount task error!");
  112. }
  113. return RT_EOK;
  114. }
  115. INIT_APP_EXPORT(filesystem_mount);
  116. #else
  117. static int filesystem_mount(void)
  118. {
  119. return ram_disk_mount("/"); /* mount ramdisk as / */
  120. }
  121. INIT_APP_EXPORT(filesystem_mount);
  122. #endif // #ifdef BSP_USING_SDCARD_FATFS
  123. #endif // #if defined(RT_USING_DFS)