forked from Freetz-NG/freetz-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fwmod
executable file
·2215 lines (1953 loc) · 92.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
#! /usr/bin/env bash
# Freetz script to modify AVM FRITZ!Box and OEM firmware images
#
# Copyright (C) 2005-2006 Daniel Eiband <[email protected]>
# Copyright (C) 2006-2018 by the Freetz developers (http://freetz.org)
# Copyright (C) 2019 by Freetz developers (https://freetz-ng.github.io/ and https://freetz.github.io)
#
# 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] [-s] [-v <pub> [-w <pub> [-x <pub>]]] [-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
-v <pub> validate firmware image with the pub key (not for recovery.exe)
-w <pub> validate 2nd firmware image with the pub key (not for recovery.exe)
-x <pub> validate 3rd firmware image with the pub key (not for recovery.exe)
-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 fakeroot/pseudo 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; DO_VALIDATE=0; DO_VALIDATE2=0; DO_VALIDATE3=0
FIRMWARE_NOCOMPILE=0; FORCE_PACK=0; DO_ZIP=0; COPY_FS_DIR=; DIR=
# Parse command line parameters
while getopts umpsanfzc:i:d:v:w:x: opt; do
case "$opt" in
u) DO_UNPACK=1 ;;
m) DO_MOD=1 ;;
p) DO_PACK=1 ;;
v) DO_VALIDATE=1; FIRMWARE_PUB="$OPTARG" ;;
w) DO_VALIDATE2=1; FIRMWARE2_PUB="$OPTARG" ;;
x) DO_VALIDATE3=1; FIRMWARE3_PUB="$OPTARG" ;;
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 + $DO_VALIDATE)) -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)
[ ! -x "${TOOLS_DIR}/busybox" ] && \
error 1 "Can not find host tools, please run 'make tools' first."
[ "$DO_SIGN" -gt 0 -a $(($DO_PACK + $DO_ZIP)) -eq 0 ] && \
error 1 "Firmware signing without packing is not supported. Please use PeterPawn's sign_image tool to sign an already existing .image file."
[ $(($DO_SIGN + $DO_VALIDATE + $DO_VALIDATE2 + $DO_VALIDATE3 )) -gt 0 -a ! -x "$(command -v openssl)" ] && \
error 1 "OpenSSL is required to verify and create image signature. Please install it or disable the option(s) in menuconfig."
# 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 not yet exists (not in validate only mode)
[ $(($DO_UNPACK + $DO_MOD + $DO_PACK + $DO_ZIP )) -ne 0 ] && mkdir -p "$DIR"
#################################
## Initialise script variables ##
#################################
# Relative subdirectories
SOURCE_SUBDIR="source"
FIRMWARE_SUBDIR="firmware"
FITIMAGE_SUBDIR="fit-image"
FILESYSTEM_SUBDIR="filesystem"
FILESYSTEM_OUTER_SUBDIR="filesystem.outer"
AVMPLUGINS_SUBDIR="plugins.image"
EXTERNAL_SUBDIR="external"
KERNEL_SUBDIR="kernel"
VARTAR_SUBDIR="var.tar"
SIGNATURE_SUBDIR=".signature"
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"
TAR_GNU_TOOL="tar-gnu"
MD5SUM_TOOL="md5sum"
BUSYBOX_TOOL="busybox"
# Optional tools
PV="$(command -v pv >/dev/null && echo 'pv -rcb')"
# 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"
TAR="${TOOLS_DIR}/${TAR_TOOL}"
TAR_GNU="${TOOLS_DIR}/${TAR_GNU_TOOL}"
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
PACKAGES_LIST_FILE="${BASE_DIR}/.packages"
STATIC_ADDON_FILES="${ADDON_DIR}/*.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}
[ "$FREETZ_VERBOSITY_LEVEL" -lt 2 ] && PV=''
# 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 -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
#################################
## Validate firmware signature ##
#################################
# validate_signature
# $1 ("1") when signature validating is enabled
# $2 (img) the .image file to validate
# $3 (key) public key, format: "EXPONENTxMODULUS". If no EXPONENT is given, default 010001 will be used (as in all images at the moment)
validate_signature() {
echo -n "checking signature: "
[ "$1" != "1" ] && echo -e "\033[1;33mdisabled\033[0m" && return
[ -z "$3" ] && echo -e "\033[1;34munknown\033[0m" && error 1 "no public key available to validate signature"
# var
local TMP="$(mktemp -dt freetz-sig-XXX)"
local MOD="${3#*x}"
local EXP="${3%x*}"
[ "$EXP" == "$MOD" ] && EXP='010001'
if [ "$FREETZ_FWMOD_VALIDATE_METHOD_SI" == "y" ]; then
echo -e "$MOD\n$EXP" > "$TMP/sig"
${TOOLS_DIR}/yf/signimage/yf_check_signature "$2" -a "$TMP/sig" 2> "$TMP/out"
RET=$?
[ $RET -ne 0 ] && echo -e "\033[0;31mfailed\033[0m" && cat "$TMP/out"
rm -rf "$TMP"
[ $RET -ne 0 ] && error $RET "image could not be verified with public key"
else
# img
cat "$2" > "$TMP/img"
# fix
local FIX="$(mktemp -p $TMP)"
"$TAR_GNU" -f "$TMP/img" --owner=0 --group=0 --numeric-owner --format=oldgnu -C "$TMP" --append ${FIX##*/}
# sig
"$TAR_GNU" -f "$TMP/img" --owner=0 --group=0 --numeric-owner --format=oldgnu -C "$TMP" --extract ./var/signature --transform='s!.*!sig!'
"$TAR_GNU" -f "$TMP/img" --owner=0 --group=0 --numeric-owner --format=oldgnu --delete ./var/signature ${FIX##*/}
# key
{
echo -n 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQ==' | base64 -d
echo -n "$MOD" | while read -n2 x; do echo -ne "\x$x"; done
echo -ne "\x2"
echo -ne "\x$((${#EXP}/2))"
echo -n "$EXP" | while read -n2 x; do echo -ne "\x$x"; done
} > "$TMP/key"
# chk
VAL="$(openssl md5 -verify "$TMP/key" -keyform der -signature "$TMP/sig" "$TMP/img" 2>&1)"
RET="$?"
# clr
rm -rf "$TMP"
# act
[ "$RET" != "0" ] && echo -e "\033[0;31mfailed\033[0m\nOpenSSL: $VAL" && error 1 "image could not be verified with public key"
fi
echo -e "\033[0;32mvalid\033[0m"
}
# validate_signature only
[ $DO_VALIDATE -eq 1 -a $(($DO_UNPACK + $DO_MOD + $DO_PACK + $DO_ZIP)) -eq 0 ] && validate_signature "$DO_VALIDATE" "$FIRMWARE" "$FIRMWARE_PUB" && exit 0
##############################################
## Initialise fakeroot/pseudo incl. caching ##
##############################################
# Create id string based on MD5 hashes of firmware images
FIRMWARE_MD5="$("$MD5SUM" "$FIRMWARE" | cut -d ' ' -f 1)"
[ "$FIRMWARE2" ] && FIRMWARE_MD5="$FIRMWARE_MD5-$("$MD5SUM" "$FIRMWARE2" | cut -d ' ' -f 1)"
[ "$FREETZ_ROOTEMU_FAKEROOT" != 'y' ] && FIRMWARE_MD5="$FIRMWARE_MD5-pseudo" || FIRMWARE_MD5="$FIRMWARE_MD5-fakeroot"
FAKEROOT_CACHE_DIR="$ABS_BASE_DIR/.fakeroot-cache"
[ "$FREETZ_ROOTEMU_FAKEROOT" != 'y' ] && FAKEROOT_CACHE_DB="$FAKEROOT_CACHE_DIR/files.db" || FAKEROOT_CACHE_DB="$FAKEROOT_CACHE_DIR/fakeroot.cvs"
# Are we in a fakeroot/pseudo context?
if [ -n "$FWMOD_RECURSIVE" ] && [ $UID -eq 0 ]; then
# Yes -> fakeroot/pseudo is active
# Make sure fakeroot/pseudo call came from ourselves so we can assume the cache settings to be correct
if [ $((FWMOD_RECURSIVE)) -eq 0 ] || [ "./$SELF" != "$(cat /proc/$FWMOD_RECURSIVE/cmdline | tr '\0' '\n' | sed -n 2p)" ]; then
error 1 "$SELF must not run in a fakeroot/pseudo environment"
fi
# Has current combination of firmware already been unpacked?
if [ "$(cat "$UNPACKED_FILE" 2>/dev/null)" == "$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/pseudo is inactive
if [ "$(cat "$UNPACKED_FILE" 2>/dev/null)" != "$FIRMWARE_MD5" ]; then
# Dont keep old databases on a new unpack
mkdir -p "$FAKEROOT_CACHE_DIR"
rm -f $FAKEROOT_CACHE_DIR/*
fi
if [ "$FREETZ_ROOTEMU_FAKEROOT" != 'y' ]; then
PSEUDO_PREFIX="$TOOLS_DIR/build" PSEUDO_LOCALSTATEDIR="$FAKEROOT_CACHE_DIR" "$TOOLS_DIR/build/bin/pseudo" -B &>/dev/null #scan
FWMOD_RECURSIVE="$$" PSEUDO_PREFIX="$TOOLS_DIR/build" PSEUDO_LOCALSTATEDIR="$FAKEROOT_CACHE_DIR" PSEUDO_DEBUG="0" "$TOOLS_DIR/build/bin/pseudo" '--' "${CMDLINE_ORIG[@]}"
PSEUDO_PREFIX="$TOOLS_DIR/build" PSEUDO_LOCALSTATEDIR="$FAKEROOT_CACHE_DIR" "$TOOLS_DIR/build/bin/pseudo" -S &>/dev/null #halt
else
# Has current combination of firmware already been unpacked?
if [ "$(cat "$UNPACKED_FILE" 2>/dev/null)" == "$FIRMWARE_MD5" ]; then
# Yes -> use existing cache and firmwares (no unpack necessary)
NON_FAKEROOT_USERNAME=$(id -u -n) FWMOD_RECURSIVE=$$ $TOOLS_DIR/build/bin/fakeroot -i "$FAKEROOT_CACHE_DB" -s "$FAKEROOT_CACHE_DB" '--' "${CMDLINE_ORIG[@]}"
else
# No -> create new fakeroot cache and unpack firmwares
NON_FAKEROOT_USERNAME=$(id -u -n) FWMOD_RECURSIVE=$$ $TOOLS_DIR/build/bin/fakeroot -s "$FAKEROOT_CACHE_DB" '--' "${CMDLINE_ORIG[@]}"
fi
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/pseudo)
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"
fi
echo0 -b "FINISHED"
exit 0
fi
############################################
## Unpack and unsquash the firmware image ##
############################################
ORG_DIR="${DIR}/original"
FIRMWARE_DIR="${ORG_DIR}/${FIRMWARE_SUBDIR}"
FITIMAGE_DIR="${ORG_DIR}/${FITIMAGE_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}"
echo
STEP="1"
if [ "$DO_UNPACK" -gt 0 ]; then
[ "$FIRMWARE2" ] && UNPACK_SUBSTEP="A" && UNPACK_MAIN=" 1ST"
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"
REAL_FIRMWARE="$(readlink ${FIRMWARE} 2>/dev/null || echo ${FIRMWARE##*/})"
echo2 -l "source firmware file: ${FIRMWARE/${FIRMWARE##*\/}/$REAL_FIRMWARE}"
rm -rf "$ORG_DIR"
mkdir "$ORG_DIR"
# validate_signature
if [ "$FIRMWARE_EXTENSION" == "cvc" ]; then
[ $DO_VALIDATE -ne 1 ] && validate_signature "$DO_VALIDATE"
elif [ "$FIRMWARE_EXTENSION" != "exe" ]; then
validate_signature "$DO_VALIDATE" "$FIRMWARE" "$FIRMWARE_PUB"
else
validate_signature "0"
fi
echo "unpacking firmware image"
mkdir "$FIRMWARE_DIR"
if [ "$FIRMWARE_EXTENSION" == "cvc" ]; then
# Check and skip garbage (cvc&p7b)
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)"
[ -n "$image_grbg" ] && echo "Skipping $(($image_grbg)) Bytes garbage"
# validate_signature
if [ $DO_VALIDATE -eq 1 ]; then
TMP_SIG="$(mktemp -t freetz-sig-XXX)"
tail -c $(( $image_size - ${image_grbg:-0x0} )) "$FIRMWARE" > "$TMP_SIG"
validate_signature "$DO_VALIDATE" "$TMP_SIG" "$FIRMWARE_PUB"
rm "$TMP_SIG"
fi
# Unpack standard *.image file in tar format
tail -c $(( $image_size - ${image_grbg:-0x0} )) "$FIRMWARE" | \
"$TAR_GNU" -x --owner=0 --group=0 --numeric-owner -C "$FIRMWARE_DIR" || untar_failed=1
elif [ "$FIRMWARE_EXTENSION" != "exe" ]; then
# Unpack standard *.image file in tar format
"$TAR_GNU" -xif "$FIRMWARE" --owner=0 --group=0 --numeric-owner -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" ] || error 1 "unpacking failed"
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
if [ -e "$FIRMWARE_DIR/var/firmware-update.uimg" ]; then
echo0 "unpacking uimg file"
"${TOOLS_DIR}/uimg" -u "$FIRMWARE_DIR/var/firmware-update.uimg" >/dev/null
chmod +w $FIRMWARE_DIR/var/*.bin
mkdir -p "$FIRMWARE_DIR/var/remote/var/tmp/x86"
ln $FIRMWARE_DIR/var/*_ATOM_ROOTFS.bin "$FIRMWARE_DIR/var/remote/var/tmp/x86/filesystem.image"
ln $FIRMWARE_DIR/var/*_ATOM_KERNEL.bin "$FIRMWARE_DIR/var/remote/var/tmp/x86/kernel.image"
elif [ -e "$FIRMWARE_DIR/var/tmp/fit-image" ]; then
echo0 "unpacking fitimg file"
$TOOLS_DIR/fit.sh unpack "$FIRMWARE_DIR/var/tmp/fit-image" "$FITIMAGE_DIR" || error 1 "unpacking failed"
for x in kernel filesystem; do ln $FITIMAGE_DIR/$x.image $FIRMWARE_DIR/var/tmp/; done
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
# Is cpio up to date ?
if [ "$FREETZ_AVM_PROP_INNER_FILESYSTEM_TYPE_CPIO" == "y" ]; then
[ -z "$(cpio --version 2>/dev/null | sed -nr 's/^cpio .* 2\.([0-9]*)(\.[0-9]+)*/\1/p' | sed -r 's/^([0-9]|10|11)$//')" ] && \
error 1 "cpio version 2.12 is required"
fi
# 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" --owner=0 --group=0 --numeric-owner -C "$AVMPLUGINS_DIR" || error 1 "tar failed"
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" --owner=0 --group=0 --numeric-owner -C "$VARTAR_DIR" || error 1 "tar failed"
image_details
echo2 -lc "detected firmware version: ${SOURCE_IMAGE_DETAILS}"
echo -e "done.\n"
# Unpack secondary firmware image
if [ -n "$FIRMWARE2" ]; then
rm -rf "${DIR}/.tk"
# validate_signature
[ $DO_VALIDATE2 -eq 1 ] && UNPACK_VALIDATE2="-v $FIRMWARE2_PUB"
( set -o pipefail; "$0" -u $UNPACK_VALIDATE2 -d "${DIR}/.tk" "$FIRMWARE2" |
sed "s/STEP 1: UNPACK/STEP 1B: UNPACK 2ND${FORCE_UNPACK}/" )
[ $? -ne 0 ] && error 1 "unpacking failed"
fi
# Unpack auxiliary firmware image
if [ -n "$FIRMWARE3" ]; then
rm -rf "${DIR}/.aux"
# validate_signature
[ $DO_VALIDATE3 -eq 1 ] && UNPACK_VALIDATE3="-v $FIRMWARE3_PUB"
( set -o pipefail; "$0" -u $UNPACK_VALIDATE3 -d "${DIR}/.aux" "$FIRMWARE3" |
sed "s/STEP 1: UNPACK/STEP 1C: UNPACK 3RD${FORCE_UNPACK}/" )
[ $? -ne 0 ] && error 1 "unpacking failed"
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}"
FITIMAGE_MOD_DIR="${MOD_DIR}/${FITIMAGE_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_SYSTEM_TYPE_BCM63138}" == "y" ] && TARGET_TOOLCHAIN_ID__GCC_SUFFIX="-bcm"
[ "${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}${TARGET_TOOLCHAIN_ID__GCC_SUFFIX}_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
# $MENU_DATA_LUA will be set below to ${FILESYSTEM_MOD_DIR}/usr/lua/menu_data.lua or ${HTML_LANG_MOD_DIR}/menus/menu_data.lua or filenotavailable
# $SYSTEMD_CORE_MOD_DIR will be set below to ${FILESYSTEM_MOD_DIR}/etc/boot.d/core if it exists
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:0:31}" | 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
;;
dir) #addons/plugins: 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
}
image_details
STEP="2"
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
[ -n "$PV" ] && echo0 "preparing directory build/modified/"
mkdir -p "$MOD_DIR"
[ "$FREETZ_AVM_HAS_UDEV" == y ] || exclude_dev="--exclude=./filesystem/dev"
"$TAR_GNU" -c --owner=0 --group=0 --numeric-owner -C "$ORG_DIR" $exclude_dev --exclude='*.log' . | ${PV:-cat} | "$TAR" -x -C "$MOD_DIR" || error 1 "tar failed"
# 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 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" ] && for oem in ${FILESYSTEM_MOD_DIR}/etc/default.Fritz_Box_*/*; do oems="$oems ${oem##*/}"; done
[ -z "$oems" ] && error 1 "failed to determine OEMs supported by the original firmware"
echo0 -n "applying symlinks, deleting additional webinterfaces"
echo1 -ln " 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
echo1 -ln " ${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
))
# Apply patches
if [ -d "$FREETZ_TYPE_PREFIX_SERIES_SUBDIR" ]; then
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}/})"
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
# Find menu_data.lua
MENU_DATA_LUA="filenotavailable"
for dir in "${FILESYSTEM_MOD_DIR}/usr/lua" "${HTML_LANG_MOD_DIR}/menus"; do
[ -f "$dir/menu_data.lua" ] && MENU_DATA_LUA="$dir/menu_data.lua" && break
done
# Check systemd direcotry
[ -d "${FILESYSTEM_MOD_DIR}/etc/boot.d/core" ] && SYSTEMD_CORE_MOD_DIR="${FILESYSTEM_MOD_DIR}/etc/boot.d/core" || SYSTEMD_CORE_MOD_DIR=""
# 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
[ "$FREETZ_AVM_VERSION_07_0X_MIN" == "y" -o -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/[2345].[0123456789]*.* -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, /home and /ftp"
rm -rf "$FILESYSTEM_MOD_DIR"/tmp
ln -s var/{tmp,mod} "$FILESYSTEM_MOD_DIR"/
ln -s var/mod/home "$FILESYSTEM_MOD_DIR"/
ln -s var/media/ftp "$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
FREETZ_VERSION="$(${TOOLS_DIR}/freetz_revision fwmod)"
echo1 "setting freetz-version '${FREETZ_VERSION}'"
echo "${FREETZ_VERSION}" > "${FILESYSTEM_MOD_DIR}/etc/.freetz-version"
ln -f "${FILESYSTEM_MOD_DIR}/etc/.freetz-version" "${FILESYSTEM_MOD_DIR}/etc/.freetz-ng-version"
# Execute general patch scripts
for i in "${PATCHES_SCRIPTS_DIR}/"*.sh; do
[ -r "$i" ] || continue
echo2 -l "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
echo1 -l "patches applied"
# remove oems
echo1 -ln "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"
for i in $oems; do
if [ "$(eval "echo \"\$FREETZ_REMOVE_BRANDING_${i}\"")" == "y" ]; then
echo1 -ln " $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