-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-firmware.sh
executable file
·200 lines (172 loc) · 5.2 KB
/
build-firmware.sh
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
#!/bin/bash
BINDIR=`dirname $0`
. "$BINDIR/common.inc"
DIR="$1"
NEXT_PARAM=""
if [ "$1" == "-h" ]; then
echo "Usage: $0 [FMK directory] [-nopad | -min]"
exit 1
fi
if [ "$DIR" == "" ] || [ "$DIR" == "-nopad" ] || [ "$DIR" == "-min" ]; then
DIR="fmk"
NEXT_PARAM="$1"
else
NEXT_PARAM="$2"
fi
# Need to extract file systems as ROOT
if [ "$UID" != "0" ]; then
SUDO="sudo"
else
SUDO=""
fi
DIR=$(readlink -f $DIR)
# Make sure we're operating out of the FMK directory
cd $(dirname $(readlink -f $0))
# Order matters here!
eval $(cat shared-ng.inc)
eval $(cat $CONFLOG)
FSOUT="$DIR/new-filesystem.$FS_TYPE"
printf "Firmware Mod Kit (build) ${VERSION}, (c)2011-2013 Craig Heffner, Jeremy Collake\n\n"
if [ ! -d "$DIR" ]; then
echo -e "Usage: $0 [build directory] [-nopad]\n"
exit 1
fi
# Always try to rebuild, let make decide if necessary
Build_Tools
echo "Building new $FS_TYPE file system... (this may take several minutes!)"
# Clean up any previously created files
rm -rf "$FWOUT" "$FSOUT"
# Build the appropriate file system
case $FS_TYPE in
"squashfs")
# Check for squashfs 4.0 realtek, which requires the -comp option to build lzma images.
if [ "$FS_COMPRESSION" == "lzma" ]; then
if [ "$(echo $MKFS | grep 'squashfs-4.0-realtek')" != "" ] || [ "$(echo $MKFS | grep 'squashfs-4.2')" != "" ]; then
COMP="-comp lzma"
else
COMP=""
fi
fi
# Mksquashfs 4.0 tools don't support the -le option; little endian is built by default
if [ "$(echo $MKFS | grep 'squashfs-4.')" != "" ] && [ "$ENDIANESS" == "-le" ]; then
ENDIANESS=""
fi
# Increasing the block size minimizes the resulting image size (larger dictionary). Max block size of 1MB.
if [ "$NEXT_PARAM" == "-min" ]; then
echo "Blocksize override (-min). Original used $((FS_BLOCKSIZE/1024))KB blocks. New firmware uses 1MB blocks."
FS_BLOCKSIZE="$((1024*1024))"
fi
# if blocksize var exists, then add '-b' parameter
if [ "$FS_BLOCKSIZE" != "" ]; then
BS="-b $FS_BLOCKSIZE"
HR_BLOCKSIZE="$(($FS_BLOCKSIZE/1024))"
echo "Squashfs block size is $HR_BLOCKSIZE Kb"
fi
$SUDO $MKFS "$ROOTFS" "$FSOUT" $ENDIANESS $BS $COMP -all-root
;;
"cramfs")
$SUDO $MKFS "$ROOTFS" "$FSOUT"
if [ "$ENDIANESS" == "-be" ]; then
mv "$FSOUT" "$FSOUT.le"
./src/cramfsswap/cramfsswap "$FSOUT.le" "$FSOUT"
rm -f "$FSOUT.le"
fi
;;
"yaffs")
$SUDO $MKFS "$ROOTFS" "$FSOUT"
echo "WARNING: YAFFS2 completely untested !! Hit any key to confirm ..."
pause
;;
*)
echo "Unsupported file system '$FS_TYPE'!"
;;
esac
if [ ! -e $FSOUT ]; then
echo "Failed to create new file system! Quitting..."
exit 1
fi
# Append the new file system to the first part of the original firmware file
cp $HEADER_IMAGE $FWOUT
$SUDO cat $FSOUT >> $FWOUT
# Calculate and create any filler bytes required between the end of the file system and the footer / EOF.
CUR_SIZE=$(ls -l $FWOUT | awk '{print $5}')
((FILLER_SIZE=$FW_SIZE-$CUR_SIZE-$FOOTER_SIZE))
if [ "$FILLER_SIZE" -lt 0 ]; then
echo "ERROR: New firmware image will be larger than original image!"
echo " Building firmware images larger than the original can brick your device!"
echo " Try re-running with the -min option, or remove any unnecessary files."
echo " REFUSING to create new firmware image."
echo ""
echo " Original file size: $FW_SIZE"
echo " Current file size: $CUR_SIZE (plus footer of $FOOTER_SIZE bytes)"
echo ""
echo " Quitting..."
# rm -f "$FWOUT" "$FSOUT"
exit 1
else
if [ "$NEXT_PARAM" != "-nopad" ]; then
echo "Remaining free bytes in firmware image: $FILLER_SIZE"
perl -e "print \"\xFF\"x$FILLER_SIZE" >> "$FWOUT"
else
echo "Padding of firmware image disabled via -nopad"
fi
fi
# Append the footer to the new firmware image, if there is any footer
if [ "$FOOTER_SIZE" -gt "0" ]; then
cat $FOOTER_IMAGE >> "$FWOUT"
fi
CHECKSUM_ERROR=0
# Calculate new checksum values for the firmware header
# trx, dlob, uimage
# Buffalo and some other post-processors obfuscate these images
# so we must akways try prior to vendor processing below
./src/crcalc/crcalc "$FWOUT" "$BINLOG"
if [ $? -ne 0 ]; then
CHECKSUM_ERROR=1
fi
# Vendor specific post-processing
# For some images, will apply checksum (TP-Link)
# Others, will encrypt image (Buffalo)
case $HEADER_TYPE in
"tp-link")
printf "\nCorrecting TP-Link firmware image ... "
src/tpl-tool/src/tpl-tool -x "$FWOUT"
if [ $? -ne 0 ]; then
CHECKSUM_ERROR=1
printf "ERROR"
else
src/tpl-tool/src/tpl-tool -b "$FWOUT"
mv "$FWOUT-new" "$FWOUT"
src/tpl-tool/src/tpl-tool -s "$FWOUT"
if [ $? -ne 0 ]; then
CHECKSUM_ERROR=1
printf "ERROR"
else
printf "Done"
CHECKSUM_ERROR=0
fi
fi
rm -f "$FWOUT-header" "$FWOUT-kernel" "$FWOUT-rootfs"
;;
"buffalo")
printf "\nEncrypting Buffalo firmware image ... "
src/firmware-tools/buffalo-enc -i "$FWOUT" -o "$FWOUT.enc"
if [ $? -ne 0 ]; then
CHECKSUM_ERROR=1
printf "ERROR"
else
printf "saved as $FWOUT.enc"
fi
;;
*)
;;
esac
if [ $CHECKSUM_ERROR -eq 0 ]; then
printf "\nFinished! "
else
printf "\nFirmware header not supported; firmware checksums may be incorrect. "
fi
if [ -e "$FSOUT" ]; then
rm -f "$FSOUT"
fi
printf "\nNew firmware image has been saved to: $FWOUT\n"