e1f6daac18c0ae6657fd7e38c8cbfbd5a8971375
[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 devices = {
7 -- deviceName, boardName, partition1, partition2, offset, envVar1, envVar1Value1, envVar1Value2, envVar2, envVar2Value1, envVar2Value2
8 {"Linksys EA3500", "linksys-audi", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
9 {"Linksys E4200v2/EA4500", "linksys-viper", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
10 {"Linksys EA6350v3", "linksys-ea6350v3", "mtd10", "mtd12", 192, "boot_part", 1, 2},
11 {"Linksys EA8300", "linksys-ea8300", "mtd10", "mtd12", 192, "boot_part", 1, 2},
12 {"Linksys EA8500", "ea8500", "mtd13", "mtd15", 32, "boot_part", 1, 2},
13 -- {"Linksys EA9500", "linksys-panamera", "mtd3", "mtd6", 28, "boot_part", 1, 2},
14 {"Linksys WRT1200AC", "linksys-caiman", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
15 {"Linksys WRT1900AC", "linksys-mamba", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
16 {"Linksys WRT1900ACv2", "linksys-cobra", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
17 {"Linksys WRT1900ACS", "linksys-shelby", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
18 {"Linksys WRT3200ACM", "linksys-rango", "mtd5", "mtd7", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
19 {"Linksys WRT32X", "linksys-venom", "mtd5", "mtd7", nil, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
20 {"ZyXEL NBG6817","nbg6817","mmcblk0p4","mmcblk0p7", 32, nil, 255, 1}
21 }
22
23 errorMessage = nil
24 rom_board_name = luci.util.trim(luci.sys.exec("cat /tmp/sysinfo/board_name"))
25 for i=1, #devices do
26 device_board_name = devices[i][2]:gsub('%p','')
27 if rom_board_name and rom_board_name:gsub('%p',''):match(device_board_name) then
28 device_name = devices[i][1]
29 partition_one_mtd = devices[i][3] or nil
30 partition_two_mtd = devices[i][4] or nil
31 partition_skip = devices[i][5] or nil
32 boot_envvar1 = devices[i][6] or nil
33 boot_envvar1_partition_one = tonumber(devices[i][7]) or nil
34 boot_envvar1_partition_two = tonumber(devices[i][8]) or nil
35 boot_envvar2 = devices[i][9] or nil
36 boot_envvar2_partition_one = devices[i][10] or nil
37 boot_envvar2_partition_two = devices[i][11] or nil
38 if partition_one_mtd and partition_skip then
39 partition_one_label = luci.util.trim(luci.sys.exec("dd if=/dev/" .. partition_one_mtd .. " bs=1 skip=" .. partition_skip .. " count=128" .. " 2>/dev/null"))
40 n, partition_one_version = string.match(partition_one_label, '(Linux)-([%d|.]+)')
41 end
42 if partition_two_mtd and partition_skip then
43 partition_two_label = luci.util.trim(luci.sys.exec("dd if=/dev/" .. partition_two_mtd .. " bs=1 skip=" .. partition_skip .. " count=128" .. " 2>/dev/null"))
44 n, partition_two_version = string.match(partition_two_label, '(Linux)-([%d|.]+)')
45 end
46 if partition_one_label and string.find(partition_one_label, "LEDE") then partition_one_os = "LEDE" end
47 if partition_one_label and string.find(partition_one_label, "OpenWrt") then partition_one_os = "OpenWrt" end
48 if partition_one_label and string.find(partition_one_label, "Linksys") then partition_one_os = "Linksys" end
49 if partition_two_label and string.find(partition_two_label, "LEDE") then partition_two_os = "LEDE" end
50 if partition_two_label and string.find(partition_two_label, "OpenWrt") then partition_two_os = "OpenWrt" end
51 if partition_two_label and string.find(partition_two_label, "Linksys") then partition_two_os = "Linksys" end
52 if device_name and device_name == "ZyXEL NBG6817" then
53 if not partition_one_os then partition_one_os = "ZyXEL" end
54 if not partition_two_os then partition_two_os = "ZyXEL" end
55 end
56 if device_name and device_name == "Linksys WRT32X" then
57 if not partition_one_os then partition_one_os = "Unknown/Compressed" end
58 if not partition_two_os then partition_two_os = "Unknown/Compressed" end
59 end
60 if not partition_one_os then partition_one_os = "Unknown" end
61 if not partition_two_os then partition_two_os = "Unknown" end
62 if partition_one_os and partition_one_version then partition_one_os = partition_one_os .. " (Linux " .. partition_one_version .. ")" end
63 if partition_two_os and partition_two_version then partition_two_os = partition_two_os .. " (Linux " .. partition_two_version .. ")" end
64
65 if device_name and device_name == "ZyXEL NBG6817" then
66 if not zyxelFlagPartition then zyxelFlagPartition = luci.util.trim(luci.sys.exec("source /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
67 if not zyxelFlagPartition then
68 errorMessage = errorMessage or "" .. luci.i18n.translate("Unable to find Dual Boot Flag Partition." .. " ")
69 luci.util.perror(luci.i18n.translate("Unable to find Dual Boot Flag Partition."))
70 else
71 current_partition = tonumber(luci.sys.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
72 end
73 else
74 if nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then
75 current_partition = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1)))
76 end
77 end
78 other_partition = current_partition == boot_envvar1_partition_two and boot_envvar1_partition_one or boot_envvar1_partition_two
79 end
80 end
81
82 function index()
83 entry({"admin", "system", "advanced_reboot"}, template("advanced_reboot/advanced_reboot"), _("Advanced Reboot"), 90)
84 entry({"admin", "system", "advanced_reboot", "reboot"}, post("action_reboot"))
85 entry({"admin", "system", "advanced_reboot", "alternative_reboot"}, post("action_altreboot"))
86 entry({"admin", "system", "advanced_reboot", "power_off"}, post("action_poweroff"))
87 end
88
89 function action_reboot()
90 local uci = require "luci.model.uci".cursor()
91 luci.template.render("admin_system/applyreboot", {
92 title = luci.i18n.translate("Rebooting..."),
93 msg = luci.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."),
94 addr = luci.ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1"
95 })
96 luci.sys.reboot()
97 end
98
99 function action_altreboot()
100 local uci = require "luci.model.uci".cursor()
101 local zyxelFlagPartition, zyxelBootFlag, zyxelNewBootFlag, errorCode, curEnvSetting, newEnvSetting
102 errorMessage = nil
103 errorCode = 0
104 if luci.http.formvalue("cancel") then
105 luci.http.redirect(luci.dispatcher.build_url('admin/system/advanced_reboot'))
106 return
107 end
108 local step = tonumber(luci.http.formvalue("step") or 1)
109 if step == 1 then
110 if device_name and nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then
111 luci.template.render("advanced_reboot/alternative_reboot",{})
112 else
113 luci.template.render("advanced_reboot/advanced_reboot",{errorMessage = luci.i18n.translate("No access to fw_printenv or fw_printenv!")})
114 end
115 elseif step == 2 then
116 if boot_envvar1 or boot_envvar2 then -- Linksys devices
117 if boot_envvar1 then
118 curEnvSetting = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1)))
119 if not curEnvSetting then
120 errorMessage = errorMessage .. luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar1 .. ". "
121 luci.util.perror(luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar1 .. ".")
122 else
123 newEnvSetting = curEnvSetting == boot_envvar1_partition_one and boot_envvar1_partition_two or boot_envvar1_partition_one
124 errorCode = luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar1 .. " " .. newEnvSetting)
125 if errorCode ~= 0 then
126 errorMessage = errorMessage or "" .. luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar1 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ". "
127 luci.util.perror(luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar1 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ".")
128 end
129 end
130 end
131 if boot_envvar2 then
132 curEnvSetting = luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar2))
133 if not curEnvSetting then
134 errorMessage = errorMessage or "" .. luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar2 .. ". "
135 luci.util.perror(luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar2 .. ".")
136 else
137 newEnvSetting = curEnvSetting == boot_envvar2_partition_one and boot_envvar2_partition_two or boot_envvar2_partition_one
138 errorCode = luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar2 .. " '" .. newEnvSetting .. "'")
139 if errorCode ~= 0 then
140 errorMessage = errorMessage or "" .. luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar2 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ". "
141 luci.util.perror(luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar2 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ".")
142 end
143 end
144 end
145 else -- NetGear device
146 if not zyxelFlagPartition then zyxelFlagPartition = luci.util.trim(luci.sys.exec("source /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
147 if not zyxelFlagPartition then
148 errorMessage = errorMessage .. luci.i18n.translate("Unable to find Dual Boot Flag Partition." .. " ")
149 luci.util.perror(luci.i18n.translate("Unable to find Dual Boot Flag Partition."))
150 else
151 zyxelBootFlag = tonumber(luci.sys.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
152 zyxelNewBootFlag = zyxelBootFlag and zyxelBootFlag == 1 and "\\xff" or "\\x01"
153 if zyxelNewBootFlag then
154 errorCode = luci.sys.call("printf \"" .. zyxelNewBootFlag .. "\" >" .. zyxelFlagPartition )
155 if errorCode ~= 0 then
156 errorMessage = errorMessage or "" .. luci.i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ". "
157 luci.util.perror(luci.i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ".")
158 end
159 end
160 end
161 end
162 if not errorMessage then
163 luci.template.render("admin_system/applyreboot", {
164 title = luci.i18n.translate("Rebooting..."),
165 msg = luci.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."),
166 addr = luci.ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1"
167 })
168 luci.sys.reboot()
169 else
170 luci.template.render("advanced_reboot/advanced_reboot",{
171 rom_board_name=rom_board_name,
172 device_name=device_name,
173 boot_envvar1_partition_one=boot_envvar1_partition_one,
174 partition_one_os=partition_one_os,
175 boot_envvar1_partition_two=boot_envvar1_partition_two,
176 partition_two_os=partition_two_os,
177 current_partition=current_partition,
178 errorMessage = errorMessage})
179 end
180 end
181 end
182
183 function action_poweroff()
184 local uci = require "luci.model.uci".cursor()
185 if luci.http.formvalue("cancel") then
186 luci.http.redirect(luci.dispatcher.build_url('admin/system/advanced_reboot'))
187 return
188 end
189 local step = tonumber(luci.http.formvalue("step") or 1)
190 if step == 1 then
191 if nixio.fs.access("/sbin/poweroff") then
192 luci.template.render("advanced_reboot/power_off",{})
193 else
194 luci.template.render("advanced_reboot/advanced_reboot",{})
195 end
196 elseif step == 2 then
197 luci.template.render("admin_system/applyreboot", {
198 title = luci.i18n.translate("Shutting down..."),
199 msg = luci.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."),
200 addr = luci.ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1"
201 })
202 luci.sys.call("/sbin/poweroff")
203 end
204 end