| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859 |
- /*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2019-09-22 Warfalcon first version
- */
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <board.h>
- #include <fal.h>
- #include <tinycrypt.h>
- #include <fastlz.h>
- #include <quicklz.h>
- #include <rt_fota.h>
- #include <signal_led.h>
- #include "sys_config.h"
- #include "easyflash.h"
- #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
- #include <finsh.h>
- #include <shell.h>
- #endif
- #include <rt_fota.h>
- extern void HMI_WRITE(char *fmt,...);
- #define DBG_ENABLE
- #define DBG_SECTION_NAME "fota"
- #ifdef RT_FOTA_DEBUG
- #define DBG_LEVEL DBG_LOG
- #else
- #define DBG_LEVEL DBG_INFO
- #endif
- #define DBG_COLOR
- #include <rtdbg.h>
- #ifndef RT_FOTA_THREAD_STACK_SIZE
- #define RT_FOTA_THREAD_STACK_SIZE 4096
- #endif
- #ifndef RT_FOTA_THREAD_PRIORITY
- #define RT_FOTA_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 3)
- #endif
- #ifndef RT_FOTA_ALGO_BUFF_SIZE
- #define RT_FOTA_ALGO_BUFF_SIZE 2048 //2025-0428 CHANGE 2048
- #endif
- /**
- * AES256 encryption algorithm option
- */
- #ifndef RT_FOTA_ALGO_AES_IV
- #define RT_FOTA_ALGO_AES_IV "0123456789ABCDEF"
- #endif
- #ifndef RT_FOTA_ALGO_AES_KEY
- #define RT_FOTA_ALGO_AES_KEY "0123456789ABCDEF0123456789ABCDEF"
- #endif
- #ifndef RT_FOTA_BLOCK_HEADER_SIZE
- #define RT_FOTA_BLOCK_HEADER_SIZE 4
- #endif
- #ifndef RT_FOTA_CMPRS_BUFFER_SIZE
- #define RT_FOTA_CMPRS_BUFFER_SIZE 4096
- #endif
- #ifndef RT_FOTA_FASTLZ_BUFFER_PADDING
- #define RT_FOTA_FASTLZ_BUFFER_PADDING FASTLZ_BUFFER_PADDING(RT_FOTA_CMPRS_BUFFER_SIZE)
- #endif
- #ifndef RT_FOTA_QUICKLZ_BUFFER_PADDING
- #define RT_FOTA_QUICKLZ_BUFFER_PADDING QLZ_BUFFER_PADDING
- #endif
- #ifndef RT_FOTA_ENTER_SHELL_KEY
- #define RT_FOTA_ENTER_SHELL_KEY 0x0d
- #endif
- #ifndef RT_FOTA_GET_CHAR_WAITTIGN
- #define RT_FOTA_GET_CHAR_WAITTIGN (RT_TICK_PER_SECOND * 5)
- #endif
- #ifndef RT_FOTA_SIGNAL_LED_PIN
- #define RT_FOTA_SIGNAL_LED_PIN GET_PIN(B, 3)
- #endif
- #ifndef RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE
- #define RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE 1024
- #endif
- #ifndef RT_FOTA_SIGNAL_LED_THREAD_PRIORITY
- #define RT_FOTA_SIGNAL_LED_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 4)
- #endif
- /* For signal led */
- static led_t *signal_led = NULL;
- static led_mem_opreation_t signal_led_mem_op;
- const char *led_shell_mode = "500,500,"; /* 1Hz ��˸ */
- const char *led_upgrade_mode = "50,50,"; /* 10Hz ��˸ */
- const char *led_off_mode = "0,100,"; /* ���� */
- const char *led_on_mode = "100,0,"; /* ���� */
- static int stop_in_bootloader=0;
- /* ===================== v1.3.0 升级回滚机制 ===================== */
- /* RTC 备份寄存器 DR2 状态约定:
- * 0x0000 —— APP 启动成功:Application_CBD 的 main() 启动后立即写入,
- * 表示 bootloader 本次"启动尝试"已被 APP 确认;
- * 0x5AA5 —— 升级请求:应用侧 JSONRPC "update" 指令写入,bootloader
- * 读取后清 0 并停留执行 ymodem 升级;
- * 0xDEAD —— 启动尝试标记:bootloader 跳转 APP 前写入;若复位进入
- * bootloader 后该值仍在(或为其它非 0 值),说明 APP 未运行
- * 到清零处(未正常启动),将从 df_area 备份回滚 APP 固件。
- * 说明:DR2 为 16 位,位于 BKP_BASE + 4*2,与 Application_CBD 的
- * main.c / dtu_uart2.c 访问方式配对。 */
- #define RT_FOTA_YMODEM_REQ_BKP_MAGIC 0x5AA5
- #define RT_FOTA_BOOT_TRY_BKP_MAGIC 0xDEAD
- /* df_area 备份格式:
- * [0, 4KB) —— rt_fota_backup_head_t 头(magic + 长度 + CRC32),
- * 头部有效即代表存在有效备份;
- * [4KB, 4KB + data_len) —— APP 分区镜像(裸数据,恢复时原样写回)。
- * 写入顺序:先失效旧头 -> 写数据 -> 最后写头,任一步骤掉电都不会残留
- * 半新半旧的"有效备份"。 */
- #define RT_FOTA_BACKUP_MAGIC 0x46424B31 /* "FBK1" */
- #define RT_FOTA_BACKUP_HEAD_RSV 4096 /* 头部预留(独占首扇区) */
- #define RT_FOTA_BACKUP_BLK_SIZE 4096
- typedef struct
- {
- rt_uint32_t magic; /* RT_FOTA_BACKUP_MAGIC */
- rt_uint32_t data_len; /* 备份数据长度(字节) */
- rt_uint32_t data_crc; /* 备份数据 CRC32(rt_fota_crc 语义) */
- rt_uint32_t head_crc; /* 头部前 12 字节 CRC32 */
- } rt_fota_backup_head_t;
- /* 使能 PWR/BKP 时钟并解除备份域写访问保护(幂等,可重复调用) */
- static void rt_fota_bkp_access_enable(void)
- {
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_RCC_BKP_CLK_ENABLE();
- HAL_PWR_EnableBkUpAccess();
- }
- static rt_uint16_t rt_fota_bkp_dr2_read(void)
- {
- rt_fota_bkp_access_enable();
- return *(__IO uint16_t *)(BKP_BASE + 4 * 2);
- }
- static void rt_fota_bkp_dr2_write(rt_uint16_t value)
- {
- rt_fota_bkp_access_enable();
- *(__IO uint16_t *)(BKP_BASE + 4 * 2) = value;
- }
- /* For shell */
- static rt_sem_t shell_sem = RT_NULL;
- static rt_device_t shell_dev = RT_NULL;
- typedef struct {
- char type[4];
- rt_uint16_t fota_algo;
- rt_uint8_t fm_time[6];
- char app_part_name[16];
- char download_version[24];
- char current_version[24];
- rt_uint32_t code_crc;
- rt_uint32_t hash_val;
- rt_uint32_t raw_size;
- rt_uint32_t com_size;
- rt_uint32_t head_crc;
- } rt_fota_part_head, *rt_fota_part_head_t;
- typedef void (*rt_fota_app_func)(void);
- static rt_fota_app_func app_func = RT_NULL;
- static rt_fota_part_head fota_part_head;
- static void rt_fota_signal_led_on(void)
- {
- rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_LOW);
- }
- static void rt_fota_signal_led_off(void)
- {
- rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_HIGH);
- }
- static void rt_fota_signal_led_entry(void *arg)
- {
- while(1)
- {
- led_ticks();
- rt_thread_mdelay(LED_TICK_TIME);
- }
- }
- static void rt_fota_signal_led_init(void)
- {
- rt_pin_mode(RT_FOTA_SIGNAL_LED_PIN, PIN_MODE_OUTPUT);
- signal_led_mem_op.malloc_fn = (void* (*)(size_t))rt_malloc;
- signal_led_mem_op.free_fn = rt_free;
- led_set_mem_operation(&signal_led_mem_op);
- signal_led = led_create(rt_fota_signal_led_on, rt_fota_signal_led_off);
- /* Config signal led mode */
- led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_on_mode);
- led_set_blink_over_callback(signal_led, RT_NULL);
- led_start(signal_led);
- rt_thread_t tid;
- tid = rt_thread_create("sig_led", rt_fota_signal_led_entry, RT_NULL, RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE, RT_FOTA_SIGNAL_LED_THREAD_PRIORITY, 10);
- if (tid)
- rt_thread_startup(tid);
- }
- static void rt_fota_signal_led_mode(const char *led_cfg)
- {
- RT_ASSERT(led_cfg != RT_NULL);
-
- led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_cfg);
- }
- static int rt_fota_boot_verify(void)
- {
- int fota_res = RT_FOTA_NO_ERR;
- rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
-
- /* partition initial */
- fal_init();
- extern int fal_init_check(void);
- /* verify partition */
- if (fal_init_check() != 1)
- {
- LOG_D("Partition initialized failed!");
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_boot_verify;
- }
- __exit_boot_verify:
- return fota_res;
- }
- int rt_fota_part_fw_verify(const char *part_name)
- {
- #define RT_FOTA_CRC_BUFF_SIZE 4096
- #define RT_FOTA_CRC_INIT_VAL 0xffffffff
- int fota_res = RT_FOTA_NO_ERR;
- const struct fal_partition *part;
- rt_fota_part_head part_head;
- rt_uint8_t *body_buf = RT_NULL;
- rt_uint32_t body_crc = RT_FOTA_CRC_INIT_VAL;
- rt_uint32_t hdr_crc;
- if (part_name == RT_NULL)
- {
- LOG_D("Invaild paramenter input!");
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_partition_verify;
- }
- part = fal_partition_find(part_name);
- if (part == RT_NULL)
- {
- LOG_D("Partition[%s] not found.", part_name);
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_partition_verify;
- }
- /* read the head of RBL files */
- if (fal_partition_read(part, 0, (rt_uint8_t *)&part_head, sizeof(rt_fota_part_head)) < 0)
- {
- LOG_D("Partition[%s] read error!", part->name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_partition_verify;
- }
- extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
- hdr_crc = rt_fota_crc((rt_uint8_t *)&part_head, sizeof(rt_fota_part_head) - 4);
- if (hdr_crc != part_head.head_crc)
- {
- LOG_D("Partition[%s] head CRC32 error!", part->name);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_partition_verify;
- }
-
- if (rt_strcmp(part_head.type, "RBL") != 0)
- {
- LOG_D("Partition[%s] type[%s] not surport.", part->name, part_head.type);
- fota_res = RT_FOTA_CHECK_FAILED;
- goto __exit_partition_verify;
- }
- if (fal_partition_find(part_head.app_part_name) == RT_NULL)
- {
- LOG_D("Partition[%s] not found.", part_head.app_part_name);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_partition_verify;
- }
- body_buf = rt_malloc(RT_FOTA_CRC_BUFF_SIZE);
- if (body_buf == RT_NULL)
- {
- LOG_D("Not enough memory for body CRC32 verify.");
- fota_res = RT_FOTA_NO_MEM_ERR;
- goto __exit_partition_verify;
- }
- for (int body_pos = 0; body_pos < part_head.com_size;)
- {
- int body_read_len = fal_partition_read(part, sizeof(rt_fota_part_head) + body_pos, body_buf, RT_FOTA_CRC_BUFF_SIZE);
- if (body_read_len > 0)
- {
- if ((body_pos + body_read_len) > part_head.com_size)
- {
- body_read_len = part_head.com_size - body_pos;
- }
-
- extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
- body_crc = rt_fota_step_crc(body_crc, body_buf, body_read_len);
- body_pos = body_pos + body_read_len;
- }
- else
- {
- LOG_D("Partition[%s] read error!", part->name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_partition_verify;
- }
- }
- body_crc = body_crc ^ RT_FOTA_CRC_INIT_VAL;
-
- if (body_crc != part_head.code_crc)
- {
- LOG_D("Partition[%s] firmware integrity verify failed.", part->name);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_partition_verify;
- }
- __exit_partition_verify:
- if (fota_res == RT_FOTA_NO_ERR)
- {
- rt_enter_critical();
- rt_memcpy(&fota_part_head, &part_head, sizeof(rt_fota_part_head));
- rt_exit_critical();
- LOG_D("partition[%s] verify success!", part->name);
- }
- else
- {
- rt_enter_critical();
- rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
- rt_exit_critical();
-
- LOG_D("Partition[%s] verify failed!", part->name);
- }
- if (body_buf)
- rt_free(body_buf);
-
- return fota_res;
- }
- int rt_fota_check_upgrade(void)
- {
- int is_upgrade = 0;
- if (rt_strcmp(fota_part_head.download_version, fota_part_head.current_version) != 0)
- {
- is_upgrade = 1;
- LOG_D("Application need upgrade.");
- goto __exit_check_upgrade;
- }
- __exit_check_upgrade:
- return is_upgrade;
- }
- int rt_fota_copy_version(const char *part_name)
- {
- #define THE_NOR_FLASH_GRANULARITY 4096
- int fota_res = RT_FOTA_NO_ERR;
- const struct fal_partition *part;
-
- rt_fota_part_head_t part_head = RT_NULL;
- rt_uint8_t *cache_buf = RT_NULL;
- part = fal_partition_find(part_name);
- if (part == RT_NULL)
- {
- LOG_D("Find partition[%s] not found.", part_name);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_copy_version;
- }
-
- cache_buf = rt_malloc(THE_NOR_FLASH_GRANULARITY);
- if (cache_buf == RT_NULL)
- {
- LOG_D("Not enough memory for head erase.");
- fota_res = RT_FOTA_NO_MEM_ERR;
- goto __exit_copy_version;
- }
- part_head = (rt_fota_part_head_t)cache_buf;
-
- if (fal_partition_read(part, 0, cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
- {
- LOG_I("Read partition[%s] failed.", part_name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_copy_version;
- }
-
- rt_memcpy(part_head->current_version, part_head->download_version, sizeof(part_head->current_version));
- extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
- part_head->head_crc = rt_fota_crc((rt_uint8_t *)part_head, sizeof(rt_fota_part_head) - 4);
-
- if (fal_partition_erase(part, 0, THE_NOR_FLASH_GRANULARITY) < 0)
- {
- LOG_D("Erase partition[%s] failed.", part_name);
- fota_res = RT_FOTA_PART_ERASE_ERR;
- goto __exit_copy_version;
- }
-
- if (fal_partition_write(part, 0, (const rt_uint8_t *)cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
- {
- LOG_I("Write partition[%s] failed.", part_name);
- fota_res = RT_FOTA_PART_WRITE_ERR;
- goto __exit_copy_version;
- }
- __exit_copy_version:
- if (cache_buf)
- rt_free(cache_buf);
-
- if (fota_res != RT_FOTA_NO_ERR)
- LOG_I("Copy firmware version failed!");
- else
- LOG_I("Copy firmware version Success!");
-
- return fota_res;
- }
- int rt_fota_erase_app_part(void)
- {
- int fota_res = RT_FOTA_NO_ERR;
- const struct fal_partition *part;
- part = fal_partition_find(fota_part_head.app_part_name);
- if (part == RT_NULL)
- {
- LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_partition_erase;
- }
-
- LOG_I("Partition[%s] erase start:", part->name);
- if (fal_partition_erase(part, 0, fota_part_head.raw_size) < 0)
- {
- LOG_D("Partition[%s] erase failed!", part->name);
- fota_res = RT_FOTA_PART_ERASE_ERR;
- goto __exit_partition_erase;
- }
- __exit_partition_erase:
- if (fota_res == RT_FOTA_NO_ERR)
- {
- LOG_D("Partition[%s] erase %d bytes success!", part->name, fota_part_head.raw_size);
- }
- return fota_res;
- }
- int rt_fota_write_app_part(int fw_pos, rt_uint8_t *fw_buf, int fw_len)
- {
- int rt_fota_res = RT_FOTA_NO_ERR;
- const struct fal_partition *part;
- part = fal_partition_find(fota_part_head.app_part_name);
- if (part == RT_NULL)
- {
- LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
- rt_fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __partition_write_exit;
- }
- if (fal_partition_write(part, fw_pos, fw_buf, fw_len) < 0)
- {
- LOG_D("Partition[%s] write failed!", part->name);
- rt_fota_res = RT_FOTA_PART_WRITE_ERR;
- goto __partition_write_exit;
- }
- __partition_write_exit:
- if (rt_fota_res == RT_FOTA_NO_ERR)
- {
- LOG_D("Partition[%s] write %d bytes success!", part->name, fw_len);
- }
- return rt_fota_res;
- }
- static int rt_fota_read_part(const struct fal_partition *part, int read_pos, tiny_aes_context *aes_ctx, rt_uint8_t *aes_iv, rt_uint8_t *decrypt_buf, rt_uint32_t decrypt_len)
- {
- int fota_err = RT_FOTA_NO_ERR;
- rt_uint8_t *encrypt_buf = RT_NULL;
- if ((part == RT_NULL) || (decrypt_buf == RT_NULL)
- || (decrypt_len % 16 != 0) || (decrypt_len > RT_FOTA_ALGO_BUFF_SIZE))
- {
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_read_decrypt;
- }
- rt_memset(decrypt_buf, 0x0, decrypt_len);
- /* Not use AES256 algorithm */
- if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
- {
- fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, decrypt_buf, decrypt_len);
- if (fota_err <= 0)
- {
- fota_err = RT_FOTA_PART_READ_ERR;
- }
- goto __exit_read_decrypt;
- }
- encrypt_buf = rt_malloc(decrypt_len);
- if (encrypt_buf == RT_NULL)
- {
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_read_decrypt;
- }
- rt_memset(encrypt_buf, 0x0, decrypt_len);
- fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, encrypt_buf, decrypt_len);
- if (fota_err <= 0 || fota_err % 16 != 0)
- {
- fota_err = RT_FOTA_PART_READ_ERR;
- goto __exit_read_decrypt;
- }
- tiny_aes_crypt_cbc(aes_ctx, AES_DECRYPT, fota_err, aes_iv, encrypt_buf, decrypt_buf);
- __exit_read_decrypt:
- if (encrypt_buf)
- rt_free(encrypt_buf);
-
- return fota_err;
- }
- int rt_fota_upgrade(const char *part_name)
- {
- int fota_err = RT_FOTA_NO_ERR;
-
- const struct fal_partition *part;
- rt_fota_part_head_t part_head = RT_NULL;
-
- tiny_aes_context *aes_ctx = RT_NULL;
- rt_uint8_t *aes_iv = RT_NULL;
- rt_uint8_t *crypt_buf = RT_NULL;
-
- int fw_raw_pos = 0;
- int fw_raw_len = 0;
- rt_uint32_t total_copy_size = 0;
- rt_uint8_t block_hdr_buf[RT_FOTA_BLOCK_HEADER_SIZE];
- rt_uint32_t block_hdr_pos = RT_FOTA_ALGO_BUFF_SIZE;
- rt_uint32_t block_size = 0;
- rt_uint32_t dcprs_size = 0;
-
- qlz_state_decompress *dcprs_state = RT_NULL;
- rt_uint8_t *cmprs_buff = RT_NULL;
- rt_uint8_t *dcprs_buff = RT_NULL;
- rt_uint32_t padding_size = 0;
- if (part_name == RT_NULL)
- {
- LOG_D("Invaild paramenter input!");
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- part = fal_partition_find(part_name);
- if (part == RT_NULL)
- {
- LOG_D("Upgrade partition[%s] not found.", part_name);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
-
- /* Application partition erase */
- fota_err = rt_fota_erase_app_part();
- if (fota_err != RT_FOTA_NO_ERR)
- {
- goto __exit_upgrade;
- }
- /* rt_fota_erase_app_part() has check fota_part_head vaild already */
- part_head = &fota_part_head;
- crypt_buf = rt_malloc(RT_FOTA_ALGO_BUFF_SIZE);
- if (crypt_buf == RT_NULL)
- {
- LOG_D("Not enough memory for firmware buffer.");
- fota_err = RT_FOTA_NO_MEM_ERR;
- goto __exit_upgrade;
- }
- /* AES256 algorithm enable */
- if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
- {
- aes_ctx = rt_malloc(sizeof(tiny_aes_context));
- aes_iv = rt_malloc(rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
- if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
- {
- LOG_D("Not enough memory for firmware hash verify.");
- fota_err = RT_FOTA_NO_MEM_ERR;
- goto __exit_upgrade;
- }
- rt_memset(aes_iv, 0x0, rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
- rt_memcpy(aes_iv, RT_FOTA_ALGO_AES_IV, rt_strlen(RT_FOTA_ALGO_AES_IV));
- tiny_aes_setkey_dec(aes_ctx, (rt_uint8_t *)RT_FOTA_ALGO_AES_KEY, 256);
- }
- else if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
- {
- LOG_I("Not surpport XOR.");
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
-
- /* If enable fastlz compress function */
- if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
- {
- cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_FASTLZ_BUFFER_PADDING);
- dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
- if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL)
- {
- LOG_D("Not enough memory for firmware hash verify.");
- fota_err = RT_FOTA_NO_MEM_ERR;
- goto __exit_upgrade;
- }
- padding_size = RT_FOTA_FASTLZ_BUFFER_PADDING;
- }
- else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
- {
- cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_QUICKLZ_BUFFER_PADDING);
- dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
- dcprs_state = rt_malloc(sizeof(qlz_state_decompress));
- if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL || dcprs_state == RT_NULL)
- {
- LOG_D("Not enough memory for firmware hash verify.");
- fota_err = RT_FOTA_NO_MEM_ERR;
- goto __exit_upgrade;
- }
- padding_size = RT_FOTA_QUICKLZ_BUFFER_PADDING;
- rt_memset(dcprs_state, 0x0, sizeof(qlz_state_decompress));
- }
- else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
- {
- LOG_I("Not surpport GZIP.");
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- LOG_I("Start to copy firmware from %s to %s partition:", part->name, part_head->app_part_name);
- while (fw_raw_pos < part_head->com_size)
- {
- if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
- {
- if (block_hdr_pos >= RT_FOTA_ALGO_BUFF_SIZE)
- {
- fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
- if (fw_raw_len < 0)
- {
- LOG_D("AES256 algorithm failed.");
- fota_err = RT_FOTA_PART_READ_ERR;
- goto __exit_upgrade;
- }
- fw_raw_pos += fw_raw_len;
- rt_memcpy(block_hdr_buf, crypt_buf, RT_FOTA_BLOCK_HEADER_SIZE);
- block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
- /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
- if ((fw_raw_len < RT_FOTA_BLOCK_HEADER_SIZE) ||
- (block_size > (rt_uint32_t)(fw_raw_len - RT_FOTA_BLOCK_HEADER_SIZE)) ||
- (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size)))
- {
- LOG_D("Invalid compressed block size(%d).", block_size);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
- rt_memcpy(cmprs_buff, &crypt_buf[RT_FOTA_BLOCK_HEADER_SIZE], block_size);
- block_hdr_pos = RT_FOTA_BLOCK_HEADER_SIZE + block_size;
- }
- else
- {
- rt_uint8_t hdr_tmp_pos = 0;
- while (block_hdr_pos < RT_FOTA_ALGO_BUFF_SIZE)
- {
- if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
- {
- block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
- }
- else
- {
- block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
- /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
- if (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size))
- {
- LOG_D("Invalid compressed block size(%d).", block_size);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
-
- rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
- if (block_size > (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos))
- {
- rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos));
- fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
- if (fw_raw_len < 0)
- {
- LOG_D("AES256 algorithm failed.");
- fota_err = RT_FOTA_PART_READ_ERR;
- goto __exit_upgrade;
- }
- fw_raw_pos += fw_raw_len;
- /* 校验跨缓冲块的剩余长度不超过本次读取的字节数 */
- if (((block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE) > (rt_uint32_t)fw_raw_len)
- {
- LOG_D("Invalid compressed block size(%d).", block_size);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- rt_memcpy(&cmprs_buff[RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos], &crypt_buf[0], (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE);
- block_hdr_pos = (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE;
- }
- else
- {
- rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
- block_hdr_pos = block_hdr_pos + block_size;
- }
- break;
- }
- }
-
- if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
- {
- fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
- if (fw_raw_len < 0)
- {
- LOG_D("AES256 algorithm failed.");
- fota_err = RT_FOTA_PART_READ_ERR;
- goto __exit_upgrade;
- }
- fw_raw_pos += fw_raw_len;
- block_hdr_pos = 0;
- while (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
- {
- block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
- }
- block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
- /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
- if ((fw_raw_len < (int)block_hdr_pos) ||
- (block_size > (rt_uint32_t)(fw_raw_len - block_hdr_pos)) ||
- (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size)))
- {
- LOG_D("Invalid compressed block size(%d).", block_size);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
- rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
- block_hdr_pos = (block_hdr_pos + block_size) % RT_FOTA_ALGO_BUFF_SIZE;
- }
- }
- rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
- if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
- {
- dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
- }
- else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
- {
- /* 校验解压长度:qlz 解压长度由流头自报,必须在目标缓冲区容量内 */
- if (qlz_size_decompressed((const char *)&cmprs_buff[0]) > RT_FOTA_CMPRS_BUFFER_SIZE)
- {
- LOG_D("Invalid quicklz decompressed size in upgrade package.");
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
- }
-
- if (dcprs_size <= 0)
- {
- LOG_D("Decompress failed: %d.", dcprs_size);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
- {
- fota_err = RT_FOTA_COPY_FAILED;
- goto __exit_upgrade;
- }
- total_copy_size += dcprs_size;
- rt_kprintf("#");
- }
- /* no compress option */
- else
- {
- fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
- if (fw_raw_len < 0)
- {
- LOG_D("AES256 algorithm failed.");
- fota_err = RT_FOTA_PART_READ_ERR;
- goto __exit_upgrade;
- }
- fw_raw_pos += fw_raw_len;
- if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
- {
- fota_err = RT_FOTA_COPY_FAILED;
- goto __exit_upgrade;
- }
-
- total_copy_size += fw_raw_len;
- rt_kprintf("#");
- }
- }
- /* it has compress option */
- if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
- {
- while (total_copy_size < part_head->raw_size)
- {
- if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
- {
- rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
- block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
- if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
- {
- rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
- rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
- rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
-
- block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
- if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
- {
- dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
- }
- else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
- {
- /* 校验解压长度:qlz 解压长度由流头自报,必须在目标缓冲区容量内 */
- if (qlz_size_decompressed((const char *)&cmprs_buff[0]) > RT_FOTA_CMPRS_BUFFER_SIZE)
- {
- LOG_D("Invalid quicklz decompressed size in upgrade package.");
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
- }
-
- if (dcprs_size <= 0)
- {
- LOG_D("Decompress failed: %d.", dcprs_size);
- fota_err = RT_FOTA_GENERAL_ERR;
- goto __exit_upgrade;
- }
- if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
- {
- fota_err = RT_FOTA_COPY_FAILED;
- goto __exit_upgrade;
- }
- total_copy_size += dcprs_size;
- rt_kprintf("#");
- }
- else
- {
- break;
- }
- }
- else
- {
- break;
- }
- }
- }
- rt_kprintf("\r\n");
- /* �п�������ֵ�����,��ΪAES��Ҫ���16�ֽ�����,�����Ľ��ܽ�ѹֵ�Ĵ������������Ǵ��ڵ���raw_size */
- /* �ȽϺõķ�������һ��У��,Ŀǰ��������HASH_CODE�㷨��֪�� */
- if (total_copy_size < part_head->raw_size)
- {
- LOG_D("Decompress check failed.");
- fota_err = RT_FOTA_GENERAL_ERR;
- }
- __exit_upgrade:
- if (aes_ctx)
- rt_free(aes_ctx);
- if (aes_iv)
- rt_free(aes_iv);
- if (crypt_buf)
- rt_free(crypt_buf);
- if (cmprs_buff)
- rt_free(cmprs_buff);
- if (dcprs_buff)
- rt_free(dcprs_buff);
- if (dcprs_state)
- rt_free(dcprs_state);
- if (fota_err == RT_FOTA_NO_ERR)
- {
- LOG_I("Upgrade success, total %d bytes.", total_copy_size);
- }
- return fota_err;
- }
- int rt_fota_start_application(void)
- {
- int fota_res = RT_FOTA_NO_ERR;
- const struct fal_partition *part;
- rt_uint32_t app_addr;
- part = fal_partition_find(RT_FOTA_APP_PART_NAME);
- if (part == RT_NULL)
- {
- LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_start_application;
- }
- app_addr = part->offset + 0x08000000;
- //�ж��Ƿ�Ϊ0x08XXXXXX.
- if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
- {
- LOG_I("Illegal Flash code.");
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_start_application;
- }
- // ���ջ����ַ�Ƿ�Ϸ�.
- if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
- {
- LOG_I("Illegal Stack code.");
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_start_application;
- }
- /* v1.3.0:写"启动尝试"标记(0xDEAD)——APP 启动后(main 开头)会清零;
- * 若复位后仍残留,说明 APP 未正常启动,由 rt_fota_check_boot_state() 触发回滚 */
- rt_fota_bkp_dr2_write(RT_FOTA_BOOT_TRY_BKP_MAGIC);
- LOG_I("Implement application now.");
- __disable_irq();
- /* 禁用并清除全部 NVIC 中断(原循环因 F1 无 FPU_IRQn 被注释,改用寄存器遍历),
- * 防止 APP 启动初期残留/挂起中断使用旧向量表或未初始化外设 */
- for (int nvic_idx = 0; nvic_idx < 8; nvic_idx++)
- {
- NVIC->ICER[nvic_idx] = 0xFFFFFFFF;
- NVIC->ICPR[nvic_idx] = 0xFFFFFFFF;
- }
- /* 清除可能挂起的系统异常(PendSV/SysTick) */
- SCB->ICSR |= SCB_ICSR_PENDSVCLR_Msk;
- SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;
- /* 停止 SysTick */
- SysTick->CTRL = 0;
- SysTick->LOAD = 0;
- SysTick->VAL = 0;
- /* 外设与时钟恢复到默认状态 */
- HAL_DeInit();
- HAL_RCC_DeInit();
- HAL_DeInit();
- /* 向量表预先指向 APP(其 SystemInit 会先写回 0x08000000,此步保证交接窗口安全) */
- SCB->VTOR = app_addr;
- __DSB();
- __ISB();
- //用户代码区第二个字为程序开始地址(复位地址)
- app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
- /* Configure main stack */
- __set_MSP(*(__IO uint32_t *)app_addr);
- /* 由线程(PSP)切回 MSP:APP 以复位状态(CONTROL=0)运行 */
- __set_CONTROL(0);
- /* 恢复中断屏蔽(PRIMASK=0),以接近复位后的状态进入 APP */
- __enable_irq();
- /* jump to application */
- app_func();
-
- __exit_start_application:
- LOG_I("Implement application failed.");
- HMI_WRITE("Implement application failed.");
- return fota_res;
- }
- static rt_err_t rt_fota_get_shell_key(void)
- {
- char ch;
- rt_err_t res = RT_EOK;
- rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
- rt_tick_t tick_start, tick_stop;
- RT_ASSERT(shell_dev != RT_NULL);
- RT_ASSERT(shell_sem != RT_NULL);
- rt_tick_set(0);
- tick_start = rt_tick_get();
- while (1)
- {
- if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
- {
- if (rt_sem_take(shell_sem, timeout) != RT_EOK)
- {
- res = RT_ERROR;
- goto __exit_get_shell_key;
- }
- }
-
- if (ch == 0x0d)
- goto __exit_get_shell_key;
- tick_stop = rt_tick_get();
- if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
- {
- res = RT_ERROR;
- goto __exit_get_shell_key;
- }
- timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
- }
- __exit_get_shell_key:
- if(stop_in_bootloader!=0)
- {
- res = RT_EOK;
- }
- return res;
- }
- static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
- {
- RT_ASSERT(shell_sem != RT_NULL);
- /* release semaphore to let fota thread rx data */
- rt_sem_release(shell_sem);
- return RT_EOK;
- }
- static rt_err_t rt_fota_set_device(const char *device_name)
- {
- rt_device_t dev;
- RT_ASSERT(device_name != RT_NULL);
-
- dev = rt_device_find(device_name);
- if (dev == RT_NULL)
- {
- LOG_D("Can not find device: %s.", device_name);
- return RT_ERROR;
- }
- if (shell_sem)
- rt_sem_delete(shell_sem);
-
- shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
- if (shell_sem == RT_NULL)
- return RT_ERROR;
- if (dev == shell_dev)
- return RT_EOK;
-
- if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
- {
- if (shell_dev != RT_NULL)
- {
- /* close old finsh device */
- rt_device_close(shell_dev);
- rt_device_set_rx_indicate(shell_dev, RT_NULL);
- }
- shell_dev = dev;
- rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
-
- LOG_D("Shell device %s open success.", device_name);
- return RT_EOK;
- }
-
- LOG_D("Shell device %s open failed.", device_name);
- return RT_ERROR;
- }
- #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
- static rt_err_t rt_fota_check_defalut_key(void)
- {
- int chk_idx;
-
- /* GPIO initialized */
- rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
- /* Delay for power up */
- rt_thread_mdelay(500);
- if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
- rt_kprintf("Default firmware key pressed:\n");
-
- /* Check GPIO status */
- for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
- {
- rt_thread_mdelay(RT_TICK_PER_SECOND);
- rt_kprintf(">");
- }
- if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
- return RT_ERROR;
- else
- return RT_EOK;
- }
- #endif
- /* 检测应用侧写入的 RTC_BKP_DR2 升级请求(值约定见文件头 v1.3.0 区块):
- * 命中 0x5AA5 时清除标志并返回 RT_EOK(请求 ymodem 升级) */
- static rt_err_t rt_fota_check_ymodem_request(void)
- {
- rt_uint16_t boot_flag;
- boot_flag = rt_fota_bkp_dr2_read();
- if (boot_flag != RT_FOTA_YMODEM_REQ_BKP_MAGIC)
- {
- return RT_ERROR;
- }
- /* 先清除标志:避免 ymodem 完成复位后再次进入升级模式(app 启动时也会清除) */
- rt_fota_bkp_dr2_write(0x0000);
- return RT_EOK;
- }
- /* ===================== v1.3.0 APP 固件备份 / 回滚 ===================== */
- /* 将当前 APP 分区固件备份到 df_area(升级搬运前自动调用;uart4 "backupapp" 手动触发)。
- * 流程:失效旧头 -> 读 APP 计算 CRC -> 擦数据区 -> 回写数据 -> 最后写头。
- * 返回 RT_FOTA_NO_ERR 表示备份有效。 */
- int rt_fota_backup_app_to_df(void)
- {
- rt_fota_backup_head_t head;
- const struct fal_partition *app_part;
- const struct fal_partition *df_part;
- rt_uint8_t *buf = RT_NULL;
- rt_uint32_t data_len = 0, pos, read_need, read_len, crc, erase_len;
- int fota_res = RT_FOTA_NO_ERR;
- app_part = fal_partition_find(RT_FOTA_APP_PART_NAME);
- df_part = fal_partition_find(RT_FOTA_DF_PART_NAME);
- if ((app_part == RT_NULL) || (df_part == RT_NULL))
- {
- LOG_I("Backup: partition not found.");
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_backup;
- }
- /* 数据区(头部预留 + APP 大小)必须容得下 */
- data_len = app_part->len;
- erase_len = RT_ALIGN(data_len, RT_FOTA_BACKUP_BLK_SIZE);
- if ((RT_FOTA_BACKUP_HEAD_RSV + erase_len) > df_part->len)
- {
- LOG_I("Backup: no enough space in [%s].", df_part->name);
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_backup;
- }
- buf = rt_malloc(RT_FOTA_BACKUP_BLK_SIZE);
- if (buf == RT_NULL)
- {
- LOG_I("Backup: no memory.");
- fota_res = RT_FOTA_NO_MEM_ERR;
- goto __exit_backup;
- }
- /* 1. 先擦除备份头:旧备份即刻失效,中途掉电不会残留旧的"有效"magic */
- if (fal_partition_erase(df_part, 0, RT_FOTA_BACKUP_HEAD_RSV) < 0)
- {
- LOG_I("Backup: erase backup head failed.");
- fota_res = RT_FOTA_PART_ERASE_ERR;
- goto __exit_backup;
- }
- /* 2. 读取 APP 分区并计算数据 CRC32 */
- extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
- crc = 0xffffffff;
- for (pos = 0; pos < data_len;)
- {
- read_need = data_len - pos;
- if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
- read_need = RT_FOTA_BACKUP_BLK_SIZE;
- read_len = fal_partition_read(app_part, pos, buf, read_need);
- if (read_len <= 0)
- {
- LOG_I("Backup: read [%s] failed.", app_part->name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_backup;
- }
- crc = rt_fota_step_crc(crc, buf, read_len);
- pos += read_len;
- rt_kprintf("#");
- }
- crc ^= 0xffffffff;
- /* 3. 擦除数据区 */
- if (fal_partition_erase(df_part, RT_FOTA_BACKUP_HEAD_RSV, erase_len) < 0)
- {
- LOG_I("Backup: erase data area failed.");
- fota_res = RT_FOTA_PART_ERASE_ERR;
- goto __exit_backup;
- }
- /* 4. 回写 APP 数据到 df_area */
- for (pos = 0; pos < data_len;)
- {
- read_need = data_len - pos;
- if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
- read_need = RT_FOTA_BACKUP_BLK_SIZE;
- read_len = fal_partition_read(app_part, pos, buf, read_need);
- if (read_len <= 0)
- {
- LOG_I("Backup: read [%s] failed.", app_part->name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_backup;
- }
- if (fal_partition_write(df_part, RT_FOTA_BACKUP_HEAD_RSV + pos, buf, read_len) < 0)
- {
- LOG_I("Backup: write [%s] failed.", df_part->name);
- fota_res = RT_FOTA_PART_WRITE_ERR;
- goto __exit_backup;
- }
- pos += read_len;
- HMI_WRITE("debug.update.val=%d", (int)(pos * 100 / data_len));
- rt_kprintf("#");
- }
- rt_kprintf("\r\n");
- /* 5. 最后写头部:备份自此生效 */
- head.magic = RT_FOTA_BACKUP_MAGIC;
- head.data_len = data_len;
- head.data_crc = crc;
- extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
- head.head_crc = rt_fota_crc((rt_uint8_t *)&head, sizeof(head) - 4);
- if (fal_partition_write(df_part, 0, (const rt_uint8_t *)&head, sizeof(head)) < 0)
- {
- LOG_I("Backup: write backup head failed.");
- fota_res = RT_FOTA_PART_WRITE_ERR;
- goto __exit_backup;
- }
- __exit_backup:
- if (buf)
- rt_free(buf);
- if (fota_res == RT_FOTA_NO_ERR)
- LOG_I("Backup app to df_area success, %d bytes.", data_len);
- return fota_res;
- }
- /* 将 df_area 中的有效备份固件恢复到 APP 分区(带完整性校验;供启动失败自动
- * 回滚、uart4 的 "dbg_restore" / "resetapp" 命令共用)。
- * consume_backup:恢复成功后是否消费(擦除)备份头——自动回滚置 1,防止"恢复
- * 的固件也无法启动"时反复恢复擦写 flash;手动命令置 0,保留备份便于重复调试。
- * 返回 RT_FOTA_NO_ERR 表示恢复完成。 */
- int rt_fota_restore_app_from_df(int consume_backup)
- {
- rt_fota_backup_head_t head;
- const struct fal_partition *app_part;
- const struct fal_partition *df_part;
- rt_uint8_t *buf = RT_NULL;
- rt_uint32_t pos, read_need, read_len, crc;
- int fota_res = RT_FOTA_NO_ERR;
- rt_memset(&head, 0, sizeof(head));
- app_part = fal_partition_find(RT_FOTA_APP_PART_NAME);
- df_part = fal_partition_find(RT_FOTA_DF_PART_NAME);
- if ((app_part == RT_NULL) || (df_part == RT_NULL))
- {
- LOG_I("Restore: partition not found.");
- fota_res = RT_FOTA_GENERAL_ERR;
- goto __exit_restore;
- }
- /* 1. 读备份头并校验(magic + 头 CRC32 + 长度合法性) */
- if (fal_partition_read(df_part, 0, (rt_uint8_t *)&head, sizeof(head)) < 0)
- {
- LOG_I("Restore: read backup head failed.");
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_restore;
- }
- extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
- if ((head.magic != RT_FOTA_BACKUP_MAGIC) ||
- (rt_fota_crc((rt_uint8_t *)&head, sizeof(head) - 4) != head.head_crc))
- {
- LOG_I("Restore: no valid backup in [%s].", df_part->name);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_restore;
- }
- if ((head.data_len == 0) ||
- (head.data_len > app_part->len) ||
- ((RT_FOTA_BACKUP_HEAD_RSV + head.data_len) > df_part->len))
- {
- LOG_I("Restore: invalid backup length(%d).", head.data_len);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_restore;
- }
- buf = rt_malloc(RT_FOTA_BACKUP_BLK_SIZE);
- if (buf == RT_NULL)
- {
- LOG_I("Restore: no memory.");
- fota_res = RT_FOTA_NO_MEM_ERR;
- goto __exit_restore;
- }
- /* 2. 备份数据 CRC32 校验(先校验后擦写,避免把无效数据写入 APP 分区) */
- extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
- crc = 0xffffffff;
- for (pos = 0; pos < head.data_len;)
- {
- read_need = head.data_len - pos;
- if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
- read_need = RT_FOTA_BACKUP_BLK_SIZE;
- read_len = fal_partition_read(df_part, RT_FOTA_BACKUP_HEAD_RSV + pos, buf, read_need);
- if (read_len <= 0)
- {
- LOG_I("Restore: read [%s] failed.", df_part->name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_restore;
- }
- crc = rt_fota_step_crc(crc, buf, read_len);
- pos += read_len;
- rt_kprintf("#");
- }
- crc ^= 0xffffffff;
- if (crc != head.data_crc)
- {
- LOG_I("Restore: backup data CRC32 error (0x%08X != 0x%08X).", crc, head.data_crc);
- fota_res = RT_FOTA_FW_VERIFY_FAILED;
- goto __exit_restore;
- }
- /* 3. 擦除 APP 分区并回写备份数据 */
- if (fal_partition_erase(app_part, 0, app_part->len) < 0)
- {
- LOG_I("Restore: erase [%s] failed.", app_part->name);
- fota_res = RT_FOTA_PART_ERASE_ERR;
- goto __exit_restore;
- }
- for (pos = 0; pos < head.data_len;)
- {
- read_need = head.data_len - pos;
- if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
- read_need = RT_FOTA_BACKUP_BLK_SIZE;
- read_len = fal_partition_read(df_part, RT_FOTA_BACKUP_HEAD_RSV + pos, buf, read_need);
- if (read_len <= 0)
- {
- LOG_I("Restore: read [%s] failed.", df_part->name);
- fota_res = RT_FOTA_PART_READ_ERR;
- goto __exit_restore;
- }
- if (fal_partition_write(app_part, pos, buf, read_len) < 0)
- {
- LOG_I("Restore: write [%s] failed.", app_part->name);
- fota_res = RT_FOTA_PART_WRITE_ERR;
- goto __exit_restore;
- }
- pos += read_len;
- HMI_WRITE("debug.update.val=%d", (int)(pos * 100 / head.data_len));
- rt_kprintf("#");
- }
- rt_kprintf("\r\n");
- /* 4. 自动回滚场景消费备份,防止"恢复的固件仍无法启动"反复恢复 */
- if (consume_backup)
- {
- if (fal_partition_erase(df_part, 0, RT_FOTA_BACKUP_HEAD_RSV) < 0)
- LOG_I("Restore: consume backup head failed.");
- else
- LOG_I("Restore: backup consumed.");
- }
- __exit_restore:
- if (buf)
- rt_free(buf);
- if (fota_res == RT_FOTA_NO_ERR)
- LOG_I("Restore app from df_area success, %d bytes.", head.data_len);
- else
- LOG_I("Restore app from df_area failed(%d).", fota_res);
- return fota_res;
- }
- /* 启动失败检测与自动回滚:进入 bootloader 后读取 RTC_BKP_DR2——
- * 0 / 0x5AA5 分别表示上次启动正常 / 升级请求(由 ymodem 检测处理),跳过;
- * 其它非 0 值(跳转前写入的 0xDEAD 未被 APP 清零)说明 APP 未正常启动:
- * 标记 0xDEAD -> 有有效备份则回滚 APP -> 对齐 fm_area 版本,防止同一升级包
- * 在回滚后再次被自动应用(用户重新下发升级包会重写 fm_area,不受影响)。 */
- static void rt_fota_check_boot_state(void)
- {
- rt_uint16_t boot_flag = rt_fota_bkp_dr2_read();
- if ((boot_flag == 0x0000) || (boot_flag == RT_FOTA_YMODEM_REQ_BKP_MAGIC))
- {
- return;
- }
- LOG_I("RTC_BKP_DR2 = 0x%04X: application boot failed, try restore.", boot_flag);
- rt_fota_signal_led_mode(led_upgrade_mode);
- /* 约定:检测到启动失败先标记 0xDEAD */
- rt_fota_bkp_dr2_write(RT_FOTA_BOOT_TRY_BKP_MAGIC);
- if (rt_fota_restore_app_from_df(1) == RT_FOTA_NO_ERR)
- {
- /* 对齐 fm_area 版本:本次升级包视为已处理,避免回滚后再次自动升级 */
- rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
- }
- else
- {
- LOG_I("No valid backup in df_area, skip restore.");
- }
- }
- void rt_fota_thread_entry(void *arg)
- {
- int fota_err = RT_FOTA_NO_ERR;
- extern int finsh_system_init(void);
- /* Signal led initialized */
- rt_fota_signal_led_init();
- /* Partition initialized */
- fota_err = rt_fota_boot_verify();
- if (fota_err != RT_FOTA_NO_ERR)
- {
- LOG_I("Partition initialized failed.");
- }
- #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
- /* Default key check */
- if (rt_fota_check_defalut_key() == RT_EOK)
- {
- /* enter to defalut key mode */
- rt_fota_signal_led_mode(led_upgrade_mode);
- goto __exit_default_entry;
- }
- #endif
- /* RTC 备份寄存器 DR2 升级请求检测:命中 0x5AA5 → 停留 bootloader 执行 ymodem 升级 */
- if (rt_fota_check_ymodem_request() == RT_EOK)
- {
- extern void ymodem_ota(uint8_t argc, char **argv);
- LOG_I("RTC_BKP_DR2 = 0x5AA5, stay in bootloader, start ymodem ota.");
- rt_fota_signal_led_mode(led_upgrade_mode);
- /* 下载分区默认 fm_area;ymodem 完成后内部复位,正常不返回 */
- ymodem_ota(1, RT_NULL);
- }
- /* v1.3.0:启动失败检测与自动回滚(详见函数注释),需在 shell 交互前处理 */
- rt_fota_check_boot_state();
- /* Shell initialized */
- if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
- {
- if(easyflash_init()==EF_NO_ERR) //初始化成功
- {
- rt_uint8_t boot_flag_buf[4] = {0, 0, 0, 0};
- ef_get_env_blob("stop_in_bootloader", boot_flag_buf, sizeof(boot_flag_buf), NULL);
- /* 兼容三种历史存储格式:字符串 "0"/"1"(EasyFlash 默认值)与二进制 0/1。
- * 历史缺陷:默认值 "0" 按二进制读为 0x30(非零),会误判为停留引导模式 */
- if (boot_flag_buf[0] == '0')
- stop_in_bootloader = 0;
- else if (boot_flag_buf[0] == '1')
- stop_in_bootloader = 1;
- else
- stop_in_bootloader = (boot_flag_buf[0] != 0) ? 1 : 0;
- rt_kprintf("stop_in_bootloader:%d\n", stop_in_bootloader);
- }
- rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
- if (rt_fota_get_shell_key() == RT_EOK)
- {
- if(stop_in_bootloader!=0)
- {
- stop_in_bootloader = 0;
- ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
- }
- goto __exit_shell_entry;
- }
- }
- else
- {
- LOG_I("Shell device config failed.");
- }
- /* Firmware partition verify */
- fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
- if (fota_err != RT_FOTA_NO_ERR)
- goto __exit_boot_entry;
- /* Check upgrade status */
- if (rt_fota_check_upgrade() <= 0)
- goto __exit_boot_entry;
- /* enter to upgrade mode */
- rt_fota_signal_led_mode(led_upgrade_mode);
- /* v1.3.0:搬运前先把当前 APP 固件备份到 df_area(新固件启动失败时回滚用)。
- * 备份失败不阻断升级(极端情况 df_area 异常;升级包读自 fm_area) */
- if (rt_fota_backup_app_to_df() != RT_FOTA_NO_ERR)
- {
- LOG_I("Backup app to df_area failed, continue upgrade.");
- }
- /* Implement upgrade, copy firmware partition to app partition */
- fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
- if (fota_err != RT_FOTA_NO_ERR)
- goto __exit_boot_entry;
- /* Update new application verison in RBL file of firmware partition */
- fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
- if (fota_err != RT_FOTA_NO_ERR)
- goto __exit_boot_entry;
- __exit_boot_entry:
- /* Implement application */
- rt_fota_start_application();
- #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
- __exit_default_entry:
- #endif
- /* Implement upgrade, copy default partition to app partition */
- if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
- {
- if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
- {
- rt_fota_start_application();
- }
- }
- LOG_I("Boot application failed, entry shell mode.");
-
- __exit_shell_entry:
- /* enter to shell mode */
- rt_fota_signal_led_mode(led_shell_mode);
- /* Implement shell */
- finsh_system_init();
- }
- void rt_fota_init(void)
- {
- rt_thread_t tid;
- tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
- if (tid != RT_NULL)
- {
- rt_thread_startup(tid);
- }
- else
- {
- LOG_I("rt-fota thread create failed.");
- }
- }
- void rt_fota_info(rt_uint8_t argc, char **argv)
- {
- char put_buf[24];
- char part_name[2][FAL_DEV_NAME_MAX] =
- {
- {RT_FOTA_FM_PART_NAME},
- {RT_FOTA_DF_PART_NAME}
- };
-
- const char* help_info[] =
- {
- [0] = "fota probe - probe RBL file of partiton",
- [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
- [2] = "fota clone des_part src_part - clone src partition to des partiton",
- [3] = "fota exec - execute application program",
- };
- if (argc < 2)
- {
- rt_kprintf("Usage:\n");
- for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
- {
- rt_kprintf("%s\n", help_info[i]);
- }
- rt_kprintf("\n");
- }
- else
- {
- const char *operator = argv[1];
- if (!rt_strcmp(operator, "probe"))
- {
- for (int i = 0; i < 2; i++)
- {
- if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
- {
- LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
- LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
- rt_memset(put_buf, 0x0, sizeof(put_buf));
- if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
- {
- rt_strncpy(put_buf, " AES", 4);
- }
- else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
- {
- rt_strncpy(put_buf, " XOR", 4);
- }
- else
- {
- rt_strncpy(put_buf, "NONE", 4);
- }
- if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
- {
- rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
- }
- else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
- {
- rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
- }
- else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
- {
- rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
- }
- if (rt_strlen(put_buf) <= 0)
- {
- rt_strncpy(put_buf, "None", 4);
- }
- LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
- LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
- LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
- LOG_I("| Code package size | %11d |", fota_part_head.com_size);
- LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
- }
- }
- }
- else if (!rt_strcmp(operator, "show"))
- {
- const struct fal_partition *part;
- const char *part_name = argv[2];
-
- rt_uint32_t addr = strtol(argv[3], NULL, 0);
- rt_uint32_t size = strtol(argv[4], NULL, 0);
- rt_uint8_t buf[16];
-
-
- part = fal_partition_find(part_name);
- if (part != RT_NULL)
- {
- while (size > 16)
- {
- fal_partition_read(part, addr, buf, 16);
-
- rt_kprintf("%08X: ", addr);
- for (int i = 0; i < 16; i++)
- {
- rt_kprintf("%02X ", buf[i]);
- }
- rt_kprintf("\n");
- size -= 16;
- addr += 16;
- }
- fal_partition_read(part, addr, buf, size);
- rt_kprintf("%08X: ", addr);
- for (int i = 0; i < size; i++)
- {
- rt_kprintf("%02X ", buf[i]);
- }
- rt_kprintf("\n");
- }
- else
- {
- rt_kprintf("%s partition is not exist!\n", part_name);
- }
- }
- else if (!rt_strcmp(operator, "clone"))
- {
- const char *dst_part_name = argv[2];
- const char *src_part_name = argv[3];
- const struct fal_partition *dst_part;
- const struct fal_partition *src_part;
- dst_part = fal_partition_find(dst_part_name);
- src_part = fal_partition_find(src_part_name);
- if (dst_part == RT_NULL || src_part == RT_NULL)
- {
- if (dst_part == RT_NULL)
- rt_kprintf("%s partition is not exist!\n", dst_part_name);
- if (src_part == RT_NULL)
- rt_kprintf("%s partition is not exist!\n", src_part_name);
- }
- else
- {
- rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
- if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
- {
- int clone_pos = 0;
- int clone_len = 0, clone_tol_len;
- rt_uint8_t *buf = rt_malloc(4096);
- if (dst_part->len < src_part->len)
- clone_tol_len = dst_part->len;
- else
- clone_tol_len = src_part->len;
-
- while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
- {
- clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
- if (clone_len < 0)
- {
- rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
- break;
- }
-
- if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
- {
- rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
- break;
- }
- rt_kprintf("#");
- clone_pos += clone_len;
- }
-
- if (clone_pos >= clone_tol_len)
- rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
- else
- rt_kprintf("\nClone partition failed!\n");
- if (buf)
- rt_free(buf);
- }
- }
- }
- else if (!rt_strcmp(operator, "exec"))
- {
- rt_fota_start_application();
- }
- else
- {
- rt_kprintf("Usage:\n");
- for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
- {
- rt_kprintf("%s\n", help_info[i]);
- }
- rt_kprintf("\n");
- }
- }
- }
- /**
- * rt-fota />ymodem_ota
- */
- MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);
- /* msh 命令:查看/设置 RTC_BKP_DR2 启动标志(取值约定见文件头 v1.3.0 区块)。
- * 典型用途:手动进入 bootloader 维护后、重新上电前执行 `dr2 0` 清除标志,
- * 避免残留非 0 值(如启动失败检测写入的 0xDEAD)在下次上电被
- * rt_fota_check_boot_state() 误判为“APP 未正常启动”而触发 df_area→app 回滚;
- * 也可写入 0x5AA5 / 0xDEAD 分别验证 ymodem 升级入口 / 启动失败回滚路径。 */
- void rt_fota_dr2(int argc, char **argv)
- {
- rt_uint16_t boot_flag;
- char *endp = NULL;
- if (argc < 2)
- {
- boot_flag = rt_fota_bkp_dr2_read();
- rt_kprintf("RTC_BKP_DR2 = 0x%04X", boot_flag);
- if (boot_flag == 0x0000)
- rt_kprintf(" (normal)\n");
- else if (boot_flag == RT_FOTA_YMODEM_REQ_BKP_MAGIC)
- rt_kprintf(" (ymodem upgrade request)\n");
- else if (boot_flag == RT_FOTA_BOOT_TRY_BKP_MAGIC)
- rt_kprintf(" (boot-fail marker, restore pending)\n");
- else
- rt_kprintf(" (unknown, restore pending)\n");
- return;
- }
- boot_flag = (rt_uint16_t)strtol(argv[1], &endp, 0);
- if (endp == argv[1])
- {
- rt_kprintf("usage: dr2 - show boot flag\n");
- rt_kprintf(" dr2 0 - clear flag (avoid false restore)\n");
- rt_kprintf(" dr2 <value> - set value (0x5AA5 / 0xDEAD for test)\n");
- return;
- }
- rt_fota_bkp_dr2_write(boot_flag);
- rt_kprintf("RTC_BKP_DR2 set to 0x%04X\n", rt_fota_bkp_dr2_read());
- }
- MSH_CMD_EXPORT_ALIAS(rt_fota_dr2, dr2, Show or set RTC_BKP_DR2 boot flag);
|