Created
July 3, 2020 19:50
-
-
Save Igortorrente/d94d61041e0c71b5f0a46cfd947bf31b to your computer and use it in GitHub Desktop.
A script to get android images from a server and flash them into a phone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Igor .. Torrente | |
# Last Edit 02/08/2019 | |
#One script to download them all, | |
#one script to find them, | |
#one script to bring them and in the device flash them | |
# TODO: change -i to a mandatory parameter / choosable .img flash / *test it more* / | |
VERSION=0.7.0 | |
# Customize your default values | |
DEFAULT_USER=torrente | |
DEFAULT_SERVER=mserv019 | |
DEFAULT_PULL_METHOD=local | |
DEFAULT_UPDATE_METHOD=flash | |
DEFAULT_KEY=none | |
DEFAULT_FASTBOOT=Linux/fastboot | |
# Setting default values | |
p=$DEFAULT_PULL_METHOD u=$DEFAULT_USER s=$DEFAULT_SERVER m=$DEFAULT_UPDATE_METHOD k=$DEFAULT_KEY f=$DEFAULT_FASTBOOT | |
i=none o=none r=none | |
# saner programming env: these switches turn some bugs into errors | |
set -o errexit -o pipefail -o noclobber -o nounset -e | |
# -allow a command to fail with !’s side effect on errexit | |
# -use return value from ${PIPESTATUS[0]}, because ! hosed $? | |
! getopt --test > /dev/null | |
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then | |
echo 'I’m sorry, `getopt --test` failed in this environment.' | |
exit 1 | |
fi | |
OPTIONS=f:r:i:o:p:u:s:m:k:h | |
LONGOPTS=fastboot:,rebootwith:,input:,output:,pullmethod:,user:,server:,method:,sshkey:,help | |
# -regarding ! and PIPESTATUS see above | |
# -temporarily store output to be able to check for errors | |
# -activate quoting/enhanced mode (e.g. by writing out “--options”) | |
# -pass arguments only via -- "$@" to separate them correctly | |
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") | |
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then | |
# e.g. return value is 1 | |
# then getopt has complained about wrong arguments to stdout | |
exit 2 | |
fi | |
# read getopt’s output this way to handle the quoting right: | |
eval set -- "$PARSED" | |
# Parser block | |
while true; do | |
case "$1" in | |
-r|--rebootwith) | |
r="$2" | |
shift 2 | |
;; | |
-i|--input) | |
i="$2" | |
shift 2 | |
;; | |
-f|--fastboot) | |
f="$2" | |
shift 2 | |
;; | |
-o|--output) | |
o="$2" | |
shift 2 | |
;; | |
-p|--pullmethod) | |
p="$2" | |
shift 2 | |
;; | |
-u|--user) | |
u="$2" | |
shift 2 | |
;; | |
-s|--server) | |
s="$2" | |
shift 2 | |
;; | |
-m|--method) | |
m="$2" | |
shift 2 | |
;; | |
-k|--sshkey) | |
k="$2" | |
shift 2 | |
;; | |
-h|--help) | |
printf 'V%s - This program flash images from a source into your connected device\n\n' $VERSION | |
printf 'Ex:\t./flash.sh -i /localrepo/John/olson/out/target/product/olson/ -r adb -p rsync -o olson-out -u JohnH -s mserv099 -m flash\n\n' | |
printf '%s --input\t%s\n' '-i' 'Device images folder' | |
printf '%s --fastboot\t%s\n' '-f' 'Command, relative or absolute path to fastboot' | |
printf '%s --output\t%s\n' '-o' 'The path of rsync local folder (required for rsync pull method)' | |
printf '%s --rebootwith\t%s\n' '-r' 'Reboot device with (adb, fastboot or none)' | |
printf '%s --pullmethod\t%s\n' '-p' 'Way to pull images (rsync or local)' | |
printf '%s --method\t%s\n' '-m' 'Method of flash (flash or sync)' | |
printf '%s --user\t%s\n' '-u' 'User in the server (required for rsync pull method)' | |
printf '%s --server\t%s\n' '-s' 'Server address or name (required for rsync pull method)' | |
printf '%s --sshkey\t%s\n' '-k' 'Autentication key on the server (required for rsync pull method)' | |
printf '%s --help\t%s\n' '-h' 'Show This Message' | |
exit 0 | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
echo "Programming error" | |
exit 3 | |
;; | |
esac | |
done | |
# Wait device reboot to boatloader | |
function wait_for_device(){ | |
device_searching=1 | |
device_time_count=0 | |
while [[ "$device_searching" -ne "0" ]] | |
do | |
sleep 1 | |
dlist="" | |
dlist+="$(fastboot devices | grep $1 | grep -v finished | grep -v waiting | grep -v Done | grep -v OKAY | grep -v "\.\.\." | sed -e "s/(bootloader)//g" -e "s/INFO//g" | cut -f1)" | |
if [ "$dlist" ];then | |
return 0 | |
fi | |
device_time_count=$(($device_time_count+1)) | |
done | |
return 0 | |
} | |
# Flash function | |
function flash(){ | |
#printf "input:|%s| rebootwith:|%s| output:|%s| pullmethod:|%s| user:|%s| server:|%s| method:|%s|:key |%s| " $1 $2 $3 $4 $5 $6 $7 $8 | |
#printf "fastboot: |%s|\n\n" $9 | |
# Check -i mandatory parameter | |
if [ "$1" == "none" ]; then | |
>&2 echo "Folder with images is needed" | |
exit 1 | |
fi | |
# Check m mandatory parameter | |
if [ "$7" != "flash" ] && [ "$7" != "sync" ]; then | |
>&2 echo "'$7' is not a valid mode, try flash or sync" | |
exit 1 | |
fi | |
# Check -r parameter | |
if [ "$2" != "adb" ] && [ "$2" != "fastboot" ] && [ "$2" != "none" ]; then | |
>&2 echo " '$2' is not a valid restart mode selected, try adb, fastboot or none" | |
exit 1 | |
fi | |
# Pull images from remote server | |
if [ "$3" == "rsync" ]; then | |
if [ "$6" == "none" ]; then | |
>&2 echo "Rsync mode need a output folder, use -o <Your-Folder> or --output <Your-Folder>" | |
exit 1 | |
fi | |
echo "Starting Rsync of images at $6" | |
if [ "$8" != "none" ]; then | |
rsync -r -e "ssh -i $8" $4@$5:$1/*.img $6 | |
echo "Starting Rsync of fastboot at $6" | |
rsync -r -e "ssh -i $8" $4@$5:$1/Linux $6 | |
else | |
rsync -r $4@$5:$1/*.img $6 | |
echo "Starting Rsync of fastboot at $6" | |
rsync -r $4@$5:$1/Linux $6 | |
fi | |
echo "Ending Rsync" | |
cd $6 | |
elif [ "$3" == "local" ]; then | |
cd $1 | |
else | |
>&2 echo " '$3' is not a valid sync mode selected, try rsync or local" | |
exit 1 | |
fi | |
#### Select device block start here #### | |
# Take all devices conected | |
listDevices=$(adb devices -l | sed '1d;$d') | |
# Count the number of devices | |
numbDevices=$(echo "$listDevices" | wc -l) | |
number=1 | |
# If listDevices is empty | |
if [ -z "$listDevices" ]; then | |
echo 'No device connected' | |
exit 1 | |
fi | |
# If grater than 1 | |
if [ $numbDevices -gt 1 ]; then | |
echo "List of devices:" | |
# Print all connected devices and its numbers | |
for ((i=1; i <= $numbDevices; i++)) | |
do | |
printf "%s - %s\n" $i "$(echo "$listDevices" | head -n $i | tail -1)" | |
done | |
printf "\n%s\n" "Choose the device number that you want flash/sync:" | |
# Read the user input | |
read number | |
echo ' ' | |
# Check it is a number | |
if ! [[ "$number" =~ ^[0-9]+$ ]]; then | |
echo ''$number' is not a number' | |
exit 1 | |
fi | |
# and if it is in the range of devices | |
if [ $number -lt 1 ] || [ $number -gt $numbDevices ]; then | |
echo ''$number' is not in the range' | |
exit 1 | |
fi | |
fi | |
# Cut only the selected device | |
selectedDevice=$(echo "$listDevices" | head -n $number | tail -1 | cut -d ' ' -f1) | |
#### Select device block ends here #### | |
# Restart device | |
if [ "$2" == "adb" ]; then | |
printf "Restarting the device..." | |
echo " with '$2'" | |
adb reboot-bootloader -d $selectedDevice | |
elif [ "$2" == "fastboot" ]; then | |
printf "Restarting the device..." | |
echo " with '$2'" | |
$9 reboot-bootloader -s $selectedDevice | |
fi | |
# Wait bootloader | |
if [ "$7" != "sync" ]; then | |
wait=$(wait_for_device $selectedDevice) | |
if [ "$wait" == 1 ]; then | |
echo "Time expired device not seen" | |
exit 1 | |
fi | |
fi | |
# Update image block | |
if [ "$7" == "flash" ]; then | |
$9 flash boot boot.img -s $selectedDevice | |
$9 flash dtbo dtbo.img -s $selectedDevice | |
$9 flash vendor vendor.img -s $selectedDevice | |
$9 flash system system.img -s $selectedDevice | |
$9 flash vbmeta vbmeta.img -s $selectedDevice | |
$9 -w | |
$9 reboot -s $selectedDevice | |
elif [ "$7" == "sync" ]; then | |
ANDROID_PRODUCT_OUT=. adb sync | |
fi | |
} | |
flash $i $r $p $u $s $o $m $k $f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment