drv_filesystem.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-13 balanceTWK add sdcard port file
  9. * 2021-05-10 Meco Man fix a bug that cannot use fatfs in the main thread at starting up
  10. * 2021-07-28 Meco Man implement romfs as the root filesystem
  11. */
  12. #include <rtthread.h>
  13. #ifdef BSP_USING_FS
  14. #include <dfs_romfs.h>
  15. #include <dfs_fs.h>
  16. #include <dfs_file.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <sys/stat.h>
  20. #include <sys/statfs.h>
  21. #if DFS_FILESYSTEMS_MAX < 4
  22. #error "Please define DFS_FILESYSTEMS_MAX more than 4"
  23. #endif
  24. #if DFS_FILESYSTEM_TYPES_MAX < 4
  25. #error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
  26. #endif
  27. #define DBG_TAG "app.filesystem"
  28. #define DBG_LVL DBG_INFO
  29. #include <rtdbg.h>
  30. #ifdef BSP_USING_SDCARD_FATFS
  31. static int onboard_sdcard_mount(void)
  32. {
  33. if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
  34. {
  35. LOG_I("SD card mount to '/sdcard'");
  36. }
  37. else
  38. {
  39. LOG_E("SD card mount to '/sdcard' failed!");
  40. }
  41. return RT_EOK;
  42. }
  43. #endif
  44. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  45. #include <fal.h>
  46. #define FS_PARTITION_NAME "spiflash0"
  47. static int onboard_spiflash_mount(void)
  48. {
  49. struct rt_device *mtd_dev = RT_NULL;
  50. fal_init();
  51. mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  52. if (!mtd_dev)
  53. {
  54. LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
  55. }
  56. if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
  57. {
  58. LOG_I("spi flash mount to '/spiflash'");
  59. }
  60. else
  61. {
  62. dfs_mkfs("lfs", FS_PARTITION_NAME);
  63. if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
  64. {
  65. LOG_I("spi flash mount to '/spiflash'");
  66. }
  67. else
  68. {
  69. LOG_E("spi flash failed to mount to '/spiflash'");
  70. }
  71. }
  72. return RT_EOK;
  73. }
  74. #endif
  75. static const struct romfs_dirent _romfs_root[] =
  76. {
  77. #ifdef BSP_USING_SDCARD_FATFS
  78. {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
  79. #endif
  80. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  81. {ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
  82. #endif
  83. };
  84. const struct romfs_dirent romfs_root =
  85. {
  86. ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
  87. };
  88. static int filesystem_mount(void)
  89. {
  90. if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
  91. {
  92. LOG_E("rom mount to '/' failed!");
  93. }
  94. #ifdef BSP_USING_SDCARD_FATFS
  95. onboard_sdcard_mount();
  96. #endif
  97. #ifdef BSP_USING_SPI_FLASH_LITTLEFS
  98. onboard_spiflash_mount();
  99. #endif
  100. return RT_EOK;
  101. }
  102. INIT_APP_EXPORT(filesystem_mount);
  103. #endif /* BSP_USING_FS */