@echo off
color 0a
ECHO. 

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

adb shell "rm -rf /sdcard/backup "
adb shell "mkdir /sdcard/backup"

call :backup_partition dtb
call :backup_partition boot
call :backup_partition bootloader
call :backup_partition cri_data
call :backup_partition dtbo
call :backup_partition env
call :backup_partition keydata
call :backup_partition metadata
call :backup_partition odm
call :backup_partition product
call :backup_partition recovery
call :backup_partition reserved
call :backup_partition rsv
call :backup_partition vbmeta

rem 提示用户备份 vendor 分区可能需要一些时间
ECHO 正在备份 vendor 分区，可能需要较长时间...... 
adb shell "dd if=/dev/block/vendor | busybox gzip -4 > /sdcard/backup/vendor.img.gz"

rem 提示用户备份 system 分区可能需要一些时间
ECHO 正在备份 system 分区，可能需要较长时间...... 
adb shell "dd if=/dev/block/system | busybox gzip -4 > /sdcard/backup/system.img.gz"

call :backup_partition skbackup
call :backup_partition backup
call :backup_partition cmccbackup
call :backup_partition skmac
call :backup_partition cmccmac
call :backup_partition skparam

call :upload_and_cleanup

ECHO 备份完成，固件存放位置为工具根目录 backup 文件！！！

pause > NUL
goto :eof

:backup_partition
set PARTITION_NAME=%1
ECHO 正在备份 %PARTITION_NAME% 分区...... 
adb shell "dd if=/dev/block/%PARTITION_NAME% of=/sdcard/backup/%PARTITION_NAME%.img"
goto :eof

:upload_and_cleanup
ECHO 正在上传备份的固件，可能需要较长时间......
adb pull -p /sdcard/backup backup
adb shell "rm -rf /sdcard/backup"
goto :eof
