rt_fota.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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. /* For shell */
  93. static rt_sem_t shell_sem = RT_NULL;
  94. static rt_device_t shell_dev = RT_NULL;
  95. typedef struct {
  96. char type[4];
  97. rt_uint16_t fota_algo;
  98. rt_uint8_t fm_time[6];
  99. char app_part_name[16];
  100. char download_version[24];
  101. char current_version[24];
  102. rt_uint32_t code_crc;
  103. rt_uint32_t hash_val;
  104. rt_uint32_t raw_size;
  105. rt_uint32_t com_size;
  106. rt_uint32_t head_crc;
  107. } rt_fota_part_head, *rt_fota_part_head_t;
  108. typedef void (*rt_fota_app_func)(void);
  109. static rt_fota_app_func app_func = RT_NULL;
  110. static rt_fota_part_head fota_part_head;
  111. static void rt_fota_signal_led_on(void)
  112. {
  113. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_LOW);
  114. }
  115. static void rt_fota_signal_led_off(void)
  116. {
  117. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_HIGH);
  118. }
  119. static void rt_fota_signal_led_entry(void *arg)
  120. {
  121. while(1)
  122. {
  123. led_ticks();
  124. rt_thread_mdelay(LED_TICK_TIME);
  125. }
  126. }
  127. static void rt_fota_signal_led_init(void)
  128. {
  129. rt_pin_mode(RT_FOTA_SIGNAL_LED_PIN, PIN_MODE_OUTPUT);
  130. signal_led_mem_op.malloc_fn = (void* (*)(size_t))rt_malloc;
  131. signal_led_mem_op.free_fn = rt_free;
  132. led_set_mem_operation(&signal_led_mem_op);
  133. signal_led = led_create(rt_fota_signal_led_on, rt_fota_signal_led_off);
  134. /* Config signal led mode */
  135. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_on_mode);
  136. led_set_blink_over_callback(signal_led, RT_NULL);
  137. led_start(signal_led);
  138. rt_thread_t tid;
  139. 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);
  140. if (tid)
  141. rt_thread_startup(tid);
  142. }
  143. static void rt_fota_signal_led_mode(const char *led_cfg)
  144. {
  145. RT_ASSERT(led_cfg != RT_NULL);
  146. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_cfg);
  147. }
  148. static int rt_fota_boot_verify(void)
  149. {
  150. int fota_res = RT_FOTA_NO_ERR;
  151. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  152. /* partition initial */
  153. fal_init();
  154. extern int fal_init_check(void);
  155. /* verify partition */
  156. if (fal_init_check() != 1)
  157. {
  158. LOG_D("Partition initialized failed!");
  159. fota_res = RT_FOTA_GENERAL_ERR;
  160. goto __exit_boot_verify;
  161. }
  162. __exit_boot_verify:
  163. return fota_res;
  164. }
  165. int rt_fota_part_fw_verify(const char *part_name)
  166. {
  167. #define RT_FOTA_CRC_BUFF_SIZE 4096
  168. #define RT_FOTA_CRC_INIT_VAL 0xffffffff
  169. int fota_res = RT_FOTA_NO_ERR;
  170. const struct fal_partition *part;
  171. rt_fota_part_head part_head;
  172. rt_uint8_t *body_buf = RT_NULL;
  173. rt_uint32_t body_crc = RT_FOTA_CRC_INIT_VAL;
  174. rt_uint32_t hdr_crc;
  175. if (part_name == RT_NULL)
  176. {
  177. LOG_D("Invaild paramenter input!");
  178. fota_res = RT_FOTA_GENERAL_ERR;
  179. goto __exit_partition_verify;
  180. }
  181. part = fal_partition_find(part_name);
  182. if (part == RT_NULL)
  183. {
  184. LOG_D("Partition[%s] not found.", part_name);
  185. fota_res = RT_FOTA_GENERAL_ERR;
  186. goto __exit_partition_verify;
  187. }
  188. /* read the head of RBL files */
  189. if (fal_partition_read(part, 0, (rt_uint8_t *)&part_head, sizeof(rt_fota_part_head)) < 0)
  190. {
  191. LOG_D("Partition[%s] read error!", part->name);
  192. fota_res = RT_FOTA_PART_READ_ERR;
  193. goto __exit_partition_verify;
  194. }
  195. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  196. hdr_crc = rt_fota_crc((rt_uint8_t *)&part_head, sizeof(rt_fota_part_head) - 4);
  197. if (hdr_crc != part_head.head_crc)
  198. {
  199. LOG_D("Partition[%s] head CRC32 error!", part->name);
  200. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  201. goto __exit_partition_verify;
  202. }
  203. if (rt_strcmp(part_head.type, "RBL") != 0)
  204. {
  205. LOG_D("Partition[%s] type[%s] not surport.", part->name, part_head.type);
  206. fota_res = RT_FOTA_CHECK_FAILED;
  207. goto __exit_partition_verify;
  208. }
  209. if (fal_partition_find(part_head.app_part_name) == RT_NULL)
  210. {
  211. LOG_D("Partition[%s] not found.", part_head.app_part_name);
  212. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  213. goto __exit_partition_verify;
  214. }
  215. body_buf = rt_malloc(RT_FOTA_CRC_BUFF_SIZE);
  216. if (body_buf == RT_NULL)
  217. {
  218. LOG_D("Not enough memory for body CRC32 verify.");
  219. fota_res = RT_FOTA_NO_MEM_ERR;
  220. goto __exit_partition_verify;
  221. }
  222. for (int body_pos = 0; body_pos < part_head.com_size;)
  223. {
  224. int body_read_len = fal_partition_read(part, sizeof(rt_fota_part_head) + body_pos, body_buf, RT_FOTA_CRC_BUFF_SIZE);
  225. if (body_read_len > 0)
  226. {
  227. if ((body_pos + body_read_len) > part_head.com_size)
  228. {
  229. body_read_len = part_head.com_size - body_pos;
  230. }
  231. extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
  232. body_crc = rt_fota_step_crc(body_crc, body_buf, body_read_len);
  233. body_pos = body_pos + body_read_len;
  234. }
  235. else
  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. }
  242. body_crc = body_crc ^ RT_FOTA_CRC_INIT_VAL;
  243. if (body_crc != part_head.code_crc)
  244. {
  245. LOG_D("Partition[%s] firmware integrity verify failed.", part->name);
  246. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  247. goto __exit_partition_verify;
  248. }
  249. __exit_partition_verify:
  250. if (fota_res == RT_FOTA_NO_ERR)
  251. {
  252. rt_enter_critical();
  253. rt_memcpy(&fota_part_head, &part_head, sizeof(rt_fota_part_head));
  254. rt_exit_critical();
  255. LOG_D("partition[%s] verify success!", part->name);
  256. }
  257. else
  258. {
  259. rt_enter_critical();
  260. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  261. rt_exit_critical();
  262. LOG_D("Partition[%s] verify failed!", part->name);
  263. }
  264. if (body_buf)
  265. rt_free(body_buf);
  266. return fota_res;
  267. }
  268. int rt_fota_check_upgrade(void)
  269. {
  270. int is_upgrade = 0;
  271. if (rt_strcmp(fota_part_head.download_version, fota_part_head.current_version) != 0)
  272. {
  273. is_upgrade = 1;
  274. LOG_D("Application need upgrade.");
  275. goto __exit_check_upgrade;
  276. }
  277. __exit_check_upgrade:
  278. return is_upgrade;
  279. }
  280. int rt_fota_copy_version(const char *part_name)
  281. {
  282. #define THE_NOR_FLASH_GRANULARITY 4096
  283. int fota_res = RT_FOTA_NO_ERR;
  284. const struct fal_partition *part;
  285. rt_fota_part_head_t part_head = RT_NULL;
  286. rt_uint8_t *cache_buf = RT_NULL;
  287. part = fal_partition_find(part_name);
  288. if (part == RT_NULL)
  289. {
  290. LOG_D("Find partition[%s] not found.", part_name);
  291. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  292. goto __exit_copy_version;
  293. }
  294. cache_buf = rt_malloc(THE_NOR_FLASH_GRANULARITY);
  295. if (cache_buf == RT_NULL)
  296. {
  297. LOG_D("Not enough memory for head erase.");
  298. fota_res = RT_FOTA_NO_MEM_ERR;
  299. goto __exit_copy_version;
  300. }
  301. part_head = (rt_fota_part_head_t)cache_buf;
  302. if (fal_partition_read(part, 0, cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  303. {
  304. LOG_I("Read partition[%s] failed.", part_name);
  305. fota_res = RT_FOTA_PART_READ_ERR;
  306. goto __exit_copy_version;
  307. }
  308. rt_memcpy(part_head->current_version, part_head->download_version, sizeof(part_head->current_version));
  309. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  310. part_head->head_crc = rt_fota_crc((rt_uint8_t *)part_head, sizeof(rt_fota_part_head) - 4);
  311. if (fal_partition_erase(part, 0, THE_NOR_FLASH_GRANULARITY) < 0)
  312. {
  313. LOG_D("Erase partition[%s] failed.", part_name);
  314. fota_res = RT_FOTA_PART_ERASE_ERR;
  315. goto __exit_copy_version;
  316. }
  317. if (fal_partition_write(part, 0, (const rt_uint8_t *)cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  318. {
  319. LOG_I("Write partition[%s] failed.", part_name);
  320. fota_res = RT_FOTA_PART_WRITE_ERR;
  321. goto __exit_copy_version;
  322. }
  323. __exit_copy_version:
  324. if (cache_buf)
  325. rt_free(cache_buf);
  326. if (fota_res != RT_FOTA_NO_ERR)
  327. LOG_I("Copy firmware version failed!");
  328. else
  329. LOG_I("Copy firmware version Success!");
  330. return fota_res;
  331. }
  332. int rt_fota_erase_app_part(void)
  333. {
  334. int fota_res = RT_FOTA_NO_ERR;
  335. const struct fal_partition *part;
  336. part = fal_partition_find(fota_part_head.app_part_name);
  337. if (part == RT_NULL)
  338. {
  339. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  340. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  341. goto __exit_partition_erase;
  342. }
  343. LOG_I("Partition[%s] erase start:", part->name);
  344. if (fal_partition_erase(part, 0, fota_part_head.raw_size) < 0)
  345. {
  346. LOG_D("Partition[%s] erase failed!", part->name);
  347. fota_res = RT_FOTA_PART_ERASE_ERR;
  348. goto __exit_partition_erase;
  349. }
  350. __exit_partition_erase:
  351. if (fota_res == RT_FOTA_NO_ERR)
  352. {
  353. LOG_D("Partition[%s] erase %d bytes success!", part->name, fota_part_head.raw_size);
  354. }
  355. return fota_res;
  356. }
  357. int rt_fota_write_app_part(int fw_pos, rt_uint8_t *fw_buf, int fw_len)
  358. {
  359. int rt_fota_res = RT_FOTA_NO_ERR;
  360. const struct fal_partition *part;
  361. part = fal_partition_find(fota_part_head.app_part_name);
  362. if (part == RT_NULL)
  363. {
  364. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  365. rt_fota_res = RT_FOTA_FW_VERIFY_FAILED;
  366. goto __partition_write_exit;
  367. }
  368. if (fal_partition_write(part, fw_pos, fw_buf, fw_len) < 0)
  369. {
  370. LOG_D("Partition[%s] write failed!", part->name);
  371. rt_fota_res = RT_FOTA_PART_WRITE_ERR;
  372. goto __partition_write_exit;
  373. }
  374. __partition_write_exit:
  375. if (rt_fota_res == RT_FOTA_NO_ERR)
  376. {
  377. LOG_D("Partition[%s] write %d bytes success!", part->name, fw_len);
  378. }
  379. return rt_fota_res;
  380. }
  381. 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)
  382. {
  383. int fota_err = RT_FOTA_NO_ERR;
  384. rt_uint8_t *encrypt_buf = RT_NULL;
  385. if ((part == RT_NULL) || (decrypt_buf == RT_NULL)
  386. || (decrypt_len % 16 != 0) || (decrypt_len > RT_FOTA_ALGO_BUFF_SIZE))
  387. {
  388. fota_err = RT_FOTA_GENERAL_ERR;
  389. goto __exit_read_decrypt;
  390. }
  391. rt_memset(decrypt_buf, 0x0, decrypt_len);
  392. /* Not use AES256 algorithm */
  393. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  394. {
  395. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, decrypt_buf, decrypt_len);
  396. if (fota_err <= 0)
  397. {
  398. fota_err = RT_FOTA_PART_READ_ERR;
  399. }
  400. goto __exit_read_decrypt;
  401. }
  402. encrypt_buf = rt_malloc(decrypt_len);
  403. if (encrypt_buf == RT_NULL)
  404. {
  405. fota_err = RT_FOTA_GENERAL_ERR;
  406. goto __exit_read_decrypt;
  407. }
  408. rt_memset(encrypt_buf, 0x0, decrypt_len);
  409. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, encrypt_buf, decrypt_len);
  410. if (fota_err <= 0 || fota_err % 16 != 0)
  411. {
  412. fota_err = RT_FOTA_PART_READ_ERR;
  413. goto __exit_read_decrypt;
  414. }
  415. tiny_aes_crypt_cbc(aes_ctx, AES_DECRYPT, fota_err, aes_iv, encrypt_buf, decrypt_buf);
  416. __exit_read_decrypt:
  417. if (encrypt_buf)
  418. rt_free(encrypt_buf);
  419. return fota_err;
  420. }
  421. int rt_fota_upgrade(const char *part_name)
  422. {
  423. int fota_err = RT_FOTA_NO_ERR;
  424. const struct fal_partition *part;
  425. rt_fota_part_head_t part_head = RT_NULL;
  426. tiny_aes_context *aes_ctx = RT_NULL;
  427. rt_uint8_t *aes_iv = RT_NULL;
  428. rt_uint8_t *crypt_buf = RT_NULL;
  429. int fw_raw_pos = 0;
  430. int fw_raw_len = 0;
  431. rt_uint32_t total_copy_size = 0;
  432. rt_uint8_t block_hdr_buf[RT_FOTA_BLOCK_HEADER_SIZE];
  433. rt_uint32_t block_hdr_pos = RT_FOTA_ALGO_BUFF_SIZE;
  434. rt_uint32_t block_size = 0;
  435. rt_uint32_t dcprs_size = 0;
  436. qlz_state_decompress *dcprs_state = RT_NULL;
  437. rt_uint8_t *cmprs_buff = RT_NULL;
  438. rt_uint8_t *dcprs_buff = RT_NULL;
  439. rt_uint32_t padding_size = 0;
  440. if (part_name == RT_NULL)
  441. {
  442. LOG_D("Invaild paramenter input!");
  443. fota_err = RT_FOTA_GENERAL_ERR;
  444. goto __exit_upgrade;
  445. }
  446. part = fal_partition_find(part_name);
  447. if (part == RT_NULL)
  448. {
  449. LOG_D("Upgrade partition[%s] not found.", part_name);
  450. fota_err = RT_FOTA_GENERAL_ERR;
  451. goto __exit_upgrade;
  452. }
  453. /* Application partition erase */
  454. fota_err = rt_fota_erase_app_part();
  455. if (fota_err != RT_FOTA_NO_ERR)
  456. {
  457. goto __exit_upgrade;
  458. }
  459. /* rt_fota_erase_app_part() has check fota_part_head vaild already */
  460. part_head = &fota_part_head;
  461. crypt_buf = rt_malloc(RT_FOTA_ALGO_BUFF_SIZE);
  462. if (crypt_buf == RT_NULL)
  463. {
  464. LOG_D("Not enough memory for firmware buffer.");
  465. fota_err = RT_FOTA_NO_MEM_ERR;
  466. goto __exit_upgrade;
  467. }
  468. /* AES256 algorithm enable */
  469. if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  470. {
  471. aes_ctx = rt_malloc(sizeof(tiny_aes_context));
  472. aes_iv = rt_malloc(rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
  473. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  474. {
  475. LOG_D("Not enough memory for firmware hash verify.");
  476. fota_err = RT_FOTA_NO_MEM_ERR;
  477. goto __exit_upgrade;
  478. }
  479. rt_memset(aes_iv, 0x0, rt_strlen(RT_FOTA_ALGO_AES_IV) + 1);
  480. rt_memcpy(aes_iv, RT_FOTA_ALGO_AES_IV, rt_strlen(RT_FOTA_ALGO_AES_IV));
  481. tiny_aes_setkey_dec(aes_ctx, (rt_uint8_t *)RT_FOTA_ALGO_AES_KEY, 256);
  482. }
  483. else if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  484. {
  485. LOG_I("Not surpport XOR.");
  486. fota_err = RT_FOTA_GENERAL_ERR;
  487. goto __exit_upgrade;
  488. }
  489. /* If enable fastlz compress function */
  490. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  491. {
  492. cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_FASTLZ_BUFFER_PADDING);
  493. dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
  494. if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL)
  495. {
  496. LOG_D("Not enough memory for firmware hash verify.");
  497. fota_err = RT_FOTA_NO_MEM_ERR;
  498. goto __exit_upgrade;
  499. }
  500. padding_size = RT_FOTA_FASTLZ_BUFFER_PADDING;
  501. }
  502. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  503. {
  504. cmprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE + RT_FOTA_QUICKLZ_BUFFER_PADDING);
  505. dcprs_buff = rt_malloc(RT_FOTA_CMPRS_BUFFER_SIZE);
  506. dcprs_state = rt_malloc(sizeof(qlz_state_decompress));
  507. if (cmprs_buff == RT_NULL || dcprs_buff == RT_NULL || dcprs_state == RT_NULL)
  508. {
  509. LOG_D("Not enough memory for firmware hash verify.");
  510. fota_err = RT_FOTA_NO_MEM_ERR;
  511. goto __exit_upgrade;
  512. }
  513. padding_size = RT_FOTA_QUICKLZ_BUFFER_PADDING;
  514. rt_memset(dcprs_state, 0x0, sizeof(qlz_state_decompress));
  515. }
  516. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  517. {
  518. LOG_I("Not surpport GZIP.");
  519. fota_err = RT_FOTA_GENERAL_ERR;
  520. goto __exit_upgrade;
  521. }
  522. LOG_I("Start to copy firmware from %s to %s partition:", part->name, part_head->app_part_name);
  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. /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
  540. if ((fw_raw_len < RT_FOTA_BLOCK_HEADER_SIZE) ||
  541. (block_size > (rt_uint32_t)(fw_raw_len - RT_FOTA_BLOCK_HEADER_SIZE)) ||
  542. (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size)))
  543. {
  544. LOG_D("Invalid compressed block size(%d).", block_size);
  545. fota_err = RT_FOTA_GENERAL_ERR;
  546. goto __exit_upgrade;
  547. }
  548. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  549. rt_memcpy(cmprs_buff, &crypt_buf[RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  550. block_hdr_pos = RT_FOTA_BLOCK_HEADER_SIZE + block_size;
  551. }
  552. else
  553. {
  554. rt_uint8_t hdr_tmp_pos = 0;
  555. while (block_hdr_pos < RT_FOTA_ALGO_BUFF_SIZE)
  556. {
  557. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  558. {
  559. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  560. }
  561. else
  562. {
  563. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  564. /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
  565. if (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size))
  566. {
  567. LOG_D("Invalid compressed block size(%d).", block_size);
  568. fota_err = RT_FOTA_GENERAL_ERR;
  569. goto __exit_upgrade;
  570. }
  571. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  572. if (block_size > (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos))
  573. {
  574. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos));
  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. /* 校验跨缓冲块的剩余长度不超过本次读取的字节数 */
  584. if (((block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE) > (rt_uint32_t)fw_raw_len)
  585. {
  586. LOG_D("Invalid compressed block size(%d).", block_size);
  587. fota_err = RT_FOTA_GENERAL_ERR;
  588. goto __exit_upgrade;
  589. }
  590. 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);
  591. block_hdr_pos = (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE;
  592. }
  593. else
  594. {
  595. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  596. block_hdr_pos = block_hdr_pos + block_size;
  597. }
  598. break;
  599. }
  600. }
  601. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  602. {
  603. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  604. if (fw_raw_len < 0)
  605. {
  606. LOG_D("AES256 algorithm failed.");
  607. fota_err = RT_FOTA_PART_READ_ERR;
  608. goto __exit_upgrade;
  609. }
  610. fw_raw_pos += fw_raw_len;
  611. block_hdr_pos = 0;
  612. while (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  613. {
  614. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  615. }
  616. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  617. /* 校验块长:防止损坏/伪造的升级包造成缓冲区越界 */
  618. if ((fw_raw_len < (int)block_hdr_pos) ||
  619. (block_size > (rt_uint32_t)(fw_raw_len - block_hdr_pos)) ||
  620. (block_size > (RT_FOTA_CMPRS_BUFFER_SIZE + padding_size)))
  621. {
  622. LOG_D("Invalid compressed block size(%d).", block_size);
  623. fota_err = RT_FOTA_GENERAL_ERR;
  624. goto __exit_upgrade;
  625. }
  626. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  627. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  628. block_hdr_pos = (block_hdr_pos + block_size) % RT_FOTA_ALGO_BUFF_SIZE;
  629. }
  630. }
  631. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  632. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  633. {
  634. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  635. }
  636. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  637. {
  638. /* 校验解压长度:qlz 解压长度由流头自报,必须在目标缓冲区容量内 */
  639. if (qlz_size_decompressed((const char *)&cmprs_buff[0]) > RT_FOTA_CMPRS_BUFFER_SIZE)
  640. {
  641. LOG_D("Invalid quicklz decompressed size in upgrade package.");
  642. fota_err = RT_FOTA_GENERAL_ERR;
  643. goto __exit_upgrade;
  644. }
  645. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  646. }
  647. if (dcprs_size <= 0)
  648. {
  649. LOG_D("Decompress failed: %d.", dcprs_size);
  650. fota_err = RT_FOTA_GENERAL_ERR;
  651. goto __exit_upgrade;
  652. }
  653. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  654. {
  655. fota_err = RT_FOTA_COPY_FAILED;
  656. goto __exit_upgrade;
  657. }
  658. total_copy_size += dcprs_size;
  659. rt_kprintf("#");
  660. }
  661. /* no compress option */
  662. else
  663. {
  664. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  665. if (fw_raw_len < 0)
  666. {
  667. LOG_D("AES256 algorithm failed.");
  668. fota_err = RT_FOTA_PART_READ_ERR;
  669. goto __exit_upgrade;
  670. }
  671. fw_raw_pos += fw_raw_len;
  672. if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
  673. {
  674. fota_err = RT_FOTA_COPY_FAILED;
  675. goto __exit_upgrade;
  676. }
  677. total_copy_size += fw_raw_len;
  678. rt_kprintf("#");
  679. }
  680. }
  681. /* it has compress option */
  682. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  683. {
  684. while (total_copy_size < part_head->raw_size)
  685. {
  686. if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
  687. {
  688. rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
  689. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  690. if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
  691. {
  692. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  693. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  694. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  695. block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
  696. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  697. {
  698. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  699. }
  700. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  701. {
  702. /* 校验解压长度:qlz 解压长度由流头自报,必须在目标缓冲区容量内 */
  703. if (qlz_size_decompressed((const char *)&cmprs_buff[0]) > RT_FOTA_CMPRS_BUFFER_SIZE)
  704. {
  705. LOG_D("Invalid quicklz decompressed size in upgrade package.");
  706. fota_err = RT_FOTA_GENERAL_ERR;
  707. goto __exit_upgrade;
  708. }
  709. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  710. }
  711. if (dcprs_size <= 0)
  712. {
  713. LOG_D("Decompress failed: %d.", dcprs_size);
  714. fota_err = RT_FOTA_GENERAL_ERR;
  715. goto __exit_upgrade;
  716. }
  717. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  718. {
  719. fota_err = RT_FOTA_COPY_FAILED;
  720. goto __exit_upgrade;
  721. }
  722. total_copy_size += dcprs_size;
  723. rt_kprintf("#");
  724. }
  725. else
  726. {
  727. break;
  728. }
  729. }
  730. else
  731. {
  732. break;
  733. }
  734. }
  735. }
  736. rt_kprintf("\r\n");
  737. /* �п�������ֵ�����,��ΪAES��Ҫ���16�ֽ�����,�����Ľ��ܽ�ѹֵ�Ĵ������������Ǵ��ڵ���raw_size */
  738. /* �ȽϺõķ�������һ��У��,Ŀǰ��������HASH_CODE�㷨��֪�� */
  739. if (total_copy_size < part_head->raw_size)
  740. {
  741. LOG_D("Decompress check failed.");
  742. fota_err = RT_FOTA_GENERAL_ERR;
  743. }
  744. __exit_upgrade:
  745. if (aes_ctx)
  746. rt_free(aes_ctx);
  747. if (aes_iv)
  748. rt_free(aes_iv);
  749. if (crypt_buf)
  750. rt_free(crypt_buf);
  751. if (cmprs_buff)
  752. rt_free(cmprs_buff);
  753. if (dcprs_buff)
  754. rt_free(dcprs_buff);
  755. if (dcprs_state)
  756. rt_free(dcprs_state);
  757. if (fota_err == RT_FOTA_NO_ERR)
  758. {
  759. LOG_I("Upgrade success, total %d bytes.", total_copy_size);
  760. }
  761. return fota_err;
  762. }
  763. int rt_fota_start_application(void)
  764. {
  765. int fota_res = RT_FOTA_NO_ERR;
  766. const struct fal_partition *part;
  767. rt_uint32_t app_addr;
  768. part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  769. if (part == RT_NULL)
  770. {
  771. LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
  772. fota_res = RT_FOTA_GENERAL_ERR;
  773. goto __exit_start_application;
  774. }
  775. app_addr = part->offset + 0x08000000;
  776. //�ж��Ƿ�Ϊ0x08XXXXXX.
  777. if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
  778. {
  779. LOG_I("Illegal Flash code.");
  780. fota_res = RT_FOTA_GENERAL_ERR;
  781. goto __exit_start_application;
  782. }
  783. // ���ջ����ַ�Ƿ�Ϸ�.
  784. if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
  785. {
  786. LOG_I("Illegal Stack code.");
  787. fota_res = RT_FOTA_GENERAL_ERR;
  788. goto __exit_start_application;
  789. }
  790. LOG_I("Implement application now.");
  791. __disable_irq();
  792. /* 禁用并清除全部 NVIC 中断(原循环因 F1 无 FPU_IRQn 被注释,改用寄存器遍历),
  793. * 防止 APP 启动初期残留/挂起中断使用旧向量表或未初始化外设 */
  794. for (int nvic_idx = 0; nvic_idx < 8; nvic_idx++)
  795. {
  796. NVIC->ICER[nvic_idx] = 0xFFFFFFFF;
  797. NVIC->ICPR[nvic_idx] = 0xFFFFFFFF;
  798. }
  799. /* 清除可能挂起的系统异常(PendSV/SysTick) */
  800. SCB->ICSR |= SCB_ICSR_PENDSVCLR_Msk;
  801. SCB->ICSR |= SCB_ICSR_PENDSTCLR_Msk;
  802. /* 停止 SysTick */
  803. SysTick->CTRL = 0;
  804. SysTick->LOAD = 0;
  805. SysTick->VAL = 0;
  806. /* 外设与时钟恢复到默认状态 */
  807. HAL_DeInit();
  808. HAL_RCC_DeInit();
  809. HAL_DeInit();
  810. /* 向量表预先指向 APP(其 SystemInit 会先写回 0x08000000,此步保证交接窗口安全) */
  811. SCB->VTOR = app_addr;
  812. __DSB();
  813. __ISB();
  814. //用户代码区第二个字为程序开始地址(复位地址)
  815. app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
  816. /* Configure main stack */
  817. __set_MSP(*(__IO uint32_t *)app_addr);
  818. /* 由线程(PSP)切回 MSP:APP 以复位状态(CONTROL=0)运行 */
  819. __set_CONTROL(0);
  820. /* 恢复中断屏蔽(PRIMASK=0),以接近复位后的状态进入 APP */
  821. __enable_irq();
  822. /* jump to application */
  823. app_func();
  824. __exit_start_application:
  825. LOG_I("Implement application failed.");
  826. HMI_WRITE("Implement application failed.");
  827. return fota_res;
  828. }
  829. static rt_err_t rt_fota_get_shell_key(void)
  830. {
  831. char ch;
  832. rt_err_t res = RT_EOK;
  833. rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
  834. rt_tick_t tick_start, tick_stop;
  835. RT_ASSERT(shell_dev != RT_NULL);
  836. RT_ASSERT(shell_sem != RT_NULL);
  837. rt_tick_set(0);
  838. tick_start = rt_tick_get();
  839. while (1)
  840. {
  841. if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
  842. {
  843. if (rt_sem_take(shell_sem, timeout) != RT_EOK)
  844. {
  845. res = RT_ERROR;
  846. goto __exit_get_shell_key;
  847. }
  848. }
  849. if (ch == 0x0d)
  850. goto __exit_get_shell_key;
  851. tick_stop = rt_tick_get();
  852. if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
  853. {
  854. res = RT_ERROR;
  855. goto __exit_get_shell_key;
  856. }
  857. timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
  858. }
  859. __exit_get_shell_key:
  860. if(stop_in_bootloader!=0)
  861. {
  862. res = RT_EOK;
  863. }
  864. return res;
  865. }
  866. static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
  867. {
  868. RT_ASSERT(shell_sem != RT_NULL);
  869. /* release semaphore to let fota thread rx data */
  870. rt_sem_release(shell_sem);
  871. return RT_EOK;
  872. }
  873. static rt_err_t rt_fota_set_device(const char *device_name)
  874. {
  875. rt_device_t dev;
  876. RT_ASSERT(device_name != RT_NULL);
  877. dev = rt_device_find(device_name);
  878. if (dev == RT_NULL)
  879. {
  880. LOG_D("Can not find device: %s.", device_name);
  881. return RT_ERROR;
  882. }
  883. if (shell_sem)
  884. rt_sem_delete(shell_sem);
  885. shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
  886. if (shell_sem == RT_NULL)
  887. return RT_ERROR;
  888. if (dev == shell_dev)
  889. return RT_EOK;
  890. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  891. {
  892. if (shell_dev != RT_NULL)
  893. {
  894. /* close old finsh device */
  895. rt_device_close(shell_dev);
  896. rt_device_set_rx_indicate(shell_dev, RT_NULL);
  897. }
  898. shell_dev = dev;
  899. rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
  900. LOG_D("Shell device %s open success.", device_name);
  901. return RT_EOK;
  902. }
  903. LOG_D("Shell device %s open failed.", device_name);
  904. return RT_ERROR;
  905. }
  906. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  907. static rt_err_t rt_fota_check_defalut_key(void)
  908. {
  909. int chk_idx;
  910. /* GPIO initialized */
  911. rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
  912. /* Delay for power up */
  913. rt_thread_mdelay(500);
  914. if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
  915. rt_kprintf("Default firmware key pressed:\n");
  916. /* Check GPIO status */
  917. for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
  918. {
  919. rt_thread_mdelay(RT_TICK_PER_SECOND);
  920. rt_kprintf(">");
  921. }
  922. if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
  923. return RT_ERROR;
  924. else
  925. return RT_EOK;
  926. }
  927. #endif
  928. /* 应用侧(JSONRPC "update" 指令)将 RTC 备份寄存器 DR2 写入 0x5AA5 并重启,
  929. * bootloader 检测到该值后停留在 bootloader 并进入 ymodem 升级模式 */
  930. #define RT_FOTA_YMODEM_REQ_BKP_MAGIC 0x5AA5
  931. /* 检测应用侧写入的 RTC_BKP_DR2 升级请求:
  932. * BKP_BASE + 4*2 即 RTC_BKP_DR2(16 位),访问方式与 Application_CBD 的
  933. * main.c / dtu_uart2.c 配对;命中约定值时清除标志并返回 RT_EOK(请求 ymodem 升级) */
  934. static rt_err_t rt_fota_check_ymodem_request(void)
  935. {
  936. rt_uint16_t boot_flag;
  937. /* 使能 PWR/BKP 时钟并解除备份域写访问保护(DBP) */
  938. __HAL_RCC_PWR_CLK_ENABLE();
  939. __HAL_RCC_BKP_CLK_ENABLE();
  940. HAL_PWR_EnableBkUpAccess();
  941. boot_flag = *(__IO uint16_t *)(BKP_BASE + 4 * 2);
  942. if (boot_flag != RT_FOTA_YMODEM_REQ_BKP_MAGIC)
  943. {
  944. return RT_ERROR;
  945. }
  946. /* 先清除标志:避免 ymodem 完成复位后再次进入升级模式(app 启动时也会清除) */
  947. *(__IO uint16_t *)(BKP_BASE + 4 * 2) = 0x0000;
  948. return RT_EOK;
  949. }
  950. void rt_fota_thread_entry(void *arg)
  951. {
  952. int fota_err = RT_FOTA_NO_ERR;
  953. extern int finsh_system_init(void);
  954. /* Signal led initialized */
  955. rt_fota_signal_led_init();
  956. /* Partition initialized */
  957. fota_err = rt_fota_boot_verify();
  958. if (fota_err != RT_FOTA_NO_ERR)
  959. {
  960. LOG_I("Partition initialized failed.");
  961. }
  962. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  963. /* Default key check */
  964. if (rt_fota_check_defalut_key() == RT_EOK)
  965. {
  966. /* enter to defalut key mode */
  967. rt_fota_signal_led_mode(led_upgrade_mode);
  968. goto __exit_default_entry;
  969. }
  970. #endif
  971. /* RTC 备份寄存器 DR2 升级请求检测:命中 0x5AA5 → 停留 bootloader 执行 ymodem 升级 */
  972. if (rt_fota_check_ymodem_request() == RT_EOK)
  973. {
  974. extern void ymodem_ota(uint8_t argc, char **argv);
  975. LOG_I("RTC_BKP_DR2 = 0x5AA5, stay in bootloader, start ymodem ota.");
  976. rt_fota_signal_led_mode(led_upgrade_mode);
  977. /* 下载分区默认 fm_area;ymodem 完成后内部复位,正常不返回 */
  978. ymodem_ota(1, RT_NULL);
  979. }
  980. /* Shell initialized */
  981. if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
  982. {
  983. if(easyflash_init()==EF_NO_ERR) //初始化成功
  984. {
  985. rt_uint8_t boot_flag_buf[4] = {0, 0, 0, 0};
  986. ef_get_env_blob("stop_in_bootloader", boot_flag_buf, sizeof(boot_flag_buf), NULL);
  987. /* 兼容三种历史存储格式:字符串 "0"/"1"(EasyFlash 默认值)与二进制 0/1。
  988. * 历史缺陷:默认值 "0" 按二进制读为 0x30(非零),会误判为停留引导模式 */
  989. if (boot_flag_buf[0] == '0')
  990. stop_in_bootloader = 0;
  991. else if (boot_flag_buf[0] == '1')
  992. stop_in_bootloader = 1;
  993. else
  994. stop_in_bootloader = (boot_flag_buf[0] != 0) ? 1 : 0;
  995. rt_kprintf("stop_in_bootloader:%d\n", stop_in_bootloader);
  996. }
  997. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  998. if (rt_fota_get_shell_key() == RT_EOK)
  999. {
  1000. if(stop_in_bootloader!=0)
  1001. {
  1002. stop_in_bootloader = 0;
  1003. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  1004. }
  1005. goto __exit_shell_entry;
  1006. }
  1007. }
  1008. else
  1009. {
  1010. LOG_I("Shell device config failed.");
  1011. }
  1012. /* Firmware partition verify */
  1013. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  1014. if (fota_err != RT_FOTA_NO_ERR)
  1015. goto __exit_boot_entry;
  1016. /* Check upgrade status */
  1017. if (rt_fota_check_upgrade() <= 0)
  1018. goto __exit_boot_entry;
  1019. /* enter to upgrade mode */
  1020. rt_fota_signal_led_mode(led_upgrade_mode);
  1021. /* Implement upgrade, copy firmware partition to app partition */
  1022. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  1023. if (fota_err != RT_FOTA_NO_ERR)
  1024. goto __exit_boot_entry;
  1025. /* Update new application verison in RBL file of firmware partition */
  1026. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  1027. if (fota_err != RT_FOTA_NO_ERR)
  1028. goto __exit_boot_entry;
  1029. __exit_boot_entry:
  1030. /* Implement application */
  1031. rt_fota_start_application();
  1032. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  1033. __exit_default_entry:
  1034. #endif
  1035. /* Implement upgrade, copy default partition to app partition */
  1036. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1037. {
  1038. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1039. {
  1040. rt_fota_start_application();
  1041. }
  1042. }
  1043. LOG_I("Boot application failed, entry shell mode.");
  1044. __exit_shell_entry:
  1045. /* enter to shell mode */
  1046. rt_fota_signal_led_mode(led_shell_mode);
  1047. /* Implement shell */
  1048. finsh_system_init();
  1049. }
  1050. void rt_fota_init(void)
  1051. {
  1052. rt_thread_t tid;
  1053. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  1054. if (tid != RT_NULL)
  1055. {
  1056. rt_thread_startup(tid);
  1057. }
  1058. else
  1059. {
  1060. LOG_I("rt-fota thread create failed.");
  1061. }
  1062. }
  1063. void rt_fota_info(rt_uint8_t argc, char **argv)
  1064. {
  1065. char put_buf[24];
  1066. char part_name[2][FAL_DEV_NAME_MAX] =
  1067. {
  1068. {RT_FOTA_FM_PART_NAME},
  1069. {RT_FOTA_DF_PART_NAME}
  1070. };
  1071. const char* help_info[] =
  1072. {
  1073. [0] = "fota probe - probe RBL file of partiton",
  1074. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  1075. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  1076. [3] = "fota exec - execute application program",
  1077. };
  1078. if (argc < 2)
  1079. {
  1080. rt_kprintf("Usage:\n");
  1081. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1082. {
  1083. rt_kprintf("%s\n", help_info[i]);
  1084. }
  1085. rt_kprintf("\n");
  1086. }
  1087. else
  1088. {
  1089. const char *operator = argv[1];
  1090. if (!rt_strcmp(operator, "probe"))
  1091. {
  1092. for (int i = 0; i < 2; i++)
  1093. {
  1094. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  1095. {
  1096. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  1097. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  1098. rt_memset(put_buf, 0x0, sizeof(put_buf));
  1099. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  1100. {
  1101. rt_strncpy(put_buf, " AES", 4);
  1102. }
  1103. else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  1104. {
  1105. rt_strncpy(put_buf, " XOR", 4);
  1106. }
  1107. else
  1108. {
  1109. rt_strncpy(put_buf, "NONE", 4);
  1110. }
  1111. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  1112. {
  1113. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  1114. }
  1115. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  1116. {
  1117. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  1118. }
  1119. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  1120. {
  1121. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  1122. }
  1123. if (rt_strlen(put_buf) <= 0)
  1124. {
  1125. rt_strncpy(put_buf, "None", 4);
  1126. }
  1127. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  1128. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  1129. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  1130. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  1131. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1132. }
  1133. }
  1134. }
  1135. else if (!rt_strcmp(operator, "show"))
  1136. {
  1137. const struct fal_partition *part;
  1138. const char *part_name = argv[2];
  1139. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  1140. rt_uint32_t size = strtol(argv[4], NULL, 0);
  1141. rt_uint8_t buf[16];
  1142. part = fal_partition_find(part_name);
  1143. if (part != RT_NULL)
  1144. {
  1145. while (size > 16)
  1146. {
  1147. fal_partition_read(part, addr, buf, 16);
  1148. rt_kprintf("%08X: ", addr);
  1149. for (int i = 0; i < 16; i++)
  1150. {
  1151. rt_kprintf("%02X ", buf[i]);
  1152. }
  1153. rt_kprintf("\n");
  1154. size -= 16;
  1155. addr += 16;
  1156. }
  1157. fal_partition_read(part, addr, buf, size);
  1158. rt_kprintf("%08X: ", addr);
  1159. for (int i = 0; i < size; i++)
  1160. {
  1161. rt_kprintf("%02X ", buf[i]);
  1162. }
  1163. rt_kprintf("\n");
  1164. }
  1165. else
  1166. {
  1167. rt_kprintf("%s partition is not exist!\n", part_name);
  1168. }
  1169. }
  1170. else if (!rt_strcmp(operator, "clone"))
  1171. {
  1172. const char *dst_part_name = argv[2];
  1173. const char *src_part_name = argv[3];
  1174. const struct fal_partition *dst_part;
  1175. const struct fal_partition *src_part;
  1176. dst_part = fal_partition_find(dst_part_name);
  1177. src_part = fal_partition_find(src_part_name);
  1178. if (dst_part == RT_NULL || src_part == RT_NULL)
  1179. {
  1180. if (dst_part == RT_NULL)
  1181. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1182. if (src_part == RT_NULL)
  1183. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1184. }
  1185. else
  1186. {
  1187. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1188. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1189. {
  1190. int clone_pos = 0;
  1191. int clone_len = 0, clone_tol_len;
  1192. rt_uint8_t *buf = rt_malloc(4096);
  1193. if (dst_part->len < src_part->len)
  1194. clone_tol_len = dst_part->len;
  1195. else
  1196. clone_tol_len = src_part->len;
  1197. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1198. {
  1199. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1200. if (clone_len < 0)
  1201. {
  1202. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1203. break;
  1204. }
  1205. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1206. {
  1207. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1208. break;
  1209. }
  1210. rt_kprintf("#");
  1211. clone_pos += clone_len;
  1212. }
  1213. if (clone_pos >= clone_tol_len)
  1214. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1215. else
  1216. rt_kprintf("\nClone partition failed!\n");
  1217. if (buf)
  1218. rt_free(buf);
  1219. }
  1220. }
  1221. }
  1222. else if (!rt_strcmp(operator, "exec"))
  1223. {
  1224. rt_fota_start_application();
  1225. }
  1226. else
  1227. {
  1228. rt_kprintf("Usage:\n");
  1229. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1230. {
  1231. rt_kprintf("%s\n", help_info[i]);
  1232. }
  1233. rt_kprintf("\n");
  1234. }
  1235. }
  1236. }
  1237. /**
  1238. * rt-fota />ymodem_ota
  1239. */
  1240. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);