Untitled

 avatar
unknown
batchfile
21 days ago
7.6 kB
12
Indexable
@echo off
setlocal EnableDelayedExpansion

REM ================= ANSI colors =================
for /f %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
set "O=%ESC%[38;5;208m"
set "C=%ESC%[96m"
set "G=%ESC%[92m"
set "R=%ESC%[91m"
set "Y=%ESC%[93m"
set "W=%ESC%[97m"
set "D=%ESC%[90m"
set "B=%ESC%[1m"
set "X=%ESC%[0m"

REM ================= Config =====================
set "SRC=C:\Fox"
set "ZIP1=rom.zip"
set "DATA_BACKUP=data"
set "ZIP2=dm.zip"
set "ZIP3=patch.zip"
set "DEST=/data/media/0"
set "RDIR=%TEMP%\foxflash_results"

REM ================= Worker dispatch =============
if /i "%~1"=="WORKER" goto :worker

REM ================= Banner =====================
cls
echo %O%%B%
echo    ==============================================================
echo    ==                                                          ==
echo    ==   O R A N G E   F O X   --   C O M B O   F L A S H       ==
echo    ==         Redmi Note 7   /   lavender                      ==
echo    ==   [ FULL AUTO: ROM -^> Backup -^> DM -^> Anti-Deadlock ZIP ] ==
echo    ==                                                          ==
echo    ==============================================================
echo %X%

REM ================= Detect devices =============
set "COUNT=0"
for /f "skip=1 tokens=1,2" %%d in ('adb devices') do (
  set "ST=%%e"
  set "USE="
  if "!ST!"=="device" set "USE=1"
  if "!ST!"=="recovery" set "USE=1"
  if defined USE (
    set /a COUNT+=1
    set "DEV[!COUNT!]=%%d"
  )
)

if %COUNT%==0 (
  echo %R%%B%   [ERROR] No usable ADB device found.%X%
  echo %D%   Devices must be in system or recovery, USB debugging on, authorized.%X%
  echo.
  pause
  exit /b
)

if not exist "%SRC%\%ZIP3%" (
  echo %R%%B%   [ERROR] %SRC%\%ZIP3% not found.%X%
  echo %D%   Put your original patch.zip in %SRC%.%X%
  echo.
  pause
  exit /b
)

echo %Y%%B%   Detected %COUNT% device^(s^):%X%
for /l %%n in (1,1,%COUNT%) do echo        %G%%%n%X%   %W%!DEV[%%n]!%X%
echo.
echo %R%%B%   *** COMBO FLASH: Wipes data, Flashes all ZIPs seamlessly, Reboots ONCE. ***%X%
echo %D%   Make sure rom.zip, data backup, dm.zip, and patch.zip are in %SRC%.%X%
echo.
choice /c YN /m "   Launch all devices now"
if errorlevel 2 exit /b

REM ---- fresh results dir ----
if exist "%RDIR%" rd /s /q "%RDIR%"
mkdir "%RDIR%"

REM ---- spawn one worker window per device ----
for /l %%n in (1,1,%COUNT%) do start "Fox %%n - !DEV[%%n]!" cmd /c "%~f0" WORKER !DEV[%%n]!

echo.
echo %C%%B%   Launched %COUNT% flashers - each device has its own window.%X%
echo.

REM ---- wait for all result files ----
set "WC=0"
:waitall
set "DONE=0"
for /f %%c in ('dir /b "%RDIR%\*.txt" 2^>nul ^| find /c /v ""') do set "DONE=%%c"
<nul set /p "=%ESC%[2K%ESC%[1G   %C%Progress:  %G%!DONE!%C% / %COUNT%  devices finished%X%"
if !DONE! geq %COUNT% goto :allfin
set /a WC+=1
if !WC! geq 1440 goto :allfin
ping -n 6 127.0.0.1 >nul
goto :waitall

:allfin
echo.
echo.
set "OK=0"
set "FAIL=0"
for %%f in ("%RDIR%\*.txt") do (
  set "RES="
  set /p RES=<"%%f"
  if "!RES!"=="OK" set /a OK+=1
  if "!RES!"=="FAIL" set /a FAIL+=1
)
set /a UNK=%COUNT%-OK-FAIL

echo %O%%B%   ==============================================================%X%
echo       %G%%B%FINISHED%X%    %G%OK: !OK!%X%    %R%FAIL: !FAIL!%X%    %Y%PENDING: !UNK!%X%    %W%TOTAL: %COUNT%%X%
echo %O%%B%   ==============================================================%X%
if !UNK! gtr 0 echo %Y%   [*] PENDING means a window is still running or crashed without writing a result.%X%
echo.
pause
exit /b

REM ======================================================
REM  WORKER  (one device)   %~2 = serial
REM ======================================================
:worker
set "S=%~2"
title Fox - %S%
cls
echo %O%%B%
echo    ==============================================================
echo     COMBO FLASH (ANTI-DEADLOCK):  %W%%S%%O%
echo    ==============================================================
echo %X%

