做S32K144的一个案子,MDK只生成了一些bin文件,还要手动合成一个烧录文件,麻烦。
在网上找了半天,居然没找到一个我想要的bin文件合并小软件,晚上加班写了一个,寥寥一百行。
程序及用法示例上传到了CSDN,下载链接:
https://download.csdn.net/download/timini/11780137
其中的批处理文件内容如下,这个案子iap代码是从0x0000开始的,app代码是从0x5000开始的:
del "bmcu_iap.bin"
del "bmcu_app.bin"
del "bmcu_program.bin"
bincombin.exe "bmcu_iap.bin" 0 "\iap\VECTOR_ROM"
bincombin.exe "bmcu_iap.bin" 0 "\iap\ER_m_flash_config"
bincombin.exe "bmcu_iap.bin" 0 "\iap\ER_m_text"
bincombin.exe "bmcu_app.bin" 5000 "VECTOR_ROM"
bincombin.exe "bmcu_app.bin" 5000 "ER_m_flash_config"
bincombin.exe "bmcu_app.bin" 5000 "ER_m_text"
bincombin.exe "bmcu_program.bin" 0 "bmcu_iap.bin"
bincombin.exe "bmcu_program.bin" 0 "bmcu_app.bin" 5000
pause
C#代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace bincombin
{
class Program
{
static void show_help()
{
StringBuilder help_str = new StringBuilder();
help_str.Append("\r\nError: Failed to Combin. Here is the help of this command:\r\n\r\n");
help_str.Append("******************************************************************************\r\n\r\n");
help_str.Append(" command : bincombin dst.bin dst_start src.bin [src_start]\r\n\r\n");
help_str.Append(" dst.bin : 目标文件相对路径(相对本程序而言)。如果不存在,则将创建该文件\r\n\r\n");
help_str.Append(" dst_start : 目标文件起始地址(HEX格式)\r\n\r\n");
help_str.Append(" src.bin : 源文件相对路径(相对本程序而言)。该文件将被追加到目标文件中\r\n\r\n");
help_str.Append(" src_start : 源文件起始地址(HEX格式)。如不指定则合并时,源文件紧跟目标文件之后\r\n\r\n\r\n");
help_str.Append(" 例如,有一个批处理文件内容如下:\r\n\r\n");
help_str.Append(" bincombin “test\\all.bin\" 5000 \"test\\VECTOR_ROM\"\r\n");
help_str.Append(" bincombin “test\\all.bin\" 5000 \"test\\ER_m_flash_config\"\r\n");
help_str.Append(" bincombin “test\\all.bin\" 5000 \"test\\ER_m_text\" 6000\r\n\r\n");
help_str.Append(" 批处理文件执行后,会在test文件夹下生成all.bin, 其内容如下:\r\n\r\n");
help_str.Append(" 文件开始为VECTOR_ROM文件的内容。\r\n");
help_str.Append(" 之后就是ER_m_flash_config文件的内容。\r\n");
help_str.Append(" 在all.bin的1000(即6000-5000)处开始,是ER_m_text文件的内容。\r\n\r\n");
help_str.Append("******************************************************************************\r\n");
Console.Write(help_str.ToString());
}
static void Main(string[] args)
{
try
{
string file_dst = Directory.GetCurrentDirectory() + "\\" + args[0];
string file_src = Directory.GetCurrentDirectory() + "\\" + args[2];
//读取 dst.bin文件
byte[] dst_bin = read_bin(file_dst);
UInt32 dst_start = UInt32.Parse(args[1], System.Globalization.NumberStyles.HexNumber);
UInt32 dst_size = (UInt32)dst_bin.Length;
//读取 src.bin文件
byte[] src_bin = read_bin(file_src);
UInt32 src_size = (UInt32)src_bin.Length;
//合并
UInt32 src_start = dst_size;
if (args.Length > 4)
{
src_start = UInt32.Parse(args[3], System.Globalization.NumberStyles.HexNumber);
src_start -= dst_start;
}
UInt32 comb_size = src_start + src_size;
comb_size = (comb_size > dst_size) ? comb_size : dst_size;
byte[] combin = new byte[comb_size];
for (UInt32 i = 0; i < dst_size; i++)
{
combin[i] = dst_bin[i];
}
for (UInt32 i = src_start; i < comb_size; i++)
{
combin[i] = src_bin[i - src_start];
}
write_bin(file_dst, combin); //合并结果覆盖dst.bin
Console.WriteLine("[OK]");
}
catch
{
show_help();
Console.Read();
}
}
static byte[] read_bin(string path)
{
FileStream rf = new FileStream(path, FileMode.OpenOrCreate);
BinaryReader rb = new BinaryReader(rf);
byte[] data = new byte[rf.Length];
rb.Read(data, 0, data.Length);
rb.Close();
rf.Close();
return data;
}
static void write_bin(string path, byte[] data)
{
FileStream wf = new FileStream(path, FileMode.Create);
BinaryWriter wb = new BinaryWriter(wf);
wb.Write(data, 0, data.Length);
wb.Close();
wf.Close();
}
}
}