-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_boot.sh
executable file
·290 lines (253 loc) · 8.76 KB
/
build_boot.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
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
#!/bin/bash
__usage="
Usage: build_boot [OPTIONS]
Build openEuler SBCs boot image.
The target boot.img will be generated in the build folder of the directory where the build_boot.sh script is located.
Options:
--board BOARD_CONFIG Required! The config of target board in the boards folder, which defaults to firefly-rk3399.
-b, --branch KERNEL_BRANCH The branch name of kernel source's repository, which defaults to openEuler-20.03-LTS.
-k, --kernel KERNEL_URL Required! The URL of kernel source's repository.
-c, --config KERNEL_DEFCONFIG The name/path of defconfig file when compiling kernel, which defaults to openeuler_rockchip_defconfig.
--cores N The number of cpu cores to be used during making.
-h, --help Show command help.
"
help()
{
echo "$__usage"
exit $1
}
default_param() {
workdir=$(pwd)/build
branch=openEuler-20.03-LTS
default_defconfig=openeuler_rockchip_defconfig
board=firefly-rk3399
dtb_name=rk3399-firefly
platform=rockchip
kernel_url="https://gitee.com/openeuler/rockchip-kernel.git"
boot_dir=$workdir/boot
log_dir=$workdir/log
make_cores=$(nproc)
}
local_param(){
if [ -f $workdir/.param ]; then
branch=$(cat $workdir/.param | grep branch)
branch=${branch:7}
default_defconfig=$(cat $workdir/.param | grep default_defconfig)
default_defconfig=${default_defconfig:18}
board=$(cat $workdir/.param | grep board)
board=${board:6}
kernel_url=$(cat $workdir/.param | grep kernel_url)
kernel_url=${kernel_url:11}
fi
}
parseargs()
{
if [ "x$#" == "x0" ]; then
return 0
fi
while [ "x$#" != "x0" ];
do
if [ "x$1" == "x-h" -o "x$1" == "x--help" ]; then
return 1
elif [ "x$1" == "x" ]; then
shift
elif [ "x$1" == "x--board" ]; then
board=`echo $2`
shift
shift
elif [ "x$1" == "x-b" -o "x$1" == "x--branch" ]; then
branch=`echo $2`
shift
shift
elif [ "x$1" == "x-c" -o "x$1" == "x--config" ]; then
default_defconfig=`echo $2`
shift
shift
elif [ "x$1" == "x-k" -o "x$1" == "x--kernel" ]; then
kernel_url=`echo $2`
shift
shift
elif [ "x$1" == "x--cores" ]; then
make_cores=`echo $2`
shift
shift
else
echo `date` - ERROR, UNKNOWN params "$@"
return 2
fi
done
}
buildid=$(date +%Y%m%d%H%M%S)
builddate=${buildid:0:8}
ERROR(){
echo `date` - ERROR, $* | tee -a ${log_dir}/${builddate}.log
}
LOG(){
echo `date` - INFO, $* | tee -a ${log_dir}/${builddate}.log
}
LOSETUP_D_IMG(){
set +e
if [ -d $workdir/boot_emmc ]; then
if grep -q "$workdir/boot_emmc " /proc/mounts ; then
umount $workdir/boot_emmc
fi
fi
if [ -d $workdir/boot_emmc ]; then
rm -rf $workdir/boot_emmc
fi
set -e
}
clone_and_check_kernel_source() {
cd $workdir
if [ -d kernel ]; then
if [ -f $workdir/.param_last ]; then
last_branch=$(cat $workdir/.param_last | grep branch)
last_branch=${last_branch:7}
last_default_defconfig=$(cat $workdir/.param_last | grep default_defconfig)
last_default_defconfig=${last_default_defconfig:18}
last_dtb_name=$(cat $workdir/.param_last | grep dtb_name)
last_dtb_name=${last_dtb_name:9}
last_platform_name=$(cat $workdir/.param_last | grep platform)
last_platform_name=${last_dtb_name:9}
last_kernel_url=$(cat $workdir/.param_last | grep kernel_url)
last_kernel_url=${last_kernel_url:11}
cd $workdir/kernel
git remote -v update
lastest_kernel_version=$(git rev-parse @{u})
local_kernel_version=$(git rev-parse @)
cd $workdir
if [[ ${last_branch} != ${branch} || \
${last_default_defconfig} != ${default_defconfig} || \
${last_dtb_name} != ${dtb_name} || \
${last_kernel_url} != ${kernel_url} || \
${lastest_kernel_version} != ${local_kernel_version} ]]; then
if [ -d $workdir/kernel ];then rm -rf $workdir/kernel; fi
if [ -d $workdir/boot ];then rm -rf $workdir/boot; fi
if [ -f $workdir/boot.img ];then rm $workdir/boot.img; fi
git clone --depth=1 -b $branch $kernel_url kernel
LOG "clone kernel source done."
fi
fi
else
git clone --depth=1 -b $branch $kernel_url kernel
LOG "clone kernel source done."
fi
}
make_kernel(){
LOG "make kernel(${default_defconfig}) begin..."
kernel_dir_tmp=$1
cd "${kernel_dir_tmp}"
if [ "x${kernel_defconfig:0:1}" != "x/" ]; then
if [ ! -f arch/arm64/configs/${kernel_defconfig} ]; then
ERROR "config file ${kernel_defconfig} can not be found in kernel source".
exit 2
fi
kernel_defconfig=arch/arm64/configs/${kernel_defconfig}
fi
make distclean
cp ${kernel_defconfig} .config
make ARCH=arm64 olddefconfig
kernel_defconfig=${kernel_defconfig##*/}
make ARCH=arm64 -j${make_cores}
LOG "make kernel(${default_defconfig}) end."
}
install_kernel() {
if [ ! -f $workdir/kernel/arch/arm64/boot/Image ]; then
ERROR "kernel Image can not be found!"
exit 2
else
LOG "make kernel done."
fi
if [ -d $workdir/kernel/kernel-modules ];then rm -rf $workdir/kernel/kernel-modules; fi
if [ -d ${boot_dir} ];then rm -rf ${boot_dir}; fi
mkdir -p ${boot_dir}
mkdir -p $workdir/kernel/kernel-modules
cd $workdir/kernel
make ARCH=arm64 install INSTALL_PATH=${boot_dir}
make ARCH=arm64 modules_install INSTALL_MOD_PATH=$workdir/kernel/kernel-modules
LOG "device tree name is ${dtb_name}.dtb"
cp arch/arm64/boot/dts/${platform}/${dtb_name}.dtb ${boot_dir}
cp arch/arm64/boot/Image ${boot_dir}
LOG "prepare kernel done."
}
mk_boot() {
LOG "start make bootimg..."
mkdir -p ${boot_dir}/extlinux
LOG "start gen initrd..."
dracut --no-kernel ${boot_dir}/initrd.img
LOG "gen initrd done."
dtb_name=$(ls $boot_dir | grep dtb)
LOG "gen extlinux config for $dtb_name"
if [ "${platform}" == "rockchip" ];then
bootargs=${rockchip_bootargs}
elif [ "${platform}" == "phytium" ];then
bootargs=${phytium_bootargs}
else
echo "Unsupported platform"
exit 2
fi
echo "label openEuler
kernel /Image
initrd /initrd.img
fdt /${dtb_name}
append ${bootargs}" \
> ${boot_dir}/extlinux/extlinux.conf
LOG "gen extlinux config done."
dd if=/dev/zero of=$workdir/boot.img bs=1M count=240 status=progress
mkfs.vfat -n boot $workdir/boot.img
if [ -d $workdir/boot_emmc ];then rm -rf $workdir/boot_emmc; fi
mkdir $workdir/boot_emmc
mount $workdir/boot.img $workdir/boot_emmc/
cp -r ${boot_dir}/* $workdir/boot_emmc/
umount $workdir/boot.img
rmdir $workdir/boot_emmc
if [ -f $workdir/boot.img ]; then
LOG "make boot image done."
else
ERROR "make boot image failed!"
exit 2
fi
LOG "clean boot directory."
rm -rf ${boot_dir}
}
kernel_defconfig=""
default_param
local_param
parseargs "$@" || help $?
set -e
rockchip_bootargs="earlyprintk console=ttyS2,1500000 rw root=UUID=614e0000-0000-4b53-8000-1d28000054a9 rootfstype=ext4 init=/sbin/init rootwait"
phytium_bootargs="console=ttyAMA1,115200 earlycon=pl011,0x2800d000 rw root=UUID=614e0000-0000-4b53-8000-1d28000054a9 rootfstype=ext4 rootwait cma=256m"
if [ ! -f $default_defconfig ] ; then
LOG "kernel defconfig is : ${default_defconfig}"
kernel_defconfig=$default_defconfig
elif [ -f $default_defconfig ]; then
LOG "use local kernel defconfig..."
cp $default_defconfig ${workdir}/
kernel_defconfig=${workdir}/${default_defconfig##*/}
fi
source $workdir/../boards/${board}.conf
if [ ! -d $workdir ]; then
mkdir $workdir
fi
if [ ! -d ${log_dir} ];then mkdir -p ${log_dir}; fi
if [ ! -f $workdir/.done ];then
touch $workdir/.done
fi
sed -i 's/bootimg//g' $workdir/.done
LOG "build boot..."
clone_and_check_kernel_source
if [[ -f $workdir/kernel/arch/arm64/boot/dts/${platform}/${dtb_name}.dtb && -f $workdir/kernel/arch/arm64/boot/Image ]];then
LOG "kernel is the latest"
else
make_kernel $workdir/kernel
fi
if [[ -f $workdir/boot.img && $(cat $workdir/.done | grep bootimg) == "bootimg" ]];then
LOG "boot is the latest"
else
trap 'LOSETUP_D_IMG' EXIT
LOSETUP_D_IMG
install_kernel
mk_boot
fi
LOG "The boot.img is generated in the ${workdir}."
echo "bootimg" >> $workdir/.done