echo %C%   [1/6]%X% Boot into OrangeFox
adb -s %S% reboot recovery 2>nul
call :waitrec "%S%" 90
timeout /t 4 /nobreak >nul

echo %C%   [2/6]%X% Super Deep Clean: Wipe System + Hard Wipe Data
adb -s %S% shell twrp wipe cache
adb -s %S% shell twrp wipe dalvik
adb -s %S% shell twrp wipe system
adb -s %S% shell twrp wipe data
adb -s %S% shell twrp mount /data
adb -s %S% shell "rm -rf /data/* 2>/dev/null; rm -rf /data/.* 2>/dev/null"

echo %C%   [3/6]%X% Reboot OrangeFox %D%[apply wipe before pushing files]%X%
adb -s %S% reboot recovery 2>nul
call :waitrec "%S%" 90
timeout /t 5 /nobreak >nul

echo %C%   [4/6]%X% Mount and Push files %D%[large - please wait]%X%
adb -s %S% shell twrp mount /data
adb -s %S% shell mkdir -p "%DEST%"
adb -s %S% push "%SRC%\%ZIP1%" "%DEST%/"
if errorlevel 1 goto :worker_fail
adb -s %S% push "%SRC%\%DATA_BACKUP%" "%DEST%/"
adb -s %S% push "%SRC%\%ZIP2%" "%DEST%/"
adb -s %S% push "%SRC%\%ZIP3%" "%DEST%/"

echo %C%   [5/6]%X% Combo Flash: ROM -^> Backup -^> DM Verity -^> Patch.zip
echo %D%        Flashing ROM...%X%
adb -s %S% shell twrp install "%DEST%/%ZIP1%"
REM เพิ่มดีเลย์ 5 วินาที ให้ OrangeFox พักหายใจเคลียร์ Thread ก่อนรับงานใหม่
timeout /t 5 /nobreak >nul

echo %D%        Restoring Data Backup...%X%
adb -s %S% shell twrp restore "%DEST%/%DATA_BACKUP%"
REM เพิ่มดีเลย์ 5 วินาที
timeout /t 5 /nobreak >nul

echo %D%        Flashing DM Verity...%X%
adb -s %S% shell twrp install "%DEST%/%ZIP2%"
REM เพิ่มดีเลย์ 3 วินาที
timeout /t 3 /nobreak >nul

echo %D%        Flashing Original patch.zip (Anti-Deadlock Mode)...%X%
adb -s %S% shell "mkdir -p /tmp/patch_run && unzip -oq %DEST%/%ZIP3% -d /tmp/patch_run"
adb -s %S% shell "chmod +x /tmp/patch_run/META-INF/com/google/android/update-binary"
adb -s %S% shell "exec 3>&1; /tmp/patch_run/META-INF/com/google/android/update-binary 2 3 %DEST%/%ZIP3% | grep -v 'ui_print'"

echo %C%   [6/6]%X% Force wipe cache, Clean up, and Reboot
echo %D%        Clearing Dalvik cache (preventing bootloops)...%X%
adb -s %S% shell twrp wipe dalvik >nul 2>&1
adb -s %S% shell "rm -rf /data/dalvik-cache 2>/dev/null"
adb -s %S% shell "rm -rf /tmp/patch_run"

echo %D%        Cleaning up internal storage...%X%
adb -s %S% shell rm -f "%DEST%/%ZIP1%" "%DEST%/%ZIP2%" "%DEST%/%ZIP3%"
adb -s %S% shell rm -rf "%DEST%/%DATA_BACKUP%"

echo %G%   Done! Rebooting system...%X%
adb -s %S% reboot
set "RESULT=OK"
goto :worker_done

:worker_fail
set "RESULT=FAIL"

:worker_done
> "%RDIR%\%S%.txt" echo %RESULT%
echo.
if "%RESULT%"=="OK" (
  echo %G%%B%   [OK]  %S%  deep-cleaned, combo flashed flawlessly, rebooting%X%
  echo %D%   Window closes soon.%X%
  timeout /t 10 /nobreak >nul
) else (
  echo %R%%B%   [FAIL]  %S%  - not reachable / mount or file missing - see log above%X%
  echo %D%   Window stays open - close it after reviewing.%X%
  pause >nul
)
exit /b

REM ======================================================
REM  :waitrec  <serial>  <max_tries ~2s each>
REM ======================================================
:waitrec
set "WS=%~1"
set /a "WMAX=%~2"
set /a "WI=0"
:waitrec_loop
set "STATE="
for /f "tokens=*" %%s in ('adb -s %WS% get-state 2^>nul') do set "STATE=%%s"
if "!STATE!"=="recovery" goto :eof
set /a WI+=1
if !WI! geq %WMAX% goto :eof
ping -n 3 127.0.0.1 >nul
goto :waitrec_loop
Editor is loading...
Leave a Comment