Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
AkinaAcct committed Jun 12, 2024
1 parent 3d8271c commit 40977f1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 64 deletions.
76 changes: 45 additions & 31 deletions AAP.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ RANDOMNUM=$(date "+%N") # RANDOM NUMBER

# 格式化打印消息
msg_info() { # 打印消息 格式: "[INFO] TIME: MSG"(BLUE)
printf "${BLUE}[INFO]$(date "+%H:%M:%S"): $1${RESET}\n"
printf "${BLUE}[INFO] $(date "+%H:%M:%S"): ${1}${RESET}\n"
}
msg_warn() { # 打印消息 格式: "[WARN] TIME: MSG"(YELLOW)
printf "${YELLOW}[WARN]$(date "+%H:%M:%S"): $1${RESET}\n"
printf "${YELLOW}[WARN] $(date "+%H:%M:%S"): ${1}${RESET}\n"
}
msg_err() { # 打印消息 格式: "[ERROR] TIME: MSG"(RED)
printf "${RED}[ERROR]$(date "+%H:%M:%S"): $1${RESET}\n"
printf "${RED}[ERROR] $(date "+%H:%M:%S"): ${1}${RESET}\n"
}
msg_fatal() { # 打印消息并停止运行 格式: "[FATAL] TIME: MSG"(RED)
printf "${RED}[FATAL] $(date "+%H:%M:%S"): ${1}${RESET}\n"
}
# ROOT 检测
if [ "$(id -u)" -ne 0 ]; then
ROOT=true
else
ROOT=false
fi
# OS 检测
if command -v getprop >/dev/null 2>&1; then
OS="android"
msg_info "OS: ${OS}"
Expand All @@ -28,7 +38,7 @@ else
msg_warn "You are using ${OS}. Using this script on ${OS} is still under testing."
fi
if [ -z "$(echo ${PREFIX} | grep -i termux)" -a "${OS}" = "android" ]; then
msg_err "Unsupport terminal app(not in Termux)."
msg_warn "Unsupported terminal app(not in Termux)."
fi
print_help() {
printf "${BLUE}%s${RESET}" "
Expand Down Expand Up @@ -57,7 +67,7 @@ while getopts ":hvi:k:IVs:SE:" OPT; do
if [ -e "${BOOTPATH}" ]; then
msg_info "Boot image path specified. Current image path: ${BOOTPATH}"
else
msg_err "SPECIFIED BOOT IMAGE PATH IS WRONG! NO SUCH FILE!"
msg_fatal "SPECIFIED BOOT IMAGE PATH IS WRONG! NO SUCH FILE!"
exit 1
fi
;;
Expand All @@ -77,7 +87,7 @@ while getopts ":hvi:k:IVs:SE:" OPT; do
INSTALL="true"
msg_info "The -I parameter was received. Will install after patching."
else
msg_err "Do not use this arg without Android!"
msg_fatal "Do not use this arg without Android!"
exit 1
fi
;;
Expand All @@ -94,39 +104,40 @@ while getopts ":hvi:k:IVs:SE:" OPT; do
msg_info "The -E parameter was received. Current extra args: ${EXTRAARGS}"
;;
:)
msg_err "Option -${OPTARG} requires an argument.." >&2 && exit 1
msg_fatal "Option -${OPTARG} requires an argument.." >&2
exit 1
;;

?)
msg_err "Invalid option: -${OPTARG}" >&2 && exit 1
msg_fatal "Invalid option: -${OPTARG}" >&2
exit 1
;;
esac
done

# ROOT 检测
if [ "$(id -u)" -ne 0 ]; then
msg_err "Run this script with root!"
exit 127
fi
# 镜像路径检测(For Linux)
if [ "${OS}" = "linux" -a -z "${BOOTPATH}" ]; then
msg_err "You are using ${OS}, but there is no image specified by you. Exited."
msg_fatal "You are using ${OS}, but there is no image specified by you. Exited."
exit 1
fi
# 无 ROOT 并且未指定 BOOT 镜像路径则退出
if [ -z "${BOOTPATH}" -a ${ROOT} ]; then
msg_fatal "No root and no boot image is specified. Exited."
exit 1
fi
# 设置工作文件夹
if [ "${OS}" = "android" ]; then
WORKDIR="/data/local/tmp/LuoYanTmp_${RANDOMNUM}"
WORKDIR="./LuoYanTmp_${RANDOMNUM}"
else
WORKDIR="/tmp/LuoYanTmp_${RANDOMNUM}"
fi
# 判断用户设备是否为ab分区,是则设置$BOOTSUFFIX
BYNAMEPATH=$(getprop ro.frp.pst | sed 's/\/frp//g')
if [ "${OS}" = "android" ]; then
if [ ! -e ${BYNAMEPATH}/boot ]; then
if [ ! -e "${BYNAMEPATH}/boot" ]; then
BOOTSUFFIX=$(getprop ro.boot.slot_suffix)
fi
else
msg_warn "Current OS is: ${OS}. Skip boot slot check."
msg_warn "Current OS is not Android. Skip boot slot check."
fi
if [ -n "${SAVEROOT}" -a -n "${BOOTSUFFIX}" -a "${OS}" = "android" ]; then
if [ "${BOOTSUFFIX}" = "_a" ]; then
Expand All @@ -139,37 +150,40 @@ fi
if [ -z "${SUPERKEY}" ]; then
SUPERKEY=${RANDOMNUM}
fi

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

