IAP升级 整合多个bin文件为一个升级包
1、我们的程序分区目前为:
Boot 0x8000000 – 0x8004000 16KB
Settings 0x8004000 – 0x8006000 8KB
App 0x8006000 – 0x800B000 20KB
Download 0x800B000 – 0x800FFFF 20KB
2、烧录时候的bin文件有三个:
Bootloader.bin. 起始地址0x8000000
Settings.bin 起始地址0x8004000
Application.bin 起始地址0x8006000
3、我们新建一个(16+8+20)*1024个字节的数组,将Bootloader.bin从0开始填到数组中,将Settings.bin从0x4000处填充到数组中,将Application.bin 从0x6000填充到数组中,最后写入文件中,即可得到我们想要的all.bin
4、代码路径:
5、代码分析:
generate_bin.c
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "settings.h"
#include "flash.h"
#define APP_BIN_PATH "../Application/build/Application.bin"
#define BOOT_BIN_PATH "../Bootloader/build/Bootloader.bin"
#define SETTING_BIN_PATH "../Settings/build/Settings.bin"
#define ALL_BIN_LENGTH (16 + 8 + 20) * 1024
#define OTA_BIN_LENGTH (8 + 20) * 1024
static int copy_bootloader_bin(MI_U8 *p_buff)
{
FILE *file;
MI_U8 r_buff[BOOT_SIZE] = {0};
size_t r_size;
size_t file_size;
file = fopen(BOOT_BIN_PATH,"rb");
if (file == NULL)
{
printf("fopen BootLoader error\n");
return -1;
}
fseek(file,0,SEEK_END);
file_size = ftell(file);
fseek(file,0,SEEK_SET);
r_size = fread(r_buff,sizeof(MI_U8),sizeof(r_buff),file);
if (r_size != file_size)
{
printf("read bootloader size == %ld\n",r_size);
}
else
{
printf("read bootloader file ok\n" );
}
memcpy(p_buff,r_buff,r_size);
fclose(file);
return 0;
}
static int copy_settings_bin(MI_U8 *p_buff)
{
FILE *file = fopen(SETTING_BIN_PATH,"rb");
MI_U8 r_buff[SETTINGS_SIZE] = {0};
size_t r_size;
size_t file_size;
if (file == NULL)
{
printf("fopen Application error\n");
return -1;
}
fseek(file,0,SEEK_END);
file_size = ftell(file);
fseek(file,0,SEEK_SET);
r_size = fread(r_buff,sizeof(MI_U8),sizeof(r_buff),file);
if (r_size != file_size)
{
printf("read settings size == %ld\n",r_size);
}
else
{
printf("read settings file ok\n" );
}
p_buff += (BOOT_SIZE);
memcpy(p_buff,r_buff,r_size);
fclose(file);
return 0;
}
static int copy_application_bin(MI_U8 *p_buff)
{
FILE *file = fopen(APP_BIN_PATH,"rb");
MI_U8 r_buff[APP_SIZE] = {0};
size_t r_size;
size_t file_size;
if (file == NULL)
{
printf("fopen Application error\n");
return -1;
}
fseek(file,0,SEEK_END);
file_size = ftell(file);
fseek(file,0,SEEK_SET);
r_size = fread(r_buff,sizeof(MI_U8),sizeof(r_buff),file);
if (r_size != file_size)
{
printf("read Application size == %ld\n",r_size);
}
else
{
printf("read Application file ok\n" );
}
p_buff += (BOOT_SIZE + SETTINGS_SIZE);
memcpy(p_buff,r_buff,r_size);
fclose(file);
return 0;
}
static MI_BOOL create_all_bin(void)
{
int ret;
MI_U8 all_data[ALL_BIN_LENGTH] = {0};
size_t w_size = 0;
ret = copy_bootloader_bin(all_data);
if (ret != 0)
{
printf("copy_bootloader_bin error\n");
return -1;
}
ret = copy_settings_bin(all_data);
if (ret != 0)
{
printf("copy_settings_bin error\n");
return -1;
}
ret = copy_application_bin(all_data);
if (ret != 0)
{
printf("copy_application_bin error\n");
return -1;
}
FILE *file = fopen("../output/all.bin","wb");
if (file == NULL)
{
printf("fopen all.bin error\n");
return -1;
}
w_size = fwrite(all_data,1,(BOOT_SIZE + SETTINGS_SIZE + APP_SIZE),file);
fclose(file);
if (w_size == BOOT_SIZE + SETTINGS_SIZE + APP_SIZE)
{
printf("Create bin is Ok\n");
}
else
{
printf("Create bin error\n");
return -1;
}
return MI_TRUE;
}
int main(int argc,char *argv[])
{
if (MI_TRUE == create_all_bin())
printf(FONT_COLOR_BLUE"Create all.bin Success!\n"COLOR_NONE);
else
printf(BACKGROUND_COLOR_RED"Create all.bin Success!\n"COLOR_NONE);
return 0;
}
6、Makefile
CC = gcc
SRC = $(wildcard *.c)
OBJ = $(addprefix $(BUILD_DIR)/,$(notdir $(SRC:.c=.o)))
#OBJ = $(patsubst %.c, %.o, $(SRC))
TRAGET = generate
BUILD_DIR = build
C_INCLUDES += \
-I../Common/Flash \
-I../Common/Settings \
-I../Common
C_FLAG += -Wall -MD -MP -MF"$(@:%.o=%.d)"
C_FLAG += -DUSER_SETTING_IN_HOST
all: $(BUILD_DIR) $(BUILD_DIR)/$(TRAGET)
$(BUILD_DIR)/$(TRAGET):$(OBJ) Makefile | $(BUILD_DIR)
$(CC) $< -o $@
$(OBJ): $(SRC) Makefile | $(BUILD_DIR)
$(CC) -c $(C_FLAG) $(C_INCLUDES) $< -o $@
$(BUILD_DIR):
@-mkdir $@
clean:
@rm -rf $(TRAGET)
@rm -rf $(OBJ)
@rm -rf $(BUILD_DIR)
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***
7、根目录下make后回在output下生成all.bin文件
8、make allbin 烧录到办卡验证。