#!/data/data/com.termux/files/usr/bin/bash # ------------------------------------- # Helper script for samloader on Termux # Github: lekron42 # Version: v1.03 # ------------------------------------- show_usage() { echo "This script automates firmware downloading using samloader" echo -e "Usage: $(basename $(readlink -f $0)) [argument]\n" echo "Arguments:" echo " -f|--force : Force update even if no new version is found" echo " -r|--repair : Update samloader to fix download failure" echo " -h|--help : Show this help" exit 0 } update_samloader() { { proceed=$(pip3 uninstall samloader | tee /dev/fd/5); } 5>&1 if [ $(echo "$proceed" | grep -c "Successfully") -gt 0 ]; then pip3 install git+https://github.com/nlscc/samloader.git echo "Successfully reinstalled samloader." else echo "Uninstalling cancelled.." fi exit 0 } if [ $(echo "$0" | grep -c "termux") -eq 0 ]; then echo "Script not executed in Termux! Exiting.." 1>&2 exit 1 fi if [ $# -eq 1 ]; then case "$1" in -h|--help) show_usage ;; -f|--force) force_download=true ;; -r|--repair) update_samloader ;; *) echo "Incorrect input!" 1>&2 show_usage ;; esac elif [ $# -gt 1 ]; then echo "Only 1 argument supported. $# given." 1>&2 exit 1 fi i_python=$(pkg list-installed "python" 2> /dev/null | grep -c "installed") i_git=$(pkg list-installed "git" 2> /dev/null | grep -c "installed") if [ "$i_python" -eq 0 ] && [ "$i_git" -eq 0 ]; then echo "Python and Git are not installed but required.." read -p "Install now? (y/N) " r_pg case "$r_pg" in y|Y) ;; *) exit 0;; esac apt-get update pkg install python git if [ $? -ne 0 ]; then echo "Failed to install python and/or git!" 1>&2 exit 1 fi elif [ "$i_python" -eq 0 ]; then echo "Python is required but not installed.." read -p "Install now? (y/N) " r_p case "$r_p" in y|Y) ;; *) exit 0;; esac apt-get update pkg install python if [ $? -ne 0 ]; then echo "Failed to install python!" 1>&2 exit 1 fi elif [ "$i_git" -eq 0 ]; then echo "Git is required but not installed.." read -p "Install now? (y/N) " r_g case "$r_g" in y|Y) ;; *) exit 0;; esac apt-get update pkg install git if [ $? -ne 0 ]; then echo "Failed to install git!" 1>&2 exit 1 fi fi if [ $(pip3 list 2> /dev/null | grep -c "samloader") -eq 0 ]; then echo "Samloader is required but not installed.." read -p "Install now? (y/N) " r_install case "$r_install" in y|Y) ;; *) exit 0;; esac pip3 install git+https://github.com/nlscc/samloader.git fi model="$(getprop 'ro.product.model')" region="$(getprop 'ro.csc.sales_code')" current_pda="$(getprop 'ro.system.build.version.incremental')" current_csc="$(getprop 'ro.omc.build.version')" echo -e "- Device:\t$model / $region" latest_version="$(samloader -m $model -r $region checkupdate)" latest_pda="$(echo $latest_version | cut -d/ -f1)" latest_csc="$(echo $latest_version | cut -d/ -f2)" echo -e "- Installed:\t$current_pda/$current_csc" echo -e "- Latest:\t$latest_pda/$latest_csc" echo "---------------" if [ "$current_pda" = "$latest_pda" ] && [ "$current_csc" = "$latest_csc" ]; then if [ ! "$force_download" = true ]; then echo "No new version found! Use --force to download anyway.." exit 0 fi fi read -p "Download? (y/N) " r_download case "$r_download" in y|Y) ;; *) exit 0;; esac while true; do read -p "Please enter an absolute path to save the file to after downloading (e.g. \"/sdcard/\"): " r_path parse_f=$(echo "$r_path" | head -c 1) parse_l=$(echo "$r_path" | tail -c 2) if [ "$parse_f" = "\"" ] || [ "$parse_l" = "\"" ]; then echo "Please enter path again without quotes!" 1>&2 else if [ "$parse_l" != "/" ]; then r_path="$r_path/" fi if [ -e "$r_path" ]; then break else echo "Path not valid! Please try again.." 1>&2 fi fi done touch "$r_path.samcheck" 2> /dev/null if [ $? -ne 0 ]; then echo "Permission denied! Running \"termux-setup-storage\".." 1>&2 echo "Waiting for storage permission.." sleep 1 termux-setup-storage count=0 while true; do touch "$r_path.samcheck" 2> /dev/null if [ $? -eq 0 ]; then break fi let count++ if [ "$count" -gt 30 ]; then echo "Operation timed out!" 1>&2 exit 1 fi sleep 1 done fi if [ -e "$r_path.samcheck" ]; then rm -f "$r_path.samcheck" fi echo "Downloading..." filename="$(samloader -m $model -r $region download -v $latest_version -O $r_path/. | cut -d' ' -f2)" size=$(wc -c "$r_path$filename" | cut -d' ' -f1) if [ "$size" -lt 4500000000 ]; then echo -e "\n\nDownload failed.." 1>&2 echo "Please use $(basename $(readlink -f $0)) --repair.\n" 1>&2 exit 1 fi echo "Downloading finished!" encr_protocol="$(echo $filename | tail -c 2)" input="$r_path/$filename" output="$r_path/$(echo $filename | cut -d. -f1-2)" echo "Decrypting..." samloader -m "$model" -r "$region" decrypt -v "$latest_version" -V "$encr_protocol" -i "$input" -o "$output" echo "Decrypting finished!" if [ -e "$input" ]; then rm "$input" fi echo "Done! Output:" echo "$r_path$(echo $filename | cut -d. -f1-2)" exit 0