1 -- Copyright 2017-2018 Stan Grishin <stangri@melmac.net>
2 -- Licensed to the public under the Apache License 2.0.
4 module("luci.controller.advanced_reboot", package.seeall)
6 local util = require "luci.util"
7 local fs = require "nixio.fs"
8 local sys = require "luci.sys"
9 local http = require "luci.http"
10 local dispatcher = require "luci.dispatcher"
11 local i18n = require "luci.i18n"
12 local ltemplate = require "luci.template"
13 local ip = require "luci.ip"
14 local http = require "luci.http"
15 local sys = require "luci.sys"
16 local dispatcher = require "luci.dispatcher"
17 local uci = require "luci.model.uci".cursor()
18 local packageName = "advanced-reboot"
19 local devices_dir = "/usr/lib/lua/luci/" .. packageName .. "/devices/"
22 util.exec("logger -t " .. packageName .. " '" .. tostring(t) .. "'")
25 function is_alt_mountable(p1_mtd, p2_mtd)
26 if p1_mtd:sub(1,3) == "mtd" and
27 p2_mtd:sub(1,3) == "mtd" and
28 fs.access("/usr/sbin/ubiattach") and
29 fs.access("/usr/sbin/ubiblock") and
30 fs.access("/bin/mount") then
37 function get_partition_os_info(op_ubi)
38 local cp_info, op_info
39 if fs.access("/etc/os-release") then
40 cp_info = util.trim(util.exec('. /etc/os-release && echo "$PRETTY_NAME"'))
41 if cp_info:find("SNAPSHOT") then
42 cp_info = util.trim(util.exec('. /etc/os-release && echo "$OPENWRT_RELEASE"'))
45 logger(i18n.translate("attempting to mount alternative partition") .. " (mtd" .. tostring(op_ubi) .. ")")
46 alt_partition_unmount(op_ubi)
47 alt_partition_mount(op_ubi)
48 if fs.access("/alt/rom/etc/os-release") then
49 op_info = util.trim(util.exec('. /alt/rom/etc/os-release && echo "$PRETTY_NAME"'))
50 if op_info:find("SNAPSHOT") then
51 op_info = util.trim(util.exec('. /alt/rom/etc/os-release && echo "$OPENWRT_RELEASE"'))
54 logger(i18n.translate("attempting to unmount alternative partition") .. " (mtd" .. tostring(op_ubi) .. ")")
55 alt_partition_unmount(op_ubi)
56 return cp_info, op_info
59 function alt_partition_mount(op_ubi)
61 util.exec('for i in rom overlay firmware; do [ ! -d "$i" ] && mkdir -p "/alt/${i}"; done')
62 ubi_dev = tostring(util.exec("ubiattach -m " .. tostring(op_ubi)))
63 _, _, ubi_dev = ubi_dev:find("UBI device number (%d+)")
65 util.exec("ubidetach -m " .. tostring(op_ubi))
68 util.exec("ubiblock --create /dev/ubi" .. ubi_dev .. "_0")
69 util.exec("mount -t squashfs -o ro /dev/ubiblock" .. ubi_dev .. "_0 /alt/rom")
70 util.exec("mount -t ubifs /dev/ubi" .. ubi_dev .. "_1 /alt/overlay")
71 -- util.exec("mount -t overlay overlay -o noatime,lowerdir=/alt/rom,upperdir=/alt/overlay/upper,workdir=/alt/overlay/work /alt/firmware")
74 function alt_partition_unmount(op_ubi)
76 local mtdCount = tonumber(util.exec("ubinfo | grep 'Present UBI devices' | grep -c ','"))
77 mtdCount = mtdCount and mtdCount + 1 or 10
78 -- util.exec("[ -d /alt/firmware ] && umount /alt/firmware")
79 util.exec("[ -d /alt/overlay ] && umount /alt/overlay")
80 util.exec("[ -d /alt/rom ] && umount /alt/rom")
81 for i = 0, mtdCount do
82 if not fs.access("/sys/devices/virtual/ubi/ubi" .. tostring(i) .. "/mtd_num") then break end
83 ubi_mtd = tonumber(util.trim(util.exec("cat /sys/devices/virtual/ubi/ubi" .. i .. "/mtd_num")))
84 if ubi_mtd and ubi_mtd == op_ubi then
85 util.exec("ubiblock --remove /dev/ubi" .. tostring(i) .. "_0")
86 util.exec("ubidetach -m " .. tostring(op_ubi))
87 util.exec('rm -rf /alt')
92 function obtain_device_info()
93 local p, boardName, n, p1_label, p1_version, p2_label, p2_version, p1_os, p2_os
94 local errorMessage, current_partition
95 local op_ubi, cp_info, op_info, zyxelFlagPartition
96 local romBoardName = util.trim(util.exec("cat /tmp/sysinfo/board_name"))
97 for filename in fs.dir(devices_dir) do
98 local p_func = loadfile(devices_dir .. filename)
99 setfenv(p_func, { _ = i18n.translate })
101 boardName = p.boardName:gsub('%p','')
102 if romBoardName and romBoardName:gsub('%p',''):match(boardName) then
103 if p.labelOffset then
104 if p.partition1MTD then
105 p1_label = util.trim(util.exec("dd if=/dev/" .. p.partition1MTD .. " bs=1 skip=" .. p.labelOffset .. " count=128" .. " 2>/dev/null"))
106 n, p1_version = p1_label:match('(Linux)-([%d|.]+)')
108 if p.partition2MTD then
109 p2_label = util.trim(util.exec("dd if=/dev/" .. p.partition2MTD .. " bs=1 skip=" .. p.labelOffset .. " count=128" .. " 2>/dev/null"))
110 n, p2_version = p2_label:match('(Linux)-([%d|.]+)')
112 if p1_label and p1_label:find("LEDE") then p1_os = "LEDE" end
113 if p1_label and p1_label:find("OpenWrt") then p1_os = "OpenWrt" end
114 if p1_label and p.vendorName and p1_label:find(p.vendorName) then p1_os = p.vendorName end
115 if p2_label and p2_label:find("LEDE") then p2_os = "LEDE" end
116 if p2_label and p2_label:find("OpenWrt") then p2_os = "OpenWrt" end
117 if p2_label and p.vendorName and p2_label:find(p.vendorName) then p2_os = p.vendorName end
118 if not p1_os then p1_os = p.vendorName .. "/" .. i18n.translate("Unknown") end
119 if not p2_os then p2_os = p.vendorName .. "/" .. i18n.translate("Unknown") end
120 if p1_os and p1_version then p1_os = p1_os .. " (Linux " .. p1_version .. ")" end
121 if p2_os and p2_version then p2_os = p2_os .. " (Linux " .. p2_version .. ")" end
123 p1_os = p.vendorName .. "/" .. i18n.translate("Unknown") .. " (" .. i18n.translate("Compressed") .. ")"
124 p2_os = p.vendorName .. "/" .. i18n.translate("Unknown") .. " (" .. i18n.translate("Compressed") .. ")"
128 if fs.access("/usr/sbin/fw_printenv") and fs.access("/usr/sbin/fw_setenv") then
129 current_partition = tonumber(util.trim(util.exec("fw_printenv -n " .. p.bootEnv1)))
132 if not zyxelFlagPartition then zyxelFlagPartition = util.trim(util.exec(". /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
133 if zyxelFlagPartition then
134 current_partition = tonumber(util.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
136 errorMessage = errorMessage or i18n.translate("Unable to find Dual Boot Flag Partition.")
137 util.perror(i18n.translate("Unable to find Dual Boot Flag Partition."))
141 if is_alt_mountable(p.partition1MTD, p.partition2MTD) then
142 if current_partition == p.bootEnv1Partition1Value then
143 op_ubi = tonumber(p.partition2MTD:sub(4)) + 1
145 op_ubi = tonumber(p.partition1MTD:sub(4)) + 1
147 local cp_info, op_info = get_partition_os_info(op_ubi)
148 if current_partition == p.bootEnv1Partition1Value then
149 p1_os = cp_info or p1_os
150 p2_os = op_info or p2_os
152 p1_os = op_info or p1_os
153 p2_os = cp_info or p2_os
156 return romBoardName, p.vendorName .. " " .. p.deviceName, p.bootEnv1, p.bootEnv1Partition1Value, p1_os, p.bootEnv1Partition2Value, p2_os, p.bootEnv2, p.bootEnv2Partition1Value, p.bootEnv2Partition1Value, current_partition
159 return romBoardName, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
163 entry({"admin", "system", "advanced_reboot"}, call("action_template"), _("Advanced Reboot"), 90)
164 entry({"admin", "system", "advanced_reboot", "reboot"}, post("action_reboot"))
165 entry({"admin", "system", "advanced_reboot", "alternative_reboot"}, post("action_altreboot"))
166 entry({"admin", "system", "advanced_reboot", "power_off"}, post("action_poweroff"))
169 function action_template()
170 local romBoardName, device_name, bev1, bev1p1, p1_os, bev1p2, p2_os, bev2, bev2p1, bev2p2, current_partition = obtain_device_info()
171 ltemplate.render("advanced_reboot/advanced_reboot",{
172 romBoardName=romBoardName,
173 device_name=device_name,
178 current_partition=current_partition,
179 errorMessage = errorMessage})
182 function action_reboot()
183 ltemplate.render("advanced_reboot/applyreboot", {
184 title = i18n.translate("Rebooting..."),
185 msg = i18n.translate("The system is rebooting 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."),
186 addr = ip.new(util.imatch(uci:get("network", "lan", "ipaddr"))() or "192.168.1.1")
191 function action_altreboot()
192 local romBoardName, device_name, bev1, bev1p1, p1_os, bev1p2, p2_os, bev2, bev2p1, bev2p2, current_partition = obtain_device_info()
193 local zyxelFlagPartition, zyxelBootFlag, zyxelNewBootFlag, errorCode, curEnvSetting, newEnvSetting
196 if http.formvalue("cancel") then
197 http.redirect(dispatcher.build_url('admin/system/advanced_reboot'))
200 local step = tonumber(http.formvalue("step") or 1)
202 if fs.access("/usr/sbin/fw_printenv") and fs.access("/usr/sbin/fw_setenv") then
203 ltemplate.render("advanced_reboot/alternative_reboot",{})
205 ltemplate.render("advanced_reboot/advanced_reboot",{errorMessage = i18n.translate("No access to fw_printenv or fw_printenv!")})
207 elseif step == 2 then
208 if bev1 or bev2 then -- Linksys devices
210 curEnvSetting = tonumber(util.trim(util.exec("fw_printenv -n " .. bev1)))
211 if not curEnvSetting then
212 errorMessage = errorMessage .. i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev1 .. ". "
213 util.perror(i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev1 .. ".")
215 newEnvSetting = curEnvSetting == bev1p1 and bev1p2 or bev1p1
216 errorCode = sys.call("fw_setenv " .. bev1 .. " " .. newEnvSetting)
217 if errorCode ~= 0 then
218 errorMessage = errorMessage or "" .. i18n.translate("Unable to set firmware environment variable") .. ": " .. bev1 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ". "
219 util.perror(i18n.translate("Unable to set firmware environment variable") .. ": " .. bev1 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ".")
224 curEnvSetting = util.trim(util.exec("fw_printenv -n " .. bev2))
225 if not curEnvSetting then
226 errorMessage = errorMessage or "" .. i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev2 .. ". "
227 util.perror(i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev2 .. ".")
229 newEnvSetting = curEnvSetting == bev2p1 and bev2p2 or bev2p1
230 errorCode = sys.call("fw_setenv " .. bev2 .. " '" .. newEnvSetting .. "'")
231 if errorCode ~= 0 then
232 errorMessage = errorMessage or "" .. i18n.translate("Unable to set firmware environment variable") .. ": " .. bev2 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ". "
233 util.perror(i18n.translate("Unable to set firmware environment variable") .. ": " .. bev2 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ".")
237 else -- NetGear device
238 if not zyxelFlagPartition then zyxelFlagPartition = util.trim(util.exec(". /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
239 if not zyxelFlagPartition then
240 errorMessage = errorMessage .. i18n.translate("Unable to find Dual Boot Flag Partition." .. " ")
241 util.perror(i18n.translate("Unable to find Dual Boot Flag Partition."))
243 zyxelBootFlag = tonumber(util.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
244 zyxelNewBootFlag = zyxelBootFlag and zyxelBootFlag == 1 and "\\xff" or "\\x01"
245 if zyxelNewBootFlag then
246 errorCode = sys.call("printf \"" .. zyxelNewBootFlag .. "\" >" .. zyxelFlagPartition )
247 if errorCode ~= 0 then
248 errorMessage = errorMessage or "" .. i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ". "
249 util.perror(i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ".")
254 if not errorMessage then
255 ltemplate.render("advanced_reboot/applyreboot", {
256 title = i18n.translate("Rebooting..."),
257 msg = i18n.translate("The system is rebooting to an alternative partition 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."),
258 addr = ip.new(util.imatch(uci:get("network", "lan", "ipaddr"))() or "192.168.1.1")
262 ltemplate.render("advanced_reboot/advanced_reboot",{
263 romBoardName=romBoardName,
264 device_name=device_name,
269 current_partition=current_partition,
270 errorMessage = errorMessage})
275 function action_poweroff()
276 if http.formvalue("cancel") then
277 http.redirect(dispatcher.build_url('admin/system/advanced_reboot'))
280 local step = tonumber(http.formvalue("step") or 1)
282 if fs.access("/sbin/poweroff") then
283 ltemplate.render("advanced_reboot/power_off",{})
285 ltemplate.render("advanced_reboot/advanced_reboot",{})
287 elseif step == 2 then
288 ltemplate.render("advanced_reboot/applyreboot", {
289 title = i18n.translate("Shutting down..."),
290 msg = i18n.translate("The system is shutting down now.<br /> DO NOT POWER OFF THE DEVICE!<br /> It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),
291 addr = ip.new(util.imatch(uci:get("network", "lan", "ipaddr"))() or "192.168.1.1")
293 sys.call("/sbin/poweroff")