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