Skip to content

Commit

Permalink
Major Upd: bump version && drop old version
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinaAcct committed Jun 4, 2024
1 parent 11f2d60 commit 14068f6
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 637 deletions.
173 changes: 95 additions & 78 deletions AAP.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,61 @@
#!/bin/sh
#by nya
#2024-02-06
#by Akina | LuoYan
#2024-06-03 Rewrite

RED="\E[1;31m"
YELLOW="\E[1;33m"
BLUE="\E[1;34m"
GREEN="\E[1;32m"
RESET="\E[0m"

alias echo="echo -e"
LUOYANRANDOM="$(date "+%N")"
log_info() {
echo -e "${BLUE}[INFO] $(date "+%H:%M:%S"): $1${RESET}"
}
log_err() {
echo -e "${RED}[ERROR] $(date "+%H:%M:%S"): $1${RESET}"
}
log_warn() {
echo -e "${YELLOW}[WARN] $(date "+%H:%M:%S"): $1${RESET}"
}
if (command -v getprop >/dev/null 2>&1); then
OS="android"
log_info "OS: ${OS}"
else
OS="linux"
log_warn "You are using ${OS}. Using this script on ${OS} is still under testing."
fi

print_help() {
echo "${GREEN}"
echo -e "${GREEN}"
cat <<-EOF
APatch Auto Patch Tool
Written by Akina
Version: 1.0.1
Current DIR: $(pwd)
APatch Auto Patch Tool
Written by Akina
Version: 2.1.0
Current DIR: $(pwd)
-h, -v, print the usage and version.
-h, -v, print the usage and version.
-i [BOOT IMAGE PATH], specify a boot image path.
-n, do not install the patched boot image, save the image in /storage/emulated/0/patched_boot.img, or on Linux ${HOME}/patched_boot.img.
-k [RELEASE NAME], specify a kernelpatch version [RELEASE NAME].
-s "STRING", specify a superkey. Use STRING as superkey.
-S, Install to another slot (for OTA).
-E [ARGS], Add args [ARGS] to kptools when patching.
-V, verbose mode.
-i [BOOT IMAGE PATH], specify a boot image path.
-n, do not install the patched boot image, save the image in /storage/emulated/0/patched_boot.img, or on Linux ${HOME}/patched_boot.img.
-k [RELEASE NAME], specify a kernelpatch version [RELEASE NAME].
-s "STRING", specify a superkey. Use STRING as superkey.
-S, Install to another slot (for OTA).
-E [ARGS], Add args [ARGS] to kptools when patching.
-V, verbose mode.
EOF
echo "${RESET}"
echo -e "${RESET}"
exit 0
}

# 参数解析
while getopts ":hvi:k:nVs:SE:" OPT; do
while getopts ":hvi:k:IVs:SE:" OPT; do
case $OPT in
i) # 处理选项i
BOOTPATH="${OPTARG}"
if [[ -e "${BOOTPATH}" ]]; then
echo "${BLUE}I: Boot image path specified. Current image path: ${BOOTPATH}${RESET}"
log_info "Boot image path specified. Current image path: ${BOOTPATH}"
else
echo "${RED}E: SPECIFIED BOOT IMAGE PATH IS WRONG! NO SUCH FILE!${RESET}"
log_err "SPECIFIED BOOT IMAGE PATH IS WRONG! NO SUCH FILE!"
exit 1
fi
;;
Expand All @@ -49,116 +64,120 @@ while getopts ":hvi:k:nVs:SE:" OPT; do
;;
S)
SAVEROOT="true"
echo "${BLUE}I: The -S parameter was received. The patched image will be flashed into another slot if this is a ab partition device."
log_info "The -S parameter was received. The patched image will be flashed into another slot if this is a ab partition device."
;;
V)
set -x
echo "${YELLOW}W: DEBUG MODE IS ON.${RESET}"
log_warn "DEBUG MODE IS ON."
;;
n)
NOINSTALL="true"
echo "${BLUE}I: The -n parameter was received. Won't install after patch.${RESET}"
I)
if [[ "${OS}" == "android" ]]; then
INSTALL="true"
log_info "The -I parameter was received. Will install after patching."
else
log_err "Do not use this arg without Android!"
exit 1
fi
;;
s)
SUPERKEY="${OPTARG}"
echo "${BLUE}I: The -s parameter was received. Currently specified SuperKey: ${SUPERKEY}.${RESET}"
log_info "The -s parameter was received. Currently specified SuperKey: ${SUPERKEY}."
;;
k)
KPTOOLVER="${OPTARG}"
echo "${BLUE}I: The -k parameter was received. Will use kptool ${KPTOOLVER}.${RESET}"
log_info "The -k parameter was received. Will use kptool ${KPTOOLVER}."
;;
E)
EXTRAARGS="${OPTARG}"
log_info "The -E parameter was received. Current extra args: ${EXTRAARGS}"
;;
E)
EXTRAARGS="${OPTARG}"
echo "${BLUE}I: The -E parameter was received. Current extra args: ${EXTRAARGS}"
;;
:)
echo "${YELLOW}W: Option -$OPTARG requires an argument..${RESET}" >&2 && exit 1
log_err "Option -${OPTARG} requires an argument.." >&2 && exit 1
;;

