luat_sfd_lfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "luat_base.h"
  2. #include "luat_sfd.h"
  3. #include "luat_mem.h"
  4. #define LUAT_LOG_TAG "lfs"
  5. #include "luat_log.h"
  6. luat_sfd_lfs_t* sfd_lfs;
  7. // Read a block
  8. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  9. lfs_off_t off, void *buffer, lfs_size_t size) {
  10. sfd_drv_t* drv = cfg->context;
  11. int ret = luat_sfd_read(drv, buffer, block * LFS_BLOCK_DEVICE_ERASE_SIZE + off, size);
  12. // LLOGD("sfd read %d %d %d %d", block, off, size, ret);
  13. if (ret >= 0) {
  14. // LLOGD("block_device_read return LFS_ERR_OK");
  15. return LFS_ERR_OK;
  16. }
  17. // LLOGD("block_device_read return LFS_ERR_IO");
  18. return LFS_ERR_IO;
  19. }
  20. // Program a block
  21. //
  22. // The block must have previously been erased.
  23. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  24. lfs_off_t off, const void *buffer, lfs_size_t size) {
  25. sfd_drv_t* drv = cfg->context;
  26. int ret = luat_sfd_write(drv, buffer, block * LFS_BLOCK_DEVICE_ERASE_SIZE + off, size);
  27. // LLOGD("sfd write %d %d %d %d", block, off, size, ret);
  28. if (ret >= 0) {
  29. // LLOGD("block_device_prog return LFS_ERR_OK");
  30. return LFS_ERR_OK;
  31. }
  32. // LLOGD("block_device_prog return LFS_ERR_IO");
  33. return LFS_ERR_IO;
  34. }
  35. // Erase a block
  36. //
  37. // A block must be erased before being programmed. The
  38. // state of an erased block is undefined.
  39. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  40. sfd_drv_t* drv = cfg->context;
  41. int ret = luat_sfd_erase(drv, block * LFS_BLOCK_DEVICE_ERASE_SIZE, LFS_BLOCK_DEVICE_ERASE_SIZE);
  42. // LLOGD("sfd erase %d %d", block, ret);
  43. (void)ret;
  44. return 0;
  45. }
  46. // Sync the block device
  47. static int block_device_sync(const struct lfs_config *cfg) {
  48. return 0;
  49. }
  50. int luat_sfd_lfs_init(sfd_drv_t *drv) {
  51. int ret = 0;
  52. if (sfd_lfs == NULL) {
  53. sfd_lfs = luat_heap_malloc(sizeof(luat_sfd_lfs_t));
  54. if (sfd_lfs == NULL) {
  55. LLOGE("out of memory when malloc sfd_lfs");
  56. return -1;
  57. }
  58. memset(sfd_lfs, 0, sizeof(luat_sfd_lfs_t));
  59. sfd_lfs->conf.read = block_device_read;
  60. sfd_lfs->conf.prog = block_device_prog;
  61. sfd_lfs->conf.erase = block_device_erase;
  62. sfd_lfs->conf.sync = block_device_sync;
  63. sfd_lfs->conf.attr_max = 0;
  64. sfd_lfs->conf.file_max = 4096;
  65. sfd_lfs->conf.block_count = (LFS_BLOCK_DEVICE_TOTOAL_SIZE) / LFS_BLOCK_DEVICE_ERASE_SIZE;
  66. sfd_lfs->conf.block_size = LFS_BLOCK_DEVICE_ERASE_SIZE;
  67. sfd_lfs->conf.block_cycles = 200;
  68. sfd_lfs->conf.name_max = 63;
  69. sfd_lfs->conf.read_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  70. sfd_lfs->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  71. sfd_lfs->conf.prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  72. sfd_lfs->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  73. sfd_lfs->conf.lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  74. sfd_lfs->conf.lookahead_buffer = sfd_lfs->lookahead_buffer;
  75. sfd_lfs->conf.prog_buffer = sfd_lfs->prog_buffer;
  76. sfd_lfs->conf.read_buffer = sfd_lfs->read_buffer;
  77. sfd_lfs->conf.context = drv;
  78. ret = lfs_mount(&sfd_lfs->lfs, &sfd_lfs->conf);
  79. if (ret != LFS_ERR_OK) {
  80. LLOGI("sfd_lfs mount ret %d, exec auto-format", ret);
  81. ret = lfs_format(&sfd_lfs->lfs, &sfd_lfs->conf);
  82. if (ret != LFS_ERR_OK) {
  83. luat_heap_free(sfd_lfs);
  84. sfd_lfs = NULL;
  85. LLOGE("sfd_lfs auto-format ret %d", ret);
  86. return ret;
  87. }
  88. ret = lfs_mount(&sfd_lfs->lfs, &sfd_lfs->conf);
  89. if (ret != LFS_ERR_OK) {
  90. luat_heap_free(sfd_lfs);
  91. sfd_lfs = NULL;
  92. LLOGE("sfd_lfs remount ret %d", ret);
  93. return ret;
  94. }
  95. }
  96. LLOGD("init ok");
  97. }
  98. return 0;
  99. }