2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2011 Jo-Philipp Wich <xm@subsignal.org>
6 Copyright 2012 Daniel Golle <dgolle@allnet.de>
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
17 module("luci.controller.failsafe.failsafe", package.seeall)
21 if not root.target then
22 root.target = alias("failsafe")
28 page.target = alias("failsafe")
32 page = node("failsafe")
33 page.title = _("Fail-safe")
34 page.target = alias("failsafe", "flashops")
37 page.setgroup = "root"
40 entry({"failsafe", "flashops"}, call("action_flashops"), _("Flash Firmware"), 70).index = true
41 entry({"failsafe", "reboot"}, call("action_reboot"), _("Reboot"), 90)
44 function action_flashops()
45 local sys = require "luci.sys"
46 local fs = require "luci.fs"
48 local upgrade_avail = nixio.fs.access("/lib/upgrade/platform.sh")
49 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
51 local image_tmp = "/tmp/firmware.img"
53 local function image_supported()
55 return ( 0 == os.execute(
56 ". /lib/functions.sh; " ..
57 "include /lib/upgrade; " ..
58 "platform_check_image %q >/dev/null"
63 local function image_checksum()
64 return (luci.sys.exec("md5sum %q" % image_tmp):match("^([^%s]+)"))
67 local function storage_size()
69 if nixio.fs.access("/proc/mtd") then
70 for l in io.lines("/proc/mtd") do
71 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
72 if n == "linux" or n == "firmware" then
73 size = tonumber(s, 16)
77 elseif nixio.fs.access("/proc/partitions") then
78 for l in io.lines("/proc/partitions") do
79 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
80 if b and n and not n:match('[0-9]') then
81 size = tonumber(b) * 1024
91 luci.http.setfilehandler(
92 function(meta, chunk, eof)
94 if meta and meta.name == "image" then
95 fp = io.open(image_tmp, "w")
107 if luci.http.formvalue("image") or luci.http.formvalue("step") then
109 -- Initiate firmware flash
111 local step = tonumber(luci.http.formvalue("step") or 1)
113 if image_supported() then
114 luci.template.render("failsafe/upgrade", {
115 checksum = image_checksum(),
116 storage = storage_size(),
117 size = nixio.fs.stat(image_tmp).size,
121 nixio.fs.unlink(image_tmp)
122 luci.template.render("failsafe/flashops", {
123 reset_avail = reset_avail,
124 upgrade_avail = upgrade_avail,
129 -- Start sysupgrade flash
131 elseif step == 2 then
132 local keep = (luci.http.formvalue("keep") == "1") and "" or "-n"
133 luci.template.render("failsafe/applyreboot", {
134 title = luci.i18n.translate("Flashing..."),
135 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."),
136 addr = (#keep > 0) and "192.168.1.1" or nil
138 fork_exec("killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
144 luci.template.render("failsafe/flashops", {
145 reset_avail = reset_avail,
146 upgrade_avail = upgrade_avail
151 function action_reboot()
152 local reboot = luci.http.formvalue("reboot")
153 luci.template.render("failsafe/reboot", {reboot=reboot})
159 function fork_exec(command)
160 local pid = nixio.fork()
164 -- change to root dir
167 -- patch stdin, out, err to /dev/null
168 local null = nixio.open("/dev/null", "w+")
170 nixio.dup(null, nixio.stderr)
171 nixio.dup(null, nixio.stdout)
172 nixio.dup(null, nixio.stdin)
173 if null:fileno() > 2 then
178 -- replace with target command
179 nixio.exec("/bin/sh", "-c", command)
183 function ltn12_popen(command)
185 local fdi, fdo = nixio.pipe()
186 local pid = nixio.fork()
192 local buffer = fdi:read(2048)
193 local wpid, stat = nixio.waitpid(pid, "nohang")
194 if not close and wpid and stat == "exited" then
198 if buffer and #buffer > 0 then
206 nixio.dup(fdo, nixio.stdout)
209 nixio.exec("/bin/sh", "-c", command)