-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDuck Tools.bat
1609 lines (1564 loc) · 49 KB
/
Duck Tools.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
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
@echo off
setlocal enableextensions enabledelayedexpansion
set AIK=AIK_3.8
set BIN=%AIK%\android_win_tools
set LETTERS=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set DIR=%~dp0
cd /d "!DIR!"
mkdir Output > nul 2>&1
title Duck Tools 1.0
>nul 2>&1 net session || (
echo Error.
echo.
echo This script uses some tools that require admin rights.
echo Run again as admin.
echo.
pause
exit
)
if not exist Files (
echo Error.
echo.
echo Files directory is missing.
echo Can not proceed without it.
echo Please, download Duck Patcher again.
echo.
pause
exit
)
adb version > nul 2>&1 || (
echo Error.
echo.
echo adb.exe is missing.
echo.
echo G. Get latest platform tools ^(adb and fastboot^) from Google
echo S. Select adb.exe directory
echo E. Exit
echo.
echo|set /p="Choose: " & choice /c gse /n
if !errorlevel!==1 (
echo.
echo Downloading platform tools...
certutil -urlcache -split -f https://dl.google.com/android/repository/platform-tools-latest-windows.zip > nul 2>&1 || (
echo Failed.
echo.
pause
exit
)
echo Done
echo.
echo Unpacking platform-tools-latest-windows.zip...
7z.exe x platform-tools-latest-windows.zip > nul 2>&1 || (
echo Failed.
echo.
pause
if exist platform-tools-latest-windows.zip del /f /q platform-tools-latest-windows.zip > nul 2>&1
exit
)
if not exist platform-tools (
echo Failed.
echo.
pause
if exist platform-tools-latest-windows.zip del /f /q platform-tools-latest-windows.zip > nul 2>&1
exit
)
echo Done.
echo.
if exist !SYSTEMDRIVE!\adb (
ren !SYSTEMDRIVE!\adb adb-old > nul 2>&1
mkdir !SYSTEMDRIVE!\adb
)
(
xcopy platform-tools\adb.exe !SYSTEMDRIVE!\adb\ /y /q > nul 2>&1
xcopy platform-tools\AdbWinApi.dll !SYSTEMDRIVE!\adb\ /y /q > nul 2>&1
xcopy platform-tools\AdbWinUsbApi.dll !SYSTEMDRIVE!\adb\ /y /q > nul 2>&1
xcopy platform-tools\fastboot.exe !SYSTEMDRIVE!\adb\ /y /q > nul 2>&1
) || (
if exist !SYSTEMDRIVE!\adb ren !SYSTEMDRIVE!\adb-old adb > nul 2>&1
if exist platform-tools rmdir /s /q platform-tools > nul 2>&1
if exist platform-tools-latest-windows.zip del /f /q platform-tools-latest-windows.zip > nul 2>&1
echo Failed.
echo.
pause
exit
)
set "ADB_DIR=!SYSTEMDRIVE!\adb"
if exist platform-tools-latest-windows.zip del /f /q platform-tools-latest-windows.zip > nul 2>&1
if exist platform-tools rmdir /s /q platform-tools > nul 2>&1
goto PLATFORM_JUMP
exit
) else if !errorlevel!==2 (
:PICK_ADB
for /F "delims=" %%A in ('powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.Filter = '|adb.exe'; $openFileDialog.Title = 'Select adb.exe'; $openFileDialog.InitialDirectory = '!DIR!'; $openFileDialog.ShowDialog() | Out-Null; $openFileDialog.FileName}"') do set "ADB_DIR=%%~dpA"
echo.
if not exist "!ADB_DIR!" (
echo|set /p="Do you want to try again? [y/n]: " & choice /c yn /n
if !errorlevel!==1 (
goto PICK_ADB
) else (
exit
)
)
:PLATFORM_JUMP
set "PATH=!PATH!;!ADB_DIR!"
echo|set /p="Do you want to set it system-wide? [y/n]: " & choice /c yn /n
if !errorlevel!==1 (
for /F "tokens=2,*" %%A in ('reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| find /i "Path"') do (
set "reg_path=%%B;!ADB_DIR!"
)
echo.
echo Adding adb path into PATH...
reg add "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /f /v Path /t REG_SZ /d "!reg_path!^">nul 2>&1&& (
echo Done.
echo Since you keep adb binary in this same path, you will not be prompted about it.
echo This change will take effect in the next logon.
) || (
echo Failed.
)
)
echo.
pause
goto ADB_DRIVER
) else if !errorlevel!==3 (
exit
)
)
:ADB_DRIVER
REM ADB driver check
reg query "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Class\{3f966bd9-fa04-4ec5-991c-d326973b5128}" /v "Class" | findstr /i /c:"AndroidUsbDeviceClass" > nul 2>&1 || (
echo Warning.
echo.
echo It seems adb driver is not installed yet.
echo.
echo|set /p="Do you want to install it now? [y/n]: " & choice /c yn /n
if !errorlevel!==1 (
echo.
echo Downloading usb_driver_r13-windows.zip...
certutil -urlcache -split -f https://dl.google.com/android/repository/usb_driver_r13-windows.zip > nul 2>&1 || (
echo Failed.
echo.
pause
exit
)
echo Done
echo.
echo.
echo Unpacking usb_driver_r13-windows.zip...
7z.exe x usb_driver_r13-windows.zip > nul 2>&1 || (
echo Failed.
echo.
pause
if exist usb_driver_r13-windows.zip del /f /q usb_driver_r13-windows.zip > nul 2>&1
exit
)
if not exist usb_driver (
echo Failed.
echo.
pause
if exist usb_driver_r13-windows.zip del /f /q usb_driver_r13-windows.zip > nul 2>&1
exit
)
echo Done
echo.
echo.
echo Installing Android USB Driver...
pnputil /i /a usb_driver\android_winusb.inf || (
echo Failed.
echo.
pause
if exist usb_driver_r13-windows.zip del /f /q usb_driver_r13-windows.zip
if exist usb_driver rmdir /s /q usb_driver > nul 2>&1
exit
)
if exist usb_driver_r13-windows.zip del /f /q usb_driver_r13-windows.zip
if exist usb_driver rmdir /s /q usb_driver > nul 2>&1
)
echo Done
echo.
timeout 3 /nobreak > nul 2>&1
)
call :MAIN
exit
::FUNCTIONS ############################################################################################################
:MAIN
call :CLEAN_TMP
cls
echo Main page
echo.
echo.
echo P. Patch recovery ^(Orange Fox recommended^)
echo S. Send DuckPart files to current recovery
echo D. Deploy Windows image ^(.img, .iso or .esd^)
echo R. Run DuckPart
echo G. Get Windows image
echo.
echo E. Exit
echo.
echo|set /p="Choose: " & choice /c psdrge /n
if !errorlevel!==1 (
call :PATCH_RECOVERY
) else if !errorlevel!==2 (
:SEND_DUCKPART
call :CONNECT_RECOVERY
cls
echo ^> S. Send DuckPart files to current recovery
echo.
echo Pushing files to device...
echo ______________________________________
adb shell mkdir /sbin/duckpartdir && (
set push_error=
adb push Files\dmsetup /sbin/ || set push_error=dmsetup
adb shell chmod 755 /sbin/dmsetup
adb push Files\duckpart /sbin/ || set push_error=!push_error!, duckpart
adb shell chmod 755 /sbin/duckpart
adb push Files\parted /sbin/ || set push_error=!push_error!, parted
adb shell chmod 755 /sbin/parted
adb push Files\sgdisk /sbin/ || set push_error=!push_error!, sgdisk
adb shell chmod 755 /sbin/sgdisk
adb push Files\toybox /sbin/duckpartdir/ || set push_error=!push_error!, toybox
adb push Files\busybox /sbin/duckpartdir/ || set push_error=!push_error!, busybox
adb shell chmod -R 755 /sbin/duckpartdir
echo ______________________________________
if not "!push_error!"=="" (
echo Error.
echo Could not push the following files: !push_error!
echo duckpart will not work this way.
)
if !return_to_deployment!==1 (
set return_to_deployment=0
call :DEPLOY_IMAGE
)
) || (
echo Error.
echo Could not create duckpart directory in /sbin
)
) else if !errorlevel!==3 (
call :DEPLOY_IMAGE
) else if !errorlevel!==4 (
cls
echo ^> R. Run DuckPart
echo.
echo.
echo 1. Open a new CMD window
echo 2. Run: adb shell
echo 3. In adb shell run: duckpart
echo.
echo It must be launched this way due some adb limitations.
echo Use it wisely.
) else if !errorlevel!==5 (
set site_error=
start https://worproject.com/esd || set site_error=1
cls
echo ^> G. Get Windows image
echo.
echo.
echo How to download:
echo.
if !site_error!==1 echo Access^: https^:^/^/worproject.com^/esd
echo Version^: Select Windows 11
echo Build^: Select the first one
echo Architecture^: Select ARM64
echo Edition^: Select CLIENT
echo Language^: Select a desired language
) else if !errorlevel!==6 (
exit
)
echo.
pause
call :MAIN
goto :EOF
:DEPLOY_IMAGE
set INDEXES=0
set "INDEXLIST="
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo.
echo.
echo|set /p="Do you want to put device in Mass Storage mode? [y/n]: " & choice /c yn /n
if !errorlevel! == 1 (
call :CONNECT_RECOVERY
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo.
echo.
adb shell ls /sbin/duckpart > nul 2>&1 || (
echo Could not locate duckpart on your device.
echo.
echo P. Patch recovery
echo S. Send duckpart Files
echo R. Return
echo.
echo|set /p="Choose: " & choice /c psr /n
if !errorlevel! == 1 (
call :PATCH_RECOVERY
) else if !errorlevel! == 2 (
set return_to_deployment=1
call :SEND_DUCKPART
) else if !errorlevel! == 3 (
call :MAIN
)
echo.
pause
call :MAIN
)
echo Be sure your recovery screen is unlocked before proceeding.
echo.
pause
echo.
adb shell duckpart msc > nul 2>&1
echo Done.
echo If there is no device drive on This PC^:
echo 1. Not all custom recoveries support USB Mass Storage mode.
echo 2. Maybe it^'s up to Windows USB^/MTP drivers.
echo 3. Try to switch MTP mode on recovery and use duckpart U option.
echo.
pause
)
cls
echo ^> D. Deploy Windows image (.img or .esd)
echo.
echo Step 1 - Select device Windows drive: RUNNING
echo Step 2 - Select device ESP drive: WAITING
echo Step 3 - Select an image: WAITING
echo.
echo.
:: Get PC Windows drive letter
for /F "skip=1 tokens=2 delims==:" %%a in ('wmic os get "SystemDrive" /value') do set "HOSTDRIVE=%%a"
:: Check if HOSTDRIVE exists
if not exist !HOSTDRIVE!: (
echo.
echo Couldn't find PC Windows drive.
echo.
pause
call :MAIN
)
:: Open Computer screen to help use get drive letters
explorer ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
:: Ask user if he can see Windows device drive letter
echo|set /p="Is device Windows partition visible on This PC? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
:ENTER_WIN_LETTER_TO_ASSIGN
echo.
echo|set /p="Enter an available drive letter to assign to device Windows volume: " & choice /c !LETTERS! /n
call :NUMBER2LETTER WINDRIVE !errorlevel!
echo.
if exist !WINDRIVE!: (
echo !WINDRIVE!: is already in use.
echo Please, be careful next time.
call :ENTER_WIN_LETTER_TO_ASSIGN
)
set VOL=0
set "NUMVOLS="
echo Now you must be pretty sure about what you are doing.
echo Do not select an incorrect volume number.
echo.
for /F "tokens=*" %%a in ('echo list volume ^| diskpart ^| findstr "Volume "') do (
echo %%a
set /a VOL+=1
)
set /a VOL-=2
for /L %%a in (0,1,!VOL!) do (
set "NUMVOLS=!NUMVOLS!%%a"
)
:ASSIGN_WINDOWS_LETTER
echo.
echo|set /p="Select the device Windows volume number: " & choice /c !NUMVOLS! /n
set SELVOL=!errorlevel!
set /a SELVOL-=1
echo.
echo|set /p="Are you sure device Windows volume is !SELVOL!? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
call :ASSIGN_WINDOWS_LETTER
)
echo.
(
echo select volume !SELVOL!
echo assign letter !WINDRIVE!
) > diskpart_script.tmp
echo Assigning Volume !SELVOL! as !WINDRIVE!:...
diskpart /s diskpart_script.tmp >nul 2>&1 && (
echo Done.
) || (
del /f /q diskpart_script.tmp >nul 2>&1
echo Error.
echo.
pause
call :MAIN
)
del diskpart_script.tmp
) else if !errorlevel!==1 (
set DRIVES_LIST=
set DRIVES_LIST_CHOICE=
for /l %%i in (0,1,26) do (
set /a "SEQUENCE=%%i + 1"
set "LETTER=!LETTERS:~%%i,1!"
if exist !LETTER!: (
set DRIVES_LIST=!DRIVES_LIST! !LETTER!
set DRIVES_LIST_CHOICE=!DRIVES_LIST_CHOICE!!LETTER!
)
)
echo.
echo|set /p="Enter device Windows drive letter: " & choice /c !DRIVES_LIST_CHOICE! /n
set WIN_DRIVE_NUMBER=!errorlevel!
set ROUND=1
for %%i in (!DRIVES_LIST!) do (
if !ROUND!==!WIN_DRIVE_NUMBER! (
set WINDRIVE=%%i
)
set /a ROUND+=1
)
)
:: Check if user chose PC Windows to install wim file
if /I !WINDRIVE!==!HOSTDRIVE! (
echo.
echo !HOSTDRIVE!: is already being used by PC Windows.
echo Pay more attention next time.
echo.
pause
call :MAIN
)
:: Check Win drive letter chose by user is empty
dir !WINDRIVE!:\* >nul 2>&1 && (
echo.
echo Device Windows drive !WINDRIVE!^: is not empty.
echo.
echo|set /p="Do you want to format it? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
call :MAIN
) else (
echo.
echo Formatting device Windows drive !WINDRIVE!^:...
echo select volume !WINDRIVE! > diskpart_script.tmp
for /F "tokens=2 delims==" %%a in ('wmic logicaldisk where "DeviceID='!WINDRIVE!:'" get VolumeName /value ^| findstr "="') do (
echo format quick fs=ntfs label=^"%%a^" >> diskpart_script.tmp
)
echo exit >> diskpart_script.tmp
echo ______________________________________
diskpart /s diskpart_script.tmp && (
echo ______________________________________
echo Done.
) || (
echo ______________________________________
echo Failed.
echo Format it using duckpart and try again.
echo.
pause
if exist diskpart_script.tmp del diskpart_script.tmp
call :MAIN
)
if exist diskpart_script.tmp del diskpart_script.tmp
)
)
cls
echo ^> D. Deploy Windows image (.img or .esd)
echo.
echo Step 1 - Select device Windows drive: !WINDRIVE!
echo Step 2 - Select device ESP drive: RUNNING
echo Step 3 - Select an image: WAITING
echo.
echo.
:: Ask user if he can see device ESP drive letter
echo|set /p="Is device ESP partition visible on My PC? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
:ENTER_ESP_LETTER_TO_ASSIGN
echo.
echo|set /p="Enter an available drive letter to assign to device ESP volume: " & choice /c !LETTERS! /n
call :NUMBER2LETTER ESPDRIVE !errorlevel!
echo.
if exist !ESPDRIVE!: (
echo !ESPDRIVE!: is already in use.
echo Please, be careful next time.
call :ENTER_ESP_LETTER_TO_ASSIGN
)
set VOL=0
set "NUMVOLS="
echo Now you must be pretty sure about what you are doing.
echo Do not select an incorrect volume number.
echo.
for /F "tokens=*" %%a in ('echo list volume ^| diskpart ^| findstr "Volume "') do (
echo %%a
set /a VOL+=1
)
set /a VOL-=2
for /L %%a in (0,1,!VOL!) do (
set "NUMVOLS=!NUMVOLS!%%a"
)
:ASSIGN_ESP_LETTER
echo.
echo|set /p="Select the device ESP volume number: " & choice /c !NUMVOLS! /n
set SELVOL=!errorlevel!
set /a SELVOL-=1
echo.
echo|set /p="Are you sure device ESP volume is !SELVOL!? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
call :ASSIGN_ESP_LETTER
)
echo.
(
echo select volume !SELVOL!
echo assign letter !ESPDRIVE!
) > diskpart_script.tmp
echo Assigning Volume !SELVOL! as !ESPDRIVE!^:...
diskpart /s diskpart_script.tmp >nul 2>&1 && (
echo Done.
) || (
del /f /q diskpart_script.tmp > nul 2>&1
echo Error.
echo.
pause
call :MAIN
)
if exist diskpart_script.tmp del /f /q diskpart_script.tmp > nul 2>&1
) else if !errorlevel!==1 (
set DRIVES_LIST=
set DRIVES_LIST_CHOICE=
for /l %%i in (0,1,26) do (
set /a "SEQUENCE=%%i + 1"
set "LETTER=!LETTERS:~%%i,1!"
if exist !LETTER!: (
set DRIVES_LIST=!DRIVES_LIST! !LETTER!
set DRIVES_LIST_CHOICE=!DRIVES_LIST_CHOICE!!LETTER!
)
)
echo.
echo|set /p="Enter device ESP drive letter: " & choice /c !DRIVES_LIST_CHOICE! /n
set ESP_DRIVE_NUMBER=!errorlevel!
set ROUND=1
for %%i in (!DRIVES_LIST!) do (
if !ROUND!==!ESP_DRIVE_NUMBER! (
set ESPDRIVE=%%i
)
set /a ROUND+=1
)
)
:: Check if user chose PC Windows to install wim file
if /I !ESPDRIVE!==!HOSTDRIVE! (
echo.
echo !HOSTDRIVE!^: is already being used by PC Windows.
echo Pay more attention next time.
echo.
pause
call :MAIN
)
:: Check ESP drive is empty
dir !ESPDRIVE!:\* >nul 2>&1 && (
echo.
echo Device ESP drive !ESPDRIVE!^: is not empty.
echo.
echo|set /p="Do you want to format it using diskpart? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
call :MAIN
) else (
echo.
echo Formatting device ESP drive !ESPDRIVE!^:...
echo select volume !ESPDRIVE! > diskpart_script.tmp
for /F "tokens=2 delims==" %%a in ('wmic logicaldisk where "DeviceID='!ESPDRIVE!:'" get VolumeName /value ^| findstr "="') do (
echo format quick fs=fat32 label=^"%%a^" >> diskpart_script.tmp
)
echo exit >> diskpart_script.tmp
echo ______________________________________
diskpart /s diskpart_script.tmp && (
echo ______________________________________
echo Done.
) || (
echo ______________________________________
echo Failed.
echo Format it using duckpart and try again.
echo.
pause
if exist diskpart_script.tmp del /f /q diskpart_script.tmp > nul 2>&1
call :MAIN
)
if exist diskpart_script.tmp del /f /q diskpart_script.tmp > nul 2>&1
)
)
:: Check if ESP drive chose is the same device Windows letter
if /I !ESPDRIVE!==!WINDRIVE! (
echo.
echo Windows and ESP can't be in same partition.
echo Pay more attention next time.
echo.
pause
call :MAIN
)
:IMAGE_FILE_SELECTION
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo.
echo Step 1 - Select device Windows drive: !WINDRIVE!
echo Step 2 - Select device ESP drive: !ESPDRIVE!
echo Step 3 - Select an image: WAITING
echo.
echo.
echo Now press any key to pick the Windows image file to be deployed.
echo.
pause
for /F "delims=" %%a in ('powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.Filter = 'Windows Imaging Format (.wim; .esd; .iso)|*.wim;*.esd;*.iso'; $openFileDialog.Title = 'Select a image file'; $openFileDialog.InitialDirectory = '!DIR!'; $openFileDialog.ShowDialog() | Out-Null; $openFileDialog.FileName}"') do set "WIMFILE=%%a"
if not exist "!WIMFILE!" (
echo.
echo It seems you didn't select any file.
:ASK_IMG_SEL
echo.
echo|set /p="Do you want to try again? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
call :MAIN
)
goto IMAGE_FILE_SELECTION
)
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo.
echo Step 1 - Select device Windows drive: !WINDRIVE!
echo Step 2 - Select device ESP drive: !ESPDRIVE!
echo Step 3 - Select an image: WAITING
echo.
echo.
if /I "!WIMFILE:~-3!"=="iso" (
for /F "delims=" %%A in ('7z l !WIMFILE! ^| find "sources" ^| findstr /i "install."') do (
set "TEST_WIM_NAME=%%~nxA"
if "!TEST_WIM_NAME:~-4!"==".wim" set "WIM_NAME=%%~nxA"
if "!TEST_WIM_NAME:~-4!"==".esd" set "WIM_NAME=%%~nxA"
)
if "!WIM_NAME!"=="" (
echo Invalid file.
goto ASK_IMG_SEL
)
7z l !WIMFILE! | findstr /C:"sources\!WIM_NAME!" > nul 2>&1 || (
echo Invalid file.
goto ASK_IMG_SEL
)
echo.
echo Extracting !WIM_NAME! file from .iso file...
7z e !WIMFILE! sources\!WIM_NAME! -o!DIR! > nul 2>&1 && (
echo Done.
echo.
echo.
set "WIMFILE=!WIM_NAME!"
set INDEX=1
timeout 2 /nobreak > nul 2>&1
) || (
echo Failed.
goto ASK_IMG_SEL
)
)
echo Listing Windows products index...
echo.
dism /Get-WimInfo /WimFile:"!WIMFILE!"
echo.
echo.
for /F "tokens=2 delims=:" %%A in ('dism /Get-WimInfo /WimFile:"!WIMFILE!"') do (
set /a INDEXES+=1
)
set /a INDEXES=(INDEXES-2)/4
if !INDEXES! GTR 1 (
for /L %%i in (1,1,!INDEXES!) do set "INDEXLIST=!INDEXLIST!%%i"
echo|set /p="Choose a product to install [1 to !INDEXES!]: " & choice /c !INDEXLIST! /n
set INDEX=!errorlevel!
) else (
set INDEX=1
)
set "line_number=0"
set exit_gin=0
for /F "tokens=*" %%a in ('dism /Get-WimInfo /WimFile:"!WIMFILE!"') do (
set /a line_number+=1
set "line=%%a"
set "line=!line:*:=!"
if !exit_gin!==1 (
set "INDEX_PRODUCT=!line!"
goto exit_gin
)
if "!line!"==" !INDEX!" set exit_gin=1
)
:exit_gin
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo.
echo.
echo Device Windows drive: !WINDRIVE!:
echo Device ESP drive: !ESPDRIVE!:
echo Windows image file: !WIMFILE!
echo Windows product: !INDEX_PRODUCT!
echo.
echo|set /p="Proceed with deployment? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
call :MAIN
)
echo.
echo.
echo Deploying Windows image into !WINDRIVE!: drive...
echo I'll notify you when it get done.
echo.
dism /apply-image /ImageFile:!WIMFILE! /index:!INDEX! /ApplyDir:!WINDRIVE!:\ && (
echo Done.
msg * Windows deployment done.
) || (
echo Failed.
echo Check the log.
msg * Windows deployment failed.
echo.
pause
if exist !WIM_NAME! del /f /q !WIM_NAME! > nul 2>&1
call :MAIN
)
echo.
echo.
echo Creating boot entry on device ESP partition...
bcdboot !WINDRIVE!:\Windows /s !ESPDRIVE!: /f UEFI > nul 2>&1 && (
echo Done.
) || (
echo Failed.
echo Check the screen.
echo.
pause
if exist !WIM_NAME! del /f /q !WIM_NAME! > nul 2>&1
call :MAIN
)
reg query "HKLM\WOASYSTEM" >nul 2>&1
if not !errorlevel!==0 reg load HKLM\WOASYSTEM !WINDRIVE!:\Windows\System32\config\System > nul 2>&1
reg query "HKLM\WOASOFTWARE" >nul 2>&1
if not !errorlevel!==0 reg load HKLM\WOASOFTWARE !WINDRIVE!:\Windows\System32\config\SOFTWARE > nul 2>&1
REM Set some default values
reg add "HKLM\WOASYSTEM\ControlSet001\Control\USB" /v OsDefaultRoleSwitchMode /t REG_DWORD /d 1 /f >nul 2>&1
reg add "HKLM\WOASOFTWARE\Microsoft\Windows\CurrentVersion\OOBE" /v BypassNRO /t REG_DWORD /d 1 /f >nul 2>&1
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} testsigning on >nul 2>&1
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} nointegritychecks on >nul 2>&1
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} recoveryenabled no >nul 2>&1
reg add "HKLM\WOASYSTEM\ControlSet001\Control\Power" /v HibernateEnabled /t REG_DWORD /d 0 /f >nul 2>&1
reg add "HKLM\WOASYSTEM\ControlSet001\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /f >nul 2>&1
:REGISTRY_OPTIONS
bcdedit /store %ESPDRIVE%:\EFI\Microsoft\BOOT\BCD /enum {default} > bcdedit.tmp
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo ^'-^> Post-installation set up
echo.
echo.
REM 1. USB fix
reg query "HKLM\WOASYSTEM\ControlSet001\Control\USB" /v OsDefaultRoleSwitchMode >nul 2>&1
if !errorlevel!==0 (
for /F "tokens=3" %%a in ('reg query "HKLM\WOASYSTEM\ControlSet001\Control\USB" /v "OsDefaultRoleSwitchMode" 2^>nul ^| findstr /i /c:"OsDefaultRoleSwitchMode"') do (
if "%%a"=="0x1" (
echo U. USB host mode enforcement is^: ON ^(OsDefaultRoleSwitchMode^)
set usb_fix=1
) else (
echo U. USB host mode enforcement is^: OFF ^(OsDefaultRoleSwitchMode^)
set usb_fix=0
)
)
) else (
echo echo U. USB host mode enforcement is^: OFF ^(OsDefaultRoleSwitchMode^)
set usb_fix=0
)
echo If your device has driverless USB support, set this ON.
echo.
REM 2. OOBE bypass
reg query "HKLM\WOASOFTWARE\Microsoft\Windows\CurrentVersion\OOBE" /v "BypassNRO" >nul 2>&1
if !errorlevel!==0 (
for /F "tokens=3" %%a in ('reg query "HKLM\WOASOFTWARE\Microsoft\Windows\CurrentVersion\OOBE" /v "BypassNRO" 2^>nul ^| findstr /i /c:"BypassNRO"') do (
if "%%a"=="0x1" (
echo O. Bypass Windows Out of Box Experience is^: ON ^(OOBE bypass^)
set oobe_fix=1
) else (
echo O. Bypass Windows Out of Box Experience is^: OFF ^(OOBE bypass^)
set oobe_fix=0
)
)
) else (
echo O. Bypass Windows Out of Box Experience is^: OFF ^(OOBE bypass^)
set oobe_fix=0
)
echo If your device doesn^'t have any internet connection ^(wi-fi or internet connection through USB^), set this ON.
echo.
REM 3. Driver signature check
findstr /c:"testsigning" bcdedit.tmp | findstr /c:"Yes" > nul && (
echo D. Allow unsigned drivers^: Yes ^(testsigning^)
set test_signing=1
) || (
echo D. Allow unsigned drivers^: No ^(testsigning^)
set test_signing=0
)
echo If your device uses any unsigned driver, set it Yes.
echo.
REM 4. System files integrity test
findstr /c:"nointegritychecks" bcdedit.tmp | findstr /c:"Yes" > nul && (
echo S. System files integrity check^: No ^(nointegritychecks^)
set no_integrity_checks=1
) || (
echo S. System files integrity check^: Yes ^(nointegritychecks^)
set no_integrity_checks=0
)
echo If your device uses any unsigned driver, set it No.
echo.
REM 5. Recovery capabilities
findstr /c:"recoveryenabled" bcdedit.tmp | findstr /c:"No" > nul && (
echo R. Recovery status is^: OFF ^(recoveryenabled^)
set recovery_enabled=0
) || (
echo R. Recovery status is^: ON ^(recoveryenabled^)
set recovery_enabled=1
)
echo Sometimes Windows breaks UFS GPT layout on trying to fix a boot failure. I recommend set it OFF.
echo.
REM 6. Hibernation
reg query "HKLM\WOASYSTEM\ControlSet001\Control\Power" /v HibernateEnabled >nul 2>&1
if !errorlevel!==0 (
for /F "tokens=3" %%a in ('reg query "HKLM\WOASYSTEM\ControlSet001\Control\Power" /v "HibernateEnabled" 2^>nul ^| findstr /i /c:"HibernateEnabled"') do (
if "%%a"=="0x1" (
echo H. Hibernation is^: ON ^(HibernateEnabled^)
set hibernate_enabled=1
) else (
echo H. Hibernation is^: OFF ^(HibernateEnabled^)
set hibernate_enabled=0
)
)
) else (
echo echo H. Hibernation is^: OFF ^(HibernateEnabled^)
set hibernate_enabled=0
)
echo No hint this time. I suggest let it OFF.
echo.
REM 7. Fast startup
reg query "HKLM\WOASYSTEM\ControlSet001\Control\Session Manager\Power" /v HiberbootEnabled >nul 2>&1
if !errorlevel!==0 (
for /F "tokens=3" %%a in ('reg query "HKLM\WOASYSTEM\ControlSet001\Control\Session Manager\Power" /v "HiberbootEnabled" 2^>nul ^| findstr /i /c:"HiberbootEnabled"') do (
if "%%a"=="0x1" (
echo F. Fast Startup is^: ON ^(HiberbootEnabled^)
set faststartup_enabled=1
) else (
echo F. Fast Startup is^: OFF ^(HiberbootEnabled^)
set faststartup_enabled=0
)
)
) else (
echo echo F. Fast Startup is^: OFF ^(HiberbootEnabled^)
set faststartup_enabled=0
)
echo Set it OFF if you use WoA Helper.
echo.
REM 8. Import file
echo I. Import a .reg file.
echo.
echo P. Proceed
echo Just proceed ^:^)
echo.
echo|set /p="Choose: " & choice /c uodsrhfip /n
if !errorlevel!==1 (
if !usb_fix!==1 (
reg add "HKLM\WOASYSTEM\ControlSet001\Control\USB" /v OsDefaultRoleSwitchMode /t REG_DWORD /d 6 /f >nul 2>&1 && set usb_fix=0
) else (
reg add "HKLM\WOASYSTEM\ControlSet001\Control\USB" /v OsDefaultRoleSwitchMode /t REG_DWORD /d 1 /f >nul 2>&1 && set usb_fix=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==2 (
if !oobe_fix!==1 (
reg add "HKLM\WOASOFTWARE\Microsoft\Windows\CurrentVersion\OOBE" /v BypassNRO /t REG_DWORD /d 0 /f >nul 2>&1 && set oobe_fix=0
) else (
reg add "HKLM\WOASOFTWARE\Microsoft\Windows\CurrentVersion\OOBE" /v BypassNRO /t REG_DWORD /d 1 /f >nul 2>&1 && set oobe_fix=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==3 (
echo.
echo Please wait...
if !test_signing!==1 (
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} testsigning off >nul 2>&1 && set test_signing=0
) else (
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} testsigning on >nul 2>&1 && set test_signing=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==4 (
echo.
echo Please wait...
if !no_integrity_checks!==1 (
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} nointegritychecks off >nul 2>&1 && set no_integrity_checks=0
) else (
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} nointegritychecks on >nul 2>&1 && set no_integrity_checks=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==5 (
echo.
echo Please wait...
if !recovery_enabled!==1 (
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} recoveryenabled no >nul 2>&1 && set recovery_enabled=0
) else (
bcdedit /store !ESPDRIVE!:\EFI\Microsoft\BOOT\BCD /set {default} recoveryenabled yes >nul 2>&1 && set recovery_enabled=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==6 (
echo.
echo Please wait...
if !hibernate_enabled!==1 (
reg add "HKLM\WOASYSTEM\ControlSet001\Control\Power" /v HibernateEnabled /t REG_DWORD /d 0 /f >nul 2>&1 && set hibernate_enabled=0
) else (
reg add "HKLM\WOASYSTEM\ControlSet001\Control\Power" /v HibernateEnabled /t REG_DWORD /d 1 /f >nul 2>&1 && set hibernate_enabled=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==7 (
echo.
echo Please wait...
if !faststartup_enabled!==1 (
reg add "HKLM\WOASYSTEM\ControlSet001\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /f >nul 2>&1 && set faststartup_enabled=0
) else (
reg add "HKLM\WOASYSTEM\ControlSet001\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 1 /f >nul 2>&1 && set faststartup_enabled=1
)
goto REGISTRY_OPTIONS
) else if !errorlevel!==8 (
:PICK_REG_FILE
for /F "delims=" %%I in ('powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms; $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $openFileDialog.Filter = 'Registry file (.reg)|*.reg'; $openFileDialog.Title = 'Select a registry file'; $openFileDialog.InitialDirectory = '!DIR!'; $openFileDialog.ShowDialog() | Out-Null; $openFileDialog.FileName}"') do set "REG_FILE=%%I"
if not exist "!REG_FILE!" (
echo.
echo It seems you didn't select any file.
echo|set /p="Do you want to try again? [y/n]: " & choice /c yn /n
if !errorlevel!==1 (
goto PICK_REG_FILE
)
)
for %%a in ("!REG_FILE!") do (
set "REG_FILE_NAME=%%~nxa"
set "REG_FILE_NAME_FIXED=Fixed-%%~nxa"
)
cls
echo ^> D. Deploy Windows image ^(.img, .iso or .esd^)
echo ^'-^> Post-installation set up
echo ^'-^> I. Import a .reg file.
echo.
echo.
echo !REG_FILE_NAME! content:
echo ______________________________________
echo.
type "!REG_FILE!"
echo.
echo ______________________________________
echo.
echo.
echo|set /p="Import this file into device Windows? [y/n]: " & choice /c yn /n
if !errorlevel!==2 (
goto REGISTRY_OPTIONS
)
(
for /F "tokens=*" %%a in ('type "!REG_FILE!"') do (
set "line=%%a"
set "line=!line:HKEY_LOCAL_MACHINE\System=HKEY_LOCAL_MACHINE\WOASYSTEM!"
set "line=!line:HKEY_LOCAL_MACHINE\Software=HKEY_LOCAL_MACHINE\WOASOFTWARE!"
echo(!line!
)
) > "!REG_FILE_NAME_FIXED!"
set reg_error=0
for /F "usebackq delims=" %%a in ("!REG_FILE_NAME_FIXED!") do (
echo %%a | findstr /C:"HKEY_LOCAL_MACHINE\System" >nul && set reg_error=1
echo %%a | findstr /C:"HKEY_LOCAL_MACHINE\Software" >nul && set reg_error=1
)
echo.
if !reg_error!==1 (
echo Could not load !REG_FILE_NAME!.
) else (
echo Importing !REG_FILE_NAME!...
regedit /s !REG_FILE_NAME_FIXED! > nul 2>&1 && (
echo Done.
) || (
echo Failed.
)
)
echo.
pause
if exist !REG_FILE_NAME_FIXED! del /f /q !REG_FILE_NAME_FIXED!
goto REGISTRY_OPTIONS
)
reg unload HKLM\WOASYSTEM > nul 2>&1 || (
echo.
echo Could not unload device Windows System hive.