forked from Freetz/freetz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fwmod
executable file
·1831 lines (1587 loc) · 72.4 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 <[email protected]>
# 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] [-z] [-c <dir>] [-n] [-f] [-i <cfg>] [-d <dir>] <orig_fw> [<tk_fw> [<aux_fw>]]
main actions
-u unpack firmware image
-m modify previously unpacked image
-p pack firmware image
-a all: unpack, modify and pack firmware image
special actions
-s sign firmware image
-z zip file system into archive for USB/NFS root
-c <dir> copy file system to target directory for NFS/USB root (implies -z)
options
-n firmware-nocompile: do not install kernel and busybox
-f force pack even if image is too big for flash (AVM SDK)
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)
<aux_fw> 3rd firmware name (e.g. to borrow missing files)
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; DO_SIGN=0
FIRMWARE_NOCOMPILE=0; FORCE_PACK=0; DO_ZIP=0; COPY_FS_DIR=; DIR=
# Parse command line parameters
while getopts umpsanfzc:i:d: opt; do
case "$opt" in
u) DO_UNPACK=1 ;;
m) DO_MOD=1 ;;
p) DO_PACK=1 ;;
s) DO_SIGN=1 ;;
a) DO_UNPACK=1; DO_MOD=1; DO_PACK=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 action and one (non-empty) firmware image name must be specified
[ $(($DO_UNPACK + $DO_MOD + $DO_PACK + $DO_ZIP)) -eq 0 -o $# -lt 1 -o $# -gt 3 -o ! "$1" ] && usage >&2 && exit 1
# Check if it's a completely automated run in the "no freetz"-mode and cache the value.
# We are interested in what the user asked us for and not what we are actually doing,
# the UNPACK step might be skipped (and the value of DO_UNPACK changed) if the already
# unpacked firmware is found on the disk.
[ "$DO_UNPACK" -gt 0 -a "$DO_MOD" -eq 0 -a "$DO_PACK" -gt 0 ] && NO_FREETZ_AUTOMATED_RUN=1 || NO_FREETZ_AUTOMATED_RUN=0
# 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.)
source "${TOOLS_DIR}/freetz_functions" ||
(echo "cannot find script freetz_functions" >&2; exit 1)
if [ "$DO_SIGN" -gt 0 -a $(($DO_PACK + $DO_ZIP)) -eq 0 ]; then
error 1 "Firmware signing without packing is not supported, please use PeterPawn's sign_image tool directly if you plan to sign some already existing .image file"
fi
# Initialise firmware image names
FIRMWARE="$1"
FIRMWARE2="$2"
FIRMWARE3="$3"
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"
[ "$FIRMWARE3" -a ! -r "$FIRMWARE3" ] &&
error 1 "firmware image $(basename "$FIRMWARE3") not found"
# Config file must exist
[ -r "$DOT_CONFIG" ] ||
error 1 "not configured"
# 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_OUTER_SUBDIR="filesystem.outer"
AVMPLUGINS_SUBDIR="plugins.image"
EXTERNAL_SUBDIR="external"
KERNEL_SUBDIR="kernel"
VARTAR_SUBDIR="var.tar"
HTML_SUBDIR="usr/www"
GRAPHICS_SUBDIR="graphics"
# (temporary) files created during firmware unpacking
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"
MAKEDEVS_TOOL="makedevs"
DEBUGFS_TOOL="debugfs"
EXTERNAL_TOOL="external"
TAR_TOOL="tar"
MD5SUM_TOOL="md5sum"
BUSYBOX_TOOL="busybox"
# Absolute tools paths
EXTERNAL="${TOOLS_DIR}/${EXTERNAL_TOOL}"
DEBUGFS="${TOOLS_DIR}/${DEBUGFS_TOOL}"
FINDSQUASHFS="${TOOLS_DIR}/${FINDSQUASHFS_TOOL}"
TICHKSUM="${TOOLS_DIR}/${TICHKSUM_TOOL}"
MAKEDEVS="${TOOLS_DIR}/${MAKEDEVS_TOOL}"
MAKEDEVS_FILE="${TOOLS_DIR}/device_table.txt"
TAR="${TOOLS_DIR}/${TAR_TOOL}"
TAR_GNU="${TOOLS_DIR}/tar-gnu"
MD5SUM="${TOOLS_DIR}/${MD5SUM_TOOL}"
BUSYBOX="${TOOLS_DIR}/${BUSYBOX_TOOL}"
BLKID="${TOOLS_DIR}/blkid"
# 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"
STATIC_ADDON_FILE="${ADDON_DIR}/static.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}"
AUX_DIR="${DIR}/.aux/original"
FIRMWARE_AUX_DIR="${AUX_DIR}/${FIRMWARE_SUBDIR}"
FILESYSTEM_AUX_DIR="${AUX_DIR}/${FILESYSTEM_SUBDIR}"
KERNEL_AUX_DIR="${AUX_DIR}/${KERNEL_SUBDIR}"
VARTAR_AUX_DIR="${KERNEL_AUX_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"
source "$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}
# Kernel/SquashFS images contained in firmware
KERNEL_IMAGE="${FREETZ_AVM_IMAGES_SUBDIR}/kernel.image"
FILESYSTEM_IMAGE="${FREETZ_AVM_IMAGES_SUBDIR}/filesystem.image"
FILESYSTEM_INNER_IMAGE="filesystem_core.squashfs"
AVMPLUGINS_FILE="${FREETZ_AVM_IMAGES_SUBDIR}/plugins.update"
# 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"
# Tools and options for (un)packing SquashFS
UNSQUASHFS_TOOL="unsquashfs4-avm-${FREETZ_AVM_SQUASHFS_ENDIANNESS}"
UNSQUASHFS="${TOOLS_DIR}/${UNSQUASHFS_TOOL}"
UNSQUASHFS_OPTIONS="-no-progress -processors 1 -exit-on-error"
MKSQUASHFS_TOOL[2]="mksquashfs2-lzma"
MKSQUASHFS_TOOL[3]="mksquashfs3-multi"
MKSQUASHFS_TOOL[4]="mksquashfs4-avm-${FREETZ_AVM_SQUASHFS_ENDIANNESS}"
MKSQUASHFS="${TOOLS_DIR}/${MKSQUASHFS_TOOL[${FREETZ_SQUASHFS_VERSION}]}"
MKSQUASHFS_OPTIONS="-all-root -info"
if [ "${FREETZ_SQUASHFS_VERSION}" -gt 2 ]; then
MKSQUASHFS_OPTIONS+=" -no-progress -no-exports -no-sparse"
fi
if [ "${FREETZ_SQUASHFS_VERSION}" -lt 4 ]; then
MKSQUASHFS_OPTIONS+=" -noappend"
MKSQUASHFS_OPTIONS+=" -${FREETZ_AVM_SQUASHFS_ENDIANNESS}"
fi
if [ "${FREETZ_SQUASHFS_VERSION}" -eq 3 -a "${FREETZ_AVM_SQUASHFS_COMPRESSION}" == "lzma" ]; then
MKSQUASHFS_OPTIONS+=" -lzma1"
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)
NON_FAKEROOT_USERNAME=$(id -u -n) FWMOD_RECURSIVE=$$ "$FAKEROOT" -i "$FAKEROOT_CACHE" -s "$FAKEROOT_CACHE" '--' "${CMDLINE_ORIG[@]}"
else
# No -> create new fakeroot cache and unpack firmwares
NON_FAKEROOT_USERNAME=$(id -u -n) 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_OUTER_DIR="${ORG_DIR}/${FILESYSTEM_OUTER_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_SEPARATE_FILESYSTEM_IMAGE}" == "y" ]; then
if [ "${FREETZ_AVM_HAS_INNER_OUTER_FILESYSTEM}" == "y" ]; then
FILESYSTEM="${FILESYSTEM_OUTER_DIR}/${FILESYSTEM_INNER_IMAGE}"
INNER_FS_PREFIX="inner-"
else
FILESYSTEM="${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}"
fi
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" && FIRMWARE_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
# Unpack standard *.image file in tar format
"$TAR_GNU" -xif "$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/${FREETZ_AVM_IMAGES_SUBDIR}"
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/${FREETZ_AVM_IMAGES_SUBDIR}"
;;
*/hr_kernel.image)
exe_hr_mode=1
mv "$exe_image" "$FIRMWARE_DIR/${FREETZ_AVM_IMAGES_SUBDIR}/kernel.image"
touch "$FIRMWARE_DIR/${FREETZ_AVM_IMAGES_SUBDIR}/filesystem.image"
;;
*/kernel.image)
[ "$exe_hr_mode" ] || cp "$exe_image" "$FIRMWARE_DIR/${FREETZ_AVM_IMAGES_SUBDIR}/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/${FREETZ_AVM_IMAGES_SUBDIR}/filesystem.image"
fi
;;
esac
done
rm -r "$(dirname "$exe_image")"
fi
# Do the images exist ?
[ ! -r "${KERNEL}" ] && error 1 "cannot find kernel.image (${KERNEL})"
[ ! -r "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" ] && error 1 "cannot find filesystem.image (${FIRMWARE_DIR}/${FILESYSTEM_IMAGE})"
# Do we have the tool ?
for tool in "$UNSQUASHFS" "$FINDSQUASHFS" "$TICHKSUM"; do
[ ! -x $tool ] && error 1 "cannot find the tool $tool"
done
# Remove NMI vector from SquashFS
if [ "$FREETZ_AVM_HAS_NMI_VECTOR" == "y" ]; then
echo0 "removing NMI vector from SquashFS"
[ "$FREETZ_AVM_HAS_SEPARATE_FILESYSTEM_IMAGE" == "y" ] && FILE_WITH_NMI_VECTOR_TO_PROCESS="${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" || FILE_WITH_NMI_VECTOR_TO_PROCESS="$KERNEL"
if [ "$FREETZ_VERBOSITY_LEVEL" -lt 2 ]; then
"$TOOLS_DIR/remove-nmi-vector" "$FILE_WITH_NMI_VECTOR_TO_PROCESS" "$FILE_WITH_NMI_VECTOR_TO_PROCESS.no-nmi" >/dev/null 2>&1
else
"$TOOLS_DIR/remove-nmi-vector" "$FILE_WITH_NMI_VECTOR_TO_PROCESS" "$FILE_WITH_NMI_VECTOR_TO_PROCESS.no-nmi"
fi
case "$?" in
0) mv -f "$FILE_WITH_NMI_VECTOR_TO_PROCESS.no-nmi" "$FILE_WITH_NMI_VECTOR_TO_PROCESS" ;;
1) warn2 "NMI vector not found. Assuming Freetz or Freetz-repacked image." ;;
*) error 1 "fatal error while removing NMI vector from SquashFS." ;;
esac
fi
"$TICHKSUM" -r "${KERNEL}" > /dev/null
"$TICHKSUM" -r "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}" > /dev/null
mkdir -p "$KERNEL_DIR"
if [ "${FREETZ_AVM_HAS_SEPARATE_FILESYSTEM_IMAGE}" == "y" ]; then
cp "${KERNEL}" "${RAW_KERNEL}"
[ ! -r "$RAW_KERNEL" ] && error 1 "copying kernel image failed"
if [ "${FREETZ_AVM_HAS_INNER_OUTER_FILESYSTEM}" == "y" ]; then
echo "unpacking outer-filesystem image"
modunpack_autodetect_fs "${FILESYSTEM_OUTER_DIR}" "${FIRMWARE_DIR}/${FILESYSTEM_IMAGE}"
[ ! -r "$FILESYSTEM" ] && error 1 "unpacking outer-filesystem image failed"
fi
echo "unpacking ${INNER_FS_PREFIX}filesystem image"
modunpack_autodetect_fs "$FILESYSTEM_DIR" "$FILESYSTEM" || error 1 "unpacking ${INNER_FS_PREFIX}filesystem image failed"
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_SEPARATE_FILESYSTEM_IMAGE/FREETZ_AVM_HAS_INNER_OUTER_FILESYSTEM"
fi
error 1 "kernel splitting failed${_config_hint}"
fi
echo "unpacking filesystem image"
modunpack_autodetect_fs "$FILESYSTEM_DIR" "$FILESYSTEM" || error 1 "unpacking filesystem image failed"
fi
if [ -r "$AVMPLUGINS" ]; then
echo0 "unpacking AVM plugins"
mkdir -p "$AVMPLUGINS_DIR"
"$TAR_GNU" -xif "$AVMPLUGINS" -C "$AVMPLUGINS_DIR" || exit 1
for i in "${AVMPLUGINS_DIR}/var/"*.image; do
AVMPLUGIN="${i##*/plugin-}"
AVMPLUGIN="${AVMPLUGIN%\.image}"
echo2 "$AVMPLUGIN image"
modunpack_autodetect_fs "$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"
"$TAR_GNU" -xipf "$VARTAR" -C "$VARTAR_DIR" || exit 1
echo -e "done.\n"
# Unpack secondary firmware image
if [ -n "$FIRMWARE2" ]; then
echo0 -b "FINISHED"
rm -rf "${DIR}/.tk"
"$0" -u -d "${DIR}/.tk" "$FIRMWARE2" |
sed "s/STEP 1: UNPACK/STEP 1B: UNPACK SECONDARY${FORCE_UNPACK}/;s/ firmware/ secondary&/"
fi
# Unpack auxiliary firmware image
if [ -n "$FIRMWARE3" ]; then
rm -rf "${DIR}/.aux"
"$0" -u -d "${DIR}/.aux" "$FIRMWARE3" |
sed "s/STEP 1: UNPACK/STEP 1C: UNPACK AUXILIARY${FORCE_UNPACK}/;s/ firmware/ auxiliary&/"
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 ##
###############################################
# FREETZ_LIBRARY_DIR is the 1st FREETZ_RPATH entry
FREETZ_LIBRARY_DIR=$(stripTrailingSlash "${FREETZ_RPATH%%:*}")
MOD_DIR="${DIR}/modified"
FIRMWARE_MOD_DIR="${MOD_DIR}/${FIRMWARE_SUBDIR}"
FILESYSTEM_MOD_DIR="${MOD_DIR}/${FILESYSTEM_SUBDIR}"
FILESYSTEM_OUTER_MOD_DIR="${MOD_DIR}/${FILESYSTEM_OUTER_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_SEPARATE_FILESYSTEM_IMAGE}" == "y" ]; then
if [ "${FREETZ_AVM_HAS_INNER_OUTER_FILESYSTEM}" == "y" ]; then
MAIN_FILESYSTEM_MOD="${FILESYSTEM_OUTER_MOD_DIR}/${FILESYSTEM_INNER_IMAGE}"
else
MAIN_FILESYSTEM_MOD="$RAW_FILESYSTEM_MOD"
fi
else
MAIN_FILESYSTEM_MOD="$RAW_FILESYSTEM_MOD"
fi
VARTAR_MOD="${FILESYSTEM_MOD_DIR}/${VARTAR_FILE}"
[ "${FREETZ_AVM_UCLIBC_NPTL_ENABLED}" == "y" ] && TARGET_TOOLCHAIN_ID__UCLIBC_SUFFIX="-nptl"
[ "${FREETZ_KERNEL_VERSION_3_10_MIN}" == "y" ] && TARGET_TOOLCHAIN_ID__KERNEL_SUFFIX="_kernel-${FREETZ_KERNEL_VERSION_MAJOR}"
TARGET_TOOLCHAIN_ID="${FREETZ_TARGET_ARCH_ENDIANNESS_DEPENDENT}_gcc-${FREETZ_TARGET_GCC_VERSION}_uClibc-${FREETZ_TARGET_UCLIBC_VERSION}${TARGET_TOOLCHAIN_ID__UCLIBC_SUFFIX}${TARGET_TOOLCHAIN_ID__KERNEL_SUFFIX}"
PACKAGES_DIR="${PACKAGES_DIR_ROOT}/target-${TARGET_TOOLCHAIN_ID}"
TARGET_SPECIFIC_ROOT_DIR="${PACKAGES_DIR}/root"
HTML_MOD_DIR="${FILESYSTEM_MOD_DIR}/${HTML_SUBDIR}"
# $LUA_MOD_DIR can't be defined here. eg: ${FILESYSTEM_MOD_DIR}/usr/www/all/lua
# $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
if [ -z "$FREETZ_SQUASHFS_BLOCKSIZE" ]; then # expected to be empty only if FREETZ_SQUASHFS_BLOCKSIZE_ORIG is set
echo -n "determining SquashFS block size of the original firmware: "
FREETZ_SQUASHFS_BLOCKSIZE=$("$UNSQUASHFS" -s "$FILESYSTEM" 2>/dev/null | sed -nr 's/Block size ([0-9]+)/\1/p')
[ -z "$FREETZ_SQUASHFS_BLOCKSIZE" ] && error 1 "failed" || echo -e "$((FREETZ_SQUASHFS_BLOCKSIZE/1024)) kB\n"
fi
MKSQUASHFS_OPTIONS+=" -b $FREETZ_SQUASHFS_BLOCKSIZE"
#echo "Using '$MKSQUASHFS_OPTIONS' as MKSQUASHFS_OPTIONS"
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 "%-34s " "$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
"$MKSQUASHFS" $@ $tempf $MKSQUASHFS_OPTIONS 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_PRODUCT="$(sed -rn 's/export CONFIG_PRODUKT_NAME=".*([0-9]{3}[0-9V].*)"/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf | head -n1)" # 4-digits in /etc/init.d/rc.conf
[ -z "${AVM_FW_PRODUCT}" ] && AVM_FW_PRODUCT="$(sed -rn 's/export CONFIG_PRODUKT_NAME="(FRITZ!Box|Speedport|Sinus|Eumex) ([^"]+)"/\2/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf | head -n1 | tr ' ' '-')" # symbolic name in /etc/init.d/rc.conf
if [ -e "${FILESYSTEM_DIR}/etc/init.d/rc.init" ]; then
# /etc/init.d/rc.init is not available in all firmwares
[ -z "${AVM_FW_PRODUCT}" ] && AVM_FW_PRODUCT="$(sed -rn 's/^HW=[0-9]* OEM=all _PRODUKT_NAME=(FRITZ!Box|Speedport|Sinus|Eumex)#//p' ${FILESYSTEM_DIR}/etc/init.d/rc.init | head -n1 | sed 's/#//g;s/Annex.//;s/WLAN/-&/g;s/^$/FritzBox/g')" # symbolic name in /etc/init.d/rc.init
[ -z "${AVM_FW_PRODUCT}" ] && AVM_FW_PRODUCT="$(sed -rn 's/^HW=[0-9]* OEM=all _PRODUKT_NAME=([0-9A-Z\#]{4,6}).*/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.init)" # 4-digits in /etc/init.d/rc.init
fi
[ -z "${AVM_FW_PRODUCT}" ] && warn "Failed to determine AVM_FW_PRODUCT, even though this is a cosmetic bug only please report it to the Freetz developers and provide your .config"
AVM_FW_PRODUCT="${AVM_FW_PRODUCT%% Edition*}"
AVM_FW_PRODUCT="${AVM_FW_PRODUCT% }"
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
if grep -q "^export FIRMWARE_" ${FILESYSTEM_DIR}/etc/version; then # pre-07.08 versions from AVM
AVM_FW_VERSION="$(sed -n 's/^export FIRMWARE_VERSION=.*}\.//p' ${FILESYSTEM_DIR}/etc/version)"
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)"
else # starting with FRITZ!OS 07.08 another location was used for these values
AVM_FW_VERSION="$(sed -n 's/^export CONFIG_VERSION="\(.*\)"$/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf)"
AVM_FW_REVISION="$(sed -n 's/^export CONFIG_BUILDNUMBER="\(.*\)"$/\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf)"
AVM_FW_DATE="$(date +"%d.%m.%Y %H:%M:%S" -d @$(stat -c %Y ${FILESYSTEM_DIR}/etc/version))"
fi
AVM_FW_LABOR="$(sed -rn 's/^export CONFIG_LABOR_ID_NAME="(.*)"/-\1/p' ${FILESYSTEM_DIR}/etc/init.d/rc.conf)"
echo1 -l "detected${FIRMWARE_MAIN} firmware ${AVM_FW_PRODUCT/ /-}${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 --exclude='*.log' . | "$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"
# determine OEMs supported by the original firmware
all_known_oems="1und1 aol arcor avm avme ewetel freenet otwo tcom versatel"
iterate_over_oems_pattern='for[ ]+i[ ]+in[ ]+('"${all_known_oems// /|}"')[^;]*;[ ]*do'
oems=$(sed -n -r -e '/'"${iterate_over_oems_pattern}"'/ { s/^.*for[ ]+i[ ]+in[ ]+([^;]+);[ ]+do.*$/\1/p; q }' "${FIRMWARE_MOD_DIR}/var/install")
[ -z "$oems" ] && error 1 "failed to determine OEMs supported by the original firmware"
echo0 -n "applying symlinks, deleting additional webinterfaces in:"
for webdir in \
${FILESYSTEM_MOD_DIR}/usr/www \
${FILESYSTEM_MOD_DIR}/usr/www.nas \
${FILESYSTEM_MOD_DIR}/usr/www.myfritz \
; do
[ ! -d ${webdir} ] && continue
for edition in ewetel; do
[ ! -d ${webdir}/$edition ] && continue
[ -d ${webdir}/avm ] && continue
[ -d ${webdir}/all ] && continue
# force creation of /usr/www/all/
mv ${webdir}/$edition ${webdir}/avm
done
# some recent LTE firmwares contain both avm and avme OEMs (identical in all firmwares seen so far)
# use the 1st of them (according to the alphabetical order) as the main one
main_oem=$(find "${webdir}" -maxdepth 1 -type d -name "avm*" -printf "%P\n" | sort | head -n 1)
if [ -n "${main_oem}" ]; then
echo0 -n " ${webdir#${FILESYSTEM_MOD_DIR}/}"
mv ${webdir}/${main_oem} ${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)
source "${TOOLS_DIR}/freetz_patch" || error 1 "cannot find script freetz_patch"
PATCHES_DEVICES_DIR_FW_SERIES_SPECIFIC="${PATCHES_DEVICES_DIR}/${FREETZ_TYPE_PREFIX_SERIES_SUBDIR}"
PATCHES_DEVICES_DIR_BOX_SPECIFIC="${PATCHES_DEVICES_DIR_FW_SERIES_SPECIFIC}/${FREETZ_TYPE_PREFIX%_*}"
declare -a PATCHES_DEVICES_DIR_FULL_LIST=($(
for j in \
${PATCHES_DEVICES_DIR_FW_SERIES_SPECIFIC} \
${PATCHES_DEVICES_DIR_BOX_SPECIFIC} \
$( [ -n "${FREETZ_TYPE_PREFIX_LABOR_FIRMWARE}" ] && echo "${PATCHES_DEVICES_DIR_BOX_SPECIFIC}/${FREETZ_TYPE_PREFIX_LABOR_FIRMWARE:1}" ) \
; do
for i in $j $j/${FREETZ_TYPE_LANGUAGE}; do
echo "${i}"
done
done
))
echo1 "applying patches: ${FREETZ_TYPE_PREFIX_SERIES_SUBDIR}/${FREETZ_TYPE_PREFIX}${FREETZ_TYPE_PREFIX_LABOR_FIRMWARE}-${FREETZ_TYPE_LANGUAGE} (${PATCHES_DEVICES_DIR_FULL_LIST[@]#${PATCHES_DEVICES_DIR}/})"
# Apply patches
if [ ! -d "${PATCHES_DEVICES_DIR_BOX_SPECIFIC}" -a ! -d "${PATCHES_DEVICES_DIR_FW_SERIES_SPECIFIC}" ]; then
error 1 "missing ${PATCHES_DEVICES_DIR_BOX_SPECIFIC}"
fi
# Execute firmware-series-specific and box-specific patch scripts (both common for all languages and language-specific)
shopt -s nullglob
for j in ${PATCHES_DEVICES_DIR_FULL_LIST[@]}; do
for i in $j/*.sh; do
echo2 "applying patch file $i"
source $i
done
done
shopt -u nullglob
# 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
# Now check language specific html directory (if there is any)
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
echo1 "Language specific HTML directory: ${HTML_SPEC_MOD_DIR:-none}"
if [ -z "$HTML_SPEC_MOD_DIR" ]; then
HTML_SPEC_MOD_DIR="${HTML_LANG_MOD_DIR}/html/doesnotexist"
FREETZ_AVM_HAS_ONLY_LUA=y
[ -e "${FILESYSTEM_MOD_DIR}/usr/www/all/css/rd/images/fritzLogo.svg" ] && FREETZ_AVM_HAS_LUA_SCALABLE=y
fi
# Check for LUA directory
LUA_MOD_DIR="${FILESYSTEM_MOD_DIR}/usr/lua"
[ -d "$LUA_MOD_DIR" ] || LUA_MOD_DIR="${HTML_LANG_MOD_DIR}/lua"
[ -d "$LUA_MOD_DIR" ] && echo1 "LUA directory: ${LUA_MOD_DIR}" || FREETZ_AVM_HAS_ONLY_HTML=y
# Determine $MODULES_DIR in AVM image, eg: "${FILESYSTEM_MOD_DIR}/lib/modules/2.6.19.2
MODULES_DIR="$(find ${FILESYSTEM_MOD_DIR}/lib/modules/2.6.* ${FILESYSTEM_MOD_DIR}/lib/modules/[34].*.* -maxdepth 0 -type d 2>/dev/null)"
[ ! -d "$MODULES_DIR" ] && error 1 "Cannot find modules dir."
# Set $MODULES_SUBDIR relative to root dir in AVM image, eg: "lib/modules/2.6.19.2
MODULES_SUBDIR="${MODULES_DIR#${FILESYSTEM_MOD_DIR}/}"
# Apply firmware-series-specific and box-specific patches (both common for all languages and language-specific)
shopt -s nullglob
for ((j=0; j<${#PATCHES_DEVICES_DIR_FULL_LIST[@]}; j++)); do
for i in ${PATCHES_DEVICES_DIR_FULL_LIST[$j]}/*.patch; do
# Check if a more specific version of the patch exists,
# i.e. define an order on all patches with the same name
# FW_SERIES/XY.patch
# FW_SERIES/LANG/XY.patch
# FW_SERIES/BOXID/XY.patch
# FW_SERIES/BOXID/LANG/XY.patch
# and do NOT apply patch XY.patch from dir FOO
# if it also exists in a directory with a higher order.
#
# This makes it possible in situations where one box needs
# a box-specific version of the patch to have just two versions
# of it: one box-specific and one for all other boxes.
i_basename=$(basename "$i")
for ((jj=j+1; jj<${#PATCHES_DEVICES_DIR_FULL_LIST[@]}; jj++)); do
if [ -f "${PATCHES_DEVICES_DIR_FULL_LIST[$jj]}/${i_basename}" ]; then
continue 2
fi
done
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
# Determine Freetz version
REVISION=$(${TOOLS_DIR}/freetz-version 2>/dev/null)
FREETZ_VERSION="freetz${REVISION:+-}${REVISION}"
echo1 "setting freetz-version '${FREETZ_VERSION}'"
echo "${FREETZ_VERSION}" > "${FILESYSTEM_MOD_DIR}/etc/.freetz-version"
PACKAGES_LIST_FILE="${BASE_DIR}/.packages"
rm -f "${PACKAGES_LIST_FILE}"
for pkg in $(static_packages) $(static_addons); do
echo "$pkg" >> "${PACKAGES_LIST_FILE}"
done
# Execute general patch scripts
for i in "${PATCHES_SCRIPTS_DIR}/"*.sh; do
[ -r "$i" ] || continue
echo2 "applying patch file $i"
source $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=
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/'"${iterate_over_oems_pattern}"'/for i in '"$oem_list"' ; do/' "${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 to the firmware
if [ "$FREETZ_AVMPLUGINS_INTEGRATE" == "y" ]; then
echo0 "integrating 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"
;;
wlan)
modsed -r \
's,(if) (.+ plugin-wlan),\1 test -d "/usr/share/avmplugins/plugin-wlan" || \2,' \
"${FILESYSTEM_MOD_DIR}/etc/init.d/rc.wlan"
;;
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}${FREETZ_LIBRARY_DIR}"
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"
mkdir -p "${VARTAR_MOD_DIR}/var/mod/etc/onlinechanged"
mkdir -p "${VARTAR_MOD_DIR}/var/mod/etc/cron.d"
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
echo0 "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_}
if [ "$FREETZ_KEEP_AVM_UCLIBC" == "y" ]; then
# libutil is actually also a uClibc lib, but it's not provided by AVM, so keep installing Freetz version
if echo "${bn}" | grep -qE '^(ld_uClibc|libcrypt|libdl|libm|libpthread|librt|libthread_db|libubacktrace|libuClibc)$'; then
echo1 "keeping AVM version of uClibc library $bn"
continue
fi
fi
fn=${bn//_/[-+._]}
sizeinfo "$bn"
for dn in lib usr/lib ${FREETZ_LIBRARY_DIR#/} 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}/"
if [ "${FREETZ_AVM_HAS_INNER_OUTER_FILESYSTEM}" == "y" ]; then
wrapper_fs_required_libs="ld_uClibc libuClibc"