Add 'pa' as MODTYPE; include extension and pdroid patches
[auto-patcher.git] / auto_patcher
bloba05979a888acf3447d32acc0ab689df212e4b981
1 #!/bin/bash
3 # version: beta v2.1.9, patches 2012.010.16.
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 patche 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.
86 # SHELL PROGRAMMING NOTES:
87 # [ -s FILE ]
88 # [[ -n STRING ]] or [[ -z STRING ]]
90 VERSION="Auto-Patcher v2.1.9"
92 echo ""
93 echo "The $VERSION"
94 echo ""
95 # by pastime1971 and mateorod
98 # pass variables to children
99 set -a
101 # function for converting hex to binary- for testing
102 #function h2b { echo "obase=2; ibase=16; $( echo "$*" | sed -e 's/0x//g' -e 's/\([a-z]\)/\u\1/g' )" | bc; }
104 printusage () {
105 echo " The $VERSION"
106 echo " pdroid 1.32 patches 2012.09.13."
107 echo " v6supercharger patches 2012.08.17."
108 echo ""
109 echo "usage: $0 ROM.ZIP MODS [ROMTYPE] [PATCHDATE]"
110 echo ""
111 echo "MODS can be pdroid,v6supercharger or pdroid or v6supercharger"
112 echo "optional [ROMTYPE] can be cm9, aokp, etc."
113 echo "optional [PATCHDATE] can be specified to use older patch versions."
114 echo "try \"$0 ROM.ZIP -h\" to see what is available"
115 echo ""
116 exit
119 debug () {
120 echo "! please refer to $LOG for any debugging"
121 echo ""
122 echo "! patching failed."
123 echo "! it may be possible to patch $ROM using other patch version"
124 echo ""
125 [ -d "$TEMP" ] && \rm -rf "$TEMP"
126 printusage
129 printerror () {
130 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
131 echo "!!! error: $@"
132 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
133 echo ""
134 debug
137 printtask () {
138 echo "" >> "$LOG"
139 echo $@ | tee -a "$LOG"
142 copyfailed () {
143 echo ""
144 echo "File for which patching failed:"
145 echo "---------------------------------------------"
146 echo ""
147 echo "${FAIL##*/}"
148 echo ""
149 echo "---------------------------------------------"
150 echo ""
151 echo "We can try to add a premade copy of those files."
152 echo "This sometimes works."
153 echo ""
154 echo "But IT MAY NOT WORK!!!"
155 echo ""
156 echo ""
157 askconsent && return 0 || return 1
160 askconsent () {
161 sleep 1
162 echo -n "Do you want to proceed ? (YES/no) "
163 read PROCEED
164 echo "askconsent intial = ${PROCEED}" >> "$LOG"
165 case $PROCEED in
166 [Yy] | [Yy][Ee][Ss])
167 echo ""
170 return 1; # error
172 esac
173 echo -n "Are you sure ? (YES/no) "
174 read PROCEED
175 echo "askconsent final = ${PROCEED}: for $FAIL" >> "$LOG"
176 case $PROCEED in
177 [Yy] | [Yy][Ee][Ss])
178 return 0; # successful
181 return 1; # error
183 esac
186 abspath () {
187 case `uname -s` in
188 CYGWIN*)
189 echo $(cygpath -ua "$1")
191 Darwin)
192 #[[ $(echo $1 | awk '/^\//') == $1 ]] && echo "$1" || echo "$PWD/$1"
193 [[ ${1:0:1} == "/" ]] && echo "$1" || echo "$PWD/$1"
195 Linux)
196 echo $(readlink -f "$1")
199 if [[ ${1:0:1} == "/" ]]; then
200 echo "$1"
201 elif [[ ${1:0:2} == "./" ]]; then
202 echo "$PWD/${1:2}"
203 else
204 echo "$PWD/$1"
207 esac
210 extpath () {
211 case `uname -s` in
212 CYGWIN*)
213 echo $(cygpath -da "$1")
216 echo $(abspath "$1")
218 esac
221 #contains () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; }
223 parse () { echo "${1%%_*}"; }
225 getbuildprop () {
226 if [ -f system/build.prop ]; then
227 result=(`grep $1 system/build.prop | tr -d '\r' | tr '=' ' '`)
228 echo "${result[1]}"
229 else
230 echo ""
234 proberom () {
235 VID=$(getbuildprop ro.build.version.release)
237 if [ "${VID%.*}" == "2.3" ]; then
238 ANDR=gingerbread
239 RID=cm7;
240 elif [ "${VID%.*}" == "4.0" ]; then
241 ANDR=ics
242 MID=$(getbuildprop ro.rommanager.developerid)
243 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.modversion))
244 DID=$(getbuildprop ro.product.device)
245 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
247 case "$MID" in
248 pa|cm9|cm|cyanogenmod*)
249 RID=cm9;
251 aokp|teamkang)
252 RID=aokp;
254 # aosp|AOSP) # AOSP probing does not work yet
255 # RID=aosp;
256 # ;;
258 RID=aosp;
259 echo "!!! $MID is not supported yet, trying generic latest !!!"
261 esac
262 elif [ "${VID%.*}" == "4.1" ]; then
263 ANDR=jellybean
264 MID=$(getbuildprop ro.build.display.id)
265 [[ "$MID" == "" ]] && MID=$(parse $(getbuildprop ro.build.display.id))
266 DID=$(getbuildprop ro.product.device)
267 [[ "$DID" == "" ]] && DID=$(getbuildprop ro.build.product)
269 case "$MID" in
270 cm10|cm*|cyanogenmod*)
271 RID=cm10;
273 aokp*|teamkang)
274 RID=aokp-jb;
276 pa*)
277 RID=pa;
280 aosp_mod_check;
282 esac
283 else
284 printerror "unknown ROM version $VID"
286 echo "... result of ROM check: version= $VID rom= $RID ..."
288 # Adding build date query
289 UTC=$(getbuildprop ro.build.date.utc)
290 BDID=`date -d "1970-01-01 UTC $UTC seconds" +"%Y-%m-%d"`
293 aosp_mod_check() {
294 if [ -s 'jar -xvf "$ROMX" system/framework/framework2.jar' ]; then
295 RID=aosp-mod
296 echo "Framework2.jar found; defaulting to $RID"
297 else
298 RID=aosp-jb
299 echo "No framework2.jar found; using $RID patches"
302 proberil () {
303 RIL=$(getbuildprop ro.telephony.ril_class)
304 [[ -n $RIL ]] && printtask "... result of ROM check: RIL= $RIL"
307 garbage () {
308 unset GARBAGE
309 GARBAGE=($(find $@ -name "*.orig")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
310 GARBAGE=($(find $@ -name "*.rej")) && rm -rf ${GARBAGE[@]} && echo "GARBAGE= ${GARBAGE[@]}" >> "$LOG"
313 rominfo () {
314 echo "# VER= $VERSION" >> "$LOG"
315 echo "# OS= $OSTYPE ${BIT}bit" >> "$LOG"
316 echo "# VID= $VID" >> "$LOG"
317 echo "# MID= $MID" >> "$LOG"
318 echo "# RID= $RID" >> "$LOG"
319 echo "# BDID= $BDID" >> "$LOG"
320 echo "# DID= $DID" >> "$LOG"
321 echo "# BID= $BID" >> "$LOG"
322 echo "# PID= ${PID[@]}" >> "$LOG"
325 [[ $# -lt 1 || $1 == "-h" || -z $1 ]] && printusage
327 ROOT=$(abspath `dirname "$0"`)
328 TOOL="$ROOT/patch_tools.tgz"
329 PTCH="$ROOT/patch_patches.tgz"
330 ROM=$(abspath "$1")
331 ROMX=$(extpath "$1")
333 if [[ $OSTYPE == "cygwin" ]]; then
334 EXE=".exe"
335 PGFORM="0x"
338 DATE=`date +%Y%m%d%H%M%S`
339 TEMP=$(abspath ./tmp$DATE)
340 LOG=$(abspath ./log$DATE)
341 #echo $ROOT $TOOL $ROM $TEMP "$LOG"
342 echo "ROOT= $ROOT" >> "$LOG"
343 echo "TOOL= $TOOL" >> "$LOG"
344 echo "ROM= $ROM" >> "$LOG"
345 echo "ROMX= $ROMX" >> "$LOG"
347 \rm -f restore.zip update.zip
349 CHK=$(getconf LONG_BIT)
350 if [[ $CHK = "64" ]]; then
351 BIT=64
352 else [[ $BIT = "32" ]];
353 BIT=32
357 printtask "### verify tools ###"
358 which patch >> "$LOG" || printerror "Missing \"patch diff command\""
359 which tar >> "$LOG" || printerror "Missing \"tar archive command\""
360 which jar >> "$LOG" || printerror "Missing \"jar (JDK)\""
361 which java >> "$LOG" || printerror "Missing \"java (JRE)\""
362 which cpio >> "$LOG" || printerror "Missing \"cpio archive command\""
363 [ -f "$TOOL" ] || printerror "patch tool is missing"
364 [ -f "$PTCH" ] || printerror "patch patches are missing"
365 printtask "### verify ROM ###"
366 [ -f "$ROM" ] || printerror "Cannot find $ROM"
367 echo "### running $0 $@" >> "$LOG"
368 mkdir "$TEMP"
369 cd "$TEMP"
371 PATH="$TEMP:$PATH"
372 export PATH
373 echo "PATH= $PATH" >> "$LOG"
375 printtask "### unzip patch tools ###"
376 tar xvzf "$TOOL" > /dev/null || printerror "failed extracting tools !!!"
378 # Get OS specific tools
379 case `uname -s` in
380 CYGWIN*)
381 \mv cygwin/aapt.exe .
382 \mv cygwin/*.exe boot/
383 \rm -rf cygwin # why? If you want to we can keep/add it to mac/linux, though.
385 [Dd]arwin)
386 \mv mac/aapt .
388 [Ll]inux)
389 \mv linux/aapt .
392 \mv linux/aapt .
394 esac
396 printtask "### unzip patch patches ###"
397 tar xvzf "$PTCH" > /dev/null || printerror "failed extracting patches !!!"
399 # we need build.prop for both RIL probing and ROM probing
400 jar -xvf "$ROMX" system/build.prop 2>> "$LOG" || printerror "failed extracting build.prop from $ROMX !!!"
401 # ril type
402 proberil
403 printtask "### probe rom type ###"
404 proberom
405 \rm -f system/build.prop
406 # build date
407 if [ $# -gt 3 ]; then
408 BID=$4
409 else
410 BID=latest
412 # rom type
413 if [ $# -gt 2 ]; then
414 RID=$3
415 echo "... Using $RID patches ..."
416 else
417 echo "... Using results of ROM check ..."
420 # patch type
421 if [ $# -gt 1 ]; then
422 PIL=${2//,/ }
423 else
424 #PIL=`ls patches/`
425 PIL="pdroid v6supercharger"
427 # a hack to make sure pdroid is the first mod
428 [[ ${PIL[@]} =~ pdroid ]] && PID=(pdroid)
429 for P in ${PIL[@]}; do
430 [[ ${PID[@]} =~ $P ]] || PID=(${PID[@]} $P)
431 done
433 # echo ROM info
434 rominfo
436 if [[ ( "$PID" == "-h" || "$RID" == "-h" || "$BID" == "-h" ) ]]; then
437 echo "### supported patch candidates ###"
438 [ "$PID" != "-h" ] && PL=${PID[@]} || PL=`ls patches/`
439 for P in ${PL[@]}; do
440 [ "$PID" != "-h" -a "$RID" != "-h" ] && RL=${RID[@]} || RL=`ls patches/$P/`
441 for R in ${RL[@]} ; do
442 [ $R == "common" -o ! -d patches/$P/$R ] && continue
443 echo -n "$P : $R : "
444 \ls --ignore="*.sh" --ignore="*.txt" -r patches/$P/$R/
445 done
446 done
447 else
448 for P in ${PID[@]}; do
449 [ -d patches/$P ] || printerror "$P is not available"
451 [ -d patches/$P/$RID ] && R=$RID || R=generic
452 #[ -d patches/$P/$R/$BID ] && B=$BID || B=latest
454 unset FILES
455 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
456 [ -f patches/$P/$R/files.txt ] && FILES=$(cat patches/$P/$R/files.txt)
457 for FILE in ${FILES[@]}; do
458 case "$FILE" in
459 +system/*)
460 [[ ${CID[@]} =~ ${FILE:1} ]] && printerror "${FILE:1} is already being copied" || CID=(${CID[@]} ${FILE:1})
462 -system/*)
463 [[ ${EID[@]} =~ ${FILE:1} ]] || EID=(${EID[@]} ${FILE:1})
465 system/*.jar)
466 [[ ${JID[@]} =~ $FILE ]] || JID=(${JID[@]} $FILE)
468 system/*.apk)
469 [[ ${AID[@]} =~ $FILE ]] || AID=(${AID[@]} $FILE)
471 *.img)
472 [[ ${IID[@]} =~ $FILE ]] || IID=(${IID[@]} $FILE)
474 system/*)
475 [[ ${TID[@]} =~ $FILE ]] || TID=(${TID[@]} $FILE)
477 esac
478 done
479 done
480 echo "### DELETE FILES= ${EID[@]}" >> "$LOG"
481 echo "### COPY FILES= ${CID[@]}" >> "$LOG"
482 echo "### JAR FILES= ${JID[@]}" >> "$LOG"
483 echo "### APK FILES= ${AID[@]}" >> "$LOG"
484 echo "### IMG FILES= ${IID[@]}" >> "$LOG"
485 echo "### TXT FILES= ${TID[@]}" >> "$LOG"
487 printtask "### unzip rom ###"
488 # extract system files
489 jar -xvf "$ROMX" ${JID[@]} ${AID[@]} ${IID[@]} ${TID[@]} 2>> "$LOG" || printerror "failed extracting ${JID[@]} ${AID[@]} ${IID[@]} from $ROMX !!!"
490 #\cp -a system/ backup # only for debugging purpose
492 printtask "### create updater script ###"
493 jar -xvf "$ROMX" META-INF/com/google/android/updater-script META-INF/com/google/android/update-binary
494 jar -xvf "$ROMX" META-INF/com/google/android/update-binary-installer 1> /dev/null 2> /dev/null
495 if [ -f META-INF/com/google/android/update-binary-installer ]; then
496 printtask "... using Aroma installer ..."
497 \mv META-INF/com/google/android/update-binary-installer META-INF/com/google/android/update-binary
498 else
499 printtask "... using Clockworkmod installer ..."
501 if [ -f META-INF/com/google/android/update-binary ]; then
502 #echo "# original updater-script" >> "$LOG"
503 #cat META-INF/com/google/android/updater-script >> "$LOG"
505 cat META-INF/com/google/android/updater-script | grep ^mount | grep "/system" | head -n 1 > updater-script
506 [ -s updater-script ] || echo "run_program(\"/sbin/busybox\", \"mount\", \"/system\");" > updater-script
508 for FILE in ${EID[@]}; do
509 # restore the original
510 jar -xvf "$ROMX" $FILE 2> /dev/null || printtask "!!! warning $FILE cannot be restored !!!"
511 done
512 for FILE in ${CID[@]}; do
513 # replace with the original or delete it
514 jar -xvf "$ROMX" $FILE 2> /dev/null || echo "delete(\"/$FILE\");" >> updater-script
515 done
517 echo "package_extract_dir(\"system\", \"/system\");" >> updater-script
518 # add permission management
519 cat META-INF/com/google/android/updater-script | grep set_perm | grep "/system" >> updater-script
520 # TODO: remove if we don't need it any more
521 for P in ${PID[@]}; do
522 if [ -s patches/$P/set_perm.txt ]; then
523 echo "...adding permissions..."
524 cat patches/$P/set_perm.txt >> updater-script
526 done
528 cat META-INF/com/google/android/updater-script | grep ^unmount | grep "/system" | head -n 1 >> updater-script
530 [[ ${IID[@]} =~ boot.img ]] && cat META-INF/com/google/android/updater-script | grep "boot.img" >> updater-script
532 cat updater-script > META-INF/com/google/android/updater-script
534 echo "# generated updater-script" >> "$LOG"
535 cat updater-script >> "$LOG"
536 else
537 # this is not enough any more
538 jar -xvf update-template.zip META-INF/com/google/android/update-binary META-INF/com/google/android/updater-script
541 printtask "### create restore.zip ###"
542 jar -cfv tmp.zip ${JID[@]} ${AID[@]} ${IID[@]} META-INF > /dev/null 2>> "$LOG"
543 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip signed.zip 2>> "$LOG"
544 \mv signed.zip restore.zip
545 \rm -f tmp.zip
547 printtask "### decompile system files ###"
548 # decompile system files
549 for FILE in ${JID[@]}; do
550 JAR=${FILE##*/}
551 printtask "... decompile $JAR ..."
552 java -jar baksmali.jar -b -a 15 -o tmp/${FILE}.out/smali ${FILE} >> "$LOG" 2>> "$LOG"
553 if [ $FILE == "system/framework/framework.jar" ]; then
554 printtask "... extract preloaded-classes ..."
555 jar -xvf ${FILE} preloaded-classes >> "$LOG" 2>> "$LOG"
556 \mv preloaded-classes tmp/${FILE}.out/
558 done
559 for FILE in ${AID[@]}; do
560 APK=${FILE##*/}
561 printtask "... decompile $APK ..."
562 if [ $FILE == "system/framework/framework-res.apk" ]; then
563 printtask "... installing framework files ..."
564 java -jar apktool.jar if $FILE
566 java -jar apktool.jar d -f $FILE tmp/$FILE.out
568 done
569 for FILE in ${IID[@]}; do
570 printtask "### unpacking $FILE ###"
571 case "$FILE" in
572 boot.img)
573 cd boot
574 ./unpackbootimg$EXE -i ../boot.img >> "$LOG" 2>> "$LOG"
576 mkdir ramdisk
577 cd ramdisk
578 gzip -dc ../boot.img-ramdisk.gz | cpio -i >> "$LOG" 2>> "$LOG"
579 cd ../../
582 printerror "cannot process $FILE"
584 esac
585 done
588 # apply system modification
589 for P in ${PID[@]}; do
590 [ -d patches/$P/$RID ] && R=$RID || R=generic
591 [ -d patches/$P/$R/$BID ] && B=$BID || B=latest
592 PR=patches/$P/$R
593 PRB=patches/$P/$R/$B
595 unset FILES COPY DELS JARS APKS IMGS TXTS
596 [ -f patches/$P/files.txt ] && FILES=$(cat patches/$P/files.txt)
597 [ -f patches/$P/$R/files.txt ] && FILES=$(cat patches/$P/$R/files.txt)
598 for FILE in ${FILES[@]}; do
599 case $FILE in
600 +system/*)
601 COPY=(${COPY[@]} ${FILE:1})
603 -system/*)
604 DELS=(${DELS[@]} ${FILE:1})
606 system/*.jar)
607 JARS=(${JARS[@]} $FILE)
609 system/*.apk)
610 APKS=(${APKS[@]} $FILE)
612 *.img)
613 IMGS=(${IMGS[@]} $FILE)
615 system/*)
616 TXTS=(${TXTS[@]} $FILE)
618 esac
619 done
621 [[ -n ${COPY[@]} ]] && printtask "### copy patch $P/$R/$B ###"
622 for FILE in ${COPY[@]}; do
623 printtask "... copying $FILE ..."
624 DEST=${FILE%/*}
625 mkdir -p ./$DEST
626 if [ -e patches/$P/$R/$B/$FILE ]; then
627 [ -e $FILE ] && printtask "... overwriting $FILE from $P ..."
628 \cp -av patches/$P/$R/$B/$FILE ./$DEST >> "$LOG"
629 else
630 printerror "$FILE is missing in the patch"
632 done
634 # unconditional smali copying
635 [ -f patches/$P/smali.txt ] && SMALI=$(cat patches/$P/smali.txt)
636 [ -f patches/$P/$R/smali.txt ] && SMALI=$(cat patches/$P/$R/smali.txt)
637 [ -f patches/$P/$R/$B/smali.txt ] && SMALI=$(cat patches/$P/$R/$B/smali.txt)
638 MOVE=${SMALI##*/}
639 for SMALI in ${SMALI[@]}; do
640 printtask "... overwriting $MOVE ..."
641 \cp -av patches/$P/$ANDR/provisionals/$SMALI tmp/$SMALI >> "$LOG"
642 done
645 [[ -n ${DELS[@]} ]] && printtask "### delete patch $P/$R/$B ###"
646 for FILE in ${DELS[@]}; do
647 printtask "... deleting $FILE ..."
648 \rm -rf ./$FILE
649 done
651 [[ -n ${JARS[@]} ]] && printtask "### jar patch $P/$R/$B ###"
652 for FILE in ${JARS[@]}; do
653 JAR=${FILE##*/}
654 NAME=${JAR%.jar}
655 printtask "... apply $NAME.patch ..."
656 patch -Nls -p1 -d tmp/${FILE}.out < $PRB/${NAME}.patch 2>&1 > ${JAR}.log
657 cat ${JAR}.log | tee -a "$LOG"
658 case "$NAME" in
659 framework)
660 # patch preloaded-classes only if a patch exists
661 if [ -f $PRB/preloaded.patch ]; then
662 printtask "... apply preloaded.patch ..."
663 patch -Nls -p1 -d tmp/${FILE}.out < $PRB/preloaded.patch 2>&1 > preloaded.log
664 cat preloaded.log | tee -a "$LOG"
665 [[ `grep FAILED preloaded.log` != "" ]] && printerror "failed preloaded.patch"
669 echo -n "" # do nothing else
671 esac
672 # substitute failed smali with premade jar
673 unset FAILS
674 FAILS=(`grep FAILED ${JAR}.log | sed -e 's/.*smali\///' | sed -e 's/\.rej//'`)
675 if [[ -n ${FAILS[@]} ]]; then
676 printtask "!!! PATCHING FAILED FOR ${FAILS[@]} !!!"
677 echo "### replace ${FAILS[@]} ###" > ${JAR}.log
678 for FAIL in ${FAILS[@]}; do
679 echo "!!! patch error for $FAIL !!!" >> "$LOG"
680 #cat tmp/$FILE.out/smali/${FAIL}.rej >> "$LOG"
681 [[ ${SID[@]} =~ $FAIL ]] && printerror "$FAIL was already replaced before"
682 [ -f patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} ] || printerror "failed $FAIL in $JAR cannot be remedied"
683 copyfailed || printerror "failed $NAME.patch"
684 printtask "... replace $FAIL ..."
685 \rm -rf tmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL}.orig tmp/${FILE}.out/smali/${FAIL}.rej
686 \cp -av patches/$P/$ANDR/provisionals/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL} >> "$LOG" || echo "FAILED to cp $FAIL" >> ${JAR}.log
687 #echo "### replacing ${FAILS[@]} from patches/$P/common/${FILE} ###" > ${JAR}.log
688 #java -jar baksmali.jar -b -a 15 -o pmp/${FILE}.out/smali patches/$P/common/provisionals/${FAIL} >> "$LOG" 2>> "$LOG" || echo "FAILED to baksmali $FILE" >> ${JAR}.log
689 #\cp -a pmp/${FILE}.out/smali/${FAIL} tmp/${FILE}.out/smali/${FAIL} || echo "FAILED to cp $FAIL" >> ${JAR}.log
690 SID=(${SID[@]} $FAIL)
691 done
693 [[ `grep FAILED ${JAR}.log` != "" ]] && printerror "failed $NAME.patch"
694 garbage tmp/${FILE}.out
695 if [ -f patches/$P/$R/$B/{JAR}.sh ]; then
696 printtask "... apply $JAR.sh ..."
697 . patches/$P/$R/$B/{JAR}.sh 2>&1 > ${JAR}.log
698 cat ${JAR}.log | tee -a "$LOG"
700 done
702 [[ -n ${APKS[@]} ]] && printtask "### apk patch $P/$R/$B ###"
703 for FILE in ${APKS[@]}; do
704 APK=${FILE##*/}
705 if [ -s $APK ] ; then
706 printtask "... apply $APK.patch ..."
707 patch -Nls -p2 -d tmp/${FILE}.out < $PRB/${APK}.patch 2>&1 >> ${APK}.log
708 cat ${APK}.log | tee -a "$LOG"
709 [[ `grep FAILED ${APK}.log` != "" ]] && printerror "failed $APK.patch"
710 garbage tmp/${FILE}.out
711 else
712 echo "No $APK in your ROM. Not able to patch this file."
714 done
715 [[ -n ${IMGS[@]} ]] && printtask "### img patch $P/$R/$B ###"
716 for FILE in ${IMGS[@]}; do
717 IMG=${FILE##*/}
718 #NAME=${IMG%.img}
719 echo "... applying $IMG.patch ..."
720 patch -Nls -p1 -d boot/ramdisk < $PRB/${IMG}.patch >> "$LOG" || printerror "failed patching $FILE"
721 # Special Instructions- married to boot.img patching by necessity, not design. When
722 # added to it's own section, it only runs for one mod. WIP.
723 #if [ -f patches/$P/$R/special_instructions.sh ]; then
724 # . patches/$P/$R/special_instructions.sh
725 #elif [ -f patches/$P/special_instruction.sh ]; then
726 # . patches/$P/special_instructions.sh
728 done
730 [[ -n ${TXTS[@]} ]] && printtask "### text patch $P/$R/$B ###"
731 for FILE in ${TXTS[@]}; do
732 TXT=${FILE##*/}
733 if [[ -f $PRB/${TXT}.patch ]]; then
734 printtask "... apply $TXT.patch ..."
735 patch -Nls -p2 -d system/ < $PRB/${TXT}.patch 2>&1 > ${TXT}.log
736 #cat ${TXT}.log | tee -a "$LOG"
737 [[ `grep FAILED ${TXT}.log` != "" ]] && printerror "failed $TXT.patch"
739 if [[ -f $PR/${TXT}.awk ]]; then
740 printtask "... apply $TXT.awk ..."
741 awk -f $PR/${TXT}.awk $FILE > text || printerror "failed $TXT.awk"
742 mv text $FILE
744 if [[ -f $PR/${TXT}.sed ]]; then
745 printtask "... apply $TXT.sed ..."
746 sed -f $PR/${TXT}.sed $FILE > text || printerror "failed $TXT.sed"
747 mv text $FILE
749 if [[ -f $PR/${TXT}.sh ]]; then
750 printtask "... apply $TXT.sh ..."
751 . $PR/${TXT}.sh 2>&1 >> ${TXT}.log || printerror "failed $TXT.sh"
753 garbage system/
754 done
755 done
757 # Rebuilding system files
759 [[ -n ${JID[@]} ]] && printtask "### rebuild jar ###"
760 for FILE in ${JID[@]}; do
761 JAR=${FILE##*/}
762 printtask "... rebuild $JAR ..."
763 java -Xmx512M -jar smali.jar -a 15 tmp/${FILE}.out/smali -o tmp/${FILE}.out/classes.dex >> "$LOG" 2>> "$LOG"
764 [ -f tmp/${FILE}.out/classes.dex ] || printerror "failed compiling $JAR !!!"
765 jar -ufv ${FILE} -C tmp/${FILE}.out/ classes.dex >> "$LOG" 2>> "$LOG"
766 if [ $FILE == "system/framework/framework.jar" ]; then
767 jar -ufv ${FILE} -C tmp/${FILE}.out/ preloaded-classes >> "$LOG" 2>> "$LOG"
769 done
771 [[ -n ${AID[@]} ]] && printtask "### rebuild apk ###"
772 for FILE in ${AID[@]}; do
773 APK=${FILE##*/}
774 printtask "... rebuild $APK ..."
775 java -jar apktool.jar b tmp/${FILE}.out >> "$LOG" 2>> "$LOG" || printerror "failed compiling $FILE !!!"
776 jar -ufv $FILE -C tmp/${FILE}.out/build/apk/ . > /dev/null 2>> "$LOG"
777 #\rm -r tmp/${FILE}.out
778 done
780 [[ -n ${IID[@]} ]] && printtask "### rebuild img ###"
781 for FILE in ${IID[@]}; do
782 printtask "### rebuild $FILE ###"
783 case $FILE in
784 boot.img)
785 # Rebuilding boot.img
786 cd boot
787 printtask "... rebuilding ramdisk ..."
788 ./mkbootfs$EXE ./ramdisk | gzip > ramdisk-new.gz
790 printtask "... composing boot.img ..."
791 BASE=$(cat boot.img-base)
792 # convert hex to decimal in cygwin by prefixing "0x" and C-style arithmetic
793 PGSIZE=$((${PGFORM}$(cat boot.img-pagesize)))
794 CMDLINE=$(cat boot.img-cmdline)
795 echo "cmdline = no_console_suspend=1 console=null $CMDLINE" >> "$LOG"
796 echo "pagesize = $PGSIZE" >> "$LOG"
797 echo "base = $BASE" >> "$LOG"
799 printtask "... rebuilding boot.img ..."
800 ./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"
801 cd ..
804 printerror "cannot process $FILE"
806 esac
807 done
809 printtask "### create update.zip ###"
810 jar -cfv tmp.zip ${IID[@]} ${JID[@]} ${AID[@]} ${CID[@]} ${TID[@]} META-INF >> "$LOG" 2>> "$LOG"
811 java -jar signapk.jar updatecert.pem updatekey.pk8 tmp.zip update.zip 2>> "$LOG"
812 #\rm -f tmp.zip
814 #echo "# miscellaneous log" >> "$LOG"
816 \mv restore.zip update.zip ..
817 echo "### finished ###"
818 echo "flash update.zip to install the modified"
819 echo "flash restore.zip to restore the original"
821 # add announcement
822 for P in ${PID[@]}; do
823 [ -d patches/$P/$RID ] && R=$RID || R=generic
824 if [ -f patches/$P/$R/README.txt ]; then
825 cat patches/$P/$R/README.txt
826 elif [ -f patches/$P/README.txt ]; then
827 cat patches/$P/README.txt
829 done
831 cd ..
832 \rm -rf "$TEMP"