rt_fota.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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 4096
  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. while (fw_raw_pos < part_head->com_size)
  523. {
  524. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  525. {
  526. if (block_hdr_pos >= RT_FOTA_ALGO_BUFF_SIZE)
  527. {
  528. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  529. if (fw_raw_len < 0)
  530. {
  531. LOG_D("AES256 algorithm failed.");
  532. fota_err = RT_FOTA_PART_READ_ERR;
  533. goto __exit_upgrade;
  534. }
  535. fw_raw_pos += fw_raw_len;
  536. rt_memcpy(block_hdr_buf, crypt_buf, RT_FOTA_BLOCK_HEADER_SIZE);
  537. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  538. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  539. rt_memcpy(cmprs_buff, &crypt_buf[RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  540. block_hdr_pos = RT_FOTA_BLOCK_HEADER_SIZE + block_size;
  541. }
  542. else
  543. {
  544. rt_uint8_t hdr_tmp_pos = 0;
  545. while (block_hdr_pos < RT_FOTA_ALGO_BUFF_SIZE)
  546. {
  547. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  548. {
  549. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  550. }
  551. else
  552. {
  553. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  554. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  555. if (block_size > (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos))
  556. {
  557. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos));
  558. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  559. if (fw_raw_len < 0)
  560. {
  561. LOG_D("AES256 algorithm failed.");
  562. fota_err = RT_FOTA_PART_READ_ERR;
  563. goto __exit_upgrade;
  564. }
  565. fw_raw_pos += fw_raw_len;
  566. 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);
  567. block_hdr_pos = (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE;
  568. }
  569. else
  570. {
  571. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  572. block_hdr_pos = block_hdr_pos + block_size;
  573. }
  574. break;
  575. }
  576. }
  577. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  578. {
  579. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  580. if (fw_raw_len < 0)
  581. {
  582. LOG_D("AES256 algorithm failed.");
  583. fota_err = RT_FOTA_PART_READ_ERR;
  584. goto __exit_upgrade;
  585. }
  586. fw_raw_pos += fw_raw_len;
  587. block_hdr_pos = 0;
  588. while (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  589. {
  590. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  591. }
  592. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  593. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  594. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  595. block_hdr_pos = (block_hdr_pos + block_size) % RT_FOTA_ALGO_BUFF_SIZE;
  596. }
  597. }
  598. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  599. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  600. {
  601. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  602. }
  603. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  604. {
  605. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  606. }
  607. if (dcprs_size <= 0)
  608. {
  609. LOG_D("Decompress failed: %d.", dcprs_size);
  610. fota_err = RT_FOTA_GENERAL_ERR;
  611. goto __exit_upgrade;
  612. }
  613. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  614. {
  615. fota_err = RT_FOTA_COPY_FAILED;
  616. goto __exit_upgrade;
  617. }
  618. total_copy_size += dcprs_size;
  619. rt_kprintf("#");
  620. }
  621. /* no compress option */
  622. else
  623. {
  624. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  625. if (fw_raw_len < 0)
  626. {
  627. LOG_D("AES256 algorithm failed.");
  628. fota_err = RT_FOTA_PART_READ_ERR;
  629. goto __exit_upgrade;
  630. }
  631. fw_raw_pos += fw_raw_len;
  632. if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
  633. {
  634. fota_err = RT_FOTA_COPY_FAILED;
  635. goto __exit_upgrade;
  636. }
  637. total_copy_size += fw_raw_len;
  638. rt_kprintf("#");
  639. }
  640. }
  641. /* it has compress option */
  642. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  643. {
  644. while (total_copy_size < part_head->raw_size)
  645. {
  646. if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
  647. {
  648. rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
  649. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  650. if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
  651. {
  652. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  653. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  654. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  655. block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
  656. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  657. {
  658. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  659. }
  660. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  661. {
  662. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  663. }
  664. if (dcprs_size <= 0)
  665. {
  666. LOG_D("Decompress failed: %d.", dcprs_size);
  667. fota_err = RT_FOTA_GENERAL_ERR;
  668. goto __exit_upgrade;
  669. }
  670. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  671. {
  672. fota_err = RT_FOTA_COPY_FAILED;
  673. goto __exit_upgrade;
  674. }
  675. total_copy_size += dcprs_size;
  676. rt_kprintf("#");
  677. }
  678. else
  679. {
  680. break;
  681. }
  682. }
  683. else
  684. {
  685. break;
  686. }
  687. }
  688. }
  689. rt_kprintf("\r\n");
  690. /* �п�������ֵ�����,��ΪAES��Ҫ���16�ֽ�����,�����Ľ��ܽ�ѹֵ�Ĵ������������Ǵ��ڵ���raw_size */
  691. /* �ȽϺõķ�������һ��У��,Ŀǰ��������HASH_CODE�㷨��֪�� */
  692. if (total_copy_size < part_head->raw_size)
  693. {
  694. LOG_D("Decompress check failed.");
  695. fota_err = RT_FOTA_GENERAL_ERR;
  696. }
  697. __exit_upgrade:
  698. if (aes_ctx)
  699. rt_free(aes_ctx);
  700. if (aes_iv)
  701. rt_free(aes_iv);
  702. if (crypt_buf)
  703. rt_free(crypt_buf);
  704. if (cmprs_buff)
  705. rt_free(cmprs_buff);
  706. if (dcprs_buff)
  707. rt_free(dcprs_buff);
  708. if (dcprs_state)
  709. rt_free(dcprs_state);
  710. if (fota_err == RT_FOTA_NO_ERR)
  711. {
  712. LOG_I("Upgrade success, total %d bytes.", total_copy_size);
  713. }
  714. return fota_err;
  715. }
  716. int rt_fota_start_application(void)
  717. {
  718. int fota_res = RT_FOTA_NO_ERR;
  719. const struct fal_partition *part;
  720. rt_uint32_t app_addr;
  721. part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  722. if (part == RT_NULL)
  723. {
  724. LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
  725. fota_res = RT_FOTA_GENERAL_ERR;
  726. goto __exit_start_application;
  727. }
  728. app_addr = part->offset + 0x08000000;
  729. //�ж��Ƿ�Ϊ0x08XXXXXX.
  730. if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
  731. {
  732. LOG_I("Illegal Flash code.");
  733. fota_res = RT_FOTA_GENERAL_ERR;
  734. goto __exit_start_application;
  735. }
  736. // ���ջ����ַ�Ƿ�Ϸ�.
  737. if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
  738. {
  739. LOG_I("Illegal Stack code.");
  740. fota_res = RT_FOTA_GENERAL_ERR;
  741. goto __exit_start_application;
  742. }
  743. LOG_I("Implement application now.");
  744. __disable_irq();
  745. HAL_DeInit();
  746. __set_CONTROL(0);
  747. //LOG_I("Implement application now.");
  748. // for (IRQn_Type irq = WWDG_IRQn; irq <= FPU_IRQn; irq++)
  749. // {
  750. // HAL_NVIC_DisableIRQ(irq);
  751. // HAL_NVIC_ClearPendingIRQ(irq);
  752. // }
  753. //Resets the RCC clock configuration to the default reset state.
  754. HAL_RCC_DeInit();
  755. SysTick->CTRL = 0;
  756. SysTick->LOAD = 0;
  757. SysTick->VAL = 0;
  758. HAL_DeInit();
  759. //用户代码区第二个字为程序开始地址(复位地址)
  760. app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
  761. /* Configure main stack */
  762. __set_MSP(*(__IO uint32_t *)app_addr);
  763. /* jump to application */
  764. app_func();
  765. __exit_start_application:
  766. LOG_I("Implement application failed.");
  767. HMI_WRITE("Implement application failed.");
  768. return fota_res;
  769. }
  770. static rt_err_t rt_fota_get_shell_key(void)
  771. {
  772. char ch;
  773. rt_err_t res = RT_EOK;
  774. rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
  775. rt_tick_t tick_start, tick_stop;
  776. RT_ASSERT(shell_dev != RT_NULL);
  777. RT_ASSERT(shell_sem != RT_NULL);
  778. rt_tick_set(0);
  779. tick_start = rt_tick_get();
  780. while (1)
  781. {
  782. if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
  783. {
  784. if (rt_sem_take(shell_sem, timeout) != RT_EOK)
  785. {
  786. res = RT_ERROR;
  787. goto __exit_get_shell_key;
  788. }
  789. }
  790. if (ch == 0x0d)
  791. goto __exit_get_shell_key;
  792. tick_stop = rt_tick_get();
  793. if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
  794. {
  795. res = RT_ERROR;
  796. goto __exit_get_shell_key;
  797. }
  798. timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
  799. }
  800. __exit_get_shell_key:
  801. if(stop_in_bootloader!=0)
  802. {
  803. res = RT_EOK;
  804. }
  805. return res;
  806. }
  807. static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
  808. {
  809. RT_ASSERT(shell_sem != RT_NULL);
  810. /* release semaphore to let fota thread rx data */
  811. rt_sem_release(shell_sem);
  812. return RT_EOK;
  813. }
  814. static rt_err_t rt_fota_set_device(const char *device_name)
  815. {
  816. rt_device_t dev;
  817. RT_ASSERT(device_name != RT_NULL);
  818. dev = rt_device_find(device_name);
  819. if (dev == RT_NULL)
  820. {
  821. LOG_D("Can not find device: %s.", device_name);
  822. return RT_ERROR;
  823. }
  824. if (shell_sem)
  825. rt_sem_delete(shell_sem);
  826. shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
  827. if (shell_sem == RT_NULL)
  828. return RT_ERROR;
  829. if (dev == shell_dev)
  830. return RT_EOK;
  831. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  832. {
  833. if (shell_dev != RT_NULL)
  834. {
  835. /* close old finsh device */
  836. rt_device_close(shell_dev);
  837. rt_device_set_rx_indicate(shell_dev, RT_NULL);
  838. }
  839. shell_dev = dev;
  840. rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
  841. LOG_D("Shell device %s open success.", device_name);
  842. return RT_EOK;
  843. }
  844. LOG_D("Shell device %s open failed.", device_name);
  845. return RT_ERROR;
  846. }
  847. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  848. static rt_err_t rt_fota_check_defalut_key(void)
  849. {
  850. int chk_idx;
  851. /* GPIO initialized */
  852. rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
  853. /* Delay for power up */
  854. rt_thread_mdelay(500);
  855. if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
  856. rt_kprintf("Default firmware key pressed:\n");
  857. /* Check GPIO status */
  858. for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
  859. {
  860. rt_thread_mdelay(RT_TICK_PER_SECOND);
  861. rt_kprintf(">");
  862. }
  863. if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
  864. return RT_ERROR;
  865. else
  866. return RT_EOK;
  867. }
  868. #endif
  869. void rt_fota_thread_entry(void *arg)
  870. {
  871. int fota_err = RT_FOTA_NO_ERR;
  872. extern int finsh_system_init(void);
  873. /* Signal led initialized */
  874. rt_fota_signal_led_init();
  875. /* Partition initialized */
  876. fota_err = rt_fota_boot_verify();
  877. if (fota_err != RT_FOTA_NO_ERR)
  878. {
  879. LOG_I("Partition initialized failed.");
  880. }
  881. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  882. /* Default key check */
  883. if (rt_fota_check_defalut_key() == RT_EOK)
  884. {
  885. /* enter to defalut key mode */
  886. rt_fota_signal_led_mode(led_upgrade_mode);
  887. goto __exit_default_entry;
  888. }
  889. #endif
  890. /* Shell initialized */
  891. if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
  892. {
  893. if(easyflash_init()==EF_NO_ERR) //初始化成功
  894. {
  895. ef_get_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader), NULL); //读出reboot_time的值
  896. rt_kprintf("stop_in_bootloader:%d\n",stop_in_bootloader);
  897. }
  898. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  899. if (rt_fota_get_shell_key() == RT_EOK)
  900. {
  901. if(stop_in_bootloader!=0)
  902. {
  903. stop_in_bootloader = 0;
  904. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  905. }
  906. goto __exit_shell_entry;
  907. }
  908. }
  909. else
  910. {
  911. LOG_I("Shell device config failed.");
  912. }
  913. /* Firmware partition verify */
  914. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  915. if (fota_err != RT_FOTA_NO_ERR)
  916. goto __exit_boot_entry;
  917. /* Check upgrade status */
  918. if (rt_fota_check_upgrade() <= 0)
  919. goto __exit_boot_entry;
  920. /* enter to upgrade mode */
  921. rt_fota_signal_led_mode(led_upgrade_mode);
  922. /* Implement upgrade, copy firmware partition to app partition */
  923. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  924. if (fota_err != RT_FOTA_NO_ERR)
  925. goto __exit_boot_entry;
  926. /* Update new application verison in RBL file of firmware partition */
  927. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  928. if (fota_err != RT_FOTA_NO_ERR)
  929. goto __exit_boot_entry;
  930. __exit_boot_entry:
  931. /* Implement application */
  932. rt_fota_start_application();
  933. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  934. __exit_default_entry:
  935. #endif
  936. /* Implement upgrade, copy default partition to app partition */
  937. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  938. {
  939. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  940. {
  941. rt_fota_start_application();
  942. }
  943. }
  944. LOG_I("Boot application failed, entry shell mode.");
  945. __exit_shell_entry:
  946. /* enter to shell mode */
  947. rt_fota_signal_led_mode(led_shell_mode);
  948. /* Implement shell */
  949. finsh_system_init();
  950. }
  951. void rt_fota_init(void)
  952. {
  953. rt_thread_t tid;
  954. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  955. if (tid != RT_NULL)
  956. {
  957. rt_thread_startup(tid);
  958. }
  959. else
  960. {
  961. LOG_I("rt-fota thread create failed.");
  962. }
  963. }
  964. void rt_fota_info(rt_uint8_t argc, char **argv)
  965. {
  966. char put_buf[24];
  967. char part_name[2][FAL_DEV_NAME_MAX] =
  968. {
  969. {RT_FOTA_FM_PART_NAME},
  970. {RT_FOTA_DF_PART_NAME}
  971. };
  972. const char* help_info[] =
  973. {
  974. [0] = "fota probe - probe RBL file of partiton",
  975. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  976. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  977. [3] = "fota exec - execute application program",
  978. };
  979. if (argc < 2)
  980. {
  981. rt_kprintf("Usage:\n");
  982. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  983. {
  984. rt_kprintf("%s\n", help_info[i]);
  985. }
  986. rt_kprintf("\n");
  987. }
  988. else
  989. {
  990. const char *operator = argv[1];
  991. if (!rt_strcmp(operator, "probe"))
  992. {
  993. for (int i = 0; i < 2; i++)
  994. {
  995. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  996. {
  997. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  998. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  999. rt_memset(put_buf, 0x0, sizeof(put_buf));
  1000. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  1001. {
  1002. rt_strncpy(put_buf, " AES", 4);
  1003. }
  1004. else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  1005. {
  1006. rt_strncpy(put_buf, " XOR", 4);
  1007. }
  1008. else
  1009. {
  1010. rt_strncpy(put_buf, "NONE", 4);
  1011. }
  1012. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  1013. {
  1014. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  1015. }
  1016. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  1017. {
  1018. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  1019. }
  1020. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  1021. {
  1022. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  1023. }
  1024. if (rt_strlen(put_buf) <= 0)
  1025. {
  1026. rt_strncpy(put_buf, "None", 4);
  1027. }
  1028. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  1029. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  1030. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  1031. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  1032. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1033. }
  1034. }
  1035. }
  1036. else if (!rt_strcmp(operator, "show"))
  1037. {
  1038. const struct fal_partition *part;
  1039. const char *part_name = argv[2];
  1040. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  1041. rt_uint32_t size = strtol(argv[4], NULL, 0);
  1042. rt_uint8_t buf[16];
  1043. part = fal_partition_find(part_name);
  1044. if (part != RT_NULL)
  1045. {
  1046. while (size > 16)
  1047. {
  1048. fal_partition_read(part, addr, buf, 16);
  1049. rt_kprintf("%08X: ", addr);
  1050. for (int i = 0; i < 16; i++)
  1051. {
  1052. rt_kprintf("%02X ", buf[i]);
  1053. }
  1054. rt_kprintf("\n");
  1055. size -= 16;
  1056. addr += 16;
  1057. }
  1058. fal_partition_read(part, addr, buf, size);
  1059. rt_kprintf("%08X: ", addr);
  1060. for (int i = 0; i < size; i++)
  1061. {
  1062. rt_kprintf("%02X ", buf[i]);
  1063. }
  1064. rt_kprintf("\n");
  1065. }
  1066. else
  1067. {
  1068. rt_kprintf("%s partition is not exist!\n", part_name);
  1069. }
  1070. }
  1071. else if (!rt_strcmp(operator, "clone"))
  1072. {
  1073. const char *dst_part_name = argv[2];
  1074. const char *src_part_name = argv[3];
  1075. const struct fal_partition *dst_part;
  1076. const struct fal_partition *src_part;
  1077. dst_part = fal_partition_find(dst_part_name);
  1078. src_part = fal_partition_find(src_part_name);
  1079. if (dst_part == RT_NULL || src_part == RT_NULL)
  1080. {
  1081. if (dst_part == RT_NULL)
  1082. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1083. if (src_part == RT_NULL)
  1084. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1085. }
  1086. else
  1087. {
  1088. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1089. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1090. {
  1091. int clone_pos = 0;
  1092. int clone_len = 0, clone_tol_len;
  1093. rt_uint8_t *buf = rt_malloc(4096);
  1094. if (dst_part->len < src_part->len)
  1095. clone_tol_len = dst_part->len;
  1096. else
  1097. clone_tol_len = src_part->len;
  1098. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1099. {
  1100. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1101. if (clone_len < 0)
  1102. {
  1103. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1104. break;
  1105. }
  1106. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1107. {
  1108. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1109. break;
  1110. }
  1111. rt_kprintf("#");
  1112. clone_pos += clone_len;
  1113. }
  1114. if (clone_pos >= clone_tol_len)
  1115. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1116. else
  1117. rt_kprintf("\nClone partition failed!\n");
  1118. if (buf)
  1119. rt_free(buf);
  1120. }
  1121. }
  1122. }
  1123. else if (!rt_strcmp(operator, "exec"))
  1124. {
  1125. rt_fota_start_application();
  1126. }
  1127. else
  1128. {
  1129. rt_kprintf("Usage:\n");
  1130. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1131. {
  1132. rt_kprintf("%s\n", help_info[i]);
  1133. }
  1134. rt_kprintf("\n");
  1135. }
  1136. }
  1137. }
  1138. /**
  1139. * rt-fota />ymodem_ota
  1140. */
  1141. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);