luat_main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_fs.h"
  4. #include "stdio.h"
  5. #include "luat_msgbus.h"
  6. #include "luat_timer.h"
  7. #define LUAT_LOG_TAG "luat.main"
  8. #include "luat_log.h"
  9. #ifdef LUA_USE_WINDOWS
  10. #include <stdlib.h>
  11. extern int win32_argc;
  12. extern char** win32_argv;
  13. #endif
  14. static int report (lua_State *L, int status);
  15. lua_State *L;
  16. static uint8_t boot_mode = 1;
  17. void stopboot(void) {
  18. boot_mode = 0;
  19. }
  20. lua_State * luat_get_state() {
  21. return L;
  22. }
  23. int luat_search_module(const char* name, char* filename);
  24. void luat_os_print_heapinfo(const char* tag);
  25. static int pmain(lua_State *L) {
  26. int re = -2;
  27. #ifdef LUA_32BITS
  28. //LLOGD("Lua complied with LUA_32BITS");
  29. #endif
  30. // 加载内置库
  31. luat_openlibs(L);
  32. luat_os_print_heapinfo("boot");
  33. lua_gc(L, LUA_GCSETPAUSE, 90); // 设置`垃圾收集器间歇率`要低于100%
  34. // 加载main.lua
  35. #ifdef LUA_USE_WINDOWS
  36. if (win32_argc > 1) {
  37. int slen = strlen(win32_argv[1]);
  38. if (slen > 4 && !strcmp(".lua", win32_argv[1] + (slen - 4)))
  39. re = luaL_dofile(L, win32_argv[1]);
  40. }
  41. #endif
  42. if (re == -2) {
  43. char filename[32] = {0};
  44. if (luat_search_module("main", filename) == 0) {
  45. re = luaL_dofile(L, filename);
  46. }
  47. else {
  48. re = -1;
  49. luaL_error(L, "module '%s' not found", "main");
  50. }
  51. }
  52. report(L, re);
  53. lua_pushboolean(L, re == LUA_OK); /* signal no errors */
  54. return 1;
  55. }
  56. /*
  57. ** Prints an error message, adding the program name in front of it
  58. ** (if present)
  59. */
  60. static void l_message (const char *pname, const char *msg) {
  61. if (pname) lua_writestringerror("%s: ", pname);
  62. lua_writestringerror("%s\n", msg);
  63. }
  64. /*
  65. ** Check whether 'status' is not OK and, if so, prints the error
  66. ** message on the top of the stack. It assumes that the error object
  67. ** is a string, as it was either generated by Lua or by 'msghandler'.
  68. */
  69. static int report (lua_State *L, int status) {
  70. if (status != LUA_OK) {
  71. const char *msg = lua_tostring(L, -1);
  72. l_message("LUAT", msg);
  73. lua_pop(L, 1); /* remove message */
  74. }
  75. return status;
  76. }
  77. static int panic (lua_State *L) {
  78. lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
  79. lua_tostring(L, -1));
  80. return 0; /* return to Lua to abort */
  81. }
  82. #define UPDATE_BIN_PATH "/update.bin"
  83. #define ROLLBACK_MARK_PATH "/rollback_mark"
  84. #define UPDATE_MARK "/update_mark"
  85. #define FLASHX_PATH "/flashx.bin"
  86. int luat_bin_unpack(const char* path, int writeOut);
  87. static void check_update(void) {
  88. // 首先, 升级文件是否存在呢?
  89. if (!luat_fs_fexist(UPDATE_BIN_PATH)) {
  90. // 不存在, 正常启动
  91. return;
  92. }
  93. // 找到了, 检查一下大小
  94. LLOGI("found " UPDATE_BIN_PATH " ...");
  95. size_t fsize = luat_fs_fsize(UPDATE_BIN_PATH);
  96. if (fsize < 256) {
  97. // 太小了, 肯定不合法, 直接移除, 正常启动
  98. LLOGW(UPDATE_BIN_PATH " is too small, delete it");
  99. luat_fs_remove(UPDATE_BIN_PATH);
  100. return;
  101. }
  102. // 写入标志文件.
  103. // 必须提前写入, 即使解包失败, 仍标记为升级过,这样报错就能回滚
  104. LLOGI("write " UPDATE_MARK " first");
  105. FILE* fd = luat_fs_fopen(UPDATE_MARK, "wb");
  106. if (fd) {
  107. luat_fs_fclose(fd);
  108. // TODO 连标志文件都写入失败,怎么办?
  109. }
  110. // 检测升级包合法性
  111. if (luat_bin_unpack(UPDATE_BIN_PATH, 0) != LUA_OK) {
  112. LLOGE("%s is invaild!!", UPDATE_BIN_PATH);
  113. }
  114. else {
  115. // 开始解包升级文件
  116. if (luat_bin_unpack(UPDATE_BIN_PATH, 1) == LUA_OK) {
  117. LLOGI("update OK, remove " UPDATE_BIN_PATH);
  118. }
  119. else {
  120. LLOGW("update FAIL, remove " UPDATE_BIN_PATH);
  121. }
  122. }
  123. // 无论是否成功,都一定要删除升级文件, 防止升级死循环
  124. luat_fs_remove(UPDATE_BIN_PATH);
  125. // 延迟5秒,重启
  126. LLOGW("update: reboot at 5 secs");
  127. luat_timer_mdelay(5*1000);
  128. luat_os_reboot(0); // 重启
  129. }
  130. static void check_rollback(void) {
  131. // 首先, 查一下是否有回滚文件
  132. if (!luat_fs_fexist(ROLLBACK_MARK_PATH)) {
  133. return; // 没有回滚标志文件, 正常启动
  134. }
  135. // 回滚文件存在,
  136. LLOGW("Found " ROLLBACK_MARK_PATH ", check rollback");
  137. // 首先,移除回滚标志, 防止重复N次的回滚
  138. luat_fs_remove("/rollback_mark"); // TODO 如果删除也失败呢?
  139. // 然后检查原始文件, flashx.bin
  140. if (!luat_fs_fexist(FLASHX_PATH)) {
  141. LLOGW("NOT " FLASHX_PATH " , can't rollback");
  142. return;
  143. }
  144. // 存在原始flashx.bin
  145. LLOGD("found " FLASHX_PATH ", unpack it");
  146. // 开始回滚操作
  147. if (luat_bin_unpack(FLASHX_PATH, 1) == LUA_OK) {
  148. LLOGI("rollback complete!");
  149. }
  150. else {
  151. LLOGE("rollback FAIL");
  152. }
  153. // 执行完成, 准备重启
  154. LLOGW("rollback: reboot at 5 secs");
  155. // 延迟5秒后,重启
  156. luat_timer_mdelay(5*1000);
  157. luat_os_reboot(0); // 重启
  158. }
  159. int luat_main (void) {
  160. if (boot_mode == 0) {
  161. return 0; // just nop
  162. }
  163. LLOGI("LuatOS@%s %s, Build: " __DATE__ " " __TIME__, luat_os_bsp(), LUAT_VERSION);
  164. #if LUAT_VERSION_BETA
  165. LLOGD("This is a beta version, for testing");
  166. #endif
  167. // 1. 初始化文件系统
  168. luat_fs_init();
  169. // 2. 是否需要升级?
  170. check_update();
  171. // 3. 是否需要回滚呢?
  172. check_rollback();
  173. // 4. init Lua State
  174. int status = 0;
  175. int result = 0;
  176. L = lua_newstate(luat_heap_alloc, NULL);
  177. if (L == NULL) {
  178. l_message("LUAVM", "cannot create state: not enough memory\n");
  179. goto _exit;
  180. }
  181. if (L) lua_atpanic(L, &panic);
  182. //print_list_mem("after lua_newstate");
  183. lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */
  184. //lua_pushinteger(L, argc); /* 1st argument */
  185. //lua_pushlightuserdata(L, argv); /* 2nd argument */
  186. status = lua_pcall(L, 0, 1, 0); /* do the call */
  187. result = lua_toboolean(L, -1); /* get result */
  188. report(L, status);
  189. //lua_close(L);
  190. _exit:
  191. #ifdef LUA_USE_WINDOWS
  192. result = !result;
  193. LLOGE("Lua VM exit!! result:%d",result);
  194. exit(result);
  195. #endif
  196. LLOGE("Lua VM exit!! reboot in 30s");
  197. // 既然是异常退出,那肯定出错了!!!
  198. // 如果升级过, 那么就写入标志文件
  199. {
  200. if (luat_fs_fexist(UPDATE_MARK)) {
  201. FILE* fd = luat_fs_fopen("/rollback_mark", "wb");
  202. if (fd) {
  203. luat_fs_fclose(fd);
  204. }
  205. }
  206. else {
  207. // 没升级过, 那就是线刷, 不存在回滚
  208. }
  209. }
  210. // 等30秒,就重启吧
  211. luat_timer_mdelay(30*1000);
  212. luat_os_reboot(result);
  213. // 往下是肯定不会被执行的
  214. return (result && status == LUA_OK) ? 0 : 2;
  215. }
  216. #include "rotable.h"
  217. void luat_newlib(lua_State* l, const rotable_Reg* reg) {
  218. #ifdef LUAT_CONF_DISABLE_ROTABLE
  219. luaL_newlibtable(l,reg);
  220. int i;
  221. int nup = 0;
  222. luaL_checkstack(l, nup, "too many upvalues");
  223. for (; reg->name != NULL; reg++) { /* fill the table with given functions */
  224. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  225. lua_pushvalue(l, -nup);
  226. if (reg->func)
  227. lua_pushcclosure(l, reg->func, nup); /* closure with those upvalues */
  228. else
  229. lua_pushinteger(l, reg->value);
  230. lua_setfield(l, -(nup + 2), reg->name);
  231. }
  232. lua_pop(l, nup); /* remove upvalues */
  233. #else
  234. rotable_newlib(l, reg);
  235. #endif
  236. }
  237. void luat_os_print_heapinfo(const char* tag) {
  238. size_t total; size_t used; size_t max_used;
  239. luat_meminfo_luavm(&total, &used, &max_used);
  240. LLOGD("%s luavm %ld %ld %ld", tag, total, used, max_used);
  241. luat_meminfo_sys(&total, &used, &max_used);
  242. LLOGD("%s sys %ld %ld %ld", tag, total, used, max_used);
  243. }