msg_info "Downloading function file from GitHub..."
curl -L --progress-bar "https://raw.githubusercontent.com/nya-main/APatchAutoPatchTool/main/AAPFunction" -o ${WORKDIR}/AAPFunction
EXITSTATUS=$?
if [ $EXITSTATUS != 0 ]; then
msg_err "SOMETHING WENT WRONG! CHECK YOUR INTERNET CONNECTION!"
msg_fatal "Download failed. Check your Internet connection and try again."
exit 1
fi

# 备份boot
if [ "${OS}" = "android" ]; then
msg_info "Backing up boot image..."
dd if=${BYNAMEPATH}/boot${BOOTSUFFIX} of=/storage/emulated/0/stock_boot${BOOTSUFFIX}.img
EXITSTATUS=$?
if [ "${EXITSTATUS}" != "0" ]; then
msg_err "BOOT IMAGE BACKUP FAILED!"
msg_warn "Now skiping backingup boot image..."
if ${ROOT}; then
if [ "${OS}" = "android" ]; then
msg_info "Backing up boot image..."
dd if=${BYNAMEPATH}/boot${BOOTSUFFIX} of=/storage/emulated/0/stock_boot${BOOTSUFFIX}.img
EXITSTATUS=$?
if [ "${EXITSTATUS}" != "0" ]; then
msg_err "BOOT IMAGE BACKUP FAILED!"
msg_warn "Now skiping backingup boot image..."
else
msg_info "Done. Boot image path: /storage/emulated/0/stock_boot${BOOTSUFFIX}.img"
fi
else
msg_info "Done. Boot image path: /storage/emulated/0/stock_boot${BOOTSUFFIX}.img"
msg_info "Current OS: ${OS}. Skiping backup..."
fi
else
msg_info "Current OS: ${OS}. Skiping backup..."
msg_warn "No root. Skiping backup..."
fi

# 加载操作文件
source ./AAPFunction
. ${WORKDIR}/AAPFunction

get_device_boot
get_tools
Expand Down
69 changes: 36 additions & 33 deletions AAPFunction
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
get_device_boot() {
msg_info "Getting boot image..."
if [ -z "${BOOTPATH}" ]; then
msg_info "Current boot: ${BOOTSUFFIX}(If empty: A-Only devices)"
msg_info "Current boot: ${BOOTSUFFIX}(If empty: A-Only device)"
dd if=${BYNAMEPATH}/boot${BOOTSUFFIX} of=${WORKDIR}/boot${BOOTSUFFIX}.img
EXITSTATUS=$?
if [ $EXITSTATUS != 0 ]; then
msg_err "GET BOOT IMAGE FAILED!"
exit 1
msg_fatal "Get boot image from device failed!"
fi
else
cp ${BOOTPATH} ${WORKDIR}/boot${BOOTSUFFIX}.img
fi
msg_info "Done."
}

