luat_main.c 7.4 KB

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