背景:有的时候需要将不同区域的BIN文件进行合并,合并BIN文件也有专门的工具可以完成,但是需要人机交互手动完成。因此想做一个脚本实现一键合并。
思路:合并BIN文件
第一步:将要合并的BIN文件拷贝至同一路径下方便操作;
第二步:将第一部分BIN文件先定向输出至目标文件;计算两个文件之间的空文件大小;定向输出空文件至目标文件。这样第一个文件后的文件被填为空,直到第二个文件的开始部分。
第三部:将第二个BIN文件定向输出值目标文件;这样所得的目标文件就是要合并的文件。
脚本:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
::******************************************************************
:: S00962
:: 2021.06.21
:: auto execute rename handler
::******************************************************************
::clean
@echo off&setlocal enabledelayedexpansion
@echo off&set Q=&set vbs=Questiontmp.vbs
::get current path
set CURRENT_PATH=%CD%
::get source bin path
set SOURCE_BIN_PATH=%CD%\..\build\workspace\mesh_flash_xip\output\mesh_flash_xip.bin
set BOOT_FLASH_PATH=%CD%\..\build\workspace\mesh_boot_flash\output
::set dst bin file name
set DST_FILE_NAME=SRAM_BIN_IMAGE_A
::set offest size 12288(12K)
for %%a in (%BOOT_FLASH_PATH%\boot_to_flash.bin) do set /a OFFSET_SIZE=12*1024-%%~za
cd /d %SOURCE_BIN_PATH%
if not exist ER_IROM1 goto lable
if not exist ER_IROM2 goto lable
::delete previous .bin document
if exist *.txt del *.txt
if exist *.bin del *.bin
::delay 1s
ping -n 1 127.0.0.1>nul
::rename ER_IROM1 ER_IROM2 as .bin
rename ER_IROM1 ER_IROM1.bin
rename ER_IROM2 ER_IROM2.bin
::start tool
::cd /d %CURRENT_PATH%
::start binary_file_combination_tool.exe
cd /d %SOURCE_BIN_PATH%
if not exist %BOOT_FLASH_PATH%\boot_to_flash.bin goto lable_null
copy /y %BOOT_FLASH_PATH%\boot_to_flash.bin %SOURCE_BIN_PATH%
::create a empty bin file size is OFFSET_SIZE
fsutil file createnew %SOURCE_BIN_PATH%\emptyfile.bin %OFFSET_SIZE%
::delete destination file first
if exist %DST_FILE_NAME%.bin del %DST_FILE_NAME%.bin
type boot_to_flash.bin >>%DST_FILE_NAME%.bin
type emptyfile.bin >>%DST_FILE_NAME%.bin
type ER_IROM2.bin >>%DST_FILE_NAME%.bin
del emptyfile.bin
del ER_IROM2.bin boot_to_flash.bin
start %SOURCE_BIN_PATH%
exit
:lable
::delete previous .bin document
if exist *.txt del *.txt
if exist boot_to_flash.bin del boot_to_flash.bin
if exist emptyfile.bin del emptyfile.bin
echo Wsh.Echo MsgBox("This is no file can be process, "+Chr(10)+"Please build project XIP!!!", vbYes, "Rename Tool")>%vbs%
for /f %%a in ('cscript %vbs% //nologo //e:vbscript') do set "Q=%%a"
if %Q%==1 del /q /f %vbs%>nul&goto Yes
:Yes
exit
:lable_null
::delete previous .bin document
if exist *.txt del *.txt
if exist emptyfile.bin del emptyfile.bin
echo Wsh.Echo MsgBox("Not found boot_to_flash.bin, " + Chr(10) + "Please check first!!!", vbYes, "Combine Tool")>%vbs%
for /f %%a in ('cscript %vbs% //nologo //e:vbscript') do set "Q=%%a"
if %Q%==1 del /q /f %vbs%>nul&goto Yes
:Yes
exit
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
核心脚本:
获取第一个文件的大小,用第二个文件的偏移量减去第一个BIN文件的大小,就是两个BIN文件之间的空数据大小;
for %%a in (%BOOT_FLASH_PATH%\boot_to_flash.bin) do set /a OFFSET_SIZE=12*1024-%%~za
使用fsutil 创建指定大小的空文件
::create a empty bin file size is OFFSET_SIZE
fsutil file createnew %SOURCE_BIN_PATH%\emptyfile.bin %OFFSET_SIZE%
如果已经存在目标文件,首先清理掉目标文件
::delete destination file first
if exist %DST_FILE_NAME%.bin del %DST_FILE_NAME%.bin
依次定向输出3个BIN文件至目标文件(bin1、空bin、bin2)
type boot_to_flash.bin >>%DST_FILE_NAME%.bin
type emptyfile.bin >>%DST_FILE_NAME%.bin
type ER_IROM2.bin >>%DST_FILE_NAME%.bin
希望能对大家带来帮助
这篇博客介绍如何利用Bat脚本自动化合并BIN文件。首先将所有BIN文件拷贝到同一路径,然后通过创建一个空文件填充两文件间的空隙,接着定向输出文件至目标文件,最终得到合并后的文件。脚本包括文件检查、清理、创建空文件及定向输出等步骤。
2476

被折叠的 条评论
为什么被折叠?



