app启动时如果boot魔数校验错误则自动升级bootloader

This commit is contained in:
andy
2023-10-12 23:37:12 +08:00
parent 17b4ebf188
commit fda9545130
18 changed files with 2865 additions and 1408 deletions

View File

@@ -207,5 +207,6 @@
2023.10.12
移植Test命令验证成功
EJ EX 写工厂信息验证成功
2023.10.12
添加 app_debug 分类 ,app 分类禁用调试,程序压缩到100k以下
app启动时如果boot魔数校验错误则自动升级bootloader

View File

@@ -195,7 +195,7 @@ array_def *protu_encode2(protu_def *p,array_def *data)
protuc_codec_export(ym_coder,protu_decode2,protu_encode2);
//protuc_codec_export(ym_coder,protu_decode2,protu_encode2);

View File

@@ -16,6 +16,7 @@ typedef struct{
uint32_t reboot_curr;// 最后一次复位标志
void (*wdog_refresh)(void);// 刷新看门口
uint32_t reboot_times;// 软件重启次数
uint32_t boot_magic_number;// app校验boot是否为指定版本
}back_def;
@@ -79,7 +80,7 @@ void bk_init(void)
g_back->reboot_curr=g_back->reboot_type;
g_back->reboot_type=REBOOT_INIT;
g_back->wdog_refresh=0;
g_back->boot_magic_number=BOOT_MAGIC_NUM;
// 每软件重启一次就增加计数
if(bk_soft_reboot())
{
@@ -146,4 +147,13 @@ void *bk_wdog_fun(void)
return g_back->wdog_refresh;
}
// 校验魔数,不对返回1
int bk_check_magic_num(void)
{
if(g_back->boot_magic_number!=BOOT_MAGIC_NUM){
return 1;
}
return 0;
}

View File

@@ -14,6 +14,10 @@
#define REBOOT_INIT 0xffff
// 定义boot魔数
#define BOOT_MAGIC_NUM ((uint32_t)(('b')<<0)|(('o')<<8)|(('o')<<16)|(('t')<<24))
void bk_reboot_app(void);
void bk_reboot_boot(void);
void bk_reboot_updata(void);
@@ -26,6 +30,7 @@ const char *bk_get_currtype(void);
void bk_set_wdog_fun(void (*fun)(void));
void *bk_wdog_fun(void);
uint32_t bk_reboot_times(void);
int bk_check_magic_num(void);

View File

@@ -33,6 +33,8 @@ scheme 109~110
*/
#define FLASH_SECTOR_SIZE 2048
#define FLASH_BOOT_SECTOR (0)
#define FLASH_BOOT_SECTOR_NUM (8)
#define FLASH_APP_SECTOR (8)
#define FLASH_APP_SECTOR_NUM (50)
#define FLASH_BUFF_SECTOR (58)
@@ -145,7 +147,16 @@ int flash_operate_end(void)
return 0;
}
// boot区
int flash_erase_boot(void)
{
int ret;
for(int i=0;i<FLASH_BOOT_SECTOR_NUM;i++){
ret=flash_erase(FLASH_BOOT_SECTOR+i);
if(ret!=0) return -1;
}
return 0;
}
// 参数区2K
int flash_erase_param(void)
@@ -340,6 +351,12 @@ int flash_updata_app(uint8_t *rom,uint32_t size)
}
int flash_updata_boot(uint8_t *rom,uint32_t size)
{
uint8_t *dst=(uint8_t *)0x08000000;
flash_erase_boot();
return flash_write(dst,rom,size);
}
// 保存参数

View File

