luci-app-advanced-reboot: bugfix: luci error on reboot, logger errors
[project/luci.git] / applications / luci-app-advanced-reboot / luasrc / controller / advanced_reboot.lua
1 -- Copyright 2017-2018 Stan Grishin <stangri@melmac.net>
2 -- Licensed to the public under the Apache License 2.0.
3
4 module("luci.controller.advanced_reboot", package.seeall)
5
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
19 function is_alt_mountable(p1_mtd, p2_mtd)
20 if p1_mtd:sub(1,3) == "mtd" and
21 p2_mtd:sub(1,3) == "mtd" and
22 fs.access("/usr/sbin/ubiattach") and
23 fs.access("/usr/sbin/ubiblock") and
24 fs.access("/bin/mount") then
25 return true
26 else
27 return false
28 end
29 end
30
31 function get_partition_os_info(op_ubi)
32 local cp_info, ap_info
33 if fs.access("/etc/os-release") then
34 cp_info = util.trim(util.exec('. /etc/os-release && echo "$PRETTY_NAME"'))
35 end
36 alt_partition_unmount(op_ubi)
37 alt_partition_mount(op_ubi)
38 if fs.access("/alt/rom/etc/os-release") then
39 ap_info = util.trim(util.exec('. /alt/rom/etc/os-release && echo "$PRETTY_NAME"'))
40 end
41 alt_partition_unmount(op_ubi)
42 return cp_info, ap_info
43 end
44
45 function alt_partition_mount(op_ubi)
46 local ubi_dev
47 util.exec('for i in rom overlay firmware; do [ ! -d "$i" ] && mkdir -p "/alt/${i}"; done')
48 ubi_dev = tostring(util.exec("ubiattach -m " .. tostring(op_ubi)))
49 _, _, ubi_dev = ubi_dev:find("UBI device number (%d+)")
50 if not ubi_dev then
51 util.exec("ubidetach -m " .. tostring(op_ubi))
52 return
53 end
54 util.exec("ubiblock --create /dev/ubi" .. ubi_dev .. "_0")
55 util.exec("mount -t squashfs -o ro /dev/ubiblock" .. ubi_dev .. "_0 /alt/rom")
56 util.exec("mount -t ubifs /dev/ubi" .. ubi_dev .. "_1 /alt/overlay")
57 -- util.exec("mount -t overlay overlay -o noatime,lowerdir=/alt/rom,upperdir=/alt/overlay/upper,workdir=/alt/overlay/work /alt/firmware")
58 end
59
60 function alt_partition_unmount(op_ubi)
61 -- util.exec("[ -d /alt/firmware ] && umount /alt/firmware")
62 util.exec("[ -d /alt/overlay ] && umount /alt/overlay")
63 util.exec("[ -d /alt/rom ] && umount /alt/rom")
64 for i = 0, 10 do
65 if not fs.access("/sys/devices/virtual/ubi/ubi" .. tostring(i) .. "/mtd_num") then break end
66 ubi_mtd = tonumber(util.trim(util.exec("cat /sys/devices/virtual/ubi/ubi" .. i .. "/mtd_num")))
67 if ubi_mtd and ubi_mtd == op_ubi then
68 util.exec("ubiblock --remove /dev/ubi" .. tostring(i) .. "_0")
69 util.exec("ubidetach -m " .. tostring(op_ubi))
70 util.exec('rm -rf /alt')
71 end
72 end
73 end
74
75 devices = {
76 -- deviceName, boardName, part1MTD, part2MTD, offset, envVar1, envVar1Value1, envVar1Value2, envVar2, envVar2Value1, envVar2Value2
77 {"Linksys EA3500", "linksys-audi", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
78 {"Linksys E4200v2/EA4500", "linksys-viper", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
79 {"Linksys EA6350v3", "linksys-ea6350v3", "mtd10", "mtd12", 192, "boot_part", 1, 2},
80 {"Linksys EA8300", "linksys-ea8300", "mtd10", "mtd12", 192, "boot_part", 1, 2},
81 {"Linksys EA8500", "ea8500", "mtd13", "mtd15", 32, "boot_part", 1, 2},
82 -- {"Linksys EA9500", "linksys-panamera", "mtd3", "mtd6", 28, "boot_part", 1, 2},
83 {"Linksys WRT1200AC", "linksys-caiman", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
84 {"Linksys WRT1900AC", "linksys-mamba", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
85 {"Linksys WRT1900ACv2", "linksys-cobra", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
86 {"Linksys WRT1900ACS", "linksys-shelby", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
87 {"Linksys WRT3200ACM", "linksys-rango", "mtd5", "mtd7", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
88 {"Linksys WRT32X", "linksys-venom", "mtd5", "mtd7", nil, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
89 {"ZyXEL NBG6817", "nbg6817", "mmcblk0p4", "mmcblk0p7", 32, nil, 255, 1}
90 }
91
92 -- local errorMessage, d
93 -- local device_name, p1_mtd, p2_mtd, offset, bev1, bev1p1, bev1p2, bev2, bev2p1, bev2p2
94 romBoardName = util.trim(util.exec("cat /tmp/sysinfo/board_name"))
95
96 for i=1, #devices do
97 d = devices[i][2]:gsub('%p','')
98 if romBoardName and romBoardName:gsub('%p',''):match(d) then
99 device_name = devices[i][1]
100 p1_mtd = devices[i][3] or nil
101 p2_mtd = devices[i][4] or nil
102 offset = devices[i][5] or nil
103 bev1 = devices[i][6] or nil
104 bev1p1 = tonumber(devices[i][7]) or nil
105 bev1p2 = tonumber(devices[i][8]) or nil
106 bev2 = devices[i][9] or nil
107 bev2p1 = devices[i][10] or nil
108 bev2p2 = devices[i][11] or nil
109 if p1_mtd and offset then
110 p1_label = util.trim(util.exec("dd if=/dev/" .. p1_mtd .. " bs=1 skip=" .. offset .. " count=128" .. " 2>/dev/null"))
111 n, p1_version = p1_label:match('(Linux)-([%d|.]+)')
112 end
113 if p2_mtd and offset then
114 p2_label = util.trim(util.exec("dd if=/dev/" .. p2_mtd .. " bs=1 skip=" .. offset .. " count=128" .. " 2>/dev/null"))
115 n, p2_version = p2_label:match('(Linux)-([%d|.]+)')
116 end
117 if p1_label and p1_label:find("LEDE") then p1_os = "LEDE" end
118 if p1_label and p1_label:find("OpenWrt") then p1_os = "OpenWrt" end
119 if p1_label and p1_label:find("Linksys") then p1_os = "Linksys" end
120 if p2_label and p2_label:find("LEDE") then p2_os = "LEDE" end
121 if p2_label and p2_label:find("OpenWrt") then p2_os = "OpenWrt" end
122 if p2_label and p2_label:find("Linksys") then p2_os = "Linksys" end
123 if device_name == "ZyXEL NBG6817" then
124 if not p1_os then p1_os = "ZyXEL" end
125 if not p2_os then p2_os = "ZyXEL" end
126 end
127 if device_name == "Linksys WRT32X" then
128 if not p1_os then p1_os = "Unknown/Compressed" end
129 if not p2_os then p2_os = "Unknown/Compressed" end
130 end
131 if not p1_os then p1_os = "Unknown" end
132 if not p2_os then p2_os = "Unknown" end
133 if p1_os and p1_version then p1_os = p1_os .. " (Linux " .. p1_version .. ")" end
134 if p2_os and p2_version then p2_os = p2_os .. " (Linux " .. p2_version .. ")" end
135
136 if device_name == "ZyXEL NBG6817" then
137 if not zyxelFlagPartition then zyxelFlagPartition = util.trim(util.exec(". /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
138 if not zyxelFlagPartition then
139 errorMessage = errorMessage or "" .. i18n.translate("Unable to find Dual Boot Flag Partition." .. " ")
140 util.perror(i18n.translate("Unable to find Dual Boot Flag Partition."))
141 else
142 current_partition = tonumber(util.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
143 end
144 else
145 if fs.access("/usr/sbin/fw_printenv") and fs.access("/usr/sbin/fw_setenv") then
146 current_partition = tonumber(util.trim(util.exec("fw_printenv -n " .. bev1)))
147 end
148 end
149 other_partition = current_partition == bev1p2 and bev1p1 or bev1p2
150
151 if is_alt_mountable(p1_mtd, p2_mtd) then
152 if current_partition == bev1p1 then
153 op_ubi = tonumber(p2_mtd:sub(4)) + 1
154 else
155 op_ubi = tonumber(p1_mtd:sub(4)) + 1
156 end
157 local cp_info, ap_info = get_partition_os_info(op_ubi)
158 if current_partition == bev1p1 then
159 p1_os = cp_info or p1_os
160 p2_os = ap_info or p2_os
161 else
162 p1_os = ap_info or p1_os
163 p2_os = cp_info or p2_os
164 end
165 end
166 end
167 end
168
169 function index()
170 entry({"admin", "system", "advanced_reboot"}, template("advanced_reboot/advanced_reboot"), _("Advanced Reboot"), 90)
171 entry({"admin", "system", "advanced_reboot", "reboot"}, post("action_reboot"))
172 entry({"admin", "system", "advanced_reboot", "alternative_reboot"}, post("action_altreboot"))
173 entry({"admin", "system", "advanced_reboot", "power_off"}, post("action_poweroff"))
174 end
175
176 function action_reboot()
177 ltemplate.render("advanced_reboot/applyreboot", {
178 title = i18n.translate("Rebooting..."),
179 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."),
180 addr = ip.new(type(ip) == "string" and ip or "192.168.1.1") or "192.168.1.1"
181 })
182 sys.reboot()
183 end
184
185 function action_altreboot()
186 local zyxelFlagPartition, zyxelBootFlag, zyxelNewBootFlag, errorCode, curEnvSetting, newEnvSetting
187 errorMessage = nil
188 errorCode = 0
189 if http.formvalue("cancel") then
190 http.redirect(dispatcher.build_url('admin/system/advanced_reboot'))
191 return
192 end
193 local step = tonumber(http.formvalue("step") or 1)
194 if step == 1 then
195 if fs.access("/usr/sbin/fw_printenv") and fs.access("/usr/sbin/fw_setenv") then
196 ltemplate.render("advanced_reboot/alternative_reboot",{})
197 else
198 ltemplate.render("advanced_reboot/advanced_reboot",{errorMessage = i18n.translate("No access to fw_printenv or fw_printenv!")})
199 end
200 elseif step == 2 then
201 if bev1 or bev2 then -- Linksys devices
202 if bev1 then
203 curEnvSetting = tonumber(util.trim(util.exec("fw_printenv -n " .. bev1)))
204 if not curEnvSetting then
205 errorMessage = errorMessage .. i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev1 .. ". "
206 util.perror(i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev1 .. ".")
207 else
208 newEnvSetting = curEnvSetting == bev1p1 and bev1p2 or bev1p1
209 errorCode = sys.call("fw_setenv " .. bev1 .. " " .. newEnvSetting)
210 if errorCode ~= 0 then
211 errorMessage = errorMessage or "" .. i18n.translate("Unable to set firmware environment variable") .. ": " .. bev1 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ". "
212 util.perror(i18n.translate("Unable to set firmware environment variable") .. ": " .. bev1 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ".")
213 end
214 end
215 end
216 if bev2 then
217 curEnvSetting = util.trim(util.exec("fw_printenv -n " .. bev2))
218 if not curEnvSetting then
219 errorMessage = errorMessage or "" .. i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev2 .. ". "
220 util.perror(i18n.translate("Unable to obtain firmware environment variable") .. ": " .. bev2 .. ".")
221 else
222 newEnvSetting = curEnvSetting == bev2p1 and bev2p2 or bev2p1
223 errorCode = sys.call("fw_setenv " .. bev2 .. " '" .. newEnvSetting .. "'")
224 if errorCode ~= 0 then
225 errorMessage = errorMessage or "" .. i18n.translate("Unable to set firmware environment variable") .. ": " .. bev2 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ". "
226 util.perror(i18n.translate("Unable to set firmware environment variable") .. ": " .. bev2 .. " " .. i18n.translate("to") .. " " .. newEnvSetting .. ".")
227 end
228 end
229 end
230 else -- NetGear device
231 if not zyxelFlagPartition then zyxelFlagPartition = util.trim(util.exec(". /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
232 if not zyxelFlagPartition then
233 errorMessage = errorMessage .. i18n.translate("Unable to find Dual Boot Flag Partition." .. " ")
234 util.perror(i18n.translate("Unable to find Dual Boot Flag Partition."))
235 else
236 zyxelBootFlag = tonumber(util.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
237 zyxelNewBootFlag = zyxelBootFlag and zyxelBootFlag == 1 and "\\xff" or "\\x01"
238 if zyxelNewBootFlag then
239 errorCode = sys.call("printf \"" .. zyxelNewBootFlag .. "\" >" .. zyxelFlagPartition )
240 if errorCode ~= 0 then
241 errorMessage = errorMessage or "" .. i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ". "
242 util.perror(i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ".")
243 end
244 end
245 end
246 end
247 if not errorMessage then
248 ltemplate.render("advanced_reboot/applyreboot", {
249 title = i18n.translate("Rebooting..."),
250 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."),
251 addr = ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1"
252 })
253 sys.reboot()
254 else
255 ltemplate.render("advanced_reboot/advanced_reboot",{
256 romBoardName=romBoardName,
257 device_name=device_name,
258 bev1p1=bev1p1,
259 p1_os=p1_os,
260 bev1p2=bev1p2,
261 p2_os=p2_os,
262 current_partition=current_partition,
263 errorMessage = errorMessage})
264 end
265 end
266 end
267
268 function action_poweroff()
269 local uci = require "luci.model.uci".cursor()
270 if http.formvalue("cancel") then
271 http.redirect(dispatcher.build_url('admin/system/advanced_reboot'))
272 return
273 end
274 local step = tonumber(http.formvalue("step") or 1)
275 if step == 1 then
276 if fs.access("/sbin/poweroff") then
277 ltemplate.render("advanced_reboot/power_off",{})
278 else
279 ltemplate.render("advanced_reboot/advanced_reboot",{})
280 end
281 elseif step == 2 then
282 ltemplate.render("advanced_reboot/applyreboot", {
283 title = i18n.translate("Shutting down..."),
284 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."),
285 addr = ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1"
286 })
287 sys.call("/sbin/poweroff")
288 end
289 end