rt_fota.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. void firstime_clone_df(void);
  24. #define EXWATCHDOG_PIN GET_PIN(E, 11)
  25. void feed_watch(void)
  26. {
  27. if(rt_pin_read(EXWATCHDOG_PIN) == 0)rt_pin_write(EXWATCHDOG_PIN, PIN_HIGH);
  28. else if(rt_pin_read(EXWATCHDOG_PIN) == 1)rt_pin_write(EXWATCHDOG_PIN, PIN_LOW);
  29. }
  30. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  31. #include <finsh.h>
  32. #include <shell.h>
  33. #endif
  34. #define DBG_ENABLE
  35. #define DBG_SECTION_NAME "fota"
  36. #ifdef RT_FOTA_DEBUG
  37. #define DBG_LEVEL DBG_INFO
  38. #else
  39. #define DBG_LEVEL DBG_INFO
  40. #endif
  41. #define DBG_COLOR
  42. #include <rtdbg.h>
  43. #ifndef RT_FOTA_THREAD_STACK_SIZE
  44. #define RT_FOTA_THREAD_STACK_SIZE 4096
  45. #endif
  46. #ifndef RT_FOTA_THREAD_PRIORITY
  47. #define RT_FOTA_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 3)
  48. #endif
  49. #ifndef RT_FOTA_ALGO_BUFF_SIZE
  50. #define RT_FOTA_ALGO_BUFF_SIZE 4096
  51. #endif
  52. /**
  53. * AES256 encryption algorithm option
  54. */
  55. #ifndef RT_FOTA_ALGO_AES_IV
  56. #define RT_FOTA_ALGO_AES_IV "0123456789ABCDEF"
  57. #endif
  58. #ifndef RT_FOTA_ALGO_AES_KEY
  59. #define RT_FOTA_ALGO_AES_KEY "0123456789ABCDEF0123456789ABCDEF"
  60. #endif
  61. #ifndef RT_FOTA_BLOCK_HEADER_SIZE
  62. #define RT_FOTA_BLOCK_HEADER_SIZE 4
  63. #endif
  64. #ifndef RT_FOTA_CMPRS_BUFFER_SIZE
  65. #define RT_FOTA_CMPRS_BUFFER_SIZE 4096
  66. #endif
  67. #ifndef RT_FOTA_FASTLZ_BUFFER_PADDING
  68. #define RT_FOTA_FASTLZ_BUFFER_PADDING FASTLZ_BUFFER_PADDING(RT_FOTA_CMPRS_BUFFER_SIZE)
  69. #endif
  70. //#ifndef RT_FOTA_QUICKLZ_BUFFER_PADDING
  71. //#define RT_FOTA_QUICKLZ_BUFFER_PADDING QLZ_BUFFER_PADDING
  72. //#endif
  73. #ifndef RT_FOTA_ENTER_SHELL_KEY
  74. #define RT_FOTA_ENTER_SHELL_KEY 0x0d
  75. #endif
  76. #ifndef RT_FOTA_GET_CHAR_WAITTIGN
  77. #define RT_FOTA_GET_CHAR_WAITTIGN (RT_TICK_PER_SECOND * 5)
  78. #endif
  79. #ifndef RT_FOTA_SIGNAL_LED_PIN
  80. #define RT_FOTA_SIGNAL_LED_PIN 76
  81. #endif
  82. #ifndef RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE
  83. #define RT_FOTA_SIGNAL_LED_THREAD_STACK_SIZE 1024
  84. #endif
  85. #ifndef RT_FOTA_SIGNAL_LED_THREAD_PRIORITY
  86. #define RT_FOTA_SIGNAL_LED_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 4)
  87. #endif
  88. /* For signal led */
  89. static led_t *signal_led = NULL;
  90. static led_mem_opreation_t signal_led_mem_op;
  91. const char *led_shell_mode = "500,500,"; /* 1Hz 闪烁 */
  92. const char *led_upgrade_mode = "50,50,"; /* 10Hz 闪烁 */
  93. const char *led_off_mode = "0,100,"; /* 常灭 */
  94. const char *led_on_mode = "100,0,"; /* 常亮 */
  95. /* For shell */
  96. static rt_sem_t shell_sem = RT_NULL;
  97. static rt_device_t shell_dev = RT_NULL;
  98. typedef struct {
  99. char type[4];
  100. rt_uint16_t fota_algo;
  101. rt_uint8_t fm_time[6];
  102. char app_part_name[16];
  103. char download_version[24];
  104. char current_version[24];
  105. rt_uint32_t code_crc;
  106. rt_uint32_t hash_val;
  107. rt_uint32_t raw_size;
  108. rt_uint32_t com_size;
  109. rt_uint32_t head_crc;
  110. } rt_fota_part_head, *rt_fota_part_head_t;
  111. typedef void (*rt_fota_app_func)(void);
  112. static rt_fota_app_func app_func = RT_NULL;
  113. static rt_fota_part_head fota_part_head;
  114. static void rt_fota_signal_led_on(void)
  115. {
  116. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_LOW);
  117. }
  118. static void rt_fota_signal_led_off(void)
  119. {
  120. rt_pin_write(RT_FOTA_SIGNAL_LED_PIN, PIN_HIGH);
  121. }
  122. static void rt_fota_signal_led_entry(void *arg)
  123. {
  124. while(1)
  125. {
  126. led_ticks();
  127. rt_thread_mdelay(LED_TICK_TIME);
  128. }
  129. }
  130. static void rt_fota_signal_led_init(void)
  131. {
  132. rt_pin_mode(RT_FOTA_SIGNAL_LED_PIN, PIN_MODE_OUTPUT);
  133. signal_led_mem_op.malloc_fn = (void* (*)(size_t))rt_malloc;
  134. signal_led_mem_op.free_fn = rt_free;
  135. led_set_mem_operation(&signal_led_mem_op);
  136. signal_led = led_create(rt_fota_signal_led_on, rt_fota_signal_led_off);
  137. /* Config signal led mode */
  138. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_on_mode);
  139. led_set_blink_over_callback(signal_led, RT_NULL);
  140. led_start(signal_led);
  141. rt_thread_t tid;
  142. 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);
  143. if (tid)
  144. rt_thread_startup(tid);
  145. }
  146. static void rt_fota_signal_led_mode(const char *led_cfg)
  147. {
  148. RT_ASSERT(led_cfg != RT_NULL);
  149. led_set_mode(signal_led, LOOP_PERMANENT, (char *)led_cfg);
  150. }
  151. static int rt_fota_boot_verify(void)
  152. {
  153. int fota_res = RT_FOTA_NO_ERR;
  154. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  155. /* partition initial */
  156. fal_init();
  157. extern int fal_init_check(void);
  158. /* verify partition */
  159. if (fal_init_check() != 1)
  160. {
  161. PartitionInitFail
  162. LOG_D("Partition initialized failed!");
  163. fota_res = RT_FOTA_GENERAL_ERR;
  164. goto __exit_boot_verify;
  165. }
  166. __exit_boot_verify:
  167. return fota_res;
  168. }
  169. int rt_fota_part_fw_verify(const char *part_name)
  170. {
  171. #define RT_FOTA_CRC_BUFF_SIZE 4096
  172. #define RT_FOTA_CRC_INIT_VAL 0xffffffff
  173. int fota_res = RT_FOTA_NO_ERR;
  174. const struct fal_partition *part;
  175. rt_fota_part_head part_head;
  176. rt_uint8_t *body_buf = RT_NULL;
  177. rt_uint32_t body_crc = RT_FOTA_CRC_INIT_VAL;
  178. rt_uint32_t hdr_crc;
  179. if (part_name == RT_NULL)
  180. {
  181. LOG_D("Invaild paramenter input!");
  182. fota_res = RT_FOTA_GENERAL_ERR;
  183. goto __exit_partition_verify;
  184. }
  185. part = fal_partition_find(part_name);
  186. if (part == RT_NULL)
  187. {
  188. PartitionNotFound
  189. LOG_D("Partition[%s] not found.", part_name);
  190. fota_res = RT_FOTA_GENERAL_ERR;
  191. goto __exit_partition_verify;
  192. }
  193. /* read the head of RBL files */
  194. if (fal_partition_read(part, 0, (rt_uint8_t *)&part_head, sizeof(rt_fota_part_head)) < 0)
  195. {
  196. PartitionReadError //分区读取错误
  197. LOG_D("Partition[%s] read error!", part->name);
  198. fota_res = RT_FOTA_PART_READ_ERR;
  199. goto __exit_partition_verify;
  200. }
  201. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  202. hdr_crc = rt_fota_crc((rt_uint8_t *)&part_head, sizeof(rt_fota_part_head) - 4);
  203. if (hdr_crc != part_head.head_crc)
  204. {
  205. PartitionCRCError //分区数据校验错误
  206. LOG_W("Partition[%s] head CRC32 error!", part->name);
  207. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  208. goto __exit_partition_verify;
  209. }
  210. if (rt_strcmp(part_head.type, "RBL") != 0)
  211. {
  212. PartitionNotSurport //分区类型不支持
  213. LOG_W("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_W("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_W("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_W("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. PartitionIntegrityVerifyError //固件完整性校验失败
  254. LOG_W("Partition[%s] firmware integrity verify failed.", part->name);
  255. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  256. goto __exit_partition_verify;
  257. }
  258. char cache[32] = {0};
  259. __exit_partition_verify:
  260. if (fota_res == RT_FOTA_NO_ERR)
  261. {
  262. rt_enter_critical();
  263. rt_memcpy(&fota_part_head, &part_head, sizeof(rt_fota_part_head));
  264. rt_exit_critical();
  265. //PartitionVerifySuccess //固件校验成功
  266. sprintf(cache , "固件校验成功:%s" , part_name);
  267. fota_status_send( cache, strlen(cache));
  268. LOG_I("partition[%s] verify success!", part->name);
  269. }
  270. else
  271. {
  272. rt_enter_critical();
  273. rt_memset(&fota_part_head, 0x0, sizeof(rt_fota_part_head));
  274. rt_exit_critical();
  275. LOG_W("fota_res:%d", fota_res);
  276. //PartitionVerifyError //固件校验失败
  277. sprintf(cache , "固件校验成功:%s" , part_name);
  278. fota_status_send( cache, strlen(cache));
  279. LOG_W("Partition[%s] verify failed!", part->name);
  280. }
  281. if (body_buf)
  282. rt_free(body_buf);
  283. return fota_res;
  284. }
  285. int rt_fota_check_upgrade(void)
  286. {
  287. int is_upgrade = 0;
  288. rt_kprintf("download_version %s >> current_version %s \n" , fota_part_head.download_version, fota_part_head.current_version);
  289. if (rt_strcmp(fota_part_head.download_version, fota_part_head.current_version) != 0)
  290. {
  291. is_upgrade = 1;
  292. LOG_D("Application need upgrade.");
  293. goto __exit_check_upgrade;
  294. }
  295. __exit_check_upgrade:
  296. return is_upgrade;
  297. }
  298. int rt_fota_copy_version(const char *part_name)
  299. {
  300. #define THE_NOR_FLASH_GRANULARITY 4096
  301. int fota_res = RT_FOTA_NO_ERR;
  302. const struct fal_partition *part;
  303. rt_fota_part_head_t part_head = RT_NULL;
  304. rt_uint8_t *cache_buf = RT_NULL;
  305. part = fal_partition_find(part_name);
  306. if (part == RT_NULL)
  307. {
  308. LOG_D("Find partition[%s] not found.", part_name);
  309. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  310. goto __exit_copy_version;
  311. }
  312. cache_buf = rt_malloc(THE_NOR_FLASH_GRANULARITY);
  313. if (cache_buf == RT_NULL)
  314. {
  315. LOG_D("Not enough memory for head erase.");
  316. fota_res = RT_FOTA_NO_MEM_ERR;
  317. goto __exit_copy_version;
  318. }
  319. part_head = (rt_fota_part_head_t)cache_buf;
  320. if (fal_partition_read(part, 0, cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  321. {
  322. LOG_I("Read partition[%s] failed.", part_name);
  323. fota_res = RT_FOTA_PART_READ_ERR;
  324. goto __exit_copy_version;
  325. }
  326. rt_memcpy(part_head->current_version, part_head->download_version, sizeof(part_head->current_version));
  327. extern rt_uint32_t rt_fota_crc(rt_uint8_t *buf, rt_uint32_t len);
  328. part_head->head_crc = rt_fota_crc((rt_uint8_t *)part_head, sizeof(rt_fota_part_head) - 4);
  329. if (fal_partition_erase(part, 0, THE_NOR_FLASH_GRANULARITY) < 0)
  330. {
  331. LOG_D("Erase partition[%s] failed.", part_name);
  332. fota_res = RT_FOTA_PART_ERASE_ERR;
  333. goto __exit_copy_version;
  334. }
  335. if (fal_partition_write(part, 0, (const rt_uint8_t *)cache_buf, THE_NOR_FLASH_GRANULARITY) < 0)
  336. {
  337. LOG_I("Write partition[%s] failed.", part_name);
  338. fota_res = RT_FOTA_PART_WRITE_ERR;
  339. goto __exit_copy_version;
  340. }
  341. __exit_copy_version:
  342. if (cache_buf)
  343. rt_free(cache_buf);
  344. if (fota_res != RT_FOTA_NO_ERR)
  345. LOG_I("Copy firmware version failed!");
  346. else
  347. LOG_I("Copy firmware version Success!");
  348. return fota_res;
  349. }
  350. int rt_fota_erase_app_part(void)
  351. {
  352. int fota_res = RT_FOTA_NO_ERR;
  353. const struct fal_partition *part;
  354. part = fal_partition_find(fota_part_head.app_part_name);
  355. if (part == RT_NULL)
  356. {
  357. PartitionNotFound //分区未找到
  358. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  359. fota_res = RT_FOTA_FW_VERIFY_FAILED;
  360. goto __exit_partition_erase;
  361. }
  362. EraseStart //开始擦除分区
  363. LOG_I("Partition[%s] erase start:", part->name);
  364. if (fal_partition_erase(part, 0, fota_part_head.raw_size) < 0)
  365. {
  366. EraseFailed //擦除失败
  367. LOG_D("Partition[%s] erase failed!", part->name);
  368. fota_res = RT_FOTA_PART_ERASE_ERR;
  369. goto __exit_partition_erase;
  370. }
  371. __exit_partition_erase:
  372. if (fota_res == RT_FOTA_NO_ERR)
  373. {
  374. EraseSuccess //擦除成功
  375. LOG_D("Partition[%s] erase %d bytes success!", part->name, fota_part_head.raw_size);
  376. }
  377. return fota_res;
  378. }
  379. int rt_fota_write_app_part(int fw_pos, rt_uint8_t *fw_buf, int fw_len)
  380. {
  381. int rt_fota_res = RT_FOTA_NO_ERR;
  382. const struct fal_partition *part;
  383. part = fal_partition_find(fota_part_head.app_part_name);
  384. if (part == RT_NULL)
  385. {
  386. PartitionNotFound
  387. LOG_D("Erase partition[%s] not found.", fota_part_head.app_part_name);
  388. rt_fota_res = RT_FOTA_FW_VERIFY_FAILED;
  389. goto __partition_write_exit;
  390. }
  391. if (fal_partition_write(part, fw_pos, fw_buf, fw_len) < 0)
  392. {
  393. WriteFailed //写入失败
  394. LOG_D("Partition[%s] write failed!", part->name);
  395. rt_fota_res = RT_FOTA_PART_WRITE_ERR;
  396. goto __partition_write_exit;
  397. }
  398. __partition_write_exit:
  399. if (rt_fota_res == RT_FOTA_NO_ERR)
  400. {
  401. LOG_D("Partition[%s] write %d bytes success!", part->name, fw_len);
  402. }
  403. return rt_fota_res;
  404. }
  405. 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)
  406. {
  407. int fota_err = RT_FOTA_NO_ERR;
  408. rt_uint8_t *encrypt_buf = RT_NULL;
  409. if ((part == RT_NULL) || (decrypt_buf == RT_NULL)
  410. || (decrypt_len % 16 != 0) || (decrypt_len > RT_FOTA_ALGO_BUFF_SIZE))
  411. {
  412. fota_err = RT_FOTA_GENERAL_ERR;
  413. goto __exit_read_decrypt;
  414. }
  415. rt_memset(decrypt_buf, 0x0, decrypt_len);
  416. /* Not use AES256 algorithm */
  417. if (aes_ctx == RT_NULL || aes_iv == RT_NULL)
  418. {
  419. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, decrypt_buf, decrypt_len);
  420. if (fota_err <= 0)
  421. {
  422. fota_err = RT_FOTA_PART_READ_ERR;
  423. }
  424. goto __exit_read_decrypt;
  425. }
  426. encrypt_buf = rt_malloc(decrypt_len);
  427. if (encrypt_buf == RT_NULL)
  428. {
  429. fota_err = RT_FOTA_GENERAL_ERR;
  430. goto __exit_read_decrypt;
  431. }
  432. rt_memset(encrypt_buf, 0x0, decrypt_len);
  433. fota_err = fal_partition_read(part, sizeof(rt_fota_part_head) + read_pos, encrypt_buf, decrypt_len);
  434. if (fota_err <= 0 || fota_err % 16 != 0)
  435. {
  436. fota_err = RT_FOTA_PART_READ_ERR;
  437. goto __exit_read_decrypt;
  438. }
  439. // tiny_aes_crypt_cbc(aes_ctx, AES_DECRYPT, fota_err, aes_iv, encrypt_buf, decrypt_buf);
  440. __exit_read_decrypt:
  441. if (encrypt_buf)
  442. rt_free(encrypt_buf);
  443. return fota_err;
  444. }
  445. int rt_fota_upgrade(const char *part_name)
  446. {
  447. int fota_err = RT_FOTA_NO_ERR;
  448. const struct fal_partition *part;
  449. rt_fota_part_head_t part_head = RT_NULL;
  450. tiny_aes_context *aes_ctx = RT_NULL;
  451. rt_uint8_t *aes_iv = RT_NULL;
  452. rt_uint8_t *crypt_buf = RT_NULL;
  453. int fw_raw_pos = 0;
  454. int fw_raw_len = 0;
  455. rt_uint32_t total_copy_size = 0;
  456. rt_uint8_t block_hdr_buf[RT_FOTA_BLOCK_HEADER_SIZE];
  457. rt_uint32_t block_hdr_pos = RT_FOTA_ALGO_BUFF_SIZE;
  458. rt_uint32_t block_size = 0;
  459. rt_uint32_t dcprs_size = 0;
  460. // qlz_state_decompress *dcprs_state = RT_NULL;
  461. rt_uint8_t *cmprs_buff = RT_NULL;
  462. rt_uint8_t *dcprs_buff = RT_NULL;
  463. rt_uint32_t padding_size = 0;
  464. if (part_name == RT_NULL)
  465. {
  466. LOG_D("Invaild paramenter input!");
  467. fota_err = RT_FOTA_GENERAL_ERR;
  468. goto __exit_upgrade;
  469. }
  470. part = fal_partition_find(part_name);
  471. if (part == RT_NULL)
  472. {PartitionNotFound
  473. LOG_D("Upgrade partition[%s] not found.", part_name);
  474. fota_err = RT_FOTA_GENERAL_ERR;
  475. goto __exit_upgrade;
  476. }
  477. /* Application partition erase */
  478. fota_err = rt_fota_erase_app_part();
  479. if (fota_err != RT_FOTA_NO_ERR)
  480. {
  481. goto __exit_upgrade;
  482. }
  483. /* rt_fota_erase_app_part() has check fota_part_head vaild already */
  484. part_head = &fota_part_head;
  485. crypt_buf = rt_malloc(RT_FOTA_ALGO_BUFF_SIZE);
  486. if (crypt_buf == RT_NULL)
  487. {
  488. LOG_D("Not enough memory for firmware buffer.");
  489. fota_err = RT_FOTA_NO_MEM_ERR;
  490. goto __exit_upgrade;
  491. }
  492. if ((part_head->fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  493. {
  494. LOG_I("Not surpport XOR.");
  495. fota_err = RT_FOTA_GENERAL_ERR;
  496. goto __exit_upgrade;
  497. }
  498. else if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  499. {
  500. LOG_I("Not surpport GZIP.");
  501. fota_err = RT_FOTA_GENERAL_ERR;
  502. goto __exit_upgrade;
  503. }
  504. //按照输入分区名称判断显示
  505. if(rt_strcmp(part_name,"fm_area") == 0)Flash_Fw //正在刷写固件
  506. else if(rt_strcmp(part_name,"df_area") == 0)Recove_Fw //正在恢复出厂固件
  507. LOG_I("Start to copy firmware from %s to %s partition:", part->name, part_head->app_part_name);
  508. while (fw_raw_pos < part_head->com_size)
  509. {
  510. fw_raw_len = rt_fota_read_part(part, fw_raw_pos, aes_ctx, aes_iv, crypt_buf, RT_FOTA_ALGO_BUFF_SIZE);
  511. if (fw_raw_len < 0)
  512. {
  513. LOG_D("AES256 algorithm failed.");
  514. fota_err = RT_FOTA_PART_READ_ERR;
  515. goto __exit_upgrade;
  516. }
  517. fw_raw_pos += fw_raw_len;
  518. if (rt_fota_write_app_part(total_copy_size, crypt_buf, fw_raw_len) < 0)
  519. {
  520. fota_err = RT_FOTA_COPY_FAILED;
  521. goto __exit_upgrade;
  522. }
  523. total_copy_size += fw_raw_len;
  524. rt_kprintf("#");
  525. }
  526. /* it has compress option */
  527. if ((part_head->fota_algo & RT_FOTA_CMPRS_STAT_MASK) != RT_FOTA_CRYPT_ALGO_NONE)
  528. {
  529. while (total_copy_size < part_head->raw_size)
  530. {
  531. if ((block_hdr_pos < fw_raw_len) && ((fw_raw_len - block_hdr_pos) > RT_FOTA_BLOCK_HEADER_SIZE))
  532. {
  533. rt_memcpy(block_hdr_buf, &crypt_buf[block_hdr_pos], RT_FOTA_BLOCK_HEADER_SIZE);
  534. block_size = block_hdr_buf[0] * (1 << 24) + block_hdr_buf[1] * (1 << 16) + block_hdr_buf[2] * (1 << 8) + block_hdr_buf[3];
  535. if ((fw_raw_len - block_hdr_pos - RT_FOTA_BLOCK_HEADER_SIZE) >= block_size)
  536. {
  537. rt_memset(cmprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE + padding_size);
  538. rt_memcpy(cmprs_buff, &crypt_buf[block_hdr_pos + RT_FOTA_BLOCK_HEADER_SIZE], block_size);
  539. rt_memset(dcprs_buff, 0x0, RT_FOTA_CMPRS_BUFFER_SIZE);
  540. block_hdr_pos += (block_size + RT_FOTA_BLOCK_HEADER_SIZE);
  541. if (dcprs_size <= 0)
  542. {
  543. LOG_D("Decompress failed: %d.", dcprs_size);
  544. fota_err = RT_FOTA_GENERAL_ERR;
  545. goto __exit_upgrade;
  546. }
  547. if (rt_fota_write_app_part(total_copy_size, dcprs_buff, dcprs_size) < 0)
  548. {
  549. fota_err = RT_FOTA_COPY_FAILED;
  550. goto __exit_upgrade;
  551. }
  552. total_copy_size += dcprs_size;
  553. rt_kprintf("#");
  554. }
  555. else
  556. {
  557. break;
  558. }
  559. }
  560. else
  561. {
  562. break;
  563. }
  564. }
  565. }
  566. rt_kprintf("\r\n");
  567. /* 有可能两个值不相等,因为AES需要填充16字节整数,但最后的解密解压值的代码数量必须是大于等于raw_size */
  568. /* 比较好的方法是做一个校验,目前打包软件的HASH_CODE算法不知道 */
  569. if (total_copy_size < part_head->raw_size)
  570. {
  571. LOG_D("Decompress check failed.");
  572. fota_err = RT_FOTA_GENERAL_ERR;
  573. }
  574. __exit_upgrade:
  575. if (aes_ctx)
  576. rt_free(aes_ctx);
  577. if (aes_iv)
  578. rt_free(aes_iv);
  579. if (crypt_buf)
  580. rt_free(crypt_buf);
  581. if (cmprs_buff)
  582. rt_free(cmprs_buff);
  583. if (dcprs_buff)
  584. rt_free(dcprs_buff);
  585. // if (dcprs_state)
  586. // rt_free(dcprs_state);
  587. if (fota_err == RT_FOTA_NO_ERR)
  588. { UpgradeSuccess //固件升级成功
  589. LOG_I("Upgrade success, total %d bytes.", total_copy_size);
  590. }
  591. else UpgradeFail //固件升级失败
  592. return fota_err;
  593. }
  594. int rt_fota_start_application(void)
  595. {
  596. int fota_res = RT_FOTA_NO_ERR;
  597. const struct fal_partition *part;
  598. rt_uint32_t app_addr;
  599. part = fal_partition_find(RT_FOTA_APP_PART_NAME);
  600. if (part == RT_NULL)
  601. {
  602. LOG_D("Partition[%s] not found.", fota_part_head.app_part_name);
  603. fota_res = RT_FOTA_GENERAL_ERR;
  604. goto __exit_start_application;
  605. }
  606. app_addr = part->offset + 0x08000000;
  607. //判断是否为0x08XXXXXX.
  608. if (((*(__IO uint32_t *)(app_addr + 4)) & 0xff000000) != 0x08000000)
  609. {
  610. LOG_I("Illegal Flash code.");
  611. fota_res = RT_FOTA_GENERAL_ERR;
  612. goto __exit_start_application;
  613. }
  614. // 检查栈顶地址是否合法.
  615. if (((*(__IO uint32_t *)app_addr) & 0x2ffe0000) != 0x20000000)
  616. {
  617. LOG_I("Illegal Stack code.");
  618. fota_res = RT_FOTA_GENERAL_ERR;
  619. goto __exit_start_application;
  620. }
  621. StartApp //正在启动应用……
  622. rt_thread_mdelay(1000);
  623. rt_pin_mode(GET_PIN(E, 0), PIN_MODE_OUTPUT);
  624. rt_pin_write(GET_PIN(E, 0), PIN_LOW);
  625. rt_thread_mdelay(1000);
  626. rt_pin_write(GET_PIN(E, 0), PIN_HIGH);
  627. LOG_I("Implement application now.");
  628. __disable_irq();
  629. // for (IRQn_Type irq = WWDG_IRQn; irq <= FPU_IRQn; irq++)
  630. // {
  631. // HAL_NVIC_DisableIRQ(irq);
  632. // HAL_NVIC_ClearPendingIRQ(irq);
  633. // }
  634. //Resets the RCC clock configuration to the default reset state.
  635. HAL_RCC_DeInit();
  636. SysTick->CTRL = 0;
  637. SysTick->LOAD = 0;
  638. SysTick->VAL = 0;
  639. HAL_DeInit();
  640. //用户代码区第二个字为程序开始地址(复位地址)
  641. app_func = (rt_fota_app_func)*(__IO uint32_t *)(app_addr + 4);
  642. /* Configure main stack */
  643. __set_MSP(*(__IO uint32_t *)app_addr);
  644. /* jump to application */
  645. app_func();
  646. __exit_start_application:
  647. LOG_I("Implement application failed.");
  648. rt_thread_mdelay(1000);
  649. upgrade_df();//启动失败,从备份区恢复
  650. return fota_res;
  651. }
  652. static rt_err_t rt_fota_get_shell_key(void)
  653. {
  654. char ch;
  655. rt_err_t res = RT_EOK;
  656. rt_uint32_t timeout = RT_FOTA_GET_CHAR_WAITTIGN;
  657. rt_tick_t tick_start, tick_stop;
  658. RT_ASSERT(shell_dev != RT_NULL);
  659. RT_ASSERT(shell_sem != RT_NULL);
  660. rt_tick_set(0);
  661. tick_start = rt_tick_get();
  662. while (1)
  663. {
  664. if (rt_device_read(shell_dev, -1, &ch, 1) != 1)
  665. {
  666. if (rt_sem_take(shell_sem, timeout) != RT_EOK)
  667. {
  668. res = RT_ERROR;
  669. goto __exit_get_shell_key;
  670. }
  671. }
  672. if (ch == 0x0d)
  673. goto __exit_get_shell_key;
  674. tick_stop = rt_tick_get();
  675. if ((tick_stop - tick_start) > RT_FOTA_GET_CHAR_WAITTIGN)
  676. {
  677. res = RT_ERROR;
  678. goto __exit_get_shell_key;
  679. }
  680. timeout = RT_FOTA_GET_CHAR_WAITTIGN - tick_stop + tick_start;
  681. }
  682. __exit_get_shell_key:
  683. return res;
  684. }
  685. static rt_err_t rt_fota_rx_ind(rt_device_t dev, rt_size_t size)
  686. {
  687. RT_ASSERT(shell_sem != RT_NULL);
  688. /* release semaphore to let fota thread rx data */
  689. rt_sem_release(shell_sem);
  690. return RT_EOK;
  691. }
  692. static rt_err_t rt_fota_set_device(const char *device_name)
  693. {
  694. rt_device_t dev;
  695. RT_ASSERT(device_name != RT_NULL);
  696. dev = rt_device_find(device_name);
  697. if (dev == RT_NULL)
  698. {
  699. LOG_D("Can not find device: %s.", device_name);
  700. return RT_ERROR;
  701. }
  702. if (shell_sem)
  703. rt_sem_delete(shell_sem);
  704. shell_sem = rt_sem_create("shell_sem", 0, RT_IPC_FLAG_FIFO);
  705. if (shell_sem == RT_NULL)
  706. return RT_ERROR;
  707. if (dev == shell_dev)
  708. return RT_EOK;
  709. if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM) == RT_EOK)
  710. {
  711. if (shell_dev != RT_NULL)
  712. {
  713. /* close old finsh device */
  714. rt_device_close(shell_dev);
  715. rt_device_set_rx_indicate(shell_dev, RT_NULL);
  716. }
  717. shell_dev = dev;
  718. rt_device_set_rx_indicate(dev, rt_fota_rx_ind);
  719. LOG_D("Shell device %s open success.", device_name);
  720. return RT_EOK;
  721. }
  722. LOG_D("Shell device %s open failed.", device_name);
  723. return RT_ERROR;
  724. }
  725. #define RT_FOTA_DEFAULT_KEY_PIN GET_PIN(E, 15)
  726. #define RT_FOTA_DEFAULT_KEY_CHK_TIME 5
  727. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  728. static rt_err_t rt_fota_check_defalut_key(void)
  729. {
  730. int chk_idx;
  731. /* GPIO initialized */
  732. rt_pin_mode(RT_FOTA_DEFAULT_KEY_PIN, PIN_MODE_INPUT_PULLUP);
  733. /* Delay for power up */
  734. rt_thread_mdelay(500);
  735. if (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW)
  736. rt_kprintf("Default firmware key pressed:\n");
  737. /* Check GPIO status */
  738. for (chk_idx = 0; (rt_pin_read(RT_FOTA_DEFAULT_KEY_PIN) == PIN_LOW) && (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME); chk_idx++)
  739. {
  740. rt_thread_mdelay(RT_TICK_PER_SECOND);
  741. rt_kprintf(">");
  742. }
  743. if (chk_idx < RT_FOTA_DEFAULT_KEY_CHK_TIME)
  744. return RT_ERROR;
  745. else
  746. return RT_EOK;
  747. }
  748. #endif
  749. void rt_fota_thread_entry(void *arg)
  750. {
  751. int fota_err = RT_FOTA_NO_ERR;
  752. extern int finsh_system_init(void);
  753. /* Signal led initialized */
  754. rt_fota_signal_led_init();
  755. /* Partition initialized */
  756. fota_err = rt_fota_boot_verify();
  757. if (fota_err != RT_FOTA_NO_ERR)
  758. {
  759. LOG_I("Partition initialized failed.");
  760. }
  761. #if defined(RT_FOTA_DEFAULT_KEY_PIN) && defined(RT_FOTA_DEFAULT_KEY_CHK_TIME)
  762. /* Default key check */
  763. if (rt_fota_check_defalut_key() == RT_EOK)
  764. {
  765. /* enter to defalut key mode */
  766. rt_fota_signal_led_mode(led_upgrade_mode);
  767. // goto __exit_default_entry;
  768. goto __exit_shell_entry;
  769. }
  770. #endif
  771. /* Shell initialized */
  772. if (rt_fota_set_device(RT_CONSOLE_DEVICE_NAME) == RT_EOK)
  773. {
  774. rt_kprintf("Please press [Enter] key into shell mode in %d secs:\r\n", RT_FOTA_GET_CHAR_WAITTIGN / RT_TICK_PER_SECOND);
  775. if (rt_fota_get_shell_key() == RT_EOK)
  776. {
  777. goto __exit_shell_entry;
  778. }
  779. }
  780. else
  781. {
  782. LOG_I("Shell device config failed.");
  783. }
  784. /* Firmware partition verify */
  785. fota_err = rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME);
  786. if (fota_err != RT_FOTA_NO_ERR)
  787. goto __exit_boot_entry;
  788. /* Check upgrade status */
  789. if (rt_fota_check_upgrade() <= 0)
  790. goto __exit_boot_entry;
  791. /* enter to upgrade mode */
  792. rt_fota_signal_led_mode(led_upgrade_mode);
  793. /* Implement upgrade, copy firmware partition to app partition */
  794. fota_err = rt_fota_upgrade(RT_FOTA_FM_PART_NAME);
  795. if (fota_err != RT_FOTA_NO_ERR)
  796. goto __exit_boot_entry;
  797. /* Update new application verison in RBL file of firmware partition */
  798. fota_err = rt_fota_copy_version(RT_FOTA_FM_PART_NAME);
  799. if (fota_err != RT_FOTA_NO_ERR)
  800. goto __exit_boot_entry;
  801. __exit_boot_entry:
  802. /* Implement application */
  803. rt_fota_start_application();
  804. __exit_shell_entry:
  805. /* enter to shell mode */
  806. rt_fota_signal_led_mode(led_shell_mode);
  807. /* Implement shell */
  808. finsh_system_init();
  809. extern struct rt_event hmi_event;
  810. rt_event_send(&hmi_event, SWITCH_HMI);//切换到BootLoader显示页面
  811. rt_thread_mdelay(100);
  812. void display_firmware_info(void);
  813. display_firmware_info();
  814. Fw_mg_Ready //固件管理模式就绪
  815. }
  816. void rt_fota_init(void)
  817. {
  818. rt_thread_t tid;
  819. tid = rt_thread_create("rt-boot", rt_fota_thread_entry, RT_NULL, RT_FOTA_THREAD_STACK_SIZE, RT_FOTA_THREAD_PRIORITY, 10);
  820. if (tid != RT_NULL)
  821. {
  822. rt_thread_startup(tid);
  823. }
  824. else
  825. {
  826. LOG_I("rt-fota thread create failed.");
  827. }
  828. }
  829. void rt_fota_info(rt_uint8_t argc, char **argv)
  830. {
  831. char put_buf[24];
  832. char part_name[2][FAL_DEV_NAME_MAX] =
  833. {
  834. {RT_FOTA_FM_PART_NAME},
  835. {RT_FOTA_DF_PART_NAME}
  836. };
  837. const char* help_info[] =
  838. {
  839. [0] = "fota probe - probe RBL file of partiton",
  840. [1] = "fota show partition addr size - show 'size' bytes starting at 'addr'",
  841. [2] = "fota clone des_part src_part - clone src partition to des partiton",
  842. [3] = "fota exec - execute application program",
  843. [4] = "fota upgrade - execute upgrade program from fm_area",
  844. [5] = "fota upgrade_df - execute upgrade program from df_area",
  845. };
  846. if (argc < 2)
  847. {
  848. rt_kprintf("Usage:\n");
  849. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  850. {
  851. rt_kprintf("%s\n", help_info[i]);
  852. }
  853. rt_kprintf("\n");
  854. }
  855. else
  856. {
  857. const char *operator = argv[1];
  858. if (!rt_strcmp(operator, "probe"))
  859. {
  860. for (int i = 0; i < 2; i++)
  861. {
  862. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  863. {
  864. LOG_I("===== RBL of %s partition =====", &part_name[i][0]);
  865. LOG_I("| App partition name | %*.s |", 11, fota_part_head.app_part_name);
  866. rt_memset(put_buf, 0x0, sizeof(put_buf));
  867. // if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_AES256)
  868. // {
  869. // rt_strncpy(put_buf, " AES", 4);
  870. // }else
  871. if ((fota_part_head.fota_algo & RT_FOTA_CRYPT_STAT_MASK) == RT_FOTA_CRYPT_ALGO_XOR)
  872. {
  873. rt_strncpy(put_buf, " XOR", 4);
  874. }
  875. else
  876. {
  877. rt_strncpy(put_buf, "NONE", 4);
  878. }
  879. if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_GZIP)
  880. {
  881. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && GLZ", 7);
  882. }
  883. // else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_QUICKLZ)
  884. // {
  885. // rt_strncpy(&put_buf[rt_strlen(put_buf)], " && QLZ", 7);
  886. // }
  887. else if ((fota_part_head.fota_algo & RT_FOTA_CMPRS_STAT_MASK) == RT_FOTA_CMPRS_ALGO_FASTLZ)
  888. {
  889. rt_strncpy(&put_buf[rt_strlen(put_buf)], " && FLZ", 7);
  890. }
  891. if (rt_strlen(put_buf) <= 0)
  892. {
  893. rt_strncpy(put_buf, "None", 4);
  894. }
  895. LOG_I("| Algorithm mode | %*.s |", 11, put_buf);
  896. LOG_I("| Firmware version | %*.s |", 11, fota_part_head.download_version);
  897. LOG_I("| Code raw size | %11d |", fota_part_head.raw_size);
  898. LOG_I("| Code package size | %11d |", fota_part_head.com_size);
  899. LOG_I("| Build Timestamp | %11d |", *((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  900. }
  901. }
  902. }
  903. else if (!rt_strcmp(operator, "show"))
  904. {
  905. const struct fal_partition *part;
  906. const char *part_name = argv[2];
  907. rt_uint32_t addr = strtol(argv[3], NULL, 0);
  908. rt_uint32_t size = strtol(argv[4], NULL, 0);
  909. rt_uint8_t buf[16];
  910. part = fal_partition_find(part_name);
  911. if (part != RT_NULL)
  912. {
  913. while (size > 16)
  914. {
  915. fal_partition_read(part, addr, buf, 16);
  916. rt_kprintf("%08X: ", addr);
  917. for (int i = 0; i < 16; i++)
  918. {
  919. rt_kprintf("%02X ", buf[i]);
  920. }
  921. rt_kprintf("\n");
  922. size -= 16;
  923. addr += 16;
  924. }
  925. fal_partition_read(part, addr, buf, size);
  926. rt_kprintf("%08X: ", addr);
  927. for (int i = 0; i < size; i++)
  928. {
  929. rt_kprintf("%02X ", buf[i]);
  930. }
  931. rt_kprintf("\n");
  932. }
  933. else
  934. {
  935. rt_kprintf("%s partition is not exist!\n", part_name);
  936. }
  937. }
  938. else if (!rt_strcmp(operator, "clone"))
  939. {
  940. const char *dst_part_name = argv[2];
  941. const char *src_part_name = argv[3];
  942. const struct fal_partition *dst_part;
  943. const struct fal_partition *src_part;
  944. dst_part = fal_partition_find(dst_part_name);
  945. src_part = fal_partition_find(src_part_name);
  946. if (dst_part == RT_NULL || src_part == RT_NULL)
  947. {
  948. if (dst_part == RT_NULL)
  949. rt_kprintf("%s partition is not exist!\n", dst_part_name);
  950. if (src_part == RT_NULL)
  951. rt_kprintf("%s partition is not exist!\n", src_part_name);
  952. }
  953. else
  954. {
  955. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  956. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  957. {
  958. int clone_pos = 0;
  959. int clone_len = 0, clone_tol_len;
  960. rt_uint8_t *buf = rt_malloc(4096);
  961. if (dst_part->len < src_part->len)
  962. clone_tol_len = dst_part->len;
  963. else
  964. clone_tol_len = src_part->len;
  965. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  966. {
  967. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  968. if (clone_len < 0)
  969. {
  970. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  971. break;
  972. }
  973. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  974. {
  975. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  976. break;
  977. }
  978. rt_kprintf("#");
  979. clone_pos += clone_len;
  980. }
  981. if (clone_pos >= clone_tol_len)
  982. rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  983. else
  984. rt_kprintf("\nClone partition failed!\n");
  985. if (buf)
  986. rt_free(buf);
  987. }
  988. }
  989. }
  990. else if (!rt_strcmp(operator, "exec"))
  991. {
  992. rt_fota_start_application();
  993. }
  994. else if (!rt_strcmp(operator, "upgrade"))
  995. {
  996. if (rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME) == RT_FOTA_NO_ERR)
  997. {
  998. if (rt_fota_upgrade(RT_FOTA_FM_PART_NAME) == RT_FOTA_NO_ERR)
  999. {
  1000. rt_fota_start_application();
  1001. }
  1002. }
  1003. LOG_I("Boot application failed, entry shell mode.");
  1004. }
  1005. else if (!rt_strcmp(operator, "upgrade_df"))
  1006. {
  1007. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1008. {
  1009. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1010. {
  1011. rt_fota_start_application();
  1012. }
  1013. }
  1014. LOG_I("Boot application failed, entry shell mode.");
  1015. }
  1016. else
  1017. {
  1018. rt_kprintf("Usage:\n");
  1019. for (int i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  1020. {
  1021. rt_kprintf("%s\n", help_info[i]);
  1022. }
  1023. rt_kprintf("\n");
  1024. }
  1025. }
  1026. }
  1027. /**
  1028. * rt-fota />ymodem_ota
  1029. */
  1030. MSH_CMD_EXPORT_ALIAS(rt_fota_info, fota, Check RBL file of partition);
  1031. /******************自行增加子函数********************/
  1032. void upgrade_df(void)//恢复出厂固件
  1033. {
  1034. if (rt_fota_part_fw_verify(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1035. {
  1036. if (rt_fota_upgrade(RT_FOTA_DF_PART_NAME) == RT_FOTA_NO_ERR)
  1037. {
  1038. UpgradeSuccess
  1039. rt_thread_mdelay(1000);
  1040. rt_fota_start_application();
  1041. }
  1042. }UpgradeFail
  1043. LOG_I("upgrade_df failed, entry shell mode.");
  1044. }
  1045. void upgrade(void)//从fm区烧写固件
  1046. {
  1047. if (rt_fota_part_fw_verify(RT_FOTA_FM_PART_NAME) == RT_FOTA_NO_ERR)
  1048. {
  1049. if (rt_fota_upgrade(RT_FOTA_FM_PART_NAME) == RT_FOTA_NO_ERR)
  1050. {
  1051. /* 更新fw分区当前的应用程序版本号*/
  1052. if(rt_fota_copy_version(RT_FOTA_FM_PART_NAME) == RT_FOTA_NO_ERR)
  1053. {
  1054. UpgradeFail//显示升级失败
  1055. }
  1056. UpgradeSuccess//升级成功
  1057. goto __exit_boot_entry;
  1058. }
  1059. }
  1060. UpgradeFail//显示升级失败
  1061. LOG_I("upgrade_fw failed, entry shell mode.");
  1062. goto __exit_boot_entry;
  1063. __exit_boot_entry:
  1064. /* Implement application */
  1065. rt_thread_mdelay(1000); //延时一段时间保证状态显示效果
  1066. rt_fota_start_application();
  1067. }
  1068. #include "bsp_date.h"
  1069. rtc_time fm_compiletime;
  1070. rtc_time df_compiletime;
  1071. rtc_time time_convert(time_t timestamp)
  1072. {
  1073. rtc_time compiletime;
  1074. to_tm((timestamp + 8*60*60), &compiletime);/*把定时器的值转换为北京时间*/
  1075. /* 输出时间戳,公历时间 */
  1076. log_i("UNIX tamp = %d : %d-%d-%d-%0.2d:%0.2d:%0.2d\n",timestamp,
  1077. compiletime.tm_year, compiletime.tm_mon, compiletime.tm_mday,
  1078. compiletime.tm_hour, compiletime.tm_min, compiletime.tm_sec);
  1079. return compiletime;
  1080. }
  1081. extern void Display_fun_str(const char *position,char *str);
  1082. //显示版本号
  1083. void display_firmware_info(void)
  1084. {
  1085. char buf[100]= "\0";
  1086. char part_name[2][FAL_DEV_NAME_MAX] =
  1087. {
  1088. {RT_FOTA_FM_PART_NAME},
  1089. {RT_FOTA_DF_PART_NAME}
  1090. };
  1091. char intobl[] = "page bootloader";
  1092. Display_fun_str(intobl , NULL);
  1093. for (int i = 0; i < 2; i++)
  1094. {
  1095. if (rt_fota_part_fw_verify(&part_name[i][0]) == RT_FOTA_NO_ERR)
  1096. {
  1097. if(i == 0)
  1098. {
  1099. rt_thread_mdelay(200);
  1100. Display_fun_str("bootloader.t5.txt=\"当前固件版本:%-11s\"",fota_part_head.download_version);
  1101. fm_compiletime = time_convert(*((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1102. rt_memset(buf, '\0', 100);
  1103. rt_sprintf(buf,"%.2d/%.2d/%.2d %0.2d:%0.2d:%0.2d",
  1104. fm_compiletime.tm_year, fm_compiletime.tm_mon, fm_compiletime.tm_mday,
  1105. fm_compiletime.tm_hour, fm_compiletime.tm_min, fm_compiletime.tm_sec);
  1106. rt_thread_mdelay(500);
  1107. Display_fun_str("bootloader.t6.txt=\"编译日期:%s\"", buf);
  1108. }
  1109. else if(i == 1)
  1110. {
  1111. Display_fun_str("bootloader.t7.txt=\"出厂固件版本:%-11s\"",fota_part_head.download_version);
  1112. df_compiletime = time_convert(*((rt_uint32_t *)(&fota_part_head.fm_time[2])));
  1113. rt_memset(buf, '\0', 100);
  1114. rt_sprintf(buf,"%.2d/%.2d/%.2d %0.2d:%0.2d:%0.2d",
  1115. df_compiletime.tm_year, df_compiletime.tm_mon, df_compiletime.tm_mday,
  1116. df_compiletime.tm_hour, df_compiletime.tm_min, df_compiletime.tm_sec);
  1117. rt_thread_mdelay(500);
  1118. Display_fun_str("bootloader.t8.txt=\"编译日期:%s\"", buf);
  1119. }
  1120. rt_thread_mdelay(1000);
  1121. }
  1122. }
  1123. }
  1124. #include <easyflash.h>
  1125. uint8_t firstime_flag = 0;//第一次启动标志位
  1126. static void test_env(void)
  1127. {
  1128. uint32_t i_boot_times = 0;
  1129. size_t len = len;
  1130. len = ef_get_env_blob("boot_t", &i_boot_times, sizeof(i_boot_times), NULL);
  1131. i_boot_times++;
  1132. rt_kprintf("The system now boot %lu times\n\r", i_boot_times);
  1133. ef_set_env_blob("boot_t", &i_boot_times, sizeof(&i_boot_times));
  1134. ef_del_env("check_flag");
  1135. ef_set_env_blob("check_flag", &i_boot_times, sizeof(&i_boot_times));
  1136. }
  1137. void easyflash_initialize(void)
  1138. {
  1139. if(easyflash_init()==EF_NO_ERR) //初始化成功
  1140. {
  1141. ef_get_env_blob("firstime", &firstime_flag, sizeof(firstime_flag), NULL); //读出reboot_time的值
  1142. log_i("firstime:%d\n",firstime_flag);
  1143. test_env();
  1144. }
  1145. }
  1146. //第一次烧写程序,出厂备份区保存固件
  1147. void firstime_clone_df(void)
  1148. {
  1149. const char *dst_part_name = "df_area";
  1150. const char *src_part_name = "fm_area";
  1151. const struct fal_partition *dst_part;
  1152. const struct fal_partition *src_part;
  1153. dst_part = fal_partition_find(dst_part_name);
  1154. src_part = fal_partition_find(src_part_name);
  1155. if(firstime_flag == 0)//第一次启动标志
  1156. {
  1157. WritetoRecove //将固件转存入备份中
  1158. if (dst_part == RT_NULL || src_part == RT_NULL)
  1159. {
  1160. if (dst_part == RT_NULL)
  1161. {rt_kprintf("%s partition is not exist!\n", dst_part_name);
  1162. fota_status_send("备份分区错误", sizeof("备份分区错误"));}
  1163. if (src_part == RT_NULL)
  1164. {rt_kprintf("%s partition is not exist!\n", src_part_name);
  1165. fota_status_send("固件分区错误", sizeof("固件分区错误"));}
  1166. }
  1167. else
  1168. { fota_status_send("正在将固件存入备份分区", sizeof("正在将固件存入备份分区"));
  1169. rt_kprintf("Clone %s partition to %s partition:\n", src_part_name, dst_part_name);
  1170. if (fal_partition_erase(dst_part, 0, dst_part->len) >= 0)
  1171. {
  1172. int clone_pos = 0;
  1173. int clone_len = 0, clone_tol_len;
  1174. rt_uint8_t *buf = rt_malloc(4096);
  1175. if (dst_part->len < src_part->len)
  1176. clone_tol_len = dst_part->len;
  1177. else
  1178. clone_tol_len = src_part->len;
  1179. while ((clone_pos < clone_tol_len) && (buf != RT_NULL))
  1180. {
  1181. clone_len = fal_partition_read(src_part, clone_pos, buf, 4096);
  1182. if (clone_len < 0)
  1183. {
  1184. rt_kprintf("\nread %s partition failed, clone stop!\n", src_part_name);
  1185. break;
  1186. }
  1187. if (fal_partition_write(dst_part, clone_pos, buf, clone_len) < 0)
  1188. {
  1189. rt_kprintf("\nwrite %s partition failed, clone stop!\n", dst_part_name);
  1190. break;
  1191. }
  1192. rt_kprintf("#");
  1193. clone_pos += clone_len;
  1194. }
  1195. firstime_flag = 1;//固件保存成功后标志位改为1,并保存
  1196. ef_set_env_blob("firstime", &firstime_flag, sizeof(firstime_flag)); //写入reboot_time的值
  1197. log_i("firstime:%d\n",firstime_flag);
  1198. if (clone_pos >= clone_tol_len)
  1199. {rt_kprintf("\nClone partition success, total %d bytes!\n", clone_tol_len);
  1200. fota_status_send("固件存入备份分区成功", sizeof("固件存入备份分区成功"));}
  1201. else
  1202. {rt_kprintf("\nClone partition failed!\n");
  1203. fota_status_send("固件存入备份分区失败", sizeof("固件存入备份分区失败"));}
  1204. if (buf)rt_free(buf);
  1205. }
  1206. }
  1207. display_firmware_info();//刷新版本号显示
  1208. }
  1209. }