-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmk_kernel.sh
executable file
·300 lines (267 loc) · 7.24 KB
/
mk_kernel.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
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
WORKSPACE_PATH=${PWD}
KERNEL_SRC="${WORKSPACE_PATH}/linux"
SCRIPT_NAME=${0##*/}
# SCRIPT_PATH=`S=\`readlink "$0"\`; [ -z "$S" ] && S=$0; dirname $S`
SCRIPT_PATH=$(dirname $(readlink -f $0))
source ${SCRIPT_PATH}/common.sh
init() {
# Check kernel source
if [ ! -e "${KERNEL_SRC}" ]; then
echo "Please link the kernel source directory to 'linux'."
exit 1
fi
# Source env
ENV_FILE="${SCRIPT_PATH}/env/${SCRIPT_NAME}"
source ${ENV_FILE}
if [ "${NO_CROSS_COMPILE}" == "1" ]; then
# No use cross compile toolchain on same platform
if ([ "${HOST_ARCH}" == "aarch64" ] && [ "${ARCH}" == "arm64" ]) || ([ "${HOST_ARCH:0:3}" == "arm" ] && [ "${ARCH}" == "arm" ]); then
unset CROSS_COMPILE
fi
fi
# Common Variable
export PATH=${TOOLCHAIN_PATH}/${TOOLCHAIN_NAME}/bin:${PATH}
export INSTALL_MOD_PATH="${WORKSPACE_PATH}/install"
export INSTALL_HDR_PATH=${INSTALL_MOD_PATH}
export INSTALL_MOD_STRIP=1
KERNEL_VERSION=$(make -s -C ${KERNEL_SRC} kernelversion)
export KERNELRELEASE=${KERNEL_VERSION}-${KERNEL_BRANCH}-${ARCH}
BUILD_PATH=${WORKSPACE_PATH}/.build
BUILD_ARGS="-j$(nproc) O=${BUILD_PATH} KERNELRELEASE=${KERNELRELEASE}"
if [ "${ARCH_DEFCONFIG}" != "" ]; then
DEFCONFIG=${ARCH_DEFCONFIG}
fi
if [ "${LINK_DEFCONFIG}" != "" ]; then
DEFCONFIG=${LINK_DEFCONFIG}
fi
KERNEL_CURRENT=$(git -C ${KERNEL_SRC} config remote.origin.url)
if [ $? -ne 0 ]; then
KERNEL_CURRENT="Archive File"
fi
}
build_info() {
echo "================ Build Info ================"
echo -e "BOARD_NAME: ${BOARD_NAME}"
echo -e "CPU_INFO: ${CPU_INFO}"
echo -e "DT_FILE: ${DT_FILE}.dts"
echo -e "ARCH: ${ARCH}"
echo -e "KERNEL_VERSION: ${KERNEL_VERSION}"
echo -e "KERNEL_CURRENT: ${KERNEL_CURRENT}"
echo -e "KERNEL_RECOMMEND: ${KERNEL_RECOMMEND}"
echo -e "DEFCONFIG: ${DEFCONFIG}"
echo -e "BUILD_ARGS: ${BUILD_ARGS}"
echo -e "CROSS_COMPILE: ${CROSS_COMPILE}"
echo -e "INSTALL_MOD_PATH: ${INSTALL_MOD_PATH}"
echo -e "INSTALL_HDR_PATH: ${INSTALL_HDR_PATH}"
}
build_kernel() {
cd ${KERNEL_SRC}
case $1 in
"kernel")
TIME="Total Time: %E\tExit:%x" time make ${KERNEL_TARGET} ${BUILD_ARGS}
;;
"modules")
TIME="Total Time: %E\tExit:%x" time make modules ${BUILD_ARGS}
;;
"dtbs")
if [ "${DT_FILE}" != "" ]; then
TIME="Total Time: %E\tExit:%x" time make dtbs ${BUILD_ARGS}
fi
;;
*)
echo "Invalid Parmameter: [$1]"
;;
esac
return $?
}
install_kernel() {
cd "${KERNEL_SRC}"
rm -rf ${INSTALL_MOD_PATH}
# Install Modules
TIME="Total Time: %E\tExit:%x" time make modules_install ${BUILD_ARGS}
if [ "$ARCH" == "arm64" ]; then
local KERNEL_IMG="${BUILD_PATH}/arch/${ARCH}/boot/Image"
if [ "${KERNEL_FMT}" == "gzip" ]; then
gzip -9cnk ${KERNEL_IMG} > "${INSTALL_MOD_PATH}/Image.gz"
else
# Generate uImage
mkimage -A "${ARCH}" -O linux -T kernel -C none -a 0x1080000 -e 0x1080000 -n linux-next -d ${KERNEL_IMG} "${INSTALL_MOD_PATH}/uImage"
fi
elif [ "$ARCH" == "arm" ]; then
# Copy zImage
cp -f "${BUILD_PATH}/arch/${ARCH}/boot/${KERNEL_TARGET}" "${INSTALL_MOD_PATH}"
fi
# Copy dts/dtb
if [ "${DT_FILE}" != "" ]; then
if [ "$ARCH" == "arm64" ]; then
local _DT_PATH="arch/${ARCH}/boot/dts/${VENDOR}"
elif [ "$ARCH" == "arm" ]; then
local _DT_PATH="arch/${ARCH}/boot/dts/"
fi
cp -f "${KERNEL_SRC}/${_DT_PATH}/${DT_FILE}.dts" "${INSTALL_MOD_PATH}"
cp -f "${BUILD_PATH}/${_DT_PATH}/${DT_FILE}.dtb" "${INSTALL_MOD_PATH}"
if [ "${DT_INC_FILE}" != "" ]; then
cp -f "${KERNEL_SRC}/${_DT_PATH}/${DT_INC_FILE}.dtsi" "${INSTALL_MOD_PATH}"
fi
fi
cd "${WORKSPACE_PATH}"
# Copy .config
cp -f "${BUILD_PATH}/.config" "${INSTALL_MOD_PATH}/config"
}
install_headers() {
cd "${KERNEL_SRC}"
# Install Headers
TIME="Total Time: %E\tExit:%x" time make headers_install ${BUILD_ARGS} INSTALL_HDR_PATH=${INSTALL_HDR_PATH}
}
archive_kernel() {
local pack_info
# Input log
read -p "Input Package Log:" pack_info
echo "${pack_info}" >"${INSTALL_MOD_PATH}/info"
# Package
#cd $KDIR
local PACK_NAME="linux_${PACK_NAME}_${KERNEL_VERSION}_$(date +%Y%m%d_%H%M).tar.xz"
cd "${INSTALL_MOD_PATH}"
TIME="Total Time: %E\tExit:%x" time tar cJfp "../${PACK_NAME}" *
echo "Package To ${PACK_NAME}"
}
create_deb() {
local deb_name=${DT_FILE}-kernel
local deb_version=${KERNEL_VERSION}-$(date +%Y%m%d%H%M)
cd "${WORKSPACE_PATH}"
rm -rf deb/${deb_name}
mkdir -p deb/${deb_name}
cd deb/${deb_name}
mkdir -p DEBIAN
mkdir -p boot
cp -dpr ${INSTALL_MOD_PATH}/lib ./
cp ${INSTALL_MOD_PATH}/${DT_FILE}.dtb ./boot/dtb.img
cp ${INSTALL_MOD_PATH}/uImage ./boot
cat <<EOF >DEBIAN/control
Package: ${deb_name}
Version: ${deb_version}
Architecture: ${ARCH}
Maintainer: test
Installed-Size: $(du -ks | cut -f 1)
Section: test
Priority: optional
Description: kernel for ${BOARD_NAME}
EOF
# cat <<EOF >DEBIAN/postinst
# EOF
# chmod 775 DEBIAN/postinst
cd ..
dpkg -b ${deb_name} ${deb_name}_${KERNEL_VERSION}_${ARCH}.deb
}
show_menu() {
cd "${KERNEL_SRC}"
echo "================ Menu Option ================"
echo -e "\t[1]. Use Default Config"
echo -e "\t[2]. Menu Config"
echo -e "\t[3]. Build All"
echo -e "\t[31] ├─Build Kernel"
echo -e "\t[32] ├─Build Modules"
echo -e "\t[33] └─Build DTB"
echo -e "\t[4]. Install All"
echo -e "\t[41] ├─Install Kernel And Modules"
echo -e "\t[42] └─Install Headers"
echo -e "\t[5]. Archive Kernel"
echo -e "\t[6]. Clean"
read -p "Please Select: >> " OPT
case ${OPT} in
"1")
cd ${KERNEL_SRC}
make ${DEFCONFIG} ${BUILD_ARGS}
;;
"2")
cd ${KERNEL_SRC}
make menuconfig ${BUILD_ARGS}
;;
"3")
build_kernel kernel
if [ $? != 0 ]; then
exit -1
fi
build_kernel modules
if [ $? != 0 ]; then
exit -1
fi
build_kernel dtbs
;;
"31")
build_kernel kernel
;;
"32")
build_kernel modules
;;
"33")
build_kernel dtbs
;;
"4")
install_kernel
install_headers
;;
"41")
install_kernel
;;
"42")
install_headers
;;
"5")
archive_kernel
;;
"6")
cd ${KERNEL_SRC}
make clean ${BUILD_ARGS}
rm ${INSTALL_MOD_PATH}/* -rf
;;
"mrproper")
cd ${KERNEL_SRC}
# Hide Option
make mrproper
;;
"0")
# Hide Option
echo "apt install flex bison time bc kmod u-boot-tools libncurses5-dev libgmp-dev libmpc-dev libssl-dev"
;;
*)
echo "Not Support Option: [${OPT}]"
;;
esac
}
build_probe() {
# link dts
if [ "${DT_FILE}" != "" ] && [ "${DT_LINK}" == "1" ]; then
DT_PATH="${SCRIPT_PATH}/boot/dts/${VENDOR}/mainline/"
if [ "$ARCH" == "arm64" ]; then
DT_PATH_LINK="${KERNEL_SRC}/arch/${ARCH}/boot/dts/${VENDOR}/"
elif [ "$ARCH" == "arm" ]; then
DT_PATH_LINK="${KERNEL_SRC}/arch/${ARCH}/boot/dts/"
fi
check_path DTS "${DT_PATH}/${DT_FILE}.dts"
ln -s -f "${DT_PATH}/${DT_FILE}.dts" ${DT_PATH_LINK}
if [ "${DT_INC_FILE}" != "" ]; then
check_path DTSI "${DT_PATH}/${DT_INC_FILE}.dtsi"
ln -s -f "${DT_PATH}/${DT_INC_FILE}.dtsi" ${DT_PATH_LINK}
fi
# add dtb to Makefile
grep -q ${DT_FILE} ${DT_PATH_LINK}/Makefile
if [ $? -ne 0 ]; then
echo "dtb-y += ${DT_FILE}.dtb" >>${DT_PATH_LINK}/Makefile
fi
fi
# link defconfig
if [ "${LINK_DEFCONFIG}" != "" ]; then
DEFCONFIG_PATH="${SCRIPT_PATH}/boot/configs/${LINK_DEFCONFIG}"
check_path "LINK_DEFCONFIG" "${DEFCONFIG_PATH}"
ln -s -f ${DEFCONFIG_PATH} "${KERNEL_SRC}/arch/${ARCH}/configs/"
fi
}
main() {
init
build_info
build_probe
show_menu
}
main