Guest User

nandroid-mobile.sh

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