forked from mirror/firmware-mod-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-firmware.sh
executable file
·182 lines (155 loc) · 4.42 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
#!/bin/bash
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-ng) ${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
# Check if FMK has been built, and if not, build it
if [ ! -e "./src/crcalc/crcalc" ]
then
echo "Firmware-Mod-Kit has not been built yet. Building..."
cd src && ./configure && make
if [ $? -eq 0 ]
then
cd -
else
echo "Build failed! Quitting..."
exit 1
fi
fi
echo "Building new $FS_TYPE file system..."
# 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 [ "$(echo $MKFS | grep 'squashfs-4.0-realtek')" != "" ] && [ "$FS_COMPRESSION" == "lzma" ]
then
COMP="-comp lzma"
else
COMP=""
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"
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
;;
*)
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 from the file system."
echo " Refusing to create new firmware image."
echo ""
echo " Original file size: $FW_SIZE"
echo " Current file size: $CUR_SIZE"
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
# Calculate new checksum values for the firmware header
# trx, dlob, uimage
./src/crcalc/crcalc "$FWOUT" "$BINLOG"
if [ $? -eq 0 ]
then
echo -n "Finished! "
else
echo -n "Firmware header not supported; firmware checksums may be incorrect. "
fi
# if a Buffalo image, then run encrypter - base on image name
if [ "$(echo $FWOUT | grep -i 'buffalo')" != "" ]
then
# product name, version, key, encryption type can be specified here
$KEY="" # specify full param, e.g. -k mykey
$MAGIC=""
$PRODUCT=""
$LONGSTATE=""
./src/firmware-tools/buffalo-enc -i $FWOUT -o $FWOUT.buffalo.enc $KEY $MAGIC $PRODUCT $LONGSTATE
#if [ $? -eq 0 ]
#then
# echo "Encrypted Buffalo image created."
#else
# echo "ERROR creating an encrypted Buffalo image"
#fi
fi
echo "New firmware image has been saved to: $FWOUT"