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.bat
619 lines (529 loc) · 16.9 KB
/
rootAVD.bat
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
@echo off
SetLocal DisableDelayedExpansion
set true=1==1
set false=1==0
REM ##########################################################################################
REM #
REM # Magisk Boot Image Patcher - original created by topjohnwu and modded by shakalaca's
REM # modded by NewBit XDA for Android Studio AVD
REM # Successfully tested on Android API:
REM # [Dec. 2019] - 29 Google Apis Play Store x86_64 Production Build
REM # [Jan. 2021] - 30 Google Apis Play Store x86_64 Production Build
REM # [Apr. 2021] - 30 Android (S) Google Apis Play Store x86_64 Production Build rev 3
REM #
REM ##########################################################################################
REM rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-S\google_apis_playstore\x86_64\ramdisk.img
REM rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-30\google_apis_playstore\x86_64\ramdisk.img
REM rootAVD.bat %LOCALAPPDATA%\Android\Sdk\system-images\android-29\google_apis_playstore\x86_64\ramdisk.img
call :ProcessArguments %*
call :GetANDROIDHOME
IF %DEBUG% (
echo [^!] We are in Debug Mode
echo params=%params%
echo DEBUG=%DEBUG%
echo PATCHFSTAB=%PATCHFSTAB%
echo GetUSBHPmodZ=%GetUSBHPmodZ%
echo RAMDISKIMG=%RAMDISKIMG%
echo restore=%restore%
echo InstallKernelModules=%InstallKernelModules%
echo InstallPrebuiltKernelModules=%InstallPrebuiltKernelModules%
echo ListAllAVDs=%ListAllAVDs%
echo InstallApps=%InstallApps%
echo NOPARAMSATALL=%NOPARAMSATALL%
)
IF NOT %InstallApps% (
REM If there is no file to work with, abort the script
IF "%1" == "" (
call :ShowHelpText && exit /B 0
)
IF %ListAllAVDs% (
call :ShowHelpText && exit /B 0
)
IF NOT exist "%ANDROIDHOME%%1" (
echo file %1 not found && exit /B 0
)
)
REM Set Folders and FileNames
echo [*] Set Directorys
set AVDPATHWITHRDFFILE=%ANDROIDHOME%%1
for /F "delims=" %%i in ("%AVDPATHWITHRDFFILE%") do (
set AVDPATH=%%~dpi
set RDFFILE=%%~nxi
)
REM If we can CD into the ramdisk.img, it is not a file!
cd %AVDPATHWITHRDFFILE% >nul 2>&1
IF "%ERRORLEVEL%"=="0" (
call :ShowHelpText && exit /B 0
)
IF %restore% (
call :restore_backups && exit /B 0
)
call :TestADB
REM The Folder where the script was called from
set ROOTAVD=%cd%
set MAGISKZIP=%ROOTAVD%\Magisk.zip
REM Kernel Names
set BZFILE=%ROOTAVD%\bzImage
set KRFILE=kernel-ranchu
IF %InstallApps% (
call :installapps && exit /B 0
)
set ADBWORKDIR=/data/data/com.android.shell
set ADBBASEDIR=%ADBWORKDIR%/Magisk
echo [-] In any AVD via ADB, you can execute code without root in /data/data/com.android.shell
call :TestADBWORKDIR
REM change to ROOTAVD directory
cd %ROOTAVD%
echo [*] Cleaning up the ADB working space
adb shell rm -rf %ADBBASEDIR%
echo [*] Creating the ADB working space
adb shell mkdir %ADBBASEDIR%
echo [*] looking for Magisk installer Zip
IF NOT exist "%MAGISKZIP%" (
echo [-] Please download Magisk.zip file
) ELSE (
call :pushtoAVD "%MAGISKZIP%"
)
REM Proceed with ramdisk
set INITRAMFS=%ROOTAVD%\initramfs.img
IF %RAMDISKIMG% (
REM Is it a ramdisk named file?
echo.%RDFFILE% | findstr /I ramdisk.*.img >NUL || (
echo [!] please give a path to a ramdisk file
exit /B 0
)
call :create_backup %RDFFILE%
call :pushtoAVD "%AVDPATHWITHRDFFILE%" "ramdisk.img"
IF %InstallKernelModules% (
IF EXIST "%INITRAMFS%" (
call :pushtoAVD "%INITRAMFS%"
)
)
)
echo [-] Copy rootAVD Script into Magisk DIR
adb push rootAVD.sh %ADBBASEDIR%
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 "%ERRORLEVEL%"=="0" (
REM In Debug-Mode we can skip parts of the script
IF NOT %DEBUG% (
IF %RAMDISKIMG% (
call :pullfromAVD ramdiskpatched4AVD.img "%AVDPATHWITHRDFFILE%"
call :pullfromAVD Magisk.apk %ROOTAVD%\Apps\
call :pullfromAVD Magisk.zip
IF %InstallPrebuiltKernelModules% (
call :pullfromAVD %BZFILE%
call :InstallKernelModules
)
IF %InstallKernelModules% (
call :InstallKernelModules
)
echo [-] Clean up the ADB working space
adb shell rm -rf %ADBBASEDIR%
call :installapps
echo [-] Shut-Down and Reboot [Cold Boot Now] the AVD and see IF it worked
echo [-] Root and Su with Magisk for Android Studio AVDs
echo [-] Modded by NewBit XDA - Jan. 2021
echo [*] Huge Credits and big Thanks to topjohnwu, shakalaca and vvb2060
call :ShutDownAVD
)
)
)
exit /B %ERRORLEVEL%
:TestADBWORKDIR
echo [*] Testing the ADB working space
SetLocal EnableDelayedExpansion
set ADBWORKS=
adb shell cd %ADBWORKDIR% > tmpFile 2>&1
set /P ADBWORKS=<tmpFile
del tmpFile
echo.%ADBWORKS%| FIND /I "No such file or directory">Nul && (
echo [^^!] %ADBWORKDIR% is not available
call :_Exit 2> nul
)
echo [^^!] %ADBWORKDIR% is available
EndLocal
exit /B 0
:ShutDownAVD
SetLocal EnableDelayedExpansion
set ADBPULLECHO=
adb shell setprop sys.powerctl shutdown > tmpFile 2>&1
set /P ADBPULLECHO=<tmpFile
del tmpFile
echo.%ADBPULLECHO%| FIND /I "error">Nul || (
echo [-] Trying to shut down the AVD
)
echo [^^!] If the AVD doesnt shut down, try it manually^^!
EndLocal
exit /B 0
:InstallKernelModules
SetLocal EnableDelayedExpansion
IF EXIST "%BZFILE%" (
call :create_backup %KRFILE%
echo [*] Copy %BZFILE% ^(Kernel^) into kernel-ranchu
copy "%BZFILE%" "%AVDPATH%%KRFILE%" >Nul
IF "%ERRORLEVEL%"=="0" (
del "%BZFILE%" "%INITRAMFS%"
)
)
EndLocal
exit /B 0
:pullfromAVD
SetLocal EnableDelayedExpansion
set SRC=%1
set DST=%2
set ADBPULLECHO=
setlocal enableDelayedExpansion
for /f "delims=" %%i in ("!SRC!") do (
endlocal & REM
set "SRC=%%~nxi"
)
setlocal enableDelayedExpansion
for /f "delims=" %%i in ("!DST!") do (
endlocal & REM
set "DST=%%~nxi"
)
adb pull %ADBBASEDIR%/%SRC% %2 > tmpFile 2>&1
set /P ADBPULLECHO=<tmpFile
del tmpFile
echo.%ADBPULLECHO%| FIND /I "error">Nul || (
echo [*] Pull %SRC% into %DST%
echo [-] %ADBPULLECHO%
)
EndLocal
exit /B 0
:pushtoAVD
SetLocal EnableDelayedExpansion
set SRC=%1
set DST=%2
set ADBPUSHECHO=
setlocal enableDelayedExpansion
for /f "delims=" %%i in ("!SRC!") do (
endlocal & REM
set "SRC=%%~nxi"
)
setlocal enableDelayedExpansion
for /f "delims=" %%i in ("!DST!") do (
endlocal & REM
set "DST=%%~nxi"
)
IF "%DST%"=="" (
echo [*] Push %SRC% into %ADBBASEDIR%
adb push %1 %ADBBASEDIR% > tmpFile 2>&1
) ELSE (
echo [*] Push %SRC% into %ADBBASEDIR%/%DST%
adb push %1 %ADBBASEDIR%/%DST% > tmpFile 2>&1
)
set /P ADBPUSHECHO=<tmpFile
del tmpFile
echo [-] %ADBPUSHECHO%
ENDLOCAL
exit /B 0
:create_backup
SetLocal EnableDelayedExpansion
set FILE=%1
set BACKUPFILE=%FILE%.backup
REM If no backup file exist, create one
IF NOT EXIST "%AVDPATH%%BACKUPFILE%" (
echo [*] create Backup File
copy "%AVDPATH%%FILE%" "%AVDPATH%%BACKUPFILE%" >Nul
IF EXIST "%AVDPATH%%BACKUPFILE%" (
echo [-] Backup File was created
)
) ELSE (
echo [-] Backup exists already
)
ENDLOCAL
exit /B 0
:TestADB
SetLocal EnableDelayedExpansion
set ADB_DIR=""
set ADB_EX=""
echo [-] Test IF ADB SHELL is working
set ADBWORKS=
adb shell -n echo true > tmpFile 2>&1
set /P ADBWORKS=<tmpFile
del tmpFile
IF "%ADBWORKS%" == "true" (
echo [-] ADB connection possible
) ELSE (
echo.%ADBWORKS%| FIND /I "offline">Nul && (
echo [^^!] ADB device is offline
echo [*] no ADB connection possible
call :_Exit 2> nul
)
echo.%ADBWORKS%| FIND /I "unauthorized">Nul && (
echo [^^!] %ADBWORKS%
echo [*] no ADB connection possible
call :_Exit 2> nul
)
echo.%ADBWORKS%| FIND /I "recognized">Nul && (
IF EXIST "%ANDROIDHOME%%ADB_DIR_W%" (
set ADB_DIR=%ADB_DIR_W%
) ELSE (
echo [^^!] ADB not found, please install platform-tools and add it to your %%PATH%%
call :_Exit 2> nul
)
for /f "delims=" %%i in ('dir "%ANDROIDHOME%%ADB_DIR%adb.exe" /s /b /a-d') do (
set ADB_EX=%%i
)
IF "!ADB_EX!" == "" (
echo [^^!] ADB binary not found in %ENVVAR%\%ADB_DIR%
call :_Exit 2> nul
)
echo [^^!] ADB is not in your Path, try to
echo set PATH=%ENVVAR%\!ADB_DIR!;%%PATH%%
IF EXIST "!ADB_EX!" (
echo [*] setting it, just during this session, for you
set "PATH=%ANDROIDHOME%!ADB_DIR!;%PATH%"
REM goto :TestADB
call :TestADB
)
)
echo.%ADBWORKS%| FIND /I "error">Nul && (
echo [^^!] %ADBWORKS%
echo [*] no ADB connection possible
call :_Exit 2> nul
)
echo.%ADBWORKS%| FIND /I "no devices/emulators found">Nul && (
echo [^^!] %ADBWORKS%
echo [*] no ADB connection possible
call :_Exit 2> nul
)
)
IF EXIST "!ADB_EX!" (
ENDLOCAL & set "PATH=%PATH%"
) ELSE (
ENDLOCAL
)
exit /B 0
:restore_backups
for /f "delims=" %%i in ('dir "%AVDPATH%*.backup" /s /b /a-d') do (
echo [^!] Restoring %%~ni%%~xi to %%~ni
copy "%%i" "%%~di%%~pi%%~ni" >nul 2>&1
)
echo [*] Backups still remain in place
REM call :_Exit 2> nul
exit /B 0
:ProcessArguments
set params=%*
set DEBUG=%false%
set PATCHFSTAB=%false%
set GetUSBHPmodZ=%false%
set RAMDISKIMG=%false%
set restore=%false%
set InstallKernelModules=%false%
set InstallPrebuiltKernelModules=%false%
set ListAllAVDs=%false%
set InstallApps=%false%
set NOPARAMSATALL=%false%
REM While debugging and developing you can turn this flag on
echo.%params%| FIND /I "DEBUG">Nul && (
set DEBUG=%true%
REM Shows whatever line get executed...
REM echo on
)
REM Call rootAVD with PATCHFSTAB if you want the RAMDISK merge your modded fstab.ranchu before Magisk Mirror gets mounted
echo.%params%| FIND /I "PATCHFSTAB">Nul && (
set PATCHFSTAB=%true%
)
REM Call rootAVD with GetUSBHPmodZ to download the usbhostpermissons module
echo.%params%| FIND /I "GetUSBHPmodZ">Nul && (
set GetUSBHPmodZ=%true%
)
REM Call rootAVD with ListAllAVDs to show all AVDs with command examples
echo.%params%| FIND /I "ListAllAVDs">Nul && (
set ListAllAVDs=%true%
)
REM Call rootAVD with InstallApps to just install all APKs placed in the Apps folder
echo.%params%| FIND /I "InstallApps">Nul && (
set InstallApps=%true%
)
set RAMDISKIMG=%true%
IF "%2" == "restore" (
set restore=%true%
) ELSE IF "%2"=="InstallKernelModules" (
set InstallKernelModules=%true%
) ELSE IF "%2"=="InstallPrebuiltKernelModules" (
set InstallPrebuiltKernelModules=%true%
)
IF "%params%"=="" (
REM No Parameters SET at all
set NOPARAMSATALL=%true%
)
exit /B 0
:installapps
SetLocal EnableDelayedExpansion
echo [-] Install all APKs placed in the Apps folder
for %%i in (APPS\*.apk) do (
set APK=%%i
:whileloop
echo [*] Trying to install !APK!
for /f "delims=" %%A in ('adb install -r -d !APK! 2^>^&1' ) do (
echo [-] %%A
echo.%%A| FIND /I "INSTALL_FAILED_UPDATE_INCOMPATIBLE">Nul && (
set Package=
for %%p in (%%A) do (
echo.!Package!| FIND /I "Package">Nul && (
echo [*] Need to uninstall %%p first
adb uninstall %%p > tmpFile 2>&1
set /P ADBECHO=<tmpFile
del tmpFile
echo [-] !ADBECHO!
goto :whileloop
)
set Package=%%p
)
)
)
)
ENDLOCAL
exit /B 0
:ShowHelpText
echo rootAVD A Script to root AVD by NewBit XDA
echo.
echo Usage: rootAVD [DIR/ramdisk.img] [OPTIONS] ^| [EXTRA ARGUMENTS]
echo or: rootAVD [ARGUMENTS]
echo.
echo Arguments:
echo ListAllAVDs Lists Command Examples for ALL installed AVDs
echo.
echo InstallApps Just install all APKs placed in the Apps folder
echo.
echo Main operation mode:
echo DIR a path to an AVD system-image
echo - must always be the 1st Argument after rootAVD
echo.
echo ADB Path ^| Ramdisk DIR^| ANDROID_HOME:
echo [M]ac/Darwin: export PATH=~/Library/Android/sdk/platform-tools:^$PATH
echo export PATH=^$ANDROID_HOME/platform-tools:^$PATH
echo system-images/android-^$API/google_apis_playstore/x86_64/
echo.
echo [L]inux: export PATH=~/Android/Sdk/platform-tools:^$PATH
echo export PATH=^$ANDROID_HOME/platform-tools:^$PATH
echo system-images/android-^$API/google_apis_playstore/x86_64/
echo.
echo [W]indows: set PATH=%ENVVAR%\%ADB_DIR_W%;%%PATH%%
echo system-images\android-^$API\google_apis_playstore\x86_64\
echo.
echo ANDROID_HOME: By default, the script uses %%LOCALAPPDATA%%, to set its Android Home
echo directory, search for AVD system-images and ADB binarys. This behaviour
echo can be overwritten by setting the ANDROID_HOME variable.
echo e.g. set ANDROID_HOME=%%USERPROFILE%%\Downloads\sdk
echo.
echo ^$API: 25,29,30,31,32,33,34,UpsideDownCake,etc.
echo.
echo Options:
echo restore restore all existing .backup files, but doesn't delete them
echo - the AVD doesn't need to be running
echo - no other Argument after will be processed
echo.
echo InstallKernelModules install custom build kernel and its modules into ramdisk.img
echo - kernel (bzImage) and its modules (initramfs.img) are inside rootAVD
echo - both files will be deleted after installation
echo.
echo InstallPrebuiltKernelModules download and install an AOSP prebuilt kernel and its modules into ramdisk.img
echo - similar to InstallKernelModules, but the AVD needs to be online
echo.
echo Options are exclusive, only one at the time will be processed.
echo.
echo Extra Arguments:
echo DEBUG Debugging Mode, prevents rootAVD to pull back any patched file
echo.
echo PATCHFSTAB fstab.ranchu will get patched to automount Block Devices like /dev/block/sda1
echo - other entries can be added in the script as well
echo - a custom build Kernel might be necessary
echo.
echo GetUSBHPmodZ The USB HOST Permissions Module Zip will be downloaded into /sdcard/Download
echo.
echo FAKEBOOTIMG Creates a fake Boot.img file that can directly be patched from the Magisk APP
echo - Magisk will be launched to patch the fake Boot.img within 60s
echo - the fake Boot.img will be placed under /sdcard/Download/fakeboot.img
echo.
echo Extra Arguments can be combined, there is no particular order.
echo.
echo Notes: rootAVD will
echo - always create .backup files of ramdisk*.img and kernel-ranchu
echo - replace both when done patching
echo - show a Menu, to choose the Magisk Version (Stable ^|^| Canary ^|^| Alpha), if the AVD is online
echo - make the choosen Magisk Version to its local
echo - install all APKs placed in the Apps folder
call :FindSystemImages
exit /B 0
:GetANDROIDHOME
REM set PATH=%LOCALAPPDATA%\Android\Sdk\platform-tools;%PATH%
REM set ANDROID_HOME=%USERPROFILE%\Downloads\sdk
REM set ANDROID_HOME="%USERPROFILE%\Downloads\sd k"
REM set ANDROID_HOME=%USERPROFILE%\Downloads\sd k
REM set ANDROID_HOME=%USERPROFILE%\Downloads\Program Files (x86)\Android\android-sdk
REM set ANDROID_HOME="%USERPROFILE%\Downloads\Program Files (x86)\Android\android-sdk"
set NoSystemImages=%true%
REM Default: Looking for LOCALAPPDATA to seach AVD system-images
set ENVVAR=%%LOCALAPPDATA%%\Android\Sdk
set ANDROIDHOME=%LOCALAPPDATA%\Android\Sdk\
IF defined ANDROID_HOME (
set ENVVAR=%%ANDROID_HOME%%
setlocal enableDelayedExpansion
for /f "delims=" %%A in ("!ANDROID_HOME!") do (
endlocal & REM
set "ANDROID_HOME=%%~A"
set "ANDROIDHOME=%%~A\"
)
)
set SYSIM_DIR_W=system-images\
set ADB_DIR_W=platform-tools
IF EXIST "%ANDROIDHOME%%SYSIM_DIR_W%" (
set SYSIM_DIR=%SYSIM_DIR_W%
set NoSystemImages=%false%
)
exit /B 0
:FindSystemImages
echo - use %ENVVAR% to search for AVD system images
echo.
SetLocal EnableDelayedExpansion
set SYSIM_EX=
IF %NoSystemImages% (
echo Neither system-images nor ramdisk files could be found
exit /B 1
)
for /f "delims=" %%i in ('dir "%ANDROIDHOME%%SYSIM_DIR%ramdisk*.img" /s /b /a-d') do (
set "j=%%~i"
setlocal enableDelayedExpansion
for /f "delims=" %%a in ("!ANDROIDHOME!") do (
endlocal & REM
set "j=!j:%%a=!"
)
IF %ListAllAVDs% (
IF "!SYSIM_EX!" == "" (
set SYSIM_EX=!j!
) ELSE (
set SYSIM_EX=!j! !SYSIM_EX!
)
) ELSE (
set SYSIM_EX=!j!
)
)
echo Command Examples:
echo rootAVD.bat
echo rootAVD.bat ListAllAVDs
echo rootAVD.bat InstallApps
echo.
for %%i in (%SYSIM_EX%) do (
echo rootAVD.bat %%i
echo rootAVD.bat %%i FAKEBOOTIMG
echo rootAVD.bat %%i DEBUG PATCHFSTAB GetUSBHPmodZ
echo rootAVD.bat %%i restore
echo rootAVD.bat %%i InstallKernelModules
echo rootAVD.bat %%i InstallPrebuiltKernelModules
echo rootAVD.bat %%i InstallPrebuiltKernelModules GetUSBHPmodZ PATCHFSTAB DEBUG
echo.
)
ENDLOCAL
exit /B 0
:_Exit
IF %NOPARAMSATALL% (
cmd /k
)
()
goto :eof