upgrade.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. --[[
  2. Copyright 2008 Steven Barth <steven@midlink.org>
  3. Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. ]]--
  9. local fs = require 'nixio.fs'
  10. local tmpfile = "/tmp/firmware.img"
  11. local function filehandler(meta, chunk, eof)
  12. if not fs.access(tmpfile) and not file and chunk and #chunk > 0 then
  13. file = io.open(tmpfile, "w")
  14. end
  15. if file and chunk then
  16. file:write(chunk)
  17. end
  18. if file and eof then
  19. file:close()
  20. end
  21. end
  22. local function action_upgrade(http, renderer)
  23. local disp = require 'gluon.web.dispatcher'
  24. local nixio = require 'nixio'
  25. local function fork_exec(...)
  26. local pid = nixio.fork()
  27. if pid > 0 then
  28. return
  29. elseif pid == 0 then
  30. -- change to root dir
  31. nixio.chdir("/")
  32. -- patch stdin, out, err to /dev/null
  33. local null = nixio.open("/dev/null", "w+")
  34. if null then
  35. nixio.dup(null, nixio.stderr)
  36. nixio.dup(null, nixio.stdout)
  37. nixio.dup(null, nixio.stdin)
  38. if null:fileno() > 2 then
  39. null:close()
  40. end
  41. end
  42. -- Sleep a little so the browser can fetch everything required to
  43. -- display the reboot page, then reboot the device.
  44. nixio.nanosleep(1)
  45. -- replace with target command
  46. nixio.exec(...)
  47. end
  48. end
  49. local function image_supported(tmpfile)
  50. -- XXX: yay...
  51. return (os.execute(string.format("/sbin/sysupgrade -T %q >/dev/null", tmpfile)) == 0)
  52. end
  53. local function storage_size()
  54. local size = 0
  55. if fs.access("/proc/mtd") then
  56. for l in io.lines("/proc/mtd") do
  57. local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
  58. if n == "linux" then
  59. size = tonumber(s, 16)
  60. break
  61. end
  62. end
  63. elseif fs.access("/proc/partitions") then
  64. for l in io.lines("/proc/partitions") do
  65. local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
  66. if b and n and not n:match('[0-9]') then
  67. size = tonumber(b) * 1024
  68. break
  69. end
  70. end
  71. end
  72. return size
  73. end
  74. local function image_checksum(tmpfile)
  75. return (gluon.web.util.exec(string.format("md5sum %q", tmpfile)):match("^([^%s]+)"))
  76. end
  77. -- Determine state
  78. local step = tonumber(http:getenv("REQUEST_METHOD") == "POST" and http:formvalue("step")) or 1
  79. local has_image = fs.access(tmpfile)
  80. local has_support = has_image and image_supported(tmpfile)
  81. -- Step 1: file upload, error on unsupported image format
  82. if step == 1 or not has_support then
  83. -- If there is an image but user has requested step 1
  84. -- or type is not supported, then remove it.
  85. if has_image then
  86. fs.unlink(tmpfile)
  87. end
  88. renderer.render("layout", {
  89. content = "admin/upgrade",
  90. bad_image = has_image and not has_support,
  91. })
  92. -- Step 2: present uploaded file, show checksum, confirmation
  93. elseif step == 2 then
  94. renderer.render("layout", {
  95. content = "admin/upgrade_confirm",
  96. checksum = image_checksum(tmpfile),
  97. filesize = fs.stat(tmpfile).size,
  98. flashsize = storage_size(),
  99. keepconfig = (http:formvalue("keepcfg") == "1"),
  100. })
  101. elseif step == 3 then
  102. if http:formvalue("keepcfg") == "1" then
  103. fork_exec("/sbin/sysupgrade", tmpfile)
  104. else
  105. fork_exec("/sbin/sysupgrade", "-n", tmpfile)
  106. end
  107. renderer.render("layout", {
  108. content = "admin/upgrade_reboot",
  109. hidenav = true,
  110. })
  111. end
  112. end
  113. local has_platform = fs.access("/lib/upgrade/platform.sh")
  114. if has_platform then
  115. local upgrade = entry({"admin", "upgrade"}, call(action_upgrade), _("Upgrade firmware"), 90)
  116. upgrade.filehandler = filehandler
  117. end