mnt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <dfs_fs.h>
  14. #include <dfs_file.h>
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. #include <sys/stat.h>
  18. #include <sys/statfs.h>
  19. #if defined(PKG_USING_FAL)
  20. #include <fal.h>
  21. #endif
  22. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  23. #define MOUNT_POINT_SPIFLASH0 "/"
  24. #endif
  25. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  26. #if defined(RT_USB_DEVICE_MSTORAGE)
  27. int mnt_init_spiflash0(void)
  28. {
  29. rt_kprintf("Sorry, you enabled RT_USB_DEVICE_MSTORAGE option in menu, so we won't mount flash0 on /.\n");
  30. return 0;
  31. }
  32. #else
  33. /* Recursive mkdir */
  34. #if defined(RT_USBH_MSTORAGE) && defined(UDISK_MOUNTPOINT)
  35. static int mkdir_p(const char *dir, const mode_t mode)
  36. {
  37. int ret = -1;
  38. char *tmp = NULL;
  39. char *p = NULL;
  40. struct stat sb;
  41. rt_size_t len;
  42. if (!dir)
  43. goto exit_mkdir_p;
  44. /* Copy path */
  45. /* Get the string length */
  46. len = strlen(dir);
  47. tmp = rt_strdup(dir);
  48. /* Remove trailing slash */
  49. if (tmp[len - 1] == '/')
  50. {
  51. tmp[len - 1] = '\0';
  52. len--;
  53. }
  54. /* check if path exists and is a directory */
  55. if (stat(tmp, &sb) == 0)
  56. {
  57. if (S_ISDIR(sb.st_mode))
  58. {
  59. ret = 0;
  60. goto exit_mkdir_p;
  61. }
  62. }
  63. /* Recursive mkdir */
  64. for (p = tmp + 1; p - tmp <= len; p++)
  65. {
  66. if ((*p == '/') || (p - tmp == len))
  67. {
  68. *p = 0;
  69. /* Test path */
  70. if (stat(tmp, &sb) != 0)
  71. {
  72. /* Path does not exist - create directory */
  73. if (mkdir(tmp, mode) < 0)
  74. {
  75. goto exit_mkdir_p;
  76. }
  77. }
  78. else if (!S_ISDIR(sb.st_mode))
  79. {
  80. /* Not a directory */
  81. goto exit_mkdir_p;
  82. }
  83. if (p - tmp != len)
  84. *p = '/';
  85. }
  86. }
  87. ret = 0;
  88. exit_mkdir_p:
  89. if (tmp)
  90. rt_free(tmp);
  91. return ret;
  92. }
  93. #endif
  94. int mnt_init_spiflash0(void)
  95. {
  96. if (dfs_mount("flash0", MOUNT_POINT_SPIFLASH0, "elm", 0, 0) != 0)
  97. {
  98. rt_kprintf("Failed to mount elm on %s.\n", MOUNT_POINT_SPIFLASH0);
  99. rt_kprintf("Try to execute 'mkfs -t elm flash0' first, then reboot.\n");
  100. goto exit_mnt_init_spiflash0;
  101. }
  102. rt_kprintf("mount flash0 with elmfat type: ok\n");
  103. #if defined(RT_USBH_MSTORAGE) && defined(UDISK_MOUNTPOINT)
  104. if (mkdir_p(UDISK_MOUNTPOINT, 0) < 0)
  105. {
  106. rt_kprintf("Failed to create directory on %s for RT_USBH_MSTORAGE.\n", UDISK_MOUNTPOINT);
  107. }
  108. #endif
  109. exit_mnt_init_spiflash0:
  110. return 0;
  111. }
  112. #endif
  113. INIT_ENV_EXPORT(mnt_init_spiflash0);
  114. #endif