#!/bin/bash
base=$(dirname "$_") # must be the very first command!
cd "$base"
cat >/dev/tty <<EOT
*=================================================*
* *
* Partitions Flashing Tool for ZTE Blade V9 Vita *
* *
* Exclusively for 4pda.ru. Made by friendki11er *
* *
*=================================================*
EOT
error() {
local -i rc=$1
shift
echo "ERROR($rc): $*" >/dev/tty
exit $rc
}
for x in emmcdl adb; do
type -P "$x" || error $? "No '$x' command."
done
for x in partitions.txt tools/emmc.mbn; do
[ -f "$x" ] || error 1 "No file '$x'."
done
for x in files/*.bin; do
[ "$x" = 'files/*.bin' ] && error 1 "No 'files/*.bin' here."
done
get_port() {
local prompt="$*"
local port=''
{ lsusb; ls -l /dev/ttyUSB*; } >/dev/tty
while [ -z "$port" ]; do
read -r -p "${prompt}: " port </dev/tty
[ -e "$port" ] && break
[ -e "/dev/$port" ] && { port="/dev/$port"; break; }
[ -e "/dev/tty$port" ] && { port="/dev/tty$port"; break; }
echo "No port '$port'!"
port=''
done >/dev/tty
echo "$prompt is '$port'." > /dev/tty
echo "$port"
}
get_partition() {
local partn=''
select partn in $(awk 'NF == 4 {print $2}' partitions.txt); do
if grep -q " $partn " partitions.txt; then
echo "Partition choosen is '$partn'." >/dev/tty
echo "$partn"
break
fi
done
}
dfu2edl() {
if emmcdl -p "$(get_port DFU Port)" -raw 0xFE; then
echo 'Done.'
else
cat <<EOT
* !!!Failed to change DFU to EDL mode!!! *
* *
* Please check the following: *
* *
* 1) DFU mode is active; *
* 2) DFU port was chosen properly; *
* 3) USB cable is working. *
* *
EOT
fi >/dev/tty
}
adb2edl() {
adb devices
if adb reboot edl; then
echo 'Done.'
else
cat <<EOT
* !!!Failed to change ADB to EDL mode!!! *
* *
* Please check the following: *
* *
* 1) ADB Debugging is active; *
* 2) USB cable is working. *
* *
EOT
fi >/dev/tty
}
backup_partition() {
local edlcom=$(get_port EDL Port) partn=$(get_partition)
mkdir -p backup
if emmcdl -p "$edlcom" -f tools/emmc.mbn -d "$partn" -o "backup/${partn}.bin"; then
echo 'Done.'
else
cat <<EOT
* !!!Failed to backup partition!!! *
* *
* Please check the following: *
* *
* 1) EDL mode is active; *
* 2) EDL port was chosen properly; *
* 3) USB cable is working; *
* 4) Target partition name is correct. *
* *
EOT
fi >/dev/tty
}
flash_partition() {
local edlcom=$(get_port EDL Port) partn=$(get_partition) flash=''
select flash in files/*.bin; do
[ -f "$flash" ] && break
echo 'What??'
done
echo "Flashing '$flash' to partition '$partn' via '$edlcom'..." >/dev/tty
if emmcdl -p "$edlcom" -f tools/emmc.mbn -b "$partn" "$flash"; then
echo 'Done.'
else
cat <<EOT
* !!!Failed to flash partition!!! *
* *
* Please check the following: *
* *
* 1) EDL mode is active; *
* 2) EDL port was chosen properly; *
* 3) USB cable is working; *
* 4) Target partition name is correct. *
* *
EOT
fi >/dev/tty
}
select menu in \
'DFU to EDL mode changer' \
'ADB to EDL mode changer' \
'Backup any partition' \
'Flash any partition' \
'EXIT'; do
if [ -n "$menu" ]; then
case "$menu" in
EXIT) echo "Bye."; exit;;
DFU*) dfu2edl;;
ADB*) adb2edl;;
Backup*)backup_partition;;
Flash*) flash_partition;;
*) echo "What??";;
esac > /dev/tty
fi
done
# EOF #