1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Copyright 2012 Daniel Golle <dgolle@allnet.de>
4 -- Licensed to the public under the Apache License 2.0.
6 module("luci.controller.failsafe.failsafe", package.seeall)
10 if not root.target then
11 root.target = alias("failsafe")
17 page.target = alias("failsafe")
21 page = node("failsafe")
22 page.title = _("Fail-safe")
23 page.target = alias("failsafe", "flashops")
26 page.setgroup = "root"
29 entry({"failsafe", "flashops"}, call("action_flashops"), _("Flash Firmware"), 70).index = true
30 entry({"failsafe", "reboot"}, call("action_reboot"), _("Reboot"), 90)
33 function action_flashops()
34 local sys = require "luci.sys"
35 local fs = require "nixio.fs"
37 local upgrade_avail = fs.access("/lib/upgrade/platform.sh")
38 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
40 local image_tmp = "/tmp/firmware.img"
42 local function image_supported()
44 return ( 0 == os.execute(
45 ". /lib/functions.sh; " ..
46 "include /lib/upgrade; " ..
47 "platform_check_image %q >/dev/null"
52 local function image_checksum()
53 return (luci.sys.exec("md5sum %q" % image_tmp):match("^([^%s]+)"))
56 local function storage_size()
58 if fs.access("/proc/mtd") then
59 for l in io.lines("/proc/mtd") do
60 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
61 if n == "linux" or n == "firmware" then
62 size = tonumber(s, 16)
66 elseif fs.access("/proc/partitions") then
67 for l in io.lines("/proc/partitions") do
68 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
69 if b and n and not n:match('[0-9]') then
70 size = tonumber(b) * 1024
80 luci.http.setfilehandler(
81 function(meta, chunk, eof)
83 if meta and meta.name == "image" then
84 fp = io.open(image_tmp, "w")
98 if luci.http.formvalue("image") or luci.http.formvalue("step") then
100 -- Initiate firmware flash
102 local step = tonumber(luci.http.formvalue("step") or 1)
104 if image_supported() then
105 luci.template.render("failsafe/upgrade", {
106 checksum = image_checksum(),
107 storage = storage_size(),
108 size = (fs.stat(image_tmp, "size") or 0),
113 luci.template.render("failsafe/flashops", {
114 reset_avail = reset_avail,
115 upgrade_avail = upgrade_avail,
120 -- Start sysupgrade flash
122 elseif step == 2 then
123 local keep = (luci.http.formvalue("keep") == "1") and "" or "-n"
124 luci.template.render("failsafe/applyreboot", {
125 title = luci.i18n.translate("Flashing..."),
126 msg = luci.i18n.translate("The system is flashing now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),
127 addr = (#keep > 0) and "192.168.1.1" or nil
129 fork_exec("killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
135 luci.template.render("failsafe/flashops", {
136 reset_avail = reset_avail,
137 upgrade_avail = upgrade_avail
142 function action_reboot()
143 local reboot = luci.http.formvalue("reboot")
144 luci.template.render("failsafe/reboot", {reboot=reboot})
150 function fork_exec(command)
151 local pid = nixio.fork()
155 -- change to root dir
158 -- patch stdin, out, err to /dev/null
159 local null = nixio.open("/dev/null", "w+")
161 nixio.dup(null, nixio.stderr)
162 nixio.dup(null, nixio.stdout)
163 nixio.dup(null, nixio.stdin)
164 if null:fileno() > 2 then
169 -- replace with target command
170 nixio.exec("/bin/sh", "-c", command)
174 function ltn12_popen(command)
176 local fdi, fdo = nixio.pipe()
177 local pid = nixio.fork()
183 local buffer = fdi:read(2048)
184 local wpid, stat = nixio.waitpid(pid, "nohang")
185 if not close and wpid and stat == "exited" then
189 if buffer and #buffer > 0 then
197 nixio.dup(fdo, nixio.stdout)
200 nixio.exec("/bin/sh", "-c", command)