lfs_port.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "lfs_port.h"
  5. #include "lfs_util.h"
  6. #include "luat_base.h"
  7. #define LUAT_LOG_TAG "luat.fs.port"
  8. #include "luat_log.h"
  9. #include "wm_include.h"
  10. #include "lfs.h"
  11. #include "wm_flash_map.h"
  12. #include "wm_internal_flash.h"
  13. #ifndef FLASH_FS_REGION_SIZE
  14. #define FLASH_FS_REGION_SIZE 112
  15. #endif
  16. #ifndef LFS_START_ADDR
  17. #ifdef AIR103
  18. #define LFS_START_ADDR (0x0FC000 - FLASH_FS_REGION_SIZE * 1024)
  19. #else
  20. #define LFS_START_ADDR (0x1FC000 - FLASH_FS_REGION_SIZE * 1024)
  21. #endif
  22. #endif
  23. /***************************************************
  24. *************** MACRO ******************
  25. ***************************************************/
  26. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  27. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  28. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  29. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  30. #define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE * 1024)
  31. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  32. /***************************************************
  33. ******* FUNCTION FORWARD DECLARTION ********
  34. ***************************************************/
  35. // Read a block
  36. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  37. lfs_off_t off, void *buffer, lfs_size_t size);
  38. // Program a block
  39. //
  40. // The block must have previously been erased.
  41. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  42. lfs_off_t off, const void *buffer, lfs_size_t size);
  43. // Erase a block
  44. //
  45. // A block must be erased before being programmed. The
  46. // state of an erased block is undefined.
  47. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block);
  48. // Sync the block device
  49. static int block_device_sync(const struct lfs_config *cfg);
  50. // utility functions for traversals
  51. static int lfs_statfs_count(void *p, lfs_block_t b);
  52. /***************************************************
  53. *************** GLOBAL VARIABLE *****************
  54. ***************************************************/
  55. #ifdef LFS_THREAD_SAFE_MUTEX
  56. static osMutexId_t lfs_mutex;
  57. #endif
  58. static char lfs_read_buf[256];
  59. static char lfs_prog_buf[256];
  60. // static __ALIGNED(4) char lfs_lookahead_buf[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  61. static char __attribute__((aligned(4))) lfs_lookahead_buf[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  62. // configuration of the filesystem is provided by this struct
  63. struct lfs_config lfs_cfg =
  64. {
  65. .context = NULL,
  66. // block device operations
  67. .read = block_device_read,
  68. .prog = block_device_prog,
  69. .erase = block_device_erase,
  70. .sync = block_device_sync,
  71. // block device configuration
  72. .read_size = LFS_BLOCK_DEVICE_READ_SIZE,
  73. .prog_size = LFS_BLOCK_DEVICE_PROG_SIZE,
  74. .block_size = LFS_BLOCK_DEVICE_ERASE_SIZE,
  75. .block_count = LFS_BLOCK_DEVICE_TOTOAL_SIZE / LFS_BLOCK_DEVICE_ERASE_SIZE,
  76. .block_cycles = 200,
  77. .cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE,
  78. .lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD,
  79. .read_buffer = lfs_read_buf,
  80. .prog_buffer = lfs_prog_buf,
  81. .lookahead_buffer = lfs_lookahead_buf,
  82. .name_max = 63,
  83. .file_max = 0,
  84. .attr_max = 0
  85. };
  86. lfs_t lfs;
  87. /***************************************************
  88. ******* INTERANL FUNCTION ********
  89. ***************************************************/
  90. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  91. lfs_off_t off, void *buffer, lfs_size_t size)
  92. {
  93. int ret;
  94. //LLOGD("block_device_read ,block = %d, off = %d, size = %d",block, off, size);
  95. ret = tls_fls_read(block * 4096 + off + LFS_START_ADDR, (u8 *)buffer, size);
  96. //LLOGD("block_device_read return val : %d",ret);
  97. if (ret != TLS_FLS_STATUS_OK)
  98. {
  99. return -1;
  100. }
  101. return 0;
  102. }
  103. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  104. lfs_off_t off, const void *buffer, lfs_size_t size)
  105. {
  106. int ret;
  107. //LLOGD("block_device_prog ,block = %d, off = %d, size = %d",block, off, size);
  108. ret = tls_fls_write(block * 4096 + off + LFS_START_ADDR, (u8 *)buffer, size);
  109. //LLOGD("block_device_prog return val : %d",ret);
  110. if (ret != TLS_FLS_STATUS_OK)
  111. {
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block)
  117. {
  118. int ret;
  119. ret = tls_fls_erase((block * 4096 + LFS_START_ADDR) / INSIDE_FLS_SECTOR_SIZE);
  120. if (ret != TLS_FLS_STATUS_OK)
  121. {
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. static int block_device_sync(const struct lfs_config *cfg)
  127. {
  128. return 0;
  129. }
  130. static int lfs_statfs_count(void *p, lfs_block_t b)
  131. {
  132. *(lfs_size_t *)p += 1;
  133. return 0;
  134. }
  135. // Initialize
  136. int LFS_Init(void)
  137. {
  138. //printf("------------LFS_Init------------\r\n");
  139. // mount the filesystem
  140. int err = lfs_mount(&lfs, &lfs_cfg);
  141. //printf("lfs_mount %d\r\n",err);
  142. // reformat if we can't mount the filesystem
  143. // this should only happen on the first boot
  144. if (err)
  145. {
  146. err = lfs_format(&lfs, &lfs_cfg);
  147. //printf("lfs_format %d\r\n",err);
  148. if(err)
  149. return err;
  150. err = lfs_mount(&lfs, &lfs_cfg);
  151. //printf("lfs_mount %d\r\n",err);
  152. if(err)
  153. return err;
  154. }
  155. return 0;
  156. }