Merge pull request #1221 from Cye3s/master
[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 WRT1200AC", "armada-385-linksys-caiman", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
9   {"Linksys WRT1900AC", "armada-xp-linksys-mamba", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
10   {"Linksys WRT1900ACv2", "armada-385-linksys-cobra", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
11   {"Linksys WRT1900ACS", "armada-385-linksys-shelby", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
12   {"Linksys WRT3200ACM", "armada-385-linksys-rango", "mtd5", "mtd7", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
13   {"Linksys E4200v2/EA4500", "linksys-viper", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"},
14   {"Linksys EA8500", "ea8500", "mtd13", "mtd15", 32, "boot_part", 1, 2}
15 }
16
17 board_name = luci.util.trim(luci.sys.exec("cat /tmp/sysinfo/board_name"))
18 for i=1, #devices do
19   if board_name and devices[i][2] == board_name then
20     device_name = devices[i][1]
21     partition_one_mtd = devices[i][3] or nil
22     partition_two_mtd = devices[i][4] or nil
23     partition_skip = devices[i][5] or nil
24     boot_envvar1 = devices[i][6] or nil
25     boot_envvar1_partition_one = tonumber(devices[i][7]) or nil
26     boot_envvar1_partition_two = tonumber(devices[i][8]) or nil
27     boot_envvar2 = devices[i][9] or nil
28     boot_envvar2_partition_one = devices[i][10] or nil
29     boot_envvar2_partition_two = devices[i][11] or nil
30     if partition_one_mtd and partition_skip then
31       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"))
32       n, partition_one_version = string.match(partition_one_label, '(Linux)-([%d|.]+)')
33     end
34     if partition_two_mtd and partition_skip then
35       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"))
36       n, partition_two_version = string.match(partition_two_label, '(Linux)-([%d|.]+)')
37     end
38     if string.find(partition_one_label, "LEDE") then partition_one_os = "LEDE" end
39     if string.find(partition_one_label, "OpenWrt") then partition_one_os = "OpenWrt" end
40     if string.find(partition_one_label, "Linksys") then partition_one_os = "Linksys" end
41     if string.find(partition_two_label, "LEDE") then partition_two_os = "LEDE" end
42     if string.find(partition_two_label, "OpenWrt") then partition_two_os = "OpenWrt" end
43     if string.find(partition_two_label, "Linksys") then partition_two_os = "Linksys" end
44     if not partition_one_os then partition_one_os = "Unknown" end
45     if not partition_two_os then partition_two_os = "Unknown" end
46     if partition_one_os and partition_one_version then partition_one_os = partition_one_os .. " (Linux " .. partition_one_version .. ")" end
47     if partition_two_os and partition_two_version then partition_two_os = partition_two_os .. " (Linux " .. partition_two_version .. ")" end
48     if nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then
49       current_partition = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1)))
50       other_partition = current_partition == boot_envvar1_partition_one and boot_envvar1_partition_two or boot_envvar1_partition_one
51     end
52   end
53 end
54
55 function index()
56   entry({"admin", "system", "advanced_reboot"}, template("advanced_reboot/advanced_reboot"), _("Advanced Reboot"), 90)
57   entry({"admin", "system", "advanced_reboot", "reboot"}, post("action_reboot"))
58 --  if device_name then entry({"admin", "system", "advanced_reboot", "altreboot"}, post("action_altreboot")) end
59   entry({"admin", "system", "advanced_reboot", "alternative_reboot"}, post("action_altreboot"))
60   entry({"admin", "system", "advanced_reboot", "power_off"}, post("action_poweroff"))
61 end
62
63 function action_reboot()
64   luci.template.render("admin_system/applyreboot", {
65         title = luci.i18n.translate("Rebooting..."),
66         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."),
67         addr  = luci.ip.new(uci.cursor():get("network", "lan", "ipaddr")) or "192.168.1.1"
68       })
69   luci.sys.reboot()
70 end
71
72 function action_altreboot()
73   if luci.http.formvalue("cancel") then
74     luci.http.redirect(luci.dispatcher.build_url('admin/system/advanced_reboot'))
75     return
76   end
77   local step = tonumber(luci.http.formvalue("step") or 1)
78   if step == 1 then
79     if device_name and nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then
80       luci.template.render("advanced_reboot/alternative_reboot",{})
81     else
82       luci.template.render("advanced_reboot/advanced_reboot",{})
83     end
84   elseif step == 2 then
85     luci.template.render("admin_system/applyreboot", {
86           title = luci.i18n.translate("Rebooting..."),
87           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."),
88           addr  = luci.ip.new(uci.cursor():get("network", "lan", "ipaddr")) or "192.168.1.1"
89         })
90     if boot_envvar1 then env1 = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1))) end
91     if boot_envvar2 then env2 = luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar2)) end
92     if env1 and env1 == boot_envvar1_partition_one then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar1 .. " " .. boot_envvar1_partition_two) end
93     if env1 and env1 == boot_envvar1_partition_two then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar1 .. " " .. boot_envvar1_partition_one) end
94     if env2 and env2 == boot_envvar2_partition_one then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar2 .. " '" .. boot_envvar2_partition_two .. "'") end
95     if env2 and env2 == boot_envvar2_partition_two then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar2 .. " '" .. boot_envvar2_partition_one .. "'") end
96     luci.sys.reboot()
97   end
98 end
99
100 function action_poweroff()
101   if luci.http.formvalue("cancel") then
102     luci.http.redirect(luci.dispatcher.build_url('admin/system/advanced_reboot'))
103     return
104   end
105   local step = tonumber(luci.http.formvalue("step") or 1)
106   if step == 1 then
107     if nixio.fs.access("/sbin/poweroff") then
108       luci.template.render("advanced_reboot/power_off",{})
109     else
110       luci.template.render("advanced_reboot/advanced_reboot",{})
111     end
112   elseif step == 2 then
113     luci.template.render("admin_system/applyreboot", {
114           title = luci.i18n.translate("Shutting down..."),
115           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."),
116           addr  = luci.ip.new(uci.cursor():get("network", "lan", "ipaddr")) or "192.168.1.1"
117         })
118     luci.sys.call("/sbin/poweroff")
119   end
120 end