diff --git a/Backup-TA.sh b/Backup-TA.sh new file mode 100755 index 0000000..cc8d1ec --- /dev/null +++ b/Backup-TA.sh @@ -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 \ No newline at end of file diff --git a/scripts/linux/backup.sh b/scripts/linux/backup.sh new file mode 100755 index 0000000..13d53b4 --- /dev/null +++ b/scripts/linux/backup.sh @@ -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 \ No newline at end of file diff --git a/scripts/linux/busybox-off.sh b/scripts/linux/busybox-off.sh new file mode 100755 index 0000000..e0bcfaa --- /dev/null +++ b/scripts/linux/busybox-off.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +adb shell rm /data/local/tmp/busybox-backup-ta \ No newline at end of file diff --git a/scripts/linux/busybox-on.sh b/scripts/linux/busybox-on.sh new file mode 100755 index 0000000..63e95a7 --- /dev/null +++ b/scripts/linux/busybox-on.sh @@ -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 \ No newline at end of file diff --git a/scripts/linux/check-root.sh b/scripts/linux/check-root.sh new file mode 100755 index 0000000..ef7712b --- /dev/null +++ b/scripts/linux/check-root.sh @@ -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 \ No newline at end of file diff --git a/scripts/linux/find.sh b/scripts/linux/find.sh new file mode 100755 index 0000000..f2050d5 --- /dev/null +++ b/scripts/linux/find.sh @@ -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 \ No newline at end of file