Add working test for boot.img before patching
[auto-patcher.git] / auto_patcher
blobed5421b85699c021cbaf1ea907284a5f4c086629
1 #!/bin/bash
3 # version: beta v2.2.1, patches 2012.010.20.
5 # PDroid created by svyat. Official PDroid thread is
6 # http://forum.xda-developers.com/showthread.php?t=1357056
8 # The Auto-Patcher created by pastime1971 and mateorod
9 # Thread and support for the Auto-Patcher can be found at
10 # http://forum.xda-developers.com/showthread.php?p=27589651#post27589651
12 # ChangeLog
13 # 2012.05.00. (v0.1)
14 # ...........
15 # 2012.05.12. (v0.2) allows user to specify a different version of patches
16 # 2012.05.13. (v0.3.0) force "patch -N", remove temp upon failure, generate log output
17 # 2012.05.13. (v0.3.1) revised the generation of updater-script (SGS2)
18 # 2012.05.15. added AOSP patches
19 # 2012.05.15. added crespo-aosp patches, added device-specific patching if patch exists
20 # 2012.05.15. remove restore.zip upon failure
21 # 2012.05.15. (v0.4.1) added CM7 (GB) patches
22 # 2012.05.18. (v0.4.2) added checking for tool commands
23 # 2012.05.21. (v0.4.3) apply device-specific patches only when it is specified
24 # 2012.05.26. added task title to log
25 # 2012.05.31. (v0.4.4) CM9 patches updated, crespo-aosp added
26 # 2012.06.04. (v0.4.5) added checking tools and rom, added aroma installer support, add patch outputs reroute
27 # 2012.06.15. CM9 patches updated
28 # 2012.06.16. (v0.4.6) AOKP patches updated, patch versions restructured
29 # 2012.06.17. (v0.5.0) patcher script reorganized, removed build.prop
30 # 2012.06.17. (v0.6.0) auto patcher script for pdroid and v6 patches
31 # 2012.06.19. (v0.6.1) add patches for CM7.2 Final
32 # 2012.06.20. (v0.6.2) updated CM 7.2 patches
33 # 2012.06.23. (v0.6.3) fix symlinks
34 # 2012.06.26. (v0.6.4) added 0618 patches for cm7 nightlies; removed 0619 cm7.2 final
35 # 2012.06.27. (v0.6.5) added new CM9 patches (20120626)
36 # 2012.06.27. (v0.6.6) added CM7 nightly patches (20120625) (finally!)
37 # 2012.07.02. (v0.7.0) Redid AOSP/AOKP; add Windows support (using Cygwin). Thanks kobik77!
38 # 2012.07.06. (v0.7.1) updated CM9 patches (20120706)
39 # 2012.07.08. (v0.7.2) updated AOKP (20120707) and CM9 (20120708) pdroid patches
40 # 2012.07.11. (v1.0.0) reworked script works on Linux, Mac OSX, and Windows (on Cygwin);
41 # added new ICS/JellyBean support for the new V6-Supercharger
42 # 2012.07.18. (v1.0.1) new CM7/CM9 Pdroid patches (20120718)
43 # 2012.07.23. (v1.0.2) updated CM9 patches (20120723)
44 # 2012.07.23. (v1.0.3) updated CM9 patches (20120724)
45 # 2012.08.06. (v1.5.0) updated smali/baksmali binaries to 1.33
46 # 2012.08.06. Added Pdroid/V6supercharger support for CM10 Jellybean!
47 # 2012.08.06. Improved PDroid stability and efficiency (Thanks CollegeDev)
48 # 2012.08.06. (v1.6.0) added aokp-jb patches for pdroid/v6supercharger, script update
49 # 2012.08.17. (v1.9.5) Added support for pdroid_addon, by CollegeDev.
50 # Added insecure boot.img mod
51 # Added support for aosp-jb (This needs testing, and may not be
52 # a wide patch due to large variation between AOSP roms.
53 # -Please report all experiences!
54 # Updated all v6supercharger patches w/new edits to ProcessList
55 # Major script overhaul in preparation for additional mods!
56 # 2012.08.20. (1.9.6) Conditional support for Official cm10 and aokp-jb builds
57 # 2012.08.23. (1.9.7) Final/confirmed pdroid support for Official CM10 and AOKP-JB
58 # 2012.09.04 (1.9.8) Bundled cygwin-compatible tools (original was corrupt).
59 # For Cygwin, replaced h2b function with simple bash math
60 # conversion (req'd for compat w/ Cygwin tools).
61 # 2012.08.26. (2.0.0) Added .apk patching
62 # Added 3G Dongle support for Nexus 7
63 # TabletUI support for Nexus 7 (maybe more)
64 # Google Voice support for Nexus 7
65 # mods are 3gdongle, tabletUI, voice, or nexus7suite (for all)
66 # Added provisional file copy menu to widen patching base
68 # 2012.09.11. (2.0.1) Cygwin support fixes
69 # 2012.09.11. (2.0.2) Bugfix for 3gdongle (all)
70 # 2012.09.16. (2.1.0) Significant restructuring of core operations; added edit text file
71 # fix for "method-cap" errors in pdroid.
72 # 2012.09.20. (2.1.1) Fix for Phone FC's; OSX support finalized; ContentResolver resolved
73 # 2012.09.28. (2.1.2) Hotfix for failed pdroid patching. Logging improvements.
74 # 2012.10.07. (2.1.5) Final pdroid support for CM7.2 Official Releases
75 # Added aosp-mod patches for pdroid patching of AOSP kangs
76 # Switched to Chainfire's (f)aapt for speed/space reasons
77 # 2012.10.13. (2.1.6) New PDroid patches for aokp-jb and cm10 4.1.2
78 # Revert (f)aapt- temporary due to reported issues
79 # 2012.10.15. (2.17) New (again) CM10 pdroid patches.
80 # Move files to be copied into $p/$R/$B
81 # -This required a bunch of file moving-report broken symlinks.
82 # Some error check on the old RIL delete functions (thanks bundrik)
83 # 2012.10.16. (2.1.7.5) Fix FC on phone with CM10 pdroid patches
84 # 2012.10.19. (2.1.9) Testing CM10 support for PDroid Extension by CollegeDev.
85 # 2012.10.22. (2.2.0) aosp/aokp and cm10 support for pdroid extension by CollegeDev
86 # MAJOR reworking of patches and improved query. only 4 mod types:
87 # cm, aokp, aosp, and pa. See our OP for details.
88 # 2012.10.23. (2.2.0.1) Some fixes for the breakage due to update.
89 # Adjust aapt permissions and add fix_aapt.sh script.
90 # Fix for aosp probing and mod/jb determining. Test Update.
91 # 2012.10.23. (2.2.0.2) Add the '-h' menu back, bump version for log purposes.
92 # 2012.10.24. (2.2.0.3) Fixes for -h menu- Mms.apk as a copy file; aapt permission fix
93 # 2012.10.24. (2.2.0.4) Change Mms.apk to copy file for now, for non-phone devices.
94 # 2012.10.24. (2.2.0.5) Small fixes as we are almost to 2.1.1 stability (I hope)
95 # 2012.10.24. (2.2.1) Final merge of restructure. Fixes for OSX helper scripts
96 # Rename 'extension' to 'pd2.0'
98 # SHELL PROGRAMMING NOTES:
99 # [ -s FILE ]
100 # [[ -n STRING ]] or [[ -z STRING ]]
102 VERSION="Auto-Patcher v2.2.1"
104 echo ""
105 echo "The $VERSION"
106 echo ""
107 # by pastime1971 and mateorod
110 # pass variables to children
111 set -a
113 # function for converting hex to binary- for testing
114 #function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
116 printusage () {
117 echo " The $VERSION"
118 echo ""
119 echo "usage: $0 ROM.ZIP MODS [ROMTYPE] [PATCHDATE]"
120 echo ""
121 echo "MODS can be pdroid, extension, v6supercharger, tabletUI, etc..."
122 echo "recommended: [ROMTYPE] can be cm, aokp, aosp or pa"
123 echo "optional: [PATCHDATE] can be specified to use older patch versions."
124 echo "try \"$0 ROM.ZIP -h\" to see what is available"
125 echo ""
126 exit
129 debug () {
130 echo "! please refer to $LOG for any debugging"
131 echo ""
132 echo "! patching failed."
133 echo "! it may be possible to patch $ROM using other patch version"
134 echo ""
135 cleanup
136 printusage
139 cleanup () {
140 [ -d "$TEMP" ] && \rm -rf "$TEMP"
142 printerror () {
143 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
144 echo "!!! error: $@"
145 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
146 echo ""
147 debug
149 misspatch () {
150 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
151 echo "!! error: $@ "
152 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
153 echo ""
154 echo "!! The Auto-Patcher could not find its patches and/or tools!"
155 echo ""
156 echo "* Did you run ./batch.sh first?"
157 echo ""
158 echo "The patch_patches.tgz and patch_tools.tgz need to be in this directory,"
159 echo " one way or another..."
160 echo ""
161 cleanup
162 printusage
165 misstools () {
166 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
167 echo "Your set-up is missing a dependency."
168 echo "We require JDK, patch and cpio."
169 echo ""
170 echo ""
171 echo "Each distro is different"
172 echo "but you must ensure you have them all installed and try again."
173 echo ""
174 cleanup
175 printusage
178 printtask () {
179 echo "" >> "$LOG"
180 echo $@ | tee -a "$LOG"
183 copyfailed () {
184 echo ""
185 echo "File for which patching failed:"
186 echo "---------------------------------------------"
187 echo ""
188 echo "${FAIL##*/}"
189 echo ""
190 echo "---------------------------------------------"
191 echo ""
192 echo "We can try to add a premade copy of those files."
193 echo "This sometimes works."
194 echo ""
195 echo "But IT MAY NOT WORK!!!"
196 echo ""
197 echo ""
198 askconsent && return 0 || return 1
201 askconsent () {
202 sleep 1
203 echo -n "Do you want to proceed ? (YES/no) "
204 read PROCEED
205 echo "askconsent final = ${PROCEED}: for $FAIL" >> "$LOG"
206 case $PROCEED in
207 [Yy] | [Yy][Ee][Ss])
208 return 0; # successful:
211 return 1; # error
213 esac
216 abspath () {
217 case `uname -s` in
218 CYGWIN*)
219 echo $(cygpath -ua "$1")
221 Darwin)
222 #[[ $(echo $1 | awk '/^\//') == $1 ]] && echo "$1" || echo "$PWD/$1"
223 [[ ${1:0:1} == "/" ]] && echo "$1" || echo "$PWD/$1"
225 Linux)
226 echo $(readlink -f "$1")
229 if [[ ${1:0:1} == "/" ]]; then
230 echo "$1"
231 elif [[ ${1:0:2} == "./" ]]; then
232 echo "$PWD/${1:2}"
233 else
234 echo "$PWD/$1"
237 esac
240 extpath () {
241 case `uname -s` in
242 CYGWIN*)
243 echo $(cygpath -da "$1")
246 echo $(abspath "$1")
248 esac
251 #contains () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
253 parse () { echo "${1%%_*}"; }
255 getbuildprop () {
256 if [ -f system/build.prop ]; then
257 result=(`grep $1 system/build.prop | tr -d '\r' | tr '=' ' '`)
258 echo "${result[1]}"
259 else
260 echo ""
265 aosp_mod_check() {
266 jar -xvf "$ROMX" system/framework/framework2.jar >> $LOG
267 if [ -s system/framework/framework2.jar ]; then
268 AOSP=aosp-mod
269 else
270 AOSP=aosp-jb
272 echo $AOSP
275 proberom () {
276 VID=$(getbuildprop ro.build.version.release)
278 if [ "${VID%.*}" == "2.3" ]; then
279 ANDR=gingerbread
280 RID=cm;
281 elif [ "${VID%.*}" == "4.0" ]; then
282 ANDR=ics
283 MID=$(getbuildprop ro.rommanager.developerid)
284 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.modversion))
285 DID=$(getbuildprop ro.product.device)
286 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
288 case "$MID" in
289 pa|cm9|cm|cyanogenmod*)
290 RID=cm;
292 aokp|teamkang)
293 RID=aokp;
296 RID=aosp;
297 echo "!!! $MID is not supported yet, trying generic latest !!!"
299 esac
300 elif [ "${VID%.*}" == "4.1" ]; then
301 ANDR=jellybean
302 MID=$(getbuildprop ro.build.display.id)
303 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
304 DID=$(getbuildprop ro.product.device)
305 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
307 case "$MID" in
308 cm10|cm*|cyanogenmod*)
309 RID=cm;
311 aokp*|teamkang)
312 RID=aokp;
314 pa*)
315 RID=pa;
318 RID=`aosp_mod_check`;
320 esac
321 else
322 printerror "unknown ROM version $VID"
324 echo "... result of ROM check: version= $ANDR rom= $RID ..."
327 # NEW # Advanced build date query
328 # This should set the patch date automatically.
330 buildquery() {
331 UTC=$(getbuildprop ro.build.date.utc)
332 case `uname -s` in
333 [Dd]arwin)
334 BDID=`date -r $UTC +%Y%m%d`
335 echo "#### Darwin Build Query used ####" >> "$LOG"
338 BDID=`date -d "1970-01-01 UTC $UTC seconds" +"%Y%m%d"`
339 echo "#### Standard Build Query ####" >> "$LOG"
341 esac
343 if [ $# -gt 3 ]; then
344 BID=${4//,/ };
345 elif
346 [[ ${#PID[@]} -gt 1 ]]; then
347 BID=latest;
348 else
349 for P in ${PID[@]}; do
350 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
351 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
352 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
353 PAR=patches/$P/$A/$R
354 PARB=patches/$P/$A/$R/$B
355 DATES=`ls patches/$P/$A/$R/ | grep '201*'`
356 # attempt to set builds before extant patches to earliest
357 for i in $DATES; do
358 if [[ $i -lt $BID ]]; then
359 BID=$i
361 if [[ $(($BID-$BDID)) -lt 0 ]]; then
362 BID=$i
363 else
364 for g in $DATES; do
365 if [[ $(($BDID-$g)) -eq 0 ]]; then
366 BID=$g
367 break
368 elif [[ $(($BDID-$g)) -gt 0 ]] && [[ $g -gt $BID ]]; then
369 BID=$g
371 done
373 done
374 echo "... $PID patch date has been set to: $BID..."
375 B=$BID
376 done
380 proberil () {
381 RIL=$(getbuildprop ro.telephony.ril_class)
382 [[ -n $RIL ]] && printtask "... result of ROM check: RIL= $RIL"
385 garbage () {
386 unset GARBAGE
387 GARBAGE=($(find $@ -name "*.orig")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
388 GARBAGE=($(find $@ -name "*.rej")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
391 rominfo () {
392 echo "# VER= $VERSION" >> "$LOG"
393 echo "# OS= $OSTYPE ${BIT}bit" >> "$LOG"
394 echo "# VID= $VID" >> "$LOG"
395 echo "# PID= ${PID[@]}" >> "$LOG"
396 echo "# MID= $MID" >> "$LOG"
397 echo "# RID= $RID" >> "$LOG"
398 echo "# ANDR= $ANDR" >> "$LOG"
399 echo "# BDID= $BDID" >> "$LOG"
400 echo "# DID= $DID" >> "$LOG"
401 echo "# BID= $BID" >> "$LOG"
405 [[ $# -lt 1 || $1 == "-h" || -z $1 ]] && printusage
407 ROOT=$(abspath `dirname "$0"`)
408 TOOL="$ROOT/patch_tools.tgz"
409 PTCH="$ROOT/patch_patches.tgz"
410 ROM=$(abspath "$1")
411 ROMX=$(extpath "$1")
413 if [[ $OSTYPE == "cygwin" ]]; then
414 EXE=".exe"
415 PGFORM="0x"
418 DATE=`date +%Y%m%d%H%M%S`
419 TEMP=$(abspath ./tmp$DATE)
420 LOG=$(abspath ./log$DATE.txt)
421 #echo $ROOT $TOOL $ROM $TEMP "$LOG"
422 echo "ROOT= $ROOT" >> "$LOG"
423 echo "TOOL= $TOOL" >> "$LOG"
424 echo "ROM= $ROM" >> "$LOG"
425 echo "ROMX= $ROMX" >> "$LOG"
427 \rm -f restore.zip update.zip
429 CHK=$(getconf LONG_BIT)
430 if [[ $CHK = "64" ]]; then
431 BIT=64
432 else [[ $BIT = "32" ]];
433 BIT=32
437 printtask "### verify tools ###"
438 which patch >> "$LOG" || misstools "Missing \"patch diff command\""
439 which tar >> "$LOG" || misstools "Missing \"tar archive command\""
440 which jar >> "$LOG" || misstools "Missing \"jar (JDK)\""
441 which java >> "$LOG" || misstools "Missing \"java (JRE)\""
442 which cpio >> "$LOG" || misstools "Missing \"cpio archive command\""
443 [ -f "$TOOL" ] || misspatch "patch tool is missing"
444 [ -f "$PTCH" ] || misspatch "patch patches are missing"
445 printtask "### verify ROM ###"
446 [ -f "$ROM" ] || printerror "Cannot find $ROM"
447 echo "### running $0 $@" >> "$LOG"
448 mkdir "$TEMP"
449 cd "$TEMP"
451 PATH="$TEMP:$PATH"
452 export PATH
453 echo "PATH= $PATH" >> "$LOG"
455 printtask "### unzip patch tools ###"
456 tar xvzf "$TOOL" > /dev/null || printerror "failed extracting tools !!!"
458 # Get OS specific tools
459 case `uname -s` in
460 CYGWIN*)
461 \mv cygwin/aapt.exe .
462 \mv cygwin/*.exe boot/
463 \rm -rf cygwin # why? If you want to we can keep/add it to mac/linux, though.
465 [Dd]arwin)
466 \mv mac/aapt .
468 [Ll]inux)
469 \mv linux/aapt .
472 \mv linux/aapt .
474 esac
475 # Ensure aapt permissions - possibly to one day be expanded to all tools in a function.
476 chmod a+x aapt$EXE
478 printtask "### unzip patch patches ###"
479 tar xvzf "$PTCH" > /dev/null || printerror "failed extracting patches !!!"
481 # we need build.prop for both RIL probing and ROM probing
482 jar -xvf "$ROMX" system/build.prop 2>> "$LOG" || printerror "failed extracting build.prop from $ROMX !!!"
484 # rom + ril type
485 proberil
486 printtask "### probe rom type ###"
487 proberom
489 # rom type
490 if [ $# -gt 2 ]; then
491 RID=$3
492 case "$RID" in
493 cm10|cm*|cyanogenmod*)
495 aokp*|teamkang)
496 RID=aokp;
498 pa*)
499 RID=pa;
502 RID=`aosp_mod_check`;
504 esac
505 echo "... Using $RID patches ..."
506 else
507 echo "... Using results of ROM check ..."
510 # patch type
511 if [ $# -gt 1 ]; then
512 PIL=${2//,/ }
513 else
514 #PIL=`ls patches/`
515 PIL="pdroid v6supercharger"
516 echo "No mods selected. Defaulting to the v6 and Pdroid, just because we can."
518 # a hack to make sure pdroid is the first mod
519 [[ ${PIL[@]} =~ pdroid ]] && PID=(pdroid)
520 for P in ${PIL[@]}; do
521 [[ ${PID[@]} =~ $P ]] || PID=(${PID[@]} $P)
522 done
524 # Build date
525 if [ $# -gt 3 ]; then
526 BID=$4
527 echo "... $PID patch date set manually to $BID ..."
528 else
529 buildquery
532 # echo ROM info
533 rominfo
535 if [[ ( "$PID" == "-h" || "$RID" == "-h" || "$BID" == "-h" ) ]]; then
536 case `uname -s` in
537 [Dd]arwin)
538 LIST="find . -name '*.sh' 'common' 'provisionals' '*.txt' '*.awk' -prune"
541 LIST="ls --ignore="common" --ignore="*.sh" --ignore "provisionals" --ignore="*.txt" --ignore "*.awk""
543 esac
544 echo "### supported patch candidates ###"
545 MODS=$(ls --ignore "extension" patches)
546 #echo ${MODS[@]}
548 for M in ${MODS[@]}; do
549 AND=$($LIST patches/$M)
550 #echo $M: ${AND[@]}
551 for A in ${AND[@]}; do
552 RID=$($LIST patches/$M/$A)
553 #echo $M: $A: ${RID[@]}
554 for R in ${RID[@]}; do
555 BID=$($LIST -v patches/$M/$A/$R)
556 echo $M: $A: $R: ${BID[@]}
557 done
558 done
559 done
560 echo ""
561 echo "*****************************************************************"
562 echo ""
563 echo "Note: There are ONLY 4 ROMTYPES: [pa, cm, aokp, and aosp]."
564 echo "The Auto-Patcher will automatically slot if you need aosp-jb or aosp-mod"
565 echo ""
567 else
568 for P in ${PID[@]}; do
569 [ -d patches/$P ] || printerror "$P is not available"
570 [ -d patches/$P/$ANDR ] && R=$ANDR || A=all
571 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
572 #[ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
574 unset FILES
575 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
576 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
577 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
578 for FILE in ${FILES[@]}; do
579 case "$FILE" in
580 +system/*)
581 [[ ${CID[@]} =~ ${FILE:1} ]] && printerror "${FILE:1} is already being copied" || CID=(${CID[@]} ${FILE:1})
583 -system/*)
584 [[ ${EID[@]} =~ ${FILE:1} ]] || EID=(${EID[@]} ${FILE:1})
586 system/*.jar)
587 [[ ${JID[@]} =~ $FILE ]] || JID=(${JID[@]} $FILE)
589 system/*.apk)
590 [[ ${AID[@]} =~ $FILE ]] || AID=(${AID[@]} $FILE)
592 *.img)
593 [[ ${IID[@]} =~ $FILE ]] || IID=(${IID[@]} $FILE)
595 system/*)
596 [[ ${TID[@]} =~ $FILE ]] || TID=(${TID[@]} $FILE)
598 esac
599 done
600 done
601 echo "### DELETE FILES= ${EID[@]}" >> "$LOG"
602 echo "### COPY FILES= ${CID[@]}" >> "$LOG"
603 echo "### JAR FILES= ${JID[@]}" >> "$LOG"
604 echo "### APK FILES= ${AID[@]}" >> "$LOG"
605 echo "### IMG FILES= ${IID[@]}" >> "$LOG"
606 echo "### TXT FILES= ${TID[@]}" >> "$LOG"
608 printtask "### unzip rom ###"
609 # extract system files
610 jar -xvf "$ROMX" ${JID[@]} ${AID[@]} ${IID[@]} ${TID[@]} 2>> "$LOG" || printerror "failed extracting ${JID[@]} ${AID[@]} ${IID[@]} from $ROMX !!!"
611 #\cp -a system/ backup # only for debugging purpose
613 printtask "### create updater script ###"
614 jar -xvf "$ROMX" META-INF/com/google/android/updater-script META-INF/com/google/android/update-binary
615 jar -xvf "$ROMX" META-INF/com/google/android/update-binary-installer 1> /dev/null 2> /dev/null
616 if [ -f META-INF/com/google/android/update-binary-installer ]; then
617 printtask "... using Aroma installer ..."
618 \mv META-INF/com/google/android/update-binary-installer META-INF/com/google/android/update-binary
619 else
620 printtask "... using Clockworkmod installer ..."
622 if [ -f META-INF/com/google/android/update-binary ]; then
623 #echo "# original updater-script" >> "$LOG"
624 #cat META-INF/com/google/android/updater-script >> "$LOG"
626 cat META-INF/com/google/android/updater-script | grep ^mount | grep "/system" | head -n 1 > updater-script
627 [ -s updater-script ] || echo "run_program(\"/sbin/busybox\", \"mount\", \"/system\");" > updater-script
629 for FILE in ${EID[@]}; do
630 # restore the original
631 jar -xvf "$ROMX" $FILE 2> /dev/null || printtask "!!! warning $FILE cannot be restored !!!"
632 done
633 for FILE in ${CID[@]}; do
634 # replace with the original or delete it
635 jar -xvf "$ROMX" $FILE 2> /dev/null || echo "delete(\"/$FILE\");" >> updater-script
636 done
638 echo "package_extract_dir(\"system\", \"/system\");" >> updater-script
639 # add permission management
640 cat META-INF/com/google/android/updater-script | grep set_perm | grep "/system" >> updater-script
641 # TODO: remove if we don't need it any more
642 for P in ${PID[@]}; do
643 if [ -s patches/$P/$A/set_perm.txt ]; then
644 echo "...adding permissions..."
645 cat patches/$P/$A/set_perm.txt >> updater-script
647 done
649 cat META-INF/com/google/android/updater-script | grep ^unmount | grep "/system" | head -n 1 >> updater-script
651 [[ ${IID[@]} =~ boot.img ]] && cat META-INF/com/google/android/updater-script | grep "boot.img" >> updater-script
653 cat updater-script > META-INF/com/google/android/updater-script
655 echo "# generated updater-script" >> "$LOG"
656 cat updater-script >> "$LOG"
657 else
658 # this is not enough any more
659 jar -xvf update-template.zip META-INF/com/google/android/update-binary META-INF/com/google/android/updater-script
662 printtask "### create restore.zip ###"
663 jar -cfv tmp.zip ${JID[@]} ${AID[@]} ${IID[@]} META-INF > /dev/null 2>> "$LOG"
664 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip signed.zip 2>> "$LOG"
665 \mv signed.zip restore.zip
666 \rm -f tmp.zip
668 printtask "### decompile system files ###"
670 # decompile system files
671 for FILE in ${JID[@]}; do
672 JAR=${FILE##*/}
673 printtask "... decompile $JAR ..."
674 java -jar baksmali.jar -b -a 15 -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
675 if [ $FILE == "system/framework/framework.jar" ]; then
676 printtask "... extract preloaded-classes ..."
677 jar -xvf ${FILE} preloaded-classes >> "$LOG" 2>> "$LOG"
678 \mv preloaded-classes tmp/${FILE}.out/
680 done
681 for FILE in ${AID[@]}; do
682 APK=${FILE##*/}
683 printtask "... decompile $APK ..."
684 if [ $FILE == "system/framework/framework-res.apk" ]; then
685 printtask "... installing framework files ..."
686 java -jar apktool.jar if $FILE
688 java -jar apktool.jar d -f $FILE tmp/$FILE.out
690 done
691 for FILE in ${IID[@]}; do
692 printtask "### unpacking $FILE ###"
693 case "$FILE" in
694 boot.img)
695 if [ -f boot.img ]; then
696 cd boot
697 ./unpackbootimg$EXE -i ../boot.img >> "$LOG" 2>> "$LOG"
698 mkdir ramdisk
699 cd ramdisk
700 gzip -dc ../boot.img-ramdisk.gz | cpio -i >> "$LOG" 2>> "$LOG"
701 cd ../../
702 else
703 printerror "A mod needs to patch boot.img! This rom does not contains boot.img."
707 printerror "cannot process $FILE"
709 esac
710 done
713 # apply system modification
715 for P in ${PID[@]}; do
716 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
717 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
718 [ -d patches/$P/$A/$R/$BID ] && B=$BID || B=latest
719 PAR=patches/$P/$A/$R
720 PARB=patches/$P/$A/$R/$B
722 unset FILES COPY DELS JARS APKS IMGS TXTS
723 [ -f patches/$P/$A/files.txt ] && FILES=$(cat patches/$P/$A/files.txt)
724 [ -f patches/$P/$A/$R/files.txt ] && FILES=$(cat patches/$P/$A/$R/files.txt)
725 for FILE in ${FILES[@]}; do
726 case $FILE in
727 +system/*)
728 COPY=(${COPY[@]} ${FILE:1})
730 -system/*)
731 DELS=(${DELS[@]} ${FILE:1})
733 system/*.jar)
734 JARS=(${JARS[@]} $FILE)
736 system/*.apk)
737 APKS=(${APKS[@]} $FILE)
739 *.img)
740 IMGS=(${IMGS[@]} $FILE)
742 system/*)
743 TXTS=(${TXTS[@]} $FILE)
745 esac
746 done
748 [[ -n ${COPY[@]} ]] && printtask "### copy patch $P/$A/$R/$B ###"
749 for FILE in ${COPY[@]}; do
750 printtask "... copying $FILE ..."
751 DEST=${FILE%/*}
752 mkdir -p ./$DEST
753 if [ -e patches/$P/$A/$R/$B/$FILE ]; then
754 [ -e $FILE ] && printtask "... overwriting $FILE from $P ..."
755 \cp -av patches/$P/$A/$R/$B/$FILE ./$DEST >> "$LOG"
756 else
757 printerror "$FILE is missing in the patch"
759 done
761 # unconditional smali copying
762 [ -f patches/$P/smali.txt ] && SMALI=$(cat patches/$P/smali.txt)
763 [ -f patches/$P/$A/smali.txt ] && SMALI=$(cat patches/$P/$A/smali.txt)
764 [ -f patches/$P/$A/$R/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/smali.txt)
765 [ -f patches/$P/$A/$R/$B/smali.txt ] && SMALI=$(cat patches/$P/$A/$R/$B/smali.txt)
766 MOVE=${SMALI##*/}
767 for SMALI in ${SMALI[@]}; do
768 printtask "... overwriting $MOVE ..."
769 \cp -av patches/$P/$ANDR/provisionals/$SMALI tmp/$SMALI >> "$LOG"
770 done
773 [[ -n ${DELS[@]} ]] && printtask "### delete patch $P/$A/$R/$B ###"
774 for FILE in ${DELS[@]}; do
775 printtask "... deleting $FILE ..."
776 \rm -rf ./$FILE
777 done
779 [[ -n ${JARS[@]} ]] && printtask "### jar patch $P/$A/$R/$B ###"
780 for FILE in ${JARS[@]}; do
781 JAR=${FILE##*/}
782 NAME=${JAR%.jar}
783 printtask "... apply $NAME.patch ..."
784 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/${NAME}.patch 2>&1 > ${JAR}.log
785 cat ${JAR}.log | tee -a "$LOG"
786 case "$NAME" in
787 framework)
788 # patch preloaded-classes only if a patch exists
789 if [ -f $PARB/preloaded.patch ]; then
790 printtask "... apply preloaded.patch ..."
791 patch -Nls -p1 -d tmp/${FILE}.out < $PARB/preloaded.patch 2>&1 > preloaded.log
792 cat preloaded.log | tee -a "$LOG"
793 [[ `grep FAILED preloaded.log` != "" ]] && printerror "failed preloaded.patch"
797 echo -n "" # do nothing else
799 esac
800 # substitute failed smali with premade jar
801 unset FAILS
802 FAILS=(`grep FAILED ${JAR}.log | sed -e 's/.*smali\///' | sed -e 's/\.rej//'`)
803 if [[ -n ${FAILS[@]} ]]; then
804 printtask "!!! PATCHING FAILED FOR ${FAILS[@]} !!!"
805 echo "### replace ${FAILS[@]} ###" > ${JAR}.log
806 for FAIL in ${FAILS[@]}; do
807 echo "!!! patch error for $FAIL !!!" >> "$LOG"
808 #cat tmp/$FILE.out/smali/${FAIL}.rej >> "$LOG"
809 [[ ${SID[@]} =~ $FAIL ]] && printerror "$FAIL was already replaced before"
810 [ -f patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} ] || printerror "failed $FAIL in $JAR cannot be remedied"
811 copyfailed || printerror "failed $NAME.patch"
812 printtask "... replace $FAIL ..."
813 \rm -rf tmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL}.orig tmp/${FILE}.out/smali/${FAIL}.rej
814 \cp -av patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL} >> "$LOG" || echo "FAILED to cp $FAIL" >> ${JAR}.log
815 SID=(${SID[@]} $FAIL)
816 done
818 [[ `grep FAILED ${JAR}.log` != "" ]] && printerror "failed $NAME.patch"
819 garbage tmp/${FILE}.out
820 if [ -f patches/$P/$A/$R/$B/{JAR}.sh ]; then
821 printtask "... apply $JAR.sh ..."
822 . patches/$P/$A/$R/$B/{JAR}.sh 2>&1 > ${JAR}.log
823 cat ${JAR}.log | tee -a "$LOG"
825 done
827 [[ -n ${APKS[@]} ]] && printtask "### apk patch $P/$R/$B ###"
828 for FILE in ${APKS[@]}; do
829 APK=${FILE##*/}
830 if [ -d "tmp/${FILE}.out" ] ; then
831 printtask "... apply $APK.patch ..."
832 patch -Nls -p2 -d tmp/${FILE}.out < $PARB/${APK}.patch 2>&1 >> ${APK}.log
833 cat ${APK}.log | tee -a "$LOG"
834 [[ `grep FAILED ${APK}.log` != "" ]] && printerror "failed $APK.patch"
835 garbage tmp/${FILE}.out
836 else
837 echo "No $APK in your ROM. Not able to patch this file."
838 echo "## No $APK in ROM ###" >> "$LOG"
840 done
841 [[ -n ${IMGS[@]} ]] && printtask "### img patch $P/$A/$R/$B ###"
842 for FILE in ${IMGS[@]}; do
843 IMG=${FILE##*/}
844 #NAME=${IMG%.img}
845 echo "... applying $IMG.patch ..."
846 patch -Nls -p1 -d boot/ramdisk < $PARB/${IMG}.patch >> "$LOG" || printerror "failed patching $FILE"
847 # Special Instructions- married to boot.img patching by necessity, not design. When
848 # added to it's own section, it only runs for one mod. WIP.
849 #if [ -f patches/$P/$R/special_instructions.sh ]; then
850 # . patches/$P/$R/special_instructions.sh
851 #elif [ -f patches/$P/special_instruction.sh ]; then
852 # . patches/$P/special_instructions.sh
854 done
856 [[ -n ${TXTS[@]} ]] && printtask "### text patch $P/$A/$R/$B ###"
857 for FILE in ${TXTS[@]}; do
858 TXT=${FILE##*/}
859 if [[ -f $PARB/${TXT}.patch ]]; then
860 printtask "... apply $TXT.patch ..."
861 patch -Nls -p2 -d system/ < $PARB/${TXT}.patch 2>&1 > ${TXT}.log
862 #cat ${TXT}.log | tee -a "$LOG"
863 [[ `grep FAILED ${TXT}.log` != "" ]] && printerror "failed $TXT.patch"
865 if [[ -f $PAR/${TXT}.awk ]]; then
866 printtask "... apply $TXT.awk ..."
867 awk -f $PAR/${TXT}.awk $FILE > text || printerror "failed $TXT.awk"
868 mv text $FILE
870 if [[ -f $PAR/${TXT}.sed ]]; then
871 printtask "... apply $TXT.sed ..."
872 sed -f $PAR/${TXT}.sed $FILE > text || printerror "failed $TXT.sed"
873 mv text $FILE
875 if [[ -f $PAR/${TXT}.sh ]]; then
876 printtask "... apply $TXT.sh ..."
877 . $PAR/${TXT}.sh 2>&1 >> ${TXT}.log || printerror "failed $TXT.sh"
879 garbage system/
880 done
881 done
883 # Rebuilding system files
885 [[ -n ${JID[@]} ]] && printtask "### rebuild jar ###"
886 for FILE in ${JID[@]}; do
887 JAR=${FILE##*/}
888 printtask "... rebuild $JAR ..."
889 java -Xmx512M -jar smali.jar -a 15 tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
890 [ -f tmp/${FILE}.out/classes.dex ] || printerror "failed compiling $JAR !!!"
891 jar -ufv ${FILE} -C tmp/${FILE}.out/ classes.dex >> "$LOG" 2>> "$LOG"
892 if [ $FILE == "system/framework/framework.jar" ]; then
893 jar -ufv ${FILE} -C tmp/${FILE}.out/ preloaded-classes >> "$LOG" 2>> "$LOG"
895 done
897 [[ -n ${AID[@]} ]] && printtask "### rebuild apk ###"
898 for FILE in ${AID[@]}; do
899 APK=${FILE##*/}
900 printtask "... rebuild $APK ..."
901 java -jar apktool.jar b tmp/${FILE}.out >> "$LOG" 2>> "$LOG" || printerror "failed compiling $FILE !!!"
902 jar -ufv $FILE -C tmp/${FILE}.out/build/apk/ . > /dev/null 2>> "$LOG"
903 #\rm -r tmp/${FILE}.out
904 done
906 [[ -n ${IID[@]} ]] && printtask "### rebuild img ###"
907 for FILE in ${IID[@]}; do
908 printtask "### rebuild $FILE ###"
909 case $FILE in
910 boot.img)
911 # Rebuilding boot.img
912 cd boot
913 printtask "... rebuilding ramdisk ..."
914 ./mkbootfs$EXE ./ramdisk | gzip > ramdisk-new.gz
916 printtask "... composing boot.img ..."
917 BASE=$(cat boot.img-base)
918 # convert hex to decimal in cygwin by prefixing "0x" and C-style arithmetic
919 PGSIZE=$((${PGFORM}$(cat boot.img-pagesize)))
920 CMDLINE=$(cat boot.img-cmdline)
921 echo "cmdline = no_console_suspend=1 console=null $CMDLINE" >> "$LOG"
922 echo "pagesize = $PGSIZE" >> "$LOG"
923 echo "base = $BASE" >> "$LOG"
925 printtask "... rebuilding boot.img ..."
926 ./mkbootimg$EXE --kernel boot.img-zImage --ramdisk ramdisk-new.gz --cmdline "no_console_suspend=1 console=null $CMDLINE" --base "$BASE" --pagesize "$PGSIZE" -o ../boot.img || printerror "failed building boot.img"
927 cd ..
930 printerror "cannot process $FILE"
932 esac
933 done
935 printtask "### create update.zip ###"
936 jar -cfv tmp.zip ${IID[@]} ${JID[@]} ${AID[@]} ${CID[@]} ${TID[@]} META-INF >> "$LOG" 2>> "$LOG"
937 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip update.zip 2>> "$LOG"
938 #\rm -f tmp.zip
940 #echo "# miscellaneous log" >> "$LOG"
942 \mv restore.zip update.zip ..
943 echo "### finished ###"
944 echo "flash update.zip to install the modified"
945 echo "flash restore.zip to restore the original"
947 # add announcement
948 for P in ${PID[@]}; do
949 [ -d patches/$P/$ANDR ] && A=$ANDR || A=all
950 [ -d patches/$P/$A/$RID ] && R=$RID || R=generic
951 if [ -f patches/$P/$A/$R/README.txt ]; then
952 cat patches/$P/$A/$R/README.txt
953 elif [ -f patches/$P/$A/README.txt ]; then
954 cat patches/$P/$A/README.txt
956 done
958 cd ..
959 \rm -rf "$TEMP"