rt_fota.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. if(stop_in_bootloader>1)
  902. {
  903. stop_in_bootloader = 0;
  904. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  905. }
  906. }
  907. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  908. if (rt_fota_get_shell_key() == RT_EOK)
  909. {
  910. if(stop_in_bootloader!=0)
  911. {
  912. stop_in_bootloader = 0;
  913. ef_set_env_blob("stop_in_bootloader", &stop_in_bootloader, sizeof(stop_in_bootloader));
  914. }
  915. goto __exit_shell_entry;
  916. }
  917. }
  918. else
  919. {
  920. LOG_I("Shell device config failed.");
  921. }
  922. /* Firmware partition verify */
  923. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  924. if (fota_err != RT_FOTA_NO_ERR)
  925. goto __exit_boot_entry;
  926. /* Check upgrade status */
  927. if (rt_fota_check_upgrade() <= 0)
  928. goto __exit_boot_entry;
  929. /* enter to upgrade mode */
  930. rt_fota_signal_led_mode(led_upgrade_mode);
  931. /* Implement upgrade, copy firmware partition to app partition */
  932. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  933. if (fota_err != RT_FOTA_NO_ERR)
  934. goto __exit_boot_entry;
  935. /* Update new application verison in RBL file of firmware partition */
  936. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  937. if (fota_err != RT_FOTA_NO_ERR)
  938. goto __exit_boot_entry;
  939. __exit_boot_entry:
  940. /* Implement application */
  941. rt_fota_start_application();
  942. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  943. __exit_default_entry:
  944. #endif
  945. /* Implement upgrade, copy default partition to app partition */
  946. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  947. {
  948. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  949. {
  950. rt_fota_start_application();
  951. }
  952. }
  953. LOG_I("Boot application failed, entry shell mode.");
  954. __exit_shell_entry:
  955. /* enter to shell mode */
  956. rt_fota_signal_led_mode(led_shell_mode);
  957. /* Implement shell */
  958. finsh_system_init();
  959. }
  960. void rt_fota_init(void)
  961. {
  962. rt_thread_t tid;
  963. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  964. if (tid != RT_NULL)
  965. {
  966. rt_thread_startup(tid);
  967. }
  968. else
  969. {
  970. LOG_I("rt-fota thread create failed.");
  971. }
  972. }
  973. void rt_fota_info(rt_uint8_t argc, char **argv)
  974. {
  975. char put_buf[24];
  976. char part_name[2][FAL_DEV_NAME_MAX] =
  977. {
  978. {RT_FOTA_FM_PART_NAME},
  979. {RT_FOTA_DF_PART_NAME}
  980. };
  981. const char* help_info[] =
  982. {
  983. [0] = "fota probe - probe RBL file of partiton",
  984. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  985. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  986. [3] = "fota exec - execute application program",
  987. };
  988. if (argc < 2)
  989. {
  990. rt_kprintf("Usage:\n");
  991. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  992. {
  993. rt_kprintf("%s\n", help_info[i]);
  994. }
  995. rt_kprintf("\n");
  996. }
  997. else
  998. {
  999. const char *operator = argv[1];
  1000. if (!rt_strcmp(operator, "probe"))
  1001. {
  1002. for (int i = 0; i < 2; i++)
  1003. {
  1004. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  1005. {
  1006. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  1007. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  1008. rt_memset(put_buf, 0x0, sizeof(put_buf));
  1009. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  1010. {
  1011. rt_strncpy(put_buf, " AES", 4);
  1012. }
  1013. else if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  1014. {
  1015. rt_strncpy(put_buf, " XOR", 4);
  1016. }
  1017. else
  1018. {
  1019. rt_strncpy(put_buf, "NONE", 4);
  1020. }
  1021. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  1022. {
  1023. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  1024. }
  1025. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  1026. {
  1027. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  1028. }
  1029. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  1030. {
  1031. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  1032. }
  1033. if (rt_strlen(put_buf) <= 0)
  1034. {
  1035. rt_strncpy(put_buf, "None", 4);
  1036. }
  1037. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  1038. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  1039. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  1040. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  1041. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1042. }
  1043. }
  1044. }
  1045. else if (!rt_strcmp(operator, "show"))
  1046. {
  1047. const struct fal_partition *part;
  1048. const char *part_name = argv[2];
  1049. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  1050. rt_uint32_t size = strtol(argv[4], NULL, 0);
  1051. rt_uint8_t buf[16];
  1052. part = fal_partition_find(part_name);
  1053. if (part != RT_NULL)
  1054. {
  1055. while (size > 16)
  1056. {
  1057. fal_partition_read(part, addr, buf, 16);
  1058. rt_kprintf("%08X: ", addr);
  1059. for (int i = 0; i < 16; i++)
  1060. {
  1061. rt_kprintf("%02X ", buf[i]);
  1062. }
  1063. rt_kprintf("\n");
  1064. size -= 16;
  1065. addr += 16;
  1066. }
  1067. fal_partition_read(part, addr, buf, size);
  1068. rt_kprintf("%08X: ", addr);
  1069. for (int i = 0; i < size; i++)
  1070. {
  1071. rt_kprintf("%02X ", buf[i]);
  1072. }
  1073. rt_kprintf("\n");
  1074. }
  1075. else
  1076. {
  1077. rt_kprintf("%s partition is not exist!\n", part_name);
  1078. }
  1079. }
  1080. else if (!rt_strcmp(operator, "clone"))
  1081. {
  1082. const char *dst_part_name = argv[2];
  1083. const char *src_part_name = argv[3];
  1084. const struct fal_partition *dst_part;
  1085. const struct fal_partition *src_part;
  1086. dst_part = fal_partition_find(dst_part_name);
  1087. src_part = fal_partition_find(src_part_name);
  1088. if (dst_part == RT_NULL || src_part == RT_NULL)
  1089. {
  1090. if (dst_part == RT_NULL)
  1091. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1092. if (src_part == RT_NULL)
  1093. rt_kprintf("%s partition is not exist!\n", src_part_name);
  1094. }
  1095. else
  1096. {
  1097. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1098. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1099. {
  1100. int clone_pos = 0;
  1101. int clone_len = 0, clone_tol_len;
  1102. rt_uint8_t *buf = rt_malloc(4096);
  1103. if (dst_part->len < src_part->len)
  1104. clone_tol_len = dst_part->len;
  1105. else
  1106. clone_tol_len = src_part->len;
  1107. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1108. {
  1109. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1110. if (clone_len < 0)
  1111. {
  1112. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1113. break;
  1114. }
  1115. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1116. {
  1117. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1118. break;
  1119. }
  1120. rt_kprintf("#");
  1121. clone_pos += clone_len;
  1122. }
  1123. if (clone_pos >= clone_tol_len)
  1124. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1125. else
  1126. rt_kprintf("\nClone partition failed!\n");
  1127. if (buf)
  1128. rt_free(buf);
  1129. }
  1130. }
  1131. }
  1132. else if (!rt_strcmp(operator, "exec"))
  1133. {
  1134. rt_fota_start_application();
  1135. }
  1136. else
  1137. {
  1138. rt_kprintf("Usage:\n");
  1139. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1140. {
  1141. rt_kprintf("%s\n", help_info[i]);
  1142. }
  1143. rt_kprintf("\n");
  1144. }
  1145. }
  1146. }
  1147. /**
  1148. * rt-fota />ymodem_ota
  1149. */
  1150. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);