luat_fs_air101.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // #include "luat_conf_bsp.h"
  2. #include "luat_base.h"
  3. #include "luat_fs.h"
  4. #define LUAT_LOG_TAG "fs"
  5. #include "luat_log.h"
  6. #include "lfs_port.h"
  7. #include "wm_include.h"
  8. #include "luat_timer.h"
  9. #include "stdio.h"
  10. extern struct lfs_config lfs_cfg;
  11. extern lfs_t lfs;
  12. // 分区信息
  13. // KV -- 64k
  14. // luadb -- N k
  15. // lfs - (112 + 64 - N)k
  16. uint32_t kv_addr;
  17. uint32_t kv_size_kb = 64;
  18. uint32_t luadb_addr;
  19. uint32_t luadb_size_kb;
  20. uint32_t lfs_addr;
  21. uint32_t lfs_size_kb;
  22. #define UPDATE_TGZ_PATH "/update.tgz"
  23. #define UPDATE_BIN_PATH "/update.bin"
  24. extern int zlib_decompress(FILE *source, FILE *dest);
  25. extern int luat_luadb_checkfile(const char* path);
  26. static int luat_ota(void){
  27. int ret = 0;
  28. //检测是否有压缩升级文件
  29. if(luat_fs_fexist(UPDATE_TGZ_PATH)){
  30. LLOGI("found update.tgz, decompress ...");
  31. FILE *fd_in = luat_fs_fopen(UPDATE_TGZ_PATH, "r");
  32. if (fd_in == NULL){
  33. LLOGE("open the input file : %s error!", UPDATE_TGZ_PATH);
  34. ret = -1;
  35. goto _close_decompress;
  36. }
  37. luat_fs_remove(UPDATE_BIN_PATH);
  38. FILE *fd_out = luat_fs_fopen(UPDATE_BIN_PATH, "w+");
  39. if (fd_out == NULL){
  40. LLOGE("open the output file : %s error!", UPDATE_BIN_PATH);
  41. ret = -1;
  42. goto _close_decompress;
  43. }
  44. ret = zlib_decompress(fd_in, fd_out);
  45. if (ret != 0){
  46. LLOGE("decompress file error!");
  47. }
  48. _close_decompress:
  49. if(fd_in != NULL){
  50. luat_fs_fclose(fd_in);
  51. }
  52. if(fd_out != NULL){
  53. luat_fs_fclose(fd_out);
  54. }
  55. //不论成功与否都删掉避免每次启动都执行一遍
  56. luat_fs_remove(UPDATE_TGZ_PATH);
  57. }
  58. //检测是否有升级文件
  59. if(luat_fs_fexist(UPDATE_BIN_PATH)){
  60. LLOGI("found update.bin, checking");
  61. if (luat_luadb_checkfile(UPDATE_BIN_PATH) == 0) {
  62. LLOGI("update.bin ok, updating...");
  63. #define UPDATE_BUFF_SIZE 4096
  64. char* buff = luat_heap_malloc(UPDATE_BUFF_SIZE);
  65. int len = 0;
  66. int offset = 0;
  67. if (buff != NULL) {
  68. FILE* fd = luat_fs_fopen(UPDATE_BIN_PATH, "rb");
  69. while (1) {
  70. len = luat_fs_fread(buff, UPDATE_BUFF_SIZE, 1, fd);
  71. if (len < 1)
  72. break;
  73. tls_fls_write(luadb_addr + offset, buff, len);
  74. offset += len;
  75. }
  76. }
  77. }
  78. else {
  79. ret = -1;
  80. LLOGW("update.bin NOT ok, skip");
  81. }
  82. luat_fs_remove(UPDATE_BIN_PATH);
  83. }
  84. return ret;
  85. }
  86. #ifndef FLASH_FS_REGION_SIZE
  87. #define FLASH_FS_REGION_SIZE 112
  88. #endif
  89. extern const struct luat_vfs_filesystem vfs_fs_lfs2;
  90. extern const char luadb_inline_sys[];
  91. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  92. #ifdef LUAT_USE_LVGL
  93. #include "lvgl.h"
  94. void luat_lv_fs_init(void);
  95. // void lv_bmp_init(void);
  96. // void lv_png_init(void);
  97. void lv_split_jpeg_init(void);
  98. #endif
  99. int luat_fs_init(void) {
  100. //luat_timer_mdelay(1000);
  101. #ifdef AIR103
  102. luadb_addr = 0x0E0000 - (FLASH_FS_REGION_SIZE - 112) * 1024U;
  103. #else
  104. luadb_addr = 0x1E0000 - (FLASH_FS_REGION_SIZE - 112) * 1024U;
  105. #endif
  106. //LLOGD("luadb_addr 0x%08X", luadb_addr);
  107. uint8_t *ptr = (uint8_t*)(luadb_addr + 0x8000000); //0x80E0000
  108. //LLOGD("luadb_addr ptr %p", ptr);
  109. // 兼容老的LuaTools, 并提示更新
  110. static const uint8_t luadb_magic[] = {0x01, 0x04, 0x5A, 0xA5};
  111. uint8_t header[4];
  112. memcpy(header, ptr, 4);
  113. //LLOGD(">> %02X %02X %02X %02X", header[0], header[1], header[2], header[3]);
  114. if (memcmp(header, luadb_magic, 4)) {
  115. // 老的布局
  116. LLOGW("Legacy non-LuaDB download, please upgrade your LuatIDE or LuatTools %p", ptr);
  117. lfs_addr = luadb_addr;
  118. kv_addr = lfs_addr - 64*1024U;
  119. lfs_size_kb = FLASH_FS_REGION_SIZE;
  120. luadb_addr = 0;
  121. }
  122. else {
  123. LLOGI("Using LuaDB as script zone format %p", ptr);
  124. // TODO 根据LuaDB的区域动态调整?
  125. #ifdef AIR103
  126. lfs_addr = 0x0F0000;
  127. lfs_size_kb = 48;
  128. #else
  129. lfs_addr = 0x1F0000;
  130. lfs_size_kb = 48;
  131. #endif
  132. kv_addr = luadb_addr - 64*1024U;
  133. }
  134. //LLOGD("lfs addr4 %p", &lfs_addr);
  135. //LLOGD("lfs addr5 0x%08X", lfs_addr);
  136. //LLOGD("luadb_addr 0x%08X", luadb_addr);
  137. LFS_Init();
  138. luat_vfs_reg(&vfs_fs_lfs2);
  139. luat_fs_conf_t conf = {
  140. .busname = &lfs,
  141. .type = "lfs2",
  142. .filesystem = "lfs2",
  143. .mount_point = "/"
  144. };
  145. luat_fs_mount(&conf);
  146. //OTA检测升级
  147. luat_ota();
  148. luat_vfs_reg(&vfs_fs_luadb);
  149. luat_fs_conf_t conf2 = {
  150. .busname = (char*)(luadb_addr == 0 ? luadb_inline_sys : ptr),
  151. .type = "luadb",
  152. .filesystem = "luadb",
  153. .mount_point = "/luadb/",
  154. };
  155. luat_fs_mount(&conf2);
  156. #ifdef LUAT_USE_LVGL
  157. luat_lv_fs_init();
  158. // lv_bmp_init();
  159. // lv_png_init();
  160. lv_split_jpeg_init();
  161. #endif
  162. return 0;
  163. }