-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrepair_boot.cmd
More file actions
588 lines (500 loc) · 16.6 KB
/
repair_boot.cmd
File metadata and controls
588 lines (500 loc) · 16.6 KB
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
@echo off
setlocal enabledelayedexpansion
echo +============================================+
echo + Windows Recovery Environment Repair Script +
echo +============================================+
echo + Version 2.0 +
echo +============================================+
echo.
:: Check if running in Windows RE
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinPE" >nul 2>&1
if %errorlevel% neq 0 (
echo This script must be run in Windows Recovery Environment.
echo Please boot into Windows RE and try again.
timeout /t 10 /nobreak
exit /b 1
)
:: Find Windows installation using improved detection
set "WINDOWS_DRIVE="
set "windir="
echo Detecting Windows installation...
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W Y Z) do (
if exist "%%d:\Windows\System32\config\SYSTEM" (
set "WINDOWS_DRIVE=%%d:"
set "windir=%%d:\Windows"
echo Found Windows installation on %%d:
goto found_windows
)
)
:found_windows
if not defined WINDOWS_DRIVE (
echo Could not find Windows installation.
echo Please ensure Windows is installed and accessible.
timeout /t 10 /nobreak
exit /b 1
)
:: Detect system architecture and boot mode
call :detect_system_info
:menu
cls
echo =====================================
echo Windows installation: %WINDOWS_DRIVE%
echo Boot mode: %BOOT_MODE%
echo System type: %SYSTEM_ARCH%
echo =====================================
echo Choose repair operations to perform:
echo 1) Run CHKDSK Disk Repair
echo 2) Run SFC and DISM Health Check and Repair
echo 3) Run Startup Repair
echo 4) Run Memory Diagnostic
echo 5) Run ALL Repairs (Recommended)
echo 6) Advanced Boot Repair
echo 7) Exit
echo.
set /p choice="Enter your choice (1-7): "
if "%choice%"=="1" goto chkdskscan
if "%choice%"=="2" goto sfcdism
if "%choice%"=="3" goto startup
if "%choice%"=="4" goto memory
if "%choice%"=="5" goto all
if "%choice%"=="6" goto advanced_boot
if "%choice%"=="7" goto end
echo Invalid choice. Please try again.
timeout /t 2 /nobreak
goto menu
:all
echo Running comprehensive repair sequence...
echo This may take 30-60 minutes depending on system size.
echo.
goto chkdskscan
:chkdskscan
echo.
echo ===== DISK REPAIR =====
echo Running CHKDSK on %WINDOWS_DRIVE%...
echo This may take a while...
:: Get file system type
for /f "tokens=4 delims=: " %%A in ('fsutil fsinfo volumeinfo %WINDOWS_DRIVE%\^|find "File System Name"') do (
set "FS_TYPE=%%A"
)
echo File system: %FS_TYPE%
echo %FS_TYPE% | findstr /i /r "^FAT" >nul
if !errorlevel! equ 0 (
echo Repairing FAT file system...
chkdsk "%WINDOWS_DRIVE%" /r /x >nul 2>&1
if !errorlevel! neq 0 echo Warning: CHKDSK reported errors.
) else (
echo Repairing NTFS file system...
chkdsk "%WINDOWS_DRIVE%" /r /x >nul 2>&1
if !errorlevel! neq 0 echo Warning: CHKDSK reported errors.
echo Cleaning up metadata and unallocated space...
chkdsk "%WINDOWS_DRIVE%" /sdcleanup >nul 2>&1
if !errorlevel! neq 0 echo Warning: CHKDSK reported errors.
)
echo Disk repair completed.
if "%choice%"=="5" goto sfcdism
goto menu
:sfcdism
echo.
echo ===== SYSTEM FILE REPAIR =====
echo Running SFC and DISM health check and repair...
echo This may take 20-40 minutes...
set "HAS_CORRUPTION=0"
:: Use Windows RE RAM drive (X:) for temp files
set "TEMP=X:"
set "TEMP_FILE_1=%TEMP%\dism_checkhealth_%RANDOM%_%RANDOM%.txt"
set "TEMP_FILE_2=%TEMP%\dism_scanhealth_%RANDOM%_%RANDOM%.txt"
echo.
echo Checking for corruption flags in the Windows image...
dism /image:%WINDOWS_DRIVE%\ /cleanup-image /checkhealth > "%TEMP_FILE_1%" 2>&1
set "DISM_ERROR=%errorlevel%"
if !DISM_ERROR! neq 0 (
echo Failed to execute DISM checkhealth command. Error code: !DISM_ERROR!
echo This may indicate access issues or missing files.
) else (
findstr /c:"No component store corruption detected" "%TEMP_FILE_1%" >nul
set "FIND_ERROR=!errorlevel!"
if !FIND_ERROR! neq 0 (
set HAS_CORRUPTION=1
echo Corruption flags detected in Windows image.
) else (
echo No corruption flags found in Windows image.
)
)
echo Checking for corruption in the Windows image...
dism /image:%WINDOWS_DRIVE%\ /cleanup-image /scanhealth > "%TEMP_FILE_2%" 2>&1
set "DISM_ERROR=%errorlevel%"
if !DISM_ERROR! neq 0 (
echo Failed to execute DISM scanhealth command. Error code: !DISM_ERROR!
echo This may indicate access issues or missing files.
) else (
findstr /c:"No component store corruption detected" "%TEMP_FILE_2%" >nul
set "FIND_ERROR=!errorlevel!"
if !FIND_ERROR! neq 0 (
set HAS_CORRUPTION=1
echo Corruption detected in Windows image.
) else (
echo No corruption found in Windows image.
)
)
del "%TEMP_FILE_1%" >nul 2>&1
del "%TEMP_FILE_2%" >nul 2>&1
if !HAS_CORRUPTION! equ 1 (
echo Corruption detected, restoring health of the Windows image...
dism /image:%WINDOWS_DRIVE%\ /cleanup-image /restorehealth >nul 2>&1
if !errorlevel! neq 0 (
echo Failed to restore the health of the Windows image. Error code: !errorlevel!
) else (
echo DISM repair completed successfully.
)
) else (
echo Windows image is healthy.
)
echo.
echo Checking integrity of protected system files...
sfc /scannow /offbootdir=%WINDOWS_DRIVE%\ /offwindir=%windir% >nul 2>&1
if !errorlevel! neq 0 (
echo Failed to check integrity of all protected system files. Error code: !errorlevel!
echo This may indicate access issues or missing files.
)
echo.
echo Cleaning up component store...
dism /image:%WINDOWS_DRIVE%\ /cleanup-image /analyzecomponentstore >nul 2>&1
dism /image:%WINDOWS_DRIVE%\ /cleanup-image /startcomponentcleanup /resetbase >nul 2>&1
dism /image:%WINDOWS_DRIVE%\ /cleanup-mountpoints >nul 2>&1
echo System file repair completed.
if "%choice%"=="5" goto startup
goto menu
:startup
echo.
echo ===== STARTUP REPAIR =====
echo Running comprehensive startup repair...
call :run_bootrec
echo Startup repair completed.
if "%choice%"=="5" goto memory
goto menu
:memory
echo.
echo ===== MEMORY DIAGNOSTIC =====
echo Scheduling memory diagnostic for next boot...
call :schedule_memdiag
if %errorlevel% neq 0 (
echo Failed to schedule memory diagnostic.
echo This may be due to BCD corruption or access issues.
echo You can manually run memory test from Windows RE Advanced Options.
) else (
echo Memory diagnostic scheduled successfully.
echo System will test RAM on next boot before loading Windows.
echo Test results will be available in Event Viewer after boot.
)
if "%choice%"=="5" goto end
goto menu
:advanced_boot
echo.
echo ===== ADVANCED BOOT REPAIR =====
echo This will perform aggressive boot repair operations.
echo WARNING: This may modify boot partitions!
echo.
set /p confirm="Continue? (Y/N): "
if /i not "%confirm%"=="Y" goto menu
call :advanced_boot_repair
goto menu
:: ========================= HELPER FUNCTIONS =========================
:find_available_drive_letter
:: Find an available drive letter and return it in the variable name passed as parameter
:: Usage: call :find_available_drive_letter RESULT_VAR
set "RESULT_VAR_NAME=%~1"
for %%d in (S T U V W R Q P O N M L K J I H G F E) do (
if not exist "%%d:\" (
set "%RESULT_VAR_NAME%=%%d:"
exit /b 0
)
)
:: No available drive letter found
exit /b 1
:: ========================= SYSTEM DETECTION =========================
:detect_system_info
set "BOOT_MODE=Unknown"
set "SYSTEM_ARCH=Unknown"
:: Detect boot mode (UEFI vs Legacy)
if exist "%WINDOWS_DRIVE%\EFI" (
set "BOOT_MODE=UEFI"
) else (
set "BOOT_MODE=Legacy BIOS"
)
:: Detect system architecture
if exist "%windir%\SysWOW64" (
set "SYSTEM_ARCH=64-bit"
) else (
set "SYSTEM_ARCH=32-bit"
)
exit /b 0
:: ========================= MEMORY DIAGNOSTIC FUNCTION =========================
:schedule_memdiag
set "BCD_PATH="
set "NEED_CLEANUP=0"
:: Determine BCD location based on boot mode
if "%BOOT_MODE%"=="UEFI" (
call :find_efi_partition
if !errorlevel! neq 0 (
echo Error: Could not locate EFI system partition.
exit /b 1
)
set "BCD_PATH=%EFI_DRIVE%\EFI\Microsoft\Boot\BCD"
set "NEED_CLEANUP=1"
) else (
set "BCD_PATH=%WINDOWS_DRIVE%\Boot\BCD"
)
:: Verify BCD exists
if not exist "%BCD_PATH%" (
echo Error: Boot configuration data not found at %BCD_PATH%
if !NEED_CLEANUP! == 1 call :cleanup_efi_drive
exit /b 1
)
:: Verify memdiag entry exists (it should be predefined in Windows)
bcdedit /store "%BCD_PATH%" /enum {memdiag} >nul 2>&1
if %errorlevel% neq 0 (
echo Warning: Memory diagnostic entry not found in BCD.
echo This may indicate BCD corruption or missing Windows components.
if !NEED_CLEANUP! == 1 call :cleanup_efi_drive
exit /b 1
)
:: Set boot sequence to run memory diagnostic once
bcdedit /store "%BCD_PATH%" /bootsequence {memdiag} >nul 2>&1
set "CMD_ERROR=%errorlevel%"
:: Cleanup EFI drive letter if needed
if !NEED_CLEANUP! == 1 call :cleanup_efi_drive
:: Return result
if %CMD_ERROR% neq 0 (
echo Failed to set boot sequence for memory diagnostic.
exit /b 1
)
exit /b 0
:: ========================= BOOT REPAIR FUNCTIONS =========================
:run_bootrec
echo Scanning for Windows installations...
bootrec /scanos >nul 2>&1
if %errorlevel% neq 0 (
echo Failed to scan for Windows installations.
)
echo.
echo Attempting to rebuild BCD store...
bootrec /rebuildbcd >nul 2>&1
if %errorlevel% neq 0 (
echo BCD rebuild failed. Trying advanced repair...
call :repair_bcd_advanced
)
echo.
echo Fixing boot sector...
if "%BOOT_MODE%"=="UEFI" (
echo UEFI system detected - repairing EFI boot...
call :repair_efi_boot
) else (
echo Legacy BIOS system - repairing MBR...
bootrec /fixmbr >nul 2>&1
bootrec /fixboot >nul 2>&1
if !errorlevel! neq 0 (
echo Boot sector repair failed. Trying alternative method...
bootsect /nt60 %WINDOWS_DRIVE% /mbr /force >nul 2>&1
)
)
exit /b 0
:repair_bcd_advanced
echo Backing up current BCD...
if exist "%WINDOWS_DRIVE%\Boot\BCD" (
copy "%WINDOWS_DRIVE%\Boot\BCD" "%WINDOWS_DRIVE%\Boot\BCD.backup" >nul 2>&1
)
echo Creating new BCD store...
bcdedit /createstore "%WINDOWS_DRIVE%\Boot\BCD.new" >nul 2>&1
bcdedit /store "%WINDOWS_DRIVE%\Boot\BCD.new" /create {bootmgr} >nul 2>&1
bcdedit /store "%WINDOWS_DRIVE%\Boot\BCD.new" /set {bootmgr} device boot >nul 2>&1
bcdedit /store "%WINDOWS_DRIVE%\Boot\BCD.new" /set {bootmgr} path \bootmgr >nul 2>&1
:: Try rebuilding again
bootrec /rebuildbcd >nul 2>&1
exit /b %errorlevel%
:repair_efi_boot
echo Locating EFI system partition...
call :find_efi_partition
if %errorlevel% neq 0 (
echo EFI partition not found or corrupted. Attempting recreation...
call :recreate_efi_partition
exit /b !errorlevel!
)
echo Repairing EFI boot files...
call :repair_efi_files
exit /b %errorlevel%
:find_efi_partition
:: Use Windows RE RAM drive (X:) for temp files
set "TEMP=X:"
set "tmpfile=%TEMP%\diskpart_%RANDOM%_%RANDOM%.txt"
set "EFI_DISK="
set "EFI_PARTITION="
set "EFI_DRIVE="
:: Find an available drive letter for EFI partition
call :find_available_drive_letter EFI_DRIVE
if %errorlevel% neq 0 (
echo Error: No available drive letters for EFI partition
if exist "%tmpfile%" del "%tmpfile%"
exit /b 1
)
:: List all disks and find EFI partition
echo list disk > "%tmpfile%"
for /f "skip=6 tokens=2" %%d in ('diskpart /s "%tmpfile%"') do (
echo select disk %%d > "%tmpfile%"
echo list partition >> "%tmpfile%"
for /f "tokens=1,2,3,4* delims= " %%a in ('diskpart /s "%tmpfile%" ^| findstr /i "System"') do (
if "%%e"=="System" (
set "EFI_DISK=%%d"
set "EFI_PARTITION=%%b"
goto efi_found
)
)
)
:efi_found
if not defined EFI_PARTITION (
if exist "%tmpfile%" del "%tmpfile%"
exit /b 1
)
echo Found EFI partition: Disk %EFI_DISK%, Partition %EFI_PARTITION%
:: Assign drive letter temporarily
echo select disk %EFI_DISK% > "%tmpfile%"
echo select partition %EFI_PARTITION% >> "%tmpfile%"
echo assign letter=%EFI_DRIVE:~0,1% >> "%tmpfile%"
diskpart /s "%tmpfile%" >nul
if exist "%tmpfile%" del "%tmpfile%"
exit /b 0
:repair_efi_files
echo Checking EFI partition file system...
chkdsk "%EFI_DRIVE%" /r /x >nul 2>&1
if %errorlevel% neq 0 (
echo EFI partition has file system errors.
)
echo Rebuilding EFI boot files...
bcdboot "%windir%" /s "%EFI_DRIVE%" /f UEFI >nul 2>&1
if %errorlevel% neq 0 (
echo Failed to rebuild EFI boot files.
call :cleanup_efi_drive
exit /b 1
)
echo Creating additional boot entries if needed...
bcdedit /store "%EFI_DRIVE%\EFI\Microsoft\Boot\BCD" /set {default} recoveryenabled yes >nul 2>&1
bcdedit /store "%EFI_DRIVE%\EFI\Microsoft\Boot\BCD" /set {default} bootstatuspolicy IgnoreAllFailures >nul 2>&1
call :cleanup_efi_drive
exit /b 0
:cleanup_efi_drive
if defined EFI_DRIVE if defined EFI_DISK if defined EFI_PARTITION (
:: Use Windows RE RAM drive (X:) for temp files
set "TEMP=X:"
set "tmpfile=!TEMP!\diskpart_%RANDOM%_%RANDOM%.txt"
echo select disk %EFI_DISK% > "!tmpfile!"
echo select partition %EFI_PARTITION% >> "!tmpfile!"
echo remove letter=%EFI_DRIVE:~0,1% >> "!tmpfile!"
diskpart /s "!tmpfile!" >nul
if exist "!tmpfile!" del "!tmpfile!"
)
exit /b 0
:recreate_efi_partition
echo WARNING: This will delete and recreate the EFI partition!
echo All boot data will be lost and rebuilt.
set /p confirm="Continue? (Y/N): "
if /i not "%confirm%"=="Y" exit /b 1
if not defined EFI_DISK (
echo Cannot determine target disk. Aborting.
exit /b 1
)
:: Use Windows RE RAM drive (X:) for temp files
set "TEMP=X:"
set "tmpfile=%TEMP%\diskpart_%RANDOM%_%RANDOM%.txt"
set "NEW_EFI_DRIVE="
:: Find an available drive letter for new EFI partition
call :find_available_drive_letter NEW_EFI_DRIVE
if %errorlevel% neq 0 (
echo Error: No available drive letters for new EFI partition
if exist "%tmpfile%" del "%tmpfile%"
exit /b 1
)
echo Deleting corrupted EFI partition...
echo select disk %EFI_DISK% > "%tmpfile%"
if defined EFI_PARTITION (
echo select partition %EFI_PARTITION% >> "%tmpfile%"
echo delete partition override >> "%tmpfile%"
)
echo Creating new EFI partition (260MB)...
echo create partition efi size=260 >> "%tmpfile%"
echo format fs=fat32 quick label="System" >> "%tmpfile%"
echo assign letter=%NEW_EFI_DRIVE:~0,1% >> "%tmpfile%"
echo active >> "%tmpfile%"
diskpart /s "%tmpfile%"
if %errorlevel% neq 0 (
echo Failed to create new EFI partition.
if exist "%tmpfile%" del "%tmpfile%"
exit /b 1
)
echo Rebuilding bootloader on new EFI partition...
bcdboot "%windir%" /s "%NEW_EFI_DRIVE%" /f UEFI >nul 2>&1
if %errorlevel% neq 0 (
echo Failed to install bootloader on new EFI partition.
)
echo Removing temporary drive letter...
echo select disk %EFI_DISK% > "%tmpfile%"
echo list partition >> "%tmpfile%"
for /f "tokens=1,2,3,4* delims= " %%a in ('diskpart /s "%tmpfile%" ^| findstr /i "System"') do (
if "%%e"=="System" (
echo select partition %%b >> "%tmpfile%"
echo remove letter=%NEW_EFI_DRIVE:~0,1% >> "%tmpfile%"
diskpart /s "%tmpfile%" >nul
goto efi_cleanup_done
)
)
:efi_cleanup_done
if exist "%tmpfile%" del "%tmpfile%"
exit /b 0
:advanced_boot_repair
echo Performing advanced boot repair operations...
echo 1. Backing up current boot configuration...
if exist "%WINDOWS_DRIVE%\Boot\BCD" (
copy "%WINDOWS_DRIVE%\Boot\BCD" "%WINDOWS_DRIVE%\Boot\BCD.backup.%DATE:/=-%_%TIME::=-%" >nul 2>&1
)
echo 2. Rebuilding master boot record...
bootrec /fixmbr >nul 2>&1
if %errorlevel% neq 0 echo MBR rebuild failed.
echo 3. Rebuilding boot sector...
bootrec /fixboot >nul 2>&1
if %errorlevel% neq 0 (
echo Standard boot sector repair failed, trying alternative...
bootsect /nt60 %WINDOWS_DRIVE% /mbr /force >nul 2>&1
)
echo 4. Scanning for all Windows installations...
bootrec /scanos >nul 2>&1
echo 5. Rebuilding BCD with all found installations...
bootrec /rebuildbcd >nul 2>&1
echo 6. Setting boot configuration policies...
bcdedit /set {default} recoveryenabled yes >nul 2>&1
bcdedit /set {default} bootstatuspolicy IgnoreAllFailures >nul 2>&1
bcdedit /set {bootmgr} timeout 10 >nul 2>&1
if "%BOOT_MODE%"=="UEFI" (
echo 7. Repairing UEFI boot entries...
call :repair_efi_boot
)
echo Advanced boot repair completed.
exit /b 0
:end
echo.
echo +=============================+
echo + Repair operations completed +
echo +=============================+
echo.
echo Summary of actions taken:
if "%choice%"=="5" (
echo - Disk integrity check and repair
echo - System file integrity verification
echo - Windows image health repair
echo - Boot configuration rebuild
echo - Memory diagnostic scheduled
)
echo.
echo IMPORTANT: Please restart your computer now.
echo The system will complete any pending operations during boot.
echo.
timeout /t 10 /nobreak
endlocal
exit /b 0