blob: c79ed7747401443cfaaed9518de3768e50bd7749 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
#!/bin/sh
# Copyright 2017-2020 Stan Grishin (stangri@melmac.ca)
# shellcheck disable=SC2039,SC1091,SC3043,SC3057,SC3060
readonly devices_dir="/usr/share/advanced-reboot/devices/"
. /lib/functions.sh
. /usr/share/libubox/jshn.sh
logger() { /usr/bin/logger -t advanced-reboot "$1"; }
is_present() { command -v "$1" >/dev/null 2>&1; }
is_alt_mountable() {
local p1_mtd="$1" p2_mtd="$2"
if [ "${p1_mtd:0:3}" = "mtd" ] && [ "${p2_mtd:0:3}" = "mtd" ] && \
is_present 'ubiattach' && \
is_present 'ubiblock' && \
is_present 'mount'; then
return 0
else
return 1
fi
}
alt_partition_mount() {
local ubi_dev op_ubi="$1" ubi_vol="${2:-0}"
mkdir -p /var/alt_rom
ubi_dev="$(ubiattach -m "$op_ubi")"
ubi_dev="$(echo "$ubi_dev" | sed -n "s/^UBI device number\s*\(\d*\),.*$/\1/p")"
if [ -z "$ubi_dev" ]; then
ubidetach -m "$op_ubi"
return 1
fi
ubiblock --create "/dev/ubi${ubi_dev}_${ubi_vol}" && \
mount -t squashfs -r "/dev/ubiblock${ubi_dev}_${ubi_vol}" /var/alt_rom
}
alt_partition_unmount() {
local mtdCount i=0 op_ubi="$1" ubi_vol="${2:-0}"
mtdCount="$(ubinfo | grep 'Present UBI devices' | tr ',' '\n' | grep -c 'ubi')"
[ -z "$mtdCount" ] && mtdCount=10
[ -d /var/alt_rom ] && umount /var/alt_rom
while [ "$i" -le "$mtdCount" ]; do
if [ ! -e "/sys/devices/virtual/ubi/ubi${i}/mtd_num" ]; then
break
fi
ubi_mtd="$(cat /sys/devices/virtual/ubi/ubi${i}/mtd_num)"
if [ -n "$ubi_mtd" ] && [ "$ubi_mtd" = "$op_ubi" ]; then
ubiblock --remove /dev/ubi${i}_${ubi_vol}
ubidetach -m "$op_ubi"
rm -rf /var/alt_rom
fi
i=$((i + 1))
done
}
get_main_partition_os_info(){
local cp_info
if [ -s "/etc/os-release" ]; then
cp_info="$(. /etc/os-release && echo "$PRETTY_NAME")"
if [ "${cp_info//SNAPSHOT}" != "$cp_info" ]; then
cp_info="$(. /etc/os-release && echo "${OPENWRT_RELEASE%%-*}")"
fi
fi
echo "$cp_info"
}
get_alt_partition_os_info(){
local op_info op_ubi="$1" vendor_name="$2" ubi_vol="$3"
logger "attempting to mount alternative partition (mtd${op_ubi})"
alt_partition_unmount "$op_ubi" "$ubi_vol"
alt_partition_mount "$op_ubi" "$ubi_vol"
if [ -s "/var/alt_rom/etc/os-release" ]; then
op_info="$(. /var/alt_rom/etc/os-release && echo "$PRETTY_NAME")"
if [ "${op_info//SNAPSHOT}" != "$op_info" ]; then
op_info="$(. /var/alt_rom/etc/os-release && echo "${OPENWRT_RELEASE%%-*}")"
fi
fi
if [ -s "/var/alt_rom/etc/partition_config/soft-version" ]; then
op_info="${vendor_name:+$vendor_name }$(awk -F: '$1=="soft_ver" { print $2 ;}' /var/alt_rom/etc/partition_config/soft-version)"
fi
logger "attempting to unmount alternative partition (mtd${op_ubi})"
alt_partition_unmount "$op_ubi" "$ubi_vol"
echo "$op_info"
}
find_device_data(){
local boardNames filename i romBoardName="$1"
for filename in "${devices_dir}"*.json; do
[ "$filename" = "${devices_dir}*.json" ] && return
json_load_file "$filename"
json_get_values boardNames 'boardNames'
json_cleanup
for i in $boardNames; do
if [ "$i" = "$romBoardName" ]; then
echo "$filename"
return
fi
done
done
}
print_json() { json_init; json_add_string "$1" "$2"; json_dump; json_cleanup; }
obtain_device_info(){
local romBoardName p zyxelFlagPartition i
local vendorName deviceName partition1MTD partition2MTD labelOffset
local opOffset ubiVolume
local bootEnv1 bootEnv1Partition1Value bootEnv1Partition2Value
local bootEnv2 bootEnv2Partition1Value bootEnv2Partition2Value
local p1_label p1_version p2_label p2_version p1_os p2_os
local current_partition op_ubi cp_info op_info
romBoardName="$(cat /tmp/sysinfo/board_name)"
if [ -z "$romBoardName" ]; then
print_json 'error' 'NO_BOARD_NAME'
return
fi
p="$(find_device_data "$romBoardName")"
if [ -z "$p" ] || [ ! -s "$p" ]; then
print_json 'rom_board_name' "$romBoardName"
return
fi
json_load_file "$p"
for i in vendorName deviceName partition1MTD partition2MTD labelOffset \
bootEnv1 bootEnv1Partition1Value bootEnv1Partition2Value \
bootEnv2 bootEnv2Partition1Value bootEnv2Partition2Value \
opOffset ubiVolume; do
json_get_var $i "$i"
done
json_cleanup
if [ -n "$labelOffset" ]; then
if [ -n "$partition1MTD" ]; then
p1_label="$(dd if="/dev/${partition1MTD}" bs=1 skip="${labelOffset}" count=64 2>/dev/null)"
if [ -n "$p1_label" ]; then
p1_version="$(echo "$p1_label" | sed -n "s/\(.*\)Linux-\([0-9.]\+\).*$/\2/p")"
if [ "${p1_label//LEDE}" != "$p1_label" ]; then p1_os="LEDE"; fi
if [ "${p1_label//OpenWrt}" != "$p1_label" ]; then p1_os="OpenWrt"; fi
if [ -n "$vendorName" ] && [ "${p1_label//$vendorName}" != "$p1_label" ]; then
p1_os="$vendorName"
fi
fi
if [ -z "$p1_os" ]; then
p1_os="${vendorName:-Unknown}/Unknown"
fi
fi
if [ -n "$partition2MTD" ]; then
p2_label="$(dd if="/dev/${partition2MTD}" bs=1 skip="${labelOffset}" count=64 2>/dev/null)"
if [ -n "$p2_label" ]; then
p2_version="$(echo "$p2_label" | sed -n "s/\(.*\)Linux-\([0-9.]\+\).*$/\2/p")"
if [ "${p2_label//LEDE}" != "$p2_label" ]; then p2_os="LEDE"; fi
if [ "${p2_label//OpenWrt}" != "$p2_label" ]; then p2_os="OpenWrt"; fi
if [ -n "$vendorName" ] && [ "${p2_label//$vendorName}" != "$p2_label" ]; then
p2_os="$vendorName"
fi
fi
if [ -z "$p2_os" ]; then
p2_os="${vendorName:-Unknown}/Unknown"
fi
fi
else
p1_os="${vendorName}/Unknown (Compressed)"
p2_os="${vendorName}/Unknown (Compressed)"
fi
if [ -n "$bootEnv1" ]; then
if [ -x "/usr/sbin/fw_printenv" ] && [ -x "/usr/sbin/fw_setenv" ]; then
current_partition="$(/usr/sbin/fw_printenv -n "${bootEnv1}")"
fi
else
for i in '0:dual_flag' '0:DUAL_FLAG'; do
zyxelFlagPartition="$(find_mtd_part "$i" 2>/dev/null)"
[ -n "$zyxelFlagPartition" ] && break
done
if [ -z "$zyxelFlagPartition" ]; then
print_json 'error' 'NO_DUAL_FLAG'
logger "Unable to find Dual Boot Environment or Dual Boot Flag Partition."
return
elif [ ! -b "$zyxelFlagPartition" ]; then
print_json 'error' 'NO_DUAL_FLAG_BLOCK'
logger "The Dual Boot Flag Partition: $zyxelFlagPartition is not block device."
return
else
current_partition="$(dd if="${zyxelFlagPartition}" bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 "%d"')"
fi
fi
if is_alt_mountable "$partition1MTD" "$partition2MTD"; then
opOffset="${opOffset:-1}"
ubiVolume="${ubiVolume:-0}"
if [ "$current_partition" = "$bootEnv1Partition1Value" ]; then
op_ubi=$(( ${partition2MTD:3:3} + $opOffset ))
else
op_ubi=$(( ${partition1MTD:3:3} + $opOffset ))
fi
cp_info="$(get_main_partition_os_info $op_ubi)"
op_info="$(get_alt_partition_os_info $op_ubi $vendorName $ubiVolume)"
if [ "$current_partition" = "$bootEnv1Partition1Value" ]; then
p1_os="${cp_info:-$p1_os}"
p2_os="${op_info:-$p2_os}"
else
p1_os="${op_info:-$p1_os}"
p2_os="${cp_info:-$p2_os}"
fi
fi
if [ -n "$p1_os" ] && [ -n "$p1_version" ]; then
p1_os="$p1_os (Linux ${p1_version})"
fi
if [ -n "$p2_os" ] && [ -n "$p2_version" ]; then
p2_os="$p2_os (Linux ${p2_version})"
fi
json_init
json_add_int 'current_partition' "$current_partition"
json_add_string 'device_name' "$vendorName $deviceName"
json_add_array 'partitions'
json_add_object
if [ "$bootEnv1Partition1Value" = "$current_partition" ]; then
json_add_string 'state' "Current"
else
json_add_string 'state' "Alternative"
fi
json_add_string 'os' "$p1_os"
json_add_int 'number' "$bootEnv1Partition1Value"
json_close_object
json_add_object
if [ "$bootEnv1Partition2Value" = "$current_partition" ]; then
json_add_string 'state' "Current"
else
json_add_string 'state' "Alternative"
fi
json_add_string 'os' "$p2_os"
json_add_int 'number' "$bootEnv1Partition2Value"
json_close_object
json_close_array
json_add_string 'rom_board_name' "$romBoardName"
json_dump; json_cleanup;
}
toggle_boot_partition(){
local zyxelFlagPartition i zyxelBootFlag zyxelNewBootFlag curEnvSetting newEnvSetting
local romBoardName p
local bev1 bev2 bev1p1 bev1p2 bev2p1 bev2p2
local vendorName deviceName partition1MTD partition2MTD labelOffset
local bootEnv1 bootEnv1Partition1Value bootEnv1Partition2Value
local bootEnv2 bootEnv2Partition1Value bootEnv2Partition2Value
romBoardName="$(cat /tmp/sysinfo/board_name)"
if [ -z "$romBoardName" ]; then
print_json 'error' 'NO_BOARD_NAME'
return
fi
p="$(find_device_data "$romBoardName")"
if [ -z "$p" ] || [ ! -s "$p" ]; then
print_json 'rom_board_name' "$romBoardName"
return
fi
json_load_file "$p"
for i in vendorName deviceName partition1MTD partition2MTD labelOffset \
bootEnv1 bootEnv1Partition1Value bootEnv1Partition2Value \
bootEnv2 bootEnv2Partition1Value bootEnv2Partition2Value; do
json_get_var $i "$i"
done
json_cleanup
bev1="$bootEnv1"
bev2="$bootEnv2"
if [ -n "${bev1}${bev2}" ]; then # Linksys devices
if [ -n "$bev1" ]; then
curEnvSetting="$(fw_printenv -n "${bev1}")"
if [ -z "$curEnvSetting" ]; then
logger "$(printf "Unable to obtain firmware environment variable: %s." "$bev1")"
json_init
json_add_string 'error' 'NO_FIRM_ENV'
json_add_array 'args'
json_add_string "$bev1"
json_close_array
json_add_string 'rom_board_name' "$romBoardName"
json_dump; json_cleanup;
return
else
bev1p1="$bootEnv1Partition1Value"
bev1p2="$bootEnv1Partition2Value"
if [ "$curEnvSetting" = "$bev1p1" ]; then
newEnvSetting="$bev1p2"
else
newEnvSetting="$bev1p1"
fi
if ! fw_setenv "$bev1" "$newEnvSetting"; then
logger "$(printf "Unable to set firmware environment variable: %s to %s." "$bev1" "$newEnvSetting")"
json_init
json_add_string 'error' 'ERR_SET_ENV'
json_add_array 'args'
json_add_string "$bev1"
json_add_string "$newEnvSetting"
json_close_array
json_add_string 'rom_board_name' "$romBoardName"
json_dump; json_cleanup;
return
fi
fi
fi
if [ -n "$bev2" ]; then
curEnvSetting="$(fw_printenv -n "${bev2}")"
if [ -z "$curEnvSetting" ]; then
logger "$(printf "Unable to obtain firmware environment variable: %s." "$bev2")"
json_init
json_add_string 'error' 'NO_FIRM_ENV'
json_add_array 'args'
json_add_string "$bev2"
json_close_array
json_add_string 'rom_board_name' "$romBoardName"
json_dump; json_cleanup;
return
else
bev2p1="$bootEnv2Partition1Value"
bev2p2="$bootEnv2Partition2Value"
if [ "$curEnvSetting" = "$bev2p1" ]; then
newEnvSetting="$bev2p2"
else
newEnvSetting="$bev2p1"
fi
if ! fw_setenv "$bev2" "$newEnvSetting"; then
logger "$(printf "Unable to set firmware environment variable: %s to %s." "$bev2" "$newEnvSetting")"
json_init
json_add_string 'error' 'ERR_SET_ENV'
json_add_array 'args'
json_add_string "$bev2"
json_add_string "$newEnvSetting"
json_close_array
json_add_string 'rom_board_name' "$romBoardName"
json_dump; json_cleanup;
return
fi
fi
fi
json_init
json_dump; json_cleanup;
else # NetGear device
for i in '0:dual_flag' '0:DUAL_FLAG'; do
zyxelFlagPartition="$(find_mtd_part "$i" 2>/dev/null)"
[ -n "$zyxelFlagPartition" ] && break
done
if [ -z "$zyxelFlagPartition" ]; then
print_json 'error' 'NO_DUAL_FLAG'
logger "Unable to find Dual Boot Environment or Dual Boot Flag Partition."
return
elif [ ! -b "$zyxelFlagPartition" ]; then
print_json 'error' 'NO_DUAL_FLAG_BLOCK'
logger "The Dual Boot Flag Partition: $zyxelFlagPartition is not block device."
return
else
zyxelBootFlag="$(dd if="${zyxelFlagPartition}" bs=1 count=1 2>/dev/null | hexdump -n 1 -e '1/1 "%d"')"
if [ "$zyxelBootFlag" = "1" ]; then
zyxelNewBootFlag="\\xff"
else
zyxelNewBootFlag="\\x01"
fi
if [ -n "$zyxelNewBootFlag" ]; then
if ! printf "%b" "$zyxelNewBootFlag" > "$zyxelFlagPartition"; then
logger "$(printf "Unable to set Dual Boot Flag Partition entry for partition: %s." "$zyxelFlagPartition")"
json_init
json_add_string 'error' 'ERR_SET_DUAL_FLAG'
json_add_array 'args'
json_add_string "$zyxelFlagPartition"
json_close_array
json_add_string 'rom_board_name' "$romBoardName"
json_dump; json_cleanup;
return
fi
fi
fi
json_init
json_dump; json_cleanup;
fi
}
case "$1" in
list)
json_init
json_add_object "obtain_device_info"
json_close_object
json_add_object "toggle_boot_partition"
json_close_object
json_dump
json_cleanup
;;
call)
case "$2" in
obtain_device_info)
obtain_device_info;;
toggle_boot_partition)
toggle_boot_partition;;
esac
;;
esac
|