1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 module("luci.controller.mini.system", package.seeall)
8 entry({"mini", "system"}, alias("mini", "system", "index"), _("System"), 40).index = true
9 entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), _("General"), 1)
10 entry({"mini", "system", "passwd"}, form("mini/passwd"), _("Admin Password"), 10)
11 entry({"mini", "system", "backup"}, call("action_backup"), _("Backup / Restore"), 80)
12 entry({"mini", "system", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 90)
13 entry({"mini", "system", "reboot"}, call("action_reboot"), _("Reboot"), 100)
16 function action_backup()
17 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
18 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
19 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
22 luci.http.setfilehandler(
23 function(meta, chunk, eof)
24 if not restore_fpi then
25 restore_fpi = io.popen(restore_cmd, "w")
28 restore_fpi:write(chunk)
36 local upload = luci.http.formvalue("archive")
37 local backup = luci.http.formvalue("backup")
38 local reset = reset_avail and luci.http.formvalue("reset")
40 if upload and #upload > 0 then
41 luci.template.render("mini/applyreboot")
44 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
45 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
46 luci.sys.hostname(), os.date("%Y-%m-%d")})
47 luci.http.prepare_content("application/x-targz")
48 luci.ltn12.pump.all(reader, luci.http.write)
50 luci.template.render("mini/applyreboot")
51 luci.util.exec("mtd -r erase rootfs_data")
53 luci.template.render("mini/backup", {reset_avail = reset_avail})
57 function action_reboot()
58 local reboot = luci.http.formvalue("reboot")
59 luci.template.render("mini/reboot", {reboot=reboot})
65 function action_upgrade()
66 require("luci.model.uci")
68 local tmpfile = "/tmp/firmware.img"
70 local function image_supported()
72 return ( 0 == os.execute(
73 ". /lib/functions.sh; " ..
74 "include /lib/upgrade; " ..
75 "platform_check_image %q >/dev/null"
80 local function image_checksum()
81 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
84 local function storage_size()
86 if nixio.fs.access("/proc/mtd") then
87 for l in io.lines("/proc/mtd") do
88 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
90 size = tonumber(s, 16)
94 elseif nixio.fs.access("/proc/partitions") then
95 for l in io.lines("/proc/partitions") do
96 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
97 if b and n and not n:match('[0-9]') then
98 size = tonumber(b) * 1024
107 -- Install upload handler
109 luci.http.setfilehandler(
110 function(meta, chunk, eof)
111 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
112 file = io.open(tmpfile, "w")
114 if file and chunk then
125 local keep_avail = true
126 local step = tonumber(luci.http.formvalue("step") or 1)
127 local has_image = nixio.fs.access(tmpfile)
128 local has_support = image_supported()
129 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
130 local has_upload = luci.http.formvalue("image")
132 -- This does the actual flashing which is invoked inside an iframe
133 -- so don't produce meaningful errors here because the the
134 -- previous pages should arrange the stuff as required.
136 if has_platform and has_image and has_support then
137 -- Mimetype text/plain
138 luci.http.prepare_content("text/plain")
139 luci.http.write("Starting luci-flash...\n")
141 -- Now invoke sysupgrade
142 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
143 local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
144 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
147 luci.ltn12.pump.all(flash, luci.http.write)
149 -- Make sure the device is rebooted
155 -- This is step 1-3, which does the user interaction and
159 -- Step 1: file upload, error on unsupported image format
160 elseif not has_image or not has_support or step == 1 then
161 -- If there is an image but user has requested step 1
162 -- or type is not supported, then remove it.
164 nixio.fs.unlink(tmpfile)
167 luci.template.render("mini/upgrade", {
169 bad_image=(has_image and not has_support or false),
170 keepavail=keep_avail,
171 supported=has_platform
174 -- Step 2: present uploaded file, show checksum, confirmation
175 elseif step == 2 then
176 luci.template.render("mini/upgrade", {
178 checksum=image_checksum(),
179 filesize=nixio.fs.stat(tmpfile).size,
180 flashsize=storage_size(),
181 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
184 -- Step 3: load iframe which calls the actual flash procedure
185 elseif step == 3 then
186 luci.template.render("mini/upgrade", {
188 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
193 function _keep_pattern()
195 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
198 for k, v in pairs(files) do
199 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
200 kpattern = kpattern .. " " .. v
207 function ltn12_popen(command)
209 local fdi, fdo = nixio.pipe()
210 local pid = nixio.fork()
216 local buffer = fdi:read(2048)
217 local wpid, stat = nixio.waitpid(pid, "nohang")
218 if not close and wpid and stat == "exited" then
222 if buffer and #buffer > 0 then
230 nixio.dup(fdo, nixio.stdout)
233 nixio.exec("/bin/sh", "-c", command)