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