advanced_reboot.lua 7.5 KB

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