X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=blobdiff_plain;f=applications%2Fluci-app-advanced-reboot%2Fluasrc%2Fcontroller%2Fadvanced_reboot.lua;h=b5dd4fe0f4f49f08dac1c0f463c680d9ab1fe63c;hp=2b55217b8eac776f6eb50bf0017e7d2261f30061;hb=dcfc4569c483f4218cf41b26ef0f4dfed0441c55;hpb=f6505fc01c8aee4d2f9f7bc7537c3a93cfd5dab1 diff --git a/applications/luci-app-advanced-reboot/luasrc/controller/advanced_reboot.lua b/applications/luci-app-advanced-reboot/luasrc/controller/advanced_reboot.lua index 2b55217b8..b5dd4fe0f 100644 --- a/applications/luci-app-advanced-reboot/luasrc/controller/advanced_reboot.lua +++ b/applications/luci-app-advanced-reboot/luasrc/controller/advanced_reboot.lua @@ -3,20 +3,25 @@ module("luci.controller.advanced_reboot", package.seeall) --- device, board_name, part1, part2, offset, env_var_1, value_1_1, value_1_2, env_var_2, value_2_1, value_2_2 +-- 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 devices = { - {"Linksys WRT1200AC", "armada-385-linksys-caiman", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, - {"Linksys WRT1900AC", "armada-xp-linksys-mamba", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, - {"Linksys WRT1900ACv2", "armada-385-linksys-cobra", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, - {"Linksys WRT1900ACS", "armada-385-linksys-shelby", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, - {"Linksys WRT3200ACM", "armada-385-linksys-rango", "mtd5", "mtd7", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, + {"Linksys EA3500", "linksys-audi", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, {"Linksys E4200v2/EA4500", "linksys-viper", "mtd3", "mtd5", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, - {"Linksys EA8500", "ea8500", "mtd13", "mtd15", 32, "boot_part", 1, 2} + {"Linksys EA8500", "ea8500", "mtd13", "mtd15", 32, "boot_part", 1, 2}, +-- {"Linksys EA9500", "linksys,panamera", "mtd3", "mtd6", 28, "boot_part", 1, 2}, + {"Linksys WRT1200AC", "linksys-caiman", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, + {"Linksys WRT1900AC", "linksys-mamba", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, + {"Linksys WRT1900ACv2", "linksys-cobra", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, + {"Linksys WRT1900ACS", "linksys-shelby", "mtd4", "mtd6", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, + {"Linksys WRT3200ACM", "linksys-rango", "mtd5", "mtd7", 32, "boot_part", 1, 2, "bootcmd", "run nandboot", "run altnandboot"}, + {"ZyXEL NBG6817","nbg6817","mmcblk0p4","mmcblk0p7",32,nil,255,1} } -board_name = luci.util.trim(luci.sys.exec("cat /tmp/sysinfo/board_name")) +errorMessage = "" +device_board_name = luci.util.trim(luci.sys.exec("cat /tmp/sysinfo/board_name")) for i=1, #devices do - if board_name and devices[i][2] == board_name then + table_board_name = devices[i][2]:gsub('%p','') + if device_board_name and device_board_name:gsub('%p',''):match(table_board_name) then device_name = devices[i][1] partition_one_mtd = devices[i][3] or nil partition_two_mtd = devices[i][4] or nil @@ -41,14 +46,29 @@ for i=1, #devices do if string.find(partition_two_label, "LEDE") then partition_two_os = "LEDE" end if string.find(partition_two_label, "OpenWrt") then partition_two_os = "OpenWrt" end if string.find(partition_two_label, "Linksys") then partition_two_os = "Linksys" end + if device_name and device_name == "ZyXEL NBG6817" then + if not partition_one_os then partition_one_os = "ZyXEL" end + if not partition_two_os then partition_two_os = "ZyXEL" end + end if not partition_one_os then partition_one_os = "Unknown" end if not partition_two_os then partition_two_os = "Unknown" end if partition_one_os and partition_one_version then partition_one_os = partition_one_os .. " (Linux " .. partition_one_version .. ")" end if partition_two_os and partition_two_version then partition_two_os = partition_two_os .. " (Linux " .. partition_two_version .. ")" end - if nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then - current_partition = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1))) - other_partition = current_partition == boot_envvar1_partition_one and boot_envvar1_partition_two or boot_envvar1_partition_one + + if device_name and device_name == "ZyXEL NBG6817" then + if not zyxelFlagPartition then zyxelFlagPartition = luci.util.trim(luci.sys.exec("source /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end + if not zyxelFlagPartition then + errorMessage = errorMessage .. luci.i18n.translate("Unable to find Dual Boot Flag Partition." .. " ") + luci.util.perror(luci.i18n.translate("Unable to find Dual Boot Flag Partition.")) + else + current_partition = tonumber(luci.sys.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'")) + end + else + if nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then + current_partition = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1))) + end end + other_partition = current_partition == boot_envvar1_partition_two and boot_envvar1_partition_one or boot_envvar1_partition_two end end @@ -61,17 +81,30 @@ function index() end function action_reboot() + local uci = require "luci.model.uci".cursor() luci.template.render("admin_system/applyreboot", { title = luci.i18n.translate("Rebooting..."), msg = luci.i18n.translate("The system is rebooting now.
DO NOT POWER OFF THE DEVICE!
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."), - addr = luci.ip.new(uci.cursor():get("network", "lan", "ipaddr")) or "192.168.1.1" + addr = luci.ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1" }) luci.sys.reboot() end function action_altreboot() + local uci = require "luci.model.uci".cursor() + local zyxelFlagPartition, zyxelBootFlag, zyxelNewBootFlag, errorCode, curEnvSetting, newEnvSetting + errorMessage = "" + errorCode = 0 if luci.http.formvalue("cancel") then luci.http.redirect(luci.dispatcher.build_url('admin/system/advanced_reboot')) +-- luci.template.render("advanced_reboot/advanced_reboot",{ +-- device_name=device_name, +-- boot_envvar1_partition_one=boot_envvar1_partition_one, +-- partition_one_os=partition_one_os, +-- boot_envvar1_partition_two=boot_envvar1_partition_two, +-- partition_two_os=partition_two_os, +-- current_partition=current_partition, +-- errorMessage = luci.i18n.translate("Alternative reboot cancelled.")}) return end local step = tonumber(luci.http.formvalue("step") or 1) @@ -79,25 +112,77 @@ function action_altreboot() if device_name and nixio.fs.access("/usr/sbin/fw_printenv") and nixio.fs.access("/usr/sbin/fw_setenv") then luci.template.render("advanced_reboot/alternative_reboot",{}) else - luci.template.render("advanced_reboot/advanced_reboot",{}) + luci.template.render("advanced_reboot/advanced_reboot",{errorMessage = luci.i18n.translate("No access to fw_printenv or fw_printenv!")}) end elseif step == 2 then - luci.template.render("admin_system/applyreboot", { - title = luci.i18n.translate("Rebooting..."), - msg = luci.i18n.translate("The system is rebooting to an alternative partition now.
DO NOT POWER OFF THE DEVICE!
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."), - addr = luci.ip.new(uci.cursor():get("network", "lan", "ipaddr")) or "192.168.1.1" - }) - if boot_envvar1 then env1 = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1))) end - if boot_envvar2 then env2 = luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar2)) end - if env1 and env1 == boot_envvar1_partition_one then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar1 .. " " .. boot_envvar1_partition_two) end - if env1 and env1 == boot_envvar1_partition_two then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar1 .. " " .. boot_envvar1_partition_one) end - if env2 and env2 == boot_envvar2_partition_one then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar2 .. " '" .. boot_envvar2_partition_two .. "'") end - if env2 and env2 == boot_envvar2_partition_two then luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar2 .. " '" .. boot_envvar2_partition_one .. "'") end - luci.sys.reboot() + if boot_envvar1 or boot_envvar2 then -- Linksys devices + if boot_envvar1 then + curEnvSetting = tonumber(luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar1))) + if not curEnvSetting then + errorMessage = errorMessage .. luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar1 .. ". " + luci.util.perror(luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar1 .. ".") + else + newEnvSetting = curEnvSetting == boot_envvar1_partition_one and boot_envvar1_partition_two or boot_envvar1_partition_one + errorCode = luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar1 .. " " .. newEnvSetting) + if errorCode ~= 0 then + errorMessage = errorMessage .. luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar1 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ". " + luci.util.perror(luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar1 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ".") + end + end + end + if boot_envvar2 then + curEnvSetting = luci.util.trim(luci.sys.exec("/usr/sbin/fw_printenv -n " .. boot_envvar2)) + if not curEnvSetting then + errorMessage = errorMessage .. luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar2 .. ". " + luci.util.perror(luci.i18n.translate("Unable to obtain firmware environment variable") .. ": " .. boot_envvar2 .. ".") + else + newEnvSetting = curEnvSetting == boot_envvar2_partition_one and boot_envvar2_partition_two or boot_envvar2_partition_one + errorCode = luci.sys.call("/usr/sbin/fw_setenv " .. boot_envvar2 .. " '" .. newEnvSetting .. "'") + if errorCode ~= 0 then + errorMessage = errorMessage .. luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar2 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ". " + luci.util.perror(luci.i18n.translate("Unable to set firmware environment variable") .. ": " .. boot_envvar2 .. " " .. luci.i18n.translate("to") .. " " .. newEnvSetting .. ".") + end + end + end + else -- NetGear device + if not zyxelFlagPartition then zyxelFlagPartition = luci.util.trim(luci.sys.exec("source /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end + if not zyxelFlagPartition then + errorMessage = errorMessage .. luci.i18n.translate("Unable to find Dual Boot Flag Partition." .. " ") + luci.util.perror(luci.i18n.translate("Unable to find Dual Boot Flag Partition.")) + else + zyxelBootFlag = tonumber(luci.sys.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'")) + zyxelNewBootFlag = zyxelBootFlag and zyxelBootFlag == 1 and "\\xff" or "\\x01" + if zyxelNewBootFlag then + errorCode = luci.sys.call("printf \"" .. zyxelNewBootFlag .. "\" >" .. zyxelFlagPartition ) + if errorCode ~= 0 then + errorMessage = errorMessage .. luci.i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ". " + luci.util.perror(luci.i18n.translate("Unable to set Dual Boot Flag Partition entry for partition") .. ": " .. zyxelFlagPartition .. ".") + end + end + end + end + if errorMessage == "" then + luci.template.render("admin_system/applyreboot", { + title = luci.i18n.translate("Rebooting..."), + msg = luci.i18n.translate("The system is rebooting to an alternative partition now.
DO NOT POWER OFF THE DEVICE!
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."), + addr = luci.ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1" + }) + luci.sys.reboot() + else + luci.template.render("advanced_reboot/advanced_reboot",{ + device_name=device_name, + boot_envvar1_partition_one=boot_envvar1_partition_one, + partition_one_os=partition_one_os, + boot_envvar1_partition_two=boot_envvar1_partition_two, + partition_two_os=partition_two_os, + current_partition=current_partition, + errorMessage = errorMessage}) + end end end function action_poweroff() + local uci = require "luci.model.uci".cursor() if luci.http.formvalue("cancel") then luci.http.redirect(luci.dispatcher.build_url('admin/system/advanced_reboot')) return @@ -113,7 +198,7 @@ function action_poweroff() luci.template.render("admin_system/applyreboot", { title = luci.i18n.translate("Shutting down..."), msg = luci.i18n.translate("The system is shutting down now.
DO NOT POWER OFF THE DEVICE!
It might be necessary to renew the address of your computer to reach the device again, depending on your settings."), - addr = luci.ip.new(uci.cursor():get("network", "lan", "ipaddr")) or "192.168.1.1" + addr = luci.ip.new(uci:get("network", "lan", "ipaddr")) or "192.168.1.1" }) luci.sys.call("/sbin/poweroff") end