?)
echo "${RED}E: Invalid option: -$OPTARG${RESET}" >&2 && exit 1
log_err "Invalid option: -${OPTARG}" >&2 && exit 1
;;
esac
done

# 设置工作文件夹
WORKDIR="$(pwd)/nyatmp_${RANDOM}"
# ROOT 检测
if [[ "$(id -u)" != "0" ]]; then
echo "${RED}E: RUN THIS SCRIPT WITH ROOT PERMISSION!${RESET}"
exit 2
if [[ $(id -u) -ne 0 ]]; then
log_err "Run this script with root!"
exit 127
fi
# OS 检测
if (command -v getprop >/dev/null 2>&1); then
OS="android"
echo "${BLUE}I: OS: ${OS}${RESET}"
# 镜像路径检测(For Linux)
if [[ $"${OS}" == "linux" && -z "${BOOTPATH}" ]]; then
log_err "You are using ${OS}, but there is no image specified by you. Exited."
exit 1
fi
# 设置工作文件夹
if [[ "${OS}" == "android" ]]; then
WORKDIR="/data/local/tmp/LuoYanTmp_${LUOYANRANDOM}"
else
OS="linux"
echo "${YELLOW}W: You are using ${OS}.Using this script on ${OS} is still under testing.${RESET}"
if [[ -z "${BOOTPATH}" ]]; then
echo "${RED}E: You are using ${OS}, but there is no image specified by you. Exited.${RESET}"
exit 1
fi
WORKDIR="/tmp/LuoYanTmp_${LUOYANRANDOM}"
fi
# 判断用户设备是否为ab分区,是则设置$BOOTSUFFIX
BYNAMEPATH="$(getprop ro.frp.pst | sed 's/\/frp//g')"
if [[ ! -e ${BYNAMEPATH}/boot && "${OS}" == "android" ]]; then
BOOTSUFFIX=$(getprop ro.boot.slot_suffix)
if [[ "${OS}" == "android" ]]; then
if [[ ! -e ${BYNAMEPATH}/boot ]]; then
BOOTSUFFIX=$(getprop ro.boot.slot_suffix)
fi
else
echo "${BLUE}I: Current OS is: ${OS}. Skip boot slot check.${RESET}"
log_warn "Current OS is: ${OS}. Skip boot slot check."
fi
if [[ -n "${SAVEROOT}" && -n "${BOOTSUFFIX}" && "${OS}" == "android" ]]; then
if [[ "${BOOTSUFFIX}" == "_a" ]]; then
TBOOTSUFFIX="_b"
else
TBOOTSUFFIX="_a"
fi
echo "${BLUE}I: You have specified the installation to another slot. Current slot:${BOOTSUFFIX}. Slot to be flashed into:${TBOOTSUFFIX}."
log_warn "You have specified the installation to another slot. Current slot:${BOOTSUFFIX}. Slot to be flashed into:${TBOOTSUFFIX}."
fi
if [[ -z "${SUPERKEY}" ]]; then
SUPERKEY=${RANDOM}
SUPERKEY=${LUOYANRANDOM}
fi
# 检测可能存在的APatch app, 并输出相关信息
if [[ "${OS}" == "android" ]]; then
if (pm path me.bmax.apatch >/dev/null 2>&1); then
echo "${BLUE}I: Detected that APatch is installed.${RESET}"
log_info "Detected that APatch is installed."
APKPATH="$(command echo "$(pm path me.bmax.apatch)" | sed 's/base.apk//g' | sed 's/package://g')"
APKLIBPATH="${APKPATH}lib/arm64"
APDVER="$(${APKLIBPATH}/libapd.so -V)"
LKPVER="$(${APKLIBPATH}/libkpatch.so -v)"
cat <<-EOF
Installed manager(apd) version: $(echo "${BLUE}${APDVER}${RESET}")
APatch app built-in KernelPatch version: $(echo "${BLUE}${LKPVER}${RESET}")
Installed manager(apd) version: $(echo -e "${BLUE}${APDVER}${RESET}")
APatch app built-in KernelPatch version: $(echo -e "${BLUE}${LKPVER}${RESET}")
EOF
fi
fi

