mnt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * 2022-3-12 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. #ifdef RT_USING_DFS_MNTTABLE
  29. /*
  30. const char *device_name;
  31. const char *path;
  32. const char *filesystemtype;
  33. unsigned long rwflag;
  34. const void *data;
  35. */
  36. const struct dfs_mount_tbl mount_table[] =
  37. {
  38. { "sd0", "/mnt/sd0", "elm", 0, RT_NULL },
  39. { "sd0p0", "/mnt/sd0p0", "elm", 0, RT_NULL },
  40. { "sd0p1", "/mnt/sd0p1", "elm", 0, RT_NULL },
  41. { "sd1", "/mnt/sd1", "elm", 0, RT_NULL },
  42. { "sd1p0", "/mnt/sd1p0", "elm", 0, RT_NULL },
  43. { "sd1p1", "/mnt/sd1p1", "elm", 0, RT_NULL },
  44. {0},
  45. };
  46. #endif
  47. #if defined(BOARD_USING_STORAGE_SPIFLASH)
  48. #define PARTITION_NAME_FILESYSTEM "filesystem"
  49. #define MOUNT_POINT_SPIFLASH0 "/"
  50. /* Recursive mkdir */
  51. static int mkdir_p(const char *dir, const mode_t mode)
  52. {
  53. int ret = -1;
  54. char *tmp = NULL;
  55. char *p = NULL;
  56. struct stat sb;
  57. rt_size_t len;
  58. if (!dir)
  59. goto exit_mkdir_p;
  60. /* Copy path */
  61. /* Get the string length */
  62. len = strlen(dir);
  63. tmp = rt_strdup(dir);
  64. /* Remove trailing slash */
  65. if (tmp[len - 1] == '/')
  66. {
  67. tmp[len - 1] = '\0';
  68. len--;
  69. }
  70. /* check if path exists and is a directory */
  71. if (stat(tmp, &sb) == 0)
  72. {
  73. if (S_ISDIR(sb.st_mode))
  74. {
  75. ret = 0;
  76. goto exit_mkdir_p;
  77. }
  78. }
  79. /* Recursive mkdir */
  80. for (p = tmp + 1; p - tmp <= len; p++)
  81. {
  82. if ((*p == '/') || (p - tmp == len))
  83. {
  84. *p = 0;
  85. /* Test path */
  86. if (stat(tmp, &sb) != 0)
  87. {
  88. /* Path does not exist - create directory */
  89. if (mkdir(tmp, mode) < 0)
  90. {
  91. goto exit_mkdir_p;
  92. }
  93. }
  94. else if (!S_ISDIR(sb.st_mode))
  95. {
  96. /* Not a directory */
  97. goto exit_mkdir_p;
  98. }
  99. if (p - tmp != len)
  100. *p = '/';
  101. }
  102. }
  103. ret = 0;
  104. exit_mkdir_p:
  105. if (tmp)
  106. rt_free(tmp);
  107. return ret;
  108. }
  109. int mnt_init_spiflash0(void)
  110. {
  111. #if defined(RT_USING_FAL)
  112. extern int fal_init_check(void);
  113. if (!fal_init_check())
  114. fal_init();
  115. #endif
  116. struct rt_device *psNorFlash = fal_blk_device_create(PARTITION_NAME_FILESYSTEM);
  117. if (!psNorFlash)
  118. {
  119. rt_kprintf("Failed to create block device for %s.\n", PARTITION_NAME_FILESYSTEM);
  120. goto exit_mnt_init_spiflash0;
  121. }
  122. else if (dfs_mount(psNorFlash->parent.name, MOUNT_POINT_SPIFLASH0, "elm", 0, 0) != 0)
  123. {
  124. rt_kprintf("Failed to mount elm on %s.\n", MOUNT_POINT_SPIFLASH0);
  125. rt_kprintf("Try to execute 'mkfs -t elm %s' first, then reboot.\n", PARTITION_NAME_FILESYSTEM);
  126. goto exit_mnt_init_spiflash0;
  127. }
  128. rt_kprintf("mount %s with elmfat type: ok\n", PARTITION_NAME_FILESYSTEM);
  129. mkdir_p("/mnt/sd0", 0x777);
  130. mkdir_p("/mnt/sd0p0", 0x777);
  131. mkdir_p("/mnt/sd0p1", 0x777);
  132. mkdir_p("/mnt/sd1", 0x777);
  133. mkdir_p("/mnt/sd1p0", 0x777);
  134. mkdir_p("/mnt/sd1p1", 0x777);
  135. mkdir_p(UDISK_MOUNTPOINT, 0x777);
  136. exit_mnt_init_spiflash0:
  137. return 0;
  138. }
  139. INIT_APP_EXPORT(mnt_init_spiflash0);
  140. #endif