593a4488397a87fc76e1e50319357027077cf752
[project/luci.git] / modules / niu / luasrc / controller / niu / system.lua
1 --[[
2 LuCI - Lua Development Framework
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
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
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local require, pairs, unpack, tonumber = require, pairs, unpack, tonumber
16 module "luci.controller.niu.system"
17
18 function index()
19 local toniu = {on_success_to={"niu"}}
20
21 local e = entry({"niu", "system"}, alias("niu"), "System", 40)
22 e.niu_dbtemplate = "niu/system"
23 e.niu_dbtasks = true
24 e.niu_dbicon = "icons32/preferences-system.png"
25
26 entry({"niu", "system", "general"},
27 cbi("niu/system/general", toniu), "Configure Device", 1)
28
29 entry({"niu", "system", "backup"}, call("backup"), "Backup or Restore Settings", 2)
30 entry({"niu", "system", "upgrade"}, call("upgrade"), "Upgrade Firmware", 30)
31 end
32
33 function backup()
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"
41
42 local restore_fpi
43 http.setfilehandler(
44 function(meta, chunk, eof)
45 if not restore_fpi then
46 restore_fpi = io.popen("tar -xzC/ >/dev/null 2>&1", "w")
47 end
48 if chunk then
49 restore_fpi:write(chunk)
50 end
51 if eof then
52 restore_fpi:close()
53 end
54 end
55 )
56
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"
62
63 if http.formvalue("cancel") then
64 return http.redirect(dsp.build_url("niu"))
65 end
66
67 if backup then
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)
72 end
73 end
74
75
76 http.header(
77 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
78 nixio.uname().nodename, os.date("%Y-%m-%d")
79 }
80 )
81 http.prepare_content("application/x-targz")
82
83
84 local fdin, fdout = nixio.pipe()
85 local devnull = nixio.open("/dev/null", "r+")
86 local proc = nixio.fork()
87
88 if proc == 0 then
89 fdin:close()
90 nixio.dup(devnull, nixio.stdin)
91 nixio.dup(devnull, nixio.stderr)
92 nixio.dup(fdout, nixio.stdout)
93 nixio.exec(unpack(call))
94 os.exit(1)
95 end
96
97 fdout:close()
98 http.splice(fdin)
99 http.close()
100 elseif (upload and #upload > 0) or reset then
101 tpl.render("niu/system/reboot")
102 if nixio.fork() == 0 then
103 nixio.nanosleep(1)
104 if reset then
105 nixio.execp("mtd", "-r", "erase", "rootfs_data")
106 else
107 nixio.execp("reboot")
108 end
109 os.exit(1)
110 end
111 else
112 tpl.render("niu/system/backup", {reset_avail = reset_avail})
113 end
114 end
115
116 function upgrade()
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"
123
124
125 local tmpfile = "/tmp/firmware.img"
126
127 local function image_supported()
128 -- XXX: yay...
129 return ( 0 == os.execute(
130 ". /etc/functions.sh; " ..
131 "include /lib/upgrade; " ..
132 "platform_check_image %q >/dev/null"
133 % tmpfile
134 ) )
135 end
136
137 local function image_checksum()
138 return (util.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
139 end
140
141 local function storage_size()
142 local size = 0
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]+)"')
146 if n == "linux" then
147 size = tonumber(s, 16)
148 break
149 end
150 end
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
156 break
157 end
158 end
159 end
160 return size
161 end
162
163
164 -- Install upload handler
165 local file
166 http.setfilehandler(
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")
170 end
171 if file and chunk then
172 file:write(chunk)
173 end
174 if file and eof then
175 file:close()
176 end
177 end
178 )
179
180
181 -- Determine state
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")
188
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.
192 if step == 4 then
193 if has_platform and has_image and has_support then
194 -- Mimetype text/plain
195 http.prepare_content("text/plain")
196
197 local call = {}
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)
201 end
202 end
203
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
208 })
209
210 if fd then
211 while true do
212 local ln = fd:read("*l")
213 if not ln then break end
214 http.write(ln .. "\n")
215 end
216 fd:close()
217 end
218
219 -- Make sure the device is rebooted
220 if nixio.fork() == 0 then
221 nixio.nanosleep(1)
222 nixio.execp("reboot")
223 os.exit(1)
224 end
225 end
226
227
228 --
229 -- This is step 1-3, which does the user interaction and
230 -- image upload.
231 --
232
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.
237 if has_image then
238 nixio.fs.unlink(tmpfile)
239 end
240
241 tpl.render("niu/system/upgrade", {
242 step=1,
243 bad_image=(has_image and not has_support or false),
244 keepavail=keep_avail,
245 supported=has_platform
246 } )
247
248 -- Step 2: present uploaded file, show checksum, confirmation
249 elseif step == 2 then
250 tpl.render("niu/system/upgrade", {
251 step=2,
252 checksum=image_checksum(),
253 filesize=nixio.fs.stat(tmpfile).size,
254 flashsize=storage_size(),
255 keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")
256 } )
257
258 -- Step 3: load iframe which calls the actual flash procedure
259 elseif step == 3 then
260 tpl.render("niu/system/upgrade", {
261 step=3,
262 keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")
263 } )
264 end
265 end