使用srec_cat

访问srec_cat官网,找到Windows版本进行下载

操作步骤

  1. 下载
  2. 安装
  3. 配置环境变量(选做)
  4. app.binboot.bin放在./srecord/bin目录下
  5. 打开命令提示符

固件合并

1
srec_cat -output "merge.bin" -binary "boot.bin" -binary -fill 0xff 0x0000 0x3000 "app.bin" -binary -offset 0x3000

命令解释如下

-output "merge.bin" –> 指定生成的文件名
"boot.bin" –> boot文件名
-fill 0xff 0x0000 0x3000 –> 从0x0000到0x3000填充0xff
"app.bin" –> app文件名
-offset 0x3000 –> app文件起始地址

放入/bin目录下的固件名要与指令中的固件名对应

注2:根据自身需要修改命令

hex转bin

1
srec_cat.exe APP.hex -intel -offset -0x08003000 -o APP.bin -binary

bin转hex

1
srec_cat.exe APP.bin -binary -offset 0x08003000 -o APP.hex -intel

使用JlinkJFlash

访问JLink官网,下载Windows版本

1
File -> Open data file... -> Enter start address (输入你bootloader在Flash中的起始地址,我这儿为 0x08000000)-> OK

然后

1
File -> Merge data file... -> Enter start address (输入你app在Flash中的起始地址,我这儿为 0x08003000)-> OK

然后就可以通过JFlash烧录进板子了,如下:

1
Target -> Connect -> Production Programming

当然,也可以导出合并后的固件,如下:

1
File -> Save data file as... -> 选择存放位置 -> 填入固件名 -> 选择固件类型为bin -> 保存 -> 出现Enter start address,不用管,点击OK就行

自己写C程序进行合并

直接VSCode编译运行即可

优点是:可以直接将需要的标志位写入,自定义程度高。

注:也可以不要配置文件,直接在程序中写死也行

配置文件

1
2
3
4
5
6
[Boot Path]
BootLoader.bin

[APP Path]
APP.bin

C程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

#define OUTPUT_PATH "Merge.bin"
#define CONFIG_INI_PATH "config.ini"

#define PATH_SIZE (128)
#define BOOT_START_ADDR (0X08000000)
#define FLAG_OF_OTA_ADDR (0X08004FF0)
#define APP1_START_ADDR (0X08005000)
#define APP2_START_ADDR (0x08012800) // (FLASH_END_ADDR - APP1_START_ADDR) / 2 + APP1_START_ADDR
#define FLASH_END_ADDR (0X08020000)

static int read_config_line(FILE* file, char* buffer, size_t size, const char* error_msg);
static void fill_with_pattern(FILE* file, unsigned int* address, unsigned int end_addr, unsigned char pattern);
static void write_ota_flags(FILE* file, unsigned int* address);