# 清理可能存在的上次运行文件
rm -rf ./nyatmp_*
rm -rf /tmp/LuoYanTmp_*
rm -rf ./LuoYanTmp_*
mkdir -p "${WORKDIR}"

mkdir -p ${WORKDIR}

echo "${BLUE}I: Downloading function file from GitHub...${RESET}"
curl -L --progress-bar "https://raw.githubusercontent.com/nya-main/APatchAutoPatchTool/main/AAPFunction" -o ${WORKDIR}/AAPFunction
log_info "Downloading function file from GitHub..."
curl -L --progress-bar "https://raw.githubusercontent.com/nya-main/APatchAutoPatchTool/main/new/AAPFunction" -o ${WORKDIR}/AAPFunction
EXITSTATUS=$?
if [[ $EXITSTATUS != 0 ]]; then
echo "${RED}E: SOMETHING WENT WRONG! CHECK YOUR INTERNET CONNECTION!${RESET}"
log_err "SOMETHING WENT WRONG! CHECK YOUR INTERNET CONNECTION!"
exit 1
fi

# 备份boot
if [[ "${OS}" == "android" ]]; then
echo "${BLUE}I: Backing up boot image...${RESET}"
log_info "Backing up boot image..."
dd if=${BYNAMEPATH}/boot${BOOTSUFFIX} of=/storage/emulated/0/stock_boot${BOOTSUFFIX}.img
EXITSTATUS=$?
if [[ "${EXITSTATUS}" != "0" ]]; then
echo "${RED}E: BOOT IMAGE BACKUP FAILED!${RESET}"
echo "${YELLOW}W: Now skiping backingup boot image...${RESET}"
log_err "BOOT IMAGE BACKUP FAILED!"
log_warn "Now skiping backingup boot image..."
else
echo "${GREEN}I: Done. Boot image path: /storage/emulated/0/stock_boot${BOOTSUFFIX}.img${RESET}"
log_info "Done. Boot image path: /storage/emulated/0/stock_boot${BOOTSUFFIX}.img"
fi
else
echo "${BLUE}I: Current OS: ${OS}. Skiping backup...${RESET}"
log_info "Current OS: ${OS}. Skiping backup..."
fi

# 加载操作文件
Expand All @@ -167,21 +186,19 @@ source ${WORKDIR}/AAPFunction
get_device_boot
get_tools
patch_boot
if [[ -n ${NOINSTALL} ]]; then
echo "${YELLOW}W: The -n parameter was received. Won't install patched image.${RESET}"
if [[ -n ${INSTALL} ]]; then
log_warn "The -I parameter was received. Will install patched image."
flash_boot
else
if [[ "${OS}" == "android" ]]; then
echo "${BLUE}I: Now copying patched image to /storage/emulated/0/patched_boot.img...${RESET}"
log_info "Now copying patched image to /storage/emulated/0/patched_boot.img..."
mv ${WORKDIR}/new-boot.img /storage/emulated/0/patched_boot.img
else
echo "${BLUE}I: Now copying patched image to ${HOME}/patched_boot.img...${RESET}"
log_info "Now copying patched image to ${HOME}/patched_boot.img..."
mv ${WORKDIR}/new-boot.img ${HOME}/patched_boot.img
fi
echo "${BLUE}I: Done. Now deleting tmp files...${RESET}"
log_info "Done. Now deleting tmp files..."
rm -rf ${WORKDIR}
echo "${GREEN}I: Done.${RESET}"
elif [[ "${OS}" != "android" ]]; then
echo "${BLUE}I: Current OS is: ${OS}. Won't install patched image.${RESET}"
else
flash_boot
log_info "Done."
fi
print_superkey
Loading

0 comments on commit 14068f6

Please sign in to comment.