Skip to content

Instantly share code, notes, and snippets.

@betaboon
Created December 3, 2019 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betaboon/2df369ff77cd6c4f1ac7145308ee9831 to your computer and use it in GitHub Desktop.
Save betaboon/2df369ff77cd6c4f1ac7145308ee9831 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
FILENAME=$1
USERDATA_DEVICE=/dev/block/by-name/userdata
FORWARD_PORT=5555
# check if filename has been provided
if [ $# -ne 1 ]; then
echo "Usage: $(basename $0) FILENAME"
exit 1
fi
# check if adb is available
if ! [ -x "$(command -v adb)" ]; then
echo "Error: adb is not installed" >&2
exit 1
fi
# check if pv is available
if ! [ -x "$(command -v pv)" ]; then
echo "Error: pv is not installed" >&2
exit 1
fi
# check if nc is available
if ! [ -x "$(command -v nc)" ]; then
echo "Error: nc is not installed" >&2
exit 1
fi
# check if file(system.img) exists and is readable
if ! [ -r $FILENAME ]; then
echo "Error: $FILENAME is not readable!" >&2
exit 1
fi
# check if device is available via adb
adb_state=$(adb get-state 2>/dev/null)
if [ "$adb_state" != "recovery" ]; then
echo "Error: did not find a device in recovery mode!" >&2
exit 1
fi
# check if device has twrp
adb shell "command -v twrp > /dev/null"
if [ $? -ne 0 ]; then
echo "Error: device is not running twrp!" >&2
exit 1
fi
# check if userdata-device exists
adb shell "[ -w $USERDATA_DEVICE ]"
if [ $? -ne 0 ]; then
echo "Error: could not find userdata-partition on device!" >&2
exit 1
fi
cleanup() {
# remove adb forward
echo "+ removing port-forward"
adb forward --remove tcp:$FORWARD_PORT &>/dev/null
}
main() {
# create adb forward
echo "+ creating port-forward"
adb forward tcp:$FORWARD_PORT tcp:$FORWARD_PORT
sleep 1
# run nc|gunzip|dd on device
echo "+ starting receiver"
$(adb shell "nc -l -p $FORWARD_PORT|gunzip|dd of=$USERDATA_DEVICE") &
sleep 1
# run pv|gzip|on local device
echo "+ starting sender"
pv $FILENAME|gzip|nc -N 127.0.0.1 $FORWARD_PORT
echo "+ done"
exit 0
}
trap exit INT TERM ERR
trap cleanup EXIT
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment