luci-app-advanced-reboot: improve localization by moving from translate to translatef
[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 local util = require "luci.util"
7 local fs = require "nixio.fs"
8 local sys = require "luci.sys"
9 local http = require "luci.http"
10 local dispatcher = require "luci.dispatcher"
11 local i18n = require "luci.i18n"
12 local ltemplate = require "luci.template"
13 local ip = require "luci.ip"
14 local http = require "luci.http"
15 local sys = require "luci.sys"
16 local dispatcher = require "luci.dispatcher"
17 local uci = require "luci.model.uci".cursor()
18 local packageName = "advanced-reboot"
19 local devices_dir = "/usr/lib/lua/luci/" .. packageName .. "/devices/"
20
21 function logger(t)
22         util.exec("logger -t " .. packageName .. " '" .. tostring(t) .. "'")
23 end
24
25 function is_alt_mountable(p1_mtd, p2_mtd)
26         if p1_mtd:sub(1,3) == "mtd" and
27                                  p2_mtd:sub(1,3) == "mtd" and
28                                  fs.access("/usr/sbin/ubiattach") and
29                                  fs.access("/usr/sbin/ubiblock") and 
30                                  fs.access("/bin/mount") then
31                 return true
32         else
33                 return false
34         end
35 end
36
37 function get_partition_os_info(op_ubi)
38         local cp_info, op_info
39         if fs.access("/etc/os-release") then
40                 cp_info = util.trim(util.exec('. /etc/os-release && echo "$PRETTY_NAME"'))
41                 if cp_info:find("SNAPSHOT") then
42                         cp_info = util.trim(util.exec('. /etc/os-release && echo "$OPENWRT_RELEASE"'))
43                 end 
44         end
45         logger(i18n.translatef("attempting to mount alternative partition (mtd%s)", tostring(op_ubi))
46         alt_partition_unmount(op_ubi)
47         alt_partition_mount(op_ubi)
48         if fs.access("/alt/rom/etc/os-release") then
49                 op_info = util.trim(util.exec('. /alt/rom/etc/os-release && echo "$PRETTY_NAME"'))
50                 if op_info:find("SNAPSHOT") then
51                         op_info = util.trim(util.exec('. /alt/rom/etc/os-release && echo "$OPENWRT_RELEASE"'))
52                 end 
53         end
54         logger(i18n.translatef("attempting to unmount alternative partition (mtd%s)", tostring(op_ubi))
55         alt_partition_unmount(op_ubi)
56         return cp_info, op_info
57 end
58
59 function alt_partition_mount(op_ubi)
60         local ubi_dev
61         util.exec('for i in rom overlay firmware; do [ ! -d "$i" ] && mkdir -p "/alt/${i}"; done')
62         ubi_dev = tostring(util.exec("ubiattach -m " .. tostring(op_ubi)))
63         _, _, ubi_dev = ubi_dev:find("UBI device number (%d+)")
64         if not ubi_dev then 
65                 util.exec("ubidetach -m " .. tostring(op_ubi))
66                 return 
67         end
68         util.exec("ubiblock --create /dev/ubi" .. ubi_dev .. "_0")
69         util.exec("mount -t squashfs -o ro /dev/ubiblock" .. ubi_dev .. "_0 /alt/rom")
70         util.exec("mount -t ubifs /dev/ubi" .. ubi_dev .. "_1 /alt/overlay")
71 --      util.exec("mount -t overlay overlay -o noatime,lowerdir=/alt/rom,upperdir=/alt/overlay/upper,workdir=/alt/overlay/work /alt/firmware")
72 end
73
74 function alt_partition_unmount(op_ubi)
75         local i
76         local mtdCount = tonumber(util.exec("ubinfo | grep 'Present UBI devices' | grep -c ','"))
77         mtdCount = mtdCount and mtdCount + 1 or 10
78 --      util.exec("[ -d /alt/firmware ] && umount /alt/firmware")
79         util.exec("[ -d /alt/overlay ] && umount /alt/overlay")
80         util.exec("[ -d /alt/rom ] && umount /alt/rom")
81         for i = 0, mtdCount do
82                 if not fs.access("/sys/devices/virtual/ubi/ubi" .. tostring(i) .. "/mtd_num") then break end
83                 ubi_mtd =  tonumber(util.trim(util.exec("cat /sys/devices/virtual/ubi/ubi" .. i .. "/mtd_num")))
84                 if ubi_mtd and ubi_mtd == op_ubi then
85                         util.exec("ubiblock --remove /dev/ubi" .. tostring(i) .. "_0")
86                         util.exec("ubidetach -m " .. tostring(op_ubi))
87                         util.exec('rm -rf /alt')
88                 end
89         end
90 end
91
92 function obtain_device_info()
93         local p, boardName, n, p1_label, p1_version, p2_label, p2_version, p1_os, p2_os
94         local errorMessage, current_partition
95         local op_ubi, cp_info, op_info, zyxelFlagPartition
96         local romBoardName = util.trim(util.exec("cat /tmp/sysinfo/board_name"))
97         for filename in fs.dir(devices_dir) do
98                 local p_func = loadfile(devices_dir .. filename)
99                 setfenv(p_func, { _ = i18n.translate })
100                 p = p_func()
101                 boardName = p.boardName:gsub('%p','')
102                 if romBoardName and romBoardName:gsub('%p',''):match(boardName) then
103                         if p.labelOffset then
104                                 if p.partition1MTD then
105                                         p1_label = util.trim(util.exec("dd if=/dev/" .. p.partition1MTD .. " bs=1 skip=" .. p.labelOffset .. " count=128" .. "  2>/dev/null"))
106                                         n, p1_version = p1_label:match('(Linux)-([%d|.]+)')
107                                 end
108                                 if p.partition2MTD then
109                                         p2_label = util.trim(util.exec("dd if=/dev/" .. p.partition2MTD .. " bs=1 skip=" .. p.labelOffset .. " count=128" .. "  2>/dev/null"))
110                                         n, p2_version = p2_label:match('(Linux)-([%d|.]+)')
111                                 end
112                                 if p1_label and p1_label:find("LEDE") then p1_os = "LEDE" end
113                                 if p1_label and p1_label:find("OpenWrt") then p1_os = "OpenWrt" end
114                                 if p1_label and p.vendorName and p1_label:find(p.vendorName) then p1_os = p.vendorName end
115                                 if p2_label and p2_label:find("LEDE") then p2_os = "LEDE" end
116                                 if p2_label and p2_label:find("OpenWrt") then p2_os = "OpenWrt" end
117                                 if p2_label and p.vendorName and p2_label:find(p.vendorName) then p2_os = p.vendorName end
118                                 if not p1_os then p1_os = p.vendorName .. "/" .. i18n.translate("Unknown") end
119                                 if not p2_os then p2_os = p.vendorName .. "/" .. i18n.translate("Unknown") end
120                                 if p1_os and p1_version then p1_os = p1_os .. " (Linux " .. p1_version .. ")" end
121                                 if p2_os and p2_version then p2_os = p2_os .. " (Linux " .. p2_version .. ")" end
122                         else
123                                 p1_os = p.vendorName .. "/" .. i18n.translate("Unknown") .. " (" .. i18n.translate("Compressed") .. ")"
124                                 p2_os = p.vendorName .. "/" .. i18n.translate("Unknown") .. " (" .. i18n.translate("Compressed") .. ")"
125                         end
126
127                         if p.bootEnv1 then
128                                 if fs.access("/usr/sbin/fw_printenv") and fs.access("/usr/sbin/fw_setenv") then
129                                         current_partition = tonumber(util.trim(util.exec("fw_printenv -n " .. p.bootEnv1)))
130                                 end
131                         else
132                                 if not zyxelFlagPartition then zyxelFlagPartition = util.trim(util.exec(". /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
133                                 if zyxelFlagPartition then
134                                         current_partition = tonumber(util.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
135                                 else
136                                         errorMessage = errorMessage or i18n.translate("Unable to find Dual Boot Flag Partition.")
137                                         util.perror(i18n.translate("Unable to find Dual Boot Flag Partition."))
138                                 end
139                         end
140                         
141                         if is_alt_mountable(p.partition1MTD, p.partition2MTD) then
142                                 if current_partition == p.bootEnv1Partition1Value then
143                                         op_ubi = tonumber(p.partition2MTD:sub(4)) + 1
144                                 else
145                                         op_ubi = tonumber(p.partition1MTD:sub(4)) + 1
146                                 end
147                                 local cp_info, op_info = get_partition_os_info(op_ubi)
148                                 if current_partition == p.bootEnv1Partition1Value then
149                                         p1_os = cp_info or p1_os
150                                         p2_os = op_info or p2_os
151                                 else
152                                         p1_os = op_info or p1_os
153                                         p2_os = cp_info or p2_os
154                                 end
155                         end
156                         return romBoardName, p.vendorName .. " " .. p.deviceName, p.bootEnv1, p.bootEnv1Partition1Value, p1_os, p.bootEnv1Partition2Value, p2_os, p.bootEnv2, p.bootEnv2Partition1Value, p.bootEnv2Partition1Value, current_partition
157                 end
158         end
159         return romBoardName, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
160 end
161
162 function index()
163         entry({"admin", "system", "advanced_reboot"}, call("action_template"), _("Advanced Reboot"), 90)
164         entry({"admin", "system", "advanced_reboot", "reboot"}, post("action_reboot"))
165         entry({"admin", "system", "advanced_reboot", "alternative_reboot"}, post("action_altreboot"))
166         entry({"admin", "system", "advanced_reboot", "power_off"}, post("action_poweroff"))
167 end
168
169 function action_template()
170         local romBoardName, device_name, bev1, bev1p1, p1_os, bev1p2, p2_os, bev2, bev2p1, bev2p2, current_partition = obtain_device_info()
171         ltemplate.render("advanced_reboot/advanced_reboot",{
172                                 romBoardName=romBoardName,
173                                 device_name=device_name,
174                                 bev1p1=bev1p1,
175                                 p1_os=p1_os,
176                                 bev1p2=bev1p2,
177                                 p2_os=p2_os,
178                                 current_partition=current_partition,
179                                 errorMessage=errorMessage})
180 end
181
182 function action_reboot()
183         ltemplate.render("advanced_reboot/applyreboot", {
184                                 title = i18n.translate("Rebooting..."),
185                                 msg   = 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."),
186                                 addr  = ip.new(util.imatch(uci:get("network", "lan", "ipaddr"))() or "192.168.1.1")
187                         })
188         sys.reboot()
189 end
190
191 function action_altreboot()
192         local romBoardName, device_name, bev1, bev1p1, p1_os, bev1p2, p2_os, bev2, bev2p1, bev2p2, current_partition = obtain_device_info()
193         local zyxelFlagPartition, zyxelBootFlag, zyxelNewBootFlag, errorCode, curEnvSetting, newEnvSetting
194         errorMessage = nil
195         errorCode = 0
196         if http.formvalue("cancel") then
197                 http.redirect(dispatcher.build_url('admin/system/advanced_reboot'))
198                 return
199         end
200         local step = tonumber(http.formvalue("step") or 1)
201         if step == 1 then
202                 if fs.access("/usr/sbin/fw_printenv") and fs.access("/usr/sbin/fw_setenv") then
203                         ltemplate.render("advanced_reboot/alternative_reboot",{})
204                 else
205                         ltemplate.render("advanced_reboot/advanced_reboot",{errorMessage = i18n.translate("No access to fw_printenv or fw_printenv!")})
206                 end
207         elseif step == 2 then
208                 if bev1 or bev2 then -- Linksys devices
209                         if bev1 then
210                                 curEnvSetting = tonumber(util.trim(util.exec("fw_printenv -n " .. bev1)))
211                                 if not curEnvSetting then
212                                         errorMessage = errorMessage .. i18n.translatef("Unable to obtain firmware environment variable: %s.", bev1)
213                                         util.perror(i18n.translatef("Unable to obtain firmware environment variable: %s.", bev1))
214                                 else
215                                         newEnvSetting = curEnvSetting == bev1p1 and bev1p2 or bev1p1
216                                         errorCode = sys.call("fw_setenv " .. bev1 .. " " .. newEnvSetting)
217                                                 if errorCode ~= 0 then
218                                                         errorMessage = errorMessage or "" .. i18n.translatef("Unable to set firmware environment variable: %s to %s.", bev1, newEnvSetting)
219                                                         util.perror(i18n.translatef("Unable to set firmware environment variable: %s to %s.", bev1, newEnvSetting))
220                                                 end
221                                 end
222                         end
223                         if bev2 then
224                                 curEnvSetting = util.trim(util.exec("fw_printenv -n " .. bev2))
225                                 if not curEnvSetting then
226                                         errorMessage = errorMessage or "" .. i18n.translatef("Unable to obtain firmware environment variable: %s.", bev2)
227                                         util.perror(i18n.translatef("Unable to obtain firmware environment variable: %s.", bev2))
228                                 else
229                                         newEnvSetting = curEnvSetting == bev2p1 and bev2p2 or bev2p1
230                                         errorCode = sys.call("fw_setenv " .. bev2 .. " '" .. newEnvSetting .. "'")
231                                         if errorCode ~= 0 then
232                                                 errorMessage = errorMessage or "" .. i18n.translatef("Unable to set firmware environment variable: %s to %s.", bev2, newEnvSetting)
233                                                 util.perror(i18n.translatef("Unable to set firmware environment variable: %s to %s.", bev2, newEnvSetting))
234                                         end
235                                 end
236                         end
237                 else -- NetGear device
238                         if not zyxelFlagPartition then zyxelFlagPartition = util.trim(util.exec(". /lib/functions.sh; find_mtd_part 0:DUAL_FLAG")) end
239                         if not zyxelFlagPartition then
240                                 errorMessage = errorMessage .. i18n.translate("Unable to find Dual Boot Flag Partition." .. " ")
241                                 util.perror(i18n.translate("Unable to find Dual Boot Flag Partition."))
242                         else
243                                 zyxelBootFlag = tonumber(util.exec("dd if=" .. zyxelFlagPartition .. " bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 \"%d\"'"))
244                                 zyxelNewBootFlag = zyxelBootFlag and zyxelBootFlag == 1 and "\\xff" or "\\x01"
245                                 if zyxelNewBootFlag then
246                                         errorCode = sys.call("printf \"" .. zyxelNewBootFlag .. "\" >" .. zyxelFlagPartition )
247                                         if errorCode ~= 0 then
248                                                 errorMessage = errorMessage or "" .. i18n.translatef("Unable to set Dual Boot Flag Partition entry for partition: %s.", zyxelFlagPartition)
249                                                 util.perror(i18n.translatef("Unable to set Dual Boot Flag Partition entry for partition: %s.", zyxelFlagPartition))
250                                         end
251                                 end
252                         end
253                 end
254                 if not errorMessage then
255                         ltemplate.render("advanced_reboot/applyreboot", {
256                                                 title = i18n.translate("Rebooting..."),
257                                                 msg   = 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."),
258                                                 addr  = ip.new(util.imatch(uci:get("network", "lan", "ipaddr"))() or "192.168.1.1")
259                                         })
260                         sys.reboot()
261                 else
262                         ltemplate.render("advanced_reboot/advanced_reboot",{
263                                 romBoardName=romBoardName,
264                                 device_name=device_name,
265                                 bev1p1=bev1p1,
266                                 p1_os=p1_os,
267                                 bev1p2=bev1p2,
268                                 p2_os=p2_os,
269                                 current_partition=current_partition,
270                                 errorMessage = errorMessage})
271                 end
272         end
273 end
274
275 function action_poweroff()
276         if http.formvalue("cancel") then
277                 http.redirect(dispatcher.build_url('admin/system/advanced_reboot'))
278                 return
279         end
280         local step = tonumber(http.formvalue("step") or 1)
281         if step == 1 then
282                 if fs.access("/sbin/poweroff") then
283                         ltemplate.render("advanced_reboot/power_off",{})
284                 else
285                         ltemplate.render("advanced_reboot/advanced_reboot",{})
286                 end
287         elseif step == 2 then
288                 ltemplate.render("advanced_reboot/applyreboot", {
289                                         title = i18n.translate("Shutting down..."),
290                                         msg   = 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."),
291                                         addr  = ip.new(util.imatch(uci:get("network", "lan", "ipaddr"))() or "192.168.1.1")
292                                 })
293                 sys.call("/sbin/poweroff")
294         end
295 end