rt_fota.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. #define DBG_ENABLE
  30. #define DBG_SECTION_NAME "fota"
  31. #ifdef RT_FOTA_DEBUG
  32. #define DBG_LEVEL DBG_LOG
  33. #else
  34. #define DBG_LEVEL DBG_INFO
  35. #endif
  36. #define DBG_COLOR
  37. #include <rtdbg.h>
  38. #ifndef RT_FOTA_THREAD_STACK_SIZE
  39. #define RT_FOTA_THREAD_STACK_SIZE 4096
  40. #endif
  41. #ifndef RT_FOTA_THREAD_PRIORITY
  42. #define RT_FOTA_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 3)
  43. #endif
  44. #ifndef RT_FOTA_ALGO_BUFF_SIZE
  45. #define RT_FOTA_ALGO_BUFF_SIZE 2048 // STM32 此处需要设置为2048 因为一个扇区是2K大小
  46. #endif
  47. /**
  48. * AES256 encryption algorithm option
  49. */
  50. #ifndef RT_FOTA_ALGO_AES_IV
  51. #define RT_FOTA_ALGO_AES_IV "0123456789ABCDEF"
  52. #endif
  53. #ifndef RT_FOTA_ALGO_AES_KEY
  54. #define RT_FOTA_ALGO_AES_KEY "0123456789ABCDEF0123456789ABCDEF"
  55. #endif
  56. #ifndef RT_FOTA_BLOCK_HEADER_SIZE
  57. #define RT_FOTA_BLOCK_HEADER_SIZE 4
  58. #endif
  59. #ifndef RT_FOTA_CMPRS_BUFFER_SIZE
  60. #define RT_FOTA_CMPRS_BUFFER_SIZE 4096
  61. #endif
  62. #ifndef RT_FOTA_FASTLZ_BUFFER_PADDING
  63. #define RT_FOTA_FASTLZ_BUFFER_PADDING FASTLZ_BUFFER_PADDING(RT_FOTA_CMPRS_BUFFER_SIZE)
  64. #endif
  65. #ifndef RT_FOTA_QUICKLZ_BUFFER_PADDING
  66. #define RT_FOTA_QUICKLZ_BUFFER_PADDING QLZ_BUFFER_PADDING
  67. #endif
  68. #ifndef RT_FOTA_ENTER_SHELL_KEY
  69. #define RT_FOTA_ENTER_SHELL_KEY 0x0d
  70. #endif
  71. #ifndef RT_FOTA_GET_CHAR_WAITTIGN
  72. #define RT_FOTA_GET_CHAR_WAITTIGN (RT_TICK_PER_SECOND * 5)
  73. #endif
  74. #ifndef RT_FOTA_SIGNAL_LED_PIN
  75. #define RT_FOTA_SIGNAL_LED_PIN GET_PIN(B, 3)
  76. #endif
  77. #ifndef RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE
  78. #define RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE 1024
  79. #endif
  80. #ifndef RT_FOTA_SIGNAL_LED_THREAD_PRIORITY
  81. #define RT_FOTA_SIGNAL_LED_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 4)
  82. #endif
  83. /* For signal led */
  84. static led_t *signal_led = NULL;
  85. static led_mem_opreation_t signal_led_mem_op;
  86. const char *led_shell_mode = "500,500,"; /* 1Hz ��˸ */
  87. const char *led_upgrade_mode = "50,50,"; /* 10Hz ��˸ */
  88. const char *led_off_mode = "0,100,"; /* ���� */
  89. const char *led_on_mode = "100,0,"; /* ���� */
  90. static int stop_in_bootloader=0;
  91. /* For shell */
  92. static rt_sem_t shell_sem = RT_NULL;
  93. static rt_device_t shell_dev = RT_NULL;
  94. typedef struct {
  95. char type[4];
  96. rt_uint16_t fota_algo;
  97. rt_uint8_t fm_time[6];
  98. char app_part_name[16];
  99. char download_version[24];
  100. char current_version[24];
  101. rt_uint32_t code_crc;
  102. rt_uint32_t hash_val;
  103. rt_uint32_t raw_size;
  104. rt_uint32_t com_size;
  105. rt_uint32_t head_crc;
  106. } rt_fota_part_head, *rt_fota_part_head_t;
  107. typedef void (*rt_fota_app_func)(void);
  108. static rt_fota_app_func app_func = RT_NULL;
  109. static rt_fota_part_head fota_part_head;
  110. static void rt_fota_signal_led_on(void)
  111. {
  112. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_LOW);
  113. }
  114. static void rt_fota_signal_led_off(void)
  115. {
  116. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_HIGH);
  117. }
  118. static void rt_fota_signal_led_entry(void *arg)
  119. {
  120. while(1)
  121. {
  122. led_ticks();
  123. rt_thread_mdelay(LED_TICK_TIME);
  124. }
  125. }
  126. static void rt_fota_signal_led_init(void)
  127. {
  128. rt_pin_mode(RT_FOTA_SIGNAL_LED_PIN, PIN_MODE_OUTPUT);
  129. signal_led_mem_op.malloc_fn = (void* (*)(size_t))rt_malloc;
  130. signal_led_mem_op.free_fn = rt_free;
  131. led_set_mem_operation(&signal_led_mem_op);
  132. signal_led = led_create(rt_fota_signal_led_on, rt_fota_signal_led_off);
  133. /* Config signal led mode */
  134. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_on_mode);
  135. led_set_blink_over_callback(signal_led, RT_NULL);
  136. led_start(signal_led);
  137. rt_thread_t tid;
  138. 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);
  139. if (tid)
  140. rt_thread_startup(tid);
  141. }
  142. static void rt_fota_signal_led_mode(const char *led_cfg)
  143. {
  144. RT_ASSERT(led_cfg != RT_NULL);
  145. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_cfg);
  146. }
  147. static int rt_fota_boot_verify(void)
  148. {
  149. int fota_res = RT_FOTA_NO_ERR;
  150. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  151. /* partition initial */
  152. fal_init();
  153. extern int fal_init_check(void);
  154. /* verify partition */
  155. if (fal_init_check() != 1)
  156. {
  157. LOG_D("Partition initialized failed!");
  158. fota_res = RT_FOTA_GENERAL_ERR;
  159. goto __exit_boot_verify;
  160. }
  161. __exit_boot_verify:
  162. return fota_res;
  163. }
  164. int rt_fota_part_fw_verify(const char *part_name)
  165. {
  166. #define RT_FOTA_CRC_BUFF_SIZE 4096
  167. #define RT_FOTA_CRC_INIT_VAL 0xffffffff
  168. int fota_res = RT_FOTA_NO_ERR;
  169. const struct fal_partition *part;
  170. rt_fota_part_head part_head;
  171. rt_uint8_t *body_buf = RT_NULL;
  172. rt_uint32_t body_crc = RT_FOTA_CRC_INIT_VAL;
  173. rt_uint32_t hdr_crc;
  174. if (part_name == RT_NULL)
  175. {
  176. LOG_D("Invaild paramenter input!");
  177. fota_res = RT_FOTA_GENERAL_ERR;
  178. goto __exit_partition_verify;
  179. }
  180. part = fal_partition_find(part_name);
  181. if (part == RT_NULL)
  182. {
  183. LOG_D("Partition[%s] not found.", part_name);
  184. fota_res = RT_FOTA_GENERAL_ERR;
  185. goto __exit_partition_verify;
  186. }
  187. /* read the head of RBL files */
  188. if (fal_partition_read(part, 0, (rt_uint8_t *)&part_head, sizeof(rt_fota_part_head)) < 0)
  189. {
  190. LOG_D("Partition[%s] read error!", part->name);
  191. fota_res = RT_FOTA_PART_READ_ERR;
  192. goto __exit_partition_verify;
  193. }
  194. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  195. hdr_crc = rt_fota_crc((rt_uint8_t *)&part_head, sizeof(rt_fota_part_head) - 4);
  196. if (hdr_crc != part_head.head_crc)
  197. {
  198. LOG_D("Partition[%s] head CRC32 error!", part->name);
  199. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  200. goto __exit_partition_verify;
  201. }
  202. if (rt_strcmp(part_head.type, "RBL") != 0)
  203. {
  204. LOG_D("Partition[%s] type[%s] not surport.", part->name, part_head.type);
  205. fota_res = RT_FOTA_CHECK_FAILED;
  206. goto __exit_partition_verify;
  207. }
  208. if (fal_partition_find(part_head.app_part_name) == RT_NULL)
  209. {
  210. LOG_D("Partition[%s] not found.", part_head.app_part_name);
  211. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  212. goto __exit_partition_verify;
  213. }
  214. body_buf = rt_malloc(RT_FOTA_CRC_BUFF_SIZE);
  215. if (body_buf == RT_NULL)
  216. {
  217. LOG_D("Not enough memory for body CRC32 verify.");
  218. fota_res = RT_FOTA_NO_MEM_ERR;
  219. goto __exit_partition_verify;
  220. }
  221. for (int body_pos = 0; body_pos < part_head.com_size;)
  222. {
  223. int body_read_len = fal_partition_read(part, sizeof(rt_fota_part_head) + body_pos, body_buf, RT_FOTA_CRC_BUFF_SIZE);
  224. if (body_read_len > 0)
  225. {
  226. if ((body_pos + body_read_len) > part_head.com_size)
  227. {
  228. body_read_len = part_head.com_size - body_pos;
  229. }
  230. extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
  231. body_crc = rt_fota_step_crc(body_crc, body_buf, body_read_len);
  232. body_pos = body_pos + body_read_len;
  233. }
  234. else
  235. {
  236. LOG_D("Partition[%s] read error!", part->name);
  237. fota_res = RT_FOTA_PART_READ_ERR;
  238. goto __exit_partition_verify;
  239. }
  240. }
  241. body_crc = body_crc ^ RT_FOTA_CRC_INIT_VAL;
  242. if (body_crc != part_head.code_crc)
  243. {
  244. LOG_D("Partition[%s] firmware integrity verify failed.", part->name);
  245. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  246. goto __exit_partition_verify;
  247. }
  248. __exit_partition_verify:
  249. if (fota_res == RT_FOTA_NO_ERR)
  250. {
  251. rt_enter_critical();
  252. rt_memcpy(&fota_part_head, &part_head, sizeof(rt_fota_part_head));
  253. rt_exit_critical();
  254. LOG_D("partition[%s] verify success!", part->name);
  255. }
  256. else
  257. {
  258. rt_enter_critical();
  259. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  260. rt_exit_critical();
  261. LOG_D("Partition[%s] verify failed!", part->name);
  262. }
  263. if (body_buf)
  264. rt_free(body_buf);
  265. return fota_res;
  266. }
  267. int rt_fota_check_upgrade(void)
  268. {
  269. int is_upgrade = 0;
  270. if (rt_strcmp(fota_part_head.download_version, fota_part_head.current_version) != 0)
  271. {
  272. is_upgrade = 1;
  273. LOG_D("Application need upgrade.");
  274. goto __exit_check_upgrade;
  275. }
  276. __exit_check_upgrade:
  277. return is_upgrade;
  278. }
  279. int rt_fota_copy_version(const char *part_name)
  280. {
  281. #define THE_NOR_FLASH_GRANULARITY 4096
  282. int fota_res = RT_FOTA_NO_ERR;
  283. const struct fal_partition *part;
  284. rt_fota_part_head_t part_head = RT_NULL;
  285. rt_uint8_t *cache_buf = RT_NULL;
  286. part = fal_partition_find(part_name);
  287. if (part == RT_NULL)
  288. {
  289. LOG_D("Find partition[%s] not found.", part_name);
  290. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  291. goto __exit_copy_version;
  292. }
  293. cache_buf = rt_malloc(THE_NOR_FLASH_GRANULARITY);
  294. if (cache_buf == RT_NULL)
  295. {
  296. LOG_D("Not enough memory for head erase.");
  297. fota_res = RT_FOTA_NO_MEM_ERR;
  298. goto __exit_copy_version;
  299. }
  300. part_head = (rt_fota_part_head_t)cache_buf;
  301. if (fal_partition_read(part, 0, cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  302. {
  303. LOG_I("Read partition[%s] failed.", part_name);
  304. fota_res = RT_FOTA_PART_READ_ERR;
  305. goto __exit_copy_version;
  306. }
  307. rt_memcpy(part_head->current_version, part_head->download_version, sizeof(part_head->current_version));
  308. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  309. part_head->head_crc = rt_fota_crc((rt_uint8_t *)part_head, sizeof(rt_fota_part_head) - 4);
  310. if (fal_partition_erase(part, 0, THE_NOR_FLASH_GRANULARITY) < 0)
  311. {
  312. LOG_D("Erase partition[%s] failed.", part_name);
  313. fota_res = RT_FOTA_PART_ERASE_ERR;
  314. goto __exit_copy_version;
  315. }
  316. if (fal_partition_write(part, 0, (const rt_uint8_t *)cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  317. {
  318. LOG_I("Write partition[%s] failed.", part_name);
  319. fota_res = RT_FOTA_PART_WRITE_ERR;
  320. goto __exit_copy_version;
  321. }
  322. __exit_copy_version:
  323. if (cache_buf)
  324. rt_free(cache_buf);
  325. if (fota_res != RT_FOTA_NO_ERR)
  326. LOG_I("Copy firmware version failed!");
  327. else
  328. LOG_I("Copy firmware version Success!");
  329. return fota_res;
  330. }
  331. int rt_fota_erase_app_part(void)
  332. {
  333. int fota_res = RT_FOTA_NO_ERR;
  334. const struct fal_partition *part;
  335. part = fal_partition_find(fota_part_head.app_part_name);
  336. if (part == RT_NULL)
  337. {
  338. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  339. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  340. goto __exit_partition_erase;
  341. }
  342. LOG_I("Partition[%s] erase start:", part->name);
  343. if (fal_partition_erase(part, 0, fota_part_head.raw_size) < 0)
  344. {
  345. LOG_D("Partition[%s] erase failed!", part->name);
  346. fota_res = RT_FOTA_PART_ERASE_ERR;
  347. goto __exit_partition_erase;
  348. }
  349. __exit_partition_erase:
  350. if (fota_res == RT_FOTA_NO_ERR)
  351. {
  352. LOG_D("Partition[%s] erase %d bytes success!", part->name, fota_part_head.raw_size);
  353. }
  354. return fota_res;
  355. }
  356. int rt_fota_write_app_part(int fw_pos, rt_uint8_t *fw_buf, int fw_len)
  357. {
  358. int rt_fota_res = RT_FOTA_NO_ERR;
  359. const struct fal_partition *part;
  360. part = fal_partition_find(fota_part_head.app_part_name);
  361. if (part == RT_NULL)
  362. {
  363. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  364. rt_fota_res = RT_FOTA_FW_VERIFY_FAILED;
  365. goto __partition_write_exit;
  366. }
  367. if (fal_partition_write(part, fw_pos, fw_buf, fw_len) < 0)
  368. {
  369. LOG_D("Partition[%s] write failed!", part->name);
  370. rt_fota_res = RT_FOTA_PART_WRITE_ERR;
  371. goto __partition_write_exit;
  372. }
  373. __partition_write_exit:
  374. if (rt_fota_res == RT_FOTA_NO_ERR)
  375. {
  376. LOG_D("Partition[%s] write %d bytes success!", part->name, fw_len);
  377. }
  378. return rt_fota_res;
  379. }
  380. 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)
  381. {
  382. int fota_err = RT_FOTA_NO_ERR;
  383. rt_uint8_t *encrypt_buf = RT_NULL;
  384. if ((part == RT_NULL) || (decrypt_buf == RT_NULL)
  385. || (decrypt_len % 16 != 0) || (decrypt_len > RT_FOTA_ALGO_BUFF_SIZE))
  386. {
  387. fota_err = RT_FOTA_GENERAL_ERR;
  388. goto __exit_read_decrypt;
  389. }
  390. rt_memset(decrypt_buf, 0x0, decrypt_len);
  391. /* Not use AES256 algorithm */
  392. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  393. {
  394. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, decrypt_buf, decrypt_len);
  395. if (fota_err <= 0)
  396. {
  397. fota_err = RT_FOTA_PART_READ_ERR;
  398. }
  399. goto __exit_read_decrypt;
  400. }
  401. encrypt_buf = rt_malloc(decrypt_len);
  402. if (encrypt_buf == RT_NULL)
  403. {
  404. fota_err = RT_FOTA_GENERAL_ERR;
  405. goto __exit_read_decrypt;
  406. }
  407. rt_memset(encrypt_buf, 0x0, decrypt_len);
  408. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, encrypt_buf, decrypt_len);
  409. if (fota_err <= 0 || fota_err % 16 != 0)
  410. {
  411. fota_err = RT_FOTA_PART_READ_ERR;
  412. goto __exit_read_decrypt;
  413. }
  414. tiny_aes_crypt_cbc(aes_ctx, AES_DECRYPT, fota_err, aes_iv, encrypt_buf, decrypt_buf);
  415. __exit_read_decrypt:
  416. if (encrypt_buf)
  417. rt_free(encrypt_buf);
  418. return fota_err;
  419. }
  420. int rt_fota_upgrade(const char *part_name)
  421. {
  422. int fota_err = RT_FOTA_NO_ERR;
  423. const struct fal_partition *part;
  424. rt_fota_part_head_t part_head = RT_NULL;
  425. tiny_aes_context *aes_ctx = RT_NULL;
  426. rt_uint8_t *aes_iv = RT_NULL;
  427. rt_uint8_t *crypt_buf = RT_NULL;
  428. int fw_raw_pos = 0;
  429. int fw_raw_len = 0;
  430. rt_uint32_t total_copy_size = 0;
  431. rt_uint8_t block_hdr_buf[RT_FOTA_BLOCK_HEADER_SIZE];
  432. rt_uint32_t block_hdr_pos = RT_FOTA_ALGO_BUFF_SIZE;
  433. rt_uint32_t block_size = 0;
  434. rt_uint32_t dcprs_size = 0;
  435. qlz_state_decompress *dcprs_state = RT_NULL;
  436. rt_uint8_t *cmprs_buff = RT_NULL;
  437. rt_uint8_t *dcprs_buff = RT_NULL;
  438. rt_uint32_t padding_size = 0;
  439. if (part_name == RT_NULL)
  440. {
  441. LOG_D("Invaild paramenter input!");
  442. fota_err = RT_FOTA_GENERAL_ERR;
  443. goto __exit_upgrade;
  444. }
  445. part = fal_partition_find(part_name);
  446. if (part == RT_NULL)
  447. {
  448. LOG_D("Upgrade partition[%s] not found.", part_name);
  449. fota_err = RT_FOTA_GENERAL_ERR;
  450. goto __exit_upgrade;
  451. }
  452. /* Application partition erase */
  453. fota_err = rt_fota_erase_app_part();
  454. if (fota_err != RT_FOTA_NO_ERR)
  455. {
  456. goto __exit_upgrade;
  457. }
  458. /* rt_fota_erase_app_part() has check fota_part_head vaild already */
  459. part_head = &fota_part_head;
  460. crypt_buf = rt_malloc(RT_FOTA_ALGO_BUFF_SIZE);
  461. if (crypt_buf == RT_NULL)
  462. {
  463. LOG_D("Not enough memory for firmware buffer.");
  464. fota_err = RT_FOTA_NO_MEM_ERR;
  465. goto __exit_upgrade;
  466. }
  467. /* AES256 algorithm enable */
  468. if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  469. {
  470. aes_ctx = rt_malloc(sizeof(tiny_aes_context));
  471. aes_iv = rt_malloc(rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
  472. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  473. {
  474. LOG_D("Not enough memory for firmware hash verify.");
  475. fota_err = RT_FOTA_NO_MEM_ERR;
  476. goto __exit_upgrade;
  477. }
  478. rt_memset(aes_iv, 0x0, rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
  479. rt_memcpy(aes_iv, RT_FOTA_ALGO_AES_IV, rt_strlen(RT_FOTA_ALGO_AES_IV));
  480. tiny_aes_setkey_dec(aes_ctx, (rt_uint8_t *)RT_FOTA_ALGO_AES_KEY, 256);
  481. }
  482. else if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  483. {
  484. LOG_I("Not surpport XOR.");
  485. fota_err = RT_FOTA_GENERAL_ERR;
  486. goto __exit_upgrade;
  487. }
  488. /* If enable fastlz compress function */
  489. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  490. {
  491. cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_FASTLZ_BUFFER_PADDING);
  492. dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
  493. if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL)
  494. {
  495. LOG_D("Not enough memory for firmware hash verify.");
  496. fota_err = RT_FOTA_NO_MEM_ERR;
  497. goto __exit_upgrade;
  498. }
  499. padding_size = RT_FOTA_FASTLZ_BUFFER_PADDING;
  500. }
  501. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  502. {
  503. cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_QUICKLZ_BUFFER_PADDING);
  504. dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
  505. dcprs_state = rt_malloc(sizeof(qlz_state_decompress));
  506. if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL || dcprs_state == RT_NULL)
  507. {
  508. LOG_D("Not enough memory for firmware hash verify.");
  509. fota_err = RT_FOTA_NO_MEM_ERR;
  510. goto __exit_upgrade;
  511. }
  512. padding_size = RT_FOTA_QUICKLZ_BUFFER_PADDING;
  513. rt_memset(dcprs_state, 0x0, sizeof(qlz_state_decompress));
  514. }
  515. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  516. {
  517. LOG_I("Not surpport GZIP.");
  518. fota_err = RT_FOTA_GENERAL_ERR;
  519. goto __exit_upgrade;
  520. }
  521. LOG_I("Start to copy firmware from %s to %s partition:", part->name, part_head->app_part_name);
  522. rt_kprintf("fw_raw_pos %X part_head->com_size %X\n", fw_raw_pos , part_head->com_size);
  523. while (fw_raw_pos < part_head->com_size)
  524. {
  525. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  526. {
  527. if (block_hdr_pos >= RT_FOTA_ALGO_BUFF_SIZE)
  528. {
  529. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  530. if (fw_raw_len < 0)
  531. {
  532. LOG_D("AES256 algorithm failed.");
  533. fota_err = RT_FOTA_PART_READ_ERR;
  534. goto __exit_upgrade;
  535. }
  536. fw_raw_pos += fw_raw_len;
  537. rt_memcpy(block_hdr_buf, crypt_buf, RT_FOTA_BLOCK_HEADER_SIZE);
  538. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  539. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  540. rt_memcpy(cmprs_buff, &crypt_buf[RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  541. block_hdr_pos = RT_FOTA_BLOCK_HEADER_SIZE + block_size;
  542. }
  543. else
  544. {
  545. rt_uint8_t hdr_tmp_pos = 0;
  546. while (block_hdr_pos < RT_FOTA_ALGO_BUFF_SIZE)
  547. {
  548. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  549. {
  550. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  551. }
  552. else
  553. {
  554. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  555. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  556. if (block_size > (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos))
  557. {
  558. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos));
  559. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  560. if (fw_raw_len < 0)
  561. {
  562. LOG_D("AES256 algorithm failed.");
  563. fota_err = RT_FOTA_PART_READ_ERR;
  564. goto __exit_upgrade;
  565. }
  566. fw_raw_pos += fw_raw_len;
  567. 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);
  568. block_hdr_pos = (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE;
  569. }
  570. else
  571. {
  572. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  573. block_hdr_pos = block_hdr_pos + block_size;
  574. }
  575. break;
  576. }
  577. }
  578. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  579. {
  580. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  581. if (fw_raw_len < 0)
  582. {
  583. LOG_D("AES256 algorithm failed.");
  584. fota_err = RT_FOTA_PART_READ_ERR;
  585. goto __exit_upgrade;
  586. }
  587. fw_raw_pos += fw_raw_len;
  588. block_hdr_pos = 0;
  589. while (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  590. {
  591. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  592. }
  593. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  594. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  595. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  596. block_hdr_pos = (block_hdr_pos + block_size) % RT_FOTA_ALGO_BUFF_SIZE;
  597. }
  598. }
  599. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  600. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  601. {
  602. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  603. }
  604. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  605. {
  606. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  607. }
  608. if (dcprs_size <= 0)
  609. {
  610. LOG_D("Decompress failed: %d.", dcprs_size);
  611. fota_err = RT_FOTA_GENERAL_ERR;
  612. goto __exit_upgrade;
  613. }
  614. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  615. {
  616. fota_err = RT_FOTA_COPY_FAILED;
  617. goto __exit_upgrade;
  618. }
  619. total_copy_size += dcprs_size;
  620. rt_kprintf("#");
  621. }
  622. /* no compress option */
  623. else
  624. {
  625. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  626. if (fw_raw_len < 0)
  627. {
  628. LOG_D("AES256 algorithm failed.");
  629. fota_err = RT_FOTA_PART_READ_ERR;
  630. goto __exit_upgrade;
  631. }
  632. fw_raw_pos += fw_raw_len;
  633. //rt_kprintf("rt_fota_write_app_part addr > %X size %X\n" , total_copy_size + 0x32000 , fw_raw_len);
  634. if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
  635. {
  636. fota_err = RT_FOTA_COPY_FAILED;
  637. goto __exit_upgrade;
  638. }
  639. total_copy_size += fw_raw_len;
  640. rt_kprintf("#");
  641. }
  642. }
  643. /* it has compress option */
  644. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  645. {
  646. while (total_copy_size < part_head->raw_size)
  647. {
  648. if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
  649. {
  650. rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
  651. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  652. if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
  653. {
  654. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  655. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  656. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  657. block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
  658. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  659. {
  660. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  661. }
  662. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  663. {
  664. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  665. }
  666. if (dcprs_size <= 0)
  667. {
  668. LOG_D("Decompress failed: %d.", dcprs_size);
  669. fota_err = RT_FOTA_GENERAL_ERR;
  670. goto __exit_upgrade;
  671. }
  672. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  673. {
  674. fota_err = RT_FOTA_COPY_FAILED;
  675. goto __exit_upgrade;
  676. }
  677. total_copy_size += dcprs_size;
  678. rt_kprintf("#");
  679. }
  680. else
  681. {
  682. break;
  683. }
  684. }
  685. else
  686. {
  687. break;
  688. }
  689. }
  690. }
  691. rt_kprintf("\r\n");
  692. /* �п�������ֵ�����,��ΪAES��Ҫ���16�ֽ�����,�����Ľ��ܽ�ѹֵ�Ĵ������������Ǵ��ڵ���raw_size */
  693. /* �ȽϺõķ�������һ��У��,Ŀǰ��������HASH_CODE�㷨��֪�� */
  694. if (total_copy_size < part_head->raw_size)
  695. {
  696. LOG_D("Decompress check failed.");
  697. fota_err = RT_FOTA_GENERAL_ERR;
  698. }
  699. __exit_upgrade:
  700. if (aes_ctx)
  701. rt_free(aes_ctx);
  702. if (aes_iv)
  703. rt_free(aes_iv);
  704. if (crypt_buf)
  705. rt_free(crypt_buf);
  706. if (cmprs_buff)
  707. rt_free(cmprs_buff);
  708. if (dcprs_buff)
  709. rt_free(dcprs_buff);
  710. if (dcprs_state)
  711. rt_free(dcprs_state);
  712. if (fota_err == RT_FOTA_NO_ERR)
  713. {
  714. LOG_I("Upgrade success, total %d bytes.", total_copy_size);
  715. }
  716. return fota_err;
  717. }
  718. //int rt_fota_start_application(void)
  719. //{
  720. // int fota_res = RT_FOTA_NO_ERR;
  721. // const struct fal_partition *part;
  722. // rt_uint32_t app_addr;
  723. //
  724. // part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  725. // if (part == RT_NULL)
  726. // {
  727. // LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
  728. // fota_res = RT_FOTA_GENERAL_ERR;
  729. // goto __exit_start_application;
  730. // }
  731. //
  732. // app_addr = part->offset + 0x08000000;
  733. // //�ж��Ƿ�Ϊ0x08XXXXXX.
  734. // if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
  735. // {
  736. // LOG_I("Illegal Flash code.");
  737. // fota_res = RT_FOTA_GENERAL_ERR;
  738. // goto __exit_start_application;
  739. // }
  740. // // ���ջ����ַ�Ƿ�Ϸ�.
  741. // if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
  742. // {
  743. // LOG_I("Illegal Stack code.");
  744. // fota_res = RT_FOTA_GENERAL_ERR;
  745. // goto __exit_start_application;
  746. // }
  747. //
  748. // LOG_I("Implement application now. %X addr. 250424 NEW" , app_addr);
  749. //
  750. // __disable_irq();
  751. // HAL_DeInit();
  752. //
  753. // // 禁用所有NVIC中断并清除挂起位
  754. // for (IRQn_Type irq = WWDG_IRQn; irq <= DMA2_Channel4_5_IRQn; irq++) {
  755. // HAL_NVIC_DisableIRQ(irq);
  756. // HAL_NVIC_ClearPendingIRQ(irq);
  757. // }
  758. //
  759. // HAL_RCC_DeInit();
  760. // SysTick->CTRL = 0;
  761. // SysTick->LOAD = 0;
  762. // SysTick->VAL = 0;
  763. //
  764. // // 确保使用MSP并处于特权模式
  765. // __set_CONTROL(0);
  766. // __ISB();
  767. //
  768. //
  769. // //用户代码区第二个字为程序开始地址(复位地址)
  770. // app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
  771. // /* Configure main stack */
  772. // __set_MSP(*(__IO uint32_t *)app_addr);
  773. //
  774. // __set_CONTROL(0);
  775. //
  776. // /* jump to application */
  777. // app_func();
  778. // rt_kprintf("Implement application fail.");
  779. //__exit_start_application:
  780. // LOG_I("Implement application failed.");
  781. // HMI_WRITE("Implement application failed.");
  782. // return fota_res;
  783. //}
  784. /*** 250430 测试此前的BL失效原因的进度 **/
  785. //int rt_fota_start_application(void)
  786. //{
  787. // int fota_res = RT_FOTA_NO_ERR;
  788. // rt_uint32_t app_addr = 0x08032000;
  789. // typedef void (*pFunction)(void);
  790. // pFunction JumpToApplication;
  791. //
  792. // // 1. 验证栈指针
  793. // uint32_t stackPointer = *(__IO uint32_t *)app_addr;
  794. // if (stackPointer < 0x20000000 || stackPointer > (0x20000000 + 0x5000)) {
  795. // LOG_I("Invalid Stack Pointer: 0x%08X", stackPointer);
  796. // fota_res = RT_FOTA_GENERAL_ERR;
  797. // goto __exit;
  798. // }
  799. //
  800. // // 2. 验证复位地址
  801. // uint32_t resetHandler = *(__IO uint32_t *)(app_addr + 4);
  802. // if ((resetHandler & 0xFF000000) != 0x08000000) {
  803. // LOG_I("Invalid Reset Handler: 0x%08X", resetHandler);
  804. // fota_res = RT_FOTA_GENERAL_ERR;
  805. // goto __exit;
  806. // }
  807. //
  808. // LOG_I("Jumping to APP: MSP=0x%08X, Reset=0x%08X", stackPointer, resetHandler);
  809. //
  810. // LOG_I("BL V1.10 250430."); // 250430 修复升级完成不能正常跳转的问题
  811. //
  812. //
  813. // // 3. 关闭所有外设和中断
  814. // __disable_irq();
  815. // HAL_DeInit();
  816. //
  817. // // 4. 禁用所有中断
  818. //// for (int i = 0; i < 8; i++) {
  819. //// NVIC->ICER[i] = 0xFFFFFFFF; // 禁用中断
  820. //// NVIC->ICPR[i] = 0xFFFFFFFF; // 清除挂起位
  821. //// }
  822. //
  823. // // // 禁用所有NVIC中断并清除挂起位
  824. // for (IRQn_Type irq = WWDG_IRQn; irq <= DMA2_Channel4_5_IRQn; irq++) {
  825. // HAL_NVIC_DisableIRQ(irq);
  826. // HAL_NVIC_ClearPendingIRQ(irq);
  827. // }
  828. //
  829. // // 5. 复位时钟和SysTick
  830. // HAL_RCC_DeInit();
  831. // SysTick->CTRL = 0;
  832. // SysTick->LOAD = 0;
  833. // SysTick->VAL = 0;
  834. //
  835. // // 6. 设置向量表
  836. //// SCB->VTOR = app_addr;
  837. //// __DSB();
  838. //// __ISB();
  839. //
  840. // // 7. 准备跳转
  841. // JumpToApplication = (pFunction)(*(__IO uint32_t *)(app_addr + 4));
  842. // __set_MSP(stackPointer);
  843. // __set_CONTROL(0); // 确保使用MSP
  844. //
  845. // // 8. 最终屏障
  846. //// __ASM volatile("DSB");
  847. //// __ASM volatile("ISB");
  848. //
  849. // // 9. 跳转
  850. // JumpToApplication();
  851. //
  852. // // 如果执行到这里说明跳转失败
  853. // LOG_I("Jump to APP failed!");
  854. //
  855. //__exit:
  856. // LOG_I("APP start failed, system will reboot");
  857. // NVIC_SystemReset();
  858. // return fota_res;
  859. //}
  860. int rt_fota_start_application(void)
  861. {
  862. int fota_res = RT_FOTA_NO_ERR;
  863. rt_uint32_t app_addr = 0x08032000;
  864. typedef void (*pFunction)(void);
  865. pFunction JumpToApplication;
  866. // 1. 验证栈指针
  867. uint32_t stackPointer = *(__IO uint32_t *)app_addr;
  868. if (stackPointer < 0x20000000 || stackPointer > (0x20000000 + 0x5000)) {
  869. LOG_I("Invalid Stack Pointer: 0x%08X", stackPointer);
  870. fota_res = RT_FOTA_GENERAL_ERR;
  871. goto __exit;
  872. }
  873. // 2. 验证复位地址
  874. uint32_t resetHandler = *(__IO uint32_t *)(app_addr + 4);
  875. if ((resetHandler & 0xFF000000) != 0x08000000) {
  876. LOG_I("Invalid Reset Handler: 0x%08X", resetHandler);
  877. fota_res = RT_FOTA_GENERAL_ERR;
  878. goto __exit;
  879. }
  880. LOG_I("Jumping to APP: MSP=0x%08X, Reset=0x%08X", stackPointer, resetHandler);
  881. LOG_I("BL V1.10 250430."); // 250430 修复升级完成不能正常跳转的问题
  882. // 3. 关闭所有外设和中断
  883. __disable_irq();
  884. HAL_DeInit();
  885. // 4. 禁用所有中断
  886. for (int i = 0; i < 8; i++) {
  887. NVIC->ICER[i] = 0xFFFFFFFF; // 禁用中断
  888. NVIC->ICPR[i] = 0xFFFFFFFF; // 清除挂起位
  889. }
  890. // 5. 复位时钟和SysTick
  891. HAL_RCC_DeInit();
  892. SysTick->CTRL = 0;
  893. SysTick->LOAD = 0;
  894. SysTick->VAL = 0;
  895. // 6. 设置向量表
  896. SCB->VTOR = app_addr;
  897. __DSB();
  898. __ISB();
  899. // 7. 准备跳转
  900. JumpToApplication = (pFunction)(*(__IO uint32_t *)(app_addr + 4));
  901. __set_MSP(stackPointer);
  902. __set_CONTROL(0); // 确保使用MSP
  903. // 8. 最终屏障
  904. __ASM volatile("DSB");
  905. __ASM volatile("ISB");
  906. // 9. 跳转
  907. JumpToApplication();
  908. // 如果执行到这里说明跳转失败
  909. LOG_I("Jump to APP failed!");
  910. __exit:
  911. LOG_I("APP start failed, system will reboot");
  912. NVIC_SystemReset();
  913. return fota_res;
  914. }
  915. static rt_err_t rt_fota_get_shell_key(void)
  916. {
  917. char ch;
  918. rt_err_t res = RT_EOK;
  919. rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
  920. rt_tick_t tick_start, tick_stop;
  921. RT_ASSERT(shell_dev != RT_NULL);
  922. RT_ASSERT(shell_sem != RT_NULL);
  923. rt_tick_set(0);
  924. tick_start = rt_tick_get();
  925. while (1)
  926. {
  927. if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
  928. {
  929. if (rt_sem_take(shell_sem, timeout) != RT_EOK)
  930. {
  931. res = RT_ERROR;
  932. goto __exit_get_shell_key;
  933. }
  934. }
  935. if (ch == 0x0d)
  936. goto __exit_get_shell_key;
  937. tick_stop = rt_tick_get();
  938. if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
  939. {
  940. res = RT_ERROR;
  941. goto __exit_get_shell_key;
  942. }
  943. timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
  944. }
  945. __exit_get_shell_key:
  946. if(stop_in_bootloader!=0)
  947. {
  948. res = RT_EOK;
  949. }
  950. return res;
  951. }
  952. static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
  953. {
  954. RT_ASSERT(shell_sem != RT_NULL);
  955. /* release semaphore to let fota thread rx data */
  956. rt_sem_release(shell_sem);
  957. return RT_EOK;
  958. }
  959. static rt_err_t rt_fota_set_device(const char *device_name)
  960. {
  961. rt_device_t dev;
  962. RT_ASSERT(device_name != RT_NULL);
  963. dev = rt_device_find(device_name);
  964. if (dev == RT_NULL)
  965. {
  966. LOG_D("Can not find device: %s.", device_name);
  967. return RT_ERROR;
  968. }
  969. if (shell_sem)
  970. rt_sem_delete(shell_sem);
  971. shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
  972. if (shell_sem == RT_NULL)
  973. return RT_ERROR;
  974. if (dev == shell_dev)
  975. return RT_EOK;
  976. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  977. {
  978. if (shell_dev != RT_NULL)
  979. {
  980. /* close old finsh device */
  981. rt_device_close(shell_dev);
  982. rt_device_set_rx_indicate(shell_dev, RT_NULL);
  983. }
  984. shell_dev = dev;
  985. rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
  986. LOG_D("Shell device %s open success.", device_name);
  987. return RT_EOK;
  988. }
  989. LOG_D("Shell device %s open failed.", device_name);
  990. return RT_ERROR;
  991. }
  992. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  993. static rt_err_t rt_fota_check_defalut_key(void)
  994. {
  995. int chk_idx;
  996. /* GPIO initialized */
  997. rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
  998. /* Delay for power up */
  999. rt_thread_mdelay(500);
  1000. if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
  1001. rt_kprintf("Default firmware key pressed:\n");
  1002. /* Check GPIO status */
  1003. for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
  1004. {
  1005. rt_thread_mdelay(RT_TICK_PER_SECOND);
  1006. rt_kprintf(">");
  1007. }
  1008. if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
  1009. return RT_ERROR;
  1010. else
  1011. return RT_EOK;
  1012. }
  1013. #endif
  1014. void rt_fota_thread_entry(void *arg)
  1015. {
  1016. int fota_err = RT_FOTA_NO_ERR;
  1017. extern int finsh_system_init(void);
  1018. /* Signal led initialized */
  1019. rt_fota_signal_led_init();
  1020. /* Partition initialized */
  1021. fota_err = rt_fota_boot_verify();
  1022. if (fota_err != RT_FOTA_NO_ERR)
  1023. {
  1024. LOG_I("Partition initialized failed.");
  1025. }
  1026. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  1027. /* Default key check */
  1028. if (rt_fota_check_defalut_key() == RT_EOK)
  1029. {
  1030. /* enter to defalut key mode */
  1031. rt_fota_signal_led_mode(led_upgrade_mode);
  1032. goto __exit_default_entry;
  1033. }
  1034. #endif
  1035. /* Shell initialized */
  1036. if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
  1037. {
  1038. if(easyflash_init()==EF_NO_ERR) //初始化成功
  1039. {
  1040. ef_get_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader), NULL); //读出reboot_time的值
  1041. rt_kprintf("stop_in_bootloader:%d\n",stop_in_bootloader);
  1042. }
  1043. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  1044. if (rt_fota_get_shell_key() == RT_EOK)
  1045. {
  1046. if(stop_in_bootloader!=0)
  1047. {
  1048. stop_in_bootloader = 0;
  1049. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  1050. }
  1051. goto __exit_shell_entry;
  1052. }
  1053. }
  1054. else
  1055. {
  1056. LOG_I("Shell device config failed.");
  1057. }
  1058. /* Firmware partition verify */
  1059. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  1060. if (fota_err != RT_FOTA_NO_ERR)
  1061. goto __exit_boot_entry;
  1062. /* Check upgrade status */
  1063. if (rt_fota_check_upgrade() <= 0)
  1064. goto __exit_boot_entry;
  1065. /* enter to upgrade mode */
  1066. rt_fota_signal_led_mode(led_upgrade_mode);
  1067. /* Implement upgrade, copy firmware partition to app partition */
  1068. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  1069. if (fota_err != RT_FOTA_NO_ERR)
  1070. goto __exit_boot_entry;
  1071. /* Update new application verison in RBL file of firmware partition */
  1072. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  1073. if (fota_err != RT_FOTA_NO_ERR)
  1074. goto __exit_boot_entry;
  1075. __exit_boot_entry:
  1076. /* Implement application */
  1077. rt_fota_start_application();
  1078. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  1079. __exit_default_entry:
  1080. #endif
  1081. /* Implement upgrade, copy default partition to app partition */
  1082. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1083. {
  1084. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1085. {
  1086. rt_fota_start_application();
  1087. }
  1088. }
  1089. LOG_I("Boot application failed, entry shell mode.");
  1090. __exit_shell_entry:
  1091. /* enter to shell mode */
  1092. rt_fota_signal_led_mode(led_shell_mode);
  1093. /* Implement shell */
  1094. finsh_system_init();
  1095. }
  1096. void rt_fota_init(void)
  1097. {
  1098. rt_thread_t tid;
  1099. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  1100. if (tid != RT_NULL)
  1101. {
  1102. rt_thread_startup(tid);
  1103. }
  1104. else
  1105. {
  1106. LOG_I("rt-fota thread create failed.");
  1107. }
  1108. }
  1109. void rt_fota_info(rt_uint8_t argc, char **argv)
  1110. {
  1111. char put_buf[24];
  1112. char part_name[2][FAL_DEV_NAME_MAX] =
  1113. {
  1114. {RT_FOTA_FM_PART_NAME},
  1115. {RT_FOTA_DF_PART_NAME}
  1116. };
  1117. const char* help_info[] =
  1118. {
  1119. [0] = "fota probe - probe RBL file of partiton",
  1120. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  1121. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  1122. [3] = "fota exec - execute application program",
  1123. };
  1124. if (argc < 2)
  1125. {
  1126. rt_kprintf("Usage:\n");
  1127. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1128. {
  1129. rt_kprintf("%s\n", help_info[i]);
  1130. }
  1131. rt_kprintf("\n");
  1132. }
  1133. else
  1134. {
  1135. const char *operator = argv[1];
  1136. if (!rt_strcmp(operator, "probe"))
  1137. {
  1138. for (int i = 0; i < 2; i++)
  1139. {
  1140. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  1141. {
  1142. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  1143. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  1144. rt_memset(put_buf, 0x0, sizeof(put_buf));
  1145. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  1146. {
  1147. rt_strncpy(put_buf, " AES", 4);
  1148. }
  1149. else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  1150. {
  1151. rt_strncpy(put_buf, " XOR", 4);
  1152. }
  1153. else
  1154. {
  1155. rt_strncpy(put_buf, "NONE", 4);
  1156. }
  1157. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  1158. {
  1159. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  1160. }
  1161. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  1162. {
  1163. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  1164. }
  1165. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  1166. {
  1167. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  1168. }
  1169. if (rt_strlen(put_buf) <= 0)
  1170. {
  1171. rt_strncpy(put_buf, "None", 4);
  1172. }
  1173. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  1174. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  1175. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  1176. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  1177. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1178. }
  1179. }
  1180. }
  1181. else if (!rt_strcmp(operator, "show"))
  1182. {
  1183. const struct fal_partition *part;
  1184. const char *part_name = argv[2];
  1185. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  1186. rt_uint32_t size = strtol(argv[4], NULL, 0);
  1187. rt_uint8_t buf[16];
  1188. part = fal_partition_find(part_name);
  1189. if (part != RT_NULL)
  1190. {
  1191. while (size > 16)
  1192. {
  1193. fal_partition_read(part, addr, buf, 16);
  1194. rt_kprintf("%08X: ", addr);
  1195. for (int i = 0; i < 16; i++)
  1196. {
  1197. rt_kprintf("%02X ", buf[i]);
  1198. }
  1199. rt_kprintf("\n");
  1200. size -= 16;
  1201. addr += 16;
  1202. }
  1203. fal_partition_read(part, addr, buf, size);
  1204. rt_kprintf("%08X: ", addr);
  1205. for (int i = 0; i < size; i++)
  1206. {
  1207. rt_kprintf("%02X ", buf[i]);
  1208. }
  1209. rt_kprintf("\n");
  1210. }
  1211. else
  1212. {
  1213. rt_kprintf("%s partition is not exist!\n", part_name);
  1214. }
  1215. }
  1216. else if (!rt_strcmp(operator, "clone"))
  1217. {
  1218. const char *dst_part_name = argv[2];
  1219. const char *src_part_name = argv[3];
  1220. const struct fal_partition *dst_part;
  1221. const struct fal_partition *src_part;
  1222. dst_part = fal_partition_find(dst_part_name);
  1223. src_part = fal_partition_find(src_part_name);
  1224. if (dst_part == RT_NULL || src_part == RT_NULL)
  1225. {
  1226. if (dst_part == RT_NULL)
  1227. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1228. if (src_part == RT_NULL)
  1229. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1230. }
  1231. else
  1232. {
  1233. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1234. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1235. {
  1236. int clone_pos = 0;
  1237. int clone_len = 0, clone_tol_len;
  1238. rt_uint8_t *buf = rt_malloc(4096);
  1239. if (dst_part->len < src_part->len)
  1240. clone_tol_len = dst_part->len;
  1241. else
  1242. clone_tol_len = src_part->len;
  1243. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1244. {
  1245. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1246. if (clone_len < 0)
  1247. {
  1248. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1249. break;
  1250. }
  1251. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1252. {
  1253. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1254. break;
  1255. }
  1256. rt_kprintf("#");
  1257. clone_pos += clone_len;
  1258. }
  1259. if (clone_pos >= clone_tol_len)
  1260. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1261. else
  1262. rt_kprintf("\nClone partition failed!\n");
  1263. if (buf)
  1264. rt_free(buf);
  1265. }
  1266. }
  1267. }
  1268. else if (!rt_strcmp(operator, "exec"))
  1269. {
  1270. rt_fota_start_application();
  1271. }
  1272. else
  1273. {
  1274. rt_kprintf("Usage:\n");
  1275. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1276. {
  1277. rt_kprintf("%s\n", help_info[i]);
  1278. }
  1279. rt_kprintf("\n");
  1280. }
  1281. }
  1282. }
  1283. /**
  1284. * rt-fota />ymodem_ota
  1285. */
  1286. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);