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 entry({"niu", "system"}, nil, "System", 20).dbtemplate = "niu/system"
21 entry({"niu", "system", "general"},
22 cbi("niu/system/general", {on_success_to={"niu"}}), "Configure Device", 10)
24 entry({"niu", "system", "backup"}, call("backup"), "Backup or Restore Settings", 20)
25 entry({"niu", "system", "upgrade"}, call("upgrade"), "Upgrade Firmware", 40)
29 local dsp = require "luci.dispatcher"
30 local os, io = require "os", require "io"
31 local uci = require "luci.model.uci".inst
32 local nixio, nutl = require "nixio", require "nixio.util"
33 local fs = require "nixio.fs"
34 local http = require "luci.http"
35 local tpl = require "luci.template"
39 function(meta, chunk, eof)
40 if not restore_fpi then
41 restore_fpi = io.popen("tar -xzC/ >/dev/null 2>&1", "w")
44 restore_fpi:write(chunk)
52 local reset_avail = (fs.readfile("/proc/mtd") or ""):find('"rootfs_data"')
53 local upload = http.formvalue("archive")
54 local backup = http.formvalue("backup")
55 local reset = reset_avail and http.formvalue("reset")
56 local backup_cmd = "tar -cz %s 2>/dev/null"
58 if http.formvalue("cancel") then
59 return http.redirect(dsp.build_url("niu"))
63 local call = {"/bin/tar", "-cz"}
64 for k, v in pairs(uci:get_all("luci", "flash_keep")) do
65 if k:byte() ~= 46 then -- k[1] ~= "."
66 nutl.consume(fs.glob(v), call)
72 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
73 nixio.uname().nodename, os.date("%Y-%m-%d")
76 http.prepare_content("application/x-targz")
79 local fdin, fdout = nixio.pipe()
80 local devnull = nixio.open("/dev/null", "r+")
81 local proc = nixio.fork()
85 nixio.dup(devnull, nixio.stdin)
86 nixio.dup(devnull, nixio.stderr)
87 nixio.dup(fdout, nixio.stdout)
88 nixio.exec(unpack(call))
95 elseif (upload and #upload > 0) or reset then
96 tpl.render("niu/system/reboot")
97 if nixio.fork() == 0 then
100 nixio.execp("mtd", "-r", "erase", "rootfs_data")
102 nixio.execp("reboot")
107 tpl.render("niu/system/backup", {reset_avail = reset_avail})
112 local io, os, table = require "io", require "os", require "table"
113 local uci = require "luci.store".uci_state
114 local http = require "luci.http"
115 local util = require "luci.util"
116 local tpl = require "luci.template"
117 local nixio = require "nixio", require "nixio.util", require "nixio.fs"
120 local tmpfile = "/tmp/firmware.img"
122 local function image_supported()
124 return ( 0 == os.execute(
125 ". /etc/functions.sh; " ..
126 "include /lib/upgrade; " ..
127 "platform_check_image %q >/dev/null"
132 local function image_checksum()
133 return (util.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
136 local function storage_size()
138 if nixio.fs.access("/proc/mtd") then
139 for l in io.lines("/proc/mtd") do
140 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
142 size = tonumber(s, 16)
146 elseif nixio.fs.access("/proc/partitions") then
147 for l in io.lines("/proc/partitions") do
148 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
149 if b and n and not n:match('[0-9]') then
150 size = tonumber(b) * 1024
159 -- Install upload handler
162 function(meta, chunk, eof)
163 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
164 file = io.open(tmpfile, "w")
166 if file and chunk then
177 local keep_avail = true
178 local step = tonumber(http.formvalue("step") or 1)
179 local has_image = nixio.fs.access(tmpfile)
180 local has_support = image_supported()
181 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
182 local has_upload = http.formvalue("image")
184 -- This does the actual flashing which is invoked inside an iframe
185 -- so don't produce meaningful errors here because the the
186 -- previous pages should arrange the stuff as required.
188 if has_platform and has_image and has_support then
189 -- Mimetype text/plain
190 http.prepare_content("text/plain")
193 for k, v in pairs(uci:get_all("luci", "flash_keep")) do
194 if k:byte() ~= 46 then -- k[1] ~= "."
195 nixio.util.consume(nixio.fs.glob(v), call)
199 -- Now invoke sysupgrade
200 local keepcfg = keep_avail and http.formvalue("keepcfg") == "1"
201 local fd = io.popen("/sbin/luci-flash %s %q" %{
202 keepcfg and "-k %q" % table.concat(call, " ") or "", tmpfile
207 local ln = fd:read("*l")
208 if not ln then break end
209 http.write(ln .. "\n")
214 -- Make sure the device is rebooted
215 if nixio.fork() == 0 then
217 nixio.execp("reboot")
224 -- This is step 1-3, which does the user interaction and
228 -- Step 1: file upload, error on unsupported image format
229 elseif not has_image or not has_support or step == 1 then
230 -- If there is an image but user has requested step 1
231 -- or type is not supported, then remove it.
233 nixio.fs.unlink(tmpfile)
236 tpl.render("niu/system/upgrade", {
238 bad_image=(has_image and not has_support or false),
239 keepavail=keep_avail,
240 supported=has_platform
243 -- Step 2: present uploaded file, show checksum, confirmation
244 elseif step == 2 then
245 tpl.render("niu/system/upgrade", {
247 checksum=image_checksum(),
248 filesize=nixio.fs.stat(tmpfile).size,
249 flashsize=storage_size(),
250 keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")
253 -- Step 3: load iframe which calls the actual flash procedure
254 elseif step == 3 then
255 tpl.render("niu/system/upgrade", {
257 keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")