luat_fs_air101.c 4.6 KB

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