upgrade.lua 3.5 KB

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