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) #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; }
if (read_config_line(config_file, boot_name, PATH_SIZE, "未找到[Boot Path]!") != 0) { fclose(config_file); return 1; }
if (read_config_line(config_file, boot_name, PATH_SIZE, "未找到Boot二进制文件!") != 0) { fclose(config_file); return 1; }
while (fgets(app_name, PATH_SIZE, config_file) != NULL) { if (strstr(app_name, "[APP Path]") != NULL) { break; } }
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);
FILE* boot_file = fopen(boot_name, "rb"); if (boot_file == NULL) { perror("无法打开Boot文件!"); return 1; }
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; }
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++; }
fill_with_pattern(output_file, &boot_address, FLAG_OF_OTA_ADDR, 0xFF);
write_ota_flags(output_file, &boot_address);
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++; }
fill_with_pattern(output_file, &app1_address, APP2_START_ADDR, 0xFF);
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)++; } }
static void write_ota_flags(FILE* file, unsigned int* address) { fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 4, 0x5A);
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 8, 0xBA);
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 12, 0x00);
fill_with_pattern(file, address, FLAG_OF_OTA_ADDR + 16, 0x5A); }
|