::call write system        分区名        img路径
::           recovery      分区名        img路径
::           fastboot      分区名        img路径
::           fastbootboot  img路径
::           edl           端口号        存储类型  img所在文件夹  xml路径  elf完整路径(可选,不填不发送)
::           twrpinst      zip路径
::           sideload      zip路径

@ECHO OFF
set var1=%1& set var2=%2& set var3=%3& set var4=%4& set var5=%5& set var6=%6& set var7=%7& set var8=%8& set var9=%9
goto %var1%




:SIDELOAD
SETLOCAL
set logger=write.bat-sideload
::接收变量
set zippath=%var2%
call log %logger% I 接收变量:zippath:%zippath%
:SIDELOAD-1
::检查是否存在
if not exist %zippath% ECHOC {%c_e%}找不到%zippath%{%c_i%}{\n}& call log %logger% F 找不到%zippath%& goto FATAL
::安装
call reboot recovery sideload rechk 3
call log %logger% I 安装%zippath%
adb.exe sideload %zippath% 1>>%logfile% 2>&1 || ECHOC {%c_e%}安装%zippath%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 安装%zippath%失败&& pause>nul && ECHO.重试... && goto SIDELOAD-1
ENDLOCAL
goto :eof


:TWRPINST
SETLOCAL
set logger=write.bat-twrpinst
::接收变量
set zippath=%var2%
call log %logger% I 接收变量:zippath:%zippath%
:TWRPINST-1
::检查是否存在
if not exist %zippath% ECHOC {%c_e%}找不到%zippath%{%c_i%}{\n}& call log %logger% F 找不到%zippath%& goto FATAL
::推送
call log %logger% I 推送%zippath%到./tmp/bff.zip
adb.exe push %zippath% ./tmp/bff.zip 1>>%logfile% 2>&1 || ECHOC {%c_e%}推送%zippath%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 推送%zippath%失败&& pause>nul && ECHO.重试... && goto TWRPINST-1
::安装
call log %logger% I 安装./tmp/bff.zip
adb.exe shell twrp install ./tmp/bff.zip 1>tmp\output.txt 2>&1 || type tmp\output.txt>>%logfile% && call log %logger% E 安装%zippath%失败&& ECHOC {%c_e%}安装%zippath%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& pause>nul && ECHO.重试... && goto TWRPINST-1
type tmp\output.txt>>%logfile%
find "zip" "tmp\output.txt" 1>nul 2>nul || ECHOC {%c_e%}安装%zippath%失败, TWRP未执行命令. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 安装%zippath%失败,TWRP未执行命令&& pause>nul && ECHO.重试... && goto TWRPINST-1
adb.exe shell rm ./tmp/bff.zip 1>>%logfile% 2>&1 || ECHOC {%c_e%}删除./tmp/bff.zip失败{%c_i%}{\n}&& call log %logger% E 删除./tmp/bff.zip失败
ENDLOCAL
goto :eof


:EDL
SETLOCAL
set logger=write.bat-edl
::接收变量
set port=%var2%& set memory=%var3%& set searchpath=%var4%& set xml=%var5%& set elf=%var6%
call log %logger% I 接收变量:port:%port%.memory:%memory%.searchpath:%searchpath%.xml:%xml%.elf:%elf%
:EDL-1
::检查是否存在
if not exist %searchpath% ECHOC {%c_e%}找不到%searchpath%{%c_i%}{\n}& call log %logger% F 找不到%searchpath%& goto FATAL
if not "%elf%"=="" (if not exist %elf% ECHOC {%c_e%}找不到%elf%{%c_i%}{\n}& call log %logger% F 找不到%elf%& goto FATAL)
::发送引导
if not "%elf%"=="" (
    ECHO.正在发送引导...& call log %logger% I 正在发送引导
    QSaharaServer.exe -p \\.\COM%port% -s 13:%elf% 1>>%logfile% 2>&1 || ECHOC {%c_e%}发送引导失败.{%c_h%}按任意键继续...{%c_i%}{\n}&& call log %logger% E 发送引导失败&& pause>nul)
