-
Notifications
You must be signed in to change notification settings - Fork 0
/
kramel.sh
518 lines (478 loc) · 13.9 KB
/
kramel.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/bash
# Written by: cyberknight777
# YAKB v1.0
# Copyright (c) 2022-2023 Cyber Knight <[email protected]>
#
# GNU GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
# Some Placeholders: [!] [*] [✓] [✗]
# Default defconfig to use for builds.
export CONFIG=nethunter_defconfig
# Default directory where kernel is located in.
KDIR=$(pwd)
export KDIR
# Device name.
export DEVICE="Samsung Tab A6"
# Device codename.
export CODENAME="gtaxllte"
# Builder name.
export BUILDER="Robin"
# Kernel repository URL.
export REPO_URL="https://github.com/MrRob0-X/Nethunter_kernel_samsung_exynos7870"
# Commit hash of HEAD.
COMMIT_HASH=$(git rev-parse --short HEAD)
export COMMIT_HASH
# Telegram Information. Set 1 to enable. | Set 0 to disable.
export TGI=1
export CHATID=-1001763166286
# Necessary variables to be exported.
export ci
export version
# Number of jobs to run.
PROCS=$(nproc --all)
export PROCS
# Compiler to use for builds.
export COMPILER=clang
# Module building support. Set 1 to enable. | Set 0 to disable.
export MODULE=0
# Requirements
if [ "${ci}" != 1 ]; then
if ! hash dialog make curl wget unzip find 2>/dev/null; then
echo -e "\n\e[1;31m[✗] Install dialog, make, curl, wget, unzip, and find! \e[0m"
exit 1
fi
fi
if [[ "${COMPILER}" = gcc ]]; then
if [ ! -d "${KDIR}/gcc64" ]; then
wget -O 64.tar.xz https://releases.linaro.org/components/toolchain/binaries/4.9-2016.02/aarch64-linux-gnu/gcc-linaro-4.9-2016.02-x86_64_aarch64-linux-gnu.tar.xz && tar -xf 64.tar.xz
mv "${KDIR}"/gcc-linaro-4.9-2016.02-x86_64_aarch64-linux-gnu "${KDIR}"/gcc64 && rm -rf 64.tar.xz
fi
KBUILD_COMPILER_STRING=$("${KDIR}"/gcc64/bin/aarch64-linux-gnu-gcc --version | head -n 1)
export KBUILD_COMPILER_STRING
export PATH="${KDIR}"/gcc64/bin:/usr/bin/:${PATH}
export ANDROID_MAJOR_VERSION=o
MAKE+=(
ARCH=arm64
O=out
LD_LIBRARY_PATH="${KDIR}"/gcc64/bin:/$LD_LIBRARY_PATH
CROSS_COMPILE=aarch64-linux-gnu-
)
elif [[ "${COMPILER}" = clang ]]; then
if [ ! -d "${KDIR}/clang" ]; then
mkdir clang;wget -O clang.tar.gz https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86/+archive/refs/tags/android-12.0.0_r12/clang-r416183b1.tar.gz;tar -xf clang.tar.gz -C clang;rm -rf clang.tar.gz;git clone --depth=1 https://github.com/LineageOS/android_prebuilts_gcc_linux-x86_aarch64_aarch64-linux-android-4.9 arm64;git clone --depth=1 https://github.com/LineageOS/android_prebuilts_gcc_linux-x86_arm_arm-linux-androideabi-4.9 arm
fi
KBUILD_COMPILER_STRING=$("${KDIR}"/clang/bin/clang -v 2>&1 | head -n 1 | sed 's/(https..*//' | sed 's/ version//')
export KBUILD_COMPILER_STRING
export PATH=$KDIR/clang/bin/:$KDIR/arm64/bin:$KDIR/arm/bin:/usr/bin/:${PATH}
MAKE+=(
ARCH=arm64
O=out
CROSS_COMPILE=aarch64-linux-android-
CROSS_COMPILE_ARM32=arm-linux-androideabi-
CLANG_TRIPLE=aarch64-linux-gnu-
CC=${KDIR}/clang/bin/clang
)
fi
if [[ "${MODULE}" = 1 ]]; then
if [ ! -d "${KDIR}"/modules ]; then
git clone --depth=1 https://github.com/MrRob0-X/nethunter-modules "${KDIR}"/modules
fi
fi
if [ ! -d "${KDIR}/anykernel3/" ]; then
git clone --depth=1 https://github.com/MrRob0-X/anykernel3 -b gtaxllte anykernel3
fi
if [ "${ci}" != 1 ]; then
if [ -z "${kver}" ]; then
echo -ne "\e[1mEnter kver: \e[0m"
read -r kver
else
export KBUILD_BUILD_VERSION=${kver}
fi
if [ -z "${zipn}" ]; then
echo -ne "\e[1mEnter zipname: \e[0m"
read -r zipn
fi
else
export KBUILD_BUILD_VERSION=$DRONE_BUILD_NUMBER
export KBUILD_BUILD_HOST=$DRONE_SYSTEM_HOST
export KBUILD_BUILD_USER=$BUILDER
export VERSION=$version
kver=$KBUILD_BUILD_VERSION
zipn=Nethunter-gtaxllte-${VERSION}
if [[ "${MODULE}" = "1" ]]; then
modn="${zipn}-modules"
fi
fi
# A function to exit on SIGINT.
exit_on_signal_SIGINT() {
echo -e "\n\n\e[1;31m[✗] Received INTR call - Exiting...\e[0m"
exit 0
}
trap exit_on_signal_SIGINT SIGINT
# A function to send message(s) via Telegram's BOT api.
tg() {
curl -sX POST https://api.telegram.org/bot"${TOKEN}"/sendMessage \
-d chat_id="${CHATID}" \
-d parse_mode=Markdown \
-d disable_web_page_preview=true \
-d text="$1" &>/dev/null
}
# A function to send file(s) via Telegram's BOT api.
tgs() {
MD5=$(md5sum "$1" | cut -d' ' -f1)
curl -fsSL -X POST -F document=@"$1" https://api.telegram.org/bot"${TOKEN}"/sendDocument \
-F "chat_id=${CHATID}" \
-F "parse_mode=Markdown" \
-F "caption=$2 | *MD5*: \`$MD5\`"
}
# A function to clean kernel source prior building.
clean() {
echo -e "\n\e[1;93m[*] Cleaning source and out/ directory! \e[0m"
make clean && make mrproper && rm -rf "${KDIR}"/out
echo -e "\n\e[1;32m[✓] Source cleaned and out/ removed! \e[0m"
}
# A function to regenerate defconfig.
rgn() {
echo -e "\n\e[1;93m[*] Regenerating defconfig! \e[0m"
make "${MAKE[@]}" $CONFIG
cp -rf "${KDIR}"/out/.config "${KDIR}"/arch/arm64/configs/$CONFIG
echo -e "\n\e[1;32m[✓] Defconfig regenerated! \e[0m"
}
# A function to open a menu based program to update current config.
mcfg() {
rgn
echo -e "\n\e[1;93m[*] Making Menuconfig! \e[0m"
make "${MAKE[@]}" menuconfig
cp -rf "${KDIR}"/out/.config "${KDIR}"/arch/arm64/configs/$CONFIG
echo -e "\n\e[1;32m[✓] Saved Modifications! \e[0m"
}
# A function to build the kernel.
img() {
if [[ "${TGI}" != "0" ]]; then
tg "
*Build Number*: \`${kver}\`
*Builder*: \`${BUILDER}\`
*Core count*: \`$(nproc --all)\`
*Device*: \`${DEVICE} [${CODENAME}]\`
*Kernel Version*: \`$(make kernelversion 2>/dev/null)\`
*Date*: \`$(date)\`
*Zip Name*: \`${zipn}\`
*Compiler*: \`${KBUILD_COMPILER_STRING}\`
*Branch*: \`$(git rev-parse --abbrev-ref HEAD)\`
*Last Commit*: [${COMMIT_HASH}](${REPO_URL}/commit/${COMMIT_HASH})
"
fi
rgn
echo -e "\n\e[1;93m[*] Building Kernel! \e[0m"
BUILD_START=$(date +"%s")
time make -j"$PROCS" "${MAKE[@]}" Image 2>&1 | tee log.txt
BUILD_END=$(date +"%s")
DIFF=$((BUILD_END - BUILD_START))
if [ -f "${KDIR}/out/arch/arm64/boot/Image" ]; then
if [[ "${SILENT}" != "1" ]]; then
tg "*Kernel Built after $((DIFF / 60)) minute(s) and $((DIFF % 60)) second(s)*"
fi
echo -e "\n\e[1;32m[✓] Kernel built after $((DIFF / 60)) minute(s) and $((DIFF % 60)) second(s)! \e[0m"
else
if [[ "${TGI}" != "0" ]]; then
tgs "log.txt" "*Build failed*"
fi
echo -e "\n\e[1;31m[✗] Build Failed! \e[0m"
exit 1
fi
}
# A function to build DTBs.
dtb() {
rgn
echo -e "\n\e[1;93m[*] Building DTBS! \e[0m"
time make -j"$PROCS" "${MAKE[@]}" dtbs dtbo.img
echo -e "\n\e[1;32m[✓] Built DTBS! \e[0m"
}
# A function to build out-of-tree modules.
mod() {
if [[ "${TGI}" != "0" ]]; then
tg "*Building Modules!*"
fi
rgn
echo -e "\n\e[1;93m[*] Building Modules! \e[0m"
mkdir -p "${KDIR}"/out/modules
make "${MAKE[@]}" modules_prepare
make -j"$PROCS" "${MAKE[@]}" modules INSTALL_MOD_PATH="${KDIR}"/out/modules
make "${MAKE[@]}" modules_install INSTALL_MOD_PATH="${KDIR}"/out/modules
find "${KDIR}"/out/modules -type f -iname '*.ko' -exec cp {} "${KDIR}"/modules/system/lib/modules/ \;
cd "${KDIR}"/modules || exit 1
zip -r9 "${modn}".zip . -x ".git*" -x "README.md" -x "LICENSE" -x "*.zip"
cd ../
echo -e "\n\e[1;32m[✓] Built Modules! \e[0m"
}
# A function to build an AnyKernel3 zip.
mkzip() {
if [[ "${TGI}" != "0" ]]; then
tg "*Building zip!*"
fi
echo -e "\n\e[1;93m[*] Building zip! \e[0m"
mv "${KDIR}"/out/arch/arm64/boot/Image "${KDIR}"/anykernel3
cd "${KDIR}"/anykernel3 || exit 1
zip -r9 "$zipn".zip . -x ".git*" -x "README.md" -x "LICENSE" -x "*.zip"
echo -e "\n\e[1;32m[✓] Built zip! \e[0m"
if [[ "${TGI}" != "0" ]]; then
tgs "${zipn}.zip" "*#${kver} ${KBUILD_COMPILER_STRING}*"
fi
if [[ "${MODULE}" = "1" ]]; then
cd ../modules || exit 1
tgs "${modn}.zip" "*#${kver} ${KBUILD_COMPILER_STRING}*"
fi
}
# A function to build specific objects.
obj() {
rgn
echo -e "\n\e[1;93m[*] Building ${1}! \e[0m"
time make -j"$PROCS" "${MAKE[@]}" "$1"
echo -e "\n\e[1;32m[✓] Built ${1}! \e[0m"
}
# A function to uprev localversion in defconfig.
upr() {
echo -e "\n\e[1;93m[*] Bumping localversion to -MrRobin_Ho_Od-${1}! \e[0m"
"${KDIR}"/scripts/config --file "${KDIR}"/arch/arm64/configs/$CONFIG --set-str CONFIG_LOCALVERSION "-MrRobin_Ho_Od-${1}"
rgn
if [ "${ci}" != 1 ]; then
git add arch/arm64/configs/$CONFIG
git commit -S -s -m "nethunter_defconfig: Bump to \`${1}\`"
fi
echo -e "\n\e[1;32m[✓] Bumped localversion to -MrRobin_Ho_Od-${1}! \e[0m"
}
# A function to showcase the options provided for args-based usage.
helpmenu() {
echo -e "\n\e[1m
usage: kver=<version number> zipn=<zip name> $0 <arg>
example: $0 --kver=69 --zipn=Kernel-Beta mcfg
example: $0 --kver=420 --zipn=Kernel-Beta mcfg img
example: $0 --kver=69420 --zipn=Kernel-Beta mcfg img mkzip
example: $0 --kver=1 --zipn=Kernel-Beta --obj=drivers/android/binder.o
example: $0 --kver=2 --zipn=Kernel-Beta --obj=kernel/sched/
example: $0 --kver=3 --zipn=Kernel-Beta--upr=r16
mcfg Runs make menuconfig
img Builds Kernel
dtb Builds dtb(o).img
mod Builds out-of-tree modules
mkzip Builds anykernel3 zip
--obj Builds specific driver/subsystem
rgn Regenerates defconfig
--upr Uprevs kernel version in defconfig
--kver kernel buildversion
--zipn zip name
\e[0m"
}
# A function to setup menu-based usage.
ndialog() {
HEIGHT=16
WIDTH=40
CHOICE_HEIGHT=30
BACKTITLE="Yet Another Kernel Builder"
TITLE="YAKB v1.0"
MENU="Choose one of the following options: "
OPTIONS=(1 "Build kernel"
2 "Build DTBs"
3 "Build modules"
4 "Open menuconfig"
5 "Regenerate defconfig"
6 "Uprev localversion"
7 "Build AnyKernel3 zip"
8 "Build a specific object"
9 "Clean"
10 "Exit"
)
CHOICE=$(dialog --clear \
--backtitle "$BACKTITLE" \
--title "$TITLE" \
--menu "$MENU" \
$HEIGHT $WIDTH $CHOICE_HEIGHT \
"${OPTIONS[@]}" \`
2>&1 >/dev/tty)
clear
case "$CHOICE" in
1)
clear
img
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
2)
clear
dtb
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
3)
clear
mod
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
4)
clear
mcfg
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
5)
clear
rgn
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
6)
dialog --inputbox --stdout "Enter version number: " 15 50 | tee .t
ver=$(cat .t)
clear
upr "$ver"
rm .t
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
7)
mkzip
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
8)
dialog --inputbox --stdout "Enter object path: " 15 50 | tee .f
ob=$(cat .f)
if [ -z "$ob" ]; then
dialog --inputbox --stdout "Enter object path: " 15 50 | tee .f
fi
clear
obj "$ob"
rm .f
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
9)
clear
clean
img
echo -ne "\e[1mPress enter to continue or 0 to exit! \e[0m"
read -r a1
if [ "$a1" == "0" ]; then
exit 0
else
clear
ndialog
fi
;;
10)
echo -e "\n\e[1m Exiting YAKB...\e[0m"
sleep 3
exit 0
;;
esac
}
if [ "${ci}" == 1 ]; then
upr "${version}"
fi
if [[ -z $* ]]; then
ndialog
fi
for arg in "$@"; do
case "${arg}" in
"mcfg")
mcfg
;;
"img")
img
;;
"dtb")
dtb
;;
"mod")
mod
;;
"mkzip")
mkzip
;;
"--obj="*)
object="${arg#*=}"
if [[ -z "$object" ]]; then
echo "Use --obj=filename.o"
exit 1
else
obj "$object"
fi
;;
"rgn")
rgn
;;
"--upr="*)
vers="${arg#*=}"
if [[ -z "$vers" ]]; then
echo "Use --upr=version"
exit 1
else
upr "$vers"
fi
;;
"clean")
clean
;;
"help")
helpmenu
exit 1
;;
*)
helpmenu
exit 1
;;
esac
done