blob: b5cae3d13c14360aa062443a86fb158e54e5f7b0 [file] [log] [blame]
Koushik K. Dutta8ce0be42010-02-20 15:59:06 -08001#!/sbin/sh
2
3# nandroid v2.2.2 - an Android backup tool for the G1 by infernix and brainaid
4# restore capability added by cyanogen
5
6# pensive modified to allow to add prefixes to backups, and to restore specific backups
7# pensive added the ability to exclude various images from the restore/backup operations, allows to preserve the newer
8# recovery image if an older backup is being restored or to preserve user data. Also, saves space by not backing up
9# partitions which change rarely.
10# pensive added compressing backups and restoring compressed backups
11# pensive added fetching system updates directly from the web into /sdcard/update.zip
12# pensive added fetching system updates directly from the web into /cache and applying it.
13# pensive added moving *update*.zip from /sdcard/download where a browser puts it to /sdcard/update.zip
14# pensive added deletion of stale backups
15# pensive added backup for ext2 partition on the sdcard to switch roms
16# pensive added composite options --save NAME and --switchto NAME to switch ROMS
17# pensive added list backup anywhere on the sdcard
18# pensive added list updates (more precisely *.zip) anywhere on the sdcard
19# Amon_RA : ext restore -> added check if ext backup is existing
20# Amon_RA : ext restore -> added check if ext parition is existing
21
22# Requirements:
23
24# - a modded android in recovery mode (JF 1.3 will work by default)
25# - adb shell as root in recovery mode if not using a pre-made recovery image
26# - busybox in recovery mode
27# - dump_image-arm-uclibc compiled and in path on phone
28# - mkyaffs2image-arm-uclibc compiled and installed in path on phone
29# - flash_image-arm-uclibc compiled and in path on phone
30# - unyaffs-arm-uclibc compiled and in path on phone
31# - for [de]compression needs gzip or bzip2, part of the busybox
32# - wget for the wireless updates
33
34# Reference data:
35
36# dev: size erasesize name
37#mtd0: 00040000 00020000 "misc"
38#mtd1: 00500000 00020000 "recovery"
39#mtd2: 00280000 00020000 "boot"
40#mtd3: 04380000 00020000 "system"
41#mtd4: 04380000 00020000 "cache"
42#mtd5: 04ac0000 00020000 "userdata"
43#mtd6 is everything, dump splash1 with: dd if=/dev/mtd/mtd6ro of=/sdcard/splash1.img skip=19072 bs=2048 count=150
44
45# We don't dump misc or cache because they do not contain any useful data that we are aware of at this time.
46
47# Logical steps (v2.2.1):
48#
49# 0. test for a target dir and the various tools needed, if not found then exit with error.
50# 1. check "adb devices" for a device in recovery mode. set DEVICEID variable to the device ID. abort when not found.
51# 2. mount system and data partitions read-only, set up adb portforward and create destdir
52# 3. check free space on /cache, exit if less blocks than 20MB free
53# 4. push required tools to device in /cache
54# 5 for partitions boot recovery misc:
55# 5a get md5sum for content of current partition *on the device* (no data transfered)
56# 5b while MD5sum comparison is incorrect (always is the first time):
57# 5b1 dump current partition to a netcat session
58# 5b2 start local netcat to dump image to current dir
59# 5b3 compare md5sums of dumped data with dump in current dir. if correct, contine, else restart the loop (6b1)
60# 6 for partitions system data:
61# 6a get md5sum for tar of content of current partition *on the device* (no data transfered)
62# 6b while MD5sum comparison is incorrect (always is the first time):
63# 6b1 tar current partition to a netcat session
64# 6b2 start local netcat to dump tar to current dir
65# 6b3 compare md5sums of dumped data with dump in current dir. if correct, contine, else restart the loop (6b1)
66# 6c if i'm running as root:
67# 6c1 create a temp dir using either tempdir command or the deviceid in /tmp
68# 6c2 extract tar to tempdir
69# 6c3 invoke mkyaffs2image to create the img
70# 6c4 clean up
71# 7. remove tools from device /cache
72# 8. umount system and data on device
73# 9. print success.
74
75
76DEVICEID=foo
77RECOVERY=foo
78
79SUBNAME=""
80NORECOVERY=0
81NOBOOT=0
82NODATA=0
83NOSYSTEM=0
84NOMISC=0
85NOCACHE=0
86NOSPLASH1=0
87NOSPLASH2=0
88EXT2=0
89
90COMPRESS=0
91GETUPDATE=0
92RESTORE=0
93BACKUP=0
94DELETE=0
95WEBGET=0
96LISTBACKUP=0
97LISTUPDATE=0
98AUTOREBOOT=0
99AUTOAPPLY=0
100ITSANUPDATE=0
101ITSANIMAGE=0
102WEBGETSOURCE=""
103WEBGETTARGET="/sdcard"
104
105DEFAULTUPDATEPATH="/sdcard/download"
106
107DEFAULTWEBUPDATE=http://n0rp.chemlab.org/android/update-cm-3.6.8.1-signed.zip
108# There really should be a clone link always pointing to the latest
109#DEFAULTWEBUPDATE="http://n0rp.chemlab.org/android/latestupdate-signed.zip"
110DEFAULTWEBIMAGE=http://n0rp.chemlab.org/android/cm-recovery-1.4-signed.zip
111
112# Set up the default (for pensive at least) nameservers
113NAMESERVER1=64.46.128.3
114NAMESERVER2=64.46.128.4
115
116# WiFi works, rmnet0 setup ???
117# Do not know how to start the rmnet0 interface in recovery
118# If in normal mode "ifconfig rmnet0 down" kills rild too
119# /system/bin/rild& exits immediately, todo?
120
121
122DEVICEID=`cat /proc/cmdline | sed "s/.*serialno=//" | cut -d" " -f1`
123
124# This is the default repository for backups
125BACKUPPATH="/sdcard/nandroid/$DEVICEID"
126
127
128# Boot, Cache, Data, Ext2, Misc, Recovery, System, Splash1, Splash2
129# BACKUPLEGEND, If all the partitions are backed up it should be "CBDEMRS12"
130# Enables the user to figure at a glance what is in the backup
131BACKUPLEGEND=""
132
133# Normally we want tar to be verbose for confidence building.
134TARFLAGS="v"
135
136DEFAULTCOMPRESSOR=gzip
137DEFAULTEXT=.gz
138DEFAULTLEVEL="-1"
139
140ASSUMEDEFAULTUSERINPUT=0
141
142echo2log()
143{
144 if [ -e /cache/recovery/log ]; then
145 echo $1 >>/cache/recovery/log
146 else
147 echo $1 >/cache/recovery/log
148 fi
149}
150
151batteryAtLeast()
152{
153 REQUIREDLEVEL=$1
154 ENERGY=`cat /sys/class/power_supply/battery/capacity`
155 if [ "`cat /sys/class/power_supply/battery/status`" == "Charging" ]; then
156 ENERGY=100
157 fi
158 if [ ! $ENERGY -ge $REQUIREDLEVEL ]; then
159 $ECHO "Error: not enough battery power, need at least $REQUIREDLEVEL%."
160 $ECHO "Connect charger or USB power and try again"
161 exit 1
162 fi
163}
164
165if [ "`echo $0 | grep /sbin/nandroid-mobile.sh`" == "" ]; then
166 cp $0 /sbin
167 chmod 755 /sbin/`basename $0`
168 exec /sbin/`basename $0` $@
169fi
170
171
172# Hm, have to handle old options for the current UI
173case $1 in
174 restore)
175 shift
176 RESTORE=1
177 ;;
178 backup)
179 shift
180 BACKUP=1
181 ;;
182 --)
183 ;;
184esac
185
186ECHO=echo
187OUTPUT=""
188
189for option in $(getopt --name="nandroid-mobile v2.2.2" -l norecovery -l noboot -l nodata -l nosystem -l nocache -l nomisc -l nosplash1 -l nosplash2 -l subname: -l backup -l restore -l compress -l getupdate -l delete -l path -l webget: -l webgettarget: -l nameserver: -l nameserver2: -l bzip2: -l defaultinput -l autoreboot -l autoapplyupdate -l ext2 -l save: -l switchto: -l listbackup -l listupdate -l silent -l quiet -l help -- "cbruds:p:eql" "$@"); do
190 case $option in
191 --silent)
192 ECHO=echo2log
193 ASSUMEDEFAULTUSERINPUT=1
194 TARFLAGS=""
195 OUTPUT=>>/cache/recovery/log
196 shift
197 ;;
198 --quiet)
199 ECHO=echo2log
200 ASSUMEDEFAULTUSERINPUT=1
201 TARFLAGS=""
202 OUTPUT=>>/cache/recovery/log
203 shift
204 ;;
205 -q)
206 ECHO=echo2log
207 ASSUMEDEFAULTUSERINPUT=1
208 TARFLAGS=""
209 OUTPUT=>>/cache/recovery/log
210 shift
211 ;;
212 --help)
213 ECHO=echo
214 $ECHO "Usage: $0 {--backup|--restore|--getupdate|--delete|--compress|--bzip2:ARG|--webget:URL|--listbackup|--listupdate} [options]"
215 $ECHO ""
216 $ECHO "--help Display this help"
217 $ECHO ""
218 $ECHO "-s | --subname: SUBSTRING In case of --backup the SUBSTRING is"
219 $ECHO " the prefix used with backup name"
220 $ECHO " in case of --restore or -c|--compress|--bzip2 or"
221 $ECHO " --delete SUBSTRING specifies backups on which to"
222 $ECHO " operate"
223 $ECHO ""
224 $ECHO "-u | --getupdate Will search /sdcard/download for files named"
225 $ECHO " *update*.zip, will prompt the user"
226 $ECHO " to narrow the choice if more than one is found,"
227 $ECHO " and then move the latest, if not unique,"
228 $ECHO " to sdcard root /sdcard with the update.zip name"
229 $ECHO " It is assumed the browser was used to put the *.zip"
230 $ECHO " in the /sdcard/download folder. -p|--path option"
231 $ECHO " would override /sdcard/download with the path of your"
232 $ECHO " choosing."
233 $ECHO ""
234 $ECHO "-p | --path DIR Requires an ARGUMENT, which is the path to where "
235 $ECHO " the backups are stored, can be used"
236 $ECHO " when the default path /sdcard/nandroid/$DEVICEID "
237 $ECHO " needs to be changed"
238 $ECHO ""
239 $ECHO "-b | --backup Will store a full system backup on $BACKUPPATH"
240 $ECHO " One can suppress backup of any image however with options"
241 $ECHO " starting with --no[partionname]"
242 $ECHO ""
243 $ECHO "-e | --ext2 Preserve the contents of the ext2 partition along with"
244 $ECHO " the other partitions being backed up, to easily switch roms."
245 $ECHO ""
246 $ECHO "-r | --restore Will restore the last made backup which matches --subname"
247 $ECHO " ARGUMENT for boot, system, recovery and data"
248 $ECHO " unless suppressed by other options"
249 $ECHO " if no --subname is supplied and the user fails to"
250 $ECHO " provide any input then the latest backup is used"
251 $ECHO " When restoring compressed backups, the images will remain"
252 $ECHO " decompressed after the restore, you need to use -c|-compress"
253 $ECHO " or --bzip2 to compress the backup again"
254 $ECHO ""
255 $ECHO "-d | --delete Will remove backups whose names match --subname ARGUMENT"
256 $ECHO " Will allow to narrow down, will ask if the user is certain."
257 $ECHO " Removes one backup at a time, repeat to remove multiple backups"
258 $ECHO ""
259 $ECHO "-c | --compress Will operate on chosen backups to compress image files,"
260 $ECHO " resulting in saving of about 40MB out of 90+mb,"
261 $ECHO " i.e. up to 20 backups can be stored in 1 GIG, if this "
262 $ECHO " option is turned on with --backup, the resulting backup will"
263 $ECHO " be compressed, no effect if restoring since restore will"
264 $ECHO " automatically uncompress compressed images if space is available"
265 $ECHO ""
266 $ECHO "--bzip2: -# Turns on -c|--compress and uses bzip2 for compression instead"
267 $ECHO " of gzip, requires an ARG -[1-9] compression level"
268 $ECHO ""
269 $ECHO "--webget: URL|\"\" Requires an argument, a valid URL for an *update*.zip file"
270 $ECHO " If a null string is passed then the default URL is used"
271 $ECHO " Will also create an update.MD5sum file and update.name with the"
272 $ECHO " original file name."
273 $ECHO ""
274 $ECHO "--nameserver: IP addr Provide the first nameserver IP address, to override the default"
275 $ECHO ""
276 $ECHO "--nameserver2: IP addr Provide the second nameserver IP address, to override the default"
277 $ECHO ""
278 $ECHO "--webgettarget: DIR Target directory to deposit the fetched update, default is"
279 $ECHO " /sdcard"
280 $ECHO ""
281 $ECHO "--norecovery Will suppress restore/backup of the recovery partition"
282 $ECHO " If recovery.img was not part of the backup, no need to use this"
283 $ECHO " option as the nandroid will not attempt to restore it, same goes"
284 $ECHO " for all the options below"
285 $ECHO ""
286 $ECHO "--noboot Will suppress restore/backup of the boot partition"
287 $ECHO ""
288 $ECHO "--nodata Will suppress restore/backup of the data partition"
289 $ECHO ""
290 $ECHO "--nosystem Will suppress restore/backup of the system partition"
291 $ECHO ""
292 $ECHO "--nocache Will suppress restore/backup of the cache partition"
293 $ECHO ""
294 $ECHO "--nomisc Will suppress restore/backup of the misc partition"
295 $ECHO ""
296 $ECHO "--nosplash1 Will suppress restore/backup of the splash1 partition"
297 $ECHO ""
298 $ECHO "--nosplash2 Will suppress restore/backup of the splash2 partition"
299 $ECHO ""
300 $ECHO "--defaultinput Makes nandroid-mobile non-interactive, assumes default"
301 $ECHO " inputs from the user."
302 $ECHO ""
303 $ECHO "--autoreboot Automatically reboot the phone after a backup, especially"
304 $ECHO " useful when the compression options are on -c|--compress| "
305 $ECHO " --bzip2 -level since the compression op takes time and"
306 $ECHO " you may want to go to sleep or do something else, and"
307 $ECHO " when a backup is over you want the calls and mms coming"
308 $ECHO " through, right?"
309 $ECHO ""
310 $ECHO "--autoapplyupdate Once the specific update is downloaded or chosen from the"
311 $ECHO " sdcard, apply it immediately. This option is valid as a "
312 $ECHO " modifier for either --webget or --getupdate options."
313 $ECHO ""
314 $ECHO "-e|--ext2 Save the contents of ext2 partition in the backup folder too."
315 $ECHO " Enables to keep different sets of apps for different ROMS and"
316 $ECHO " switch easily between them."
317 $ECHO ""
318 $ECHO "--save: ROMTAG Preserve EVERYTHING under ROMTAG name, a composite option,"
319 $ECHO " it uses several other options."
320 $ECHO ""
321 $ECHO "--switchto: ROMTAG Restores your entire environment including app2sd apps, cache"
322 $ECHO " to the ROM environment named ROMTAG."
323 $ECHO ""
324 $ECHO "-q|--quiet|--silent Direct all the output to the recovery log, and assume default"
325 $ECHO " user inputs."
326 $ECHO ""
327 $ECHO "-l|--listbackup Will search the entire SDCARD for backup folders and will dump"
328 $ECHO " the list to stdout for use by the UI. Should be run with --silent"
329 $ECHO ""
330 $ECHO "--listupdate Will search the entire SDCARD for updates and will dump"
331 $ECHO " the list to stdout for use by the UI. Should be run with --silent"
332 exit 0
333 ;;
334 --norecovery)
335 NORECOVERY=1
336 #$ECHO "No recovery"
337 shift
338 ;;
339 --noboot)
340 NOBOOT=1
341 #$ECHO "No boot"
342 shift
343 ;;
344 --nodata)
345 NODATA=1
346 #$ECHO "No data"
347 shift
348 ;;
349 --nosystem)
350 NOSYSTEM=1
351 #$ECHO "No system"
352 shift
353 ;;
354 --nocache)
355 NOCACHE=1
356 #$ECHO "No cache"
357 shift
358 ;;
359 --nomisc)
360 NOMISC=1
361 #$ECHO "No misc"
362 shift
363 ;;
364 --nosplash1)
365 NOSPLASH1=1
366 #$ECHO "No splash1"
367 shift
368 ;;
369 --nosplash2)
370 NOSPLASH2=1
371 #$ECHO "No splash2"
372 shift
373 ;;
374 --backup)
375 BACKUP=1
376 #$ECHO "backup"
377 shift
378 ;;
379 -b)
380 BACKUP=1
381 #$ECHO "backup"
382 shift
383 ;;
384 -e)
385 EXT2=1
386 shift
387 ;;
388 --ext2)
389 EXT2=1
390 shift
391 ;;
392 --restore)
393 RESTORE=1
394 #$ECHO "restore"
395 shift
396 ;;
397 -r)
398 RESTORE=1
399 #$ECHO "restore"
400 shift
401 ;;
402 --compress)
403 COMPRESS=1
404 #$ECHO "Compress"
405 shift
406 ;;
407 -c)
408 COMPRESS=1
409 #$ECHO "Compress"
410 shift
411 ;;
412 --bzip2)
413 COMPRESS=1
414 DEFAULTCOMPRESSOR=bzip2
415 DEFAULTEXT=.bz2
416 if [ "$2" == "$option" ]; then
417 shift
418 fi
419 DEFAULTLEVEL="$2"
420 shift
421 ;;
422 --getupdate)
423 GETUPDATE=1
424 shift
425 ;;
426 -u)
427 GETUPDATE=1
428 shift
429 ;;
430 --subname)
431 if [ "$2" == "$option" ]; then
432 shift
433 fi
434 #$ECHO $2
435 SUBNAME="$2"
436 shift
437 ;;
438 -s)
439 if [ "$2" == "$option" ]; then
440 shift
441 fi
442 #$ECHO $2
443 SUBNAME="$2"
444 shift
445 ;;
446 --path)
447 if [ "$2" == "$option" ]; then
448 shift
449 fi
450 #$ECHO $2
451 BACKUPPATH="$2"
452 DEFAULTUPDATEPATH="$2"
453 shift 2
454 ;;
455 -p)
456 if [ "$2" == "$option" ]; then
457 shift
458 fi
459 #$ECHO $2
460 BACKUPPATH="$2"
461 shift 2
462 ;;
463 --delete)
464 DELETE=1
465 shift
466 ;;
467 -d)
468 DELETE=1
469 shift
470 ;;
471 --webgettarget)
472 if [ "$2" == "$option" ]; then
473 shift
474 fi
475 WEBGETTARGET="$2"
476 shift
477 ;;
478 --webget)
479 if [ "$2" == "$option" ]; then
480 shift
481 fi
482 #$ECHO "WEBGET"
483 # if the argument is "" stick with the default: /sdcard
484 if [ ! "$2" == "" ]; then
485 WEBGETSOURCE="$2"
486 fi
487 WEBGET=1
488 shift
489 ;;
490 --nameserver)
491 if [ "$2" == "$option" ]; then
492 shift
493 fi
494 NAMESERVER1="$2"
495 shift
496 ;;
497 --nameserver2)
498 if [ "$2" == "$option" ]; then
499 shift
500 fi
501 NAMESERVER2="$2"
502 shift
503 ;;
504 --defaultinput)
505 ASSUMEDEFAULTUSERINPUT=1
506 shift
507 ;;
508 --autoreboot)
509 AUTOREBOOT=1
510 shift
511 ;;
512 --autoapplyupdate)
513 AUTOAPPLY=1
514 shift
515 ;;
516 --save)
517 if [ "$2" == "$option" ]; then
518 shift
519 fi
520 SUBNAME="$2"
521 BACKUP=1
522 ASSUMEDEFAULTUSERINPUT=1
523 EXT2=1
524 COMPRESS=1
525 shift
526 ;;
527 --switchto)
528 if [ "$2" == "$option" ]; then
529 shift
530 fi
531 SUBNAME="$2"
532 RESTORE=1
533 ASSUMEDEFAULTUSERINPUT=1
534 EXT2=1
535 shift
536 ;;
537 -l)
538 shift
539 LISTBACKUP=1
540 ;;
541 --listbackup)
542 shift
543 LISTBACKUP=1
544 ;;
545 --listupdate)
546 shift
547 LISTUPDATE=1
548 ;;
549 --)
550 shift
551 break
552 ;;
553 esac
554done
555
556$ECHO ""
557$ECHO "nandroid-mobile v2.2.1"
558$ECHO ""
559
560let OPS=$BACKUP
561let OPS=$OPS+$RESTORE
562let OPS=$OPS+$DELETE
563let OPS=$OPS+$WEBGET
564let OPS=$OPS+$GETUPDATE
565let OPS=$OPS+$LISTBACKUP
566let OPS=$OPS+$LISTUPDATE
567
568if [ "$OPS" -ge 2 ]; then
569 ECHO=echo
570 $ECHO "--backup, --restore, --delete, --webget, --getupdate, --listbackup, --listupdate are mutually exclusive operations."
571 $ECHO "Please, choose one and only one option!"
572 $ECHO "Aborting."
573 exit 1
574fi
575
576let OPS=$OPS+$COMPRESS
577
578if [ "$OPS" -lt 1 ]; then
579 ECHO=echo
580 $ECHO "Usage: $0 {-b|--backup|-r|--restore|-d|--delete|-u|--getupdate|--webget|-c|--compress|--bzip2 -level|-l|--listbackup|--listupdate} [options]"
581 $ECHO "At least one operation must be defined, try $0 --help for more information."
582 exit 0
583fi
584
585
586if [ "$LISTBACKUP" == 1 ]; then
587 umount /sdcard 2>/dev/null
588 mount /sdcard 2>/dev/null
589 CHECK=`mount | grep /sdcard`
590 if [ "$CHECK" == "" ]; then
591 echo "Error: sdcard not mounted, aborting."
592 exit 1
593 else
594 find /sdcard | grep nandroid.md5 | sed s/.gz//g | sed s/.bz2//g | sed s/nandroid.md5//g
595 umount /sdcard 2>/dev/null
596 exit 0
597 fi
598fi
599
600if [ "$LISTUPDATE" == 1 ]; then
601 umount /sdcard 2>/dev/null
602 mount /sdcard 2>/dev/null
603 CHECK=`mount | grep /sdcard`
604 if [ "$CHECK" == "" ]; then
605 echo "Error: sdcard not mounted, aborting."
606 exit 1
607 else
608 find /sdcard | grep .zip
609 umount /sdcard 2>/dev/null
610 exit 0
611 fi
612fi
613
614# Make sure it exists
615touch /cache/recovery/log
616
617
618if [ "$AUTOAPPLY" == 1 -a "$WEBGET" == 0 -a "$GETUPDATE" == 0 ]; then
619 ECHO=echo
620 $ECHO "The --autoapplyupdate option is valid only in conjunction with --webget or --getupdate."
621 $ECHO "Aborting."
622 exit 1
623fi
624
625if [ "$COMPRESS" == 1 ]; then
626 $ECHO "Compressing with $DEFAULTCOMPRESSOR, level $DEFAULTLEVEL"
627fi
628
629if [ "$WEBGET" == 1 ]; then
630 $ECHO "Fetching $WEBGETSOURCE to target folder: $WEBGETTARGET"
631fi
632
633if [ ! "$SUBNAME" == "" ]; then
634 if [ "$BACKUP" == 1 ]; then
635 if [ "$COMPRESS" == 1 ]; then
636 $ECHO "Using $SUBNAME- prefix to create a compressed backup folder"
637 else
638 $ECHO "Using $SUBNAME- prefix to create a backup folder"
639 fi
640 else
641 if [ "$RESTORE" == 1 -o "$DELETE" == 1 -o "$COMPRESS" == 1 ]; then
642 $ECHO "Searching for backup directories, matching $SUBNAME, to delete or restore"
643 $ECHO "or compress"
644 $ECHO ""
645 fi
646 fi
647else
648 if [ "$BACKUP" == 1 ]; then
649 $ECHO "Using G1 keyboard, enter a prefix substring and then <CR>"
650 $ECHO -n "or just <CR> to accept default: "
651 if [ "$ASSUMEDEFAULTUSERINPUT" == 0 ]; then
652 read SUBNAME
653 else
654 $ECHO "Accepting default."
655 fi
656 $ECHO ""
657 if [ "$COMPRESS" == 1 ]; then
658 $ECHO "Using $SUBNAME- prefix to create a compressed backup folder"
659 else
660 $ECHO "Using $SUBNAME- prefix to create a backup folder"
661 fi
662 $ECHO ""
663 else
664 if [ "$RESTORE" == 1 -o "$DELETE" == 1 -o "$COMPRESS" == 1 ]; then
665 $ECHO "Using G1 keyboard, enter a directory name substring and then <CR>"
666 $ECHO -n "to find matches or just <CR> to accept default: "
667 if [ "$ASSUMEDEFAULTUSERINPUT" == 0 ]; then
668 read SUBNAME
669 else
670 $ECHO "Accepting default."
671 fi
672 $ECHO ""
673 $ECHO "Using $SUBNAME string to search for matching backup directories"
674 $ECHO ""
675 fi
676 fi
677fi
678
679if [ "$BACKUP" == 1 ]; then
680 mkyaffs2image=`which mkyaffs2image`
681 if [ "$mkyaffs2image" == "" ]; then
682 mkyaffs2image=`which mkyaffs2image-arm-uclibc`
683 if [ "$mkyaffs2image" == "" ]; then
684 $ECHO "error: mkyaffs2image or mkyaffs2image-arm-uclibc not found in path"
685 exit 1
686 fi
687 fi
688 dump_image=`which dump_image`
689 if [ "$dump_image" == "" ]; then
690 dump_image=`which dump_image-arm-uclibc`
691 if [ "$dump_image" == "" ]; then
692 $ECHO "error: dump_image or dump_image-arm-uclibc not found in path"
693 exit 1
694 fi
695 fi
696fi
697
698if [ "$RESTORE" == 1 ]; then
699 flash_image=`which flash_image`
700 if [ "$flash_image" == "" ]; then
701 flash_image=`which flash_image-arm-uclibc`
702 if [ "$flash_image" == "" ]; then
703 $ECHO "error: flash_image or flash_image-arm-uclibc not found in path"
704 exit 1
705 fi
706 fi
707 unyaffs=`which unyaffs`
708 if [ "$unyaffs" == "" ]; then
709 unyaffs=`which unyaffs-arm-uclibc`
710 if [ "$unyaffs" == "" ]; then
711 $ECHO "error: unyaffs or unyaffs-arm-uclibc not found in path"
712 exit 1
713 fi
714 fi
715fi
716if [ "$COMPRESS" == 1 ]; then
717 compressor=`busybox | grep $DEFAULTCOMPRESSOR`
718 if [ "$compressor" == "" ]; then
719 $ECHO "Warning: busybox/$DEFAULTCOMPRESSOR is not found"
720 $ECHO "No compression operations will be performed"
721 COMPRESS=0
722 else
723 $ECHO "Found $DEFAULTCOMPRESSOR, will compress the backup"
724 fi
725fi
726
727# 1
728DEVICEID=`cat /proc/cmdline | sed "s/.*serialno=//" | cut -d" " -f1`
729RECOVERY=`cat /proc/cmdline | grep "androidboot.mode=recovery"`
730if [ "$RECOVERY" == "foo" ]; then
731 $ECHO "Error: Must be in recovery mode, aborting"
732 exit 1
733fi
734if [ "$DEVICEID" == "foo" ]; then
735 $ECHO "Error: device id not found in /proc/cmdline, aborting"
736 exit 1
737fi
738if [ ! "`id -u 2>/dev/null`" == "0" ]; then
739 if [ "`whoami 2>&1 | grep 'uid 0'`" == "" ]; then
740 $ECHO "Error: must run as root, aborting"
741 exit 1
742 fi
743fi
744
745
746if [ "$RESTORE" == 1 ]; then
747 batteryAtLeast 30
748# ENERGY=`cat /sys/class/power_supply/battery/capacity`
749# if [ "`cat /sys/class/power_supply/battery/status`" == "Charging" ]; then
750# ENERGY=100
751# fi
752# if [ ! $ENERGY -ge 30 ]; then
753# $ECHO "Error: not enough battery power"
754# $ECHO "Connect charger or USB power and try again"
755# exit 1
756# fi
757
758
759 umount /sdcard 2>/dev/null
760 mount /sdcard 2>/dev/null
761 if [ "`mount | grep sdcard`" == "" ]; then
762 $ECHO "error: unable to mount /sdcard, aborting"
763 exit 1
764 fi
765
766 # find the latest backup, but show the user other options
767 $ECHO ""
768 $ECHO "Looking for the latest backup, will display other choices!"
769 $ECHO ""
770
771 RESTOREPATH=`ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null | tail -1`
772 $ECHO " "
773
774 if [ "$RESTOREPATH" = "" ];
775 then
776 $ECHO "Error: no backups found"
777 exit 2
778 else
779 $ECHO "Default backup is the latest: $RESTOREPATH"
780 $ECHO ""
781 $ECHO "Other available backups are: "
782 $ECHO ""
783 ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null | grep -v $RESTOREPATH $OUTPUT
784 $ECHO ""
785 $ECHO "Using G1 keyboard, enter a unique name substring to change it and <CR>"
786 $ECHO -n "or just <CR> to accept: "
787 if [ "$ASSUMEDEFAULTUSERINPUT" == 0 ]; then
788 read SUBSTRING
789 else
790 $ECHO "Accepting default."
791 SUBSTRING=""
792 fi
793 $ECHO ""
794
795 if [ ! "$SUBSTRING" == "" ]; then
796 RESTOREPATH=`ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null | grep $SUBSTRING | tail -1`
797 else
798 RESTOREPATH=`ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null | tail -1`
799 fi
800 if [ "$RESTOREPATH" = "" ]; then
801 $ECHO "Error: no matching backups found, aborting"
802 exit 2
803 fi
804 fi
805
806 $ECHO "Restore path: $RESTOREPATH"
807 $ECHO ""
808
809 umount /system /data 2>/dev/null
810 mount /system 2>/dev/null
811 mount /data 2>/dev/null
812 if [ "`mount | grep data`" == "" ]; then
813 $ECHO "error: unable to mount /data, aborting"
814 exit 1
815 fi
816 if [ "`mount | grep system`" == "" ]; then
817 $ECHO "error: unable to mount /system, aborting"
818 exit 1
819 fi
820
821 CWD=$PWD
822 cd $RESTOREPATH
823
824 DEFAULTEXT=""
825 if [ `ls *.bz2 2>/dev/null|wc -l` -ge 1 ]; then
826 DEFAULTCOMPRESSOR=bzip2
827 DEFAULTDECOMPRESSOR=bunzip2
828 DEFAULTEXT=.bz2
829 fi
830 if [ `ls *.gz 2>/dev/null|wc -l` -ge 1 ]; then
831 DEFAULTCOMPRESSOR=gzip
832 DEFAULTDECOMPRESSOR=gunzip
833 DEFAULTEXT=.gz
834 fi
835
836 if [ ! -f $RESTOREPATH/nandroid.md5$DEFAULTEXT ]; then
837 $ECHO "error: $RESTOREPATH/nandroid.md5 not found, cannot verify backup data"
838 exit 1
839 fi
840
841 if [ `ls *.bz2 2>/dev/null|wc -l` -ge 1 -o `ls *.gz 2>/dev/null|wc -l` -ge 1 ]; then
842 $ECHO "This backup is compressed with $DEFAULTCOMPRESSOR."
843
844 # Make sure that $DEFAULT[DE]COMPRESSOR exists
845 if [ `busybox | grep $DEFAULTCOMPRESSOR | wc -l` -le 0 -a\
846 `busybox | grep $DEFAULTDECOMPRESSOR | wc -l` -le 0 ]; then
847
848 $ECHO "You do not have either the $DEFAULTCOMPRESSOR or the $DEFAULTDECOMPRESSOR"
849 $ECHO "to unpack this backup, cleaning up and aborting!"
850 umount /system 2>/dev/null
851 umount /data 2>/dev/null
852 umount /sdcard 2>/dev/null
853 exit 1
854 fi
855 $ECHO "Checking free space /sdcard for the decompression operation."
856 FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`"
857 # we need about 100MB for gzip to uncompress the files
858 if [ $FREEBLOCKS -le 100000 ]; then
859 $ECHO "Error: not enough free space available on sdcard (need about 100mb)"
860 $ECHO "to perform restore from the compressed images, aborting."
861 umount /system 2>/dev/null
862 umount /data 2>/dev/null
863 umount /sdcard 2>/dev/null
864 exit 1
865 fi
866 $ECHO "Decompressing images, please wait...."
867 $ECHO ""
868 # Starting from the largest while we still have more space to reduce
869 # space requirements
870 $DEFAULTCOMPRESSOR -d `ls -S *$DEFAULTEXT`
871 $ECHO "Backup images decompressed"
872 $ECHO ""
873 fi
874
875 $ECHO "Verifying backup images..."
876 md5sum -c nandroid.md5
877 if [ $? -eq 1 ]; then
878 $ECHO "Error: md5sum mismatch, aborting"
879 exit 1
880 fi
881
882 if [ `ls boot* 2>/dev/null | wc -l` == 0 ]; then
883 NOBOOT=1
884 fi
885 if [ `ls recovery* 2>/dev/null | wc -l` == 0 ]; then
886 NORECOVERY=1
887 fi
888 if [ `ls data* 2>/dev/null | wc -l` == 0 ]; then
889 NODATA=1
890 fi
891 if [ `ls system* 2>/dev/null | wc -l` == 0 ]; then
892 NOSYSTEM=1
893 fi
894 # Amon_RA : If there's no ext backup set EXT2 to 0 so ext2 restore doesn't start
895 if [ `ls ext2* 2>/dev/null | wc -l` == 0 ]; then
896 EXT2=0
897 fi
898
899 for image in boot recovery; do
900 if [ "$NOBOOT" == "1" -a "$image" == "boot" ]; then
901 $ECHO ""
902 $ECHO "Not flashing boot image!"
903 $ECHO ""
904 continue
905 fi
906 if [ "$NORECOVERY" == "1" -a "$image" == "recovery" ]; then
907 $ECHO ""
908 $ECHO "Not flashing recovery image!"
909 $ECHO ""
910 continue
911 fi
912 $ECHO "Flashing $image..."
913 $flash_image $image $image.img $OUTPUT
914 done
915
916 for image in data system; do
917 if [ "$NODATA" == "1" -a "$image" == "data" ]; then
918 $ECHO ""
919 $ECHO "Not restoring data image!"
920 $ECHO ""
921 continue
922 fi
923 if [ "$NOSYSTEM" == "1" -a "$image" == "system" ]; then
924 $ECHO ""
925 $ECHO "Not restoring system image!"
926 $ECHO ""
927 continue
928 fi
929 $ECHO "Erasing /$image..."
930 cd /$image
931 rm -rf * 2>/dev/null
932 $ECHO "Unpacking $image image..."
933 $unyaffs $RESTOREPATH/$image.img $OUTPUT
934 cd /
935 sync
936 umount /$image
937 done
938
939 if [ "$EXT2" == 1 ]; then
940 # Amon_RA : Check if there's an ext partition before starting to restore
941 if [ -e /dev/block/mmcblk0p2 ]; then
942 $ECHO "Restoring the ext2 contents."
943 CWD=`pwd`
944 cd /
945
946 if [ `mount | grep /system | wc -l` == 0 ]; then
947 mount /system
948 else
949 mount -o rw,remount /system
950 fi
951
952 if [ `mount | grep /system/sd | wc -l` == 0 ]; then
953 mount /system/sd
954 fi
955
956 cd $CWD
957 CHECK=`mount | grep /system/sd`
958
959 if [ "$CHECK" == "" ]; then
960 $ECHO "Warning: --ext2 specified but unable to mount the ext2 partition."
961 $ECHO "Warning: your phone may be in an inconsistent state on reboot."
962 exit 1
963 else
964 CWD=`pwd`
965 cd /system/sd
966 # Depending on whether the ext2 backup is compressed we do either or.
967 if [ -e $RESTOREPATH/ext2.tar ]; then
968 rm -rf * 2>/dev/null
969 tar -x$TARFLAGS -f $RESTOREPATH/ext2.tar
970 else
971 if [ -e $RESTOREPATH/ext2.tgz ]; then
972 rm -rf * 2>/dev/null
973 tar -x$TARFLAGS -zf $RESTOREPATH/ext2.tgz
974 else
975 if [ -e $RESTOREPATH/ext2.tar.bz2 ]; then
976 rm -rf * 2>/dev/null
977 tar -x$TARFLAGS -jf $RESTOREPATH/ext2.tar.bz2
978 else
979 $ECHO "Warning: --ext2 specified but cannot find the ext2 backup."
980 $ECHO "Warning: your phone may be in an inconsistent state on reboot."
981 fi
982 fi
983 fi
984 cd $CWD
985 sync
986 umount /system/sd
987 umount /system
988
989 fi
990 else
991 # Amon_RA : Just display a warning
992 $ECHO "Warning: --ext2 specified but ext2 partition present on sdcard"
993 $ECHO "Warning: your phone may be in an inconsistent state on reboot."
994 fi
995 fi
996 $ECHO "Restore done"
997 exit 0
998fi
999
1000# 2.
1001if [ "$BACKUP" == 1 ]; then
1002
1003 if [ "$COMPRESS" == 1 ]; then
1004 ENERGY=`cat /sys/class/power_supply/battery/capacity`
1005 if [ "`cat /sys/class/power_supply/battery/status`" == "Charging" ]; then
1006 ENERGY=100
1007 fi
1008 if [ ! $ENERGY -ge 30 ]; then
1009 $ECHO "Warning: Not enough battery power to perform compression."
1010 COMPRESS=0
1011 $ECHO "Turning off compression option, you can compress the backup later"
1012 $ECHO "with the compression options."
1013 fi
1014 fi
1015
1016$ECHO "mounting system and data read-only, sdcard read-write"
1017umount /system 2>/dev/null
1018umount /data 2>/dev/null
1019umount /sdcard 2>/dev/null
1020mount -o ro /system || FAIL=1
1021mount -o ro /data || FAIL=2
1022mount /sdcard || mount /dev/block/mmcblk0 /sdcard || FAIL=3
1023case $FAIL in
1024 1) $ECHO "Error mounting system read-only"; umount /system /data /sdcard; exit 1;;
1025 2) $ECHO "Error mounting data read-only"; umount /system /data /sdcard; exit 1;;
1026 3) $ECHO "Error mounting sdcard read-write"; umount /system /data /sdcard; exit 1;;
1027esac
1028
1029if [ ! "$SUBNAME" == "" ]; then
1030 SUBNAME=$SUBNAME-
1031fi
1032
1033# Identify the backup with what partitions have been backed up
1034if [ "$NOBOOT" == 0 ]; then
1035 BACKUPLEGEND=$BACKUPLEGEND"B"
1036fi
1037if [ "$NOCACHE" == 0 ]; then
1038 BACKUPLEGEND=$BACKUPLEGEND"C"
1039fi
1040if [ "$NODATA" == 0 ]; then
1041 BACKUPLEGEND=$BACKUPLEGEND"D"
1042fi
1043if [ "$EXT2" == 1 ]; then
1044 BACKUPLEGEND=$BACKUPLEGEND"E"
1045fi
1046if [ "$NOMISC" == 0 ]; then
1047 BACKUPLEGEND=$BACKUPLEGEND"M"
1048fi
1049if [ "$NORECOVERY" == 0 ]; then
1050 BACKUPLEGEND=$BACKUPLEGEND"R"
1051fi
1052if [ "$NOSYSTEM" == 0 ]; then
1053 BACKUPLEGEND=$BACKUPLEGEND"S"
1054fi
1055
1056if [ ! -e /dev/mtd/mtd6ro ]; then
1057 NOSPLASH1=1
1058 NOSPLASH2=1
1059fi
1060
1061if [ "$NOSPLASH1" == 0 ]; then
1062 BACKUPLEGEND=$BACKUPLEGEND"1"
1063fi
1064if [ "$NOSPLASH2" == 0 ]; then
1065 BACKUPLEGEND=$BACKUPLEGEND"2"
1066fi
1067
1068if [ ! "$BACKUPLEGEND" == "" ]; then
1069 BACKUPLEGEND=$BACKUPLEGEND-
1070fi
1071
1072
1073TIMESTAMP="`date +%Y%m%d-%H%M`"
1074DESTDIR="$BACKUPPATH/$SUBNAME$BACKUPLEGEND$TIMESTAMP"
1075if [ ! -d $DESTDIR ]; then
1076 mkdir -p $DESTDIR
1077 if [ ! -d $DESTDIR ]; then
1078 $ECHO "error: cannot create $DESTDIR"
1079 umount /system 2>/dev/null
1080 umount /data 2>/dev/null
1081 umount /sdcard 2>/dev/null
1082 exit 1
1083 fi
1084else
1085 touch $DESTDIR/.nandroidwritable
1086 if [ ! -e $DESTDIR/.nandroidwritable ]; then
1087 $ECHO "error: cannot write to $DESTDIR"
1088 umount /system 2>/dev/null
1089 umount /data 2>/dev/null
1090 umount /sdcard 2>/dev/null
1091 exit 1
1092 fi
1093 rm $DESTDIR/.nandroidwritable
1094fi
1095
1096# 3.
1097$ECHO "checking free space on sdcard"
1098FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`"
1099# we need about 130MB for the dump
1100if [ $FREEBLOCKS -le 130000 ]; then
1101 $ECHO "Error: not enough free space available on sdcard (need 130mb), aborting."
1102 umount /system 2>/dev/null
1103 umount /data 2>/dev/null
1104 umount /sdcard 2>/dev/null
1105 exit 1
1106fi
1107
1108
1109
1110if [ -e /dev/mtd/mtd6ro ]; then
1111 if [ "$NOSPLASH1" == 0 ]; then
1112 $ECHO -n "Dumping splash1 from device over tcp to $DESTDIR/splash1.img..."
1113 dd if=/dev/mtd/mtd6ro of=$DESTDIR/splash1.img skip=19072 bs=2048 count=150 2>/dev/null
1114 $ECHO "done"
1115 sleep 1s
1116 else
1117 $ECHO "Dump of the splash1 image suppressed."
1118 fi
1119 if [ "$NOSPLASH2" == 0 ]; then
1120 $ECHO -n "Dumping splash2 from device over tcp to $DESTDIR/splash2.img..."
1121 dd if=/dev/mtd/mtd6ro of=$DESTDIR/splash2.img skip=19456 bs=2048 count=150 2>/dev/null
1122 $ECHO "done"
1123 else
1124 $ECHO "Dump of the splash2 image suppressed."
1125 fi
1126fi
1127
1128
1129# 5.
1130for image in boot recovery misc; do
1131
1132 case $image in
1133 boot)
1134 if [ "$NOBOOT" == 1 ]; then
1135 $ECHO "Dump of the boot partition suppressed."
1136 continue
1137 fi
1138 ;;
1139 recovery)
1140 if [ "$NORECOVERY" == 1 ]; then
1141 $ECHO "Dump of the recovery partition suppressed."
1142 continue
1143 fi
1144 ;;
1145 misc)
1146 if [ "$NOMISC" == 1 ]; then
1147 $ECHO "Dump of the misc partition suppressed."
1148 continue
1149 fi
1150 ;;
1151 esac
1152
1153 # 5a
1154 DEVICEMD5=`$dump_image $image - | md5sum | awk '{ print $1 }'`
1155 sleep 1s
1156 MD5RESULT=1
1157 # 5b
1158 $ECHO -n "Dumping $image to $DESTDIR/$image.img..."
1159 ATTEMPT=0
1160 while [ $MD5RESULT -eq 1 ]; do
1161 let ATTEMPT=$ATTEMPT+1
1162 # 5b1
1163 $dump_image $image $DESTDIR/$image.img $OUTPUT
1164 sync
1165 # 5b3
1166 echo "${DEVICEMD5} $DESTDIR/$image.img" | md5sum -c -s - $OUTPUT
1167 if [ $? -eq 1 ]; then
1168 true
1169 else
1170 MD5RESULT=0
1171 fi
1172 if [ "$ATTEMPT" == "5" ]; then
1173 $ECHO "Fatal error while trying to dump $image, aborting."
1174 umount /system
1175 umount /data
1176 umount /sdcard
1177 exit 1
1178 fi
1179 done
1180 $ECHO "done"
1181done
1182
1183# 6
1184for image in system data cache; do
1185 case $image in
1186 system)
1187 if [ "$NOSYSTEM" == 1 ]; then
1188 $ECHO "Dump of the system partition suppressed."
1189 continue
1190 fi
1191 ;;
1192 data)
1193 if [ "$NODATA" == 1 ]; then
1194 $ECHO "Dump of the data partition suppressed."
1195 continue
1196 fi
1197 ;;
1198 cache)
1199 if [ "$NOCACHE" == 1 ]; then
1200 $ECHO "Dump of the cache partition suppressed."
1201 continue
1202 fi
1203 ;;
1204 esac
1205
1206 # 6a
1207 $ECHO -n "Dumping $image to $DESTDIR/$image.img..."
1208 $mkyaffs2image /$image $DESTDIR/$image.img $OUTPUT
1209 sync
1210 $ECHO "done"
1211done
1212
1213# Backing up the ext2 partition, not really for the backup but to switch ROMS and apps at the same time.
1214
1215if [ "$EXT2" == 1 ]; then
1216 $ECHO "Storing the ext2(Apps, Dalvik-cache) contents in the backup folder."
1217
1218 CHECK1=`mount | grep /system`
1219 if [ "$CHECK1" == "" ]; then
1220 mount /system 2>/dev/null
1221 fi
1222 CHECK2=`mount | grep /system/sd`
1223 if [ "$CHECK2" == "" ]; then
1224 mount /system/sd 2>/dev/null
1225 fi
1226
1227 CHECK1=`mount | grep /system`
1228 CHECK2=`mount | grep /system/sd`
1229 if [ "$CHECK1" == "" -o "$CHECK2" == "" ]; then
1230 $ECHO "Warning: --ext2 specified but unable to mount the ext2 partition."
1231 exit 1
1232 else
1233
1234 CWD=`pwd`
1235 cd /system/sd
1236 # Depending on the whether we want it compressed we do either or.
1237 if [ "$COMPRESS" == 0 ]; then
1238 tar -cvf $DESTDIR/ext2.tar ./
1239 else
1240 if [ "$DEFAULTCOMPRESSOR" == "bzip2" ]; then
1241 tar -cvjf $DESTDIR/ext2.tar.bz2 ./
1242 else
1243 tar -cvzf $DESTDIR/ext2.tgz ./
1244 fi
1245 fi
1246 cd $CWD
1247 umount /system/sd
1248 fi
1249fi
1250
1251
1252# 7.
1253$ECHO -n "generating md5sum file..."
1254CWD=$PWD
1255cd $DESTDIR
1256md5sum *img > nandroid.md5
1257
1258# 7b.
1259if [ "$COMPRESS" == 1 ]; then
1260 $ECHO "Compressing the backup, may take a bit of time, please wait..."
1261 $ECHO "checking free space on sdcard for the compression operation."
1262 FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`"
1263 # we need about 70MB for the intermediate storage needs
1264 if [ $FREEBLOCKS -le 70000 ]; then
1265 $ECHO "error: not enough free space available on sdcard for compression operation (need 70mb)"
1266 $ECHO "leaving this backup uncompressed."
1267 else
1268 # we are already in $DESTDIR, start compression from the smallest files
1269 # to maximize space for the largest's compression, less likely to fail.
1270 # To decompress reverse the order.
1271 $DEFAULTCOMPRESSOR $DEFAULTLEVEL `ls -S -r * | grep -v ext2`
1272 fi
1273fi
1274
1275cd $CWD
1276$ECHO "done"
1277
1278# 8.
1279$ECHO "unmounting system, data and sdcard"
1280umount /system
1281umount /data
1282umount /sdcard
1283
1284# 9.
1285$ECHO "Backup successful."
1286if [ "$AUTOREBOOT" == 1 ]; then
1287 reboot
1288fi
1289exit 0
1290fi
1291
1292
1293# ----------------------------------GETTING UPDATES DIRECT FROM THE WEB USING WIFI-------------
1294
1295if [ "$WEBGET" == 1 ]; then
1296 $ECHO "mounting system and data read-only, sdcard read-write"
1297 umount /system 2>/dev/null
1298 umount /data 2>/dev/null
1299 umount /sdcard 2>/dev/null
1300
1301 # Since we are in recovery, these file-systems have to be mounted
1302 $ECHO "Mounting /system and /data for starting WiFi"
1303 mount -o ro /system || FAIL=1
1304 # Need to write to this system to setup nameservers for the wifi
1305 mount -o rw /data || FAIL=2
1306 mount /sdcard || mount /dev/block/mmcblk0 /sdcard || FAIL=3
1307
1308 case $FAIL in
1309 1) $ECHO "Error mounting system read-only"; umount /system /data /sdcard; exit 1;;
1310 2) $ECHO "Error mounting data read-write"; umount /system /data /sdcard; exit 1;;
1311 3) $ECHO "Error mounting sdcard read-write"; umount /system /data /sdcard; exit 1;;
1312 esac
1313
1314 if [ "$WEBGETSOURCE" == "" ]; then
1315 # Set the URL to the current latest update
1316 if [ "$ITSANUPDATE" == 1 ]; then
1317 WEBGETSOURCE=$DEFAULTWEBUPDATE
1318 else
1319 WEBGETSOURCE=$DEFAULTWEBIMAGE
1320 fi
1321 fi
1322
1323 if [ "$AUTOAPPLY" == 0 ]; then
1324 # Need to check space on sdcard only if we dump the update there.
1325 $ECHO "Checking free space on sdcard for the update download."
1326 FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`"
1327 # we need about 50MB for the storage needs
1328 if [ $FREEBLOCKS -le 50000 ]; then
1329 $ECHO "Error: not enough free space available on sdcard for the update operation (need 50mb)"
1330 $ECHO "Please free up space before invoking this option again."
1331 $ECHO "Cleaning up, unmounting file systems, aborting."
1332 umount /system /data /sdcard
1333 exit 1
1334 fi
1335 fi
1336
1337 if [ ! `basename $WEBGETSOURCE` == `basename $WEBGETSOURCE .zip` ]; then
1338 # It is a zip, not img.
1339 ITSANUPDATE=1
1340 else
1341 if [ ! `basename $WEBGETSOURCE` == `basename $WEBGETSOURCE .img` ]; then
1342 # It is an img file.
1343 ITSANIMAGE=1
1344 else
1345 # Unknown file type
1346 $ECHO "Unknown file type, cleaning up, aborting."
1347 umount /system /data /sdcard
1348 exit 1
1349 fi
1350 fi
1351
1352
1353 if [ "$ITSANUPDATE" == 1 -a "$AUTOAPPLY" == 0 ]; then
1354 # Move the previous update aside, if things go badly with the new update, it is good
1355 # to have the last one still around :-)
1356
1357 # If we cannot figure out what the file name used to be, create this new one with a time stamp
1358 OLDNAME="OLD-update-`date +%Y%m%d-%H%M`"
1359
1360 if [ -e $WEBGETTARGET/update.zip ]; then
1361 $ECHO "There is already an update.zip in $WEBGETTARGET, backing it up to"
1362 if [ -e $WEBGETTARGET/update.name ]; then
1363 OLDNAME=`cat $WEBGETTARGET/update.name`
1364 # Backup the name file (presumably contains the old name of the update.zip
1365 mv -f $WEBGETTARGET/update.name $WEBGETTARGET/`basename $OLDNAME .zip`.name
1366 fi
1367 $ECHO "`basename $OLDNAME .zip`.zip"
1368 mv -f $WEBGETTARGET/update.zip $WEBGETTARGET/`basename $OLDNAME .zip`.zip
1369
1370 # Backup the MD5sum file
1371 if [ -e $WEBGETTARGET/update.MD5sum ]; then
1372 mv -f $WEBGETTARGET/update.MD5sum $WEBGETTARGET/`basename $OLDNAME .zip`.MD5sum
1373 fi
1374 fi
1375 fi
1376
1377 $ECHO "Starting WiFI, please wait..."
1378 insmod /system/lib/modules/wlan.ko
1379
1380 wlan_loader -f /system/etc/wifi/Fw1251r1c.bin -e /proc/calibration -i /system/etc/wifi/tiwlan.ini
1381
1382 CWD=`pwd`
1383 cd /data/local/tmp
1384
1385 wpa_supplicant -f -Dtiwlan0 -itiwlan0 -c/data/misc/wifi/wpa_supplicant.conf&
1386
1387 sleep 5
1388 $ECHO "wpa_supplicant started"
1389 $ECHO ""
1390
1391 echo "nameserver $NAMESERVER1" >/etc/resolv.conf
1392 echo "nameserver $NAMESERVER2" >>/etc/resolv.conf
1393
1394 #We want the wifi to assign a dynamic address
1395 $ECHO "Starting DHCPCD server (dynamic address assignment)"
1396 # -BKL flags????
1397 dhcpcd -d tiwlan0 2>/dev/null &
1398
1399 # Have to wait for it to init stuff
1400 sleep 10
1401
1402
1403 CHECK1=`ps | grep -v grep | grep dhcpcd`
1404 CHECK2=`ps | grep -v grep | grep wpa_supplicant`
1405 if [ "$CHECK1" == "" -o "$CHECK2" == "" ]; then
1406 $ECHO "Error: wpa_supplicant or DHCPCD server is not running, cleaning up, aborting"
1407 rm -- -Dtiwlan0
1408 cd $CWD
1409
1410 $ECHO "unmounting /system, /data and /sdcard"
1411 umount /system
1412 umount /data
1413 umount /sdcard
1414 exit 2
1415 fi
1416
1417 $ECHO "DHCPCD server started"
1418 $ECHO ""
1419
1420 $ECHO "WiFi is running!"
1421 $ECHO ""
1422
1423 if [ "$AUTOAPPLY" == 1 ]; then
1424 $ECHO "Autoapply is on, retrieving the update into /cache/`basename $WEBGETSOURCE`"
1425
1426 wget -O /cache/`basename $WEBGETSOURCE` $WEBGETSOURCE $OUTPUT
1427
1428 if [ ! -e /cache/recovery ]; then
1429 mkdir /cache/recovery
1430 chmod 777 /cache/recovery
1431 fi
1432 if [ -e /cache/recovery/command ]; then
1433 echo "--update_package=CACHE:`basename $WEBGETSOURCE`" >>/cache/recovery/command
1434 else
1435 echo "--update_package=CACHE:`basename $WEBGETSOURCE`" >/cache/recovery/command
1436 fi
1437 chmod 555 /cache/recovery/command
1438 # Once rebooted the update will be applied.
1439
1440 else
1441
1442 if [ "$ITSANUPDATE" == 1 ]; then
1443 $ECHO "Retrieving system update into $WEBGETTARGET/update.zip, please wait..."
1444 wget -O $WEBGETTARGET/update.zip $WEBGETSOURCE $OUTPUT
1445
1446 echo "`basename $WEBGETSOURCE`" > $WEBGETTARGET/update.name
1447 $ECHO ""
1448 $ECHO "Update retrieved, if concerned, please compare the md5sum with the number"
1449 $ECHO "you see on the web page, if it is NOT the same, the retrieval"
1450 $ECHO "has failed and has to be repeated."
1451 $ECHO ""
1452 $ECHO `md5sum $WEBGETTARGET/update.zip | tee $WEBGETTARGET/update.MD5sum`
1453 $ECHO ""
1454 $ECHO "MD5sum has been stored in $WEBGETTARGET/update.MD5sum"
1455 else
1456 $ECHO "Retrieving the image into $WEBGETTARGET/`basename $WEBGETSOURCE`, please wait..."
1457 wget -O $WEBGETTARGET/`basename $WEBGETSOURCE` $WEBGETSOURCE $OUTPUT
1458 $ECHO ""
1459 $ECHO "$WEBGETSOURCE retrieved, if concerned, please compare the md5sum with the number"
1460 $ECHO "you see on the web page, if it is NOT the same, the retrieval"
1461 $ECHO "has failed and has to be repeated."
1462 $ECHO ""
1463 md5sum $WEBGETTARGET/`basename $WEBGETSOURCE` | tee $WEBGETTARGET/`basename $WEBGETSOURCE .img`.MD5sum $OUTPUT
1464 $ECHO ""
1465 $ECHO "MD5sum has been stored in $WEBGETTARGET/`basename $WEBGETSOURCE .img`.MD5sum"
1466 $ECHO ""
1467 $ECHO -n "Would you like to flash this image into boot or recovery? (or no for no flash) "
1468 read ANSWER
1469 if [ "$ANSWER" == "boot" ]; then
1470 $ECHO "Flashing $WEBGETTARGET/`basename $WEBGETSOURCE` into the boot partition."
1471 $flash_image boot $WEBGETTARGET/`basename $WEBGETSOURCE`
1472 else
1473 if [ "$ANSWER" == "recovery" ]; then
1474 $ECHO "Moving $WEBGETTARGET/`basename $WEBGETSOURCE` into the /data/recovery.img"
1475 $ECHO "and /system/recovery.img"
1476 cp -f $WEBGETTARGET/`basename $WEBGETSOURCE` /data/recovery.img
1477 mount -o rw,remount /system
1478 cp -f $WEBGETTARGET/`basename $WEBGETSOURCE` /system/recovery.img
1479 $ECHO "Depending on the settings of your specific ROM, the recovery.img will be"
1480 $ECHO "flashed at the normal bootup time either from /system or /data."
1481 else
1482 $ECHO "Not flashing the image."
1483 fi
1484 fi
1485 fi
1486 $ECHO ""
1487
1488 fi
1489
1490 $ECHO "Shutting down DHCPCD service and wpa_supplicant"
1491 killall -TERM dhcpcd
1492 TEMPVAR=`ps | grep -v grep | grep wpa_supplicant`
1493 TEMPVAR=`echo $TEMPVAR | cut -f 1 -d ' '`
1494 kill -TERM $TEMPVAR
1495
1496 while true; do
1497 CHECK=`ps | grep -v grep | grep dhcpcd`
1498 if [ ! "$CHECK" == "" ]; then
1499 sleep 1
1500 else
1501 break
1502 fi
1503 done
1504
1505 while true; do
1506 CHECK=`ps | grep -v grep | grep wpa_supplicant`
1507 if [ ! "$CHECK" == "" ]; then
1508 sleep 1
1509 else
1510 break
1511 fi
1512 done
1513 #sleep 5
1514
1515 $ECHO "Cleaning up..."
1516 # Looks like cannot clean up wlan module since chdir is missing
1517 #rmmod wlan
1518 rm -- -Dtiwlan0
1519 cd $CWD
1520
1521 $ECHO "unmounting /system, /data and /sdcard"
1522 umount /system
1523 umount /data
1524 umount /sdcard
1525
1526 if [ "$AUTOAPPLY" == 1 ]; then
1527 $ECHO "Auto apply update is on, rebooting into recovery to apply the update."
1528 $ECHO "When the update is complete reboot into the normal mode."
1529 $ECHO "The device will reboot and the update will be applied in 10 seconds!"
1530 sleep 10
1531 reboot recovery
1532 else
1533 if [ "$ITSANUPDATE" == 1 ]; then
1534 $ECHO "If you put the update into a folder other than /sdcard you need to use --getupdate to"
1535 $ECHO "prepare the update for application."
1536 $ECHO "You may want to execute 'reboot recovery' and choose update option to flash the update."
1537 $ECHO "Or in the alternative, shutdown your phone with reboot -p, and then press <CAMERA>+<POWER>"
1538 $ECHO "to initiate a normal system update procedure, if you have stock SPL."
1539 fi
1540 exit 0
1541 fi
1542fi
1543
1544# -------------------------------------DELETION, COMPRESSION OF BACKUPS---------------------------------
1545if [ "$COMPRESS" == 1 -o "$DELETE" == 1 ]; then
1546 $ECHO "Unmounting /system and /data to be on the safe side, mounting /sdcard read-write."
1547 umount /system 2>/dev/null
1548 umount /data 2>/dev/null
1549 umount /sdcard 2>/dev/null
1550
1551 FAIL=0
1552 # Since we are in recovery, these file-system have to be mounted
1553 $ECHO "Mounting /sdcard to look for backups."
1554 mount /sdcard || mount /dev/block/mmcblk0 /sdcard || FAIL=1
1555
1556 if [ "$FAIL" == 1 ]; then
1557 $ECHO "Error mounting /sdcard read-write, cleaning up..."; umount /system /data /sdcard; exit 1
1558 fi
1559
1560 $ECHO "The current size of /sdcard FAT32 filesystem is `du /sdcard | tail -1 | cut -f 1 -d '/'`Kb"
1561 $ECHO ""
1562
1563 # find the oldest backup, but show the user other options
1564 $ECHO "Looking for the oldest backup to delete, newest to compress,"
1565 $ECHO "will display all choices!"
1566 $ECHO ""
1567 $ECHO "Here are the backups you have picked within this repository $BACKUPPATH:"
1568
1569 if [ "$DELETE" == 1 ]; then
1570 RESTOREPATH=`ls -td $BACKUPPATH/*$SUBNAME* 2>/dev/null | tail -1`
1571 ls -td $BACKUPPATH/*$SUBNAME* 2>/dev/null $OUTPUT
1572 else
1573 RESTOREPATH=`ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null | tail -1`
1574 ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null $OUTPUT
1575 fi
1576 $ECHO " "
1577
1578 if [ "$RESTOREPATH" = "" ]; then
1579 $ECHO "Error: no backups found"
1580 exit 2
1581 else
1582 if [ "$DELETE" == 1 ]; then
1583 $ECHO "Default backup to delete is the oldest: $RESTOREPATH"
1584 $ECHO ""
1585 $ECHO "Other candidates for deletion are: "
1586 ls -td $BACKUPPATH/*$SUBNAME* 2>/dev/null | grep -v $RESTOREPATH $OUTPUT
1587 fi
1588 if [ "$COMPRESS" == 1 ]; then
1589 $ECHO "Default backup to compress is the latest: $RESTOREPATH"
1590 $ECHO ""
1591 $ECHO "Other candidates for compression are: "
1592 ls -trd $BACKUPPATH/*$SUBNAME* 2>/dev/null | grep -v $RESTOREPATH $OUTPUT
1593 fi
1594
1595 $ECHO ""
1596 $ECHO "Using G1 keyboard, enter a unique name substring to change it and <CR>"
1597 $ECHO -n "or just <CR> to accept: "
1598 if [ "$ASSUMEDEFAULTUSERINPUT" == 0 ]; then
1599 read SUBSTRING
1600 else
1601 $ECHO "Accepting default."
1602 SUBSTRING=""
1603 fi
1604
1605 if [ ! "$SUBSTRING" == "" ]; then
1606 RESTOREPATH=`ls -td $BACKUPPATH/*$SUBNAME* 2>/dev/null | grep $SUBSTRING | tail -1`
1607 else
1608 RESTOREPATH=`ls -td $BACKUPPATH/*$SUBNAME* 2>/dev/null | tail -1`
1609 fi
1610 if [ "$RESTOREPATH" = "" ]; then
1611 $ECHO "Error: no matching backup found, aborting"
1612 exit 2
1613 fi
1614 fi
1615
1616 if [ "$DELETE" == 1 ]; then
1617 $ECHO "Deletion path: $RESTOREPATH"
1618 $ECHO ""
1619 $ECHO "WARNING: Deletion of a backup is an IRREVERSIBLE action!!!"
1620 $ECHO -n "Are you absolutely sure? {yes | YES | Yes | no | NO | No}: "
1621 if [ "$ASSUMEDEFAULTUSERINPUT" == 0 ]; then
1622 read ANSWER
1623 else
1624 ANSWER=yes
1625 $ECHO "Accepting default."
1626 fi
1627 $ECHO ""
1628 if [ "$ANSWER" == "yes" -o "$ANSWER" == "YES" -o "$ANSWER" == "Yes" ]; then
1629 rm -rf $RESTOREPATH
1630 $ECHO ""
1631 $ECHO "$RESTOREPATH has been permanently removed from your SDCARD."
1632 $ECHO "Post deletion size of the /sdcard FAT32 filesystem is `du /sdcard | tail -1 | cut -f 1 -d '/'`Kb"
1633 else
1634 if [ "$ANSWER" == "no" -o "$ANSWER" == "NO" -o "$ANSWER" == "No" ]; then
1635 $ECHO "The chosen backup will NOT be removed."
1636 else
1637 $ECHO "Invalid answer: assuming NO."
1638 fi
1639 fi
1640 fi
1641
1642 if [ "$COMPRESS" == 1 ]; then
1643
1644 CWD=`pwd`
1645 cd $RESTOREPATH
1646
1647 if [ `ls *.bz2 2>/dev/null|wc -l` -ge 1 -o `ls *.gz 2>/dev/null|wc -l` -ge 1 ]; then
1648 $ECHO "This backup is already compressed, cleaning up, aborting..."
1649 cd $CWD
1650 umount /sdcard 2>/dev/null
1651 exit 0
1652 fi
1653
1654 $ECHO "checking free space on sdcard for the compression operation."
1655 FREEBLOCKS="`df -k /sdcard| grep sdcard | awk '{ print $4 }'`"
1656 # we need about 70MB for the intermediate storage needs
1657 if [ $FREEBLOCKS -le 70000 ]; then
1658 $ECHO "Error: not enough free space available on sdcard for compression operation (need 70mb)"
1659 $ECHO "leaving this backup uncompressed."
1660 else
1661 # we are already in $DESTDIR, start compression from the smallest files
1662 # to maximize space for the largest's compression, less likely to fail.
1663 # To decompress reverse the order.
1664 $ECHO "Pre compression size of the /sdcard FAT32 filesystem is `du /sdcard | tail -1 | cut -f 1 -d '/'`Kb"
1665 $ECHO ""
1666 $ECHO "Compressing the backup may take a bit of time, please wait..."
1667 $DEFAULTCOMPRESSOR $DEFAULTLEVEL `ls -S -r *`
1668 $ECHO ""
1669 $ECHO "Post compression size of the /sdcard FAT32 filesystem is `du /sdcard | tail -1 | cut -f 1 -d '/'`Kb"
1670 fi
1671 fi
1672
1673 $ECHO "Cleaning up."
1674 cd $CWD
1675 umount /sdcard 2>/dev/null
1676 exit 0
1677
1678fi
1679
1680if [ "$GETUPDATE" == 1 ]; then
1681 $ECHO "Unmounting /system and /data to be on the safe side, mounting /sdcard read-write."
1682 umount /system 2>/dev/null
1683 umount /data 2>/dev/null
1684 umount /sdcard 2>/dev/null
1685
1686 FAIL=0
1687 # Since we are in recovery, these file-system have to be mounted
1688 $ECHO "Mounting /sdcard to look for updates to flash."
1689 mount /sdcard || mount /dev/block/mmcblk0 /sdcard || FAIL=1
1690
1691 if [ "$FAIL" == 1 ]; then
1692 $ECHO "Error mounting /sdcard read-write, cleaning up..."; umount /system /data /sdcard; exit 1
1693 fi
1694
1695 $ECHO "The current size of /sdcard FAT32 filesystem is `du /sdcard | tail -1 | cut -f 1 -d '/'`Kb"
1696 $ECHO ""
1697
1698 # find all the files with update in them, but show the user other options
1699 $ECHO "Looking for all *update*.zip candidate files to flash."
1700 $ECHO ""
1701 $ECHO "Here are the updates limited by the subname $SUBNAME found"
1702 $ECHO "within the repository $DEFAULTUPDATEPATH:"
1703 $ECHO ""
1704 RESTOREPATH=`ls -trd $DEFAULTUPDATEPATH/*$SUBNAME*.zip 2>/dev/null | grep update | tail -1`
1705 if [ "$RESTOREPATH" == "" ]; then
1706 $ECHO "Error: found no matching updates, cleaning up, aborting..."
1707 umount /sdcard 2>/dev/null
1708 exit 2
1709 fi
1710 ls -trd $DEFAULTUPDATEPATH/*$SUBNAME*.zip 2>/dev/null | grep update $OUTPUT
1711 $ECHO ""
1712 $ECHO "The default update is the latest $RESTOREPATH"
1713 $ECHO ""
1714 $ECHO "Using G1 keyboard, enter a unique name substring to change it and <CR>"
1715 $ECHO -n "or just <CR> to accept: "
1716 if [ "$ASSUMEDEFAULTUSERINPUT" == 0 ]; then
1717 read SUBSTRING
1718 else
1719 $ECHO "Accepting default."
1720 SUBSTRING=""
1721 fi
1722 $ECHO ""
1723
1724 if [ ! "$SUBSTRING" == "" ]; then
1725 RESTOREPATH=`ls -trd $DEFAULTUPDATEPATH/*$SUBNAME*.zip 2>/dev/null | grep update | grep $SUBSTRING | tail -1`
1726 else
1727 RESTOREPATH=`ls -trd $DEFAULTUPDATEPATH/*$SUBNAME*.zip 2>/dev/null | grep update | tail -1`
1728 fi
1729 if [ "$RESTOREPATH" = "" ]; then
1730 $ECHO "Error: no matching backups found, aborting"
1731 exit 2
1732 fi
1733
1734 if [ "$RESTOREPATH" == "/sdcard/update.zip" ]; then
1735 $ECHO "You chose update.zip, it is ready for flashing, there nothing to do."
1736 else
1737
1738 # Things seem ok so far.
1739
1740 # Move the previous update aside, if things go badly with the new update, it is good
1741 # have the last one still around :-)
1742
1743 # If we cannot figure out what the file name used to be, create this new one with a time stamp
1744 OLDNAME="OLD-update-`date +%Y%m%d-%H%M`"
1745
1746 if [ -e /sdcard/update.zip ]; then
1747 $ECHO "There is already an update.zip in /sdcard, backing it up to"
1748 if [ -e /sdcard/update.name ]; then
1749 OLDNAME=`cat /sdcard/update.name`
1750 # Backup the name file (presumably contains the old name of the update.zip
1751 mv -f /sdcard/update.name /sdcard/`basename $OLDNAME .zip`.name
1752 fi
1753 $ECHO "`basename $OLDNAME .zip`.zip"
1754 mv -f /sdcard/update.zip /sdcard/`basename $OLDNAME .zip`.zip
1755
1756 # Backup the MD5sum file
1757 if [ -e /sdcard/update.MD5sum ]; then
1758 mv -f /sdcard/update.MD5sum /sdcard/`basename $OLDNAME .zip`.MD5sum
1759 fi
1760 fi
1761
1762 if [ -e $DEFAULTUPDATEPATH/`basename $RESTOREPATH .zip`.MD5sum ]; then
1763 mv -f $DEFAULTUPDATEPATH/`basename $RESTOREPATH .zip`.MD5sum /sdcard/update.MD5sum
1764 else
1765 $ECHO `md5sum $RESTOREPATH | tee /sdcard/update.MD5sum`
1766 $ECHO ""
1767 $ECHO "MD5sum has been stored in /sdcard/update.MD5sum"
1768 $ECHO ""
1769 fi
1770 if [ -e $DEFAULTUPDATEPATH/`basename $RESTOREPATH .zip`.name ]; then
1771 mv -f $DEFAULTUPDATEPATH/`basename $RESTOREPATH .zip`.name /sdcard/update.name
1772 else
1773 echo "`basename $RESTOREPATH`" > /sdcard/update.name
1774 fi
1775
1776 mv -i $RESTOREPATH /sdcard/update.zip
1777
1778
1779 $ECHO "Your file $RESTOREPATH has been moved to the root of sdcard, and is ready for flashing!!!"
1780
1781 fi
1782
1783 $ECHO "You may want to execute 'reboot recovery' and then choose the update option to flash the update."
1784 $ECHO "Or in the alternative, shutdown your phone with reboot -p, and then press <CAMERA>+<POWER> to"
1785 $ECHO "initiate a standard update procedure if you have stock SPL."
1786 $ECHO ""
1787 $ECHO "Cleaning up and exiting."
1788 umount /sdcard 2>/dev/null
1789 exit 0
1790fi