::开始刷机
ECHO.正在9008刷入...& call log %logger% I 正在9008刷入
fh_loader.exe --port=\\.\COM%port% --memoryname=%memory% --search_path=%searchpath% --sendxml=%xml% --noprompt 1>>%logfile% 2>&1 || ECHOC {%c_e%}9008刷入失败.{%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 9008刷入失败&& pause>nul && ECHO.重试... && goto EDL-1
::--showpercentagecomplete --reset --noprompt --zlpawarehost=1 --testvipimpact
move /Y port_trace.txt log 1>nul 2>nul
ECHO.9008刷入完成& call log %logger% I 9008刷入完成
ENDLOCAL
goto :eof


:SYSTEM
SETLOCAL
set logger=write.bat-system
set target=./sdcard
goto ADBDD


:RECOVERY
SETLOCAL
set logger=write.bat-recovery
set target=./tmp
goto ADBDD


:ADBDD
::接收变量
set parname=%var2%& set imgpath=%var3%
call log %logger% I 接收变量:parname:%parname%.imgpath:%imgpath%
:ADBDD-1
::检查文件
if not exist %imgpath% ECHOC {%c_e%}找不到%imgpath%{%c_i%}{\n}& call log %logger% F 找不到%imgpath%& goto FATAL
::系统下要检查Root
if "%target%"=="./sdcard" (
    call log %logger% I 开始检查Root
    echo.su>tmp\cmd.txt& echo.exit>>tmp\cmd.txt& echo.exit>>tmp\cmd.txt
    adb.exe shell < tmp\cmd.txt 1>>%logfile% 2>&1 || ECHOC {%c_e%}获取Root失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 获取Root失败&& pause>nul && ECHO.重试... && goto ADBDD-1)
::推送
call log %logger% I 开始推送%imgpath%到%target%/%parname%.img
adb.exe push %imgpath% %target%/%parname%.img 1>>%logfile% 2>&1 || ECHOC {%c_e%}推送%imgpath%到%target%/%parname%.img失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 推送%imgpath%到%target%/%parname%.img失败&& pause>nul && ECHO.重试... && goto ADBDD-1
::获取分区路径
call info parpath %parname%
::刷入和清理
if "%target%"=="./sdcard" echo.su>tmp\cmd.txt& echo.dd if=%target%/%parname%.img of=%info__parpath__parpath% >>tmp\cmd.txt& echo.rm %target%/%parname%.img>>tmp\cmd.txt
if "%target%"=="./tmp" echo.dd if=%target%/%parname%.img of=%info__parpath__parpath% >tmp\cmd.txt& echo.rm %target%/%parname%.img>>tmp\cmd.txt
call log %logger% I 开始刷入%target%/%parname%.img到%info__parpath__parpath%
adb.exe shell < tmp\cmd.txt 1>>%logfile% 2>&1 || ECHOC {%c_e%}刷入%target%/%parname%.img到%info__parpath__parpath%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 刷入%target%/%parname%.img到%info__parpath__parpath%失败&& pause>nul && ECHO.重试... && goto ADBDD-1
ENDLOCAL
goto :eof


:FASTBOOT
SETLOCAL
set logger=write.bat-fastboot
::接收变量
set parname=%var2%& set imgpath=%var3%
call log %logger% I 接收变量:parname:%parname%.imgpath:%imgpath%
:FASTBOOT-1
::检查文件
if not exist %imgpath% ECHOC {%c_e%}找不到%imgpath%{%c_i%}{\n}& call log %logger% F 找不到%imgpath%& goto FATAL
::刷入
call log %logger% I 开始刷入%imgpath%到%parname%
fastboot.exe flash %parname% %imgpath% 1>>%logfile% 2>&1 || ECHOC {%c_e%}刷入%imgpath%到%parname%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 刷入%imgpath%到%parname%失败&& pause>nul && ECHO.重试... && goto FASTBOOT-1
ENDLOCAL
goto :eof


:FASTBOOTBOOT
SETLOCAL
set logger=write.bat-fastbootboot
::接收变量
set imgpath=%var2%
call log %logger% I 接收变量:imgpath:%imgpath%
:FASTBOOTBOOT-1
::检查文件
if not exist %imgpath% ECHOC {%c_e%}找不到%imgpath%{%c_i%}{\n}& call log %logger% F 找不到%imgpath%& goto FATAL
::刷入
call log %logger% I 启动%imgpath%
fastboot.exe boot %imgpath% 1>>%logfile% 2>&1 || ECHOC {%c_e%}启动%imgpath%失败. {%c_h%}按任意键重试...{%c_i%}{\n}&& call log %logger% E 启动%imgpath%失败&& pause>nul && ECHO.重试... && goto FASTBOOTBOOT-1
ENDLOCAL
goto :eof








:FATAL
ECHO. & if exist tool\Windows\ECHOC.exe (tool\Windows\ECHOC {%c_e%}抱歉, 脚本遇到问题, 无法继续运行. 请查看日志. {%c_h%}按任意键退出...{%c_i%}{\n}& pause>nul & EXIT) else (ECHO.抱歉, 脚本遇到问题, 无法继续运行. 按任意键退出...& pause>nul & EXIT)

