rt_fota.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-09-22 Warfalcon first version
  9. */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <board.h>
  16. #include <fal.h>
  17. #include <tinycrypt.h>
  18. #include <fastlz.h>
  19. #include <quicklz.h>
  20. #include <rt_fota.h>
  21. #include <signal_led.h>
  22. #include "sys_config.h"
  23. #include "easyflash.h"
  24. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  25. #include <finsh.h>
  26. #include <shell.h>
  27. #endif
  28. #include <rt_fota.h>
  29. #define DBG_ENABLE
  30. #define DBG_SECTION_NAME "fota"
  31. #ifdef RT_FOTA_DEBUG
  32. #define DBG_LEVEL DBG_LOG
  33. #else
  34. #define DBG_LEVEL DBG_INFO
  35. #endif
  36. #define DBG_COLOR
  37. #include <rtdbg.h>
  38. #ifndef RT_FOTA_THREAD_STACK_SIZE
  39. #define RT_FOTA_THREAD_STACK_SIZE 4096
  40. #endif
  41. #ifndef RT_FOTA_THREAD_PRIORITY
  42. #define RT_FOTA_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 3)
  43. #endif
  44. #ifndef RT_FOTA_ALGO_BUFF_SIZE
  45. #define RT_FOTA_ALGO_BUFF_SIZE 2048 //stm32f103flash分页是2K
  46. #endif
  47. /**
  48. * AES256 encryption algorithm option
  49. */
  50. #ifndef RT_FOTA_ALGO_AES_IV
  51. #define RT_FOTA_ALGO_AES_IV "0123456789ABCDEF"
  52. #endif
  53. #ifndef RT_FOTA_ALGO_AES_KEY
  54. #define RT_FOTA_ALGO_AES_KEY "0123456789ABCDEF0123456789ABCDEF"
  55. #endif
  56. #ifndef RT_FOTA_BLOCK_HEADER_SIZE
  57. #define RT_FOTA_BLOCK_HEADER_SIZE 4
  58. #endif
  59. #ifndef RT_FOTA_CMPRS_BUFFER_SIZE
  60. #define RT_FOTA_CMPRS_BUFFER_SIZE 4096
  61. #endif
  62. #ifndef RT_FOTA_FASTLZ_BUFFER_PADDING
  63. #define RT_FOTA_FASTLZ_BUFFER_PADDING FASTLZ_BUFFER_PADDING(RT_FOTA_CMPRS_BUFFER_SIZE)
  64. #endif
  65. #ifndef RT_FOTA_QUICKLZ_BUFFER_PADDING
  66. #define RT_FOTA_QUICKLZ_BUFFER_PADDING QLZ_BUFFER_PADDING
  67. #endif
  68. #ifndef RT_FOTA_ENTER_SHELL_KEY
  69. #define RT_FOTA_ENTER_SHELL_KEY 0x0d
  70. #endif
  71. #ifndef RT_FOTA_GET_CHAR_WAITTIGN
  72. #define RT_FOTA_GET_CHAR_WAITTIGN (RT_TICK_PER_SECOND * 5)
  73. #endif
  74. #ifndef RT_FOTA_SIGNAL_LED_PIN
  75. #define RT_FOTA_SIGNAL_LED_PIN GET_PIN(B, 3)
  76. #endif
  77. #ifndef RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE
  78. #define RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE 1024
  79. #endif
  80. #ifndef RT_FOTA_SIGNAL_LED_THREAD_PRIORITY
  81. #define RT_FOTA_SIGNAL_LED_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 4)
  82. #endif
  83. /* For signal led */
  84. static led_t *signal_led = NULL;
  85. static led_mem_opreation_t signal_led_mem_op;
  86. const char *led_shell_mode = "500,500,"; /* 1Hz ��˸ */
  87. const char *led_upgrade_mode = "50,50,"; /* 10Hz ��˸ */
  88. const char *led_off_mode = "0,100,"; /* ���� */
  89. const char *led_on_mode = "100,0,"; /* ���� */
  90. static int stop_in_bootloader=0;
  91. /* For shell */
  92. static rt_sem_t shell_sem = RT_NULL;
  93. static rt_device_t shell_dev = RT_NULL;
  94. typedef struct {
  95. char type[4];
  96. rt_uint16_t fota_algo;
  97. rt_uint8_t fm_time[6];
  98. char app_part_name[16];
  99. char download_version[24];
  100. char current_version[24];
  101. rt_uint32_t code_crc;
  102. rt_uint32_t hash_val;
  103. rt_uint32_t raw_size;
  104. rt_uint32_t com_size;
  105. rt_uint32_t head_crc;
  106. } rt_fota_part_head, *rt_fota_part_head_t;
  107. typedef void (*rt_fota_app_func)(void);
  108. static rt_fota_app_func app_func = RT_NULL;
  109. static rt_fota_part_head fota_part_head;
  110. static void rt_fota_signal_led_on(void)
  111. {
  112. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_LOW);
  113. }
  114. static void rt_fota_signal_led_off(void)
  115. {
  116. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_HIGH);
  117. }
  118. static void rt_fota_signal_led_entry(void *arg)
  119. {
  120. while(1)
  121. {
  122. led_ticks();
  123. rt_thread_mdelay(LED_TICK_TIME);
  124. }
  125. }
  126. static void rt_fota_signal_led_init(void)
  127. {
  128. rt_pin_mode(RT_FOTA_SIGNAL_LED_PIN, PIN_MODE_OUTPUT);
  129. signal_led_mem_op.malloc_fn = (void* (*)(size_t))rt_malloc;
  130. signal_led_mem_op.free_fn = rt_free;
  131. led_set_mem_operation(&signal_led_mem_op);
  132. signal_led = led_create(rt_fota_signal_led_on, rt_fota_signal_led_off);
  133. /* Config signal led mode */
  134. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_on_mode);
  135. led_set_blink_over_callback(signal_led, RT_NULL);
  136. led_start(signal_led);
  137. rt_thread_t tid;
  138. tid = rt_thread_create("sig_led", rt_fota_signal_led_entry, RT_NULL, RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE, RT_FOTA_SIGNAL_LED_THREAD_PRIORITY, 10);
  139. if (tid)
  140. rt_thread_startup(tid);
  141. }
  142. static void rt_fota_signal_led_mode(const char *led_cfg)
  143. {
  144. RT_ASSERT(led_cfg != RT_NULL);
  145. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_cfg);
  146. }
  147. static int rt_fota_boot_verify(void)
  148. {
  149. int fota_res = RT_FOTA_NO_ERR;
  150. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  151. /* partition initial */
  152. fal_init();
  153. extern int fal_init_check(void);
  154. /* verify partition */
  155. if (fal_init_check() != 1)
  156. {
  157. LOG_D("Partition initialized failed!");
  158. fota_res = RT_FOTA_GENERAL_ERR;
  159. goto __exit_boot_verify;
  160. }
  161. __exit_boot_verify:
  162. return fota_res;
  163. }
  164. int rt_fota_part_fw_verify(const char *part_name)
  165. {
  166. #define RT_FOTA_CRC_BUFF_SIZE 4096
  167. #define RT_FOTA_CRC_INIT_VAL 0xffffffff
  168. int fota_res = RT_FOTA_NO_ERR;
  169. const struct fal_partition *part;
  170. rt_fota_part_head part_head;
  171. rt_uint8_t *body_buf = RT_NULL;
  172. rt_uint32_t body_crc = RT_FOTA_CRC_INIT_VAL;
  173. rt_uint32_t hdr_crc;
  174. if (part_name == RT_NULL)
  175. {
  176. LOG_D("Invaild paramenter input!");
  177. fota_res = RT_FOTA_GENERAL_ERR;
  178. goto __exit_partition_verify;
  179. }
  180. part = fal_partition_find(part_name);
  181. if (part == RT_NULL)
  182. {
  183. LOG_D("Partition[%s] not found.", part_name);
  184. fota_res = RT_FOTA_GENERAL_ERR;
  185. goto __exit_partition_verify;
  186. }
  187. /* read the head of RBL files */
  188. if (fal_partition_read(part, 0, (rt_uint8_t *)&part_head, sizeof(rt_fota_part_head)) < 0)
  189. {
  190. LOG_D("Partition[%s] read error!", part->name);
  191. fota_res = RT_FOTA_PART_READ_ERR;
  192. goto __exit_partition_verify;
  193. }
  194. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  195. hdr_crc = rt_fota_crc((rt_uint8_t *)&part_head, sizeof(rt_fota_part_head) - 4);
  196. if (hdr_crc != part_head.head_crc)
  197. {
  198. LOG_D("Partition[%s] head CRC32 error!", part->name);
  199. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  200. goto __exit_partition_verify;
  201. }
  202. if (rt_strcmp(part_head.type, "RBL") != 0)
  203. {
  204. LOG_D("Partition[%s] type[%s] not surport.", part->name, part_head.type);
  205. fota_res = RT_FOTA_CHECK_FAILED;
  206. goto __exit_partition_verify;
  207. }
  208. if (fal_partition_find(part_head.app_part_name) == RT_NULL)
  209. {
  210. LOG_D("Partition[%s] not found.", part_head.app_part_name);
  211. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  212. goto __exit_partition_verify;
  213. }
  214. body_buf = rt_malloc(RT_FOTA_CRC_BUFF_SIZE);
  215. if (body_buf == RT_NULL)
  216. {
  217. LOG_D("Not enough memory for body CRC32 verify.");
  218. fota_res = RT_FOTA_NO_MEM_ERR;
  219. goto __exit_partition_verify;
  220. }
  221. for (int body_pos = 0; body_pos < part_head.com_size;)
  222. {
  223. int body_read_len = fal_partition_read(part, sizeof(rt_fota_part_head) + body_pos, body_buf, RT_FOTA_CRC_BUFF_SIZE);
  224. if (body_read_len > 0)
  225. {
  226. if ((body_pos + body_read_len) > part_head.com_size)
  227. {
  228. body_read_len = part_head.com_size - body_pos;
  229. }
  230. extern rt_uint32_t rt_fota_step_crc(rt_uint32_t crc, rt_uint8_t *buf, rt_uint32_t len);
  231. body_crc = rt_fota_step_crc(body_crc, body_buf, body_read_len);
  232. body_pos = body_pos + body_read_len;
  233. }
  234. else
  235. {
  236. LOG_D("Partition[%s] read error!", part->name);
  237. fota_res = RT_FOTA_PART_READ_ERR;
  238. goto __exit_partition_verify;
  239. }
  240. }
  241. body_crc = body_crc ^ RT_FOTA_CRC_INIT_VAL;
  242. if (body_crc != part_head.code_crc)
  243. {
  244. LOG_D("Partition[%s] firmware integrity verify failed.", part->name);
  245. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  246. goto __exit_partition_verify;
  247. }
  248. __exit_partition_verify:
  249. if (fota_res == RT_FOTA_NO_ERR)
  250. {
  251. rt_enter_critical();
  252. rt_memcpy(&fota_part_head, &part_head, sizeof(rt_fota_part_head));
  253. rt_exit_critical();
  254. LOG_D("partition[%s] verify success!", part->name);
  255. }
  256. else
  257. {
  258. rt_enter_critical();
  259. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  260. rt_exit_critical();
  261. LOG_D("Partition[%s] verify failed!", part->name);
  262. }
  263. if (body_buf)
  264. rt_free(body_buf);
  265. return fota_res;
  266. }
  267. int rt_fota_check_upgrade(void)
  268. {
  269. int is_upgrade = 0;
  270. rt_kprintf("download_version %s >> current_version %s \n" , fota_part_head.download_version, fota_part_head.current_version);
  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. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  540. rt_memcpy(cmprs_buff, &crypt_buf[RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  541. block_hdr_pos = RT_FOTA_BLOCK_HEADER_SIZE + block_size;
  542. }
  543. else
  544. {
  545. rt_uint8_t hdr_tmp_pos = 0;
  546. while (block_hdr_pos < RT_FOTA_ALGO_BUFF_SIZE)
  547. {
  548. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  549. {
  550. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  551. }
  552. else
  553. {
  554. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  555. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  556. if (block_size > (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos))
  557. {
  558. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], (RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos));
  559. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  560. if (fw_raw_len < 0)
  561. {
  562. LOG_D("AES256 algorithm failed.");
  563. fota_err = RT_FOTA_PART_READ_ERR;
  564. goto __exit_upgrade;
  565. }
  566. fw_raw_pos += fw_raw_len;
  567. rt_memcpy(&cmprs_buff[RT_FOTA_ALGO_BUFF_SIZE - block_hdr_pos], &crypt_buf[0], (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE);
  568. block_hdr_pos = (block_size + block_hdr_pos) - RT_FOTA_ALGO_BUFF_SIZE;
  569. }
  570. else
  571. {
  572. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  573. block_hdr_pos = block_hdr_pos + block_size;
  574. }
  575. break;
  576. }
  577. }
  578. if (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  579. {
  580. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  581. if (fw_raw_len < 0)
  582. {
  583. LOG_D("AES256 algorithm failed.");
  584. fota_err = RT_FOTA_PART_READ_ERR;
  585. goto __exit_upgrade;
  586. }
  587. fw_raw_pos += fw_raw_len;
  588. block_hdr_pos = 0;
  589. while (hdr_tmp_pos < RT_FOTA_BLOCK_HEADER_SIZE)
  590. {
  591. block_hdr_buf[hdr_tmp_pos++] = crypt_buf[block_hdr_pos++];
  592. }
  593. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  594. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  595. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos], block_size);
  596. block_hdr_pos = (block_hdr_pos + block_size) % RT_FOTA_ALGO_BUFF_SIZE;
  597. }
  598. }
  599. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  600. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  601. {
  602. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  603. }
  604. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  605. {
  606. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  607. }
  608. if (dcprs_size <= 0)
  609. {
  610. LOG_D("Decompress failed: %d.", dcprs_size);
  611. fota_err = RT_FOTA_GENERAL_ERR;
  612. goto __exit_upgrade;
  613. }
  614. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  615. {
  616. fota_err = RT_FOTA_COPY_FAILED;
  617. goto __exit_upgrade;
  618. }
  619. total_copy_size += dcprs_size;
  620. rt_kprintf("#");
  621. }
  622. /* no compress option */
  623. else
  624. {
  625. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  626. if (fw_raw_len < 0)
  627. {
  628. LOG_D("AES256 algorithm failed.");
  629. fota_err = RT_FOTA_PART_READ_ERR;
  630. goto __exit_upgrade;
  631. }
  632. fw_raw_pos += fw_raw_len;
  633. if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
  634. {
  635. fota_err = RT_FOTA_COPY_FAILED;
  636. goto __exit_upgrade;
  637. }
  638. total_copy_size += fw_raw_len;
  639. rt_kprintf("#");
  640. }
  641. }
  642. /* it has compress option */
  643. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  644. {
  645. while (total_copy_size < part_head->raw_size)
  646. {
  647. if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
  648. {
  649. rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
  650. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  651. if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
  652. {
  653. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  654. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  655. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  656. block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
  657. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  658. {
  659. dcprs_size = fastlz_decompress((const void *)&cmprs_buff[0], block_size, &dcprs_buff[0], RT_FOTA_CMPRS_BUFFER_SIZE);
  660. }
  661. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  662. {
  663. dcprs_size = qlz_decompress((const char *)&cmprs_buff[0], &dcprs_buff[0], dcprs_state);
  664. }
  665. if (dcprs_size <= 0)
  666. {
  667. LOG_D("Decompress failed: %d.", dcprs_size);
  668. fota_err = RT_FOTA_GENERAL_ERR;
  669. goto __exit_upgrade;
  670. }
  671. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  672. {
  673. fota_err = RT_FOTA_COPY_FAILED;
  674. goto __exit_upgrade;
  675. }
  676. total_copy_size += dcprs_size;
  677. rt_kprintf("#");
  678. }
  679. else
  680. {
  681. break;
  682. }
  683. }
  684. else
  685. {
  686. break;
  687. }
  688. }
  689. }
  690. rt_kprintf("\r\n");
  691. /* �п�������ֵ�����,��ΪAES��Ҫ���16�ֽ�����,�����Ľ��ܽ�ѹֵ�Ĵ������������Ǵ��ڵ���raw_size */
  692. /* �ȽϺõķ�������һ��У��,Ŀǰ��������HASH_CODE�㷨��֪�� */
  693. if (total_copy_size < part_head->raw_size)
  694. {
  695. LOG_D("Decompress check failed.");
  696. fota_err = RT_FOTA_GENERAL_ERR;
  697. }
  698. __exit_upgrade:
  699. if (aes_ctx)
  700. rt_free(aes_ctx);
  701. if (aes_iv)
  702. rt_free(aes_iv);
  703. if (crypt_buf)
  704. rt_free(crypt_buf);
  705. if (cmprs_buff)
  706. rt_free(cmprs_buff);
  707. if (dcprs_buff)
  708. rt_free(dcprs_buff);
  709. if (dcprs_state)
  710. rt_free(dcprs_state);
  711. if (fota_err == RT_FOTA_NO_ERR)
  712. {
  713. LOG_I("Upgrade success, total %d bytes.", total_copy_size);
  714. }
  715. return fota_err;
  716. }
  717. int rt_fota_start_application(void)
  718. {
  719. int fota_res = RT_FOTA_NO_ERR;
  720. const struct fal_partition *part;
  721. rt_uint32_t app_addr;
  722. part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  723. if (part == RT_NULL)
  724. {
  725. LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
  726. fota_res = RT_FOTA_GENERAL_ERR;
  727. goto __exit_start_application;
  728. }
  729. app_addr = part->offset + 0x08000000;
  730. //�ж��Ƿ�Ϊ0x08XXXXXX.
  731. if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
  732. {
  733. LOG_I("Illegal Flash code.");
  734. fota_res = RT_FOTA_GENERAL_ERR;
  735. goto __exit_start_application;
  736. }
  737. // ���ջ����ַ�Ƿ�Ϸ�.
  738. if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
  739. {
  740. LOG_I("Illegal Stack code.");
  741. fota_res = RT_FOTA_GENERAL_ERR;
  742. goto __exit_start_application;
  743. }
  744. LOG_I("Implement application now.");
  745. __set_PRIMASK(1);
  746. for (IRQn_Type irq = WWDG_IRQn; irq <= DMA2_Channel4_5_IRQn; irq++)
  747. {
  748. HAL_NVIC_DisableIRQ(irq);
  749. HAL_NVIC_ClearPendingIRQ(irq);
  750. }
  751. HAL_DeInit();
  752. //LOG_I("Implement application now.");
  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. __set_PRIMASK(0);
  760. __HAL_FLASH_PREFETCH_BUFFER_DISABLE();
  761. //用户代码区第二个字为程序开始地址(复位地址)
  762. app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
  763. /* Configure main stack */
  764. __set_MSP(*(__IO uint32_t *)app_addr);
  765. SCB->VTOR = 0x08032000;
  766. __set_CONTROL(0);
  767. /* jump to application */
  768. app_func();
  769. __exit_start_application:
  770. LOG_I("Implement application failed.");
  771. HMI_WRITE("Implement application failed.");
  772. return fota_res;
  773. }
  774. static rt_err_t rt_fota_get_shell_key(void)
  775. {
  776. char ch;
  777. rt_err_t res = RT_EOK;
  778. rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
  779. rt_tick_t tick_start, tick_stop;
  780. RT_ASSERT(shell_dev != RT_NULL);
  781. RT_ASSERT(shell_sem != RT_NULL);
  782. rt_tick_set(0);
  783. tick_start = rt_tick_get();
  784. while (1)
  785. {
  786. if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
  787. {
  788. if (rt_sem_take(shell_sem, timeout) != RT_EOK)
  789. {
  790. res = RT_ERROR;
  791. goto __exit_get_shell_key;
  792. }
  793. }
  794. if (ch == 0x0d)
  795. goto __exit_get_shell_key;
  796. tick_stop = rt_tick_get();
  797. if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
  798. {
  799. res = RT_ERROR;
  800. goto __exit_get_shell_key;
  801. }
  802. timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
  803. }
  804. __exit_get_shell_key:
  805. if(stop_in_bootloader!=0)
  806. {
  807. res = RT_EOK;
  808. }
  809. return res;
  810. }
  811. static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
  812. {
  813. RT_ASSERT(shell_sem != RT_NULL);
  814. /* release semaphore to let fota thread rx data */
  815. rt_sem_release(shell_sem);
  816. return RT_EOK;
  817. }
  818. static rt_err_t rt_fota_set_device(const char *device_name)
  819. {
  820. rt_device_t dev;
  821. RT_ASSERT(device_name != RT_NULL);
  822. dev = rt_device_find(device_name);
  823. if (dev == RT_NULL)
  824. {
  825. LOG_D("Can not find device: %s.", device_name);
  826. return RT_ERROR;
  827. }
  828. if (shell_sem)
  829. rt_sem_delete(shell_sem);
  830. shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
  831. if (shell_sem == RT_NULL)
  832. return RT_ERROR;
  833. if (dev == shell_dev)
  834. return RT_EOK;
  835. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  836. {
  837. if (shell_dev != RT_NULL)
  838. {
  839. /* close old finsh device */
  840. rt_device_close(shell_dev);
  841. rt_device_set_rx_indicate(shell_dev, RT_NULL);
  842. }
  843. shell_dev = dev;
  844. rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
  845. LOG_D("Shell device %s open success.", device_name);
  846. return RT_EOK;
  847. }
  848. LOG_D("Shell device %s open failed.", device_name);
  849. return RT_ERROR;
  850. }
  851. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  852. static rt_err_t rt_fota_check_defalut_key(void)
  853. {
  854. int chk_idx;
  855. /* GPIO initialized */
  856. rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
  857. /* Delay for power up */
  858. rt_thread_mdelay(500);
  859. if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
  860. rt_kprintf("Default firmware key pressed:\n");
  861. /* Check GPIO status */
  862. for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
  863. {
  864. rt_thread_mdelay(RT_TICK_PER_SECOND);
  865. rt_kprintf(">");
  866. }
  867. if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
  868. return RT_ERROR;
  869. else
  870. return RT_EOK;
  871. }
  872. #endif
  873. void rt_fota_thread_entry(void *arg)
  874. {
  875. int fota_err = RT_FOTA_NO_ERR;
  876. extern int finsh_system_init(void);
  877. /* Signal led initialized */
  878. rt_fota_signal_led_init();
  879. /* Partition initialized */
  880. fota_err = rt_fota_boot_verify();
  881. if (fota_err != RT_FOTA_NO_ERR)
  882. {
  883. LOG_I("Partition initialized failed.");
  884. }
  885. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  886. /* Default key check */
  887. if (rt_fota_check_defalut_key() == RT_EOK)
  888. {
  889. /* enter to defalut key mode */
  890. rt_fota_signal_led_mode(led_upgrade_mode);
  891. goto __exit_default_entry;
  892. }
  893. #endif
  894. /* Shell initialized */
  895. if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
  896. {
  897. if(easyflash_init()==EF_NO_ERR) //初始化成功
  898. {
  899. ef_get_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader), NULL); //读出reboot_time的值
  900. rt_kprintf("stop_in_bootloader:%d\n",stop_in_bootloader);
  901. }
  902. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  903. if (rt_fota_get_shell_key() == RT_EOK)
  904. {
  905. if(stop_in_bootloader!=0)
  906. {
  907. stop_in_bootloader = 0;
  908. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  909. }
  910. goto __exit_shell_entry;
  911. }
  912. }
  913. else
  914. {
  915. LOG_I("Shell device config failed.");
  916. }
  917. /* Firmware partition verify */
  918. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  919. if (fota_err != RT_FOTA_NO_ERR)
  920. goto __exit_boot_entry;
  921. /* Check upgrade status */
  922. if (rt_fota_check_upgrade() <= 0)
  923. goto __exit_boot_entry;
  924. /* enter to upgrade mode */
  925. rt_fota_signal_led_mode(led_upgrade_mode);
  926. /* Implement upgrade, copy firmware partition to app partition */
  927. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  928. if (fota_err != RT_FOTA_NO_ERR)
  929. goto __exit_boot_entry;
  930. /* Update new application verison in RBL file of firmware partition */
  931. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  932. if (fota_err != RT_FOTA_NO_ERR)
  933. goto __exit_boot_entry;
  934. __exit_boot_entry:
  935. /* Implement application */
  936. rt_fota_start_application();
  937. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  938. __exit_default_entry:
  939. #endif
  940. /* Implement upgrade, copy default partition to app partition */
  941. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  942. {
  943. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  944. {
  945. rt_fota_start_application();
  946. }
  947. }
  948. LOG_I("Boot application failed, entry shell mode.");
  949. __exit_shell_entry:
  950. /* enter to shell mode */
  951. rt_fota_signal_led_mode(led_shell_mode);
  952. /* Implement shell */
  953. finsh_system_init();
  954. }
  955. void rt_fota_init(void)
  956. {
  957. rt_thread_t tid;
  958. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  959. if (tid != RT_NULL)
  960. {
  961. rt_thread_startup(tid);
  962. }
  963. else
  964. {
  965. LOG_I("rt-fota thread create failed.");
  966. }
  967. }
  968. void rt_fota_info(rt_uint8_t argc, char **argv)
  969. {
  970. char put_buf[24];
  971. char part_name[2][FAL_DEV_NAME_MAX] =
  972. {
  973. {RT_FOTA_FM_PART_NAME},
  974. {RT_FOTA_DF_PART_NAME}
  975. };
  976. const char* help_info[] =
  977. {
  978. [0] = "fota probe - probe RBL file of partiton",
  979. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  980. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  981. [3] = "fota exec - execute application program",
  982. };
  983. if (argc < 2)
  984. {
  985. rt_kprintf("Usage:\n");
  986. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  987. {
  988. rt_kprintf("%s\n", help_info[i]);
  989. }
  990. rt_kprintf("\n");
  991. }
  992. else
  993. {
  994. const char *operator = argv[1];
  995. if (!rt_strcmp(operator, "probe"))
  996. {
  997. for (int i = 0; i < 2; i++)
  998. {
  999. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  1000. {
  1001. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  1002. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  1003. rt_memset(put_buf, 0x0, sizeof(put_buf));
  1004. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  1005. {
  1006. rt_strncpy(put_buf, " AES", 4);
  1007. }
  1008. else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  1009. {
  1010. rt_strncpy(put_buf, " XOR", 4);
  1011. }
  1012. else
  1013. {
  1014. rt_strncpy(put_buf, "NONE", 4);
  1015. }
  1016. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  1017. {
  1018. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  1019. }
  1020. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  1021. {
  1022. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  1023. }
  1024. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  1025. {
  1026. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  1027. }
  1028. if (rt_strlen(put_buf) <= 0)
  1029. {
  1030. rt_strncpy(put_buf, "None", 4);
  1031. }
  1032. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  1033. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  1034. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  1035. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  1036. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1037. }
  1038. }
  1039. }
  1040. else if (!rt_strcmp(operator, "show"))
  1041. {
  1042. const struct fal_partition *part;
  1043. const char *part_name = argv[2];
  1044. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  1045. rt_uint32_t size = strtol(argv[4], NULL, 0);
  1046. rt_uint8_t buf[16];
  1047. part = fal_partition_find(part_name);
  1048. if (part != RT_NULL)
  1049. {
  1050. while (size > 16)
  1051. {
  1052. fal_partition_read(part, addr, buf, 16);
  1053. rt_kprintf("%08X: ", addr);
  1054. for (int i = 0; i < 16; i++)
  1055. {
  1056. rt_kprintf("%02X ", buf[i]);
  1057. }
  1058. rt_kprintf("\n");
  1059. size -= 16;
  1060. addr += 16;
  1061. }
  1062. fal_partition_read(part, addr, buf, size);
  1063. rt_kprintf("%08X: ", addr);
  1064. for (int i = 0; i < size; i++)
  1065. {
  1066. rt_kprintf("%02X ", buf[i]);
  1067. }
  1068. rt_kprintf("\n");
  1069. }
  1070. else
  1071. {
  1072. rt_kprintf("%s partition is not exist!\n", part_name);
  1073. }
  1074. }
  1075. else if (!rt_strcmp(operator, "clone"))
  1076. {
  1077. const char *dst_part_name = argv[2];
  1078. const char *src_part_name = argv[3];
  1079. const struct fal_partition *dst_part;
  1080. const struct fal_partition *src_part;
  1081. dst_part = fal_partition_find(dst_part_name);
  1082. src_part = fal_partition_find(src_part_name);
  1083. if (dst_part == RT_NULL || src_part == RT_NULL)
  1084. {
  1085. if (dst_part == RT_NULL)
  1086. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1087. if (src_part == RT_NULL)
  1088. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1089. }
  1090. else
  1091. {
  1092. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1093. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1094. {
  1095. int clone_pos = 0;
  1096. int clone_len = 0, clone_tol_len;
  1097. rt_uint8_t *buf = rt_malloc(4096);
  1098. if (dst_part->len < src_part->len)
  1099. clone_tol_len = dst_part->len;
  1100. else
  1101. clone_tol_len = src_part->len;
  1102. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1103. {
  1104. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1105. if (clone_len < 0)
  1106. {
  1107. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1108. break;
  1109. }
  1110. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1111. {
  1112. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1113. break;
  1114. }
  1115. rt_kprintf("#");
  1116. clone_pos += clone_len;
  1117. }
  1118. if (clone_pos >= clone_tol_len)
  1119. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1120. else
  1121. rt_kprintf("\nClone partition failed!\n");
  1122. if (buf)
  1123. rt_free(buf);
  1124. }
  1125. }
  1126. }
  1127. else if (!rt_strcmp(operator, "exec"))
  1128. {
  1129. rt_fota_start_application();
  1130. }
  1131. else
  1132. {
  1133. rt_kprintf("Usage:\n");
  1134. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1135. {
  1136. rt_kprintf("%s\n", help_info[i]);
  1137. }
  1138. rt_kprintf("\n");
  1139. }
  1140. }
  1141. }
  1142. /**
  1143. * rt-fota />ymodem_ota
  1144. */
  1145. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);