This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
rootAVD.sh
executable file
·2960 lines (2530 loc) · 80.2 KB
/
rootAVD.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
##########################################################################################
#
# Magisk Boot Image Patcher - original created by topjohnwu and modded by shakalaca's
# modded by NewBit XDA for Android Studio AVD
##########################################################################################
###################
# Helper Functions
###################
# Copied 1 to 1 from topjohnwu
getdir() {
case "$1" in
*/*) dir=${1%/*}; [ -z $dir ] && echo "/" || echo $dir ;;
*) echo "." ;;
esac
}
get_flags() {
echo "[-] Get Flags"
if [ -f /system/init -o -L /system/init ]; then
SYSTEM_ROOT=true
else
SYSTEM_ROOT=false
grep ' / ' /proc/mounts | grep -qv 'rootfs' || grep -q ' /system_root ' /proc/mounts && SYSTEM_ROOT=true
fi
if [ -z $KEEPVERITY ]; then
if $SYSTEM_ROOT; then
KEEPVERITY=true
echo "[*] System-as-root, keep dm/avb-verity"
else
KEEPVERITY=false
fi
fi
ISENCRYPTED=false
grep ' /data ' /proc/mounts | grep -q 'dm-' && ISENCRYPTED=true
[ "$(getprop ro.crypto.state)" = "encrypted" ] && ISENCRYPTED=true
if [ -z $KEEPFORCEENCRYPT ]; then
# No data access means unable to decrypt in recovery
if $ISENCRYPTED || ! $DATA; then
KEEPFORCEENCRYPT=true
echo "[-] Encrypted data, keep forceencrypt"
else
KEEPFORCEENCRYPT=false
fi
fi
RECOVERYMODE=false
if [[ $API -eq 28 ]]; then
RECOVERYMODE=true
fi
export RECOVERYMODE
export KEEPVERITY
export KEEPFORCEENCRYPT
echo "[*] RECOVERYMODE=$RECOVERYMODE"
echo "[-] KEEPVERITY=$KEEPVERITY"
echo "[*] KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT"
}
copyARCHfiles() {
BINDIR=$BASEDIR/lib/$ABI
ASSETSDIR=$BASEDIR/assets
STUBAPK=false
if [ -e $BINDIR/libstub.so ]; then
ABI=$ARCH32
BINDIR=$BASEDIR/lib/$ABI
echo "[*] No 64-Bit Binarys found, please consider Magisk Alpha"
elif $IS64BIT && ! $IS64BITONLY; then
echo "[*] copy $ARCH32 files to $BINDIR"
cp $BASEDIR/lib/$ARCH32/lib*32.so $BINDIR 2>/dev/null
fi
cd $BINDIR
for file in lib*.so; do mv "$file" "${file:3:${#file}-6}"; done
cd $BASEDIR
echo "[-] copy all $ABI files from $BINDIR to $BASEDIR"
cp $BINDIR/* $BASEDIR 2>/dev/null
if [ -e $ASSETSDIR/stub.apk ]; then
echo "[-] copy 'stub.apk' from $ASSETSDIR to $BASEDIR"
cp $ASSETSDIR/stub.apk $BASEDIR 2>/dev/null
STUBAPK=true
fi
chmod -R 755 $BASEDIR
export STUBAPK
}
api_level_arch_detect() {
echo "[-] Api Level Arch Detect"
# Detect version and architecture
# To select the right files for the patching
ABI=$(getprop ro.product.cpu.abi)
ABILIST32=$(getprop ro.product.cpu.abilist32)
ABILIST64=$(getprop ro.product.cpu.abilist64)
API=$(getprop ro.build.version.sdk)
FIRSTAPI=$(getprop ro.product.first_api_level)
AVERSION=$(getprop ro.build.version.release)
IS64BIT=false
IS64BITONLY=false
IS32BITONLY=false
if [ "$ABI" = "x86" ]; then
ARCH=x86
ARCH32=x86
elif [ "$ABI" = "arm64-v8a" ]; then
ARCH=arm64
ARCH32=armeabi-v7a
IS64BIT=true
elif [ "$ABI" = "x86_64" ]; then
ARCH=x64
ARCH32=x86
IS64BIT=true
else
ARCH=arm
ABI=armeabi-v7a
ABI32=armeabi-v7a
IS64BIT=false
fi
if [ -z "$ABILIST32" ]; then
IS64BITONLY=true
fi
if [ -z "$ABILIST64" ]; then
IS32BITONLY=true
fi
if $IS64BITONLY || $IS32BITONLY ; then
echo "[-] Device Platform is $ARCH only"
else
echo "[-] Device Platform: $ARCH"
echo "[-] ARCH32 $ARCH32"
fi
echo "[-] Device SDK API: $API"
echo "[-] First API Level: $FIRSTAPI"
echo "[-] The AVD runs on Android $AVERSION"
[ -d /system/lib64 ] && IS64BIT=true || IS64BIT=false
export ARCH
export ARCH32
export IS64BIT
export IS64BITONLY
export IS32BITONLY
export ABI
export API
export FIRSTAPI
export AVERSION
}
abort_script() {
echo "[!] aborting the script"
exit 1
}
compression_method() {
local FILE="$1"
local FIRSTFILEBYTES
local METHOD_LZ4="02214c18"
local METHOD_GZ="1f8b0800"
local ENDG=""
FIRSTFILEBYTES=$(xxd -p -c8 -l8 "$FILE")
FIRSTFILEBYTES="${FIRSTFILEBYTES:0:8}"
if [ "$FIRSTFILEBYTES" == "$METHOD_LZ4" ]; then
ENDG=".lz4"
elif [ "$FIRSTFILEBYTES" == "$METHOD_GZ" ]; then
ENDG=".gz"
fi
echo "$ENDG"
}
detect_ramdisk_compression_method() {
echo "[*] Detecting ramdisk.img compression"
RDF=$BASEDIR/ramdisk.img
CPIO=$BASEDIR/ramdisk.cpio
CPIOORIG=$BASEDIR/ramdisk.cpio.orig
local FIRSTFILEBYTES
local METHOD_LZ4="02214c18"
local METHOD_GZ="1f8b0800"
COMPRESS_SIGN=""
FIRSTFILEBYTES=$(xxd -p -c8 -l8 "$RDF")
FIRSTFILEBYTES="${FIRSTFILEBYTES:0:8}"
RAMDISK_LZ4=false
RAMDISK_GZ=false
ENDG=""
METHOD=""
if [ "$FIRSTFILEBYTES" == "$METHOD_LZ4" ]; then
ENDG=".lz4"
METHOD="lz4_legacy"
RAMDISK_LZ4=true
mv $RDF $RDF$ENDG
RDF=$RDF$ENDG
COMPRESS_SIGN="$METHOD_LZ4"
elif [ "$FIRSTFILEBYTES" == "$METHOD_GZ" ]; then
ENDG=".gz"
METHOD="gzip"
RAMDISK_GZ=true
mv $RDF $RDF$ENDG
#cp $RDF $RDF$ENDG
COMPRESS_SIGN="$METHOD_GZ"
fi
if [ "$ENDG" == "" ]; then
echo "[!] Ramdisk.img uses UNKNOWN compression $FIRSTFILEBYTES"
abort_script
fi
echo "[!] Ramdisk.img uses $METHOD compression"
}
runMagisk_to_Patch_fake_boot_img() {
am force-stop $PKG_NAME
echo "[-] Starting Magisk"
monkey -p $PKG_NAME -c android.intent.category.LAUNCHER 1 > /dev/null 2>&1
echo "[*] Install/Patch $FBI and hit Enter when done(max. 60s)"
read -t 60 proceed
case $proceed in
*)
;;
esac
}
detecting_users() {
local userID=""
local userZero=0
echo "[*] Detecting current user"
userID=$(am get-current-user)
echo "[-] Current user $userID"
if [ "$userID" != "$userZero" ]; then
echo "[-] Switching to user $userZero"
am switch-user $userZero
userID=$(am get-current-user)
echo "[-] Current user $userID"
fi
}
generate_build_prop() {
echo "[*] generating Build.prop"
local BPR=$BASEDIR/build.prop
local recfstab=$BASEDIR/recovery.fstab
getprop > $BPR
sed -i -e 's/: /=/g' -e 's/\[//g' -e 's/\]//g' $BPR
echo "[*] generating recovery.fstab from fstab.ranchu"
cp /system/vendor/etc/fstab.ranchu $recfstab
echo "[-] adding Build.prop and recovery.fstab to Stock Ramdisk"
$BASEDIR/magiskboot cpio $CPIO \
"add 0644 system/build.prop build.prop" \
"add 0644 system/etc/recovery.fstab recovery.fstab"
#exit
#BASEDIR=$(pwd)
}
writeLittleEndian() {
printf "\x${1:6:2}\x${1:4:2}\x${1:2:2}\x${1:0:2}"
}
create_fake_boot_img() {
if $DEBUG; then
generate_build_prop
fi
echo "[*] Creating a fake Boot.img"
FBHI=$BASEDIR/fakebootheader.img
FBI=$SDCARD/fakeboot.img
RAMDISK_SZ="$(printf '%08x' $(stat -c%s $CPIO))"
PAGESIZE=2048
PAGESIZE_HEX="$(printf '%08x' $PAGESIZE)"
echo "[-] removing old $FBI"
rm -f $FBI $RDF
printf "\x41\x4E\x44\x52\x4F\x49\x44\x21" > $FBHI # ANDROID!
printf "\x00\x00\x00\x00\x00\x00\x00\x00" >> $FBHI # HEADER_VER KERNEL_SZ
writeLittleEndian $RAMDISK_SZ >> $FBHI # RAMDISK_SZ
printf "\x00\x00\x00\x00" >> $FBHI # SECOND_SZ
printf "\x00\x00\x00\x00\x00\x00\x00\x00" >> $FBHI # EXTRA_SZ
printf "\x00\x00\x00\x00" >> $FBHI
writeLittleEndian $PAGESIZE_HEX >> $FBHI # PAGESIZE_HEX
echo "[!] Only a minimal header is required for Magisk to repack the ramdisk"
#mv $RDF $CPIO
echo "[*] repacking ramdisk.img into $FBI"
$BASEDIR/magiskboot repack $FBHI $FBI > /dev/null 2>&1
test -f "$FBI"
RESULT="$?"
if [[ "$RESULT" != "0" ]]; then
echo "[*] $FBI could not be created"
echo "[-] Magisk expects a more complete boot.img header as source"
# fill 00 (to Pagesize 2048)
truncate -s $PAGESIZE $FBHI
echo "[*] Adding $CPIO to fakeboot.img header"
cat $CPIO >> $FBHI
echo "[*] Checking filesize Padding for Pagesize 2048"
FBHI_SZ=$(stat -c%s $FBHI)
FBHI_PAD_SZ=$(( FBHI_SZ / $PAGESIZE ))
FBHI_PAD_SZ=$(( FBHI_PAD_SZ * $PAGESIZE ))
if [[ ! $FBHI_PAD_SZ -eq $FBHI_SZ ]]; then
echo "[*] Padding filesize to match Pagesize of 2048 Bytes"
FBHI_PAD_SZ=$(( FBHI_SZ / $PAGESIZE +1))
FBHI_PAD_SZ=$(( FBHI_PAD_SZ * $PAGESIZE ))
truncate -s $FBHI_PAD_SZ $FBHI
fi
echo "[-] repacking ramdisk.img into $FBI with the more complete header"
$BASEDIR/magiskboot repack $FBHI $FBI > /dev/null 2>&1
test -f "$FBI"
RESULT="$?"
if [[ "$RESULT" != "0" ]]; then
echo "[!] $FBI could not be created"
abort_script
fi
fi
echo "[!] $FBI created"
InstallMagiskTemporarily
detecting_users
runMagisk_to_Patch_fake_boot_img
RemoveTemporarilyMagisk
}
unpack_patched_ramdisk_from_fake_boot_img() {
if [ "$MagiskPatchedFiles" != "" ]; then
echo "[!] magisk_patched file(s) could be found!"
for file in `ls -tu $SDCARD/*magisk_patched*`; do
MagiskPatched=$file
break
done
echo "[*] unpacking latest $MagiskPatched"
$BASEDIR/magiskboot unpack $MagiskPatched > /dev/null 2>&1
echo "[-] deleting all magisk_patched files"
for file in `ls -tu $SDCARD/*magisk_patched*`; do
rm -f $file
done
else
echo "[!] No magisk_patched file could be found!"
abort_script
fi
}
process_fake_boot_img() {
SDCARD=/sdcard/Download
echo "[*] Processing fake Boot.img"
MagiskPatchedFiles=$(ls "$SDCARD"/*magisk_patched*) > /dev/null 2>&1
if [ "$MagiskPatchedFiles" != "" ]; then
echo "[!] external magisk_patched file(s) could be found!"
unpack_patched_ramdisk_from_fake_boot_img
else
create_fake_boot_img
MagiskPatchedFiles=$(ls "$SDCARD"/*magisk_patched*) > /dev/null 2>&1
unpack_patched_ramdisk_from_fake_boot_img
fi
}
# requires additional setup
construct_environment() {
ROOT=`su -c "id -u"` 2>/dev/null
if [[ "$ROOT" == "" ]]; then
ROOT=$(id -u)
fi
echo "[-] Constructing environment - PAY ATTENTION to the AVDs Screen"
if [[ $ROOT -eq 0 ]]; then
echo "[!] we are root"
local BBBIN=$BB
local COMMONDIR=$BASEDIR/assets
local NVBASE=/data/adb
local MAGISKBIN=$NVBASE/magisk
`su -c "rm -rf $MAGISKBIN/* 2>/dev/null && \
mkdir -p $MAGISKBIN 2>/dev/null && \
cp -af $BINDIR/. $COMMONDIR/. $BBBIN $MAGISKBIN && \
chown root.root -R $MAGISKBIN && \
chmod -R 755 $MAGISKBIN && \
rm -rf $BASEDIR 2>/dev/null && \
reboot \
"`
fi
echo "[!] not root yet"
echo "[!] Couldn't construct environment"
echo "[!] Double Check Root Access"
echo "[!] Re-Run Script with clean ramdisk.img and try again"
abort_script
}
checkfile() {
#echo "checkfile $1"
if [ -r "$1" ]; then
#echo "File exists and is readable"
if [ -s "$1" ]; then
#echo "and has a size greater than zero"
if [ -w "$1" ]; then
#echo "and is writable"
if [ -f "$1" ]; then
#echo "and is a regular file."
return 1
fi
fi
fi
fi
return 0
}
# If all is done well so far, you can install some APK's to the AVD
# every APK file in the Apps DIR will be (re)installed
# Like magisk.apk etc.
install_apps() {
local ADBECHO=""
APPS="Apps/*"
echo "[-] Install all APKs placed in the Apps folder"
FILES=$APPS
for f in $FILES; do
echo "[*] Trying to install $f"
ADBECHO=""
while [[ "$ADBECHO" != *"Success"* ]]; do
ADBECHO=$(adb install -r -d "$f" 2>&1)
if [[ "$ADBECHO" == *"INSTALL_FAILED_UPDATE_INCOMPATIBLE"* ]]; then
echo "$ADBECHO" | while read I; do echo "[*] $I"; done
Package=
for I in $ADBECHO; do
if [[ "$Package" == *"Package"* ]]; then
echo "[*] Need to uninstall $I first"
ADBECHO=$(adb uninstall $I 2>&1)
echo "$ADBECHO" | while read I; do echo "[*] $I"; done
ADBECHO=$(adb install -r -d "$f" 2>&1)
break
fi
Package=$I
done
fi
done
echo "$ADBECHO" | while read I; do echo "[*] $I"; done
done
}
pushtoAVD() {
local SRC=""
local DST="$2"
local ADBPUSHECHO=""
SRC=${1##*/}
if [[ "$DST" == "" ]]; then
echo "[*] Push $SRC into $ADBBASEDIR"
ADBPUSHECHO=$(adb push "$1" $ADBBASEDIR 2>/dev/null)
else
echo "[*] Push $SRC into $ADBBASEDIR/$DST"
ADBPUSHECHO=$(adb push "$1" $ADBBASEDIR/$DST 2>/dev/null)
fi
echo "[-] $ADBPUSHECHO"
}
pullfromAVD() {
local SRC=""
local DST=""
local ADBPULLECHO=""
SRC=${1##*/}
DST=${2##*/}
ADBPULLECHO=$(adb pull $ADBBASEDIR/$SRC "$2" 2>/dev/null)
if [[ ! "$ADBPULLECHO" == *"error"* ]]; then
echo "[*] Pull $SRC into $DST"
echo "[-] $ADBPULLECHO"
fi
}
create_backup() {
local FILE=""
local FILEPATH=""
local FILENAME=""
local BACKUPFILE=""
FILE="$1"
FILEPATH=${FILE%/*}
FILENAME=${FILE##*/}
BACKUPFILE="$FILENAME.backup"
cd "$FILEPATH" > /dev/null
# If no backup file exist, create one
if ( checkfile $BACKUPFILE -eq 0 ); then
echo "[*] create Backup File of $FILENAME"
cp $FILENAME $BACKUPFILE
else
echo "[-] $FILENAME Backup exists already"
fi
cd - > /dev/null
}
restore_backups() {
local BACKUPFILE=""
local RESTOREFILE=""
cd "$1" > /dev/null
for f in $(find . -type f -name '*.backup'); do
BACKUPFILE="$f"
RESTOREFILE="${BACKUPFILE%.backup}"
echo "[!] Restoring ${BACKUPFILE##*/} to ${RESTOREFILE##*/}"
cp $BACKUPFILE $RESTOREFILE
done
cd - > /dev/null
if [ "$f" == "" ]; then
echo "[*] No Backup(s) to restore"
else
echo "[*] Backups still remain in place"
fi
exit 0
}
toggle_Ramdisk() {
#AVDPATHWITHRDFFILE="$1"
#AVDPATH=${AVDPATHWITHRDFFILE%/*}
#RDFFILE=${AVDPATHWITHRDFFILE##*/}
#RESTOREPATH=$AVDPATH
local RamdiskFile="$AVDPATHWITHRDFFILE"
local PatchedFile="$AVDPATHWITHRDFFILE.patched"
local BackupFile="$AVDPATHWITHRDFFILE.backup"
local hasBackup=false
local hasPatched=false
if ( checkfile "$BackupFile" -eq 0 ); then
echo "[!] we need a valid backup file to proceed"
exit 0
fi
echo "[-] Toggle Ramdisk"
if ( checkfile "$PatchedFile" -eq 0 ); then
echo "[*] Pushing patched Ramdisk into Stack"
mv "$RamdiskFile" "$PatchedFile"
echo "[*] Popping original Ramdisk from Backup"
cp "$BackupFile" "$RamdiskFile"
else
echo "[*] Popping patched Ramdisk back from Stack"
mv -f "$PatchedFile" "$RamdiskFile"
fi
exit 0
}
TestADB() {
local ADB_EX=""
local exportedADB=false
while true; do
echo "[-] Test if ADB SHELL is working"
ADBWORKS=$(which adb)
if [ "$ADBWORKS" == *"not found"* ] || [ "$ADBWORKS" == "" ]; then
if [ ! -d "$ANDROIDHOME/$ADB_DIR" ]; then
echo "[!] ADB not found, please install and add it to your \$PATH"
exit
fi
cd "$ANDROIDHOME" > /dev/null
for adb in $(find "$ADB_DIR" -type f -name adb); do
ADB_EX="$ANDROIDHOME/$adb"
done
cd - > /dev/null
if [[ "$ADB_EX" == "" ]]; then
echo "[!] ADB binary not found in $ENVVAR/$ADB_DIR"
exit
fi
echo "[!] ADB is not in your Path, try to:"
echo ""
echo "export PATH=$ENVVAR/$ADB_DIR:\$PATH"
echo ""
if $exportedADB; then
echo "[!] export didn't work'"
break
fi
if ( ! checkfile "$ADB_EX" -eq 0 ); then
echo "[*] setting it, just during this session, for you"
export "PATH=$ANDROIDHOME/$ADB_DIR:$PATH"
exportedADB=true
fi
else
break
fi
done
ADBWORKS=$(adb shell 'echo true' 2>/dev/null)
if [ -z "$ADBWORKS" ]; then
echo "[!] no ADB connection possible"
exit
elif [[ "$ADBWORKS" == "true" ]]; then
echo "[*] ADB connection possible"
fi
}
MakeBlueStacksRW() {
if ( checkfile "$BLUESTACKSPATH/$AVBOXFILE" -eq 0 ); then
echo "[!] $AVBOXFILE not found"
echo "[!] check your BlueStacks installation"
exit 0
fi
echo "[!] $AVBOXFILE found"
create_backup "$AVBOX"
echo "[*] Changing $ROOTVDIFILE type to \"Normal\""
sed 's,location="Root.vdi" format="VDI" type="Readonly",location="Root.vdi" format="VDI" type="Normal",' $AVBOX > $AVBOX".edit"
mv $AVBOX".edit" $AVBOX
}
ShutDownAVD() {
if ( "$BLUESTACKS" ); then
echo "[-] Shut-Down & Reboot BlueStacks and see if it worked"
echo "[-] Root and Su with Magisk for BlueStacks"
APPNAME=BlueStacks.app
if [ $(ps aux | grep -v grep | grep -c $APPNAME) -gt 0 ]; then
echo "[-] Trying to shut down BlueStacks"
pkill -x BlueStacks
if [ "$?" == "0" ]; then
echo "[*] Shut down Signal were send"
fi
echo "[!] If BlueStacks doesn't shut down, try it manually!"
fi
echo "[*] If BlueStacks Home Screen is closing, run Magisk from the Terminal and hide it"
echo "adb shell monkey -p com.topjohnwu.magisk -c android.intent.category.LAUNCHER 1"
else
echo "[-] Shut-Down & Reboot (Cold Boot Now) the AVD and see if it worked"
echo "[-] Root and Su with Magisk for Android Studio AVDs"
ADBPULLECHO=$(adb shell setprop sys.powerctl shutdown 2>/dev/null)
if [[ ! "$ADBPULLECHO" == *"error"* ]]; then
echo "[-] Trying to shut down the AVD"
fi
echo "[!] If the AVD doesn't shut down, try it manually!"
fi
echo "[-] Modded by NewBit XDA - Jan. 2021"
echo "[!] Huge Credits and big Thanks to topjohnwu, shakalaca, vvb2060 and HuskyDG"
}
GetAVDPKGRevision() {
local sourcepropfile="source.properties"
if [[ -d "$AVDPATH" ]]; then
cd "$AVDPATH" > /dev/null
# If a source.properties file exist, try to find the Pkg.Revision number
if ( ! checkfile $sourcepropfile -eq 0 ); then
echo "[-] source.properties file exist"
echo "[*] AVD system-image $(grep 'Pkg.Revision=' $sourcepropfile)"
fi
cd - > /dev/null
fi
}
CopyMagiskToAVD() {
# Set Folders and FileNames
echo "[*] Set Directorys"
if ( "$BLUESTACKS" ); then
# BlueStacks has its ramdisk.img within, no AVD Path needed
# but the VBOX container Root.vdi should be backuped
BLUESTACKSROOTVDIFILE=~/Library/BlueStacks/Android/Root.vdi
AVBOX=~/Library/BlueStacks/Android/Android.vbox
AVBOXFILE=${AVBOX##*/}
BLUESTACKSPATH=${BLUESTACKSROOTVDIFILE%/*}
ROOTVDIFILE=${BLUESTACKSROOTVDIFILE##*/}
RESTOREPATH=$BLUESTACKSPATH
else
AVDPATHWITHRDFFILE="$ANDROIDHOME/$1"
AVDPATH=${AVDPATHWITHRDFFILE%/*}
RDFFILE=${AVDPATHWITHRDFFILE##*/}
RESTOREPATH=$AVDPATH
fi
if ( "$restore" ); then
restore_backups "$RESTOREPATH"
fi
if ( "$toggleRamdisk" ); then
toggle_Ramdisk "$RESTOREPATH"
fi
if ( "$BLUESTACKS" ); then
if ( checkfile "$BLUESTACKSPATH/$ROOTVDIFILE" -eq 0 ); then
echo "[!] $ROOTVDIFILE not found"
echo "[!] check your BlueStacks installation"
exit
fi
echo "[!] $ROOTVDIFILE found"
create_backup "$BLUESTACKSROOTVDIFILE"
MakeBlueStacksRW
fi
GetAVDPKGRevision
TestADB
# The Folder where the script was called from
ROOTAVD="`getdir "${BASH_SOURCE:-$0}"`"
MAGISKZIP=$ROOTAVD/Magisk.zip
# change to ROOTAVD directory
cd "$ROOTAVD"
# Kernel Names
BZFILE=$ROOTAVD/bzImage
KRFILE=kernel-ranchu
if ( "$InstallApps" ); then
install_apps
exit
fi
ADBWORKDIR=/data/data/com.android.shell
adb shell "cd $ADBWORKDIR" 2>/dev/null
if [ "$?" != "0" ]; then
echo "[!] $ADBWORKDIR doesn't exist, switching to tmp'"
ADBWORKDIR=/data/local/tmp
fi
ADBBASEDIR=$ADBWORKDIR/Magisk
echo "[-] In any AVD via ADB, you can execute code without root in $ADBWORKDIR"
echo "[*] Cleaning up the ADB working space"
adb shell rm -rf $ADBBASEDIR
echo "[*] Creating the ADB working space"
adb shell mkdir $ADBBASEDIR
# If Magisk.zip file doesn't exist, just ignore it
if ( ! checkfile "$MAGISKZIP" -eq 0 ); then
echo "[-] Magisk installer Zip exists already"
pushtoAVD "$MAGISKZIP"
fi
# Proceed with ramdisk
if "$RAMDISKIMG"; then
# Is it a ramdisk named img file?
if [[ "$RDFFILE" != ramdisk*.img ]]; then
echo "[!] please give a path to a ramdisk file"
exit
fi
create_backup "$AVDPATHWITHRDFFILE"
pushtoAVD "$AVDPATHWITHRDFFILE" "ramdisk.img"
if ( "$InstallKernelModules" ); then
INITRAMFS=$ROOTAVD/initramfs.img
if ( ! checkfile "$INITRAMFS" -eq 0 ); then
pushtoAVD "$INITRAMFS"
fi
fi
if ( "$AddRCscripts" ); then
for f in $ROOTAVD/*.rc; do
pushtoAVD "$f"
done
pushtoAVD "$ROOTAVD/sbin"
fi
fi
pushtoAVD "rootAVD.sh"
if ( "$UpdateBusyBoxScript" ); then
pushtoAVD "libbusybox*.so"
fi
echo "[-] run the actually Boot/Ramdisk/Kernel Image Patch Script"
echo "[*] from Magisk by topjohnwu and modded by NewBit XDA"
adb shell sh $ADBBASEDIR/rootAVD.sh $@
if [ "$?" == "0" ]; then
if ( "$UpdateBusyBoxScript" ); then
pullfromAVD "bbscript.sh" "rootAVD.sh"
chmod +x rootAVD.sh
exit
fi
if ( ! "$DEBUG" && "$BLUESTACKS" ); then
pullfromAVD "Magisk.apk" "Apps/"
pullfromAVD "Magisk.zip" "$ROOTAVD"
echo "[-] Clean up the ADB working space"
adb shell rm -rf $ADBBASEDIR
install_apps
ShutDownAVD
adb kill-server
fi
# In Debug-Mode we can skip parts of the script
if ( ! "$DEBUG" && "$RAMDISKIMG" ); then
pullfromAVD "ramdiskpatched4AVD.img" "$AVDPATHWITHRDFFILE"
pullfromAVD "Magisk.apk" "Apps/"
pullfromAVD "Magisk.zip" "$ROOTAVD"
if ( "$InstallPrebuiltKernelModules" ); then
pullfromAVD "$BZFILE" "$ROOTAVD"
InstallKernelModules=true
fi
if ( "$InstallKernelModules" ); then
if ( ! checkfile "$BZFILE" -eq 0 ); then
create_backup "$AVDPATH/$KRFILE"
echo "[*] Copy $BZFILE (Kernel) into kernel-ranchu"
cp $BZFILE $AVDPATH/$KRFILE
if [ "$?" == "0" ]; then
rm -f $BZFILE $INITRAMFS
fi
fi
fi
echo "[-] Clean up the ADB working space"
adb shell rm -rf $ADBBASEDIR
install_apps
ShutDownAVD
fi
fi
}
###################################################
# Method to extract specified field data from json
# Globals: None
# Arguments: 2
# ${1} - value of field to fetch from json
# ${2} - Optional, nth number of value from extracted values, by default shows all.
# Input: file | here string | pipe
# _json_value "Arguments" < file
# _json_value "Arguments <<< "${varibale}"
# echo something | _json_value "Arguments"
# Result: print extracted value
###################################################
json_value() {
$BB grep -o "\"""${1}""\"\:.*" | $BB sed -e "s/.*\"""${1}""\": //" -e 's/[",]*$//' -e 's/["]*$//' -e 's/[,]*$//' -e "s/\"//" -n -e "${2}"p
}
CheckAVDIsOnline() {
if [ -z $AVDIsOnline ]; then
echo "[-] Checking AVDs Internet connection..."
AVDIsOnline=false
$BB timeout 3 $BB wget -q --spider --no-check-certificate http://github.com > /dev/null 2>&1
if [ $? -eq 0 ]; then
AVDIsOnline=true
else
echo "[-] Checking AVDs Internet connection another way..."
echo -e "GET http://google.com HTTP/1.0\n\n" | $BB timeout 3 $BB nc -v google.com 80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
AVDIsOnline=true
fi
fi
$AVDIsOnline && echo "[!] AVD is online" || echo "[!] AVD is offline"
fi
export AVDIsOnline
}
GetPrettyVer() {
if echo $1 | $BB grep -q '\.'; then
PRETTY_VER=$1
else
PRETTY_VER="$1($2)"
fi
echo "$PRETTY_VER"
}
DownLoadFile() {
CheckAVDIsOnline
if ("$AVDIsOnline"); then
local URL="$1"
local SRC="$2"
local DST="$3"
OF=$BASEDIR/download.tmp
rm -f $OF
BS=1024
CUTOFF=100
if [ "$DST" == "" ]; then
DST=$BASEDIR/$SRC
else
DST=$BASEDIR/$DST
fi
#echo "[*] Downloading File $SRC"
$BB wget -q -O $DST --no-check-certificate $URL$SRC
RESULT="$?"
while [ $RESULT != "0" ]
do
echo "[!] Error while downloading File $SRC"
echo "[-] patching it together"
FSIZE=$(./busybox stat $DST -c %s)
if [ $FSIZE -gt $BS ]; then
COUNT=$(( FSIZE/BS ))
if [ $COUNT -gt $CUTOFF ]; then
COUNT=$(( COUNT - $CUTOFF ))
fi
fi
$BB dd if=$DST count=$COUNT bs=$BS of=$OF > /dev/null 2>&1
mv -f $OF $DST
$BB wget -q -O $DST --no-check-certificate $URL$SRC -c
RESULT="$?"
done
echo "[!] Downloading File $SRC complete!"
fi
}
GetUSBHPmod() {
USBHPZSDDL="/sdcard/Download/usbhostpermissons.zip"
USBHPZ="https://github.com/newbit1/usbhostpermissons/releases/download/v1.0/usbhostpermissons.zip"
if [ ! -e $USBHPZSDDL ]; then
echo "[*] Downloading USB HOST Permissions Module Zip"
$BB wget -q -O $USBHPZSDDL --no-check-certificate $USBHPZ
else
echo "[*] USB HOST Permissions Module Zip is already present"
fi
}
FetchMagiskDLData() {
local SRCURL="$1"
local CHANNEL="$2"
local JSON="$CHANNEL.json"
local VER=""
local VER_CODE=""
local DLL=""
local i=1
rm -rf *.json > /dev/null 2>&1
$BB wget -q --no-check-certificate $SRCURL$JSON
VER=$(json_value "version" < $JSON)
VER_CODE=$(json_value "versionCode" 1 < $JSON)
DLL=$(json_value "link" 1 < $JSON)
VER=$(GetPrettyVer $VER $VER_CODE)
if ! echo $DLL | $BB grep -q 'https'; then
DLL=$SRCURL$DLL
fi
if [ -e $MAGISK_DL_LINKS ]; then
echo $DLL >> $MAGISK_DL_LINKS
echo $VER >> $MAGISK_VERSIONS
echo $CHANNEL >> $MAGISK_CHANNEL
i=$($BB sed -n '$=' $MAGISK_DL_LINKS)
echo "[$i] $CHANNEL $VER" >> $MAGISK_MENU
else
if [[ "$MAGISK_LOCL_VER" != "" ]]; then
echo "local" > $MAGISK_DL_LINKS
echo $MAGISK_LOCL_VER > $MAGISK_VERSIONS
echo "local "$CHANNEL > $MAGISK_CHANNEL
echo "[$i] local $CHANNEL $MAGISK_LOCL_VER (ENTER)" > $MAGISK_MENU
i=$((i+1))
fi
echo $DLL >> $MAGISK_DL_LINKS
echo $VER >> $MAGISK_VERSIONS
echo $CHANNEL >> $MAGISK_CHANNEL
if [[ "$i" == "1" ]]; then
echo "[$i] $CHANNEL $VER (ENTER)" >> $MAGISK_MENU
else
#echo $CHANNEL > $MAGISK_CHANNEL
echo "[$i] $CHANNEL $VER" >> $MAGISK_MENU
fi
fi
rm -rf *.json > /dev/null 2>&1
}
FetchMagiskRLCommits() {
#$GITHUB $TJWCOMMITSURL $TJWBLOBURL $CHANNEL $TJWREPOURL
local DOMAIN="$1"
local COMMITSURL="$2"
local BLOBURL="$3"
local CHANNEL="$4"
local JSON="$CHANNEL.json"