@echo off
color 0a
ECHO.

rem 清理旧备份
rd /s /q backup > nul 2>&1

rem 创建备份目录
adb shell "rm -rf /sdcard/backup "
adb shell "mkdir /sdcard/backup"

rem 获取平台ID
for /F %%i in ('adb shell ls /dev/block/platform/') do set commitid=%%i
echo commitid=%commitid%

set EXISTS_FLAG=false

echo %commitid% | find "soc" > nul && set EXISTS_FLAG=true

if "%EXISTS_FLAG%"=="true" (
    call :backup_partition soc
) else (
    call :backup_partition hi_mci.1
)

goto :eof

:backup_partition
set PARTITION_NAME=%1
ECHO 正在备份 %PARTITION_NAME% backup 分区，耐心等待......

for %%partition in (backup kernel boot recovery system) do (
    adb shell "dd if=/dev/block/platform/%commitid%/by-name/%%partition | busybox gzip -4 >/sdcard/backup/%%partition.img.gz"
)

call :upload_and_cleanup
goto :eof

:upload_and_cleanup
ECHO 正在上传备份的固件，耐心等待......

adb pull -p /sdcard/backup backup

adb shell "rm -rf /sdcard/backup "

ECHO 备份完成，固件存放位置为工具根目录backup文件！！！
pause > NUL
goto :eof
