2015-11-25 11:32:55 +01:00
|
|
|
#!/bin/bash
|
2016-02-18 12:02:44 +01:00
|
|
|
haveSerial=false
|
2016-02-18 15:45:22 +01:00
|
|
|
shouldWipe=false
|
2016-02-18 12:02:44 +01:00
|
|
|
serial=""
|
2016-02-18 15:38:50 +01:00
|
|
|
used_serial=""
|
|
|
|
forbidden_serials="b06474cb"
|
|
|
|
|
2016-02-18 12:02:44 +01:00
|
|
|
function usage {
|
2016-02-23 10:03:09 +01:00
|
|
|
echo -e "Usage: $(basename $0) [OPTIONS] [FILES]"
|
|
|
|
echo -e " Flash Android images"
|
|
|
|
echo -e ""
|
|
|
|
echo -e "Available options:"
|
|
|
|
echo -e " -s Specify the serial number of the device to flash"
|
|
|
|
echo -e " If not provided, only one device should be connected in fastboot mode or in adb mode"
|
|
|
|
echo -e " -w Wipe cache and data partition after flashing"
|
|
|
|
echo -e ""
|
|
|
|
echo -e "FILES is a list of file in .img separated by space"
|
|
|
|
echo -e "If no FILES provied all the .img in the current directory will be flashed"
|
|
|
|
echo -e "e.g. $(basename $0) -s 132456789 out/target/device/boot.img out/target/device/system.img"
|
2016-02-18 15:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function check_forbidden_serial {
|
|
|
|
if [[ $forbidden_serials =~ $1 ]]; then
|
|
|
|
echo "Error: You should not flash this device !"
|
|
|
|
echo " Goodbye!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_fastboot_serial {
|
|
|
|
serials=$(fastboot devices | grep "fastboot" | awk '{print $1}')
|
|
|
|
nb_device=$(fastboot devices | grep "fastboot" | wc -l)
|
|
|
|
if $haveSerial; then
|
|
|
|
if [[ ! $serials =~ $serial ]]; then
|
|
|
|
echo "Cannot find provided serial $serial: Expecting the device to be rebooting in fastboot mode "
|
|
|
|
fi
|
|
|
|
used_serial=$serial
|
|
|
|
else
|
2016-02-23 10:03:09 +01:00
|
|
|
if [[ $nb_device != 1 ]]; then
|
|
|
|
echo "Error: Too much or no device connected!"
|
|
|
|
echo " Provide a serial number with -s option or disconnect a device"
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-02-18 15:38:50 +01:00
|
|
|
used_serial=$serials
|
|
|
|
fi
|
|
|
|
check_forbidden_serial $used_serial
|
|
|
|
}
|
|
|
|
|
|
|
|
function reboot_and_wait_for_device {
|
|
|
|
check_forbidden_serial $1
|
|
|
|
adb -s $1 reboot bootloader;
|
2016-03-17 14:45:30 +01:00
|
|
|
fastboot devices | grep $1 >/dev/null 2>&1
|
2016-02-18 15:38:50 +01:00
|
|
|
while [ $? == 1 ]; do
|
|
|
|
sleep 1;
|
|
|
|
echo "Waiting for $1 in fastboot mode..."
|
2016-03-17 14:45:30 +01:00
|
|
|
fastboot devices | grep $1 >/dev/null 2>&1
|
2016-02-18 15:38:50 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_presence {
|
|
|
|
if $haveSerial ; then
|
2016-03-17 14:45:30 +01:00
|
|
|
fastboot devices | grep $serial >/dev/null 2>&1
|
2016-02-18 15:38:50 +01:00
|
|
|
if [[ $? == 1 ]] ; then
|
2016-03-17 14:45:30 +01:00
|
|
|
adb devices | grep $serial >/dev/null 2>&1
|
2016-02-18 15:38:50 +01:00
|
|
|
if [[ $? == 0 ]] ; then
|
|
|
|
reboot_and_wait_for_device $serial
|
|
|
|
else
|
2016-03-07 11:02:28 +01:00
|
|
|
echo "Error: Cannot found device $serial"
|
2016-02-18 15:38:50 +01:00
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
nb_dev=$(fastboot devices | grep "fastboot" | wc -l)
|
|
|
|
if [[ $nb_dev == 0 ]]; then
|
|
|
|
nb_dev=$(adb devices | grep "\<device\>"| wc -l )
|
|
|
|
if [[ $nb_dev == 0 ]]; then
|
|
|
|
echo "Error: Cannot found any device in fastboot or adb mode"
|
|
|
|
exit 1;
|
|
|
|
elif [[ $nb_dev == 1 ]]; then
|
|
|
|
dev=$(adb devices | grep "\<device\>" | awk '{print $1}')
|
|
|
|
echo "Reboot device $dev in fastboot mode"
|
|
|
|
serial=$dev
|
|
|
|
haveSerial=true
|
|
|
|
reboot_and_wait_for_device $serial
|
|
|
|
else
|
|
|
|
echo "Too much adb devices connected. Select one with -s or disconnect a device or reboot one in fastboot mode"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2016-02-18 12:02:44 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 15:45:22 +01:00
|
|
|
while getopts ":s:w" opt; do
|
2016-02-18 12:02:44 +01:00
|
|
|
case $opt in
|
|
|
|
s) echo "Using Serial $OPTARG"
|
|
|
|
serial=$OPTARG
|
|
|
|
haveSerial=true
|
|
|
|
;;
|
2016-02-18 15:45:22 +01:00
|
|
|
w)
|
|
|
|
shouldWipe=true
|
|
|
|
;;
|
2016-02-18 12:02:44 +01:00
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( OPTIND - 1 ))
|
|
|
|
|
2016-02-18 15:38:50 +01:00
|
|
|
if $haveSerial ; then
|
|
|
|
check_forbidden_serial $serial
|
|
|
|
fi
|
|
|
|
|
2015-11-25 11:32:55 +01:00
|
|
|
if [ $# -eq 0 ];
|
|
|
|
then
|
2016-02-18 15:38:50 +01:00
|
|
|
imgList=$(ls *.img 2>/dev/null)
|
2015-11-25 11:32:55 +01:00
|
|
|
else
|
|
|
|
imgList=$@
|
|
|
|
fi
|
2016-02-18 15:44:05 +01:00
|
|
|
echo "Images to flash: $imgList"
|
|
|
|
|
|
|
|
|
2015-11-25 11:32:55 +01:00
|
|
|
if [ -z "$imgList" ];
|
|
|
|
then
|
|
|
|
echo "No img to Flash"
|
|
|
|
exit 1
|
|
|
|
else
|
2016-02-18 15:38:50 +01:00
|
|
|
check_presence
|
|
|
|
check_fastboot_serial
|
2016-02-18 12:02:44 +01:00
|
|
|
echo "Flashing images"
|
|
|
|
for img in $imgList; do
|
|
|
|
imageName=$(basename $img)
|
|
|
|
imageName=${imageName%.img}
|
2016-02-18 15:45:22 +01:00
|
|
|
fastboot -s $used_serial -S 256M flash ${imageName} $img;
|
|
|
|
if $shouldWipe; then
|
|
|
|
fastboot -s $used_serial -w
|
|
|
|
fi
|
2016-02-18 12:02:44 +01:00
|
|
|
done && fastboot reboot
|
2015-11-25 11:32:55 +01:00
|
|
|
fi
|