mnt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-1-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtthread.h>
  13. #define LOG_TAG "mnt"
  14. #define DBG_ENABLE
  15. #define DBG_SECTION_NAME "mnt"
  16. #define DBG_LEVEL DBG_ERROR
  17. #define DBG_COLOR
  18. #include <rtdbg.h>
  19. #include <dfs_fs.h>
  20. #include <dfs_file.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <sys/stat.h>
  24. #include <sys/statfs.h>
  25. #if defined(RT_USING_FAL)
  26. #include <fal.h>
  27. #endif
  28. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  29. #define MOUNT_POINT_SPIFLASH0 "/"
  30. #endif
  31. #ifdef RT_USING_DFS_MNTTABLE
  32. /*
  33. const char *device_name;
  34. const char *path;
  35. const char *filesystemtype;
  36. unsigned long rwflag;
  37. const void *data;
  38. */
  39. const struct dfs_mount_tbl mount_table[] =
  40. {
  41. { "sd0", "/mnt/sd0", "elm", 0, RT_NULL },
  42. { "sd0p0", "/mnt/sd0p0", "elm", 0, RT_NULL },
  43. { "sd0p1", "/mnt/sd0p1", "elm", 0, RT_NULL },
  44. { "sd1", "/mnt/sd1", "elm", 0, RT_NULL },
  45. { "sd1p0", "/mnt/sd1p0", "elm", 0, RT_NULL },
  46. { "sd1p1", "/mnt/sd1p1", "elm", 0, RT_NULL },
  47. {0},
  48. };
  49. #endif
  50. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  51. #if defined(RT_USB_DEVICE_MSTORAGE)
  52. int mnt_init_spiflash0(void)
  53. {
  54. rt_kprintf("Sorry, you enabled RT_USB_DEVICE_MSTORAGE option in menu, so we won't mount flash0 on /.\n");
  55. return 0;
  56. }
  57. #else
  58. /* Recursive mkdir */
  59. #if defined(RT_USBH_MSTORAGE) && defined(UDISK_MOUNTPOINT)
  60. static int mkdir_p(const char *dir, const mode_t mode)
  61. {
  62. int ret = -1;
  63. char *tmp = NULL;
  64. char *p = NULL;
  65. struct stat sb;
  66. rt_size_t len;
  67. if (!dir)
  68. goto exit_mkdir_p;
  69. /* Copy path */
  70. /* Get the string length */
  71. len = strlen(dir);
  72. tmp = rt_strdup(dir);
  73. /* Remove trailing slash */
  74. if (tmp[len - 1] == '/')
  75. {
  76. tmp[len - 1] = '\0';
  77. len--;
  78. }
  79. /* check if path exists and is a directory */
  80. if (stat(tmp, &sb) == 0)
  81. {
  82. if (S_ISDIR(sb.st_mode))
  83. {
  84. ret = 0;
  85. goto exit_mkdir_p;
  86. }
  87. }
  88. /* Recursive mkdir */
  89. for (p = tmp + 1; p - tmp <= len; p++)
  90. {
  91. if ((*p == '/') || (p - tmp == len))
  92. {
  93. *p = 0;
  94. /* Test path */
  95. if (stat(tmp, &sb) != 0)
  96. {
  97. /* Path does not exist - create directory */
  98. if (mkdir(tmp, mode) < 0)
  99. {
  100. goto exit_mkdir_p;
  101. }
  102. }
  103. else if (!S_ISDIR(sb.st_mode))
  104. {
  105. /* Not a directory */
  106. goto exit_mkdir_p;
  107. }
  108. if (p - tmp != len)
  109. *p = '/';
  110. }
  111. }
  112. ret = 0;
  113. exit_mkdir_p:
  114. if (tmp)
  115. rt_free(tmp);
  116. return ret;
  117. }
  118. #endif
  119. int mnt_init_spiflash0(void)
  120. {
  121. if (dfs_mount("flash0", MOUNT_POINT_SPIFLASH0, "elm", 0, 0) != 0)
  122. {
  123. rt_kprintf("Failed to mount elm on %s.\n", MOUNT_POINT_SPIFLASH0);
  124. rt_kprintf("Try to execute 'mkfs -t elm flash0' first, then reboot.\n");
  125. goto exit_mnt_init_spiflash0;
  126. }
  127. rt_kprintf("mount flash0 with elmfat type: ok\n");
  128. mkdir_p("/mnt/sd0", 0x777);
  129. mkdir_p("/mnt/sd0p0", 0x777);
  130. mkdir_p("/mnt/sd0p1", 0x777);
  131. mkdir_p("/mnt/sd1", 0x777);
  132. mkdir_p("/mnt/sd1p0", 0x777);
  133. mkdir_p("/mnt/sd1p1", 0x777);
  134. #if defined(RT_USBH_MSTORAGE) && defined(UDISK_MOUNTPOINT)
  135. if (mkdir_p(UDISK_MOUNTPOINT, 0) < 0)
  136. {
  137. rt_kprintf("Failed to create directory on %s for RT_USBH_MSTORAGE.\n", UDISK_MOUNTPOINT);
  138. }
  139. #endif
  140. exit_mnt_init_spiflash0:
  141. return 0;
  142. }
  143. #endif
  144. INIT_ENV_EXPORT(mnt_init_spiflash0);
  145. #endif