mklonimg/mklonimg.sh
2024-08-14 15:26:55 +05:00

106 lines
2.6 KiB
Bash
Executable file

#!/usr/bin/env bash
# mkbootimg is the property of The Android Open Source Project and licensed under the Apache License, Version 2.0
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
CMDLINE="root=PARTLABEL=linux loglevel=3"
VMLINUZ="/boot/efi/vmlinuz-6.1.10-nabu"
DTB="/boot/efi/dtb-6.1.10-nabu"
function log() {
printf "\e[1m\e[92m==>\e[0m \e[1m%s\e[0m\n" "$*"
}
function log_err() {
printf "\e[1m\e[31m==>\e[0m \e[1m%s\e[0m\n" "$*"
}
function sigterm_handler() {
printf "\e[1m\e[31m>\e[0m \e[1m%s\e[0m\n" "Shutdown signal received."
exit 1
}
trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP
which base64 > /dev/null 2>&1 || {
log_err "base64 not found"
exit 1
}
which curl > /dev/null 2>&1 || {
log_err "curl not found"
exit 1
}
if which python3 > /dev/null 2>&1; then
PYTHON="python3"
elif which python > /dev/null 2>&1 && [[ "$(python -V)" =~ "Python 3" ]]; then
PYTHON="python"
else
log_err "python 3 not found"
exit 1
fi
if which doas > /dev/null 2>&1; then
SUDO=doas
elif which sudo > /dev/null 2>&1; then
SUDO=sudo
elif [ "$(id -u)" -eq "0" ]; then
SUDO=""
else
log_err "sudo/doas not found"
fi
# Begin script
if [ $# -eq 2 ]; then
VMLINUZ="$1"
DTB="$2"
else
log "Using default vmlinuz and dtb path"
log "vmlinuz: $VMLINUZ"
log "dtb: $DTB"
fi
[ -f "$VMLINUZ" ] || {
log_err "vmlinuz not found at $VMLINUZ"
exit 1
}
[ -f "$DTB" ] || {
log_err "dtb not found at $DTB"
exit 1
}
[ -r "$VMLINUZ" ] && [ -r "$DTB" ] && SUDO=""
{ [ -f ./mkbootimg.py ] && [ -f ./generate_gki_certificate.py ]; } || {
log "Downloading mkbootimg from google"
curl -s "https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/main/mkbootimg.py?format=text" \
| base64 -d \
| sed "s/from gki.generate_gki_certificate import generate_gki_certificate/from generate_gki_certificate import generate_gki_certificate/g" \
> mkbootimg.py || ERR="1"
curl -s "https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/main/gki/generate_gki_certificate.py?format=text" \
| base64 -d \
> generate_gki_certificate.py || ERR="1"
[[ "$ERR" -eq "1" ]] && {
log_err "Failed to download mkbootimg"
exit 1
}
}
$SUDO cat "$VMLINUZ" "$DTB" > zImage || {
log_err "Failed to create zImage"
exit 1
}
$PYTHON mkbootimg.py --kernel zImage --cmdline "$CMDLINE" --base 0x00000000 --kernel_offset 0x00008000 --tags_offset 0x00000100 --pagesize 4096 --id -o linux.boot.img > /dev/null || {
log_err "Failed to create boot image"
rm ./zImage
exit 1
}
rm ./zImage
rm -r ./__pycache__
log "Created ./linux.boot.img"