int main(int argc, char* argv[])
{
unsigned int boot_address = BOOT_START_ADDR;
unsigned int app1_address = APP1_START_ADDR;
unsigned int app2_address = APP2_START_ADDR;

char boot_name[PATH_SIZE] = {0};
char app_name[PATH_SIZE] = {0};
const char* output_name = OUTPUT_PATH;

// 打开配置文件
FILE* config_file = fopen(CONFIG_INI_PATH, "r");
if (config_file == NULL)
{
perror("无法打开配置文件!");
return 1;
}

// 读取Boot路径
if (read_config_line(config_file, boot_name, PATH_SIZE, "未找到[Boot Path]!") != 0)
{
fclose(config_file);
return 1;
}

// 读取Boot文件名
if (read_config_line(config_file, boot_name, PATH_SIZE, "未找到Boot二进制文件!") != 0)
{
fclose(config_file);
return 1;
}

// 跳过空行直到找到APP路径
while (fgets(app_name, PATH_SIZE, config_file) != NULL)
{
if (strstr(app_name, "[APP Path]") != NULL)
{
break;
}
}

// 读取APP文件名
if (read_config_line(config_file, app_name, PATH_SIZE, "未找到APP二进制文件!") != 0)
{
fclose(config_file);
return 1;
}

fclose(config_file);

printf("Boot文件: %s\nApp文件: %s\n", boot_name, app_name);

// 打开Boot文件
FILE* boot_file = fopen(boot_name, "rb");
if (boot_file == NULL)
{
perror("无法打开Boot文件!");
return 1;
}

// 打开App文件
FILE* app_file = fopen(app_name, "rb");
if (app_file == NULL)
{
perror("无法打开App文件!");
fclose(boot_file);
return 1;
}

// 获取文件大小
fseek(boot_file, 0, SEEK_END);
long boot_size = ftell(boot_file);
rewind(boot_file);
printf("Boot大小: %ld 字节\n", boot_size);

fseek(app_file, 0, SEEK_END);
long app_size = ftell(app_file);
rewind(app_file);
printf("App大小: %ld 字节\n", app_size);

// 创建输出文件
FILE* output_file = fopen(output_name, "wb");
if (output_file == NULL)
{
perror("无法创建合并文件!");
fclose(boot_file);
fclose(app_file);
return 1;
}

// 写入Boot内容
unsigned char byte;
while (fread(&byte, 1, 1, boot_file) == 1)
{
if (boot_address > APP1_START_ADDR)
{
printf("错误: Boot文件过大\n");
fclose(boot_file);
fclose(app_file);
fclose(output_file);
return 1;
}
fwrite(&byte, 1, 1, output_file);
boot_address++;
}

// 填充到OTA标志区域
fill_with_pattern(output_file, &boot_address, FLAG_OF_OTA_ADDR, 0xFF);

// 写入OTA标志
write_ota_flags(output_file, &boot_address);

// 写入第一个App副本
rewind(app_file);
while (fread(&byte, 1, 1, app_file) == 1)
{
if (app1_address > APP2_START_ADDR)
{
printf("错误: APP文件过大\n");
fclose(boot_file);
fclose(app_file);
fclose(output_file);
return 1;
}
fwrite(&byte, 1, 1, output_file);
app1_address++;
}

// 填充到第二个App区域
fill_with_pattern(output_file, &app1_address, APP2_START_ADDR, 0xFF);

// 写入第二个App副本
rewind(app_file);
while (fread(&byte, 1, 1, app_file) == 1)
{
if (app2_address > FLASH_END_ADDR)
{
printf("错误: APP文件过大\n");
break;
}
fwrite(&byte, 1, 1, output_file);
app2_address++;
}

// 获取合并后文件大小
fseek(output_file, 0, SEEK_END);
long merge_size = ftell(output_file);
rewind(output_file);
printf("合并文件大小: %ld 字节 (%.2f KB)\n", merge_size, merge_size / 1024.0);

// 校验大小
long expected_size = (APP2_START_ADDR - BOOT_START_ADDR) + app_size;
if (merge_size != expected_size)
{
printf("合并失败!\n");
printf("实际大小: %ld, 预期大小: %ld\n", merge_size, expected_size);
}

// 关闭文件
fclose(boot_file);
fclose(app_file);
fclose(output_file);

printf("合并完成, 已生成文件: '%s'\n", output_name);
return 0;
}

// 从配置文件读取一行并去除换行符
static int read_config_line(FILE* file, char* buffer, size_t size, const char* error_msg)
{
while (fgets(buffer, size, file) != NULL)
{
// 去除换行符
char* newline = strchr(buffer, '\n');
if (newline)
{
*newline = '\0';
}

// 跳过空行
if (buffer[0] != '\0' && buffer[0] != '\r')
{
return 0;
}
}

printf("%s\n", error_msg);
return 1;
}

// 用指定模式填充到目标地址
static void fill_with_pattern(FILE* file, unsigned int* address, unsigned int end_addr, unsigned char pattern)
{
while (*address < end_addr)
{
fwrite(&pattern, 1, 1, file);
(*address)++;
}
}

// 写入OTA标志
static void write_ota_flags(FILE* file, unsigned int* address)
{
// 无FCT升级任务标志
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 4, 0x5A);

// 无需备份标志
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 8, 0xBA);

// APP正在运行标志
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 12, 0x00);

// 无红外升级任务标志
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 16, 0x5A);
}