upgrade.lua 3.7 KB

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