2 LuCI - Lua Development Framework
4 Copyright 2009 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
15 local require, pairs, unpack, tonumber = require, pairs, unpack, tonumber
16 module "luci.controller.niu.system"
19 local toniu = {on_success_to={"niu"}}
21 local e = entry({"niu", "system"}, alias("niu"), "System", 20)
22 e.niu_dbtemplate = "niu/system"
24 e.niu_dbicon = "icons32/preferences-system.png"
26 entry({"niu", "system", "general"},
27 cbi("niu/system/general", toniu), "Configure Device", 1)
29 entry({"niu", "system", "backup"}, call("backup"), "Backup or Restore Settings", 2)
30 entry({"niu", "system", "upgrade"}, call("upgrade"), "Upgrade Firmware", 30)
34 local dsp = require "luci.dispatcher"
35 local os, io = require "os", require "io"
36 local uci = require "luci.model.uci".inst
37 local nixio, nutl = require "nixio", require "nixio.util"
38 local fs = require "nixio.fs"
39 local http = require "luci.http"
40 local tpl = require "luci.template"
44 function(meta, chunk, eof)
45 if not restore_fpi then
46 restore_fpi = io.popen("tar -xzC/ >/dev/null 2>&1", "w")
49 restore_fpi:write(chunk)
57 local reset_avail = (fs.readfile("/proc/mtd") or ""):find('"rootfs_data"')
58 local upload = http.formvalue("archive")
59 local backup = http.formvalue("backup")
60 local reset = reset_avail and http.formvalue("reset")
61 local backup_cmd = "tar -cz %s 2>/dev/null"
63 if http.formvalue("cancel") then
64 return http.redirect(dsp.build_url("niu"))
68 local call = {"/bin/tar", "-cz"}
69 for k, v in pairs(uci:get_all("luci", "flash_keep")) do
70 if k:byte() ~= 46 then -- k[1] ~= "."
71 nutl.consume(fs.glob(v), call)
77 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
78 nixio.uname().nodename, os.date("%Y-%m-%d")
81 http.prepare_content("application/x-targz")
84 local fdin, fdout = nixio.pipe()
85 local devnull = nixio.open("/dev/null", "r+")
86 local proc = nixio.fork()
90 nixio.dup(devnull, nixio.stdin)
91 nixio.dup(devnull, nixio.stderr)
92 nixio.dup(fdout, nixio.stdout)
93 nixio.exec(unpack(call))
100 elseif (upload and #upload > 0) or reset then
101 tpl.render("niu/system/reboot")
102 if nixio.fork() == 0 then
105 nixio.execp("mtd", "-r", "erase", "rootfs_data")
107 nixio.execp("reboot")
112 tpl.render("niu/system/backup", {reset_avail = reset_avail})
117 local io, os, table = require "io", require "os", require "table"
118 local uci = require "luci.store".uci_state
119 local http = require "luci.http"
120 local util = require "luci.util"
121 local tpl = require "luci.template"
122 local nixio = require "nixio", require "nixio.util", require "nixio.fs"
125 local tmpfile = "/tmp/firmware.img"
127 local function image_supported()
129 return ( 0 == os.execute(
130 ". /etc/functions.sh; " ..
131 "include /lib/upgrade; " ..
132 "platform_check_image %q >/dev/null"
137 local function image_checksum()
138 return (util.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
141 local function storage_size()
143 if nixio.fs.access("/proc/mtd") then
144 for l in io.lines("/proc/mtd") do
145 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
147 size = tonumber(s, 16)
151 elseif nixio.fs.access("/proc/partitions") then
152 for l in io.lines("/proc/partitions") do
153 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
154 if b and n and not n:match('[0-9]') then
155 size = tonumber(b) * 1024
164 -- Install upload handler
167 function(meta, chunk, eof)
168 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
169 file = io.open(tmpfile, "w")
171 if file and chunk then
182 local keep_avail = true
183 local step = tonumber(http.formvalue("step") or 1)
184 local has_image = nixio.fs.access(tmpfile)
185 local has_support = image_supported()
186 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
187 local has_upload = http.formvalue("image")
189 -- This does the actual flashing which is invoked inside an iframe
190 -- so don't produce meaningful errors here because the the
191 -- previous pages should arrange the stuff as required.
193 if has_platform and has_image and has_support then
194 -- Mimetype text/plain
195 http.prepare_content("text/plain")
198 for k, v in pairs(uci:get_all("luci", "flash_keep")) do
199 if k:byte() ~= 46 then -- k[1] ~= "."
200 nixio.util.consume(nixio.fs.glob(v), call)
204 -- Now invoke sysupgrade
205 local keepcfg = keep_avail and http.formvalue("keepcfg") == "1"
206 local fd = io.popen("/sbin/luci-flash %s %q" %{
207 keepcfg and "-k %q" % table.concat(call, " ") or "", tmpfile
212 local ln = fd:read("*l")
213 if not ln then break end
214 http.write(ln .. "\n")
219 -- Make sure the device is rebooted
220 if nixio.fork() == 0 then
222 nixio.execp("reboot")
229 -- This is step 1-3, which does the user interaction and
233 -- Step 1: file upload, error on unsupported image format
234 elseif not has_image or not has_support or step == 1 then
235 -- If there is an image but user has requested step 1
236 -- or type is not supported, then remove it.
238 nixio.fs.unlink(tmpfile)
241 tpl.render("niu/system/upgrade", {
243 bad_image=(has_image and not has_support or false),
244 keepavail=keep_avail,
245 supported=has_platform
248 -- Step 2: present uploaded file, show checksum, confirmation
249 elseif step == 2 then
250 tpl.render("niu/system/upgrade", {
252 checksum=image_checksum(),
253 filesize=nixio.fs.stat(tmpfile).size,
254 flashsize=storage_size(),
255 keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")
258 -- Step 3: load iframe which calls the actual flash procedure
259 elseif step == 3 then
260 tpl.render("niu/system/upgrade", {
262 keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")