Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux backup support #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
35 changes: 35 additions & 0 deletions Backup-TA.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

VERSION=9.11

echo "[ ------------------------------------------------------------ ]"
echo "[ Backup TA v$VERSION for Sony Xperia ]"
echo "[ ------------------------------------------------------------ ]"
echo "[ Initialization ]"
echo "[ ]"
echo "[ Make sure that you have USB Debugging enabled, you do ]"
echo "[ allow your computer ADB access by accepting its RSA key ]"
echo "[ (only needed for Android 4.2.2 or higher) and grant this ]"
echo "[ ADB process root permissions through superuser. ]"
echo "[ ]"
echo "[ On your computer, you need adb installed and working ]"
echo "[ ------------------------------------------------------------ ]"
echo

PS3='Please enter your choice: '
options=("Backup" "Exit")
select opt in "${options[@]}"

do
case $opt in
"Backup")
cd scripts/linux
./backup.sh $VERSION
exit
;;
"Exit")
break
;;
*) echo invalid option;;
esac
done
95 changes: 95 additions & 0 deletions scripts/linux/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

TMP=../../tmpbak

sudo adb kill-server
sudo adb start-server
adb wait-for-device

./busybox-on.sh

touch /tmp/partfile
./find.sh
partition=`cat /tmp/partfile`
rm /tmp/partfile

echo =======================================
echo FIND TA PARTITION
echo =======================================
echo partition $partition

if [[ $partition == "" ]]
then
echo No partition found
exit -1
fi

echo =======================================
echo BACKUP TA PARTITION
echo =======================================
backup_currentPartitionMD5=`adb shell su -c "$BB md5 $partition" | awk {'print $1'}`
adb shell su -c "$BB dd if=$partition of=/sdcard/backupTA.img"

echo
echo =======================================
echo INTEGRITY CHECK
echo =======================================
backup_backupMD5=`adb shell su -c "$BB md5 /sdcard/backupTA.img" | awk {'print $1'}`

if [ "$backup_currentPartitionMD5" != "$backup_backupMD5" ]
then
echo FAILED - Backup does not match TA Partition. Please try again.
echo $backup_currentPartitionMD5
echo $backup_backupMD5
exit 1
fi

if [ -d $TMP ]
then
mkdir $TMP
fi

echo
echo =======================================
echo PULL BACKUP FROM SDCARD
echo =======================================
adb pull /sdcard/backupTA.img $TMP/TA.img

echo
echo =======================================
echo INTEGRITY CHECK
echo =======================================
localmd5=`md5sum $TMP/TA.img | awk {'print $1'} `

if [ "$localmd5" != "$backup_backupMD5" ]
then
echo FAILED - Backup has gone corrupted while pulling. Please try again.
echo $backup_currentPartitionMD5
echo $backup_backupMD5
echo $localmd5
exit 1
fi

echo
echo =======================================
echo PACKAGE BACKUP
echo =======================================
serial=`adb get-serialno`

cd $TMP

backup_timestamp=`date +%Y-%m-%d.%H%M%S`
echo $backup_backupMD5 > TA.md5
echo $partition > TA.blk
echo $serial > TA.serial
echo $backup_timestamp > TA.timestamp
echo $1 > TA.version
uname -sp > TA.platform

zip ../TA-backup-$backup_timestamp.zip TA.img TA.md5 TA.blk TA.serial TA.timestamp TA.version TA.platform

cd ..

rm -rf $TMP

./scripts/linux/busybox-off.sh
3 changes: 3 additions & 0 deletions scripts/linux/busybox-off.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

adb shell rm /data/local/tmp/busybox-backup-ta
4 changes: 4 additions & 0 deletions scripts/linux/busybox-on.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

adb push ../../tools/busybox /data/local/tmp/busybox-backup-ta
adb shell chmod 755 /data/local/tmp/busybox-backup-ta
29 changes: 29 additions & 0 deletions scripts/linux/check-root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

export BB=/data/local/tmp/busybox-backup-ta

SU=/system/bin/su
suPath=`adb shell $BB ls $SU`

if [[ $suPath =~ "No such" ]]
then
SU=/system/xbin/su
suPath=`adb shell $BB ls $SU`

if [[ $suPath =~ "No such" ]]
then
echo No su found. Did you install it on the phone yet ?
exit 1
fi
fi

rootPermission=`adb shell su -c "$BB echo xtrue"`

if [[ $rootPermission =~ "xtrue" ]]
then
#echo "obtained root : $rootPermission"
echo $SU
else
echo "Failed to obtain root : $rootPermission"
exit -1
fi
79 changes: 79 additions & 0 deletions scripts/linux/find.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

PARTITION_BY_NAME=/dev/block/platform/msm_sdcc.1/by-name/TA

inspectPartition() {
opId=`adb shell su -c "$BB cat /dev/block/$1" | grep -s -m 1 -c 'OP_ID='`
opName=`adb shell su -c "$BB cat /dev/block/$1" | grep -s -m 1 -c 'OP_NAME='`
rooting=`adb shell su -c "$BB cat /dev/block/$1" | grep -s -m 1 -c 'ROOTING_ALLOWED='`
s1Boot=`adb shell su -c "$BB cat /dev/block/$1" | grep -s -m 1 -c -i 'S1_Boot'`
s1Loader=`adb shell su -c "$BB cat /dev/block/$1" | grep -s -m 1 -c -i 'S1_Loader'`
s1Hw=`adb shell su -c "$BB cat /dev/block/$1" | grep -s -m 1 -c -i 'S1_HWConf'`

if [[ "$opId" =~ "1" ]] && [[ "$opName" =~ "1" ]] && [[ "$rooting" =~ "1" ]] && [[ "$s1Boot" =~ "1" ]] && [[ "$s1Loader" =~ "1" ]] && [[ "$s1Hw" =~ "1" ]]
then
echo $1
fi
}

root=`./check-root.sh`

#if not rooted, exit
if [ "$root" == "" ]
then
echo "Not rooted"
exit 1
fi

backup_defaultTA=`adb shell su -c "$BB ls -l --color=never $PARTITION_BY_NAME" | tr -s ' ' | awk '{print $11}'`
#remove invalid head/tail chars
backup_defaultTA=`expr match "$backup_defaultTA" '\([0-9a-z\/]*\)'`

if [[ $backup_defaultTA != /* ]]
then
echo Partition not found by name, trying harder

partition="";

for part in `adb shell su -c "$BB cat /proc/partitions " | awk '{if ($3<=9999) print $4}'|grep --color=never mmcblk|xargs`
do
safepart=`expr match "$part" '\([0-9a-z\/]*\)'`
echo Testing $safepart
possible=$(inspectPartition "$safepart")

if [[ "$possible" =~ "mmc" ]] && [[ "$partition" =~ "mmc" ]]
then
echo Found more than one partition
exit 1
fi

if [[ "$possible" =~ "mmc" ]] && [[ "$partition" == "" ]]
then
partition="$possible"
fi
done

echo partition $partition

if [[ "$partition" == "" ]]
then
echo No partitions found.
exit 1
fi

echo found by type /dev/block/$partition
export backup_defaultTA=/dev/block/$partition
fi

backup_defaultTAvalid=`adb shell su -c "if [ -b $backup_defaultTA ]; then echo '1'; else echo '0'; fi"`

if [[ "$backup_defaultTAvalid" =~ "1" ]]
then
echo $backup_defaultTA > /tmp/partfile
echo found and valid $partition
else
echo Partition found but not valid
echo backup_defaultTA $backup_defaultTA
echo backup_defaultTAvalid $backup_defaultTAvalid
exit 1
fi