forked from HaleBob/freetz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fwmod
executable file
·1539 lines (1320 loc) · 57.1 KB
/
fwmod
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/bash
# Freetz script to modify AVM FRITZ!Box and OEM firmware images
#
# Copyright (C) 2005-2006 Daniel Eiband <eiband@online.de>
# Copyright (C) 2006-2012 by the Freetz developers (http://freetz.org)
#
# Licensed under the GPL v2, see the file COPYING in this tarball.
#
# This script is based on Christian Volkmann's fritzbox mod-0.57.
# Legacy URL: http://www.ip-phone-forum.de/showthread.php?t=65894
#
# Special thanks to Andreas Buehmann, Christian Volkmann, Enrik Berkhan,
# SpeedyBZ and all members on ip-phone-forum.de who contributed to this mod.
usage()
{
cat << EOF
Usage: $SELF [-u|-m|-p|-a] [-i <cfg>] [-d <dir>] <orig_fw> [<tk_fw>]
actions
-u unpack firmware image
-m modify previously unpacked image
-p pack firmware image
-a all: unpack, modify and pack firmware image (-u -m -p, default)
special actions
-n firmware-nocompile: do not install kernel and busybox
-f force pack even if image is too big for flash (AVM SDK)
-z zip file system into archive for USB/NFS root
-c <dir> copy file system to target directory for NFS/USB root (implies -z)
input/output
-i <cfg> input file for configuration data (default: .config)
-d <dir> build directory (default: <orig_firmware>.mod)
<orig_fw> original firmware name
<tk_fw> 2nd firmware name (e.g. for merging in web UI)
EOF
}
##################################################
## Initialise and check command line parameters ##
##################################################
# This script's name ("fwmod" if not linked/renamed)
SELF="$(basename "$0")"
# No parameters -> print usage info to stdout
[ $# -eq 0 ] && usage && exit 0
# Backup command line parameters [$0..$n] to save them from getopts eating them
# away, because we need them again later for the recursive fakerooted call.
for ((i=0; i<=$#; i++)); do
CMDLINE_ORIG[i]="${!i}"
done
# Default values for unset command line parameters
DOT_CONFIG="$(dirname "$0")/.config"
DO_UNPACK=0; DO_MOD=0; DO_PACK=0; FORCE_PACK=0; DO_ZIP=0; _OPT=0
COPY_FS_DIR=; DIR=
# Parse command line parameters
while getopts umpanfzc:i:d: opt; do
case "$opt" in
u) DO_UNPACK=1; _OPT=1 ;;
m) DO_MOD=1; _OPT=1 ;;
p) DO_PACK=1; _OPT=1 ;;
a) DO_UNPACK=1; DO_MOD=1; DO_PACK=1; _OPT=1 ;;
n) FIRMWARE_NOCOMPILE=1 ;;
f) FORCE_PACK=1 ;;
z) DO_ZIP=1 ;;
c) COPY_FS_DIR="$OPTARG"; [ "$COPY_FS_DIR" ] && DO_ZIP=1 ;;
i) DOT_CONFIG="$OPTARG" ;;
d) DIR="$OPTARG" ;;
*) usage >&2; exit 1 ;;
esac
done
shift $((OPTIND-1))
# At least one (non-empty) firmware image name must be specified
[ $# -ne 1 -a $# -ne 2 -o ! "$1" ] && usage >&2 && exit 1
# Freetz base + tools directories
BASE_DIR="$(dirname "$0")"
ABS_BASE_DIR="$(readlink -f "$BASE_DIR")"
TOOLS_DIR="${ABS_BASE_DIR}/tools"
# Include common helper functions (isFreetzType, echo*, error, modunsqfs* etc.)
. "${TOOLS_DIR}/freetz_functions" ||
(echo "cannot find script freetz_functions" >&2; exit 1)
# Initialise firmware image names
FIRMWARE="$1"
FIRMWARE2="$2"
FIRMWARE_EXTENSION="${FIRMWARE##*.}"
FIRMWARE_EXTENSION="${FIRMWARE_EXTENSION,,}"
# Given firmware image(s) must exist
[ -r "$FIRMWARE" ] ||
error 1 "firmware image $(basename "$FIRMWARE") not found"
[ "$FIRMWARE2" -a ! -r "$FIRMWARE2" ] &&
error 1 "firmware image $(basename "$FIRMWARE2") not found"
# Config file must exist
[ -r "$DOT_CONFIG" ] ||
error 1 "not configured"
# No -u/-m/-p/-a options = default = -a = -ump
[ "$_OPT" -eq 0 ] && DO_UNPACK=1 && DO_MOD=1 && DO_PACK=1
# Set default output directory, if not specified on command line
: ${DIR:=$FIRMWARE.mod}
# Create output directory if necessary
mkdir -p "$DIR"
#################################
## Initialise script variables ##
#################################
# Relative subdirectories
SOURCE_SUBDIR="source"
FIRMWARE_SUBDIR="firmware"
FILESYSTEM_SUBDIR="filesystem"
FILESYSTEM_CORE_SUBDIR="filesystem_core"
AVMPLUGINS_SUBDIR="plugins.image"
EXTERNAL_SUBDIR="external"
KERNEL_SUBDIR="kernel"
VARTAR_SUBDIR="var.tar"
HTML_SUBDIR="usr/www"
GRAPHICS_SUBDIR="graphics"
# Kernel/SquashFS images contained in firmware
KERNEL_IMAGE="var/tmp/kernel.image"
FILESYSTEM_IMAGE="var/tmp/filesystem.image"
FILESYSTEM_CORE_IMAGE="filesystem_core.squashfs"
# More files created during firmware unpacking
AVMPLUGINS_FILE="var/tmp/plugins.update"
RAW_KERNEL_FILE="kernel.raw"
RAW_HIDDEN_FILE="kernelsquashfs.raw"
VARTAR_FILE="var.tar"
UNPACKED_FILE="$DIR/.unpacked"
# Path to bash (shell required by fwmod)
SHELL=$BASH
# Tools used by fwmod
FINDSQUASHFS_TOOL="find-squashfs"
TICHKSUM_TOOL="tichksum"
RMTICHKSUM_TOOL="rmtichksum"
MAKEDEVS_TOOL="makedevs"
UNSQUASHFS_TOOL="unsquashfs3"
UNSQUASHFS_LZMA_TOOL="unsquashfs3-lzma"
EXTERNAL_TOOL="external"
TAR_TOOL="tar"
MD5SUM_TOOL="md5sum"
BUSYBOX_TOOL="busybox"
# Options for (un)packing SquashFS
MKSQUASHFS_OPTIONS="-noappend -all-root -info"
UNSQUASHFS_OPTIONS="-no-progress"
# Absolute tools paths
EXTERNAL="${TOOLS_DIR}/${EXTERNAL_TOOL}"
UNSQUASHFS="${TOOLS_DIR}/${UNSQUASHFS_TOOL}"
UNSQUASHFS_LZMA="${TOOLS_DIR}/${UNSQUASHFS_LZMA_TOOL}"
FINDSQUASHFS="${TOOLS_DIR}/${FINDSQUASHFS_TOOL}"
MKSQUASHFS="${TOOLS_DIR}/${MKSQUASHFS_TOOL}"
MKSQUASHFS_LZMA="${TOOLS_DIR}/${MKSQUASHFS_LZMA_TOOL}"
TICHKSUM="${TOOLS_DIR}/${TICHKSUM_TOOL}"
RMTICHKSUM="${TOOLS_DIR}/${RMTICHKSUM_TOOL}"
MAKEDEVS="${TOOLS_DIR}/${MAKEDEVS_TOOL}"
MAKEDEVS_FILE="${TOOLS_DIR}/device_table.txt"
TAR="${TOOLS_DIR}/${TAR_TOOL}"
MD5SUM="${TOOLS_DIR}/${MD5SUM_TOOL}"
BUSYBOX="${TOOLS_DIR}/${BUSYBOX_TOOL}"
# Temporarily use system's tar to unpack AVM firmware-images (see #1535)
UNTAR="$(which tar)"
# Freetz directores relative to base dir
PACKAGES_DIR_ROOT="${BASE_DIR}/packages"
PATCHES_DIR="${BASE_DIR}/patches"
PATCHES_COND_DIR="${PATCHES_DIR}/cond"
PATCHES_DEVICES_DIR="${PATCHES_DIR}/devices"
PATCHES_SCRIPTS_DIR="${PATCHES_DIR}/scripts"
ROOT_DIR="${BASE_DIR}/root"
KERNEL_REP_DIR="${BASE_DIR}/kernel"
ADDON_DIR="${BASE_DIR}/addon"
GRAPHICS_DIR="${BASE_DIR}/${GRAPHICS_SUBDIR}"
# Freetz branding directories relative to graphics subdirectory
FAVICON_DIR="${GRAPHICS_DIR}/favicon"
TAGGING_DIR="${GRAPHICS_DIR}/tagging"
# Package files
STATIC_PACKAGES_FILE="${BASE_DIR}/.static"
DYNAMIC_PACKAGES_FILE="${BASE_DIR}/.dynamic"
STATIC_ADDON_FILE="${ADDON_DIR}/static.pkg"
DYNAMIC_ADDON_FILE="${ADDON_DIR}/dynamic.pkg"
# Alien variables
TK_DIR="${DIR}/.tk/original"
FIRMWARE_TK_DIR="${TK_DIR}/${FIRMWARE_SUBDIR}"
FILESYSTEM_TK_DIR="${TK_DIR}/${FILESYSTEM_SUBDIR}"
KERNEL_TK_DIR="${TK_DIR}/${KERNEL_SUBDIR}"
VARTAR_TK_DIR="${KERNEL_TK_DIR}/${VARTAR_SUBDIR}"
##########################################################
## Include config file + initialise some more variables ##
##########################################################
# Include config file, but do not override variables which are already defined
# in the environment, e.g. via "make FREETZ_SOMETHING=y"
sed -nr 's/^([^=]+)=(.*)/: ${\1:=\2}/p' "$DOT_CONFIG" > "$DOT_CONFIG.fwmod"
. "$DOT_CONFIG.fwmod"
rm "$DOT_CONFIG.fwmod"
# Set default verbosity level, just in case none was defined in the (possibly
# user-defined and minimal) config file
: ${FREETZ_VERBOSITY_LEVEL:=0}
# If FREETZ_FWMOD_* options have been defined via config file or command line,
# they overrule(!) normal command line options -u, -m, -p, -a, -f, -z, -c.
[ "$FREETZ_FWMOD_SKIP_UNPACK" == "y" ] && DO_UNPACK=0
[ "$FREETZ_FWMOD_SKIP_MODIFY" == "y" ] && DO_MOD=0
[ "$FREETZ_FWMOD_SKIP_PACK" == "y" ] && DO_PACK=0
[ "$FREETZ_FWMOD_USBROOT" == "y" ] && DO_ZIP=1
[ "$FREETZ_FWMOD_SDK" == "y" ] && FORCE_PACK=1
[ "$FREETZ_FWMOD_NFSROOT_DIR" ] && COPY_FS_DIR="$FREETZ_FWMOD_NFSROOT_DIR" && DO_ZIP=1
# Use FREETZ_TARGET_CROSS to determine build tool paths
NM="${ABS_BASE_DIR}/toolchain/target/bin/${FREETZ_TARGET_CROSS}nm"
STRIP="${ABS_BASE_DIR}/toolchain/target/bin/${FREETZ_TARGET_CROSS}strip"
# Use FREETZ_MODULES_KVER to determine MODULES_SUBDIR (see kernel/Config.in for details)
MODULES_SUBDIR="lib/modules/${FREETZ_MODULES_KVER}"
# Use FREETZ_TARGET_ARCH_BE to determine SquashFS endianness
if [ "$FREETZ_TARGET_ARCH_BE" == "y" ]; then
MKSQUASHFS_OPTIONS+=" -be"
else
MKSQUASHFS_OPTIONS+=" -le"
fi
# Use FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE etc. to determine correct mksquashfs tool
# TODO: Add new symbols for SquashFS version and LZMA compression to Config.in
if [ "${FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE}" == "y" ]; then
MKSQUASHFS_TOOL="mksquashfs3"
MKSQUASHFS_OPTIONS+=" -no-progress -no-exports -no-sparse"
elif [ "$FREETZ_KERNEL_VERSION_2_6_13" -a ! "$FREETZ_REPLACE_KERNEL" == "y" ]; then
MKSQUASHFS_TOOL="mksquashfs-lzma"
else
MKSQUASHFS_TOOL="mksquashfs3-lzma"
MKSQUASHFS_OPTIONS+=" -no-progress -no-exports -no-sparse"
fi
#######################################
## Initialise fakeroot incl. caching ##
#######################################
# Create id string based on MD5 hashes of firmware images
if [ "$FIRMWARE2" ]; then
FIRMWARE_MD5="$("$MD5SUM" "$FIRMWARE2" | cut -d ' ' -f 1)"
fi
FIRMWARE_MD5="$("$MD5SUM" "$FIRMWARE" | cut -d ' ' -f 1)_$FIRMWARE_MD5"
FAKEROOT="$TOOLS_DIR/build/bin/fakeroot"
FAKEROOT_CACHE_DIR="$ABS_BASE_DIR/.fakeroot-cache"
FAKEROOT_CACHE="$FAKEROOT_CACHE_DIR/$FIRMWARE_MD5"
if [ ! -e "$FAKEROOT_CACHE_DIR" ]; then
mkdir -p "$FAKEROOT_CACHE_DIR"
fi
# Are we in a fakeroot context?
if [ "$FAKEROOTKEY" ] && [ $UID -eq 0 ]; then
# Yes -> fakeroot is active
# Make sure fakeroot call came from ourselves so we can assume the cache settings to be correct
if [ $((FWMOD_RECURSIVE)) -eq 0 ] || [ "$SELF" != "$(sed -nr 's/^Name:[[:blank:]]+(.*)/\1/p' /proc/$FWMOD_RECURSIVE/status)" ]; then
error 1 "$SELF must not run in a fakeroot environment"
fi
# Does fakeroot cache (size>0) exist for current combination of firmwares and has this combination already been unpacked?
if [ -s "$FAKEROOT_CACHE" ] && [ -s "$UNPACKED_FILE" ] && [ "$(cat "$UNPACKED_FILE")" == "$FIRMWARE_MD5" ]; then
# Yes -> skip unpack
[ $DO_UNPACK -gt 0 ] && SKIP_UNPACK="STEP 1: UNPACK (SKIPPED)\n\n"
DO_UNPACK=0
else
# No -> force unpack
[ $DO_UNPACK -eq 0 ] && FORCE_UNPACK=" (FORCED)"
DO_UNPACK=1
fi
else
# No -> fakeroot is inactive
# Does fakeroot cache (size>0) exist for current combination of firmwares and has this combination already been unpacked?
if [ -s "$FAKEROOT_CACHE" ] && [ -s "$UNPACKED_FILE" ] && [ "$(cat "$UNPACKED_FILE")" == "$FIRMWARE_MD5" ]; then
# Yes -> use existing cache and firmwares (no unpack necessary)
FWMOD_RECURSIVE=$$ "$FAKEROOT" -i "$FAKEROOT_CACHE" -s "$FAKEROOT_CACHE" '--' "${CMDLINE_ORIG[@]}"
else
# No -> create new fakeroot cache and unpack firmwares
FWMOD_RECURSIVE=$$ "$FAKEROOT" -s "$FAKEROOT_CACHE" '--' "${CMDLINE_ORIG[@]}"
fi
retval=$?
[ $retval -eq 0 ] || exit $retval
# ------------------------------------------------------------------
# -- Copy file system to target folder (NFS root, maybe USB root) --
# ------------------------------------------------------------------
# This cannot be done in the pack/zip section (needs real sudo, not fakeroot)
if [ "$COPY_FS_DIR" ]; then
echo0 "copying root file system to directory $COPY_FS_DIR"
rootfs_archive="$(cat "${DIR}/.rootfs_archive")" ||
error 1 "cannot determine root file system archive name"
[ -d "$COPY_FS_DIR" ] && (sudo rm -rf "$COPY_FS_DIR"/* ||
error 1 "cannot clean up directory $COPY_FS_DIR")
mkdir -p "$COPY_FS_DIR" ||
error 1 "cannot create directory $COPY_FS_DIR"
sudo "$TAR" -C "$COPY_FS_DIR" -xf "$rootfs_archive" ||
error 1 "cannot unpack root file system"
echo -e "done.\n"
echo0 -b "FINISHED"
fi
exit 0
fi
############################################
## Unpack and unsquash the firmware image ##
############################################
ORG_DIR="${DIR}/original"
FIRMWARE_DIR="${ORG_DIR}/${FIRMWARE_SUBDIR}"
FILESYSTEM_DIR="${ORG_DIR}/${FILESYSTEM_SUBDIR}"
FILESYSTEM_CORE_DIR="${ORG_DIR}/${FILESYSTEM_CORE_SUBDIR}"
KERNEL_DIR="${ORG_DIR}/${KERNEL_SUBDIR}"
AVMPLUGINS_DIR="${KERNEL_DIR}/${AVMPLUGINS_SUBDIR}"
VARTAR_DIR="${KERNEL_DIR}/${VARTAR_SUBDIR}"
HTML_DIR="${FILESYSTEM_DIR}/${HTML_SUBDIR}"
RAW_KERNEL="${KERNEL_DIR}/${RAW_KERNEL_FILE}"
RAW_FILESYSTEM="${KERNEL_DIR}/${RAW_HIDDEN_FILE}"
KERNEL="${FIRMWARE_DIR}/${KERNEL_IMAGE}"
if [ "${FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE}" == "y" ]; then
FILESYSTEM="$FILESYSTEM_CORE_DIR/$FILESYSTEM_CORE_IMAGE"
else
FILESYSTEM="${RAW_FILESYSTEM}"
fi
AVMPLUGINS="${FIRMWARE_DIR}/${AVMPLUGINS_FILE}"
VARTAR="${FILESYSTEM_DIR}/${VARTAR_FILE}"
if [ "$DO_UNPACK" -gt 0 ]; then
[ "$FIRMWARE2" ] && UNPACK_SUBSTEP="A" && UNPACK_MAIN=" MAIN"
echo0 -b "STEP 1${UNPACK_SUBSTEP}: UNPACK${UNPACK_MAIN}${FORCE_UNPACK}"
# Remove old "unpacked" marker to avoid an undefined state in case of
# interruption before unpacking operation is finished
rm -f "$UNPACKED_FILE"
rm -rf "$ORG_DIR"
mkdir "$ORG_DIR"
echo "unpacking firmware image"
mkdir "$FIRMWARE_DIR"
if [ "$FIRMWARE_EXTENSION" != "exe" ]; then
# Check and skip garbage
image_size="0x$(printf "%08X\n" $(stat -c %s "$FIRMWARE")) "
image_grbg="$($TOOLS_DIR/sfk hexfind "$FIRMWARE" _./var/_ | sed -n 's/.*hit at offset //p' |head -n1)"
[ "$grbg" != "0x0" ] && printf "Skipping %d Bytes garbage..." $grbg
# Unpack standard *.image file in tar format
tail -c $(( $image_size - $image_grbg )) "$FIRMWARE" | \
"$UNTAR" -xf "$FIRMWARE" -C "$FIRMWARE_DIR" || untar_failed=1
fi
if [ "$FIRMWARE_EXTENSION" == "exe" ] || [ "$untar_failed" ]; then
# Unpack Windows recover.exe file or mtdblock dump
exe_images=($("${TOOLS_DIR}/extract-images" "$ABS_BASE_DIR" "$FIRMWARE" | sed -nr 's/^ *(.*\.image) - .*/\1/p'))
[ "$exe_images" ] || exit 1
mkdir -p "$FIRMWARE_DIR/var/tmp"
mkdir "$KERNEL_DIR"
unset exe_hr_mode
# NOTE: This loop is sensitive to the order and format of output of
# "tools/extract-images", e.g. a hidden root image is always listed
# before its parts.
for (( i=0; i<${#exe_images[@]}; i++ )); do
exe_image="${exe_images[i]}"
case "$exe_image" in
*/urlader.image)
mv "$exe_image" "$FIRMWARE_DIR/var/tmp"
;;
*/hr_kernel.image)
exe_hr_mode=1
mv "$exe_image" "$FIRMWARE_DIR/var/tmp/kernel.image"
touch "$FIRMWARE_DIR/var/tmp/filesystem.image"
;;
*/kernel.image)
[ "$exe_hr_mode" ] || cp "$exe_image" "$FIRMWARE_DIR/var/tmp/kernel.image"
mv "$exe_image" "$KERNEL_DIR/kernel.raw"
;;
*/filesystem.image)
if [ "$exe_hr_mode" ]; then
mv "$exe_image" "$KERNEL_DIR/kernelsquashfs.raw"
else
mv "$exe_image" "$FIRMWARE_DIR/var/tmp/filesystem.image"
fi
;;
esac
done
rm -r "$(dirname "$exe_image")"
fi
# Do the images exist ?
[ ! -r "${KERNEL}" ] && error 1 "cannot find kernel.image"
[ ! -r "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" ] && error 1 "cannot find filesystem.image"
# Do we have the tool ?
for tool in "$RMTICHKSUM" "$UNSQUASHFS" "$FINDSQUASHFS"; do
[ ! -x $tool ] && error 1 "cannot find the tool $tool"
done
# Remove AVM's special junk bytes from SquashFS
if [ "$FREETZ_AVM_HAS_JUNK_BYTES" == "y" ]; then
echo0 "removing AVM SquashFS junk bytes"
[ "$FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE" == "y" ] && JUNK_BYTES_FILE="${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" || JUNK_BYTES_FILE="$KERNEL"
if [ "$FREETZ_VERBOSITY_LEVEL" -lt 2 ]; then
"$TOOLS_DIR/remove-junk-bytes" "$JUNK_BYTES_FILE" "$JUNK_BYTES_FILE.nojunk" >/dev/null 2>&1
else
"$TOOLS_DIR/remove-junk-bytes" "$JUNK_BYTES_FILE" "$JUNK_BYTES_FILE.nojunk"
fi
[ $? -eq 0 ] || error 1 "AVM SquashFS junk bytes not found"
mv -f "$JUNK_BYTES_FILE.nojunk" "$JUNK_BYTES_FILE"
fi
"$RMTICHKSUM" -f "${KERNEL}" > /dev/null
"$RMTICHKSUM" -f "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" > /dev/null
mkdir -p "$KERNEL_DIR"
if [ "${FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE}" == "y" ]; then
cp "${KERNEL}" "${RAW_KERNEL}"
echo "unpacking update image"
modunsqfs "$FILESYSTEM_CORE_DIR" "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}"
[ ! -r "$RAW_KERNEL" -o ! -r "$FILESYSTEM" ] && error 1 "copying kernel or filesystem image failed"
echo "unpacking filesystem image"
modunsqfs "$FILESYSTEM_DIR" "$FILESYSTEM"
else
echo "splitting kernel image"
( cd "$KERNEL_DIR" && "${TOOLS_DIR}/${FINDSQUASHFS_TOOL}" "../${FIRMWARE_SUBDIR}/${KERNEL_IMAGE}" > /dev/null 2>&1 )
if [ ! -r "$RAW_KERNEL" -o ! -r "$FILESYSTEM" ]; then
if [ -r "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" ] && [ $(stat -c %s "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}") -gt 0 ]; then
_config_hint=" - maybe you should configure a firmware with FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE"
fi
error 1 "kernel splitting failed${_config_hint}"
fi
echo "unpacking filesystem image"
modunsqfs_lzma "$FILESYSTEM_DIR" "$FILESYSTEM"
fi
if [ -r "$AVMPLUGINS" ]; then
echo0 "unpacking AVM plugins"
mkdir -p "$AVMPLUGINS_DIR"
"$UNTAR" -xf "$AVMPLUGINS" -C "$AVMPLUGINS_DIR" || exit 1
for i in "${AVMPLUGINS_DIR}/var/"*.image; do
AVMPLUGIN="${i##*/plugin-}"
AVMPLUGIN="${AVMPLUGIN%\.image}"
echo2 "$AVMPLUGIN image"
modunsqfs_lzma "$AVMPLUGINS_DIR/plugin-${AVMPLUGIN}" "$i" > /dev/null
done
chmod -R +w "$AVMPLUGINS_DIR"
fi
if [ ! -r "$FILESYSTEM_DIR/var" ]; then
error 1 "could not unpack the filesystem image"
fi
if [ ! -r "$VARTAR" ]; then
error 1 "no var.tar found"
fi
echo "unpacking var.tar"
mkdir "$VARTAR_DIR"
"$UNTAR" -xpf "$VARTAR" -C "$VARTAR_DIR" || exit 1
echo -e "done.\n"
# Unpack secondary firmware image
if [ -n "$FIRMWARE2" ]; then
rm -rf "${DIR}/.tk"
"$0" -u -d "${DIR}/.tk" "$FIRMWARE2" |
sed "s/STEP 1: UNPACK/STEP 1B: UNPACK SECONDARY${FORCE_UNPACK}/"
fi
# Create "unpacked" marker, recording exactly *what* was unpacked (MD5)
echo "$FIRMWARE_MD5" > "$UNPACKED_FILE"
fi
echo0 -n -b "$SKIP_UNPACK"
###############################################
# Lets copy and modify the unpacked firmware ##
###############################################
MOD_DIR="${DIR}/modified"
FIRMWARE_MOD_DIR="${MOD_DIR}/${FIRMWARE_SUBDIR}"
FILESYSTEM_MOD_DIR="${MOD_DIR}/${FILESYSTEM_SUBDIR}"
FILESYSTEM_CORE_MOD_DIR="${MOD_DIR}/${FILESYSTEM_CORE_SUBDIR}"
EXTERNAL_MOD_DIR="${MOD_DIR}/${EXTERNAL_SUBDIR}"
KERNEL_MOD_DIR="${MOD_DIR}/${KERNEL_SUBDIR}"
AVMPLUGINS_MOD_DIR="${KERNEL_MOD_DIR}/${AVMPLUGINS_SUBDIR}"
AVMPLUGINS_FILESYSTEM_MOD_DIR="${FILESYSTEM_MOD_DIR}/usr/share/avmplugins"
VARTAR_MOD_DIR="${KERNEL_MOD_DIR}/${VARTAR_SUBDIR}"
KERNEL_MOD="${FIRMWARE_MOD_DIR}/${KERNEL_IMAGE}"
FILESYSTEM_MOD="${FIRMWARE_MOD_DIR}/${FILESYSTEM_IMAGE}"
RAW_KERNEL_MOD="${KERNEL_MOD_DIR}/${RAW_KERNEL_FILE}"
RAW_FILESYSTEM_MOD="${KERNEL_MOD_DIR}/${RAW_HIDDEN_FILE}"
if [ "${FREETZ_AVM_HAS_UPDATE_FILESYSTEM_IMAGE}" == "y" ]; then
CORE_FILESYSTEM_MOD="${FILESYSTEM_CORE_MOD_DIR}/${FILESYSTEM_CORE_IMAGE}"
else
CORE_FILESYSTEM_MOD="$RAW_FILESYSTEM_MOD"
fi
VARTAR_MOD="${FILESYSTEM_MOD_DIR}/${VARTAR_FILE}"
if [ "${FREETZ_AVM_UCLIBC_NPTL_ENABLED}" == "y" ]; then
PACKAGES_DIR="${PACKAGES_DIR_ROOT}/target-${FREETZ_TARGET_ARCH}_gcc-${FREETZ_TARGET_GCC_VERSION}_uClibc-${FREETZ_TARGET_UCLIBC_VERSION}-nptl"
else
PACKAGES_DIR="${PACKAGES_DIR_ROOT}/target-${FREETZ_TARGET_ARCH}_gcc-${FREETZ_TARGET_GCC_VERSION}_uClibc-${FREETZ_TARGET_UCLIBC_VERSION}"
fi
TARGET_SPECIFIC_ROOT_DIR="${PACKAGES_DIR}/root"
HTML_MOD_DIR="${FILESYSTEM_MOD_DIR}/${HTML_SUBDIR}"
# HTML_LANG_MOD_DIR can't be defined here. eg: ${FILESYSTEM_MOD_DIR}/usr/www/all
# HTML_SPEC_MOD_DIR can't be defined here. eg: ${FILESYSTEM_MOD_DIR}/usr/www/all/html/de
MODULES_DIR="${FILESYSTEM_MOD_DIR}/${MODULES_SUBDIR}" #eg: "${FILESYSTEM_MOD_DIR}/lib/modules/2.6.19.2
[ "$FREETZ_SQUASHFS_BLOCKSIZE" ] ||
FREETZ_SQUASHFS_BLOCKSIZE=$("$UNSQUASHFS_LZMA" -s "$FILESYSTEM" 2>/dev/null | sed -nr 's/Block size ([0-9]+)/\1/p')
if [ "$FREETZ_SIZEINFO_COMPRESSED" == "y" ]; then
let FILESYSTEM_BLOCKSIZE_KB=$FREETZ_SQUASHFS_BLOCKSIZE/1024
SIZEINFO_CACHEDIR=${PACKAGES_DIR}/freetzcachedsizes
mkdir -p ${SIZEINFO_CACHEDIR}
fi
function sizeinfo()
{
[ "$FREETZ_VERBOSITY_LEVEL" -lt 1 ] && return
#formated output
if [ $# -eq 1 ]; then
if [ "$FREETZ_SIZEINFO_COMPRESSED" != "y" ]; then
echo1 "$1"
else
echo1 -n "$(printf "%-29s " "$1" | sed 's/ /\./g;s/\.\./ ./;s/\.(addon)/ (addon)/')"
fi
return
fi
[ "$FREETZ_SIZEINFO_COMPRESSED" != "y" ] && return
#cache file
local sizeinfo_mode=$1
shift
case $sizeinfo_mode in
lib) #libraries: multiple files
local shash="$(ls -l $@ | sort | $MD5SUM)"
local sfile="${SIZEINFO_CACHEDIR}/$(echo $@ | sed -rn 's!.*/(.*)\.so.*!\1!p').sizeinfo.MD5_${shash/ */}.BS_${FILESYSTEM_BLOCKSIZE_KB}"
;;
mod) #modules: one .ko file
local shash="$(ls -l $1 | $MD5SUM)"
local sfile="${SIZEINFO_CACHEDIR}/${1##*/}.sizeinfo.MD5_${shash/ */}.BS_${FILESYSTEM_BLOCKSIZE_KB}"
;;
pkg) #packages: files (without .excluded)
local shash="$(echo $* | sort | $MD5SUM)"
local sfile="${1}/.sizeinfo.MD5_${shash/ */}.BS_${FILESYSTEM_BLOCKSIZE_KB}"
shift
;;
add) #addons: one directory
local shash="$(find $1 -type f -exec ls -l {} ';' | $MD5SUM)"
local sfile="${SIZEINFO_CACHEDIR}/${1##*/}.sizeinfo.MD5_${shash/ */}.BS_${FILESYSTEM_BLOCKSIZE_KB}"
;;
*)
echo "invalid"
return
;;
esac
#compressed size
if [ ! -s "$sfile" ]; then
if [ $# -eq 0 ]; then
echo -n " 0.00 Kb (uncompressed: 0.00 Kb)" > ${sfile}
else
local tempf=/tmp/freetz-sizeinfo-$$.tmp
$TOOLS_DIR/$MKSQUASHFS_TOOL $@ $tempf $MKSQUASHFS_OPTIONS -b $FREETZ_SQUASHFS_BLOCKSIZE 2>/dev/null | grep -A1 ^Filesystem \
| sed -n 'N;s!Filesystem size \(.*\)ytes (.*\n.*(\(.*\)ytes.*$!\1 \2!p' \
| awk '{printf "%8s%3s (uncompressed:%8s%3s)",$1,$2,$3,$4}' > ${sfile}
rm -rf $tempf 2>/dev/null
fi
fi
#uncompressed size
if [ "$FREETZ_SIZEINFO_UNCOMPRESSED" = "y" ]; then
cat ${sfile}
else
cat ${sfile} | sed 's/(uncompressed.*//'
fi
echo
}
# show avm image details
avm_fw_produkt="$(sed -rn 's/export CONFIG_PRODUKT_NAME=.*([0-9]{4}.*)"/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf | head -n1)" # newer 4-digits
[ -z "$avm_fw_produkt" ] && avm_fw_produkt="$(sed -rn 's/^HW=[0-9]* OEM=all _PRODUKT_NAME=.*([0-9]{4}).*/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.init)" #older 4-digits
[ -z "$avm_fw_produkt" ] && avm_fw_produkt="$(sed -n 's/^HW=[0-9]* OEM=all _PRODUKT_NAME=\(FRITZ!Box\|Speedport\)#//p' ${FILESYSTEM_DIR}/etc/init.d/rc.init |head -n1| sed 's/#//g;s/Annex.//;s/WLAN/-&/g;s/^$/FritzBox/g')" #oldest, fon-sl-wlan etc
avm_fw_produkt="${avm_fw_produkt%% Edition*}"
avm_fw_language="$(sed -n 's/^language /_/p' ${FILESYSTEM_DIR}/etc/default.language)" #primary language
avm_fw_language="$avm_fw_language$(for x in $(ls ${FILESYSTEM_DIR}/etc/htmltext_??.db 2>/dev/null| sed 's/.*_//g;s/\.db//g'); do [ "_$x" != "$avm_fw_language" ] && echo -en "-$x"; done; echo)" # other languages
avm_fw_major="$(sed -rn 's/^export CONFIG_VERSION_MAJOR="(.*)"/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf | tail -n1)" # newer firmware
[ -z "$avm_fw_major" ] && avm_fw_major="$(sed -n 's/^HW=[0-9].*VERSION_MAJOR=//p' ${FILESYSTEM_DIR}/etc/init.d/rc.init)" # older firmware
avm_fw_version="$(sed -n 's/^export FIRMWARE_VERSION=.*}//p' ${FILESYSTEM_DIR}/etc/version)"
avm_fw_labor="$(sed -rn 's/^export CONFIG_LABOR_ID_NAME="(.*)"/-\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf)"
avm_fw_revision="$(sed -n '/--project)$/{N;s/.*echo //p}' ${FILESYSTEM_DIR}/etc/version)"
avm_fw_date="$(sed -rn 's/^export FIRMWARE_DATE="(.*)"/\1/p' ${FILESYSTEM_DIR}/etc/version)"
echo1 -l "detected firmware ${avm_fw_produkt/ /-}$avm_fw_language $avm_fw_major$avm_fw_version$avm_fw_labor rev$avm_fw_revision ($avm_fw_date)\n"
if [ "$DO_MOD" -gt 0 ]; then
echo0 -b "STEP 2: MODIFY"
# Check if firmware is unpacked
if [ ! -r "$UNPACKED_FILE" ]; then
error 1 "firmware image has to be unpacked before modifying"
fi
rm -rf "$MOD_DIR"
rm -f "${DIR}/.modified"
# Copy the unpacked directory
mkdir -p "$MOD_DIR"
[ "$FREETZ_AVM_HAS_UDEV" == y ] || exclude_dev="--exclude=./filesystem/dev"
"$TAR" -c -C "$ORG_DIR" $exclude_dev . | "$TAR" -x -C "$MOD_DIR" || exit 1
# Fix some permissions
chmod -R u+w "$FILESYSTEM_MOD_DIR"
find "${FILESYSTEM_MOD_DIR}" "${VARTAR_MOD_DIR}" -type d -exec chmod 755 {} '+'
# Give all users write permissions to /var/tmp to allow it to be used as a temp-directory.
# Set sticky bit to prevent users from deleting/renaming files they are not the owners of.
# NB: The permissions set are the regular temp-directory permissions on Unix-like systems.
chmod 1777 "${VARTAR_MOD_DIR}/var/tmp"
echo0 -n "applying symlinks, deleting additional webinterfaces in:"
oems="$(grep 'for i in avm' "${FIRMWARE_MOD_DIR}/var/install" | head -n 1 | sed -e 's/^.*for i in\(.*\); do.*$/\1/')"
for webdir in \
${FILESYSTEM_MOD_DIR}/usr/www \
${FILESYSTEM_MOD_DIR}/usr/www.nas \
${FILESYSTEM_MOD_DIR}/usr/www.myfritz \
; do
if [ -d ${webdir}/avm* ]; then
echo0 -n " ${webdir#${FILESYSTEM_MOD_DIR}/}"
mv ${webdir}/avm* ${webdir}/all
for i in $oems; do
rm -rf ${webdir}/$i
ln -s all ${webdir}/$i
done
fi
done
echo0
echo0 "applying patches"
# Include patching helper function (modpatch)
. "${TOOLS_DIR}/freetz_patch" || error 1 "cannot find script freetz_patch"
PATCHES_DEVICES_DIR_EXTRA="${PATCHES_DEVICES_DIR}/${FREETZ_TYPE_PREFIX_SERIES_SUBDIR}/${FREETZ_TYPE_PREFIX%_*}"
PATCHES_DEVICES_DIR_MULTI="${PATCHES_DEVICES_DIR}/${FREETZ_TYPE_PREFIX_SERIES_SUBDIR}"
echo1 "applying patches: ${FREETZ_TYPE_PREFIX}${FREETZ_TYPE_PREFIX_LABOR_FIRMWARE}-${FREETZ_TYPE_LANGUAGE} (${FREETZ_TYPE_PREFIX_SERIES_SUBDIR})"
# Apply patches
if [ ! -d "$PATCHES_DEVICES_DIR_EXTRA" -a ! -d "$PATCHES_DEVICES_DIR_MULTI" ]; then
error 1 "missing $PATCHES_DEVICES_DIR_EXTRA"
fi
# Execute version specific patch scripts
for j in ${PATCHES_DIR} $PATCHES_DEVICES_DIR_MULTI $PATCHES_DEVICES_DIR_EXTRA; do
for i in $j/*.sh $j/${FREETZ_TYPE_LANGUAGE}/*.sh; do
[ -r "$i" ] || continue
echo2 "applying patch file $i"
. $i
done
done
# Now we are in place to check html directory
for i in all/en all ewetel; do
if [ -d "${HTML_MOD_DIR}/$i" ]; then
HTML_LANG_MOD_DIR="${HTML_MOD_DIR}/$i"
break
fi
done
[ -z "$HTML_LANG_MOD_DIR" ] && error 1 "No HTML directory given."
# Now check language specific html directory
for i in de en; do
if [ -d "${HTML_LANG_MOD_DIR}/html/$i" ]; then
HTML_SPEC_MOD_DIR="${HTML_LANG_MOD_DIR}/html/$i"
break
fi
done
[ -z "$HTML_SPEC_MOD_DIR" ] && error 1 "No language specific HTML directory given."
# Apply (general, version specific, language specific) patches
shopt -s nullglob
for j in ${PATCHES_DIR} $PATCHES_DEVICES_DIR_MULTI $PATCHES_DEVICES_DIR_EXTRA; do
for i in $j/*.patch $j/${FREETZ_TYPE_LANGUAGE}/*.patch; do
modpatch "$FILESYSTEM_MOD_DIR" "$i"
done
done
shopt -u nullglob
echo1 "creating symlinks /tmp, /mod and /home"
rm -rf "$FILESYSTEM_MOD_DIR"/tmp
ln -s var/{tmp,mod} "$FILESYSTEM_MOD_DIR"/
ln -s var/mod/home "$FILESYSTEM_MOD_DIR"/
echo1 "creating /mnt and /opt"
mkdir -p "$FILESYSTEM_MOD_DIR"/mnt
# 7390: /opt is symlink to /var/InternerSpeicher/opt
[ ! -L "$FILESYSTEM_MOD_DIR"/opt ] && mkdir -p "$FILESYSTEM_MOD_DIR"/opt
# Set version and options
SUBVERSION_FILE="${BASE_DIR}/.version"
FIRMWAREVERSION_FILE="${FILESYSTEM_MOD_DIR}/etc/.version"
FIRMWAREVERSION="$(cat $FIRMWAREVERSION_FILE)"
PACKAGES_LIST_FILE="${BASE_DIR}/.packages"
if [ ! -r "$SUBVERSION_FILE" ]; then
error 1 "cannot determine version"
fi
if [ -d .svn ]; then
# be compatible: older versions of svnversion need WC_PATH
if REVISION="$(svnversion . 2>/dev/null | tr ":" "_")"; then
[ "${REVISION:0:6}" == "export" ] && REVISION=""
fi
elif [ -d .git ]; then
REVISION="$(git log 2>/dev/null | sed -rn 's!.*git-svn-id.*\@([^ ]*).*!\1!p' | head -n1)"
fi
[ -n "$REVISION" ] && REVISION="-$REVISION"
SUBVERSION="$(cat $SUBVERSION_FILE)${REVISION}"
#delete .packages file
rm -f "${PACKAGES_LIST_FILE}"
for i in $(set|grep -i "^FREETZ_REMOVE_.*=y$"|sed -e "s/^.\{14\}//;s/=.*//"|tr [A-Z] [a-z]|sort); do
OPTIONS+=" -$i"
done
OPTIONS_ADD=" +busybox"
for i in $(set|grep -i "^FREETZ_PATCH_.*=y$"|sed -e "s/^.\{13\}//;s/=.*//"|tr [A-Z] [a-z]); do
OPTIONS_ADD+=" +$i"
done
for pkg in $(static_packages) $(static_addons); do
OPTIONS_ADD+=" +$pkg"
echo "$pkg" >> "${PACKAGES_LIST_FILE}"
done
if [ "$FIRMWARE_NOCOMPILE" != "1" -a "$FREETZ_REPLACE_KERNEL" == "y" -a "$FWMOD_PATCH_TEST" != "y" ]; then
KERNEL_SUBVERSION_FILE="${KERNEL_REP_DIR}/.version-${FREETZ_KERNEL_LAYOUT}-${FREETZ_AVM_VERSION_STRING}"
if [ ! -r "$KERNEL_SUBVERSION_FILE" ]; then
error 1 "cannot determine kernel version"
fi
KERNEL_SUBVERSION="$(cat $KERNEL_SUBVERSION_FILE)"
OPTIONS_ADD+=" +kernel ($KERNEL_SUBVERSION)"
fi
for i in $(echo ${OPTIONS_ADD}|sed -e's: +:\n+:g'|sort); do
OPTIONS+=" $i"
done
echo1 "setting freetz-version '${SUBVERSION}'"
echo "$SUBVERSION" > "${FILESYSTEM_MOD_DIR}/etc/.freetz-version"
if [ "$FREETZ_MODIFY_AVM_VERSION" == "y" ]; then
echo1 "setting AVM firmware subversion '${SUBVERSION}'"
echo "$SUBVERSION" > "${FILESYSTEM_MOD_DIR}/etc/.subversion"
sed -i -e "s/\export\ FIRMWARE_SUBVERSION=.*\$/export\ FIRMWARE_SUBVERSION=\"${SUBVERSION}\"/g" "${FILESYSTEM_MOD_DIR}/etc/version"
if [ ! -e "${FILESYSTEM_MOD_DIR}/etc/init.d/rc.init" ]; then
AVM_SUBVERSION=`grep "SUBVERSION=" "${FILESYSTEM_MOD_DIR}/etc/init.d/rc.conf" | sed -e "s/[^-0-9]//g"`
sed -i -e "s/SUBVERSION=\"$AVM_SUBVERSION\"/SUBVERSION=\"-$SUBVERSION\"/" "${FILESYSTEM_MOD_DIR}/etc/init.d/rc.conf"
fi
sed -i -e "s/<subversion>/${SUBVERSION}/g" "${FILESYSTEM_MOD_DIR}/usr/bin/system_status"
sed -i -e "s/<options>/${OPTIONS}/g" "${FILESYSTEM_MOD_DIR}/usr/bin/system_status"
fi
# Execute general patch scripts
for i in "${PATCHES_SCRIPTS_DIR}/"*.sh; do
[ -r "$i" ] || continue
echo2 "applying patch file $i"
. $i
done
# stop execution if in patch test mode
if [ "$FWMOD_PATCH_TEST" == "y" ]; then
echo0 -b "Patch test mode: exiting without error."
exit
fi
# remove oems
[ "$FREETZ_VERBOSITY_LEVEL" -ge 1 ] && echo -n "${L1}removing oem:"
oem_removed=0
oem_kept=0
oem_list=
oems=$(grep -E 'for i in (avm|tcom|ewetel)' "${FIRMWARE_MOD_DIR}/var/install" | head -n 1 | sed -e 's/^.*for i in\(.*\); do.*$/\1/')
[ -z "$oems" ] && error 1 "no oems found"
ln -sf /usr/www/cgi-bin/freetz_status "${HTML_LANG_MOD_DIR}/cgi-bin/freetz_status"
[ "$FREETZ_PACKAGE_WOL_CGI" == "y" ] && ln -sf /usr/www/cgi-bin/freetz_wol "${HTML_LANG_MOD_DIR}/cgi-bin/freetz_wol"
for i in $oems; do
if [ "$(eval "echo \"\$FREETZ_REMOVE_BRANDING_${i}\"")" == "y" ]; then
[ "$FREETZ_VERBOSITY_LEVEL" -ge 1 ] && echo -n " $i"
# delete webinterface symlinks otherwise user can choose branding in status.cgi
rm -rf "${FILESYSTEM_MOD_DIR}/usr/www/${i}"
rm -rf "${FILESYSTEM_MOD_DIR}/usr/www.nas/${i}"
rm -rf "${FILESYSTEM_MOD_DIR}/usr/www.myfritz/${i}"
find ${FILESYSTEM_MOD_DIR}/etc/default.* -name "*${i}*" | xargs rm -rf
oem_removed=1
else
oem_list+=" $i"
oem_kept=1
fi
done
if [ "$FREETZ_VERBOSITY_LEVEL" -ge 1 ]; then
[ "$oem_removed" -eq 0 ] && echo -n " none"
echo
fi
if isFreetzType W501V; then
oem_list="tcom avm"
fi
sed -i -r -e 's/for i in (avm|tcom).*; do/for i in '"$oem_list"' ; do/g' "${FIRMWARE_MOD_DIR}/var/install"
if [ "$oem_kept" -eq 0 ]; then
error 1 "please choose at least one OEM (branding) in menuconfig: AVM, 1&1, Freenet etc."
fi
# Copy selected avmplugins into the firmware
if [ "$FREETZ_AVMPLUGINS_ENABLED" == "y" ]; then
echo0 "installing AVM plugins"
echo "#" > ${FILESYSTEM_MOD_DIR}/sbin/start_plugin.sh
rm -rf ${FIRMWARE_MOD_DIR}/${AVMPLUGINS_FILE}
mkdir "${AVMPLUGINS_FILESYSTEM_MOD_DIR}"
for i in `find "${AVMPLUGINS_MOD_DIR}" -maxdepth 1 -type d -name var -prune -false , -type d -name "plugin-*" 2>/dev/null`; do
AVMPLUGIN="${i##*plugin-}"
if [ "$(eval "echo \"\$FREETZ_AVMPLUGINS_$(echo "$AVMPLUGIN" | tr [:lower:] [:upper:])\"")" == "y" ]; then
echo1 "$AVMPLUGIN plugin"
cp -a "${AVMPLUGINS_MOD_DIR}/plugin-${AVMPLUGIN}" "${AVMPLUGINS_FILESYSTEM_MOD_DIR}/plugin-${AVMPLUGIN}"
ln -s "/usr/share/avmplugins/plugin-${AVMPLUGIN}" "${VARTAR_MOD_DIR}/var/plugin-${AVMPLUGIN}"
for module in `cd "${AVMPLUGINS_FILESYSTEM_MOD_DIR}/plugin-${AVMPLUGIN}";find -type f -name "*.ko"`; do
echo2 "moving ${module##*/} to standard modules dir"
mv "${AVMPLUGINS_FILESYSTEM_MOD_DIR}/plugin-${AVMPLUGIN}/$module" "${FILESYSTEM_MOD_DIR}/$module"
done
case "$AVMPLUGIN" in
tam)
modsed \
"s/ CONFIG_TAM_ONRAM=.*$/ CONFIG_TAM_ONRAM=\"n\"/g" \
"${FILESYSTEM_MOD_DIR}/etc/init.d/rc.conf"
;;
esac
fi
done
fi
if [ "$FREETZ_INSTALL_BASE" == "y" ]; then
echo "installing mod base"
MOD_CRON="${VARTAR_MOD_DIR}/var/spool/cron/crontabs"
MOD_ROOT="${VARTAR_MOD_DIR}/var/mod"
# Remove symlink before creating directory
rm -rf ${VARTAR_MOD_DIR}/var/spool
mkdir -p "$MOD_CRON"
mkdir -p "${MOD_ROOT}/pkg" "${MOD_ROOT}/home" "${MOD_ROOT}/lib" "${MOD_ROOT}/root"
mkdir -p "${MOD_ROOT}/bin" "${MOD_ROOT}/sbin"
mkdir -p "${MOD_ROOT}/var/cache"
mkdir -p "${MOD_ROOT}/usr/bin" "${MOD_ROOT}/usr/sbin" "${MOD_ROOT}/usr/share" \
"${MOD_ROOT}/usr/lib" "${MOD_ROOT}/usr/lib/cgi-bin" "${MOD_ROOT}/usr/lib/freetz"
mkdir -p "${MOD_ROOT}/etc/conf" "${MOD_ROOT}/etc/init.d" "${MOD_ROOT}/etc/reg"
ln -s /tmp/flash/mod/.profile "${MOD_ROOT}/root/.profile"
# AVM compatibility symlink
ln -s ../sys "${VARTAR_MOD_DIR}/var/sysfs"
echo "root:x:0:0:root:/mod/root:/bin/sh" > "${VARTAR_MOD_DIR}/var/tmp/passwd"
echo 'root:$1$$zO6d3zi9DefdWLMB.OHaO.:12332:0:99999:7:::' > "${VARTAR_MOD_DIR}/var/tmp/shadow"
echo "root:x:0:" > "${VARTAR_MOD_DIR}/var/tmp/group"
echo "users:x:1:" >> "${VARTAR_MOD_DIR}/var/tmp/group"
touch "${VARTAR_MOD_DIR}/var/tmp/ethers"
touch "${VARTAR_MOD_DIR}/var/tmp/exports"
touch "${VARTAR_MOD_DIR}/var/tmp/gshadow"
chmod 644 "${VARTAR_MOD_DIR}/var/tmp/passwd" "${VARTAR_MOD_DIR}/var/tmp/group"
chmod 600 "${VARTAR_MOD_DIR}/var/tmp/shadow" "${VARTAR_MOD_DIR}/var/tmp/gshadow"
# Fix permissions set by AVM, in particular revoke write permissions for g- and o-users.
chmod 644 "${VARTAR_MOD_DIR}/var/tmp/hosts" "${VARTAR_MOD_DIR}/var/tmp/resolv.conf"
mkdir -p "${VARTAR_MOD_DIR}/var/tmp/onlinechanged"
ln -s ../var/tmp/ethers "${FILESYSTEM_MOD_DIR}/etc/ethers"
ln -s ../var/tmp/exports "${FILESYSTEM_MOD_DIR}/etc/exports"
ln -s ../var/tmp/gshadow "${FILESYSTEM_MOD_DIR}/etc/gshadow"
rm -f ${VARTAR_MOD_DIR}/var/*/.dummy
echo1 "installing libs"
for i in $(set | grep ^FREETZ_LIB_.*=y | grep -v ^FREETZ_LIB_STDCXXLIB); do
all_files=""
found=0
conf=${i%%=*}
[ "$conf" != "${conf%%_WITH_*}" ] && continue # skip suboptions
bn=${conf#FREETZ_LIB_}
fn=${bn//_/[-+._]}
sizeinfo "$bn"
for dn in lib usr/lib usr/lib/freetz usr/lib/xtables; do
files=$(shopt -s nullglob; echo ${TARGET_SPECIFIC_ROOT_DIR}/$dn/$fn[-.]*so*)
if [ -z "$files" ]; then
continue;
fi
found=1
[ -d "${FILESYSTEM_MOD_DIR}/${dn}" ] || mkdir -p "${FILESYSTEM_MOD_DIR}/${dn}"
cp -a $files "${FILESYSTEM_MOD_DIR}/${dn}/"
all_files="$all_files $files"
done
if [ "$found" = 0 ]; then
warn "Library $bn selected, but no files found"
else
sizeinfo lib "$all_files"
fi
done
[ "$FREETZ_LIB_libuClibc" == "y" ] && cp -a "${TARGET_SPECIFIC_ROOT_DIR}/lib/libc.so.0" "${FILESYSTEM_MOD_DIR}/lib/"
if [ "$FREETZ_SHARE_terminfo" == "y" ]; then
echo1 "installing terminfos"
for i in $(set | grep ^FREETZ_SHARE_terminfo_.*=y |grep -v '^FREETZ_SHARE_terminfo_showall=' ); do
dn=usr/share/terminfo
conf=${i%%=*}
bn=${conf#FREETZ_SHARE_terminfo_}
fn=$(echo "$bn" | sed 's/MINUS/-/g;s/PLUS/+/g;s/\DOT/./g')
echo2 "$fn"
fn="${fn:0:1}/$fn"
file="${TARGET_SPECIFIC_ROOT_DIR}/$dn/$fn"
if [ ! -e "$file" ]; then
warn "Terminfo $bn selected, but no file found"
continue;
fi
dest="${FILESYSTEM_MOD_DIR}/$dn/${fn:0:1}"
[ -d "$dest" ] || mkdir -p "$dest"
cp -a $file "$dest/"
done
fi
else
error 1 "installation of base system is mandatory"
fi
if [ "$FIRMWARE_NOCOMPILE" != "1" ]; then
[ "$FREETZ_REPLACE_BUSYBOX" != "y" ] && error 1 "installation of busybox replacement is mandatory"
echo0 "installing busybox"
echo1 "replacing busybox"
[ ! -r "${PACKAGES_DIR}/busybox/busybox" ] && error 1 "cannot find busybox replacement"
cp -pf "${PACKAGES_DIR}/busybox/busybox" "${FILESYSTEM_MOD_DIR}/bin/busybox"
echo1 "installing symlinks"
[ ! -r "${PACKAGES_DIR}/busybox/busybox.links" ] && error 1 "cannot find busybox links"
# Remove old busybox links
# be compatible: do not use -delete (not found in older versions of find)
find "$FILESYSTEM_MOD_DIR" \( -lname "busybox" -or -lname "*/busybox" \) -print0 | xargs -0 rm
# Install new busybox links
for link in $(cat "${PACKAGES_DIR}/busybox/busybox.links"); do
LINK_DIR="$(dirname "$link")"
LINK_NAME="$(basename "$link")"
case "$LINK_DIR" in
/)
BUSYBOX_PATH="bin/busybox"
;;
/bin)
BUSYBOX_PATH="busybox"
;;
/sbin)
BUSYBOX_PATH="../bin/busybox"