luat_fs_air101.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. int luat_fs_init(void) {
  37. //luat_timer_mdelay(1000);
  38. #ifdef AIR103
  39. luadb_addr = 0x0E0000 - (FLASH_FS_REGION_SIZE - 112) * 1024U;
  40. #else
  41. luadb_addr = 0x1E0000 - (FLASH_FS_REGION_SIZE - 112) * 1024U;
  42. #endif
  43. //LLOGD("luadb_addr 0x%08X", luadb_addr);
  44. uint8_t *ptr = (uint8_t*)(luadb_addr + 0x8000000); //0x80E0000
  45. //LLOGD("luadb_addr ptr %p", ptr);
  46. // 兼容老的LuaTools, 并提示更新
  47. static const uint8_t luadb_magic[] = {0x01, 0x04, 0x5A, 0xA5};
  48. uint8_t header[4];
  49. memcpy(header, ptr, 4);
  50. //LLOGD(">> %02X %02X %02X %02X", header[0], header[1], header[2], header[3]);
  51. if (memcmp(header, luadb_magic, 4)) {
  52. // 老的布局
  53. LLOGW("Legacy non-LuaDB download, please upgrade your LuatIDE or LuatTools %p", ptr);
  54. lfs_addr = luadb_addr;
  55. kv_addr = lfs_addr - 64*1024U;
  56. lfs_size_kb = FLASH_FS_REGION_SIZE;
  57. luadb_addr = 0;
  58. }
  59. else {
  60. LLOGI("Using LuaDB as script zone format %p", ptr);
  61. // TODO 根据LuaDB的区域动态调整?
  62. #ifdef AIR103
  63. lfs_addr = 0x0F0000;
  64. lfs_size_kb = 48;
  65. #else
  66. lfs_addr = 0x1F0000;
  67. lfs_size_kb = 48;
  68. #endif
  69. kv_addr = luadb_addr - 64*1024U;
  70. }
  71. //LLOGD("lfs addr4 %p", &lfs_addr);
  72. //LLOGD("lfs addr5 0x%08X", lfs_addr);
  73. //LLOGD("luadb_addr 0x%08X", luadb_addr);
  74. LFS_Init();
  75. luat_vfs_reg(&vfs_fs_lfs2);
  76. luat_fs_conf_t conf = {
  77. .busname = &lfs,
  78. .type = "lfs2",
  79. .filesystem = "lfs2",
  80. .mount_point = "/"
  81. };
  82. luat_fs_mount(&conf);
  83. //检测是否有升级文件
  84. if(luat_fs_fexist("/update")){
  85. FILE *fd_in = luat_fs_fopen("/update", "r");
  86. FILE *fd_out = luat_fs_fopen("/update.bin", "w+");
  87. zlib_decompress(fd_in, fd_out);
  88. luat_fs_fclose(fd_in);
  89. luat_fs_fclose(fd_out);
  90. size_t binsize = luat_fs_fsize("/update.bin");
  91. LLOGI("update.bin size:%d",binsize);
  92. uint8_t* binbuff = (uint8_t*)luat_heap_malloc(binsize * sizeof(uint8_t));
  93. FILE * fd = luat_fs_fopen("/update.bin", "rb");
  94. if (fd) {
  95. luat_fs_fread(binbuff, sizeof(uint8_t), binsize, fd);
  96. //做一下校验
  97. if (binbuff[0] != 0x01 || binbuff[1] != 0x04 || binbuff[2]+(binbuff[3]<<8) != 0xA55A || binbuff[4]+(binbuff[5]<<8) != 0xA55A){
  98. LLOGI("Magic error");
  99. goto _close;
  100. }
  101. LLOGI("Magic OK");
  102. if (binbuff[6] != 0x02 || binbuff[7] != 0x02 || binbuff[8] != 0x02 || binbuff[9] != 0x00){
  103. LLOGI("Version error");
  104. goto _close;
  105. }
  106. LLOGI("Version OK");
  107. if (binbuff[10] != 0x03 || binbuff[11] != 0x04){
  108. LLOGI("Header error");
  109. goto _close;
  110. }
  111. uint32_t headsize = binbuff[12]+(binbuff[13]<<8)+(binbuff[14]<<16)+(binbuff[15]<<24);
  112. LLOGI("Header OK headers:%08x",headsize);
  113. if (binbuff[16] != 0x04 || binbuff[17] != 0x02){
  114. LLOGI("file count error");
  115. goto _close;
  116. }
  117. uint16_t filecount = binbuff[18]+(binbuff[19]<<8);
  118. LLOGI("file count:%04x",filecount);
  119. if (binbuff[20] != 0xFE || binbuff[21] != 0x02){
  120. LLOGI("CRC16 error");
  121. goto _close;
  122. }
  123. uint16_t CRC16 = binbuff[22]+(binbuff[23]<<8);
  124. // LLOGI("CRC16:%04x",CRC16);
  125. tls_fls_write(luadb_addr, binbuff, binsize);
  126. _close:
  127. luat_fs_fclose(fd);
  128. }
  129. luat_heap_free(binbuff);
  130. //不论成功与否都删掉避免每次启动都执行一遍
  131. luat_fs_remove("/update.bin");
  132. luat_fs_remove("/update");
  133. }
  134. luat_vfs_reg(&vfs_fs_luadb);
  135. luat_fs_conf_t conf2 = {
  136. .busname = (char*)(luadb_addr == 0 ? luadb_inline_sys : ptr),
  137. .type = "luadb",
  138. .filesystem = "luadb",
  139. .mount_point = "/luadb/",
  140. };
  141. luat_fs_mount(&conf2);
  142. #ifdef LUAT_USE_LVGL
  143. luat_lv_fs_init();
  144. // lv_bmp_init();
  145. // lv_png_init();
  146. lv_split_jpeg_init();
  147. #endif
  148. return 0;
  149. }