forked from faux123/mkbootimg_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkboot
executable file
·292 lines (275 loc) · 9.45 KB
/
mkboot
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
#!/bin/bash
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#boot.img tool
#by xiaolu
trap "clean" 2 3 4
workdir=$(pwd)
toolpath=$(readlink -f $0)
tooldir=$(dirname $toolpath)
mkbootimg_dtb=$tooldir/mkbootimg_dtb
mkbootimg_old=$tooldir/mkbootimg
old_bootimg=true
C_OUT="\033[0;1m"
C_ERR="\033[31;1m"
C_CLEAR="\033[0;0m"
pout() {
printf "${C_OUT}${*}${C_CLEAR}\n"
}
perr() {
printf "${C_ERR}${*}${C_CLEAR}\n"
}
clean()
{
rm -rf /tmp/mkboot.*
pout "..."
exit
}
usage()
{
pout "<Unpack and repack boot.img tool>\n"
pout "Not enough parameters or parameter error!"
pout "unpack boot.img & decompress ramdisk:\n $(basename $0) [img] [output dir]"
pout " $(basename $0) boot.img boot20130905"
pout "Use the unpacked directory repack boot.img(img_info):\n $(basename $0) [unpacked dir] [newbootfile]"
pout " $(basename $0) boot20130905 newboot.img"
pout "Use the boot.img and new ramdisk repack boot.img:\n $(basename $0) [img] [ramdisk.gz or ramdisk dir] [newbootfile]"
pout " $(basename $0) boot.img boot20130905/ramdisk newboot.img"
clean
}
print_info()
{
pout " kernel : $kernel"
pout " ramdisk : $ramdisk"
pout " page size : $page_size"
pout " kernel size : $kernel_size"
pout " ramdisk size : $ramdisk_size"
[ ! -z $second_size ] && [ $second_size -gt 0 ] && \
pout " second_size : $second_size"
if $old_bootimg ; then
pout " base : $base_addr"
pout " kernel addr : $kernel_addr"
pout " ramdisk addr : $ramdisk_addr"
[ -z $second_addr ] || pout " second_addr : $second_addr"
pout " tags addr : $tags_addr"
else
pout " dtb size : $dtb_size"
pout " base : $base_addr"
pout " kernel offset : $kernel_offset"
pout " ramdisk offset : $ramdisk_offset"
[ -z $second_offset ] || pout " second_offset : $second_offset"
pout " tags offset : $tags_offset"
pout " dtb img : $dt"
fi
pout " cmd line : $cmd_line"
}
mkboot_img()
{
error=0
if $old_bootimg ; then
$mkbootimg_old --kernel $kernel --ramdisk $ramdisk \
--base $base_addr --ramdiskaddr $ramdisk_addr \
--cmdline "$cmd_line" -o $1 || error=1
else
$mkbootimg_dtb --kernel $kernel --ramdisk $ramdisk \
--base $base_addr --ramdisk_offset $ramdisk_offset \
--tags_offset $tags_offset --cmdline "$cmd_line" \
--dt $dt -o $1 || error=1
fi
[ $error -eq 1 ] && return $error
ramdisk_size=$(stat -c "%s" $ramdisk)
boot_size=$(stat -c "%s" $1)
pout "Kernel size: $kernel_size, new ramdisk size: $ramdisk_size, $(basename $1): $boot_size."
pout "$(basename $1) has been created."
}
#decide action
[ $# -lt 2 ] || [ $# -gt 3 ] && usage
if [ $# -eq 2 ] && [ -d $1 ]; then
mkboot_from_dir=1
elif [ $# -eq 2 ] && [ -s $1 ]; then
split_boot_to_dir=1
elif [ $# -eq 3 ] && [ -s $1 ] && [ -e $2 ]; then
mkboot_from_img_dir=1
else
usage
fi
#mkboot_from_dir, img_info
if [ ! -z $mkboot_from_dir ]; then
pout "mkbootimg from $1/img_info."
unpacked_dir=$(readlink -f $1)
new_img=$(readlink -f $2)
cd $unpacked_dir
if [ ! -s img_info ]; then
pout "not found img_info file! can't rebuild img."
clean
fi
eval $(cat img_info)
if [ -z $kernel ] || [ -z $ramdisk ] || [ -z $base_addr ]; then
pout "img_info file have not enough parameters."
clean
fi
if [ ! -z $dt ]; then
old_bootimg=false
[ -z $kernel_offset ] && kernel_offset=$kernel_addr
[ -z $ramdisk_offset ] && ramdisk_offset=$ramdisk_addr
[ -z $tags_offset ] && tags_offset=$tags_addr
fi
kernel=$(readlink -f $kernel)
if [ -d $ramdisk ]; then
cd $ramdisk
find . | cpio -R 0:0 -H newc -o 2>/dev/null | gzip > $unpacked_dir/new_ramdisk.gz
#find . | cpio -R 0:0 -H newc -o 2>/dev/null > $unpacked_dir/new_ramdisk.gz
#mkbootfs $ramdisk > $unpacked_dir/new_ramdisk.gz
ramdisk=$unpacked_dir/new_ramdisk.gz
ramdisk_size=$(stat -c "%s" $ramdisk)
fi
cd $unpacked_dir
print_info
rm -f $new_img
mkboot_img $new_img || perr "Make boot.img Error! pls check img_info file."
rm -f new_ramdisk.gz
clean
fi
if [ ! -z $split_boot_to_dir ]; then
#split boot.img to dir.
if [ -e $2 ]; then
read -p "$2 exists, delete?(N/y)" reply
case $reply in
y | Y)
rm -rf $2
;;
*)
exit
;;
esac
fi
tempdir="$(readlink -f $2)"
mkdir -p $tempdir
pout "Unpack & decompress $1 to $2"
else
#split boot.img to tmp dir.
tempdir=$(mktemp -d /tmp/mkboot.XXXX)
pout "Repack $1 & ramdisk $2 to $3"
fi
#get boot.img info
cp -f $1 $tempdir/
cd $tempdir
bootimg=$(basename $1)
offset=$(grep -abo ANDROID! $bootimg | cut -f 1 -d :)
[ -z $offset ] && clean
if [ $offset -gt 0 ]; then
dd if=$bootimg of=bootimg bs=$offset skip=1 2>/dev/null
bootimg=bootimg
fi
dtb_size=$(od -A n -D -j 40 -N 4 $bootimg | sed 's/ //g')
kernel_v=0x$(od -A n -H -j 12 -N 4 $bootimg | sed 's/ //g')
ramdisk_v=0x$(od -A n -H -j 20 -N 4 $bootimg | sed 's/ //g')
second_v=0x$(od -A n -H -j 28 -N 4 $bootimg | sed 's/ //g')
tags_v=0x$(od -A n -H -j 32 -N 4 $bootimg | sed 's/ //g')
if [ $dtb_size -gt 0 ]; then
old_bootimg=false
kernel_offset=$kernel_v
ramdisk_offset=$ramdisk_v
second_offset=$second_v
tags_offset=$tags_v
else
kernel_addr=$kernel_v
ramdisk_addr=$ramdisk_v
second_addr=$second_v
tags_addr=$tags_v
fi
kernel_size=$(od -A n -D -j 8 -N 4 $bootimg | sed 's/ //g')
base_addr=0x$(od -A n -h -j 14 -N 2 $bootimg | sed 's/ //g')0000
ramdisk_size=$(od -A n -D -j 16 -N 4 $bootimg | sed 's/ //g')
second_size=$(od -A n -D -j 24 -N 4 $bootimg | sed 's/ //g')
page_size=$(od -A n -D -j 36 -N 4 $bootimg | sed 's/ //g')
cmd_line=$(od -A n --strings -j 64 -N 512 $bootimg)
k_count=$(((kernel_size+page_size-1)/page_size))
r_count=$(((ramdisk_size+page_size-1)/page_size))
s_count=$(((second_size+page_size-1)/page_size))
d_count=$(((dtb_size+page_size-1)/page_size))
k_offset=1
r_offset=$((k_offset+k_count))
s_offset=$((r_offset+r_count))
d_offset=$((s_offset+s_count))
dd if=$bootimg of=zImage_tmp bs=$page_size skip=$k_offset count=$k_count 2>/dev/null
dd if=zImage_tmp of=zImage bs=$kernel_size count=1 2>/dev/null
if [ ! -z $split_boot_to_dir ]; then
dd if=$bootimg of=ramdisk_tmp bs=$page_size skip=$r_offset count=$r_count 2>/dev/null
dd if=ramdisk_tmp of=ramdisk.gz bs=$ramdisk_size count=1 2>/dev/null
fi
if [ $dtb_size -gt 0 ]; then
dd if=$bootimg of=dt.img_tmp bs=$page_size skip=$d_offset count=$d_count 2>/dev/null
dd if=dt.img_tmp of=dt.img bs=$dtb_size count=1 2>/dev/null
dt="$tempdir/dt.img"
fi
rm -f *_tmp $(basename $1) $bootimg
kernel="$tempdir/zImage"
ramdisk=$tempdir/ramdisk.gz
[ ! -s $kernel ] && clean
#print boot.img info
print_info
#write info to img_info,decompression ramdisk.gz
if [ ! -z $split_boot_to_dir ]; then
if $old_bootimg ; then
printf "kernel=$(basename $kernel)\nramdisk=ramdisk\npage_size=$page_size\n\
kernel_size=$kernel_size\nramdisk_size=$ramdisk_size\nbase_addr=$base_addr\nkernel_addr=$kernel_addr\n\
ramdisk_addr=$ramdisk_addr\ntags_addr=$tags_addr\ncmd_line=\"$cmd_line\"\n" > img_info
else
dt=$(basename $dt)
printf "kernel=$(basename $kernel)\nramdisk=ramdisk\ndt=$dt\npage_size=$page_size\n\
kernel_size=$kernel_size\nramdisk_size=$ramdisk_size\ndtb_size=$dtb_size\nbase_addr=$base_addr\nkernel_offset=$kernel_offset\n\
ramdisk_offset=$ramdisk_offset\ntags_offset=$tags_offset\ncmd_line=\"$cmd_line\"\n" > img_info
fi
mkdir ramdisk
cd ramdisk
gzip -t ../ramdisk.gz
if [ $? -gt 0 ]; then
lzma -t ../ramdisk.gz
if [ $? -gt 0 ]; then
#try lz4
$tooldir/lz4 -d ../ramdisk.gz ../ramdisk.cpio
if [ $? -gt 0 ]; then
pout "ramdisk is unknown format,can't unpack ramdisk"
rm ../ramdisk.cpio
else
pout "ramdisk is lz4 format."
cpio -i -d -m --no-absolute-filenames 2>/dev/null < ../ramdisk.cpio
fi
else
pout "ramdisk is lzma format."
lzma -d -c ../ramdisk.gz | cpio -i -d -m --no-absolute-filenames 2>/dev/null
fi
else
pout "ramdisk is gzip format."
gzip -d -c ../ramdisk.gz | cpio -i -d -m --no-absolute-filenames 2>/dev/null
fi
#Unpack Finish to exit.
pout "Unpack completed."
exit
fi
#继续
cd $workdir
ramdisk=$2
#if ramdisk is a directory,create ramdisk.gz
if [ -d $ramdisk ]; then
cd $ramdisk
find . | cpio -R 0:0 -H newc -o 2>/dev/null | gzip > $tempdir/ramdisk.gz
ramdisksize=$(stat -c "%s" $tempdir/ramdisk.gz)
ramdisk="$tempdir/ramdisk.gz"
cd $workdir
fi
rm -f $3
mkboot_img $3 || perr "Make boot.img Error! pls check."
clean