luat_fs_air101.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef FLASH_FS_REGION_SIZE
  23. #define FLASH_FS_REGION_SIZE 112
  24. #endif
  25. extern const struct luat_vfs_filesystem vfs_fs_lfs2;
  26. extern const char luadb_inline_sys[];
  27. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  28. #ifdef LUAT_USE_LVGL
  29. #include "lvgl.h"
  30. void luat_lv_fs_init(void);
  31. // void lv_bmp_init(void);
  32. // void lv_png_init(void);
  33. void lv_split_jpeg_init(void);
  34. #endif
  35. extern int zlib_decompress(FILE *source, FILE *dest);
  36. extern int luat_luadb_checkfile(const char* path);
  37. int luat_fs_init(void) {
  38. //luat_timer_mdelay(1000);
  39. #ifdef AIR103
  40. luadb_addr = 0x0E0000 - (FLASH_FS_REGION_SIZE - 112) * 1024U;
  41. #else
  42. luadb_addr = 0x1E0000 - (FLASH_FS_REGION_SIZE - 112) * 1024U;
  43. #endif
  44. //LLOGD("luadb_addr 0x%08X", luadb_addr);
  45. uint8_t *ptr = (uint8_t*)(luadb_addr + 0x8000000); //0x80E0000
  46. //LLOGD("luadb_addr ptr %p", ptr);
  47. // 兼容老的LuaTools, 并提示更新
  48. static const uint8_t luadb_magic[] = {0x01, 0x04, 0x5A, 0xA5};
  49. uint8_t header[4];
  50. memcpy(header, ptr, 4);
  51. //LLOGD(">> %02X %02X %02X %02X", header[0], header[1], header[2], header[3]);
  52. if (memcmp(header, luadb_magic, 4)) {
  53. // 老的布局
  54. LLOGW("Legacy non-LuaDB download, please upgrade your LuatIDE or LuatTools %p", ptr);
  55. lfs_addr = luadb_addr;
  56. kv_addr = lfs_addr - 64*1024U;
  57. lfs_size_kb = FLASH_FS_REGION_SIZE;
  58. luadb_addr = 0;
  59. }
  60. else {
  61. LLOGI("Using LuaDB as script zone format %p", ptr);
  62. // TODO 根据LuaDB的区域动态调整?
  63. #ifdef AIR103
  64. lfs_addr = 0x0F0000;
  65. lfs_size_kb = 48;
  66. #else
  67. lfs_addr = 0x1F0000;
  68. lfs_size_kb = 48;
  69. #endif
  70. kv_addr = luadb_addr - 64*1024U;
  71. }
  72. //LLOGD("lfs addr4 %p", &lfs_addr);
  73. //LLOGD("lfs addr5 0x%08X", lfs_addr);
  74. //LLOGD("luadb_addr 0x%08X", luadb_addr);
  75. LFS_Init();
  76. luat_vfs_reg(&vfs_fs_lfs2);
  77. luat_fs_conf_t conf = {
  78. .busname = &lfs,
  79. .type = "lfs2",
  80. .filesystem = "lfs2",
  81. .mount_point = "/"
  82. };
  83. luat_fs_mount(&conf);
  84. // 检查是否有压缩升级文件
  85. #define UPDATE_BIN_PATH "/update.bin"
  86. if(luat_fs_fexist("/update.tgz")){
  87. LLOGI("found update.tgz, decompress ...");
  88. FILE *fd_in = luat_fs_fopen("/update.tgz", "r");
  89. luat_fs_remove(UPDATE_BIN_PATH);
  90. FILE *fd_out = luat_fs_fopen(UPDATE_BIN_PATH, "w+");
  91. zlib_decompress(fd_in, fd_out);
  92. luat_fs_fclose(fd_in);
  93. luat_fs_fclose(fd_out);
  94. luat_fs_remove("/update.tgz");
  95. }
  96. // 检查是否有普通升级文件
  97. if(luat_fs_fexist(UPDATE_BIN_PATH)){
  98. LLOGI("found update.bin, checking");
  99. if (luat_luadb_checkfile(UPDATE_BIN_PATH) == 0) {
  100. LLOGI("update.bin ok, updating...");
  101. #define UPDATE_BUFF_SIZE 4096
  102. char* buff = luat_heap_malloc(UPDATE_BUFF_SIZE);
  103. int len = 0;
  104. int offset = 0;
  105. if (buff != NULL) {
  106. FILE* fd = luat_fs_fopen(UPDATE_BIN_PATH, "rb");
  107. while (1) {
  108. len = luat_fs_fread(buff, UPDATE_BUFF_SIZE, 1, fd);
  109. if (len < 1)
  110. break;
  111. tls_fls_write(luadb_addr + offset, buff, len);
  112. offset += len;
  113. }
  114. }
  115. }
  116. else {
  117. LLOGW("update.bin NOT ok, skip");
  118. }
  119. luat_fs_remove(UPDATE_BIN_PATH);
  120. }
  121. luat_vfs_reg(&vfs_fs_luadb);
  122. luat_fs_conf_t conf2 = {
  123. .busname = (char*)(luadb_addr == 0 ? luadb_inline_sys : ptr),
  124. .type = "luadb",
  125. .filesystem = "luadb",
  126. .mount_point = "/luadb/",
  127. };
  128. luat_fs_mount(&conf2);
  129. #ifdef LUAT_USE_LVGL
  130. luat_lv_fs_init();
  131. // lv_bmp_init();
  132. // lv_png_init();
  133. lv_split_jpeg_init();
  134. #endif
  135. return 0;
  136. }