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")
96 if luci.http.formvalue("image") or luci.http.formvalue("step") then
98 -- Initiate firmware flash
100 local step = tonumber(luci.http.formvalue("step") or 1)
102 if image_supported() then
103 luci.template.render("failsafe/upgrade", {
104 checksum = image_checksum(),
105 storage = storage_size(),
106 size = (fs.stat(image_tmp, "size") or 0),
111 luci.template.render("failsafe/flashops", {
112 reset_avail = reset_avail,
113 upgrade_avail = upgrade_avail,
118 -- Start sysupgrade flash
120 elseif step == 2 then
121 local keep = (luci.http.formvalue("keep") == "1") and "" or "-n"
122 luci.template.render("failsafe/applyreboot", {
123 title = luci.i18n.translate("Flashing..."),
124 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."),
125 addr = (#keep > 0) and "192.168.1.1" or nil
127 fork_exec("killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
133 luci.template.render("failsafe/flashops", {
134 reset_avail = reset_avail,
135 upgrade_avail = upgrade_avail
140 function action_reboot()
141 local reboot = luci.http.formvalue("reboot")
142 luci.template.render("failsafe/reboot", {reboot=reboot})
148 function fork_exec(command)
149 local pid = nixio.fork()
153 -- change to root dir
156 -- patch stdin, out, err to /dev/null
157 local null = nixio.open("/dev/null", "w+")
159 nixio.dup(null, nixio.stderr)
160 nixio.dup(null, nixio.stdout)
161 nixio.dup(null, nixio.stdin)
162 if null:fileno() > 2 then
167 -- replace with target command
168 nixio.exec("/bin/sh", "-c", command)
172 function ltn12_popen(command)
174 local fdi, fdo = nixio.pipe()
175 local pid = nixio.fork()
181 local buffer = fdi:read(2048)
182 local wpid, stat = nixio.waitpid(pid, "nohang")
183 if not close and wpid and stat == "exited" then
187 if buffer and #buffer > 0 then
195 nixio.dup(fdo, nixio.stdout)
198 nixio.exec("/bin/sh", "-c", command)