air640w.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. --[[
  2. This file is encode by "GBK"
  3. ]]
  4. local sys = require "sys"
  5. log.info("base", "LuatOS@Air640w 刷机工具")
  6. if rtos.bsp() ~= "win32" then
  7. log.info("base", "当前仅支持win32操作")
  8. os.exit()
  9. end
  10. Base_CWD = lfs.currentdir()
  11. tool_debug = false
  12. local self_conf = {
  13. basic = {
  14. COM_PORT = "COM20",
  15. USER_PATH = "user\\",
  16. LIB_PATH = "lib\\",
  17. MAIN_LUA_DEBUG = "false",
  18. LUA_DEBUG = "false",
  19. TOOLS_PATH = "tools\\"
  20. }
  21. }
  22. local function trim5(s)
  23. return s:match'^%s*(.*%S)' or ''
  24. end
  25. local function read_local_ini()
  26. local f = io.open("/local.ini")
  27. if f then
  28. local key = "basic"
  29. for line in f:lines() do
  30. line = trim5(line)
  31. if #line > 0 then
  32. if line:byte() == '[' and line:byte(line:len()) then
  33. key = line:sub(2, line:len() - 1)
  34. if key == "air640w" then key = "basic" end
  35. if self_conf[key] == nil then
  36. self_conf[key] = {}
  37. end
  38. elseif line:find("=") then
  39. local skey = line:sub(1, line:find("=") - 1)
  40. local val = line:sub(line:find("=") + 1)
  41. if skey and val then
  42. self_conf[key][trim5(skey)] = trim5(val)
  43. end
  44. end
  45. end
  46. end
  47. f:close()
  48. end
  49. log.info("config", json.encode(self_conf))
  50. end
  51. read_local_ini()
  52. local function lsdir(path, files, shortname)
  53. local exe = io.popen("dir /b " .. (shortname and " " or " /s ") .. path)
  54. if exe then
  55. for line in exe:lines() do
  56. table.insert(files, line)
  57. end
  58. exe:close()
  59. end
  60. end
  61. local function oscall(cmd, quite, cwd)
  62. if cwd and Base_CWD then
  63. lfs.chdir(cwd)
  64. end
  65. if tool_debug then
  66. log.info("cmd", cmd)
  67. end
  68. local exe = io.popen(cmd)
  69. if exe then
  70. for line in exe:lines() do
  71. if not quite then
  72. log.info("cmd", line)
  73. end
  74. end
  75. exe:close()
  76. end
  77. if cwd and Base_CWD then
  78. lfs.chdir(Base_CWD)
  79. end
  80. end
  81. local function create_tmproot()
  82. --local tmproot = os.getenv("Temp") .. "\\luatos"
  83. local tmproot = "tmp\\"
  84. log.info("tmpdir", tmproot)
  85. oscall("rmdir /S /Q \"" .. tmproot .. "\"")
  86. oscall("mkdir \"" .. tmproot .. "\"")
  87. return tmproot
  88. end
  89. local cmds = {}
  90. cmds["help"] = function(arg)
  91. log.info("help", "==============================")
  92. log.info("help", "lfs 打包文件系统")
  93. log.info("help", "dlrom 下载底层固件")
  94. log.info("help", "dlfs 下载文件系统")
  95. log.info("help", "dlfull 下载底层固件及文件系统")
  96. log.info("help", "==============================")
  97. end
  98. local function build_flashx(rootpath)
  99. local files = {}
  100. lsdir(rootpath, files, true)
  101. local buff = zbuff.create(64*1024)
  102. -- 头部魔数
  103. buff:writeI16(0x1234)
  104. -- 逐个文件写入
  105. for _, name in ipairs(files) do
  106. if name ~= "flashx.bin" then
  107. -- 写入文件名
  108. buff:writeI16(0x0101)
  109. buff:writeI16(0x0)
  110. buff:writeI32(name:len())
  111. buff:write(name)
  112. -- 写入文件
  113. local f = io.open(rootpath .. "\\" .. name, "rb")
  114. if f == nil then
  115. log.error("文件读取失败!!!!!!!" .. name)
  116. os.exit(2)
  117. return -- impossible
  118. end
  119. local data = f:read("*a")
  120. buff:writeI16(0x0202)
  121. buff:writeI16(0x0)
  122. buff:writeI32(data:len())
  123. buff:write(data)
  124. f:close()
  125. end
  126. end
  127. local fdata = buff:toStr(0, buff:seek(0, zbuff.SEEK_CUR))
  128. io.writeFile(rootpath .. "\\flashx.bin", fdata)
  129. log.info("lfs", "flashx.bin is done", crypto.sha256(fdata))
  130. end
  131. cmds["lfs"] = function()
  132. -- 首先, 把用户的文件都取一下
  133. local files = {}
  134. lsdir(self_conf["basic"]["USER_PATH"] or "user\\", files)
  135. lsdir(self_conf["basic"]["LIB_PATH"] or "lib\\", files)
  136. for index, value in ipairs(files) do
  137. log.info("file", index ,value)
  138. end
  139. -- 创建临时目录, 把文件都拷贝过去
  140. local tmproot = create_tmproot()
  141. oscall("mkdir \"" .. tmproot .. "\"\\luafile")
  142. oscall("mkdir \"" .. tmproot .. "\"\\disk")
  143. for index, value in ipairs(files) do
  144. log.info("copy", index ,value)
  145. oscall("copy \"" .. value .. "\" " .. "\"" .. tmproot .. "\\luafile\\")
  146. end
  147. -- 对lua文件进行luac编译, 非lua文件就免了
  148. files = {}
  149. lsdir(tmproot .. "\\luafile", files, true)
  150. --local tools_path = self_conf["basic"]["TOOLS_PATH"]
  151. local luac_exe = self_conf["basic"]["TOOLS_PATH"] .. "luac_536_32bits.exe"
  152. local main_lua_debug = self_conf["basic"]["MAIN_LUA_DEBUG"] == "true"
  153. local lua_debug = self_conf["basic"]["LUA_DEBUG"] == "true"
  154. for _, name in ipairs(files) do
  155. local srcpath = tmproot .. "\\luafile\\" .. name
  156. if name:match(".+lua$") then
  157. local dstpath = tmproot .. "\\disk\\" .. name .. "c"
  158. local debug = lua_debug
  159. if name == "main.lua" and main_lua_debug then
  160. debug = true
  161. end
  162. oscall(luac_exe .. (debug and " -o " or " -s -o ") .. dstpath .." ".. srcpath, true)
  163. else
  164. local dstpath = tmproot .. "\\disk\\" .. name
  165. oscall("copy " .. srcpath .. " " .. dstpath)
  166. end
  167. end
  168. -- 文件已经全部都在%Temp%\luatos\disk 里面了
  169. -- 接下来, 生成flashx.bin
  170. build_flashx(tmproot .. "\\disk")
  171. -- 然后拷贝一份, 就是升级文件咯
  172. end
  173. cmds["dlfs"] = function ()
  174. -- 看看disk目录在不在
  175. -- 开始发送
  176. -- TODO 插入晨旭老早以前写的tools.lua, 发送文件到设备去
  177. end
  178. cmds["dlrom"] = function ()
  179. -- 检查FLS文件是否存在
  180. -- 通过xmodem协议发送固件
  181. end
  182. cmds["dlfull"] = function ()
  183. cmds["dlrom"]()
  184. cmds["dlfs"]()
  185. end
  186. sys.taskInit(function()
  187. sys.wait(10)
  188. for _, arg in ipairs(win32.args()) do
  189. if cmds[arg] then
  190. cmds[arg]()
  191. elseif cmds["-" .. arg] then
  192. cmds[arg]()
  193. elseif arg:byte() == '-' and arg:find("=") then
  194. local skey = arg:sub(2, arg:find("=") - 1)
  195. local val = arg:sub(arg:find("=") + 1)
  196. if skey and val then
  197. self_conf["basic"][trim5(skey)] = trim5(val)
  198. end
  199. end
  200. end
  201. os.exit(0)
  202. end)
  203. sys.run()