rt_fota.c 49 KB

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