rt_fota.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-09-22 Warfalcon first version
  9. */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <board.h>
  16. #include <fal.h>
  17. #include <tinycrypt.h>
  18. #include <fastlz.h>
  19. #include <quicklz.h>
  20. #include <rt_fota.h>
  21. #include <signal_led.h>
  22. #include "sys_config.h"
  23. #include "easyflash.h"
  24. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  25. #include <finsh.h>
  26. #include <shell.h>
  27. #endif
  28. #include <rt_fota.h>
  29. extern void HMI_WRITE(char *fmt,...);
  30. #define DBG_ENABLE
  31. #define DBG_SECTION_NAME "fota"
  32. #ifdef RT_FOTA_DEBUG
  33. #define DBG_LEVEL DBG_LOG
  34. #else
  35. #define DBG_LEVEL DBG_INFO
  36. #endif
  37. #define DBG_COLOR
  38. #include <rtdbg.h>
  39. #ifndef RT_FOTA_THREAD_STACK_SIZE
  40. #define RT_FOTA_THREAD_STACK_SIZE 4096
  41. #endif
  42. #ifndef RT_FOTA_THREAD_PRIORITY
  43. #define RT_FOTA_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 3)
  44. #endif
  45. #ifndef RT_FOTA_ALGO_BUFF_SIZE
  46. #define RT_FOTA_ALGO_BUFF_SIZE 2048 //2025-0428 CHANGE 2048
  47. #endif
  48. /**
  49. * AES256 encryption algorithm option
  50. */
  51. #ifndef RT_FOTA_ALGO_AES_IV
  52. #define RT_FOTA_ALGO_AES_IV "0123456789ABCDEF"
  53. #endif
  54. #ifndef RT_FOTA_ALGO_AES_KEY
  55. #define RT_FOTA_ALGO_AES_KEY "0123456789ABCDEF0123456789ABCDEF"
  56. #endif
  57. #ifndef RT_FOTA_BLOCK_HEADER_SIZE
  58. #define RT_FOTA_BLOCK_HEADER_SIZE 4
  59. #endif
  60. #ifndef RT_FOTA_CMPRS_BUFFER_SIZE
  61. #define RT_FOTA_CMPRS_BUFFER_SIZE 4096
  62. #endif
  63. #ifndef RT_FOTA_FASTLZ_BUFFER_PADDING
  64. #define RT_FOTA_FASTLZ_BUFFER_PADDING FASTLZ_BUFFER_PADDING(RT_FOTA_CMPRS_BUFFER_SIZE)
  65. #endif
  66. #ifndef RT_FOTA_QUICKLZ_BUFFER_PADDING
  67. #define RT_FOTA_QUICKLZ_BUFFER_PADDING QLZ_BUFFER_PADDING
  68. #endif
  69. #ifndef RT_FOTA_ENTER_SHELL_KEY
  70. #define RT_FOTA_ENTER_SHELL_KEY 0x0d
  71. #endif
  72. #ifndef RT_FOTA_GET_CHAR_WAITTIGN
  73. #define RT_FOTA_GET_CHAR_WAITTIGN (RT_TICK_PER_SECOND * 5)
  74. #endif
  75. #ifndef RT_FOTA_SIGNAL_LED_PIN
  76. #define RT_FOTA_SIGNAL_LED_PIN GET_PIN(B, 3)
  77. #endif
  78. #ifndef RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE
  79. #define RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE 1024
  80. #endif
  81. #ifndef RT_FOTA_SIGNAL_LED_THREAD_PRIORITY
  82. #define RT_FOTA_SIGNAL_LED_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 4)
  83. #endif
  84. /* For signal led */
  85. static led_t *signal_led = NULL;
  86. static led_mem_opreation_t signal_led_mem_op;
  87. const char *led_shell_mode = "500,500,"; /* 1Hz ��˸ */
  88. const char *led_upgrade_mode = "50,50,"; /* 10Hz ��˸ */
  89. const char *led_off_mode = "0,100,"; /* ���� */
  90. const char *led_on_mode = "100,0,"; /* ���� */
  91. static int stop_in_bootloader=0;
  92. /* ===================== v1.3.0 升级回滚机制 ===================== */
  93. /* RTC 备份寄存器 DR2 状态约定:
  94. * 0x0000 —— APP 启动成功:Application_CBD 的 main() 启动后立即写入,
  95. * 表示 bootloader 本次"启动尝试"已被 APP 确认;
  96. * 0x5AA5 —— 升级请求:应用侧 JSONRPC "update" 指令写入,bootloader
  97. * 读取后清 0 并停留执行 ymodem 升级;
  98. * 0xDEAD —— 启动尝试标记:bootloader 跳转 APP 前写入;若复位进入
  99. * bootloader 后该值仍在(或为其它非 0 值),说明 APP 未运行
  100. * 到清零处(未正常启动),将从 df_area 备份回滚 APP 固件。
  101. * 说明:DR2 为 16 位,位于 BKP_BASE + 4*2,与 Application_CBD 的
  102. * main.c / dtu_uart2.c 访问方式配对。 */
  103. #define RT_FOTA_YMODEM_REQ_BKP_MAGIC 0x5AA5
  104. #define RT_FOTA_BOOT_TRY_BKP_MAGIC 0xDEAD
  105. /* df_area 备份格式:
  106. * [0, 4KB) —— rt_fota_backup_head_t 头(magic + 长度 + CRC32),
  107. * 头部有效即代表存在有效备份;
  108. * [4KB, 4KB + data_len) —— APP 分区镜像(裸数据,恢复时原样写回)。
  109. * 写入顺序:先失效旧头 -> 写数据 -> 最后写头,任一步骤掉电都不会残留
  110. * 半新半旧的"有效备份"。 */
  111. #define RT_FOTA_BACKUP_MAGIC 0x46424B31 /* "FBK1" */
  112. #define RT_FOTA_BACKUP_HEAD_RSV 4096 /* 头部预留(独占首扇区) */
  113. #define RT_FOTA_BACKUP_BLK_SIZE 4096
  114. typedef struct
  115. {
  116. rt_uint32_t magic; /* RT_FOTA_BACKUP_MAGIC */
  117. rt_uint32_t data_len; /* 备份数据长度(字节) */
  118. rt_uint32_t data_crc; /* 备份数据 CRC32(rt_fota_crc 语义) */
  119. rt_uint32_t head_crc; /* 头部前 12 字节 CRC32 */
  120. } rt_fota_backup_head_t;
  121. /* 使能 PWR/BKP 时钟并解除备份域写访问保护(幂等,可重复调用) */
  122. static void rt_fota_bkp_access_enable(void)
  123. {
  124. __HAL_RCC_PWR_CLK_ENABLE();
  125. __HAL_RCC_BKP_CLK_ENABLE();
  126. HAL_PWR_EnableBkUpAccess();
  127. }
  128. static rt_uint16_t rt_fota_bkp_dr2_read(void)
  129. {
  130. rt_fota_bkp_access_enable();
  131. return *(__IO uint16_t *)(BKP_BASE + 4 * 2);
  132. }
  133. static void rt_fota_bkp_dr2_write(rt_uint16_t value)
  134. {
  135. rt_fota_bkp_access_enable();
  136. *(__IO uint16_t *)(BKP_BASE + 4 * 2) = value;
  137. }
  138. /* For shell */
  139. static rt_sem_t shell_sem = RT_NULL;
  140. static rt_device_t shell_dev = RT_NULL;
  141. typedef struct {
  142. char type[4];
  143. rt_uint16_t fota_algo;
  144. rt_uint8_t fm_time[6];
  145. char app_part_name[16];
  146. char download_version[24];
  147. char current_version[24];
  148. rt_uint32_t code_crc;
  149. rt_uint32_t hash_val;
  150. rt_uint32_t raw_size;
  151. rt_uint32_t com_size;
  152. rt_uint32_t head_crc;
  153. } rt_fota_part_head, *rt_fota_part_head_t;
  154. typedef void (*rt_fota_app_func)(void);
  155. static rt_fota_app_func app_func = RT_NULL;
  156. static rt_fota_part_head fota_part_head;
  157. static void rt_fota_signal_led_on(void)
  158. {
  159. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_LOW);
  160. }
  161. static void rt_fota_signal_led_off(void)
  162. {
  163. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_HIGH);
  164. }
  165. static void rt_fota_signal_led_entry(void *arg)
  166. {
  167. while(1)
  168. {
  169. led_ticks();
  170. rt_thread_mdelay(LED_TICK_TIME);
  171. }
  172. }
  173. static void rt_fota_signal_led_init(void)
  174. {
  175. rt_pin_mode(RT_FOTA_SIGNAL_LED_PIN, PIN_MODE_OUTPUT);
  176. signal_led_mem_op.malloc_fn = (void* (*)(size_t))rt_malloc;
  177. signal_led_mem_op.free_fn = rt_free;
  178. led_set_mem_operation(&signal_led_mem_op);
  179. signal_led = led_create(rt_fota_signal_led_on, rt_fota_signal_led_off);
  180. /* Config signal led mode */
  181. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_on_mode);
  182. led_set_blink_over_callback(signal_led, RT_NULL);
  183. led_start(signal_led);
  184. rt_thread_t tid;
  185. 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);
  186. if (tid)
  187. rt_thread_startup(tid);
  188. }
  189. static void rt_fota_signal_led_mode(const char *led_cfg)
  190. {
  191. RT_ASSERT(led_cfg != RT_NULL);
  192. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_cfg);
  193. }
  194. static int rt_fota_boot_verify(void)
  195. {
  196. int fota_res = RT_FOTA_NO_ERR;
  197. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  198. /* partition initial */
  199. fal_init();
  200. extern int fal_init_check(void);
  201. /* verify partition */
  202. if (fal_init_check() != 1)
  203. {
  204. LOG_D("Partition initialized failed!");
  205. fota_res = RT_FOTA_GENERAL_ERR;
  206. goto __exit_boot_verify;
  207. }
  208. __exit_boot_verify:
  209. return fota_res;
  210. }
  211. int rt_fota_part_fw_verify(const char *part_name)
  212. {
  213. #define RT_FOTA_CRC_BUFF_SIZE 4096
  214. #define RT_FOTA_CRC_INIT_VAL 0xffffffff
  215. int fota_res = RT_FOTA_NO_ERR;
  216. const struct fal_partition *part;
  217. rt_fota_part_head part_head;
  218. rt_uint8_t *body_buf = RT_NULL;
  219. rt_uint32_t body_crc = RT_FOTA_CRC_INIT_VAL;
  220. rt_uint32_t hdr_crc;
  221. if (part_name == RT_NULL)
  222. {
  223. LOG_D("Invaild paramenter input!");
  224. fota_res = RT_FOTA_GENERAL_ERR;
  225. goto __exit_partition_verify;
  226. }
  227. part = fal_partition_find(part_name);
  228. if (part == RT_NULL)
  229. {
  230. LOG_D("Partition[%s] not found.", part_name);
  231. fota_res = RT_FOTA_GENERAL_ERR;
  232. goto __exit_partition_verify;
  233. }
  234. /* read the head of RBL files */
  235. if (fal_partition_read(part, 0, (rt_uint8_t *)&part_head, sizeof(rt_fota_part_head)) < 0)
  236. {
  237. LOG_D("Partition[%s] read error!", part->name);
  238. fota_res = RT_FOTA_PART_READ_ERR;
  239. goto __exit_partition_verify;
  240. }
  241. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  242. hdr_crc = rt_fota_crc((rt_uint8_t *)&part_head, sizeof(rt_fota_part_head) - 4);
  243. if (hdr_crc != part_head.head_crc)
  244. {
  245. LOG_D("Partition[%s] head CRC32 error!", part->name);
  246. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  247. goto __exit_partition_verify;
  248. }
  249. if (rt_strcmp(part_head.type, "RBL") != 0)
  250. {
  251. LOG_D("Partition[%s] type[%s] not surport.", part->name, part_head.type);
  252. fota_res = RT_FOTA_CHECK_FAILED;
  253. goto __exit_partition_verify;
  254. }
  255. if (fal_partition_find(part_head.app_part_name) == RT_NULL)
  256. {
  257. LOG_D("Partition[%s] not found.", part_head.app_part_name);
  258. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  259. goto __exit_partition_verify;
  260. }
  261. body_buf = rt_malloc(RT_FOTA_CRC_BUFF_SIZE);
  262. if (body_buf == RT_NULL)
  263. {
  264. LOG_D("Not enough memory for body CRC32 verify.");
  265. fota_res = RT_FOTA_NO_MEM_ERR;
  266. goto __exit_partition_verify;
  267. }
  268. for (int body_pos = 0; body_pos < part_head.com_size;)
  269. {
  270. int body_read_len = fal_partition_read(part, sizeof(rt_fota_part_head) + body_pos, body_buf, RT_FOTA_CRC_BUFF_SIZE);
  271. if (body_read_len > 0)
  272. {
  273. if ((body_pos + body_read_len) > part_head.com_size)
  274. {
  275. body_read_len = part_head.com_size - body_pos;
  276. }
  277. extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
  278. body_crc = rt_fota_step_crc(body_crc, body_buf, body_read_len);
  279. body_pos = body_pos + body_read_len;
  280. }
  281. else
  282. {
  283. LOG_D("Partition[%s] read error!", part->name);
  284. fota_res = RT_FOTA_PART_READ_ERR;
  285. goto __exit_partition_verify;
  286. }
  287. }
  288. body_crc = body_crc ^ RT_FOTA_CRC_INIT_VAL;
  289. if (body_crc != part_head.code_crc)
  290. {
  291. LOG_D("Partition[%s] firmware integrity verify failed.", part->name);
  292. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  293. goto __exit_partition_verify;
  294. }
  295. __exit_partition_verify:
  296. if (fota_res == RT_FOTA_NO_ERR)
  297. {
  298. rt_enter_critical();
  299. rt_memcpy(&fota_part_head, &part_head, sizeof(rt_fota_part_head));
  300. rt_exit_critical();
  301. LOG_D("partition[%s] verify success!", part->name);
  302. }
  303. else
  304. {
  305. rt_enter_critical();
  306. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  307. rt_exit_critical();
  308. LOG_D("Partition[%s] verify failed!", part->name);
  309. }
  310. if (body_buf)
  311. rt_free(body_buf);
  312. return fota_res;
  313. }
  314. int rt_fota_check_upgrade(void)
  315. {
  316. int is_upgrade = 0;
  317. if (rt_strcmp(fota_part_head.download_version, fota_part_head.current_version) != 0)
  318. {
  319. is_upgrade = 1;
  320. LOG_D("Application need upgrade.");
  321. goto __exit_check_upgrade;
  322. }
  323. __exit_check_upgrade:
  324. return is_upgrade;
  325. }
  326. int rt_fota_copy_version(const char *part_name)
  327. {
  328. #define THE_NOR_FLASH_GRANULARITY 4096
  329. int fota_res = RT_FOTA_NO_ERR;
  330. const struct fal_partition *part;
  331. rt_fota_part_head_t part_head = RT_NULL;
  332. rt_uint8_t *cache_buf = RT_NULL;
  333. part = fal_partition_find(part_name);
  334. if (part == RT_NULL)
  335. {
  336. LOG_D("Find partition[%s] not found.", part_name);
  337. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  338. goto __exit_copy_version;
  339. }
  340. cache_buf = rt_malloc(THE_NOR_FLASH_GRANULARITY);
  341. if (cache_buf == RT_NULL)
  342. {
  343. LOG_D("Not enough memory for head erase.");
  344. fota_res = RT_FOTA_NO_MEM_ERR;
  345. goto __exit_copy_version;
  346. }
  347. part_head = (rt_fota_part_head_t)cache_buf;
  348. if (fal_partition_read(part, 0, cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  349. {
  350. LOG_I("Read partition[%s] failed.", part_name);
  351. fota_res = RT_FOTA_PART_READ_ERR;
  352. goto __exit_copy_version;
  353. }
  354. rt_memcpy(part_head->current_version, part_head->download_version, sizeof(part_head->current_version));
  355. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  356. part_head->head_crc = rt_fota_crc((rt_uint8_t *)part_head, sizeof(rt_fota_part_head) - 4);
  357. if (fal_partition_erase(part, 0, THE_NOR_FLASH_GRANULARITY) < 0)
  358. {
  359. LOG_D("Erase partition[%s] failed.", part_name);
  360. fota_res = RT_FOTA_PART_ERASE_ERR;
  361. goto __exit_copy_version;
  362. }
  363. if (fal_partition_write(part, 0, (const rt_uint8_t *)cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  364. {
  365. LOG_I("Write partition[%s] failed.", part_name);
  366. fota_res = RT_FOTA_PART_WRITE_ERR;
  367. goto __exit_copy_version;
  368. }
  369. __exit_copy_version:
  370. if (cache_buf)
  371. rt_free(cache_buf);
  372. if (fota_res != RT_FOTA_NO_ERR)
  373. LOG_I("Copy firmware version failed!");
  374. else
  375. LOG_I("Copy firmware version Success!");
  376. return fota_res;
  377. }
  378. int rt_fota_erase_app_part(void)
  379. {
  380. int fota_res = RT_FOTA_NO_ERR;
  381. const struct fal_partition *part;
  382. part = fal_partition_find(fota_part_head.app_part_name);
  383. if (part == RT_NULL)
  384. {
  385. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  386. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  387. goto __exit_partition_erase;
  388. }
  389. LOG_I("Partition[%s] erase start:", part->name);
  390. if (fal_partition_erase(part, 0, fota_part_head.raw_size) < 0)
  391. {
  392. LOG_D("Partition[%s] erase failed!", part->name);
  393. fota_res = RT_FOTA_PART_ERASE_ERR;
  394. goto __exit_partition_erase;
  395. }
  396. __exit_partition_erase:
  397. if (fota_res == RT_FOTA_NO_ERR)
  398. {
  399. LOG_D("Partition[%s] erase %d bytes success!", part->name, fota_part_head.raw_size);
  400. }
  401. return fota_res;
  402. }
  403. int rt_fota_write_app_part(int fw_pos, rt_uint8_t *fw_buf, int fw_len)
  404. {
  405. int rt_fota_res = RT_FOTA_NO_ERR;
  406. const struct fal_partition *part;
  407. part = fal_partition_find(fota_part_head.app_part_name);
  408. if (part == RT_NULL)
  409. {
  410. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  411. rt_fota_res = RT_FOTA_FW_VERIFY_FAILED;
  412. goto __partition_write_exit;
  413. }
  414. if (fal_partition_write(part, fw_pos, fw_buf, fw_len) < 0)
  415. {
  416. LOG_D("Partition[%s] write failed!", part->name);
  417. rt_fota_res = RT_FOTA_PART_WRITE_ERR;
  418. goto __partition_write_exit;
  419. }
  420. __partition_write_exit:
  421. if (rt_fota_res == RT_FOTA_NO_ERR)
  422. {
  423. LOG_D("Partition[%s] write %d bytes success!", part->name, fw_len);
  424. }
  425. return rt_fota_res;
  426. }
  427. 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)
  428. {
  429. int fota_err = RT_FOTA_NO_ERR;
  430. rt_uint8_t *encrypt_buf = RT_NULL;
  431. if ((part == RT_NULL) || (decrypt_buf == RT_NULL)
  432. || (decrypt_len % 16 != 0) || (decrypt_len > RT_FOTA_ALGO_BUFF_SIZE))
  433. {
  434. fota_err = RT_FOTA_GENERAL_ERR;
  435. goto __exit_read_decrypt;
  436. }
  437. rt_memset(decrypt_buf, 0x0, decrypt_len);
  438. /* Not use AES256 algorithm */
  439. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  440. {
  441. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, decrypt_buf, decrypt_len);
  442. if (fota_err <= 0)
  443. {
  444. fota_err = RT_FOTA_PART_READ_ERR;
  445. }
  446. goto __exit_read_decrypt;
  447. }
  448. encrypt_buf = rt_malloc(decrypt_len);
  449. if (encrypt_buf == RT_NULL)
  450. {
  451. fota_err = RT_FOTA_GENERAL_ERR;
  452. goto __exit_read_decrypt;
  453. }
  454. rt_memset(encrypt_buf, 0x0, decrypt_len);
  455. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, encrypt_buf, decrypt_len);
  456. if (fota_err <= 0 || fota_err % 16 != 0)
  457. {
  458. fota_err = RT_FOTA_PART_READ_ERR;
  459. goto __exit_read_decrypt;
  460. }
  461. tiny_aes_crypt_cbc(aes_ctx, AES_DECRYPT, fota_err, aes_iv, encrypt_buf, decrypt_buf);
  462. __exit_read_decrypt:
  463. if (encrypt_buf)
  464. rt_free(encrypt_buf);
  465. return fota_err;
  466. }
  467. int rt_fota_upgrade(const char *part_name)
  468. {
  469. int fota_err = RT_FOTA_NO_ERR;
  470. const struct fal_partition *part;
  471. rt_fota_part_head_t part_head = RT_NULL;
  472. tiny_aes_context *aes_ctx = RT_NULL;
  473. rt_uint8_t *aes_iv = RT_NULL;
  474. rt_uint8_t *crypt_buf = RT_NULL;
  475. int fw_raw_pos = 0;
  476. int fw_raw_len = 0;
  477. rt_uint32_t total_copy_size = 0;
  478. rt_uint8_t block_hdr_buf[RT_FOTA_BLOCK_HEADER_SIZE];
  479. rt_uint32_t block_hdr_pos = RT_FOTA_ALGO_BUFF_SIZE;
  480. rt_uint32_t block_size = 0;
  481. rt_uint32_t dcprs_size = 0;
  482. qlz_state_decompress *dcprs_state = RT_NULL;
  483. rt_uint8_t *cmprs_buff = RT_NULL;
  484. rt_uint8_t *dcprs_buff = RT_NULL;
  485. rt_uint32_t padding_size = 0;
  486. if (part_name == RT_NULL)
  487. {
  488. LOG_D("Invaild paramenter input!");
  489. fota_err = RT_FOTA_GENERAL_ERR;
  490. goto __exit_upgrade;
  491. }
  492. part = fal_partition_find(part_name);
  493. if (part == RT_NULL)
  494. {
  495. LOG_D("Upgrade partition[%s] not found.", part_name);
  496. fota_err = RT_FOTA_GENERAL_ERR;
  497. goto __exit_upgrade;
  498. }
  499. /* Application partition erase */
  500. fota_err = rt_fota_erase_app_part();
  501. if (fota_err != RT_FOTA_NO_ERR)
  502. {
  503. goto __exit_upgrade;
  504. }
  505. /* rt_fota_erase_app_part() has check fota_part_head vaild already */
  506. part_head = &fota_part_head;
  507. crypt_buf = rt_malloc(RT_FOTA_ALGO_BUFF_SIZE);
  508. if (crypt_buf == RT_NULL)
  509. {
  510. LOG_D("Not enough memory for firmware buffer.");
  511. fota_err = RT_FOTA_NO_MEM_ERR;
  512. goto __exit_upgrade;
  513. }
  514. /* AES256 algorithm enable */
  515. if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  516. {
  517. aes_ctx = rt_malloc(sizeof(tiny_aes_context));
  518. aes_iv = rt_malloc(rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
  519. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  520. {
  521. LOG_D("Not enough memory for firmware hash verify.");
  522. fota_err = RT_FOTA_NO_MEM_ERR;
  523. goto __exit_upgrade;
  524. }
  525. rt_memset(aes_iv, 0x0, rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
  526. rt_memcpy(aes_iv, RT_FOTA_ALGO_AES_IV, rt_strlen(RT_FOTA_ALGO_AES_IV));
  527. tiny_aes_setkey_dec(aes_ctx, (rt_uint8_t *)RT_FOTA_ALGO_AES_KEY, 256);
  528. }
  529. else if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  530. {
  531. LOG_I("Not surpport XOR.");
  532. fota_err = RT_FOTA_GENERAL_ERR;
  533. goto __exit_upgrade;
  534. }
  535. /* If enable fastlz compress function */
  536. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  537. {
  538. cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_FASTLZ_BUFFER_PADDING);
  539. dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
  540. if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL)
  541. {
  542. LOG_D("Not enough memory for firmware hash verify.");
  543. fota_err = RT_FOTA_NO_MEM_ERR;
  544. goto __exit_upgrade;
  545. }
  546. padding_size = RT_FOTA_FASTLZ_BUFFER_PADDING;
  547. }
  548. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  549. {
  550. cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_QUICKLZ_BUFFER_PADDING);
  551. dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
  552. dcprs_state = rt_malloc(sizeof(qlz_state_decompress));
  553. if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL || dcprs_state == RT_NULL)
  554. {
  555. LOG_D("Not enough memory for firmware hash verify.");
  556. fota_err = RT_FOTA_NO_MEM_ERR;
  557. goto __exit_upgrade;
  558. }
  559. padding_size = RT_FOTA_QUICKLZ_BUFFER_PADDING;
  560. rt_memset(dcprs_state, 0x0, sizeof(qlz_state_decompress));
  561. }
  562. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  563. {
  564. LOG_I("Not surpport GZIP.");
  565. fota_err = RT_FOTA_GENERAL_ERR;
  566. goto __exit_upgrade;
  567. }
  568. LOG_I("Start to copy firmware from %s to %s partition:", part->name, part_head->app_part_name);
  569. while (fw_raw_pos < part_head->com_size)
  570. {
  571. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  572. {
  573. if (block_hdr_pos >= RT_FOTA_ALGO_BUFF_SIZE)
  574. {
  575. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  576. if (fw_raw_len < 0)
  577. {
  578. LOG_D("AES256 algorithm failed.");
  579. fota_err = RT_FOTA_PART_READ_ERR;
  580. goto __exit_upgrade;
  581. }
  582. fw_raw_pos += fw_raw_len;
  583. rt_memcpy(block_hdr_buf, crypt_buf, RT_FOTA_BLOCK_HEADER_SIZE);
  584. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  585. /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
  586. if ((fw_raw_len < RT_FOTA_BLOCK_HEADER_SIZE) ||
  587. (block_size > (rt_uint32_t)(fw_raw_len - RT_FOTA_BLOCK_HEADER_SIZE)) ||
  588. (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size)))
  589. {
  590. LOG_D("Invalid compressed block size(%d).", block_size);
  591. fota_err = RT_FOTA_GENERAL_ERR;
  592. goto __exit_upgrade;
  593. }
  594. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  595. rt_memcpy(cmprs_buff, &crypt_buf[RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  596. block_hdr_pos = RT_FOTA_BLOCK_HEADER_SIZE + block_size;
  597. }
  598. else
  599. {
  600. rt_uint8_t hdr_tmp_pos = 0;
  601. while (block_hdr_pos < RT_FOTA_ALGO_BUFF_SIZE)
  602. {
  603. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  604. {
  605. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  606. }
  607. else
  608. {
  609. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  610. /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
  611. if (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size))
  612. {
  613. LOG_D("Invalid compressed block size(%d).", block_size);
  614. fota_err = RT_FOTA_GENERAL_ERR;
  615. goto __exit_upgrade;
  616. }
  617. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  618. if (block_size > (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos))
  619. {
  620. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos));
  621. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  622. if (fw_raw_len < 0)
  623. {
  624. LOG_D("AES256 algorithm failed.");
  625. fota_err = RT_FOTA_PART_READ_ERR;
  626. goto __exit_upgrade;
  627. }
  628. fw_raw_pos += fw_raw_len;
  629. /* 校验跨缓冲块的剩余长度不超过本次读取的字节数 */
  630. if (((block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE) > (rt_uint32_t)fw_raw_len)
  631. {
  632. LOG_D("Invalid compressed block size(%d).", block_size);
  633. fota_err = RT_FOTA_GENERAL_ERR;
  634. goto __exit_upgrade;
  635. }
  636. 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);
  637. block_hdr_pos = (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE;
  638. }
  639. else
  640. {
  641. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  642. block_hdr_pos = block_hdr_pos + block_size;
  643. }
  644. break;
  645. }
  646. }
  647. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  648. {
  649. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  650. if (fw_raw_len < 0)
  651. {
  652. LOG_D("AES256 algorithm failed.");
  653. fota_err = RT_FOTA_PART_READ_ERR;
  654. goto __exit_upgrade;
  655. }
  656. fw_raw_pos += fw_raw_len;
  657. block_hdr_pos = 0;
  658. while (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  659. {
  660. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  661. }
  662. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  663. /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
  664. if ((fw_raw_len < (int)block_hdr_pos) ||
  665. (block_size > (rt_uint32_t)(fw_raw_len - block_hdr_pos)) ||
  666. (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size)))
  667. {
  668. LOG_D("Invalid compressed block size(%d).", block_size);
  669. fota_err = RT_FOTA_GENERAL_ERR;
  670. goto __exit_upgrade;
  671. }
  672. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  673. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  674. block_hdr_pos = (block_hdr_pos + block_size) % RT_FOTA_ALGO_BUFF_SIZE;
  675. }
  676. }
  677. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  678. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  679. {
  680. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  681. }
  682. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  683. {
  684. /* 校验解压长度:qlz 解压长度由流头自报,必须在目标缓冲区容量内 */
  685. if (qlz_size_decompressed((const char *)&cmprs_buff[0]) > RT_FOTA_CMPRS_BUFFER_SIZE)
  686. {
  687. LOG_D("Invalid quicklz decompressed size in upgrade package.");
  688. fota_err = RT_FOTA_GENERAL_ERR;
  689. goto __exit_upgrade;
  690. }
  691. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  692. }
  693. if (dcprs_size <= 0)
  694. {
  695. LOG_D("Decompress failed: %d.", dcprs_size);
  696. fota_err = RT_FOTA_GENERAL_ERR;
  697. goto __exit_upgrade;
  698. }
  699. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  700. {
  701. fota_err = RT_FOTA_COPY_FAILED;
  702. goto __exit_upgrade;
  703. }
  704. total_copy_size += dcprs_size;
  705. rt_kprintf("#");
  706. }
  707. /* no compress option */
  708. else
  709. {
  710. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  711. if (fw_raw_len < 0)
  712. {
  713. LOG_D("AES256 algorithm failed.");
  714. fota_err = RT_FOTA_PART_READ_ERR;
  715. goto __exit_upgrade;
  716. }
  717. fw_raw_pos += fw_raw_len;
  718. if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
  719. {
  720. fota_err = RT_FOTA_COPY_FAILED;
  721. goto __exit_upgrade;
  722. }
  723. total_copy_size += fw_raw_len;
  724. rt_kprintf("#");
  725. }
  726. }
  727. /* it has compress option */
  728. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  729. {
  730. while (total_copy_size < part_head->raw_size)
  731. {
  732. if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
  733. {
  734. rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
  735. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  736. if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
  737. {
  738. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  739. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  740. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  741. block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
  742. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  743. {
  744. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  745. }
  746. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  747. {
  748. /* 校验解压长度:qlz 解压长度由流头自报,必须在目标缓冲区容量内 */
  749. if (qlz_size_decompressed((const char *)&cmprs_buff[0]) > RT_FOTA_CMPRS_BUFFER_SIZE)
  750. {
  751. LOG_D("Invalid quicklz decompressed size in upgrade package.");
  752. fota_err = RT_FOTA_GENERAL_ERR;
  753. goto __exit_upgrade;
  754. }
  755. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  756. }
  757. if (dcprs_size <= 0)
  758. {
  759. LOG_D("Decompress failed: %d.", dcprs_size);
  760. fota_err = RT_FOTA_GENERAL_ERR;
  761. goto __exit_upgrade;
  762. }
  763. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  764. {
  765. fota_err = RT_FOTA_COPY_FAILED;
  766. goto __exit_upgrade;
  767. }
  768. total_copy_size += dcprs_size;
  769. rt_kprintf("#");
  770. }
  771. else
  772. {
  773. break;
  774. }
  775. }
  776. else
  777. {
  778. break;
  779. }
  780. }
  781. }
  782. rt_kprintf("\r\n");
  783. /* �п�������ֵ�����,��ΪAES��Ҫ���16�ֽ�����,�����Ľ��ܽ�ѹֵ�Ĵ������������Ǵ��ڵ���raw_size */
  784. /* �ȽϺõķ�������һ��У��,Ŀǰ��������HASH_CODE�㷨��֪�� */
  785. if (total_copy_size < part_head->raw_size)
  786. {
  787. LOG_D("Decompress check failed.");
  788. fota_err = RT_FOTA_GENERAL_ERR;
  789. }
  790. __exit_upgrade:
  791. if (aes_ctx)
  792. rt_free(aes_ctx);
  793. if (aes_iv)
  794. rt_free(aes_iv);
  795. if (crypt_buf)
  796. rt_free(crypt_buf);
  797. if (cmprs_buff)
  798. rt_free(cmprs_buff);
  799. if (dcprs_buff)
  800. rt_free(dcprs_buff);
  801. if (dcprs_state)
  802. rt_free(dcprs_state);
  803. if (fota_err == RT_FOTA_NO_ERR)
  804. {
  805. LOG_I("Upgrade success, total %d bytes.", total_copy_size);
  806. }
  807. return fota_err;
  808. }
  809. int rt_fota_start_application(void)
  810. {
  811. int fota_res = RT_FOTA_NO_ERR;
  812. const struct fal_partition *part;
  813. rt_uint32_t app_addr;
  814. part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  815. if (part == RT_NULL)
  816. {
  817. LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
  818. fota_res = RT_FOTA_GENERAL_ERR;
  819. goto __exit_start_application;
  820. }
  821. app_addr = part->offset + 0x08000000;
  822. //�ж��Ƿ�Ϊ0x08XXXXXX.
  823. if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
  824. {
  825. LOG_I("Illegal Flash code.");
  826. fota_res = RT_FOTA_GENERAL_ERR;
  827. goto __exit_start_application;
  828. }
  829. // ���ջ����ַ�Ƿ�Ϸ�.
  830. if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
  831. {
  832. LOG_I("Illegal Stack code.");
  833. fota_res = RT_FOTA_GENERAL_ERR;
  834. goto __exit_start_application;
  835. }
  836. /* v1.3.0:写"启动尝试"标记(0xDEAD)——APP 启动后(main 开头)会清零;
  837. * 若复位后仍残留,说明 APP 未正常启动,由 rt_fota_check_boot_state() 触发回滚 */
  838. rt_fota_bkp_dr2_write(RT_FOTA_BOOT_TRY_BKP_MAGIC);
  839. LOG_I("Implement application now.");
  840. __disable_irq();
  841. /* 禁用并清除全部 NVIC 中断(原循环因 F1 无 FPU_IRQn 被注释,改用寄存器遍历),
  842. * 防止 APP 启动初期残留/挂起中断使用旧向量表或未初始化外设 */
  843. for (int nvic_idx = 0; nvic_idx < 8; nvic_idx++)
  844. {
  845. NVIC->ICER[nvic_idx] = 0xFFFFFFFF;
  846. NVIC->ICPR[nvic_idx] = 0xFFFFFFFF;
  847. }
  848. /* 清除可能挂起的系统异常(PendSV/SysTick) */
  849. SCB->ICSR |= SCB_ICSR_PENDSVCLR_Msk;
  850. SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;
  851. /* 停止 SysTick */
  852. SysTick->CTRL = 0;
  853. SysTick->LOAD = 0;
  854. SysTick->VAL = 0;
  855. /* 外设与时钟恢复到默认状态 */
  856. HAL_DeInit();
  857. HAL_RCC_DeInit();
  858. HAL_DeInit();
  859. /* 向量表预先指向 APP(其 SystemInit 会先写回 0x08000000,此步保证交接窗口安全) */
  860. SCB->VTOR = app_addr;
  861. __DSB();
  862. __ISB();
  863. //用户代码区第二个字为程序开始地址(复位地址)
  864. app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
  865. /* Configure main stack */
  866. __set_MSP(*(__IO uint32_t *)app_addr);
  867. /* 由线程(PSP)切回 MSP:APP 以复位状态(CONTROL=0)运行 */
  868. __set_CONTROL(0);
  869. /* 恢复中断屏蔽(PRIMASK=0),以接近复位后的状态进入 APP */
  870. __enable_irq();
  871. /* jump to application */
  872. app_func();
  873. __exit_start_application:
  874. LOG_I("Implement application failed.");
  875. HMI_WRITE("Implement application failed.");
  876. return fota_res;
  877. }
  878. static rt_err_t rt_fota_get_shell_key(void)
  879. {
  880. char ch;
  881. rt_err_t res = RT_EOK;
  882. rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
  883. rt_tick_t tick_start, tick_stop;
  884. RT_ASSERT(shell_dev != RT_NULL);
  885. RT_ASSERT(shell_sem != RT_NULL);
  886. rt_tick_set(0);
  887. tick_start = rt_tick_get();
  888. while (1)
  889. {
  890. if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
  891. {
  892. if (rt_sem_take(shell_sem, timeout) != RT_EOK)
  893. {
  894. res = RT_ERROR;
  895. goto __exit_get_shell_key;
  896. }
  897. }
  898. if (ch == 0x0d)
  899. goto __exit_get_shell_key;
  900. tick_stop = rt_tick_get();
  901. if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
  902. {
  903. res = RT_ERROR;
  904. goto __exit_get_shell_key;
  905. }
  906. timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
  907. }
  908. __exit_get_shell_key:
  909. if(stop_in_bootloader!=0)
  910. {
  911. res = RT_EOK;
  912. }
  913. return res;
  914. }
  915. static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
  916. {
  917. RT_ASSERT(shell_sem != RT_NULL);
  918. /* release semaphore to let fota thread rx data */
  919. rt_sem_release(shell_sem);
  920. return RT_EOK;
  921. }
  922. static rt_err_t rt_fota_set_device(const char *device_name)
  923. {
  924. rt_device_t dev;
  925. RT_ASSERT(device_name != RT_NULL);
  926. dev = rt_device_find(device_name);
  927. if (dev == RT_NULL)
  928. {
  929. LOG_D("Can not find device: %s.", device_name);
  930. return RT_ERROR;
  931. }
  932. if (shell_sem)
  933. rt_sem_delete(shell_sem);
  934. shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
  935. if (shell_sem == RT_NULL)
  936. return RT_ERROR;
  937. if (dev == shell_dev)
  938. return RT_EOK;
  939. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  940. {
  941. if (shell_dev != RT_NULL)
  942. {
  943. /* close old finsh device */
  944. rt_device_close(shell_dev);
  945. rt_device_set_rx_indicate(shell_dev, RT_NULL);
  946. }
  947. shell_dev = dev;
  948. rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
  949. LOG_D("Shell device %s open success.", device_name);
  950. return RT_EOK;
  951. }
  952. LOG_D("Shell device %s open failed.", device_name);
  953. return RT_ERROR;
  954. }
  955. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  956. static rt_err_t rt_fota_check_defalut_key(void)
  957. {
  958. int chk_idx;
  959. /* GPIO initialized */
  960. rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
  961. /* Delay for power up */
  962. rt_thread_mdelay(500);
  963. if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
  964. rt_kprintf("Default firmware key pressed:\n");
  965. /* Check GPIO status */
  966. for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
  967. {
  968. rt_thread_mdelay(RT_TICK_PER_SECOND);
  969. rt_kprintf(">");
  970. }
  971. if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
  972. return RT_ERROR;
  973. else
  974. return RT_EOK;
  975. }
  976. #endif
  977. /* 检测应用侧写入的 RTC_BKP_DR2 升级请求(值约定见文件头 v1.3.0 区块):
  978. * 命中 0x5AA5 时清除标志并返回 RT_EOK(请求 ymodem 升级) */
  979. static rt_err_t rt_fota_check_ymodem_request(void)
  980. {
  981. rt_uint16_t boot_flag;
  982. boot_flag = rt_fota_bkp_dr2_read();
  983. if (boot_flag != RT_FOTA_YMODEM_REQ_BKP_MAGIC)
  984. {
  985. return RT_ERROR;
  986. }
  987. /* 先清除标志:避免 ymodem 完成复位后再次进入升级模式(app 启动时也会清除) */
  988. rt_fota_bkp_dr2_write(0x0000);
  989. return RT_EOK;
  990. }
  991. /* ===================== v1.3.0 APP 固件备份 / 回滚 ===================== */
  992. /* 将当前 APP 分区固件备份到 df_area(升级搬运前自动调用;uart4 "backupapp" 手动触发)。
  993. * 流程:失效旧头 -> 读 APP 计算 CRC -> 擦数据区 -> 回写数据 -> 最后写头。
  994. * 返回 RT_FOTA_NO_ERR 表示备份有效。 */
  995. int rt_fota_backup_app_to_df(void)
  996. {
  997. rt_fota_backup_head_t head;
  998. const struct fal_partition *app_part;
  999. const struct fal_partition *df_part;
  1000. rt_uint8_t *buf = RT_NULL;
  1001. rt_uint32_t data_len = 0, pos, read_need, read_len, crc, erase_len;
  1002. int fota_res = RT_FOTA_NO_ERR;
  1003. app_part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  1004. df_part = fal_partition_find(RT_FOTA_DF_PART_NAME);
  1005. if ((app_part == RT_NULL) || (df_part == RT_NULL))
  1006. {
  1007. LOG_I("Backup: partition not found.");
  1008. fota_res = RT_FOTA_GENERAL_ERR;
  1009. goto __exit_backup;
  1010. }
  1011. /* 数据区(头部预留 + APP 大小)必须容得下 */
  1012. data_len = app_part->len;
  1013. erase_len = RT_ALIGN(data_len, RT_FOTA_BACKUP_BLK_SIZE);
  1014. if ((RT_FOTA_BACKUP_HEAD_RSV + erase_len) > df_part->len)
  1015. {
  1016. LOG_I("Backup: no enough space in [%s].", df_part->name);
  1017. fota_res = RT_FOTA_GENERAL_ERR;
  1018. goto __exit_backup;
  1019. }
  1020. buf = rt_malloc(RT_FOTA_BACKUP_BLK_SIZE);
  1021. if (buf == RT_NULL)
  1022. {
  1023. LOG_I("Backup: no memory.");
  1024. fota_res = RT_FOTA_NO_MEM_ERR;
  1025. goto __exit_backup;
  1026. }
  1027. /* 1. 先擦除备份头:旧备份即刻失效,中途掉电不会残留旧的"有效"magic */
  1028. if (fal_partition_erase(df_part, 0, RT_FOTA_BACKUP_HEAD_RSV) < 0)
  1029. {
  1030. LOG_I("Backup: erase backup head failed.");
  1031. fota_res = RT_FOTA_PART_ERASE_ERR;
  1032. goto __exit_backup;
  1033. }
  1034. /* 2. 读取 APP 分区并计算数据 CRC32 */
  1035. extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
  1036. crc = 0xffffffff;
  1037. for (pos = 0; pos < data_len;)
  1038. {
  1039. read_need = data_len - pos;
  1040. if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
  1041. read_need = RT_FOTA_BACKUP_BLK_SIZE;
  1042. read_len = fal_partition_read(app_part, pos, buf, read_need);
  1043. if (read_len <= 0)
  1044. {
  1045. LOG_I("Backup: read [%s] failed.", app_part->name);
  1046. fota_res = RT_FOTA_PART_READ_ERR;
  1047. goto __exit_backup;
  1048. }
  1049. crc = rt_fota_step_crc(crc, buf, read_len);
  1050. pos += read_len;
  1051. rt_kprintf("#");
  1052. }
  1053. crc ^= 0xffffffff;
  1054. /* 3. 擦除数据区 */
  1055. if (fal_partition_erase(df_part, RT_FOTA_BACKUP_HEAD_RSV, erase_len) < 0)
  1056. {
  1057. LOG_I("Backup: erase data area failed.");
  1058. fota_res = RT_FOTA_PART_ERASE_ERR;
  1059. goto __exit_backup;
  1060. }
  1061. /* 4. 回写 APP 数据到 df_area */
  1062. for (pos = 0; pos < data_len;)
  1063. {
  1064. read_need = data_len - pos;
  1065. if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
  1066. read_need = RT_FOTA_BACKUP_BLK_SIZE;
  1067. read_len = fal_partition_read(app_part, pos, buf, read_need);
  1068. if (read_len <= 0)
  1069. {
  1070. LOG_I("Backup: read [%s] failed.", app_part->name);
  1071. fota_res = RT_FOTA_PART_READ_ERR;
  1072. goto __exit_backup;
  1073. }
  1074. if (fal_partition_write(df_part, RT_FOTA_BACKUP_HEAD_RSV + pos, buf, read_len) < 0)
  1075. {
  1076. LOG_I("Backup: write [%s] failed.", df_part->name);
  1077. fota_res = RT_FOTA_PART_WRITE_ERR;
  1078. goto __exit_backup;
  1079. }
  1080. pos += read_len;
  1081. HMI_WRITE("debug.update.val=%d", (int)(pos * 100 / data_len));
  1082. rt_kprintf("#");
  1083. }
  1084. rt_kprintf("\r\n");
  1085. /* 5. 最后写头部:备份自此生效 */
  1086. head.magic = RT_FOTA_BACKUP_MAGIC;
  1087. head.data_len = data_len;
  1088. head.data_crc = crc;
  1089. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  1090. head.head_crc = rt_fota_crc((rt_uint8_t *)&head, sizeof(head) - 4);
  1091. if (fal_partition_write(df_part, 0, (const rt_uint8_t *)&head, sizeof(head)) < 0)
  1092. {
  1093. LOG_I("Backup: write backup head failed.");
  1094. fota_res = RT_FOTA_PART_WRITE_ERR;
  1095. goto __exit_backup;
  1096. }
  1097. __exit_backup:
  1098. if (buf)
  1099. rt_free(buf);
  1100. if (fota_res == RT_FOTA_NO_ERR)
  1101. LOG_I("Backup app to df_area success, %d bytes.", data_len);
  1102. return fota_res;
  1103. }
  1104. /* 将 df_area 中的有效备份固件恢复到 APP 分区(带完整性校验;供启动失败自动
  1105. * 回滚、uart4 的 "dbg_restore" / "resetapp" 命令共用)。
  1106. * consume_backup:恢复成功后是否消费(擦除)备份头——自动回滚置 1,防止"恢复
  1107. * 的固件也无法启动"时反复恢复擦写 flash;手动命令置 0,保留备份便于重复调试。
  1108. * 返回 RT_FOTA_NO_ERR 表示恢复完成。 */
  1109. int rt_fota_restore_app_from_df(int consume_backup)
  1110. {
  1111. rt_fota_backup_head_t head;
  1112. const struct fal_partition *app_part;
  1113. const struct fal_partition *df_part;
  1114. rt_uint8_t *buf = RT_NULL;
  1115. rt_uint32_t pos, read_need, read_len, crc;
  1116. int fota_res = RT_FOTA_NO_ERR;
  1117. rt_memset(&head, 0, sizeof(head));
  1118. app_part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  1119. df_part = fal_partition_find(RT_FOTA_DF_PART_NAME);
  1120. if ((app_part == RT_NULL) || (df_part == RT_NULL))
  1121. {
  1122. LOG_I("Restore: partition not found.");
  1123. fota_res = RT_FOTA_GENERAL_ERR;
  1124. goto __exit_restore;
  1125. }
  1126. /* 1. 读备份头并校验(magic + 头 CRC32 + 长度合法性) */
  1127. if (fal_partition_read(df_part, 0, (rt_uint8_t *)&head, sizeof(head)) < 0)
  1128. {
  1129. LOG_I("Restore: read backup head failed.");
  1130. fota_res = RT_FOTA_PART_READ_ERR;
  1131. goto __exit_restore;
  1132. }
  1133. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  1134. if ((head.magic != RT_FOTA_BACKUP_MAGIC) ||
  1135. (rt_fota_crc((rt_uint8_t *)&head, sizeof(head) - 4) != head.head_crc))
  1136. {
  1137. LOG_I("Restore: no valid backup in [%s].", df_part->name);
  1138. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  1139. goto __exit_restore;
  1140. }
  1141. if ((head.data_len == 0) ||
  1142. (head.data_len > app_part->len) ||
  1143. ((RT_FOTA_BACKUP_HEAD_RSV + head.data_len) > df_part->len))
  1144. {
  1145. LOG_I("Restore: invalid backup length(%d).", head.data_len);
  1146. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  1147. goto __exit_restore;
  1148. }
  1149. buf = rt_malloc(RT_FOTA_BACKUP_BLK_SIZE);
  1150. if (buf == RT_NULL)
  1151. {
  1152. LOG_I("Restore: no memory.");
  1153. fota_res = RT_FOTA_NO_MEM_ERR;
  1154. goto __exit_restore;
  1155. }
  1156. /* 2. 备份数据 CRC32 校验(先校验后擦写,避免把无效数据写入 APP 分区) */
  1157. extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
  1158. crc = 0xffffffff;
  1159. for (pos = 0; pos < head.data_len;)
  1160. {
  1161. read_need = head.data_len - pos;
  1162. if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
  1163. read_need = RT_FOTA_BACKUP_BLK_SIZE;
  1164. read_len = fal_partition_read(df_part, RT_FOTA_BACKUP_HEAD_RSV + pos, buf, read_need);
  1165. if (read_len <= 0)
  1166. {
  1167. LOG_I("Restore: read [%s] failed.", df_part->name);
  1168. fota_res = RT_FOTA_PART_READ_ERR;
  1169. goto __exit_restore;
  1170. }
  1171. crc = rt_fota_step_crc(crc, buf, read_len);
  1172. pos += read_len;
  1173. rt_kprintf("#");
  1174. }
  1175. crc ^= 0xffffffff;
  1176. if (crc != head.data_crc)
  1177. {
  1178. LOG_I("Restore: backup data CRC32 error (0x%08X != 0x%08X).", crc, head.data_crc);
  1179. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  1180. goto __exit_restore;
  1181. }
  1182. /* 3. 擦除 APP 分区并回写备份数据 */
  1183. if (fal_partition_erase(app_part, 0, app_part->len) < 0)
  1184. {
  1185. LOG_I("Restore: erase [%s] failed.", app_part->name);
  1186. fota_res = RT_FOTA_PART_ERASE_ERR;
  1187. goto __exit_restore;
  1188. }
  1189. for (pos = 0; pos < head.data_len;)
  1190. {
  1191. read_need = head.data_len - pos;
  1192. if (read_need > RT_FOTA_BACKUP_BLK_SIZE)
  1193. read_need = RT_FOTA_BACKUP_BLK_SIZE;
  1194. read_len = fal_partition_read(df_part, RT_FOTA_BACKUP_HEAD_RSV + pos, buf, read_need);
  1195. if (read_len <= 0)
  1196. {
  1197. LOG_I("Restore: read [%s] failed.", df_part->name);
  1198. fota_res = RT_FOTA_PART_READ_ERR;
  1199. goto __exit_restore;
  1200. }
  1201. if (fal_partition_write(app_part, pos, buf, read_len) < 0)
  1202. {
  1203. LOG_I("Restore: write [%s] failed.", app_part->name);
  1204. fota_res = RT_FOTA_PART_WRITE_ERR;
  1205. goto __exit_restore;
  1206. }
  1207. pos += read_len;
  1208. HMI_WRITE("debug.update.val=%d", (int)(pos * 100 / head.data_len));
  1209. rt_kprintf("#");
  1210. }
  1211. rt_kprintf("\r\n");
  1212. /* 4. 自动回滚场景消费备份,防止"恢复的固件仍无法启动"反复恢复 */
  1213. if (consume_backup)
  1214. {
  1215. if (fal_partition_erase(df_part, 0, RT_FOTA_BACKUP_HEAD_RSV) < 0)
  1216. LOG_I("Restore: consume backup head failed.");
  1217. else
  1218. LOG_I("Restore: backup consumed.");
  1219. }
  1220. __exit_restore:
  1221. if (buf)
  1222. rt_free(buf);
  1223. if (fota_res == RT_FOTA_NO_ERR)
  1224. LOG_I("Restore app from df_area success, %d bytes.", head.data_len);
  1225. else
  1226. LOG_I("Restore app from df_area failed(%d).", fota_res);
  1227. return fota_res;
  1228. }
  1229. /* 启动失败检测与自动回滚:进入 bootloader 后读取 RTC_BKP_DR2——
  1230. * 0 / 0x5AA5 分别表示上次启动正常 / 升级请求(由 ymodem 检测处理),跳过;
  1231. * 其它非 0 值(跳转前写入的 0xDEAD 未被 APP 清零)说明 APP 未正常启动:
  1232. * 标记 0xDEAD -> 有有效备份则回滚 APP -> 对齐 fm_area 版本,防止同一升级包
  1233. * 在回滚后再次被自动应用(用户重新下发升级包会重写 fm_area,不受影响)。 */
  1234. static void rt_fota_check_boot_state(void)
  1235. {
  1236. rt_uint16_t boot_flag = rt_fota_bkp_dr2_read();
  1237. if ((boot_flag == 0x0000) || (boot_flag == RT_FOTA_YMODEM_REQ_BKP_MAGIC))
  1238. {
  1239. return;
  1240. }
  1241. LOG_I("RTC_BKP_DR2 = 0x%04X: application boot failed, try restore.", boot_flag);
  1242. rt_fota_signal_led_mode(led_upgrade_mode);
  1243. /* 约定:检测到启动失败先标记 0xDEAD */
  1244. rt_fota_bkp_dr2_write(RT_FOTA_BOOT_TRY_BKP_MAGIC);
  1245. if (rt_fota_restore_app_from_df(1) == RT_FOTA_NO_ERR)
  1246. {
  1247. /* 对齐 fm_area 版本:本次升级包视为已处理,避免回滚后再次自动升级 */
  1248. rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  1249. }
  1250. else
  1251. {
  1252. LOG_I("No valid backup in df_area, skip restore.");
  1253. }
  1254. }
  1255. void rt_fota_thread_entry(void *arg)
  1256. {
  1257. int fota_err = RT_FOTA_NO_ERR;
  1258. extern int finsh_system_init(void);
  1259. /* Signal led initialized */
  1260. rt_fota_signal_led_init();
  1261. /* Partition initialized */
  1262. fota_err = rt_fota_boot_verify();
  1263. if (fota_err != RT_FOTA_NO_ERR)
  1264. {
  1265. LOG_I("Partition initialized failed.");
  1266. }
  1267. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  1268. /* Default key check */
  1269. if (rt_fota_check_defalut_key() == RT_EOK)
  1270. {
  1271. /* enter to defalut key mode */
  1272. rt_fota_signal_led_mode(led_upgrade_mode);
  1273. goto __exit_default_entry;
  1274. }
  1275. #endif
  1276. /* RTC 备份寄存器 DR2 升级请求检测:命中 0x5AA5 → 停留 bootloader 执行 ymodem 升级 */
  1277. if (rt_fota_check_ymodem_request() == RT_EOK)
  1278. {
  1279. extern void ymodem_ota(uint8_t argc, char **argv);
  1280. LOG_I("RTC_BKP_DR2 = 0x5AA5, stay in bootloader, start ymodem ota.");
  1281. rt_fota_signal_led_mode(led_upgrade_mode);
  1282. /* 下载分区默认 fm_area;ymodem 完成后内部复位,正常不返回 */
  1283. ymodem_ota(1, RT_NULL);
  1284. }
  1285. /* v1.3.0:启动失败检测与自动回滚(详见函数注释),需在 shell 交互前处理 */
  1286. rt_fota_check_boot_state();
  1287. /* Shell initialized */
  1288. if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
  1289. {
  1290. if(easyflash_init()==EF_NO_ERR) //初始化成功
  1291. {
  1292. rt_uint8_t boot_flag_buf[4] = {0, 0, 0, 0};
  1293. ef_get_env_blob("stop_in_bootloader", boot_flag_buf, sizeof(boot_flag_buf), NULL);
  1294. /* 兼容三种历史存储格式:字符串 "0"/"1"(EasyFlash 默认值)与二进制 0/1。
  1295. * 历史缺陷:默认值 "0" 按二进制读为 0x30(非零),会误判为停留引导模式 */
  1296. if (boot_flag_buf[0] == '0')
  1297. stop_in_bootloader = 0;
  1298. else if (boot_flag_buf[0] == '1')
  1299. stop_in_bootloader = 1;
  1300. else
  1301. stop_in_bootloader = (boot_flag_buf[0] != 0) ? 1 : 0;
  1302. rt_kprintf("stop_in_bootloader:%d\n", stop_in_bootloader);
  1303. }
  1304. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  1305. if (rt_fota_get_shell_key() == RT_EOK)
  1306. {
  1307. if(stop_in_bootloader!=0)
  1308. {
  1309. stop_in_bootloader = 0;
  1310. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  1311. }
  1312. goto __exit_shell_entry;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. LOG_I("Shell device config failed.");
  1318. }
  1319. /* Firmware partition verify */
  1320. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  1321. if (fota_err != RT_FOTA_NO_ERR)
  1322. goto __exit_boot_entry;
  1323. /* Check upgrade status */
  1324. if (rt_fota_check_upgrade() <= 0)
  1325. goto __exit_boot_entry;
  1326. /* enter to upgrade mode */
  1327. rt_fota_signal_led_mode(led_upgrade_mode);
  1328. /* v1.3.0:搬运前先把当前 APP 固件备份到 df_area(新固件启动失败时回滚用)。
  1329. * 备份失败不阻断升级(极端情况 df_area 异常;升级包读自 fm_area) */
  1330. if (rt_fota_backup_app_to_df() != RT_FOTA_NO_ERR)
  1331. {
  1332. LOG_I("Backup app to df_area failed, continue upgrade.");
  1333. }
  1334. /* Implement upgrade, copy firmware partition to app partition */
  1335. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  1336. if (fota_err != RT_FOTA_NO_ERR)
  1337. goto __exit_boot_entry;
  1338. /* Update new application verison in RBL file of firmware partition */
  1339. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  1340. if (fota_err != RT_FOTA_NO_ERR)
  1341. goto __exit_boot_entry;
  1342. __exit_boot_entry:
  1343. /* Implement application */
  1344. rt_fota_start_application();
  1345. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  1346. __exit_default_entry:
  1347. #endif
  1348. /* Implement upgrade, copy default partition to app partition */
  1349. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1350. {
  1351. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1352. {
  1353. rt_fota_start_application();
  1354. }
  1355. }
  1356. LOG_I("Boot application failed, entry shell mode.");
  1357. __exit_shell_entry:
  1358. /* enter to shell mode */
  1359. rt_fota_signal_led_mode(led_shell_mode);
  1360. /* Implement shell */
  1361. finsh_system_init();
  1362. }
  1363. void rt_fota_init(void)
  1364. {
  1365. rt_thread_t tid;
  1366. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  1367. if (tid != RT_NULL)
  1368. {
  1369. rt_thread_startup(tid);
  1370. }
  1371. else
  1372. {
  1373. LOG_I("rt-fota thread create failed.");
  1374. }
  1375. }
  1376. void rt_fota_info(rt_uint8_t argc, char **argv)
  1377. {
  1378. char put_buf[24];
  1379. char part_name[2][FAL_DEV_NAME_MAX] =
  1380. {
  1381. {RT_FOTA_FM_PART_NAME},
  1382. {RT_FOTA_DF_PART_NAME}
  1383. };
  1384. const char* help_info[] =
  1385. {
  1386. [0] = "fota probe - probe RBL file of partiton",
  1387. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  1388. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  1389. [3] = "fota exec - execute application program",
  1390. };
  1391. if (argc < 2)
  1392. {
  1393. rt_kprintf("Usage:\n");
  1394. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1395. {
  1396. rt_kprintf("%s\n", help_info[i]);
  1397. }
  1398. rt_kprintf("\n");
  1399. }
  1400. else
  1401. {
  1402. const char *operator = argv[1];
  1403. if (!rt_strcmp(operator, "probe"))
  1404. {
  1405. for (int i = 0; i < 2; i++)
  1406. {
  1407. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  1408. {
  1409. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  1410. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  1411. rt_memset(put_buf, 0x0, sizeof(put_buf));
  1412. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  1413. {
  1414. rt_strncpy(put_buf, " AES", 4);
  1415. }
  1416. else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  1417. {
  1418. rt_strncpy(put_buf, " XOR", 4);
  1419. }
  1420. else
  1421. {
  1422. rt_strncpy(put_buf, "NONE", 4);
  1423. }
  1424. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  1425. {
  1426. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  1427. }
  1428. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  1429. {
  1430. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  1431. }
  1432. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  1433. {
  1434. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  1435. }
  1436. if (rt_strlen(put_buf) <= 0)
  1437. {
  1438. rt_strncpy(put_buf, "None", 4);
  1439. }
  1440. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  1441. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  1442. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  1443. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  1444. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1445. }
  1446. }
  1447. }
  1448. else if (!rt_strcmp(operator, "show"))
  1449. {
  1450. const struct fal_partition *part;
  1451. const char *part_name = argv[2];
  1452. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  1453. rt_uint32_t size = strtol(argv[4], NULL, 0);
  1454. rt_uint8_t buf[16];
  1455. part = fal_partition_find(part_name);
  1456. if (part != RT_NULL)
  1457. {
  1458. while (size > 16)
  1459. {
  1460. fal_partition_read(part, addr, buf, 16);
  1461. rt_kprintf("%08X: ", addr);
  1462. for (int i = 0; i < 16; i++)
  1463. {
  1464. rt_kprintf("%02X ", buf[i]);
  1465. }
  1466. rt_kprintf("\n");
  1467. size -= 16;
  1468. addr += 16;
  1469. }
  1470. fal_partition_read(part, addr, buf, size);
  1471. rt_kprintf("%08X: ", addr);
  1472. for (int i = 0; i < size; i++)
  1473. {
  1474. rt_kprintf("%02X ", buf[i]);
  1475. }
  1476. rt_kprintf("\n");
  1477. }
  1478. else
  1479. {
  1480. rt_kprintf("%s partition is not exist!\n", part_name);
  1481. }
  1482. }
  1483. else if (!rt_strcmp(operator, "clone"))
  1484. {
  1485. const char *dst_part_name = argv[2];
  1486. const char *src_part_name = argv[3];
  1487. const struct fal_partition *dst_part;
  1488. const struct fal_partition *src_part;
  1489. dst_part = fal_partition_find(dst_part_name);
  1490. src_part = fal_partition_find(src_part_name);
  1491. if (dst_part == RT_NULL || src_part == RT_NULL)
  1492. {
  1493. if (dst_part == RT_NULL)
  1494. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1495. if (src_part == RT_NULL)
  1496. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1497. }
  1498. else
  1499. {
  1500. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1501. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1502. {
  1503. int clone_pos = 0;
  1504. int clone_len = 0, clone_tol_len;
  1505. rt_uint8_t *buf = rt_malloc(4096);
  1506. if (dst_part->len < src_part->len)
  1507. clone_tol_len = dst_part->len;
  1508. else
  1509. clone_tol_len = src_part->len;
  1510. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1511. {
  1512. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1513. if (clone_len < 0)
  1514. {
  1515. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1516. break;
  1517. }
  1518. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1519. {
  1520. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1521. break;
  1522. }
  1523. rt_kprintf("#");
  1524. clone_pos += clone_len;
  1525. }
  1526. if (clone_pos >= clone_tol_len)
  1527. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1528. else
  1529. rt_kprintf("\nClone partition failed!\n");
  1530. if (buf)
  1531. rt_free(buf);
  1532. }
  1533. }
  1534. }
  1535. else if (!rt_strcmp(operator, "exec"))
  1536. {
  1537. rt_fota_start_application();
  1538. }
  1539. else
  1540. {
  1541. rt_kprintf("Usage:\n");
  1542. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1543. {
  1544. rt_kprintf("%s\n", help_info[i]);
  1545. }
  1546. rt_kprintf("\n");
  1547. }
  1548. }
  1549. }
  1550. /**
  1551. * rt-fota />ymodem_ota
  1552. */
  1553. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);
  1554. /* msh 命令:查看/设置 RTC_BKP_DR2 启动标志(取值约定见文件头 v1.3.0 区块)。
  1555. * 典型用途:手动进入 bootloader 维护后、重新上电前执行 `dr2 0` 清除标志,
  1556. * 避免残留非 0 值(如启动失败检测写入的 0xDEAD)在下次上电被
  1557. * rt_fota_check_boot_state() 误判为“APP 未正常启动”而触发 df_area→app 回滚;
  1558. * 也可写入 0x5AA5 / 0xDEAD 分别验证 ymodem 升级入口 / 启动失败回滚路径。 */
  1559. void rt_fota_dr2(int argc, char **argv)
  1560. {
  1561. rt_uint16_t boot_flag;
  1562. char *endp = NULL;
  1563. if (argc < 2)
  1564. {
  1565. boot_flag = rt_fota_bkp_dr2_read();
  1566. rt_kprintf("RTC_BKP_DR2 = 0x%04X", boot_flag);
  1567. if (boot_flag == 0x0000)
  1568. rt_kprintf(" (normal)\n");
  1569. else if (boot_flag == RT_FOTA_YMODEM_REQ_BKP_MAGIC)
  1570. rt_kprintf(" (ymodem upgrade request)\n");
  1571. else if (boot_flag == RT_FOTA_BOOT_TRY_BKP_MAGIC)
  1572. rt_kprintf(" (boot-fail marker, restore pending)\n");
  1573. else
  1574. rt_kprintf(" (unknown, restore pending)\n");
  1575. return;
  1576. }
  1577. boot_flag = (rt_uint16_t)strtol(argv[1], &endp, 0);
  1578. if (endp == argv[1])
  1579. {
  1580. rt_kprintf("usage: dr2 - show boot flag\n");
  1581. rt_kprintf(" dr2 0 - clear flag (avoid false restore)\n");
  1582. rt_kprintf(" dr2 <value> - set value (0x5AA5 / 0xDEAD for test)\n");
  1583. return;
  1584. }
  1585. rt_fota_bkp_dr2_write(boot_flag);
  1586. rt_kprintf("RTC_BKP_DR2 set to 0x%04X\n", rt_fota_bkp_dr2_read());
  1587. }
  1588. MSH_CMD_EXPORT_ALIAS(rt_fota_dr2, dr2, Show or set RTC_BKP_DR2 boot flag);