forked from PeterPawn/modfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modfs
executable file
·3681 lines (3676 loc) · 107 KB
/
modfs
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 shiftwidth=4 syntax=sh highlight=on
# SPDX-License-Identifier: GPL-2.0-or-later
################################################################################
# #
# an alternative approach to modify firmware images for AVM FRITZ!Box routers #
# #
# Copyright (C) 2014-2020 P.Hämmerlein (http://www.yourfritz.de) #
# #
# This program 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. #
# #
################################################################################
#
# AVM "constants"
#
# procfs of MTD driver
procmtd="/proc/mtd"
# device name of MTD partition for access with character oriented I/O
mtdprefix="mtd"
# device name of MTD partitions for access with block oriented I/O
mtdblockname="mtdblock"
# prefix for inactive partition names
reservedprefix="reserved-"
# name of kernel partition
kernelname="kernel"
# name of file system partition
filesystemname="filesystem"
# name of the partition with NAND flash available to the user
nandname="nand-filesystem"
# environment file name within the TFFS procfs
envpathname="/proc/sys/urlader/environment"
# variable used to switch between boot time systems
fsvarname="linux_fs_start"
# hardware revision variable name
hwrevname="HWRevision"
# supported hardware revisions as "modfs" host
# 3370, 7490, 3390, 7362SL, 3490, 7430, 7412, 7272, 3272, 5490, 5491
hwrevs_supported="175 185 193 203 212 209 218 192 198 223 243"
# AVM's binary to modify the NAND based partitions (busybox has it's own tools, but not ooB on a device with original firmware)
update_kernel_binary="/sbin/update_kernel"
# where is the wrapper mounted at the running system ? (if any)
wrapperdir="/wrapper"
# the file name of the root file system image, if a wrapper YAFFS2 image exists
rootfsname="filesystem_core.squashfs"
# the name of a firmware image, it will be renamed during download
firmwarestoragename="firmware.image"
# the kernel image name within a firmware image
firmware_kernel_image="./var/tmp/kernel.image"
# the (outer) file system image name within a firmware image
firmware_filesystem_image="./var/tmp/filesystem.image"
# (outer) file system image file name
outerimagename="filesystem.image"
# kernel image file name
kernelfilename="kernel.image"
# the name of the busybox to be used, will be changed later
bb="/bin/busybox"
# the name of the shell on the box
shl="$bb sh"
# the name of vendor utility to "spy" into files
testvalue="/bin/testvalue"
# the name of the ring buffer utility from the vendor
debugoutput="/bin/showshringbuf -i \$scriptname"
## the name of the ring buffer utility from the vendor
debugtest="/bin/showshringbuf -?"
# our debug ring buffer file name
ringbufferfile="/var/.srb_\$scriptname"
# our custom modscript selection file name
custom_selection_file="custom_modscripts"
#
# some more values which have to be changed, if AVM modifies its firmware
#
# 128MB ext3 image plus 10 percent
free_space_for_unpack="285M"
# 48 MB for packed and double space for unpacked image
free_space_for_unpack_tmpfs="512M"
# even with a small amount of RAM at all, there should be a minimum of free space
free_space_at_tmpfs="128M"
# that's really low on space, better restart the box again
warning_space_at_tmpfs="64M"
# one more time the space needed for our ext3 container + 5%
free_space_at_nand="285M"
# the estimated space needed to store an extracted squashfs image at NAND, it's hard
# to predict a correct value, because the available 48 MB of a filesystem partition
# may contain a much much larger "expanded" structure due to automatic avoidance of
# compression for identical files - so we use a value usually large enough to contain
# up to four different brandings
warning_space_at_nand="192M"
# the estimated space needed to download a firmware image from the FTP server,
# computed as filesystem partition size + kernel partition size + security surcharge
# for additional files in the tar archive - currently the images are uncompressed
download_space_needed="128M"
# the estimated space needed to extract an outer file system from a firmware image,
# the extracted tree may be be larger than the size of a filesystem partition
extract_space_needed="48M"
# the estimated space needed to store a kernel image, an outer image (if used) and the
# extracted SquashFS image - at least during the modify_rootfs execution,
# computed as kernel partition size + 2x filesystem partition size, because it's
# possible, that (in worst case) the (outer) ext2 image with the pseudo SquashFS header
# exists together with the "cutted" image (during "dd" execution) for a short time,
# the extracted SquashFS image will be smaller than the second ext2 copy
fullimage_space_needed="256M"
# needed swap space at all (MB)
swap_space_needed="512"
# the "normal" base for temporary files and directories
tmpfsbasedir="/var/tmp"
# known native file system types, we can use them without a container
nativefilesystems="tmpfs ext2 ext3 ext4 yaffs2"
#
# error codes
#
# 32 - different MD5 hashes for kernel MTDs
# 33 - unable to find kernel MTD
# 34 - unable to find alternative kernel MTD
# 35 - not used anymore, was missing linux_fs_start at environment
# 36 - unsupported hardware revision
# 37 - system modified already, restart needed
# 38 - specified squashfs source file not found
# 39 - unable to extract outer image from firmware archive
# 40 - unable to mount outer file system
# 41 - unable to copy root image from outer filesystem
# 42 - unable to download firmware image from manufacturer
# 43 - error downloading firmware image (file size is 0)
# 44 - error copying kernel image to alternative partition
# 45 - error copying outer file system to alternative partition
# 46 - unknown error copying running system to inactive partitions
# 47 - unable to get hardware revision
# 48 - unable to find file system MTD partition
# 49 - unable to find alternative file system MTD partition
# 50 - not enough free space available at tmpfs
# 51 - unable to find free space for unpacking of squashfs
# 52 - unable to find free space for squashfs extraction from image
# 53 - error saving permanent copy of source squashfs image
# 54 - error unpacking loopback device image
# 55 - error mounting loopback device image
# 56 - error unmounting loopback device image
# 57 - error removing loopback device image
# 58 - not enough free space on any storage volume
# 60 - wrong modscript detected, missing needed comment lines
# 61 - wrong modscript detected, missing header at all
# 62 - error extracting kernel image from firmware image file
# 66 - the message file was not found after fallback to 'en'
# 127 - internal error
# 187 - unable to detect the current firmware version on the FTP server
# 190 - no newer version found for "update" mode without local source file
# 191 - error comparing version numbers (e.g. wrong format of them)
# 200 - input image is missing or invalid
# 202 - unspecified error mounting an ext2 image to extract data from it
# 205 - a downloaded image file did not pass the integrity check (signature)
# 222 - error detecting filesystem type of an image file
#
# our "internal" constants
#
modfs_version=0.8.2-140120231234
modfs_comment="please look at https://github.com/PeterPawn/modfs for further info"
bindirname="bin"
localedirname="locale"
filesdirname="files"
scriptdirname="modscripts"
contribdirname="contrib"
tmpdirbase="/var/tmp"
packedpartition="256MB_ext3.gz"
squashfsdirname="squashfs-root"
scriptwrapper="wrap_script"
modfsshelldebug="/var/tmp/modfs_debug_shell.log"
scriptsdir="bin/scripts"
getversionvaluesscript="$scriptsdir/extract_version_values"
showprogress=0
msgtext_clearscreen="\x1B[2J\x1B[0;0H"
msgtext_normal="\x1B[0m"
msgtext_bold="\x1B[1m"
msgtext_error="\x1B[1;31m"
msgtext_ok="\x1B[1;32m"
msgtext_warning="\x1B[1;33m"
msgtext_highlight="\x1B[1;34m"
msgtext_CR="\r\x1B[K"
#
# name and relative path of squashfs tools
#
sq_version=4 # default version
sq_pack="\$(bindir)/mksquashfs\${sq_version}"
sq_unpack="\$(bindir)/unsquashfs\${sq_version}"
#
# universal functions
#
# 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()
{
local item i=0
for item in $2; do
let i+=1
[ x"$1" == x"$item" ] && echo $i && return 0
done
return 1
}
#
# get binaries subdirectory name
#
bindir() { echo "$scriptpath/$bindirname/$(get_hardware_revision)"; }
#
# get scripts subdirectory name
#
scriptdir() { echo "$scriptpath/$scriptsdir"; }
#
# get locale subdirectory name
#
localedir() { echo "$scriptpath/$localedirname"; }
#
# get files subdirectory name
#
filesdir() { echo "$scriptpath/$filesdirname"; }
#
# get contributed content subdirectory name
#
contribdir() { echo "$scriptpath/$contribdirname"; }
#
# remove temporary file
# $1 - file name
#
remove_temp_file()
{
local rc
[ -z "$1" ] && return 127
[ ${#1} -lt 10 ] && return 127
$bb rm "$1" 2>/dev/null
rc=$?
$bb sed -e "\|^rm *$1\$|d" -i $tempfilelist 2>/dev/null
debug "remove_temp_file: file=$1, rc=$rc"
return 0
}
#
# remove directory and its reference from cleanup list too
# $1 - directory to remove
#
remove_directory()
{
local dir="$1" rc
[ ${#1} -lt 10 ] && return 127
$bb rm -r $dir 2>/dev/null
rc=$?
$bb sed -e "\|^rm -r *$dir\$|d" -i $tempfilelist 2>/dev/null
debug "remove_directory: directory=$1, rc=$rc"
return 0
}
#
# get temporary directory name
#
get_temp_dir()
{
local name="$tmpdirbase/$$_$($bb date +%s)"
$bb mkdir -p "$name"
echo "$bb rm -r $name" >>$tempfilelist
debug "get_temp_dir: directory=$name"
echo "$name"
return 0
}
#
# get temporary file name
# $1 - optional base directory name
#
get_temp_file()
{
local name="$1" dir out
dir="$(get_temp_dir)"
[ -z "$name" ] && out="$dir/$($bb date +%s)" || out="$dir/$name"
echo "$bb rm $out" >>$tempfilelist
debug "get_temp_file: file=$out"
echo "$out"
return 0
}
#
# write to debug ring buffer
# stdin -> lines to append to the buffer
#
debug()
{
[ $MODFS_DEBUG -ne 1 ] && return
echo -e "$@" | $bb sed -e ':x;$!N;s/\n/{LF}/;tx' | $debugoutput
}
#
# get next system index from environment
#
get_system_switch()
{
local fsvar
fsvar=$($bb sed -n -e "s/^$fsvarname\t\([01]\)/\1/p" $envpathname)
[ -z "$fsvar" ] && fsvar="0"
echo $fsvar
}
#
# get hardware revision from environment (it has not to be a number !)
#
get_hardware_revision() { $bb sed -n -e "s/^$hwrevname\t\(.*\)/\1/p" $envpathname; }
#
# check, if the specified hardware revision is supported
# $1 - hardware revision
#
check_hardware_revision() { return $(index_of_item "$1" "$hwrevs_supported"); }
#
# check MTD "name"
# $1 - MTD index number
# $2 - expected MTD name from /proc/mtd
#
check_mtd() { $bb grep "$mtdprefix$1: [0-9a-f]* [0-9a-f]* \"\($reservedprefix\)*$2\"" $procmtd; }
#
# get MTD number by name
# $1 - requested MTD partition name
#
get_mtd_by_name() { $bb sed -n -e "s|$mtdprefix\([0-9]\{1,2\}\): [0-9a-f]\{8\} [0-9a-f]\{8\} \"$1\"|\1|p" $procmtd; }
#
# get MTD name by number
# $1 - requested MTD partition number
#
get_mtd_by_number() { $bb sed -n -e "s|$mtdprefix$1: [0-9a-f]\{8\} [0-9a-f]\{8\} \"\(.*\)\"|\1|p" $procmtd; }
#
# build MTD partition groups
#
build_partitions()
{
local g1 g2
g1=$(get_mtd_by_name $kernelname):$(get_mtd_by_name $filesystemname)
g2=$(get_mtd_by_name $reservedprefix$kernelname):$(get_mtd_by_name $reservedprefix$filesystemname)
if [ ${g1:0:1} -lt ${g2:0:1} ]; then
echo "$g1 $g2"
else
echo "$g2 $g1"
fi
}
#
# convert string to upper case
# $@ - string(s) to convert
#
uppercase() { echo "$@" | $bb tr 'abcdefghijklmnopqrstuvwxyzäöü' 'ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ'; }
#
# escape a string to use it within a regular expression without any hassle
#
escape_regexp() { echo "$@" | $bb sed -e 's/\./\\./g'; }
#
# get localized strings (to make it easier for german users)
# - the message text may contain the macros '%{error}', '%{ok}', '%{warning}' and
# '%{normal}' to change text color
# - to insert variable parts into the text, the 'printf' command will be used with
# the specified parameters (shifted two times to remove our own arguments)
# $1 - language index (ISO 639-1 code)
# $2 - message id
# $3-$n - values to substitute
#
get_localized()
{
local lang=$1 lcfile=$(localedir)/$lang msgno=$2 rc=0 msg
shift 2
msg="$($bb sed -n -e "/^$msgno=/s/^[0-9]*=\(.*\)/\1/p" $lcfile | $bb sed -e "s/%{error}/\${msgtext_error}/g" | $bb sed -e "s/%{ok}/\${msgtext_ok}/g" | $bb sed -e "s/%{warning}/\${msgtext_warning}/g" | $bb sed -e "s/%{bold}/\${msgtext_bold}/g" | $bb sed -e "s/%{normal}/\${msgtext_normal}/g" | $bb sed -e "s/%{highlight}/\${msgtext_highlight}/g")"
if [ "${#msg}" = "0" ]; then
echo "The specified message number $msgno was not found at the language file for '$lang'." 1>&2
rc=1
else
eval "printf \"$msg\" "$@""
fi
return $rc
}
#
# get a yes or no decision from the user
# $1 - default answer to assume, if only return is pressed
# $2 - the question to display first
# - to avoid a default answer (return key), use any other default than 'y' or 'n'
#
ask_yes_or_no()
{
local def=$1 yes no p_yes p_no answer nl prompt="$2"
yes=$(get_localized $lang 129)
no=$(get_localized $lang 130)
cancel=$(get_localized $lang 227)
p_yes=$yes
p_no=$no
p_cancel="$(get_localized $lang 228)"
if [ $def == n ]; then
p_no=$(uppercase $no)
else
if [ $def == y ]; then
p_yes=$(uppercase $yes)
fi
fi
# 1 -eq 1 is better than 'true', because 'true' is not an ash builtin in every case
debug "ask_yes_or_no: Q=$prompt"
while [ 1 -eq 1 ]; do
while read -n 10 -s -t 1; do :; done
echo -ne "$prompt" 1>&2
read -n 1 -s -p " ($p_yes/$p_no/$p_cancel) " answer 1>&2
nl="$answer\n"
[ ${#answer} -eq 0 ] && answer=$def && nl="\n"
debug "ask_yes_or_no: A=$answer"
if [ "$answer" == $(uppercase $yes) -o $answer == $yes ]; then
echo -n 'Y'
echo -e -n "$nl" 1>&2
return 0
elif [ "$answer" == $(uppercase $no) -o $answer == $no ]; then
echo -n 'N'
echo -e -n "$nl" 1>&2
return 0
elif [ "$answer" == $(uppercase $cancel) -o $answer == $cancel ]; then
echo -n 'C'
echo -e -n "$nl" 1>&2
return 1
else
echo -e "$selection\n$(get_localized $lang 128)" 1>&2
fi
done
return 1
}
#
# show progress messages on console
# - output will be written to stderr
# $1 - message type
# 1 - CR + show text without LF
# 2 - show text without LF
# 3 - show text with LF
# 4 - show text with LF in front of it and at the end
# $2 - message id to get localized text version
#
progress()
{
local msg mode=$1
shift
msg="$(get_localized $lang "$@")"
if [ $mode -eq 1 ]; then
echo -ne "$msgtext_CR$msg" 1>&2
else
if [ $mode -eq 2 ]; then
echo -ne "$msg" 1>&2
else
if [ $mode -eq 3 ]; then
echo -ne "$msg\n" 1>&2
else
if [ $mode -eq 4 ]; then
echo -ne "\n$msg\n"
fi
fi
fi
fi
debug "progress: mode=$mode, msg=$msg"
return
}
#
# check next system against running
#
is_switched()
{
local nxt kernel filesys kname fname switched=1
nxt=$(get_system_switch)
set -- $(build_partitions)
[ $nxt -gt 0 ] && shift
kernel=${1%:*}
filesys=${1#*:}
kname="$(get_mtd_by_number $kernel)"
fname="$(get_mtd_by_number $filesys)"
[ "${kname:0:${#reservedprefix}}" == "$reservedprefix" ] && switched=0
return $switched
}
#
# compare kernel checksums to make sure, the systems are identical
# $1 and $2 have to be the MTD numbers of the two kernel images
#
check_kernels()
{
local rc=0
if [ ${#1} -eq 0 -o ${#2} -eq 0 ];then
rc=127
else
hash0=$($bb md5sum /dev/$mtdblockname$1 | $bb sed -n -e 's/^\([0-9a-f]*\).*/\1/p')
hash1=$($bb md5sum /dev/$mtdblockname$2 | $bb sed -n -e 's/^\([0-9a-f]*\).*/\1/p')
debug "check_kernels: /dev/$mtdblockname$1 = $hash0, /dev/$mtdblockname$2 = $hash1"
if [ $hash0 != $hash1 ]; then
rc=32
fi
fi
return $rc
}
#
# call mksquashfs utility
#
sq_mksquashfs()
{
local rc=0 binary
binary="$(eval echo $sq_pack)"
debug "sq_mksquashfs: $binary $@"
$binary "$@"
rc=$?
debug "sq_mksquashfs: exiting, rc=$rc"
return $rc
}
#
# call unsquashfs utility
#
sq_unsquashfs()
{
local rc=0 binary
binary="$(eval echo $sq_unpack)"
debug "sq_unsquashfs: $binary $@"
$binary "$@"
rc=$?
debug "sq_unsquashfs: exiting, rc=$rc"
return $rc
}
#
# switch system selection variable to other value
#
switch_system()
{
local newval
newval=$(( ( $(get_system_switch) + 1 ) % 2 ))
echo $fsvarname $newval >$envpathname
debug "switch_system: switched to $newval"
}
#
# 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()
{
local next
next=$(get_system_switch)
debug "switch_system_to: $1"
if is_switched; then
if [ x"$1" == xcurrent ]; then
switch_system
fi
else
if [ x"$1" == xalternative ]; then
switch_system
fi
fi
return 0
}
#
# detect filesystem image type
# types supported: squashfs3, squashfs4, ext2, sqfs_dummy256_{type}, with "-de" suffix if different endianess detected
# $1 - source image
#
detect_image_filesystem()
{
local src="$1" rc=222 fstype major sig offset=${2:-0}
# do not use blkid anymore to avoid confusions from different versions
# instead we'll probe at different offsets to detect/guess the right type
[ -f "$src" ] || return $rc
sig=$($testvalue "$src" 4 $offset)
[ $? -ne 0 ] && return $rc
major=$($testvalue "$src" 2 $(( offset + 28 )))
[ $? -ne 0 ] && return $rc
if [ $sig == 1936814952 ]; then
# sqsh signature found at offset 0
if [ $major -ne 0 ]; then
fstype="squashfs$major"
else
if [ $offset -eq 0 ]; then
# recursive call with offset 256
sig="$(detect_image_filesystem "$src" 256)"
if [ $? -eq 0 ]; then
fstype="sqfs_dummy256_$sig"
fi
fi
fi
elif [ $sig == 1752396147 ]; then
# different endian signature found at offset 0
if [ $major -ne 0 ]; then
fstype="squashfs${major}-de"
fi
else
sig=$($testvalue "$src" 2 $(( offset + 1080 )))
if [ $sig -eq 21487 ]; then
fstype="ext2"
fi
fi
[ ${#fstype} -gt 0 ] && rc=0
echo $fstype
debug "detect_image_filesystem: src=$src, offset=$offset, fstype=$fstype, rc=$rc"
return $rc
}
#
# copy kernel image to alternative partition
# $1 - optional file name of the new kernel image - if omitted, the current kernel will be copied
#
copy_kernel_image()
{
local src="$1" target rc
if [ ${#src} -eq 0 ]; then
src=$(get_mtd_by_name $kernelname)
src="/dev/$mtdprefix$src"
fi
target=$(get_mtd_by_name $reservedprefix$kernelname)
debug "copy_kernel_image: src=$src, target=/dev/$mtdprefix$target"
$update_kernel_binary -i "$src" -o /dev/$mtdprefix$target >/dev/null 2>&1
rc=$?
debug "copy_kernel_image: exiting, rc=$rc"
return $rc
}
#
# copy outer file system to alternative partition
#
copy_filesystem()
{
local tmp target rc
target=$(get_mtd_by_name $reservedprefix$filesystemname)
$update_kernel_binary -o /dev/$mtdprefix$target >/dev/null 2>&1
debug "copy_filesystem: /dev/$mtdprefix$target cleared"
tmp=$(get_temp_dir)
$bb mount -t yaffs2 /dev/$mtdblockname$target "$tmp"
$bb cp -R $wrapperdir/* $tmp/
rc=$?
debug "copy_filesystem: copied everything from $tmp, rc=$rc"
$bb umount "$tmp"
remove_directory "$tmp"
debug "copy_filesystem: exiting, rc=$rc"
return $rc
}
#
# copy running system to alternative partitions
#
copy_running_system()
{
local rc
# prepare MTD writes
# notify power management
echo MODE=update > /dev/avm_power
# prevent unwanted watchdog restarts, may be necessary but update is very fast
# echo "disable" > /dev/watchdog
copy_kernel_image
rc=$?
if [ $rc -ne 0 ]; then
rc=44
else
copy_filesystem
rc=$?
if [ $rc -ne 0 ]; then
rc=45
fi
fi
debug "copy_running_system: exiting, rc=$rc"
return $rc
}
#
# copy new outer file system
# $1 - source file system image
# $2 - root file system image name to be excluded from copying
#
copy_outer_filesystem()
{
local rc target src="$1" tmp fstype mrc=0 kernelversion
target=$(get_mtd_by_name $reservedprefix$filesystemname)
# prepare MTD writes
# notify power management
echo MODE=update > /dev/avm_power
# prevent unwanted watchdog restarts, may be necessary but update is very fast
# echo "disable" > /dev/watchdog
# clear filesystem first
debug "copy_outer_filesystem: src=$src, rootfs=$rootfsname"
$update_kernel_binary -o /dev/$mtdprefix$target >/dev/null 2>&1
rc=$?
debug "copy_outer_filesystem: clearing target device /dev/$mtdprefix$target, rc=$rc"
if [ $rc -ne 0 ]; then
rc=44
else
fstype=$(detect_image_filesystem "$src")
rc=$?
if [ $rc -eq 0 ]; then
# mount squashfs and yaffs2 partition and copy from one to the other
tmp="$(get_temp_dir)"
$bb mkdir -p "$tmp/yaffs" "$tmp/outerfs"
$bb mount -t yaffs2 /dev/$mtdblockname$target "$tmp/yaffs"
if [ "$fstype" == "ext2" -o "$fstype" == "sqfs_dummy256_ext2" ]; then
# ext2 image with AVM's dummy header
mount_ext2_image "$src" "$tmp/outerfs" "$fstype"
rc=$?
mrc=$rc
else
$bb mount "$src" "$tmp/outerfs" 2>/dev/null
rc=$?
mrc=$rc
if [ $rc -ne 0 ]; then
# try to extract with unsquashfs3, if this is a 3.xx kernel based
# system without knowledge about SquashFS3 format while mounting
kernelversion=$($bb uname -r)
kernelversion=${kernelversion%%.*}
if [ $kernelversion -eq 3 -a x"$fstype" == x"squashfs3" ]; then
unpack_squashfs "$src" "$tmp" 2>&1 >/dev/null
rc=$?
if [ $rc -eq 0 ]; then
$bb rmdir "$tmp/outerfs"
$bb mv "$tmp/$squashfsdirname" "$tmp/outerfs"
rc=$?
$bb rm "$tmp/outerfs/$rootfsname"
fi
fi
fi
fi
if [ $rc -eq 0 ]; then
$bb tar -c -O -C "$tmp/outerfs" --exclude=$rootfsname . | $bb tar -x -C "$tmp/yaffs"
rc=$?
debug "copy_outer_filesystem: copying done, rc=$rc"
if [ -n "$ADD_TO_WRAPPER" ]; then
ADD_TO_WRAPPER="$(eval printf "%s" "$ADD_TO_WRAPPER")"
if [ -d "$ADD_TO_WRAPPER" ]; then
debug "copy_outer_filesystem: subdirectory '$ADD_TO_WRAPPER' exists, copying content to the new wrapper partition"
cp -a $ADD_TO_WRAPPER/* $tmp/yaffs/
rc=$?
debug "copy_outer_filesystem: add_to_wrapper directory '$ADD_TO_WRAPPER' copied, rc=$rc"
else
debug "copy_outer_filesystem: add_to_wrapper directory '$ADD_TO_WRAPPER' not found"
fi
fi
[ $mrc -eq 0 ] && $bb umount -d "$tmp/outerfs"
$bb umount "$tmp/yaffs"
remove_directory "$tmp"
rc=0
fi
fi
fi
debug "copy_outer_filesystem: exiting, rc=$rc"
return $rc
}
#
# copy new root file system to alternative YAFFS2 partition
# $1 - file name of new root file system image
#
copy_new_root_filesystem()
{
local src="$1" target tmp
target=$(get_mtd_by_name $reservedprefix$filesystemname)
tmp="$(get_temp_dir)"
debug "copy_new_root_filesystem: src=$src, target=/dev/$mtdblockname$target"
$bb mount -t yaffs2 /dev/$mtdblockname$target "$tmp"
$bb cp -a "$src" "$tmp/$rootfsname"
rc=$?
$bb umount "$tmp"
remove_directory "$tmp"
debug "copy_new_root_filesystem: exiting, rc=$rc"
return $rc
}
#
# get squash file system block size
# $1 - source file system name
#
get_squashfs_blocksize()
{
local bs sq_version
sq_version=$(get_squashfs_tools_version "$1")
debug "get_squashfs_blocksize: using SquashFS version $sq_version"
bs=$(sq_unsquashfs -s "$1" 2>/dev/null | $bb sed -n -e "s/^Block size \(.*\)/\1/p")
debug "get_squashfs_blocksize: block size=$bs"
echo $bs
}
#
# get squash file system block size
# $1 - source file system name
#
get_squashfs_endianess()
{
local endianess=0 endian sq_version
sq_version=$(get_squashfs_tools_version "$1")
debug "get_squashfs_endianess: using SquashFS version $sq_version"
endian=$(sq_unsquashfs -s "$1" 2>/dev/null | $bb sed -n -e "s/Found a valid \(big\|little\) endian SQUASHFS.*/\1/p")
[ ${#endian} -gt 0 ] && [ x"$endian" == xlittle ] && endianess=1
debug "get_squashfs_endianess: $endian endian"
echo $endianess
return 0
}
#
# unpack squash file system
# $1 - source file system name
# $2 - target directory (has to exist)
# $3 - optional file with list of names to unpack
#
unpack_squashfs()
{
local src="$1" target="$2" files="$3" popd rc sq_version
if [ -r "$src" ]; then
sq_version=$(get_squashfs_tools_version "$src")
debug "unpack_squashfs: using SquashFS version $sq_version"
[ -z "$files" ] || files="-e $files"
popd="$(pwd)"
cd "$target"
sq_unsquashfs -info "$src" $files
rc=$?
cd "$popd"
else
rc=38
fi
debug "unpack_squashfs: exiting, rc=$rc"
return $rc
}
#
# pack squash file system image from source tree
# $1 - source directory
# $2 - target file name
# $3 - endianess (0 - BE, 1 - LE)
# $4 - block size
#
pack_squashfs()
{
local src="$1" target="$2" endian blocksize="$4" popd rc sq_version=$5
if [ $sq_version -eq 3 ]; then
[ $3 -eq 0 ] && endian="-be" || endian="-le"
else
endian=""
fi
popd="$(pwd)"
debug "pack_squashfs: using SquashFS version $sq_version"
cd "$src"
echo $modfs_version > squashfs-root/etc/.modfs_version
sq_mksquashfs squashfs-root "$target" -info -b $blocksize $endian -force-uid 0 -force-gid 0
rc=$?
cd "$popd"
debug "pack_squashfs: exiting, rc=$rc"
return $rc
}
#
# check, if an option is supported
# $1 - option to look for
# $2 - options from script
is_supported()
{
local option="$1" options="$2" opt rc=0
for opt in $options; do
curr="$opt"
if [ $(expr index "\(" "$opt") -gt 0 ]; then
opt="${opt%%(*)}"
fi
if [ x"$opt" == x"$option" ]; then
echo "$curr"
rc=1
break
fi
done
debug "is_supported: option=$option, from=$options, rc=$rc"
return $rc
}
#
# get version values from unpacked image
# $1 - target directory
#
extract_values_from_image()
{
$bb sh $getversionvaluesscript "$1" -m MODFS_
}
#
# get the kernel version from target system
# $1 - target directory
get_target_kernel_version()
{
local rootdir="$1"
local rc=0
local version
version="$(extract_values_from_image "$rootdir" | $bb sed -n -e "s|^MODFS_KernelVersion=\"\(.*\)\"\$|\1|p")"
debug "get_target_kernel_version: result=\"$version\", rc=$rc"
echo "TARGET_KERNEL_VERSION=\"$version\""
}
#
# get the FRITZ!OS version from target system
# $1 - target directory
get_target_system_version()
{
local rootdir="$1"
local rc=0
local version
local subversion
local date
local values
values="$(extract_values_from_image "$rootdir")"
version="$(printf "%s\n" "$values" | $bb sed -n -e "s|^MODFS_Version=\"\(.*\)\"\$|\1|p")"
subversion="$(printf "%s\n" "$values" | $bb sed -n -e "s|^MODFS_Subversion=\"\(.*\)\"\$|\1|p")"
date="$(printf "%s\n" "$values" | $bb sed -n -e "s|^MODFS_Date=\"\(.*\)\"\$|\1|p")"
debug "get_target_system_version: version=\"$version\", subversion=\"$subversion\", date=\"$date\", rc=$rc"
echo "TARGET_SYSTEM_VERSION=\"$version\" TARGET_SYSTEM_SUBVERSION=\"$subversion\" TARGET_SYSTEM_DATE=\"$date\""
}
#
# get the FRITZ!OS brandings from target system
# $1 - target directory
get_target_brandings()
{
local rootdir="$1"
local rc=0
local brandings
brandings="$(extract_values_from_image "$rootdir" | $bb sed -n -e "s|^MODFS_Brandings=\"\(.*\)\"\$|\1|p")"
debug "get_target_brandings: brandings=\"$brandings\", rc=$rc"
printf "%s\n" "$brandings"
}
#
# get the FRITZ!OS brandings from target system
# $1 - target directory
get_first_branding()
{
local brandings="$1"
for branding in $brandings; do
debug "get_first_branding: brandings=\"$brandings\", branding=\"$branding\", rc=$rc"
printf "%s\n" "$branding"
return
done
}
#
# execute a modscript without further user interaction
# $1 - script name
# $2 - root file system directory
# $3 - mode (auto / onrequest)
#
execute_modscript()
{
local script="$1" scriptname="${script##*/}" rootdir="$2" mode="$3" rc=0 supports name language uselang languages header opt msg
debug "execute_modscript: script=$script, root=$rootdir, mode=$mode"
header="$($bb sed -n -e "1p" "$script")"
if [ x"$header" == x"# MODFS_MODSCRIPT" ]; then
supports="$($bb sed -n -e "s/^# SUPPORTS \(.*\)/\1/p" "$script")"
name="$($bb sed -n -e "s/^# NAME \(.*\)/\1/p" "$script")"
name="${name// /\\ }"
if [ ${#name} -gt 0 -a ${#supports} -gt 0 ]; then
if [ x"$mode" == xauto ]; then
echo -e "$(get_localized $lang 172 "$name")" 1>&2
progress 1 173
fi
language="$(is_supported 'language' "$supports")"
if [ $? -eq 1 ]; then
languages="${language##language(}"
languages="${languages%)}"
uselang=$(IFS=, set -- $languages; for lng in $@; do [ $lng == $lang ] && echo $lng && break; done)
if [ ${#uselang} -eq 0 ]; then
[ x"$mode" == xauto ] && progress 3 174
uselang=en
else
[ x"$mode" == xauto ] && progress 3 96
fi
else
[ x"$mode" == xauto ] && progress 3 174
uselang=en
fi
if [ x"$mode" == xauto -o x"$mode" == xprecheck ]; then
rc=0
progress 1 175
opt=$(is_supported 'precheck' "$supports")
if [ $? -eq 1 ]; then
# execute pre check
msg="$(eval export $MODSCRIPT_ENV;$shl $scriptwrapper $script $uselang $rootdir $mode precheck)"
rc=$?
if [ $rc -gt 0 ]; then
progress 3 177 $rc
echo -e "${msgtext_bold}$msg${msgtext_normal}" 1>&2
else
progress 3 96
[ ${#msg} -gt 0 ] && echo -e "${msgtext_bold}$msg${msgtext_normal}" 1>&2
fi
else
progress 3 176
fi
if [ x"$mode" == xprecheck ]; then
return $rc
fi
else
rc=0
fi
if [ $rc -eq 0 ]; then
progress 1 178
msg="$(eval export $MODSCRIPT_ENV;$shl $scriptwrapper $script $uselang $rootdir $mode install)"
rc=$?
if [ $rc -gt 0 ]; then
progress 3 177 $rc
echo -e "${msgtext_bold}$msg${msgtext_normal}" 1>&2
else
progress 3 96
[ ${#msg} -gt 0 ] && echo -e "${msgtext_bold}$msg${msgtext_normal}" 1>&2
progress 1 179
opt=$(is_supported 'postcheck' "$supports")
if [ $? -eq 1 ]; then
# execute post check
msg="$(eval export $MODSCRIPT_ENV;$shl $scriptwrapper $script $uselang $rootdir $mode postcheck)"
rc=$?
if [ $rc -gt 0 ]; then
progress 3 177 $rc
echo -e "${msgtext_bold}$msg${msgtext_normal}" 1>&2
else
progress 3 96
[ ${#msg} -gt 0 ] && echo -e "${msgtext_bold}$msg${msgtext_normal}" 1>&2
fi
else
progress 3 176
fi
fi
fi