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