get_tools() {
cd "${WORKDIR}" || exit 1
msg_info "Downloading kptools-${OS}..."
Expand All @@ -32,8 +30,8 @@ get_tools() {
EXITSTATUS=$?
fi
if [ $EXITSTATUS != 0 ]; then
msg_err "DOWNLOAD FAILED!"
msg_err "Please check your internet connection."
msg_fatal "DOWNLOAD FAILED!"
msg_fatal "Please check your internet connection."
exit 1
fi
chmod +x kptools-${OS}
Expand All @@ -47,17 +45,17 @@ get_tools() {
EXITSTATUS=$?
fi
if [ $EXITSTATUS != 0 ]; then
msg_err "DOWNLOAD FAILED!"
msg_err "Please check your internet connection."
msg_fatal "DOWNLOAD FAILED!"
msg_fatal "Please check your internet connection."
exit 1
fi
msg_info "Done."
msg_info "Downloading magiskboot..."
curl -LO --progress-bar "https://raw.githubusercontent.com/AkinaAcct/APatchTool/main/bin/magiskboot"
EXITSTATUS=$?
if [ $EXITSTATUS != 0 ]; then
msg_err "DOWNLOAD FAILED!"
msg_err "Please check your internet connection."
msg_fatal "DOWNLOAD FAILED!"
msg_fatal "Please check your internet connection."
exit 1
fi
chmod +x magiskboot
Expand All @@ -69,7 +67,7 @@ patch_boot() {
./magiskboot unpack boot${BOOTSUFFIX}.img
EXITSTATUS=$?
if [ $EXITSTATUS != 0 ]; then
msg_err "UNPACK BOOT IMAGE FAILED!"
msg_fatal "UNPACK BOOT IMAGE FAILED!"
exit 1
fi
msg_info "Done."
Expand All @@ -85,47 +83,52 @@ patch_boot() {
./kptools-${OS} --patch --kpimg kpimg-android --skey "${SUPERKEY}" --image kernel --out kernel ${EXTRAARGS}
EXITSTATUS=$?
if [ ${EXITSTATUS} != 0 ]; then
msg_err "PATCH FAILED!"
msg_fatal "PATCH FAILED!"
exit 1
fi
msg_info "Done."
msg_info "Repacking..."
./magiskboot repack boot${BOOTSUFFIX}.img
EXITSTATUS=$?
if [ $EXITSTATUS != 0 ]; then
msg_err "REPACK FAILED!"
msg_fatal "REPACK FAILED!"
exit 1
fi
msg_info "Done. Finished paching."
}

flash_boot() {
if [ "${OS}" = "android" ]; then
msg_info "Flashing boot image..."
if [ -n "${TBOOTSUFFIX}" ]; then
msg_warn "You previously specified that you want to install to another slot. Target slot:${TBOOTSUFFIX}."
BOOTSUFFIX=${TBOOTSUFFIX}
fi
dd if=${WORKDIR}/new-boot.img of=${BYNAMEPATH}/boot${BOOTSUFFIX}
EXITSTATUS=$?
if [ ${EXITSTATUS} != 0 ]; then
msg_err "WARNING! IMAGE FLASH FAILED!"
msg_err "Now trying to restore..."
dd if=${WORKDIR}/boot${BOOTSUFFIX}.img of=${BYNAMEPATH}/boot${BOOTSUFFIX}
if ${ROOT}; then
msg_info "Flashing boot image..."
if [ -n "${TBOOTSUFFIX}" ]; then
msg_warn "You previously specified that you want to install to another slot. Target slot:${TBOOTSUFFIX}."
BOOTSUFFIX=${TBOOTSUFFIX}
fi
dd if=${WORKDIR}/new-boot.img of=${BYNAMEPATH}/boot${BOOTSUFFIX}
EXITSTATUS=$?
if [ ${EXITSTATUS} != 0 ]; then
msg_err "WARNING!!! RESTORE FAILED!!!"
msg_err "Even I can't help you now. You can try to restore boot manually."
exit 1
msg_fatal "WARNING! IMAGE FLASH FAILED!"
msg_fatal "Now trying to restore..."
dd if=${WORKDIR}/boot${BOOTSUFFIX}.img of=${BYNAMEPATH}/boot${BOOTSUFFIX}
EXITSTATUS=$?
if [ ${EXITSTATUS} != 0 ]; then
msg_fatal "WARNING!!! RESTORE FAILED!!!"
msg_fatal "Even I can't help you now. You can try to restore boot manually."
exit 1
fi
msg_info "Restore Sucessfully."
fi
msg_info "Restore Sucessfully."
msg_info "Flash done."
msg_info "Cleaning temporary files..."
rm -rf ${WORKDIR}
msg_info "Done."
else
msg_fatal "No root. Unable to continue. Exited."
exit 1
fi
msg_info "Flash done."
msg_info "Cleaning temporary files..."
rm -rf ${WORKDIR}
msg_info "Done."
else
msg_err "You are trying to change this script. Linux does not require a flashing step. This is the second level of warning."
msg_fatal "You are trying to change this script. Linux does not require a flashing step. This is the second level of warning."
exit 1
fi
}
Expand Down

0 comments on commit 40977f1

Please sign in to comment.