upgrade.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2008 Steven Barth <steven@midlink.org>
  4. Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. $Id$
  10. ]]--
  11. module("luci.controller.admin.upgrade", package.seeall)
  12. function index()
  13. entry({"admin", "upgrade"}, call("action_upgrade"), "Firmware aktualisieren", 90)
  14. end
  15. function action_upgrade()
  16. require("luci.model.uci")
  17. local tmpfile = "/tmp/firmware.img"
  18. local function image_supported()
  19. -- XXX: yay...
  20. return ( 0 == os.execute(
  21. ". /lib/functions.sh; " ..
  22. "include /lib/upgrade; " ..
  23. "platform_check_image %q >/dev/null"
  24. % tmpfile
  25. ) )
  26. end
  27. local function image_checksum()
  28. return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
  29. end
  30. local function storage_size()
  31. local size = 0
  32. if nixio.fs.access("/proc/mtd") then
  33. for l in io.lines("/proc/mtd") do
  34. local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
  35. if n == "linux" then
  36. size = tonumber(s, 16)
  37. break
  38. end
  39. end
  40. elseif nixio.fs.access("/proc/partitions") then
  41. for l in io.lines("/proc/partitions") do
  42. local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
  43. if b and n and not n:match('[0-9]') then
  44. size = tonumber(b) * 1024
  45. break
  46. end
  47. end
  48. end
  49. return size
  50. end
  51. -- Install upload handler
  52. local file
  53. luci.http.setfilehandler(
  54. function(meta, chunk, eof)
  55. if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
  56. file = io.open(tmpfile, "w")
  57. end
  58. if file and chunk then
  59. file:write(chunk)
  60. end
  61. if file and eof then
  62. file:close()
  63. end
  64. end
  65. )
  66. -- Determine state
  67. local keep_avail = true
  68. local step = tonumber(luci.http.formvalue("step") or 1)
  69. local has_image = nixio.fs.access(tmpfile)
  70. local has_support = image_supported()
  71. local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
  72. local has_upload = luci.http.formvalue("image")
  73. --
  74. -- This is step 1-3, which does the user interaction and
  75. -- image upload.
  76. --
  77. -- Step 1: file upload, error on unsupported image format
  78. if not has_image or not has_support or step == 1 then
  79. -- If there is an image but user has requested step 1
  80. -- or type is not supported, then remove it.
  81. if has_image then
  82. nixio.fs.unlink(tmpfile)
  83. end
  84. luci.template.render("admin/upgrade", {
  85. step=1,
  86. bad_image=(has_image and not has_support or false),
  87. keepavail=keep_avail,
  88. supported=has_platform
  89. } )
  90. -- Step 2: present uploaded file, show checksum, confirmation
  91. elseif step == 2 then
  92. luci.template.render("admin/upgrade", {
  93. step=2,
  94. checksum=image_checksum(),
  95. filesize=nixio.fs.stat(tmpfile).size,
  96. flashsize=storage_size(),
  97. keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
  98. } )
  99. -- Step 3: load iframe which calls the actual flash procedure
  100. elseif step == 3 then
  101. -- invoke sysupgrade
  102. local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
  103. fork_exec("/sbin/sysupgrade %s %q" %
  104. { keepcfg and "" or "-n"
  105. , tmpfile
  106. }
  107. )
  108. luci.template.render("admin/upgrade", { step=3 } )
  109. end
  110. end
  111. function fork_exec(command)
  112. local pid = nixio.fork()
  113. if pid > 0 then
  114. return
  115. elseif pid == 0 then
  116. -- change to root dir
  117. nixio.chdir("/")
  118. -- patch stdin, out, err to /dev/null
  119. local null = nixio.open("/dev/null", "w+")
  120. if null then
  121. nixio.dup(null, nixio.stderr)
  122. nixio.dup(null, nixio.stdout)
  123. nixio.dup(null, nixio.stdin)
  124. if null:fileno() > 2 then
  125. null:close()
  126. end
  127. end
  128. -- replace with target command
  129. nixio.exec("/bin/sh", "-c", command)
  130. end
  131. end