upgrade.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
  14. if has_platform then
  15. entry({"admin", "upgrade"}, call("action_upgrade"), "Firmware aktualisieren", 90)
  16. entry({"admin", "upgrade", "reboot"}, template("admin/upgrade_reboot"), nil, nil)
  17. end
  18. end
  19. function action_upgrade()
  20. local tmpfile = "/tmp/firmware.img"
  21. -- Install upload handler
  22. local file
  23. luci.http.setfilehandler(
  24. function(meta, chunk, eof)
  25. if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
  26. file = io.open(tmpfile, "w")
  27. end
  28. if file and chunk then
  29. file:write(chunk)
  30. end
  31. if file and eof then
  32. file:close()
  33. end
  34. end
  35. )
  36. -- Determine state
  37. local step = tonumber(luci.http.formvalue("step") or 1)
  38. local has_image = nixio.fs.access(tmpfile)
  39. local has_support = image_supported(tmpfile)
  40. -- Step 1: file upload, error on unsupported image format
  41. if not has_image or not has_support or step == 1 then
  42. -- If there is an image but user has requested step 1
  43. -- or type is not supported, then remove it.
  44. if has_image then
  45. nixio.fs.unlink(tmpfile)
  46. end
  47. luci.template.render("admin/upgrade", {
  48. bad_image=(has_image and not has_support or false)
  49. } )
  50. -- Step 2: present uploaded file, show checksum, confirmation
  51. elseif step == 2 then
  52. luci.template.render("admin/upgrade_confirm", {
  53. checksum=image_checksum(tmpfile),
  54. filesize=nixio.fs.stat(tmpfile).size,
  55. flashsize=storage_size(),
  56. keepconfig=luci.http.formvalue("keepcfg") == "1"
  57. } )
  58. elseif step == 3 then
  59. local keepcfg = luci.http.formvalue("keepcfg") == "1"
  60. fork_exec("/sbin/sysupgrade %s %q" % { keepcfg and "" or "-n", tmpfile })
  61. luci.http.redirect(luci.dispatcher.build_url("admin", "upgrade", "reboot"))
  62. end
  63. end
  64. function fork_exec(command)
  65. local pid = nixio.fork()
  66. if pid > 0 then
  67. return
  68. elseif pid == 0 then
  69. -- change to root dir
  70. nixio.chdir("/")
  71. -- patch stdin, out, err to /dev/null
  72. local null = nixio.open("/dev/null", "w+")
  73. if null then
  74. nixio.dup(null, nixio.stderr)
  75. nixio.dup(null, nixio.stdout)
  76. nixio.dup(null, nixio.stdin)
  77. if null:fileno() > 2 then
  78. null:close()
  79. end
  80. end
  81. -- replace with target command
  82. nixio.exec("/bin/sh", "-c", command)
  83. end
  84. end
  85. function image_supported(tmpfile)
  86. -- XXX: yay...
  87. return ( 0 == os.execute(
  88. ". /lib/functions.sh; " ..
  89. "include /lib/upgrade; " ..
  90. "platform_check_image %q >/dev/null"
  91. % tmpfile
  92. ) )
  93. end
  94. function storage_size()
  95. local size = 0
  96. if nixio.fs.access("/proc/mtd") then
  97. for l in io.lines("/proc/mtd") do
  98. local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
  99. if n == "linux" then
  100. size = tonumber(s, 16)
  101. break
  102. end
  103. end
  104. elseif nixio.fs.access("/proc/partitions") then
  105. for l in io.lines("/proc/partitions") do
  106. local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
  107. if b and n and not n:match('[0-9]') then
  108. size = tonumber(b) * 1024
  109. break
  110. end
  111. end
  112. end
  113. return size
  114. end
  115. function image_checksum(tmpfile)
  116. return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
  117. end