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