@@ -59,6 +59,7 @@ typedef struct{
uint8_t *flash_get_rom(rom_head **head);
int flash_updata_app(uint8_t *rom,uint32_t size);
int flash_updata_boot(uint8_t *rom,uint32_t size);

View File

@@ -41,6 +41,10 @@
commend_export(cmd_,__msh_##fun_,#desc);
#define ConsoleRxDisable()
#define ConsoleRxEnable()
int ConsoleUsart_send_bytes(uint8_t *d,int len);
int SaveBoardInfo(void);

View File

@@ -178,7 +178,7 @@ typedef struct{
uint8_t *rx_buff;
uint32_t rx_buff_size;
void *sem;
int in_send;
volatile int in_send;
}self_data;

View File

@@ -6,7 +6,7 @@
#define BUILD_DATE "2023-10-12 18:40:51"
#define BUILD_DATE "2023-10-12 23:23:27"
#define SOFT_VERSION "0.03"

View File

@@ -22,14 +22,26 @@
#include "elec_det.h"
// 升级bootloader
static void boot_updata(void)
{
const uint8_t ALIGN(4) bootcode[]={
#include "bootcode.txt"
};
if(sizeof(bootcode)==0)
return;
if(bk_check_magic_num())
{
flash_updata_boot((uint8_t *)bootcode,sizeof(bootcode));
bk_reboot_app();
}
}
int main()
{
const sys_param_def *par=sys_param();
boot_updata();
debug_init();
elec_init();
app_init();
@@ -45,12 +57,7 @@ int main()
connect(protu2,protu_recv_signal,0,cmd,cmd_recv_slot);
connect(cmd,cmd_reply_signal,0,protu2,protu_send_call);
// DBG_LOG("询预压");
// DBG_LOG("我知道");
//--diag_suppress=550,177
while(1)
{
elec_led2_power(1);
@@ -87,7 +94,7 @@ static int test(list_def *argv)
cmd_print("test num=%d,hex=%x",num,num);
return 0;
}
commend_export(test,test,"cmd test")
//commend_export(test,test,"cmd test")
@@ -270,7 +277,7 @@ static int sys_set(list_def *argv)
return 0;
}
commend_export(set,sys_set,"set the system params")
//commend_export(set,sys_set,"set the system params")

View File

@@ -2,7 +2,7 @@ import sys
import os
import datetime
from datetime import datetime, timedelta
import mycopy
@@ -15,6 +15,7 @@ SOFT_VERION = "0.03"
MOD_FILE="mod_signals.c"
MOD_PATH="./source/task/"
INFO_FILE="./source/main/compiler_info.h"
BOOTCODE_ARRAY="./source/main/bootcode.txt"
# 找到文件夹中指定后缀的文件
@@ -137,9 +138,31 @@ def creat_compile_info(dst:str):
f.write("\n\n\n\n\n\n\n#endif\n")
# 生成bootcode数组
def creat_bootcode(dst:str):
if os.path.exists(dst):
os.remove(dst)
data=bytearray()
boot_file=mycopy.BOOT_FILE_SRC+".bin"
if(os.path.exists(boot_file)):
with open(boot_file,"rb") as f:
data=f.read()
with open(dst,"w") as f:
while len(data)>0:
if(len(data)>=20):
t=data[0:20]
data=data[20:]
else:
t=data
data=[]
s=t.hex(" ")
s="0x"+s.replace(" ",",0x")+",\n"
f.write(s)
def main():
creat_bootcode(BOOTCODE_ARRAY)
creat_compile_info(INFO_FILE)
mod_file_creat(MOD_PATH+MOD_FILE)
print("prepare success.\n")

View File

@@ -175,12 +175,17 @@ void param_err_handle(const char *param,const char *file,const char *fun,int lin
void cpy4byte(uint32_t *dst,uint32_t *src,int num_4byte);
#ifdef DEBUG
// 如果s==0,则打印
#define param_check(s) \
if((s)==0){\
param_err_handle(#s,__FILE__,__func__,__LINE__);}
#else
#define param_check(s)
#endif
#ifdef RT_THREAD

View File

@@ -134,18 +134,18 @@
// <h>Console Configuration
// <c1>Using console
// <i>Using console
#define RT_USING_CONSOLE
//#define RT_USING_CONSOLE
// </c>
// <o>the buffer size of console <1-1024>
// <i>the buffer size of console
// <i>Default: 128 (128Byte)
#define RT_CONSOLEBUF_SIZE 128
//#define RT_CONSOLEBUF_SIZE 128
// <s>The device name for console
// <i>The device name for console
// <i>Default: uart2
#define RT_CONSOLE_DEVICE_NAME "uart2"
//#define RT_CONSOLE_DEVICE_NAME "uart2"
// </h>
#if defined(RTE_USING_DEVICE)
@@ -202,5 +202,8 @@
#define IDLE_THREAD_STACK_SIZE 1024
#define RT_USING_CPU_FFS
#endif

View File

@@ -38,7 +38,7 @@
#ifdef DEBUG
typedef struct{
int inited;
@@ -95,7 +95,7 @@ void debug_log(const char *file,const char *fun,int line,int level,const char *f
#endif
}
#endif

View File

@@ -9,8 +9,11 @@
/*r{ 修改日志打印等级 }c*/
#ifdef DEBUG
#define DBG_LOG_LEVEL 0
#else
#define DBG_LOG_LEVEL 4
#endif
/*r{ 定义打印数据等级 }c*/
@@ -51,9 +54,17 @@
//#define DBG_LOG_(type_,msg_,...)\
// debug_log("-","-",__LINE__,type_,(msg_),##__VA_ARGS__)
#ifdef DEBUG
int debug_init(void);
void debug_log(const char *file,const char *fun,int line,int level,const char *fmt, ...);
#else
#define debug_init()
#define debug_log(...)
#endif