mnt.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * on chip filesystem support
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2021-06-27 Chenyingchun first version
  10. */
  11. #include "board.h"
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #ifdef BSP_USING_ON_CHIP_FS
  15. #ifndef RT_USING_FAL
  16. #error "if you want to use on chip filesystem, you need to enable FAL package()"
  17. #endif
  18. #ifndef RT_USING_DFS
  19. #error "if you want to use on chip filesystem, you need to enable DFS componment"
  20. #endif
  21. #ifndef BSP_USING_ON_CHIP_FLASH
  22. #error "if you want to use on chip filesystem, you need to enable on-chip flash"
  23. #endif
  24. #ifndef RT_USING_MTD_NOR
  25. #error "if you want to use on chip filesystem, you need to enable mtd nor"
  26. #endif
  27. #ifndef PKG_USING_LITTLEFS
  28. #error "if you want to use on chip filesystem, you need to enable littlefs"
  29. #endif
  30. #include "fal.h"
  31. #include <dfs_file.h>
  32. #include <unistd.h>
  33. #include <stdio.h>
  34. #include <sys/stat.h>
  35. #include <sys/statfs.h>
  36. #define LOG_TAG "drv.fs"
  37. #define DBG_LVL DBG_LOG
  38. #include <rtdbg.h>
  39. #define FS_PARTITION_NAME ON_CHIP_PARTION_NAME
  40. /**
  41. * @brief on chip filesystem init
  42. * @param void
  43. * @retval 0: filesystem init success, -1: filesystem init failed
  44. */
  45. static int on_chip_fs_init(void)
  46. {
  47. int result = 0;
  48. fal_init();
  49. struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  50. if (flash_dev == NULL)
  51. {
  52. LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
  53. result = -1;
  54. goto err;
  55. }
  56. else
  57. {
  58. LOG_D("Create a block device on the %s partition of flash successful.", FS_PARTITION_NAME);
  59. }
  60. if (rt_device_find(FS_PARTITION_NAME) != RT_NULL)
  61. {
  62. int mkfs_res = dfs_mkfs("lfs", FS_PARTITION_NAME);
  63. if (mkfs_res != 0)
  64. {
  65. LOG_E("dfs_mkfs error, errno = %d", rt_get_errno());
  66. result = -1;
  67. goto err;
  68. }
  69. if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
  70. {
  71. LOG_D("onchip elm filesystem mount to '/'");
  72. }
  73. else
  74. {
  75. LOG_E("onchip elm filesystem mount to '/' failed!");
  76. result = -1;
  77. goto err;
  78. }
  79. }
  80. else
  81. {
  82. LOG_E("find filesystem portion failed");
  83. }
  84. err:
  85. return result;
  86. }
  87. INIT_ENV_EXPORT(on_chip_fs_init);
  88. #endif /* BSP_USING_ON_CHIP_FS */