/
gui_bootmanager
executable file
·1532 lines (1531 loc) · 87.2 KB
/
gui_bootmanager
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/sh
# vim: set tabstop=4 syntax=sh :
# SPDX-License-Identifier: GPL-2.0-or-later
#######################################################################################################
# #
# extension for the 'restart' page of AVM's FRITZ!OS GUI to provide a selection between the two #
# installed systems on devices with 'dual boot' support #
# #
###################################################################################################VER#
# #
# gui_bootmanager, version 0.6.5 #
# #
# This script is a part of the YourFritz project from https://github.com/PeterPawn/YourFritz. #
# #
###################################################################################################CPY#
# #
# Copyright (C) 2014-2020 P.Haemmerlein (peterpawn@yourfritz.de) #
# #
###################################################################################################LIC#
# #
# This project is free software, you can redistribute it and/or modify it under the terms of the GNU #
# General Public License as published by the Free Software Foundation; either version 2 of the #
# License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without #
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License under http://www.gnu.org/licenses/gpl-2.0.html for more details. #
# #
#######################################################################################################
# #
# "FRITZ!Box" and "FRITZ!" are registered word marks and "AVM" is a registered word and figurative #
# mark of: #
# #
# AVM Computersysteme Vertriebs GmbH, 10559, Berlin, DE. #
# #
#######################################################################################################
# #
# This script implements all actions necessary to switch between the two installed system versions on #
# AVM router devices with 'dual-boot' capabilities. #
# #
# It should support any VR9- or AR10-based device as well as devices with Puma6/7 chipsets (Puma7 #
# support is currently untested and solely based on already published script code from AVM). Devices #
# with a GRX5 SoC are supported, too - but there's only switching of system possible and there's no #
# further distinction between bootloader versions, where the branding may be changed (some earlier #
# 7560 and 7580 devices) and newer devices, where a branding is always reset to the finalized value #
# from bootloader partition. #
# #
# With update to 0.6.4, support for IPQ4019-based devices was added, the restrictions are the same as #
# for GRX5 devices. #
# #
# This version is able to detect a fixed branding value, if it's set from '/etc/init.d/rc.conf' with #
# a modified 'export OEM' statement - in such a case, an appropriate output is generated, containing #
# a message about this fixed value. #
# #
# There are six operations supported as first argument (at least one parameter is always required): #
# #
# is_supported - returns true (exit code 0) or false (exit code 1), if the device doesn't support the #
# dual-boot selection #
# is_blocked - check the used semaphore file, if there's already a running instance #
# html_display - creates a little HTML snippet (containing JS code, too), which may be included into #
# the original reboot.lua page (tested up to version 07.01) #
# - if the second parameter is 'nocache', an existing cache file (where those data is #
# stored otherwise - ready to be displayed on next call) gets ignored #
# - the data for 'get_values' is cached at the same time, if 'nocache' wasn't specified #
# switch_to - switches to the 'running' or 'alternative' system (2nd parameter) and (optionally) #
# sets the OEM name to the 3rd parameter (which isn't checked any further) #
# get_values - provide a list of relevant values for implementers of other interfaces, without the #
# needs to implement the full logic once again #
# - if the second parameter is 'nocache', an existing cache file (where those data is #
# stored otherwise - ready for output on next call) gets ignored #
# - while the call for 'html_display' creates the cache file for this function too, this #
# one doesn't create a cached HTML output and any later call to 'html_display' w/o the #
# 'nocache' parameter will refresh the cached data of this function again #
# debug - this operation is intended only for diagnose, it shows some settings and their #
# current values on STDOUT to provide information about the current system #
# clear_cache - remove cached HTML output and/or saved data from an earlier 'html_display' or #
# 'get_values' call without 'nocache' argument #
# #
# The script tries to detect the framework (if any), which was used to modify the original firmware. #
# To find this, it checks for the YourFritz version file, the Freetz version file and the "modfs" #
# version file in this order and the first file found is assumed to be a mark of the used framework. #
# #
#######################################################################################################
# #
# constants #
# #
#######################################################################################################
# #
# common values #
# #
#######################################################################################################
readonly html_cache_file="/var/tmp/gui_bootmanager.html"
readonly html_cache_error="/var/tmp/gui_bootmanager.rc"
readonly data_cache_file="/var/tmp/gui_bootmanager.data"
readonly boot_cache_file="/var/tmp/gui_bootmanager.boot"
readonly urlader_environment="/proc/sys/urlader/environment"
readonly system_select_name="linux_fs_start"
readonly active_name="active"
readonly inactive_name="inactive"
readonly branding_name="firmware_version"
readonly proc_mtd="/proc/mtd"
readonly proc_devices="/proc/devices"
# shellcheck disable=SC2034
readonly mtd_prefix="mtd"
readonly mtdblock_prefix="/dev/mtdblock"
readonly mtdblock_mask="${mtdblock_prefix}%u"
readonly proc_cpuinfo="/proc/cpuinfo"
readonly proc_mountinfo="/proc/self/mountinfo"
readonly kernel_name="kernel"
readonly filesystem_name="filesystem"
readonly reserved_prefix="reserved"
readonly alt_root_mount_name="alt_root"
readonly defaults_dir="etc/default.%s"
readonly sysfs_block_dir="/sys/block"
readonly loop_driver_name="loop"
readonly backing_file_name="backing_file"
readonly tmpdir="${TMPDIR:-/var/tmp}"
readonly avm_rcconf="etc/init.d/rc.conf"
readonly semafile="/var/run/gui_bootmanager.sem"
readonly fdt_chosen="/proc/device-tree/chosen"
readonly radio_button_checked=" checked=\\\"checked\\\""
#######################################################################################################
# #
# various sources for version numbers #
# #
#######################################################################################################
readonly yourfritz_version_file="etc/.yourfritz_version"
readonly freetz_version_file="etc/.freetz-version"
readonly freetz_ng_version_file="etc/.freetz-ng-version"
readonly modfs_version_file="etc/.modfs_version"
readonly avm_version_file_old="etc/version"
readonly avm_version_file_old_prefix="FIRMWARE_"
readonly avm_version_file="etc/init.d/rc.conf"
readonly avm_version_file_prefix="CONFIG_"
readonly avm_version_main_components="VERSION SUBVERSION"
readonly avm_version_major_name="CONFIG_VERSION_MAJOR"
readonly avm_version_date="DATE"
readonly avm_product_name="CONFIG_PRODUKT"
readonly version_files="$yourfritz_version_file $freetz_ng_version_file $freetz_version_file $modfs_version_file"
readonly modified_by="YourFritz Freetz-NG Freetz modfs -"
#######################################################################################################
# #
# GRX5, VR9 and IPQ4019 common settings #
# #
#######################################################################################################
# shellcheck disable=SC2034
readonly mtd_filesystem_name_active="${filesystem_name}"
# shellcheck disable=SC2034
readonly mtd_filesystem_name_inactive="${reserved_prefix}-${filesystem_name}"
# shellcheck disable=SC2034
readonly mtd_kernel_name_active="${kernel_name}"
# shellcheck disable=SC2034
readonly mtd_kernel_name_inactive="${reserved_prefix}-${kernel_name}"
#######################################################################################################
# #
# IPQ4019 specific settings (IPQ4018-based devices should use SPI flash with a single OS) #
# #
#######################################################################################################
readonly ipq4019_cpuinfo_check="s|^model name[ \t]*: ARMv7 Processor rev 5 (v7l)\$|IPQ4019|p;tq;b;:q q"
#######################################################################################################
# #
# VR9 specific settings #
# #
#######################################################################################################
readonly wrapper_mount_name="wrapper"
readonly vr9_wrapper_dir="/wrapper"
readonly vr9_rootfs_name="/filesystem_core.squashfs"
readonly vr9_image_source_active="${vr9_wrapper_dir}${vr9_rootfs_name}"
readonly vr9_image_source_inactive="mount:%s:${vr9_rootfs_name}"
########################################################################################################
# #
# Puma6 / Puma7 specific settings #
# #
#######################################################################################################
readonly puma_helper="/etc/puma6_helper.sh"
readonly puma_partitions="/proc/avm_partitions"
# shellcheck disable=SC2034
readonly puma_filesystem_name_active="${filesystem_name}_ATOM"
# shellcheck disable=SC2034
readonly puma_filesystem_name_inactive="${filesystem_name}_${reserved_prefix}_ATOM"
# shellcheck disable=SC2034
readonly puma_kernel_name_active="${kernel_name}_ATOM"
# shellcheck disable=SC2034
readonly puma_kernel_name_inactive="${kernel_name}_${reserved_prefix}_ATOM"
#######################################################################################################
# #
# sub-functions #
# #
#######################################################################################################
# #
# (safe) detection of Puma6 and Puma7 devices, only ATOM core supported - runs in a sub-shell #
# #
#######################################################################################################
is_puma_based_device()
(
[ -f "$puma_helper" ] || exit 1
# shellcheck disable=SC1090
. "$puma_helper"
if type is_puma7 2>/dev/null 1>&2; then
[ "$IS_ATOM" = "y" ] || exit 1
else
is_puma_atom || exit 1
fi
exit 0
)
#######################################################################################################
# #
# provide a shellcheck compatible use of printf with variable mask values #
# #
#######################################################################################################
printf_ss()
(
# shellcheck disable=SC2059
printf -- "$(printf -- '%s' "$1")" "$*"
)
#######################################################################################################
# #
# check, if the host system has a FDT based environment - then we'll never try to switch the branding #
# #
#######################################################################################################
has_fdt_environment()
{
[ -d "$fdt_chosen" ] && return 0 || return 1
}
#######################################################################################################
# #
# check, if the host system supports branding changes via environment writes #
# #
#######################################################################################################
change_branding_support()
{
has_fdt_environment && return 1
return 0
}
#######################################################################################################
# #
# find system type or model name #
# #
#######################################################################################################
get_system_type()
{
type="$(sed -n -e "s|^system type[ \t]*: \([^ \t]*\).*|\1|p" "$proc_cpuinfo")"
[ -n "$type" ] && printf -- '%s\n' "$type" && return 0
type="$(sed -n -e "$ipq4019_cpuinfo_check" "$proc_cpuinfo")"
[ -n "$type" ] && printf -- '%s\n' "$type" && return 0
printf -- '%s\n' "undetected"
return 1
}
#######################################################################################################
# #
# find partition number with given name #
# #
#######################################################################################################
get_mtd_number()
{
sed -n -e "s|^mtd\([0-9]*\): [^ ]* [^ ]* \"$1\"\$|\1|p" "$proc_mtd"
}
#######################################################################################################
# #
# GRX5/VR9: locate the MTD partition for kernel or filesystem for the active or alternative system #
# #
#######################################################################################################
get_mtd_partition()
(
[ "$2" = "$active_name" ] || [ "$2" = "$inactive_name" ] || exit 1
if [ "$1" = "$kernel_name" ] || [ "$1" = "$filesystem_name" ]; then
printf_ss "$mtdblock_mask" "$(get_mtd_number "$(printf -- "\${mtd_%s_name_%s}" "$1" "$2")")"
exit 0
fi
exit 1
)
#######################################################################################################
# #
# VR9: locate the MTD partition for kernel or the image name of root filesystem for the active or #
# alternative system #
# #
#######################################################################################################
get_vr9_image()
(
[ "$1" = "$filesystem_name" ] || exit 1
if [ "$2" = "$active_name" ]; then
printf -- '%s' "$vr9_image_source_active"
exit 0
elif [ "$2" = "$inactive_name" ]; then
eval local_name="\${mtd_${1}_name_${2}}"
printf_ss "$vr9_image_source_inactive" "$(printf_ss "$mtdblock_mask" "$(eval get_mtd_number "$(printf -- "\${mtd_%s_name_%s}" "$1" "$2")")")"
exit 0
fi
exit 1
)
#######################################################################################################
# #
# Puma6/7: locate the eMMC partition for filesystem or kernel of the active or alternative system #
# #
#######################################################################################################
get_puma_partition()
(
[ "$2" = "$active_name" ] || [ "$2" = "$inactive_name" ] || exit 1
if [ "$1" = "$kernel_name" ] || [ "$1" = "$filesystem_name" ]; then
sed -n -e "s|^$(eval printf -- "\$puma_%s_name_%s" "$1" "$2")=\(.*\)|\1|p" "$puma_partitions"
exit 0
fi
exit 1
)
#######################################################################################################
# #
# locate the flash device (MTD or eMMC) for filesystem or kernel of the active or alternative system #
# #
#######################################################################################################
get_partition()
(
rc=0
[ "$1" = "$kernel_name" ] || [ "$1" = "$filesystem_name" ] || exit 1
[ "$2" = "$active_name" ] || [ "$2" = "$inactive_name" ] || exit 1
if is_puma_based_device; then
get_puma_partition "$1" "$2"
rc=$?
else
st="$(get_system_type)"
if [ "$st" = "VR9" ] || [ "$st" = "AR10" ]; then
if [ "$1" = "$kernel_name" ]; then
get_mtd_partition "$1" "$2"
else
get_vr9_image "$1" "$2"
fi
rc=$?
elif [ "$st" = "GRX500" ] || [ "$st" = "GRX350" ] || [ "$st" = "IPQ4019" ]; then
get_mtd_partition "$1" "$2"
rc=$?
else
rc=1
fi
fi
exit $rc
)
#######################################################################################################
# #
# get the size of a MTD or eMMC partition #
# #
#######################################################################################################
get_partition_size()
(
if is_puma_based_device; then
symname="$(sed -n -e "s|\(.*\)=$1|\1|p" "$puma_partitions")"
size="$(sed -n -e "s|${symname}_SIZE=\(.*\)|\1|p" "$puma_partitions")"
else
part="$(expr "$1" : "[^0-9]*\(.*\)")"
size="$(sed -n -e "s|^mtd${part}: \([^ ]*\) .*|0x\1|p" "$proc_mtd")"
fi
[ "${#size}" -gt 2 ] && printf -- '%d\n' $(( size )) && exit 0
exit 1
)
#######################################################################################################
# #
# check, if a list contains an item and return its index (based on 1) #
# #
# $1 - the item to look for (needle) #
# $2 - the haystack #
# #
#######################################################################################################
index_of_item()
(
i=0
for item in $2; do
i=$(( i + 1 ))
[ "$1" = "$item" ] && printf -- '%u' "$i" && exit 0
done
exit 1
)
#######################################################################################################
# #
# get the count of words in the specified string #
# #
# $* - items to count #
# #
#######################################################################################################
count_of_items()
(
# shellcheck disable=SC2048,SC2086
set -- $*
printf -- '%u' $#
)
#######################################################################################################
# #
# create a locked section using a semaphore file, cleanup older orphaned locks automatically #
# #
# $1 - operation (get or release) #
# $2 - maximum wait time for 'get' #
# #
#######################################################################################################
spinlock()
(
{ [ "$1" = "get" ] || [ "$1" = "release" ]; } || exit 1
[ -z "$spinlock_pid" ] && exit 1
set -C
if [ "$1" = "get" ]; then
maxwait=$(( $2 ))
i=0
while [ $i -le $maxwait ]; do
while [ -f "$semafile" ]; do
pid="$(cat "$semafile")"
if [ -d "/proc/$pid" ]; then
sleep 1
i=$(( i + 1 ))
[ $i -ge $maxwait ] && exit 1
else
if [ -d "/proc" ]; then
rm "$semafile" 2>/dev/null
fi
fi
done
printf -- '%d' "$spinlock_pid" >"$semafile"
pid="$(cat "$semafile")"
[ -n "$pid" ] && [ "$pid" = "$spinlock_pid" ] && exit 0
done
exit 1
else
[ -f "$semafile" ] || exit 0
pid="$(cat "$semafile")"
{ [ -z "$pid" ] || [ "$pid" != "$spinlock_pid" ]; } && exit 0
rm -f "$semafile" 2>/dev/null
exit 0
fi
)
#######################################################################################################
# #
# compare two strings and return -1, 0 or 1 for lower, equal and higher #
# #
# $1 - left item to compare #
# $2 - right item to compare #
# #
#######################################################################################################
compare_strings()
(
t="$tempdir/cs_$$"
while [ -d "$t" ]; do
t="${t}_$$"
done
mkdir -p "$t" 2>/dev/null
trap 'rm -r "$t" 2>/dev/null' EXIT HUP INT
printf -- '%s\377' "$1" >"$t/left"
printf -- '%s\377' "$2" >"$t/right"
cmp -l "$t/left" "$t/right" 2>/dev/null >"$t/out"
read -r pos left right 2>/dev/null <"$t/out"
if [ -z "$left" ] || [ -z "$right" ]; then
printf -- '%d' "0"
else
[ "$left" -lt "$right" ] && printf -- '%d' "-1" || printf -- '%d' "1"
fi
)
#######################################################################################################
# #
# get branding from environment #
# #
#######################################################################################################
get_system_branding()
(
fixed="$(get_fixed_branding)" && printf -- '%s' "$fixed" && exit 0
sed -n -e "s/^$branding_name\t\(.*\)\$/\1/p" "$urlader_environment"
)
#######################################################################################################
# #
# get next system to boot from environment #
# #
#######################################################################################################
get_system_switch()
(
[ -n "$2" ] && sed -n -e "s/^$system_select_name\t[01]\(.*\)\$/\1/p" "$1" && return
sed -n -e "s/^$system_select_name\t\([01]\).*\$/\1/p" "${1:-$urlader_environment}"
)
#######################################################################################################
# #
# check the first 16 byte of the specified partition - if they're all 0xFF or 0x00, there's usually #
# no kernel (and no alternative system) installed #
# #
# $1 - kernel (block) device to check #
# #
#######################################################################################################
is_kernel_present()
(
__read_bytes()
{
while read -r line left right; do
[ "$left" -ne 377 ] && [ "$left" -ne 0 ] && return 0
done
return 1
}
dd if="$1" bs=16 count=1 2>/dev/null | cmp -l - /dev/zero 2>/dev/null | __read_bytes
)
#######################################################################################################
# #
# check, if the system to start next is also the running one #
# #
# - if a FDT exists, the started system may be found there #
# - if a saved environment exists, we take the value from there and treat it as the original one #
# - if neither of the places above contains a valid value, the partition of 'kernel' type with the #
# lowest device index is assumed to be from system selector value 0 #
# #
#######################################################################################################
is_switched()
(
if [ -d "$fdt_chosen" ]; then
! [ "$(sed -n -e "s|^\([01]\).*|\1|p" "$fdt_chosen/$system_select_name")" = "$(get_system_switch)" ]
else
if [ -f "$boot_cache_file" ]; then
! [ "$(get_system_switch)" = "$(get_system_switch "$boot_cache_file")" ]
else
k1="$(get_partition "$kernel_name" "$active_name")"
k2="$(get_partition "$kernel_name" "$inactive_name")"
c=$(compare_strings "$k1" "$k2")
[ "$c" -eq -1 ] && current=0 || current=1
! [ "$(get_system_switch)" = "$current" ]
fi
fi
)
#######################################################################################################
# #
# switch system selection variable to alternative value #
# #
#######################################################################################################
switch_system()
(
printf -- '%s\t%d%s\n' "$system_select_name" $(( ( $(get_system_switch) + 1 ) % 2 )) "$(get_system_switch "$urlader_environment" 1)" >"$urlader_environment"
)
#######################################################################################################
# #
# switch next boot time selection to specified system, if action is really needed #
# #
# $1 - 'running' / 'alternative' to switch to the specified system based on the running version #
# #
#######################################################################################################
switch_system_to()
(
if is_switched; then
[ "$1" = "running" ] && switch_system
else
[ "$1" = "alternative" ] && switch_system
fi
[ -z "$2" ] || change_branding "$2"
)
#######################################################################################################
# #
# replace branding, if necessary #
# #
#######################################################################################################
change_branding()
(
[ "$1" = "$(get_system_branding)" ] || printf -- '%s\t%s' "$branding_name" "$1" >"$urlader_environment"
)
#######################################################################################################
# #
# get product name value from AVM's rc.conf file #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_product_name()
(
sed -n -e "s|^[ \t]*export $avm_product_name=\"\?\([^\"]*\)\"\?\$|\1|p" "${1:-/}${1:+/}$avm_version_file" | sed -n -e \$p
)
#######################################################################################################
# #
# get major version from AVM's rc.conf file #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_version_major()
(
sed -n -e "s|^[ \t]*export $avm_version_major_name=\"\?\([^\"]*\)\"\?\$|\1|p" "${1:-/}${1:+/}$avm_version_file" | sed -n -e \$p
)
#######################################################################################################
# #
# get the contained brandings from the directory with system defaults #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_brandings()
(
fixed="$(get_fixed_branding "$1")" && printf -- '%s' "$fixed" && exit 0
if [ -z "$1" ] || [ "$1" = "/" ]; then
eval PRODUKT="\$$avm_product_name"
else
PRODUKT="$(get_product_name "$1")"
fi
path="$(printf -- '%s%s%s' "${1:-/}" "${1:+/}" "$(printf_ss "$defaults_dir" "$PRODUKT")")"
find "$path" -type d -maxdepth 1 2>/dev/null | sed -n -e "s|^$path/\(.*\)|\1|p" | sed -e '/^$/d' -e ':x;$!N;s/\n/ /;tx'
)
#######################################################################################################
# #
# extract firmware variables from AVM's firmware info source #
# #
# $1 - info source file #
# $2 - values prefix in this file #
# #
#######################################################################################################
get_version_main_values()
(
for v in $avm_version_main_components; do
sed -n -e "s|^[ \t]*export $2$v=\"\?\([^\"]*\)\"\?\$|$v=\"\1\"|p" "$1" | sed -n -e \$p
done
)
#######################################################################################################
# #
# extract firmware variables from AVM's firmware new info source #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_new_version_main_values()
(
path="${1:-/}${1:+/}$avm_version_file"
prefix="$avm_version_file_prefix"
get_version_main_values "$path" "$prefix"
)
#######################################################################################################
# #
# extract firmware variables from AVM's firmware old info source #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_old_version_main_values()
(
path="${1:-/}${1:+/}$avm_version_file_old"
prefix="$avm_version_file_old_prefix"
get_version_main_values "$path" "$prefix"
)
#######################################################################################################
# #
# check, if version info is specified in the older or newer place #
# #
# $1 - system root directory #
# #
#######################################################################################################
has_old_version_info()
(
path="${1:-/}${1:+/}$avm_version_file_old"
for v in $avm_version_main_components; do
[ -z "$(sed -n -e "s|^[ \t]*export $avm_version_file_old_prefix$v=\"\?\([^\"]*\)\"\?\$|\1|p" "$path")" ] \
&& exit 1 || exit 0
done
)
#######################################################################################################
# #
# get system version from specified system root #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_version_values()
(
if has_old_version_info "$1"; then
get_old_version_main_values "$1"
else
get_new_version_main_values "$1"
fi
)
#######################################################################################################
# #
# get system version from specified system root #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_system_version()
(
eval "$(get_version_values "$1")"
has_old_version_info "$1" && printf -- '%s%s' "$VERSION" "$SUBVERSION" || printf -- '%s.%s%s' "$(get_version_major "$1")" "$VERSION" "$SUBVERSION"
)
#######################################################################################################
# #
# parse date value with format from AVM's firmware (/etc/version) to a UNIX timestamp #
# #
# $1 - source string #
# #
#######################################################################################################
parse_date_value()
(
command -v date 2>/dev/null 1>&2 || printf -- '%s' "$1"
date --help 2>&1 | grep -q BusyBox || printf -- '%s' "$1"
eval "$(printf -- '%s\n' "$1" | sed -n -e "s|\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\) \([0-9]*\):\([0-9]*\):\([0-9]*\)|day=\1 month=\2 year=\3 hour=\4 minutes=\5 seconds=\6|p")"
# shellcheck disable=SC2154
date +%s -d "$year$month$day$hour$minutes.$seconds"
)
#######################################################################################################
# #
# get localized date string #
# #
#######################################################################################################
get_localized_date()
{
if [ "$Language" = "de" ]; then
date +'%d.%m.%Y, %H:%M:%S Uhr' -d @"$1"
else
date +'%m/%d/%Y %r' -d @"$1"
fi
}
#######################################################################################################
# #
# get system build date from specified root, it's a file modification time in the newer versions #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_system_date()
(
path="${1:-/}${1:+/}$avm_version_file_old"
if has_old_version_info "$1"; then
d="$(sed -n -e "s|^[ \t]*export $avm_version_file_old_prefix$avm_version_date=\"\?\([^\"]*\)\"\?\$|\1|p" "$path")"
v=$(parse_date_value "$d")
else
v=$(stat -c %Y "$path")
fi
printf -- '%d' "$v"
)
#######################################################################################################
# #
# get system build date from specified root as a string value with localization #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_localized_system_date()
(
get_localized_date "$(get_system_date "$1")"
)
#######################################################################################################
# #
# get fixed brandings string from specified system root #
# #
# $1 - system root directory #
# #
#######################################################################################################
get_fixed_branding()
(
path="${1:-/}${1:+/}$avm_rcconf"
b="$(sed -n -e 's|^export OEM\(=\"\?\(.*\)\"\?\)\?$|\2|p' "$path")"
printf -- '%s' "$b"
[ -z "$b" ] && exit 1 || exit 0
)
#######################################################################################################
# #
# check, if the specified directory is an active mount-point for any filesystem #
# #
#######################################################################################################
is_active_mountpoint()
(
line="$(sed -n -e "\|^[0-9]\+ [0-9]\+ [0-9:]* [^ ]* ${1:-/} .* - .*|p" "$proc_mountinfo")"
[ ${#line} -eq 0 ] && exit 1 || exit 0
)
#######################################################################################################
# #
# find an active mountpoint with the specified device #
# #
#######################################################################################################
find_mountpoint_for_device()
(
line="$(sed -n -e "s|^[0-9]\+ [0-9]\+ [0-9:]* [^ ]* \(.*\) .* - [^ ]* $1 .*|\1|p" "$proc_mountinfo")"
[ ${#line} -eq 0 ] || printf -- '%s' "$line"
[ ${#line} -eq 0 ] && exit 1 || exit 0
)
#######################################################################################################
# #
# find the device for an active mountpoint #
# #
#######################################################################################################
find_device_of_mountpoint()
(
line="$(sed -n -e "s|^[0-9]\+ [0-9]\+ [0-9:]* [^ ]* $1 .* - [^ ]* \([^ ]*\) .*|\1|p" "$proc_mountinfo")"
[ ${#line} -eq 0 ] || printf -- '%s' "$line"
[ ${#line} -eq 0 ] && exit 1 || exit 0
)
#######################################################################################################
# #
# find device's major and minor ID for the specified mountpoint #
# #
#######################################################################################################
get_ids_of_mountpoint()
(
line="$(sed -n -e "s|^[0-9]\+ [0-9]\+ *\([0-9]*:[0-9]*\) [^ ]* ${1:-/} .* - [^ ]* \([^ ]*\) .*|\1|p" "$proc_mountinfo")"
[ ${#line} -eq 0 ] || printf -- '%s' "$line"
[ ${#line} -eq 0 ] && exit 1 || exit 0
)
#######################################################################################################
# #
# check, if the specified mountpoint is from a loop device #
# #
#######################################################################################################
is_loopdevice_mountpoint()
(
loop_major="$(sed -n -e "s|^ *\([0-9]*\) *${loop_driver_name}[ \t]\?.*\$|\1|p" "$proc_devices")"
[ ${#loop_major} -eq 0 ] && exit 1
dev="$(get_ids_of_mountpoint "$1")"
[ ${#dev} -eq 0 ] && exit 1
[ "${dev%:*}" = "$loop_major" ] && exit 0 || exit 1
)
#######################################################################################################
# #
# get file name of mounted image for a loopback device #
# #
#######################################################################################################
get_loopdevice_backingfile()
(
if is_loopdevice_mountpoint "${1:-/}"; then
dev="$(get_ids_of_mountpoint "${1:-/}")"
[ -z "$dev" ] && exit 1
for device in "$sysfs_block_dir"/*; do
if [ "$(cat "$device/dev" 2>/dev/null)" = "$dev" ]; then
file="$(cat "$device/$loop_driver_name/$backing_file_name" 2>/dev/null)"
[ "${#file}" -gt 0 ] && printf -- '%s\n' "$file" && exit 0
fi
done
fi
exit 1
)
#######################################################################################################
# #
# mount alternative system' root filesystem somewhere #
# #
# CGI calls have only /bin:/usr/bin in the search PATH, the old value is not important, changing the #
# PATH should be local to this function and its children, even if it's "export"ed here. #
# #
#######################################################################################################
mount_alternative_system()
(
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
src="$(get_partition "$filesystem_name" "$inactive_name")"
mp="$tempdir/$alt_root_mount_name"
mkdir -p "$mp" 2>/dev/null || exit 1
if ! [ "${src#mount:}" = "$src" ]; then
# shellcheck disable=SC2046
set -- $(printf -- '%s' "$src" | sed -e "s|:| |g")
device="$2"
file="$3"
else
unset device
file="$src"
fi
if [ -n "$device" ]; then
wr="$tempdir/$wrapper_mount_name"
mkdir -p "$wr" 2>/dev/null || exit 1
wre="$(find_mountpoint_for_device "$device")"
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
mount -t yaffs2 -o ro "$device" "$wr" 2>/dev/null || exit 1
else
wr="$wre"
fi
fi
mount -t squashfs -o ro "$wr$file" "$mp" 2>/dev/null || exit 1
printf -- '%s' "$mp"
exit 0
)
#######################################################################################################
# #
# dismount the alternative root filesystem #
# #
#######################################################################################################
dismount_alternative_system()
(
is_active_mountpoint "$tempdir/$alt_root_mount_name" || exit 0
umount -d "$tempdir/$alt_root_mount_name" 2>/dev/null || exit 1
rmdir "$tempdir/$alt_root_mount_name" 2>/dev/null
is_active_mountpoint "$tempdir/$wrapper_mount_name" || exit 0
umount -d "$tempdir/$wrapper_mount_name" 2>/dev/null || exit 1
rmdir "$tempdir/$wrapper_mount_name" 2>/dev/null
exit 0
)
#######################################################################################################
# #
# guess modification source #
# #
# $1 - mounted system root directory (optional) #
# #
#######################################################################################################
get_modified_by()
(
base="${1:-}"
i=1
for f in $version_files; do
[ -f "$base/$f" ] && break
i=$(( i + 1 ))
done
# shellcheck disable=SC2086
set -- $modified_by
eval printf -- '%s' "\$$i"
)
#######################################################################################################
# #
# convert binary value (from STDIN) to decimal representation #
# #
#######################################################################################################
bin2dec()
(
bin2dec_read_octal()
{
e="$1"
i=1
v=0
s=-8
ff=0
while read -r pos left right; do
if [ $ff -eq 1 ]; then
[ "$e" -eq 0 ] && v=$(( v * 256 )) || s=$(( s + 8 ))
[ "$e" -eq 0 ] && ff=255 || ff=$(( 255 << s ))
v=$(( v + ff ))
i=$(( i + 1 ))
ff=0
fi
while [ $i -lt "$pos" ]; do # insert zeros for missing bytes
[ "$e" -eq 0 ] && v=$(( v * 256 )) || s=$(( s + 8 ))
i=$(( i + 1 ))
done
if [ "$right" = 377 ] && [ $ff -eq 0 ]; then
ff=1
continue
fi
[ "$e" -eq 0 ] && v=$(( v * 256 )) || s=$(( s + 8 ))
[ "$e" -eq 0 ] && right=$(( 0$right )) || right=$(( 0$right << s ))
v=$(( v + right ))
i=$(( pos + 1 ))
done
printf -- '%d' $v
}
[ "$(dd if=/proc/self/exe bs=1 count=1 skip=5 2>/dev/null | base64)" = "AQ==" ] && e=1 || e=0
{ cat; printf -- '%b' "\377"; } | cmp -l -- /dev/zero - 2>/dev/null | bin2dec_read_octal $e
return 0
)
#######################################################################################################
# #
# get mkfs_time value from SquashFS superblock #
# #
# $1 - mounted system root directory #
# #
#######################################################################################################
get_mkfs_time()
(
base="${1:-}"
if is_loopdevice_mountpoint "${base:-/}"; then
src="$(get_loopdevice_backingfile "${base:-/}")"
[ "${#src}" -gt 0 ] && len=$(stat -c %s "$src") || len=0
elif [ -z "$1" ] || [ "$1" = "/" ]; then # root of running system needs /proc/mtd
src="$(get_partition "$filesystem_name" "$active_name")"
[ "${#src}" -gt 0 ] && len=$(get_partition_size "$src") || len=0
else
src="$(find_device_of_mountpoint "$1")"
[ "${#src}" -gt 0 ] && len=$(get_partition_size "$src") || len=0
fi
if [ "${#src}" -gt 0 ]; then
v="$(dd if="$src" bs=4 count=1 skip=2 2>/dev/null | bin2dec)"
if [ "${#v}" -gt 0 ]; then
if [ "$v" -gt "$len" ]; then
printf -- '%s\n' "$v" && exit 0
fi
fi
fi
exit 1
)
#######################################################################################################
# #
# get date of last modification from specified system root #
# - take the date and time from any existing modification file or #
# - take date and time from SquashFS image, if it's a file (mounted via a loop device) #
# #
# $1 - mounted system root directory (optional) #
# #
#######################################################################################################
get_modified_date()
(
command -v stat 2>/dev/null 1>&2 || exit 1
v="$(stat -c %Y -- /proc/self/exe 2>/dev/null)"
# shellcheck disable=SC2181
[ $? -ne 0 ] && exit 1
[ "$(expr "$v" : "\([0-9]*\)")" = "$v" ] || exit 1
base="${1:-}"
unset v
for f in $version_files; do
if [ -f "$base/$f" ]; then
v="$(stat -c %Y -- "$base/$f")"
break
fi
done
if [ -z "$v" ]; then
v="$(get_mkfs_time "${base:-/}")" # try to find valid mkfs_time first
fi
if [ -z "$v" ] && is_loopdevice_mountpoint "${base:-/}"; then
file="$(get_loopdevice_backingfile "${base:-/}")"
v="$(stat -c %Y -- "$file")"
d="$(get_system_date "$1")"
if [ -n "$d" ] && [ -n "$v" ]; then
[ "$d" -lt "$v" ] && delta=$(( v - d )) || delta=$(( d - v )) # no abs() function available
[ $delta -lt 600 ] && exit 1 # within ten minutes, assume no further modifications
fi
fi
[ -z "$v" ] && exit 1
get_localized_date "$v"
exit 0