Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 1 | #!/sbin/sh |
| 2 | |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 3 | # nandroid v2.1 - an Android backup tool for the G1 by infernix and brainaid |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 4 | |
| 5 | # Requirements: |
| 6 | |
| 7 | # - a modded android in recovery mode (JF 1.3 will work by default) |
| 8 | # - adb shell as root in recovery mode if not using a pre-made recovery image |
| 9 | # - busybox in recovery mode |
| 10 | # - dump_image-arm-uclibc compiled and in path on phone |
| 11 | # - mkyaffs2image-arm-uclibc compiled and installed in path on phone |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 12 | |
| 13 | # Reference data: |
| 14 | |
| 15 | # dev: size erasesize name |
| 16 | #mtd0: 00040000 00020000 "misc" |
| 17 | #mtd1: 00500000 00020000 "recovery" |
| 18 | #mtd2: 00280000 00020000 "boot" |
| 19 | #mtd3: 04380000 00020000 "system" |
| 20 | #mtd4: 04380000 00020000 "cache" |
| 21 | #mtd5: 04ac0000 00020000 "userdata" |
| 22 | #mtd6 is everything, dump splash1 with: dd if=/dev/mtd/mtd6ro of=/sdcard/splash1.img skip=19072 bs=2048 count=150 |
| 23 | |
| 24 | # We don't dump misc or cache because they do not contain any useful data that we are aware of at this time. |
| 25 | |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 26 | # Logical steps (v2.1): |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 27 | # |
| 28 | # 0. test for a target dir and the various tools needed, if not found then exit with error. |
| 29 | # 1. check "adb devices" for a device in recovery mode. set DEVICEID variable to the device ID. abort when not found. |
| 30 | # 2. mount system and data partitions read-only, set up adb portforward and create destdir |
| 31 | # 3. check free space on /cache, exit if less blocks than 20MB free |
| 32 | # 4. push required tools to device in /cache |
| 33 | # 5 for partitions boot recovery misc: |
| 34 | # 5a get md5sum for content of current partition *on the device* (no data transfered) |
| 35 | # 5b while MD5sum comparison is incorrect (always is the first time): |
| 36 | # 5b1 dump current partition to a netcat session |
| 37 | # 5b2 start local netcat to dump image to current dir |
| 38 | # 5b3 compare md5sums of dumped data with dump in current dir. if correct, contine, else restart the loop (6b1) |
| 39 | # 6 for partitions system data: |
| 40 | # 6a get md5sum for tar of content of current partition *on the device* (no data transfered) |
| 41 | # 6b while MD5sum comparison is incorrect (always is the first time): |
| 42 | # 6b1 tar current partition to a netcat session |
| 43 | # 6b2 start local netcat to dump tar to current dir |
| 44 | # 6b3 compare md5sums of dumped data with dump in current dir. if correct, contine, else restart the loop (6b1) |
| 45 | # 6c if i'm running as root: |
| 46 | # 6c1 create a temp dir using either tempdir command or the deviceid in /tmp |
| 47 | # 6c2 extract tar to tempdir |
| 48 | # 6c3 invoke mkyaffs2image to create the img |
| 49 | # 6c4 clean up |
| 50 | # 7. remove tools from device /cache |
| 51 | # 8. umount system and data on device |
| 52 | # 9. print success. |
| 53 | |
| 54 | |
| 55 | DEVICEID=foo |
| 56 | RECOVERY=foo |
| 57 | |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 58 | echo "nandroid-mobile v2.1" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 59 | |
| 60 | |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 61 | if [ "$1" == "" ]; then |
| 62 | echo "Usage: $0 {backup|restore} [/path/to/nandroid/backup/]" |
| 63 | echo "- backup will store a full system backup on /sdcard/nandroid/$DEVICEID" |
| 64 | echo "- restore path will restore the last made backup for boot, system, recovery and data" |
| 65 | exit 0 |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 66 | fi |
| 67 | |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 68 | case $1 in |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 69 | backup) |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 70 | mkyaffs2image=`which mkyaffs2image` |
| 71 | if [ "$mkyaffs2image" == "" ]; then |
| 72 | mkyaffs2image=`which mkyaffs2image-arm-uclibc` |
| 73 | if [ "$mkyaffs2image" == "" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 74 | echo "error: mkyaffs2image or mkyaffs2image-arm-uclibc not found in path" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 75 | exit 1 |
| 76 | fi |
| 77 | fi |
| 78 | dump_image=`which dump_image` |
| 79 | if [ "$dump_image" == "" ]; then |
| 80 | dump_image=`which dump_image-arm-uclibc` |
| 81 | if [ "$dump_image" == "" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 82 | echo "error: dump_image or dump_image-arm-uclibc not found in path" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 83 | exit 1 |
| 84 | fi |
| 85 | fi |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 86 | break |
| 87 | ;; |
| 88 | restore) |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 89 | flash_image=`which flash_image` |
| 90 | if [ "$flash_image" == "" ]; then |
| 91 | flash_image=`which flash_image-arm-uclibc` |
| 92 | if [ "$flash_image" == "" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 93 | echo "error: flash_image or flash_image-arm-uclibc not found in path" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 94 | exit 1 |
| 95 | fi |
| 96 | fi |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 97 | break |
| 98 | ;; |
| 99 | esac |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 100 | |
| 101 | # 1 |
| 102 | DEVICEID=`cat /proc/cmdline | sed "s/.*serialno=//" | cut -d" " -f1` |
| 103 | RECOVERY=`cat /proc/cmdline | grep "androidboot.mode=recovery"` |
| 104 | if [ "$RECOVERY" == "foo" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 105 | echo "error: not running in recovery mode, aborting" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 106 | exit 1 |
| 107 | fi |
| 108 | if [ "$DEVICEID" == "foo" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 109 | echo "error: device id not found in /proc/cmdline, aborting" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 110 | exit 1 |
| 111 | fi |
| 112 | if [ ! "`id -u 2>/dev/null`" == "0" ]; then |
| 113 | if [ "`whoami 2>&1 | grep 'uid 0'`" == "" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 114 | echo "error: must run as root, aborting" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 115 | exit 1 |
| 116 | fi |
| 117 | fi |
| 118 | |
| 119 | |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 120 | case $1 in |
| 121 | restore) |
| 122 | ENERGY=`cat /sys/class/power_supply/battery/capacity` |
| 123 | if [ "`cat /sys/class/power_supply/battery/status`" == "Charging" ]; then |
| 124 | ENERGY=100 |
| 125 | fi |
| 126 | if [ ! $ENERGY -ge 30 ]; then |
| 127 | echo "Error: not enough battery power" |
| 128 | echo "Connect charger or USB power and try again" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 129 | exit 1 |
| 130 | fi |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 131 | RESTOREPATH=$2 |
| 132 | if [ ! -f $RESTOREPATH/nandroid.md5 ]; then |
| 133 | echo "error: $RESTOREPATH/nandroid.md5 not found, cannot verify backup data" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 134 | exit 1 |
| 135 | fi |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 136 | umount /system 2>/dev/null |
| 137 | umount /data 2>/dev/null |
| 138 | if [ ! "`mount | grep data`" == "" ]; then |
| 139 | echo "error: unable to umount /data, aborting" |
| 140 | exit 1 |
| 141 | fi |
| 142 | if [ ! "`mount | grep system`" == "" ]; then |
| 143 | echo "error: unable to umount /system, aborting" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 144 | exit 1 |
| 145 | fi |
| 146 | |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 147 | echo "Verifying backup images..." |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 148 | CWD=$PWD |
| 149 | cd $RESTOREPATH |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 150 | md5sum -c nandroid.md5 |
| 151 | if [ $? -eq 1 ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 152 | echo "error: md5sum mismatch, aborting" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 153 | exit 1 |
| 154 | fi |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 155 | for image in boot recovery; do |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 156 | echo "Flashing $image..." |
| 157 | $flash_image $image $image.img |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 158 | done |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 159 | echo "Flashing system and data not currently supported" |
| 160 | echo "Restore done" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 161 | exit 0 |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 162 | ;; |
| 163 | backup) |
| 164 | break |
| 165 | ;; |
| 166 | *) |
| 167 | echo "Usage: $0 {backup|restore} [/path/to/nandroid/backup/]" |
| 168 | echo "- backup will store a full system backup on /sdcard/nandroid/$DEVICEID" |
| 169 | echo "- restore path will restore the last made backup for boot, system, recovery and data" |
| 170 | exit 1 |
| 171 | ;; |
| 172 | esac |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 173 | |
| 174 | # 2. |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 175 | echo "mounting system and data read-only, sdcard read-write" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 176 | umount /system 2>/dev/null |
| 177 | umount /data 2>/dev/null |
| 178 | umount /sdcard 2>/dev/null |
| 179 | mount -o ro /system || FAIL=1 |
| 180 | mount -o ro /data || FAIL=2 |
| 181 | mount /sdcard || mount /dev/block/mmcblk0 /sdcard || FAIL=3 |
| 182 | case $FAIL in |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 183 | 1) echo "Error mounting system read-only"; umount /system /data /sdcard; exit 1;; |
| 184 | 2) echo "Error mounting data read-only"; umount /system /data /sdcard; exit 1;; |
| 185 | 3) echo "Error mounting sdcard read-write"; umount /system /data /sdcard; exit 1;; |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 186 | esac |
| 187 | |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 188 | TIMESTAMP="`date +%Y%m%d-%H%M`" |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 189 | DESTDIR="/sdcard/nandroid/$DEVICEID/$TIMESTAMP" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 190 | if [ ! -d $DESTDIR ]; then |
| 191 | mkdir -p $DESTDIR |
| 192 | if [ ! -d $DESTDIR ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 193 | echo "error: cannot create $DESTDIR" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 194 | umount /system 2>/dev/null |
| 195 | umount /data 2>/dev/null |
| 196 | umount /sdcard 2>/dev/null |
| 197 | exit 1 |
| 198 | fi |
| 199 | else |
| 200 | touch $DESTDIR/.nandroidwritable |
| 201 | if [ ! -e $DESTDIR/.nandroidwritable ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 202 | echo "error: cannot write to $DESTDIR" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 203 | umount /system 2>/dev/null |
| 204 | umount /data 2>/dev/null |
| 205 | umount /sdcard 2>/dev/null |
| 206 | exit 1 |
| 207 | fi |
| 208 | rm $DESTDIR/.nandroidwritable |
| 209 | fi |
| 210 | |
| 211 | # 3. |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 212 | echo "checking free space on sdcard" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 213 | FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`" |
| 214 | # we need about 130MB for the dump |
| 215 | if [ $FREEBLOCKS -le 130000 ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 216 | echo "error: not enough free space available on sdcard (need 130mb), aborting." |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 217 | umount /system 2>/dev/null |
| 218 | umount /data 2>/dev/null |
| 219 | umount /sdcard 2>/dev/null |
| 220 | exit 1 |
| 221 | fi |
| 222 | |
| 223 | |
| 224 | |
| 225 | if [ -e /dev/mtd/mtd6ro ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 226 | echo -n "Dumping splash1 from device over tcp to $DESTDIR/splash1.img..." |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 227 | dd if=/dev/mtd/mtd6ro of=$DESTDIR/splash1.img skip=19072 bs=2048 count=150 2>/dev/null |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 228 | echo "done" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 229 | sleep 1s |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 230 | echo -n "Dumping splash2 from device over tcp to $DESTDIR/splash2.img..." |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 231 | dd if=/dev/mtd/mtd6ro of=$DESTDIR/splash2.img skip=19456 bs=2048 count=150 2>/dev/null |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 232 | echo "done" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 233 | fi |
| 234 | |
| 235 | |
| 236 | # 5. |
| 237 | for image in boot recovery misc; do |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 238 | # 5a |
| 239 | DEVICEMD5=`$dump_image $image - | md5sum | awk '{ print $1 }'` |
| 240 | sleep 1s |
| 241 | MD5RESULT=1 |
| 242 | # 5b |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 243 | echo -n "Dumping $image to $DESTDIR/$image.img..." |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 244 | ATTEMPT=0 |
| 245 | while [ $MD5RESULT -eq 1 ]; do |
| 246 | let ATTEMPT=$ATTEMPT+1 |
| 247 | # 5b1 |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 248 | $dump_image $image $DESTDIR/$image.img |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 249 | sync |
| 250 | # 5b3 |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 251 | echo "${DEVICEMD5} $DESTDIR/$image.img" | md5sum -c -s - |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 252 | if [ $? -eq 1 ]; then |
| 253 | true |
| 254 | else |
| 255 | MD5RESULT=0 |
| 256 | fi |
| 257 | if [ "$ATTEMPT" == "5" ]; then |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 258 | echo "fatal error while trying to dump $image, aborting" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 259 | umount /system |
| 260 | umount /data |
| 261 | umount /sdcard |
| 262 | exit 1 |
| 263 | fi |
| 264 | done |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 265 | echo "done" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 266 | done |
| 267 | |
| 268 | # 6 |
| 269 | for image in system data cache; do |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 270 | # 6a |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 271 | echo -n "Dumping $image to $DESTDIR/$image.img..." |
| 272 | $mkyaffs2image /$image $DESTDIR/$image.img |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 273 | sync |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 274 | echo "done" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 275 | done |
| 276 | |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 277 | |
| 278 | # 7. |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 279 | echo -n "generating md5sum file..." |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 280 | CWD=$PWD |
| 281 | cd $DESTDIR |
| 282 | md5sum *img > nandroid.md5 |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 283 | cd $CWD |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 284 | echo "done" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 285 | |
| 286 | # 8. |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 287 | echo "unmounting system, data and sdcard" |
Koushik K. Dutta | 8ce0be4 | 2010-02-20 15:59:06 -0800 | [diff] [blame] | 288 | umount /system |
| 289 | umount /data |
| 290 | umount /sdcard |
| 291 | |
| 292 | # 9. |
Koushik K. Dutta | c788c26 | 2010-02-20 17:25:03 -0800 | [diff] [blame^] | 293 | echo "Backup successful." |