@echo off
color 0a
ECHO. 

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

adb ls dev/block/by-name

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

call :backup_partition MBOOT
call :backup_partition MPOOL
call :backup_partition boot
call :backup_partition customer
call :backup_partition dtb
call :backup_partition dtbo
call :backup_partition misc
call :backup_partition factoryinfo
call :backup_partition product
call :backup_partition recovery
call :backup_partition param
call :backup_partition tvcertificate
call :backup_partition tvconfig
call :backup_partition tvservice
call :backup_partition vbmeta

rem 提示用户备份 vendor 分区可能需要一些时间
ECHO 正在备份晶 vendor 分区，可能需要较长时间...... 
adb shell "dd if=/dev/block/by-name/vendor of=/mnt/sdcard/backup/vendor.img"

rem 提示用户备份 system 分区可能需要一些时间
ECHO 正在备份晶 system 分区，可能需要较长时间...... 
adb shell "dd if=/dev/block/by-name/system of=/mnt/sdcard/backup/system.img"

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/by-name/%PARTITION_NAME% of=/mnt/sdcard/backup/%PARTITION_NAME%.img"
goto :eof

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