ota_upgrade.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief OTA upgrade interface
  9. */
  10. #include <kernel.h>
  11. #include <string.h>
  12. #include <device.h>
  13. #include <drivers/flash.h>
  14. #include <soc.h>
  15. #include <fw_version.h>
  16. #include <partition/partition.h>
  17. #include <mem_manager.h>
  18. #include <crc.h>
  19. #include <ota_upgrade.h>
  20. #include <ota_backend.h>
  21. #include "ota_image.h"
  22. #include <ota_storage.h>
  23. #include "ota_manifest.h"
  24. #include "ota_breakpoint.h"
  25. #include "ota_file_patch.h"
  26. #include <os_common_api.h>
  27. #include <drivers/nvram_config.h>
  28. #include <sys/ring_buffer.h>
  29. #include <ui_mem.h>
  30. #ifdef CONFIG_OTA_LZMA
  31. #include <minlzma.h>
  32. #endif
  33. #ifdef CONFIG_BT_MANAGER
  34. #include <bt_manager.h>
  35. #endif
  36. // full upgrade config (mbrec/param/recovery)
  37. // must set CONFIG_OTA_MUTIPLE_STORAGE=y for full upgrade
  38. #define OTA_FULL_UPGRADE (0)
  39. #define OTA_REQ_MAX_SIZE (256*1024)
  40. #define OTA_RX_STACKSIZE (1536)
  41. #ifdef CONFIG_UI_MEMORY_MANAGER
  42. #define OTA_RX_BUFSIZE (64*1024)
  43. #define OTA_IN_BUFSIZE (64*1024)
  44. #define OTA_OUT_BUFSIZE (32*1024)
  45. #else
  46. #define OTA_RX_BUFSIZE (8*1024)
  47. #define OTA_IN_BUFSIZE (4*1024)
  48. #define OTA_OUT_BUFSIZE (0)
  49. #endif
  50. #define OTA_MANIFESET_FILE_NAME "ota.xml"
  51. #define OTA_FLAG_USE_RECOVERY (1 << 0)
  52. #define OTA_FLAG_USE_RECOVERY_APP (1 << 1)
  53. #define OTA_FLAG_USE_NO_VERSION_CONTROL (1 << 2)
  54. #define OTA_FLAG_ERASE_PART_FOR_UPG (1 << 3)
  55. #define OTA_FLAG_KEEP_TEMP_PART (1 << 4)
  56. #define ota_use_recovery(ota) ((ota)->flags & OTA_FLAG_USE_RECOVERY)
  57. #define ota_use_recovery_app(ota) ((ota)->flags & OTA_FLAG_USE_RECOVERY_APP)
  58. #define ota_use_no_version_control(ota) ((ota)->flags & OTA_FLAG_USE_NO_VERSION_CONTROL)
  59. #define ota_erase_part_for_upg(ota) ((ota)->flags & OTA_FLAG_ERASE_PART_FOR_UPG)
  60. #define ota_keep_temp_part(ota) ((ota)->flags & OTA_FLAG_KEEP_TEMP_PART)
  61. #define EIO_READ (1001)
  62. struct ota_rx_info {
  63. char *rx_stack;
  64. uint8_t *rx_buf;
  65. uint32_t rx_bufsize;
  66. uint8_t *in_buf;
  67. uint32_t in_bufsize;
  68. uint8_t *out_buf;
  69. uint32_t out_bufsize;
  70. os_sem rx_get_sem;
  71. os_sem rx_put_sem;
  72. struct ring_buf rbuf;
  73. os_tid_t rx_tid;
  74. uint32_t offset;
  75. uint32_t size;
  76. uint32_t seg_size;
  77. bool is_raw;
  78. int rx_errno;
  79. };
  80. struct ota_upgrade_info {
  81. int state;
  82. int backend_type;
  83. unsigned int flags;
  84. ota_notify_t notify;
  85. ota_ugrade_file_cb file_cb;
  86. struct ota_image *img;
  87. struct ota_storage *storage;
  88. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  89. struct ota_storage *storage_ext;
  90. #endif
  91. int data_buf_size;
  92. uint8_t *data_buf;
  93. uint32_t xml_offset;
  94. struct ota_manifest manifest;
  95. struct ota_breakpoint bp;
  96. struct ota_rx_info rx_info;
  97. };
  98. #ifdef CONFIG_UI_MEMORY_MANAGER
  99. #define ota_rx_malloc ui_mem_res_alloc
  100. #define ota_rx_free ui_mem_res_free
  101. #else
  102. #define ota_rx_malloc mem_malloc
  103. #define ota_rx_free mem_free
  104. #endif
  105. static int ota_update_fw_version(struct ota_upgrade_info *ota, uint8_t file_id)
  106. {
  107. struct code_res_version code_res;
  108. bool need_save = false;
  109. if (file_id == PARTITION_FILE_ID_SDFS_PART_BASE ||
  110. file_id == PARTITION_FILE_ID_SDFS_PART1 ||
  111. file_id == PARTITION_FILE_ID_SDFS_PART2) {
  112. code_res.version_code = fw_version_get_code();
  113. code_res.version_res = ota->manifest.fw_ver.version_res;
  114. need_save = true;
  115. } else if (file_id == PARTITION_FILE_ID_SYSTEM) {
  116. code_res.version_res = fw_version_get_res();
  117. code_res.version_code = ota->manifest.fw_ver.version_code;
  118. need_save = true;
  119. }
  120. if (need_save) {
  121. if (nvram_config_set_factory(FIRMWARE_VERSION, &code_res, sizeof(struct code_res_version))) {
  122. SYS_LOG_ERR("SAVE FIRMWARE_VERSION FAILD\n");
  123. return -1;
  124. }
  125. }
  126. return 0;
  127. }
  128. static int ota_save_res_version(struct ota_upgrade_info *ota)
  129. {
  130. struct ota_manifest *manifest = &ota->manifest;
  131. struct ota_file *file;
  132. for (int i = 0; i < manifest->file_cnt; i++) {
  133. file = &manifest->wfiles[i];
  134. /*sdfs update need to save res version*/
  135. if (file->file_id == PARTITION_FILE_ID_SDFS_PART_BASE ||
  136. file->file_id == PARTITION_FILE_ID_SDFS_PART1 ||
  137. file->file_id == PARTITION_FILE_ID_SDFS_PART2) {
  138. /*update res version*/
  139. ota_update_fw_version(ota, file->file_id);
  140. break;
  141. }
  142. }
  143. return 0;
  144. }
  145. static void ota_upgrade_file_cb(struct ota_upgrade_info *ota, uint8_t file_id)
  146. {
  147. if (ota->file_cb) {
  148. ota->file_cb(file_id);
  149. }
  150. }
  151. static void ota_update_state(struct ota_upgrade_info *ota, enum ota_state state)
  152. {
  153. int old_state;
  154. SYS_LOG_INF("upadte ota state: %d\n", state);
  155. old_state = ota->state;
  156. ota->state = state;
  157. if (ota->notify) {
  158. ota->notify(state, old_state);
  159. }
  160. }
  161. static int ota_partition_erase_part(struct ota_upgrade_info *ota,
  162. const struct partition_entry *part,
  163. int start_offset)
  164. {
  165. int err, align_addr, align_size, is_clean;
  166. struct ota_storage *storage = ota->storage;
  167. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  168. storage = ota_storage_find(part->storage_id);
  169. if(storage == NULL) {
  170. SYS_LOG_INF("storage not init, update failed\n");
  171. return -EINVAL;
  172. }
  173. #endif
  174. SYS_LOG_INF("erase part %s: offset 0x%x size 0x%x, start_offset 0x%x\n",
  175. part->name, part->offset, part->size, start_offset);
  176. align_addr = ROUND_DOWN(part->offset + start_offset, OTA_ERASE_ALIGN_SIZE);
  177. align_size = ROUND_UP(part->size - start_offset, OTA_ERASE_ALIGN_SIZE);
  178. SYS_LOG_INF("erase aligned offset 0x%x, size 0x%x\n",
  179. align_addr, align_size);
  180. if (ota->data_buf && ota->data_buf_size) {
  181. is_clean = ota_storage_is_clean(storage, align_addr, align_size, ota->data_buf, ota->data_buf_size);
  182. if (is_clean == 1) {
  183. SYS_LOG_INF("part is clean\n");
  184. return 0;
  185. }
  186. }
  187. err = ota_storage_erase(storage, align_addr, align_size);
  188. if (err) {
  189. return err;
  190. }
  191. return 0;
  192. }
  193. static int ota_partition_update_prepare(struct ota_upgrade_info *ota)
  194. {
  195. struct ota_breakpoint *bp = &ota->bp;
  196. const struct partition_entry *part;
  197. int i, file_state, erase_offset;
  198. SYS_LOG_INF("bp->state %d", bp->state);
  199. if (bp->state == OTA_BP_STATE_CLEAN) {
  200. /* state is clean, skip erase */
  201. SYS_LOG_INF("bp state is clean, skip erase parts");
  202. return 0;
  203. }
  204. if (ota_use_recovery(ota)) {
  205. if (bp->state == OTA_BP_STATE_UPGRADE_PENDING) {
  206. /* state is clean, skip erase */
  207. SYS_LOG_INF("upgrade pending, skip erase parts");
  208. return 0;
  209. }
  210. /* don't erase temp part is upgrading is going, it will be erased before write file */
  211. if (bp->state == OTA_BP_STATE_UPGRADE_WRITING ||
  212. bp->state == OTA_BP_STATE_UPGRADING_FAIL) {
  213. SYS_LOG_INF("upgrade is in process, skip erase temp part");
  214. return 0;
  215. }
  216. }
  217. for (i = 0; i < MAX_PARTITION_COUNT; i++) {
  218. part = partition_get_part_by_id(i);
  219. if (part == NULL)
  220. return -EINVAL;
  221. if (part->file_id == 0)
  222. continue;
  223. if (!ota_use_recovery(ota)) {
  224. /* skip current firmware's partitions */
  225. if (!partition_is_mirror_part(part)) {
  226. SYS_LOG_INF("part[%d]: skip current used partition", i);
  227. continue;
  228. }
  229. } else {
  230. /* only temp partition need be erased when recovery is enabled */
  231. if (part->type != PARTITION_TYPE_TEMP &&
  232. part->file_id != PARTITION_FILE_ID_OTA_TEMP) {
  233. SYS_LOG_INF("part[%d]: file_id %d not ota temp partition, skip erase",
  234. i, part->file_id);
  235. continue;
  236. }
  237. /* don't erase partition that not in current storage */
  238. if (part->storage_id != ota_storage_get_storage_id(ota->storage)) {
  239. SYS_LOG_INF("part[%d]: part file_id %d storage_id %d not current storage_id, skip erase",
  240. i, part->file_id, part->storage_id);
  241. continue;
  242. }
  243. }
  244. if (bp->state == OTA_BP_STATE_UPGRADE_WRITING ||
  245. bp->state == OTA_BP_STATE_WRITING_IMG) {
  246. /* check breakpoint */
  247. file_state = ota_breakpoint_get_file_state(bp, part->file_id);
  248. SYS_LOG_INF("bp->state %d file_state %d", bp->state, file_state);
  249. if (file_state == OTA_BP_FILE_STATE_CLEAN ||
  250. file_state == OTA_BP_FILE_STATE_WRITE_DONE ||
  251. file_state == OTA_BP_FILE_STATE_VERIFY_PASS ||
  252. file_state == OTA_BP_FILE_STATE_WRITING_CLEAN) {
  253. SYS_LOG_INF("part[%d]: file_id %d file_state %d, skip erase",
  254. i, part->file_id, file_state);
  255. /* skip erase this partition */
  256. continue;
  257. } else if (file_state == OTA_BP_FILE_STATE_WRITING) {
  258. if (ota_use_recovery(ota) ||
  259. (bp->mirror_id == partition_get_current_mirror_id())) {
  260. /* parition is writing, not clean */
  261. erase_offset = bp->cur_file.offset + bp->cur_orig_write_offset;
  262. erase_offset &= ~(OTA_ERASE_ALIGN_SIZE - 1);
  263. erase_offset -= part->offset;
  264. SYS_LOG_INF("part[%d]: file_id %d writing",
  265. i, part->file_id);
  266. SYS_LOG_INF("file offset 0x%x, write_offset 0x%x, need erase from 0x%x",
  267. bp->cur_file.offset, bp->cur_orig_write_offset, erase_offset);
  268. /* update write offset aligned with erase sector */
  269. bp->cur_orig_write_offset = part->offset + erase_offset - bp->cur_file.offset;
  270. ota_partition_erase_part(ota, part, erase_offset);
  271. ota_breakpoint_set_file_state(bp, part->file_id, OTA_BP_FILE_STATE_WRITING_CLEAN);
  272. continue;
  273. }
  274. }
  275. }
  276. if (!ota_keep_temp_part(ota)) {
  277. ota_partition_erase_part(ota, part, 0);
  278. ota_breakpoint_set_file_state(bp, part->file_id, OTA_BP_FILE_STATE_CLEAN);
  279. }
  280. }
  281. if (bp->state != OTA_BP_STATE_UPGRADE_WRITING &&
  282. bp->state != OTA_BP_STATE_WRITING_IMG) {
  283. if (bp->state != OTA_BP_STATE_UNKOWN) {
  284. /* clear all old status */
  285. SYS_LOG_INF("clear old bp status");
  286. ota_breakpoint_init_default_value(&ota->bp);
  287. }
  288. bp->state = OTA_BP_STATE_CLEAN;
  289. SYS_LOG_INF("bp state is clean");
  290. }
  291. ota_breakpoint_save(bp);
  292. return 0;
  293. }
  294. static int ota_caculate_storage_file_crc(struct ota_upgrade_info *ota, struct ota_file *file)
  295. {
  296. struct ota_storage *storage = ota->storage;
  297. int addr, size, rlen;
  298. uint32_t crc;
  299. crc = 0;
  300. size = file->orig_size;
  301. addr = file->offset;
  302. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  303. storage = ota_storage_find(file->storage_id);
  304. #endif
  305. SYS_LOG_INF("check file %s: addr 0x%x, size 0x%x",
  306. file->name, addr, size);
  307. rlen = ota->data_buf_size;
  308. while (size > 0) {
  309. if (size < rlen)
  310. rlen = size;
  311. ota_storage_read(storage, addr, ota->data_buf, rlen);
  312. crc = utils_crc32(crc, ota->data_buf, rlen);
  313. size -= rlen;
  314. addr += rlen;
  315. }
  316. return crc;
  317. }
  318. static int ota_verify_file(struct ota_upgrade_info *ota, struct ota_file *file)
  319. {
  320. uint32_t crc_calc, crc_orig;
  321. // FIXME
  322. //if (file->file_id != PARTITION_FILE_ID_OTA_TEMP)
  323. //{
  324. crc_calc = ota_caculate_storage_file_crc(ota, file);
  325. crc_orig = file->checksum;
  326. SYS_LOG_INF("check file %s: crc_orig 0x%x, crc_calc 0x%x",
  327. file->name, crc_orig, crc_calc);
  328. if (crc_calc != crc_orig) {
  329. return -1;
  330. }
  331. //}
  332. return 0;
  333. }
  334. #if 0
  335. //#ifdef CONFIG_OTA_RES_PATCH
  336. static int ota_is_patch_res(struct ota_upgrade_info *ota)
  337. {
  338. const struct fw_version *old_fw_ver = &ota->manifest.old_fw_ver;
  339. return (old_fw_ver->version_res == 0) ? 0 : 1;
  340. }
  341. #endif
  342. #ifdef CONFIG_OTA_FILE_PATCH
  343. static int ota_is_patch_fw(struct ota_upgrade_info *ota)
  344. {
  345. const struct fw_version *old_fw_ver = &ota->manifest.old_fw_ver;
  346. return (old_fw_ver->version_code == 0) ? 0 : 1;
  347. }
  348. static int ota_write_file_by_patch(struct ota_upgrade_info *ota, struct ota_file *file, int start_file_offs)
  349. {
  350. struct ota_image *img = ota->img;
  351. struct ota_storage *storage = ota->storage;
  352. unsigned int img_file_offset;
  353. int err, is_clean, patch_file_size;
  354. uint32_t start_time, consume_time;
  355. struct ota_file_patch_info file_patch;
  356. const struct partition_entry *part;
  357. void *mapping_addr;
  358. SYS_LOG_INF("write file %s by patch to offset 0x%x", file->name, file->offset);
  359. start_time = k_uptime_get_32();
  360. if (start_file_offs != 0) {
  361. SYS_LOG_ERR("cannot support breakpoint for file patch by now");
  362. return -EINVAL;
  363. }
  364. img_file_offset = ota_image_get_file_offset(img, file->name);
  365. if (img_file_offset < 0) {
  366. SYS_LOG_ERR("cannot found file %s in image", file->name);
  367. return -EINVAL;
  368. }
  369. patch_file_size = ota_image_get_file_length(img, file->name);
  370. part = partition_get_part(file->file_id);
  371. if (part == NULL)
  372. return -EINVAL;
  373. /* check empty */
  374. os_printk("file->size 0x%x, ota->data_buf %p, data_buf_size 0x%x, part->flag 0x%x\n",
  375. file->size, ota->data_buf, ota->data_buf_size, part->flag);
  376. is_clean = ota_storage_is_clean(storage, file->offset, file->size,
  377. ota->data_buf, ota->data_buf_size);
  378. if (is_clean != 1) {
  379. SYS_LOG_ERR("storage is not clean, offs 0x%x size 0x%x", file->offset, file->size);
  380. return -EINVAL;
  381. }
  382. ota_breakpoint_update_file_state(&ota->bp, file, OTA_BP_FILE_STATE_WRITING_DIRTY, 0, 0, 0);
  383. mapping_addr = soc_memctrl_create_temp_mapping(part->file_offset, part->flag & PARTITION_FLAG_ENABLE_CRC);
  384. memset(&file_patch, 0x0, sizeof(struct ota_file_patch_info));
  385. file_patch.img = img;
  386. file_patch.storage = storage;
  387. file_patch.old_file_mapping_addr = mapping_addr;
  388. file_patch.old_file_offset = part->file_offset;
  389. file_patch.old_file_size = part->size;
  390. file_patch.new_file_offset = file->offset; // + start_file_offs;
  391. file_patch.new_file_size = file->size;
  392. file_patch.patch_file_offset = img_file_offset;
  393. file_patch.patch_file_size = patch_file_size;
  394. file_patch.flag_use_crc = (part->flag & PARTITION_FLAG_ENABLE_CRC) ? 1 : 0;
  395. file_patch.flag_use_encrypt = (part->flag & PARTITION_FLAG_ENABLE_ENCRYPTION) ? 1 : 0;
  396. file_patch.write_cache = ota->data_buf;
  397. file_patch.write_cache_size = 0x22;
  398. file_patch.write_cache_offs = 0;
  399. file_patch.write_cache_pos = 0;
  400. err = ota_file_patch_write(&file_patch);
  401. if (err) {
  402. SYS_LOG_ERR("storage write failed, offs 0x%x size 0x%x", file->offset, file->size);
  403. return -EIO;
  404. }
  405. consume_time = k_uptime_get_32() - start_time + 1;
  406. SYS_LOG_INF("write file %s: length %d KB patch size(%d KB), consume %d ms, %d KB/s\n",
  407. file->name, file->size / 1024, patch_file_size / 1024,
  408. consume_time, file->size / consume_time);
  409. soc_memctrl_clear_temp_mapping(mapping_addr);
  410. return 0;
  411. }
  412. #endif
  413. static void ota_rx_thread(void *p1, void *p2, void *p3)
  414. {
  415. struct ota_upgrade_info *ota = (struct ota_upgrade_info *)p1;
  416. struct ota_backend *backend = ota_image_get_backend(ota->img);
  417. struct ota_rx_info *rx_info = &ota->rx_info;
  418. bool is_bt_backend;
  419. int err, rlen, req_size, max_req_size;
  420. SYS_LOG_INF("ota_rx thread started");
  421. is_bt_backend = (ota_backend_get_type(backend) == OTA_BACKEND_TYPE_BLUETOOTH);
  422. max_req_size = OTA_REQ_MAX_SIZE;
  423. ota_backend_ioctl(backend, OTA_BACKEND_IOCTL_GET_MAX_SIZE, (unsigned int)&max_req_size);
  424. while ((rx_info->size > 0) && (rx_info->rx_errno == 0)) {
  425. if (rx_info->is_raw) { //raw file
  426. req_size = rx_info->size;
  427. } else { //lzma file
  428. while ((req_size = ring_buf_space_get(&rx_info->rbuf)) == 0) {
  429. SYS_LOG_INF("ota_rx wait rbuf");
  430. os_sem_take(&rx_info->rx_put_sem, OS_FOREVER);
  431. }
  432. if (req_size > rx_info->size) {
  433. req_size = rx_info->size;
  434. }
  435. }
  436. if (req_size > max_req_size) {
  437. req_size = max_req_size;
  438. }
  439. if (is_bt_backend) {
  440. err = ota_image_read_prepare(ota->img, rx_info->offset, ota->data_buf, req_size);
  441. if (err) {
  442. SYS_LOG_ERR("cannot read data, offs 0x%x", rx_info->offset);
  443. rx_info->rx_errno = -EAGAIN;
  444. }
  445. }
  446. while (req_size > 0) {
  447. if (req_size < rx_info->seg_size) {
  448. rlen = req_size;
  449. } else {
  450. rlen = rx_info->seg_size;
  451. }
  452. if (is_bt_backend) {
  453. err = ota_image_read_complete(ota->img, rx_info->offset, ota->data_buf, rlen);
  454. } else {
  455. err = ota_image_read(ota->img, rx_info->offset, ota->data_buf, rlen);
  456. }
  457. if (err) {
  458. SYS_LOG_ERR("cannot read data, offs 0x%x", rx_info->offset);
  459. rx_info->rx_errno = -EAGAIN;
  460. break;
  461. }
  462. while (ring_buf_space_get(&rx_info->rbuf) < rlen) {
  463. SYS_LOG_INF("ota_rx wait rbuf");
  464. os_sem_take(&rx_info->rx_put_sem, OS_FOREVER);
  465. }
  466. ring_buf_put(&rx_info->rbuf, (const uint8_t *)ota->data_buf, rlen);
  467. os_sem_give(&rx_info->rx_get_sem);
  468. req_size -= rlen;
  469. rx_info->offset += rlen;
  470. rx_info->size -= rlen;
  471. }
  472. }
  473. SYS_LOG_INF("ota_rx thread exited");
  474. os_sem_give(&rx_info->rx_get_sem);
  475. }
  476. static int ota_rx_init(struct ota_upgrade_info *ota)
  477. {
  478. struct ota_rx_info *rx_info = &ota->rx_info;
  479. rx_info->rx_stack = mem_malloc(OTA_RX_STACKSIZE);
  480. if (rx_info->rx_stack == NULL) {
  481. SYS_LOG_ERR("failed to allocate %d bytes", OTA_RX_STACKSIZE);
  482. return -EINVAL;
  483. }
  484. rx_info->rx_buf = ota_rx_malloc(OTA_RX_BUFSIZE);
  485. if (rx_info->rx_buf == NULL) {
  486. SYS_LOG_ERR("failed to allocate %d bytes", OTA_RX_BUFSIZE);
  487. return -EINVAL;
  488. }
  489. rx_info->rx_bufsize = OTA_RX_BUFSIZE;
  490. rx_info->in_buf = ota_rx_malloc(OTA_IN_BUFSIZE);
  491. if (rx_info->in_buf == NULL) {
  492. SYS_LOG_ERR("failed to allocate %d bytes", OTA_IN_BUFSIZE);
  493. return -EINVAL;
  494. }
  495. rx_info->in_bufsize = OTA_IN_BUFSIZE;
  496. #if OTA_OUT_BUFSIZE > 0
  497. rx_info->out_buf = ota_rx_malloc(OTA_OUT_BUFSIZE);
  498. if (rx_info->out_buf != NULL) {
  499. rx_info->out_bufsize = OTA_OUT_BUFSIZE;
  500. } else {
  501. SYS_LOG_ERR("failed to allocate %d bytes", OTA_OUT_BUFSIZE);
  502. }
  503. #endif
  504. os_sem_init(&rx_info->rx_get_sem, 0, 5);
  505. os_sem_init(&rx_info->rx_put_sem, 0, 1);
  506. ring_buf_init(&rx_info->rbuf, rx_info->rx_bufsize, rx_info->rx_buf);
  507. return 0;
  508. }
  509. static int ota_rx_exit(struct ota_upgrade_info *ota)
  510. {
  511. struct ota_rx_info *rx_info = &ota->rx_info;
  512. if (rx_info->rx_stack != NULL) {
  513. mem_free(rx_info->rx_stack);
  514. rx_info->rx_stack = NULL;
  515. }
  516. if (rx_info->rx_buf != NULL) {
  517. ota_rx_free(rx_info->rx_buf);
  518. rx_info->rx_buf = NULL;
  519. rx_info->rx_bufsize = 0;
  520. }
  521. if (rx_info->in_buf != NULL) {
  522. ota_rx_free(rx_info->in_buf);
  523. rx_info->in_buf = NULL;
  524. rx_info->in_bufsize = 0;
  525. }
  526. if (rx_info->out_buf != NULL) {
  527. ota_rx_free(rx_info->out_buf);
  528. rx_info->out_buf = NULL;
  529. rx_info->out_bufsize = 0;
  530. }
  531. return 0;
  532. }
  533. static void ota_rx_start(struct ota_upgrade_info *ota, uint32_t offset,
  534. uint32_t size, uint32_t seg_size, bool is_raw)
  535. {
  536. struct ota_rx_info *rx_info = &ota->rx_info;
  537. char *stack_ptr;
  538. rx_info->offset = offset;
  539. rx_info->size = size;
  540. rx_info->seg_size = seg_size;
  541. rx_info->is_raw = is_raw;
  542. rx_info->rx_errno = 0;
  543. os_sem_reset(&rx_info->rx_get_sem);
  544. os_sem_reset(&rx_info->rx_put_sem);
  545. ring_buf_reset(&rx_info->rbuf);
  546. stack_ptr = (char *)ROUND_UP(rx_info->rx_stack, ARCH_STACK_PTR_ALIGN);
  547. rx_info->rx_tid = (os_tid_t)os_thread_create(stack_ptr, OTA_RX_STACKSIZE, ota_rx_thread,
  548. ota, NULL, NULL, 3, 0, OS_NO_WAIT);
  549. os_thread_name_set(rx_info->rx_tid, "ota_rx");
  550. }
  551. static void ota_rx_stop(struct ota_upgrade_info *ota)
  552. {
  553. struct ota_rx_info *rx_info = &ota->rx_info;
  554. k_thread_join(rx_info->rx_tid, K_MSEC(5000));
  555. }
  556. static int ota_write_file_normal(struct ota_upgrade_info *ota, struct ota_file *file,
  557. int start_file_offs, int start_orig_offs)
  558. {
  559. struct ota_image *img = ota->img;
  560. struct ota_storage *storage = ota->storage;
  561. struct ota_breakpoint *bp = &ota->bp;
  562. struct ota_rx_info *rx_info = &ota->rx_info;
  563. struct ota_backend *backend;
  564. unsigned int offs, file_offs;
  565. int img_file_offset;
  566. int ret, seg_size, unit_size, wlen, in_size, out_size;
  567. uint32_t start_time, consume_time, ts_start, ts_cost;
  568. bool is_record = false, no_wait = false;
  569. uint8_t *out_buf;
  570. lzma_head_t lzma_h = {0};
  571. bool is_raw = (file->size == file->orig_size);
  572. SYS_LOG_INF("write file %s size 0x%x(0x%x) to offset 0x%x start_offset 0x%x(0x%x)",
  573. file->name, file->size, file->orig_size, file->offset, start_file_offs, start_orig_offs);
  574. start_time = k_uptime_get_32();
  575. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  576. storage = ota_storage_find(file->storage_id);
  577. if(storage == NULL) {
  578. SYS_LOG_INF("storage not init, update failed\n");
  579. return -EINVAL;
  580. }
  581. #endif
  582. if ((start_file_offs >= file->size) || (start_orig_offs >= file->orig_size)) {
  583. SYS_LOG_ERR("file %s: start file offs 0x%x(0x%x) > file size 0x%x(0x%x)",
  584. file->name, start_file_offs, start_orig_offs, file->size, file->orig_size);
  585. return -EINVAL;
  586. }
  587. file_offs = start_file_offs;
  588. offs = start_orig_offs;
  589. if (strlen(file->name) == 0) {
  590. img_file_offset = ota_image_get_file_offset(img, NULL);
  591. } else {
  592. img_file_offset = ota_image_get_file_offset(img, file->name);
  593. }
  594. if (img_file_offset < 0) {
  595. SYS_LOG_ERR("cannot found file %s in image", file->name);
  596. return -EINVAL;
  597. }
  598. wlen = file->orig_size - offs;
  599. backend = ota_image_get_backend(img);
  600. unit_size = OTA_ERASE_ALIGN_SIZE;
  601. ota_backend_ioctl(backend, OTA_BACKEND_IOCTL_GET_UNIT_SIZE, (unsigned int)&unit_size);
  602. seg_size = (ota->data_buf_size / unit_size) * unit_size;
  603. /* clear nvram to avoid erase */
  604. nvram_config_clear(CONFIG_NVRAM_USER_REGION_SEGMENT_SIZE);
  605. ota_rx_start(ota, img_file_offset + file_offs, file->size - file_offs, seg_size, is_raw);
  606. while (wlen > 0) {
  607. if (!no_wait) {
  608. os_sem_take(&rx_info->rx_get_sem, OS_FOREVER);
  609. if (rx_info->rx_errno) {
  610. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_WRITING, file_offs, offs, 1);
  611. return rx_info->rx_errno;
  612. }
  613. }
  614. no_wait = false;
  615. in_size = ring_buf_size_get(&rx_info->rbuf);
  616. //os_printk("rbuf size 0x%x\n", in_size);
  617. if (in_size > rx_info->in_bufsize) {
  618. if (in_size >= rx_info->in_bufsize * 2) {
  619. no_wait = true;
  620. }
  621. in_size = rx_info->in_bufsize;
  622. }
  623. if (is_raw) { // raw file
  624. if (in_size < wlen) {
  625. if (in_size < OTA_ERASE_ALIGN_SIZE) {
  626. continue;
  627. }
  628. in_size = ROUND_DOWN(in_size, OTA_ERASE_ALIGN_SIZE);
  629. }
  630. } else { // lzma file
  631. if (lzma_h.ih_magic != LZMA_MAGIC) {
  632. if (in_size < sizeof(lzma_head_t)) {
  633. continue;
  634. }
  635. ring_buf_get(&rx_info->rbuf, (uint8_t*)&lzma_h, sizeof(lzma_head_t));
  636. os_sem_give(&rx_info->rx_put_sem);
  637. if (lzma_h.ih_magic != LZMA_MAGIC) {
  638. SYS_LOG_ERR("lzma error magic: 0x%x", lzma_h.ih_magic);
  639. return -EAGAIN;
  640. }
  641. if (rx_info->in_bufsize < lzma_h.ih_img_size) {
  642. SYS_LOG_ERR("XzDecode error! in_bufsize 0x%x < 0x%x", rx_info->in_bufsize, lzma_h.ih_img_size);
  643. return -EINVAL;
  644. }
  645. if (rx_info->out_bufsize < lzma_h.ih_org_size) {
  646. SYS_LOG_ERR("XzDecode error! out_bufsize 0x%x < 0x%x", rx_info->out_bufsize, lzma_h.ih_org_size);
  647. return -EINVAL;
  648. }
  649. in_size -= sizeof(lzma_head_t);
  650. }
  651. if (in_size < lzma_h.ih_img_size) {
  652. continue;
  653. }
  654. if (in_size >= (lzma_h.ih_img_size + sizeof(lzma_head_t))) {
  655. no_wait = true;
  656. }
  657. in_size = lzma_h.ih_img_size;
  658. }
  659. if (!is_record) {
  660. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_WRITING, file_offs, offs, 1);
  661. is_record = true;
  662. } else {
  663. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_WRITING, file_offs, offs, 0);
  664. }
  665. ret = ring_buf_get(&rx_info->rbuf, rx_info->in_buf, in_size);
  666. os_sem_give(&rx_info->rx_put_sem);
  667. if (ret != in_size) {
  668. SYS_LOG_ERR("ring buf get failed, size 0x%x", ret);
  669. return -EAGAIN;
  670. }
  671. if (is_raw) { // raw file
  672. out_buf = rx_info->in_buf;
  673. out_size = in_size;
  674. } else { // lzma file
  675. ts_start = k_uptime_get_32();
  676. if (rx_info->out_buf == NULL) {
  677. SYS_LOG_ERR("XzDecode error! out_buf is NULL");
  678. return -EINVAL;
  679. }
  680. // decompress lzma block
  681. out_size = OTA_OUT_BUFSIZE;
  682. #ifdef CONFIG_OTA_LZMA
  683. ret = XzDecode(rx_info->in_buf, in_size, rx_info->out_buf, &out_size);
  684. #else
  685. ret = 0;
  686. #endif
  687. if (ret == 0) {
  688. SYS_LOG_ERR("XzDecode error! size 0x%x", out_size);
  689. return -EAGAIN;
  690. }
  691. ts_cost = k_uptime_get_32() - ts_start + 1;
  692. os_printk("XzDecode 0x%x->0x%x (%d ms)\n", in_size, out_size, ts_cost);
  693. // check origin size
  694. if (out_size != lzma_h.ih_org_size) {
  695. SYS_LOG_ERR("XzDecode out_size mismatch! 0x%x", out_size);
  696. return -EAGAIN;
  697. }
  698. out_buf = rx_info->out_buf;
  699. lzma_h.ih_magic = 0;
  700. }
  701. ts_start = k_uptime_get_32();
  702. ret = ota_storage_write(storage, file->offset + offs, out_buf, out_size);
  703. if (ret) {
  704. SYS_LOG_ERR("storage write failed, offs 0x%x", offs);
  705. return -EIO;
  706. }
  707. ts_cost = k_uptime_get_32() - ts_start;
  708. os_printk("write 0x%x -> 0x%x(0x%x) (%d ms)\n", file_offs, offs, out_size, ts_cost);
  709. file_offs += (file->size != file->orig_size) ? in_size + sizeof(lzma_head_t) : in_size;
  710. offs += out_size;
  711. wlen -= out_size;
  712. }
  713. consume_time = k_uptime_get_32() - start_time + 1;
  714. SYS_LOG_INF("write file %s: length %d KB, consume %d ms, %d KB/s\n", file->name, file->size / 1024,
  715. consume_time, file->size / consume_time);
  716. ota_rx_stop(ota);
  717. return 0;
  718. }
  719. static int ota_write_file(struct ota_upgrade_info *ota, struct ota_file *file,
  720. int start_file_offs, int start_orig_offs)
  721. {
  722. #ifdef CONFIG_OTA_FILE_PATCH
  723. if (ota_is_patch_fw(ota)) {
  724. return ota_write_file_by_patch(ota, file, start_file_offs);
  725. } else {
  726. #endif
  727. return ota_write_file_normal(ota, file, start_file_offs, start_orig_offs);
  728. #ifdef CONFIG_OTA_FILE_PATCH
  729. }
  730. #endif
  731. }
  732. static int ota_write_and_verify_file(struct ota_upgrade_info *ota,
  733. const struct partition_entry *part,
  734. struct ota_file *file, bool need_verify)
  735. {
  736. struct ota_breakpoint *bp = &ota->bp;
  737. struct ota_storage *storage = ota->storage;
  738. int bp_file_state, bp_file_offset = 0, bp_orig_offset = 0;
  739. int err = 0, cur_storage_id, need_erase = 0;
  740. bp_file_state = ota_breakpoint_get_file_state(bp, file->file_id);
  741. SYS_LOG_INF("file %s: file_id %d, bp file state %d",
  742. file->name, file->file_id, bp_file_state);
  743. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  744. storage = ota_storage_find(file->storage_id);
  745. if(storage == NULL) {
  746. SYS_LOG_INF("storage not init, update failed\n");
  747. err = -EINVAL;
  748. goto failed;
  749. }
  750. #endif
  751. switch (bp_file_state) {
  752. case OTA_BP_FILE_STATE_WRITE_DONE:
  753. case OTA_BP_FILE_STATE_VERIFY_PASS:
  754. SYS_LOG_INF("file %s: file_id %d, already write done\n",
  755. file->name, file->file_id);
  756. break;
  757. case OTA_BP_FILE_STATE_CLEAN:
  758. SYS_LOG_INF("file %s: file_id %d, part is clean\n",
  759. file->name, file->file_id);
  760. break;
  761. case OTA_BP_FILE_STATE_WRITING_CLEAN:
  762. SYS_LOG_INF("file %s: file_id %d, part is writing clean, write offset 0x%x(0x%x)\n",
  763. file->name, file->file_id, bp->cur_file_write_offset, bp->cur_orig_write_offset);
  764. bp_file_offset = bp->cur_file_write_offset;
  765. bp_orig_offset = bp->cur_orig_write_offset;
  766. break;
  767. case OTA_BP_FILE_STATE_WRITING:
  768. SYS_LOG_INF("file %s: file_id %d, part is writing not clean! , write_offset 0x%x(0x%x)\n",
  769. file->name, file->file_id, bp->cur_file_write_offset, bp->cur_orig_write_offset);
  770. bp_file_offset = bp->cur_file_write_offset;
  771. bp_orig_offset = bp->cur_orig_write_offset;
  772. need_erase = 1;
  773. break;
  774. default:
  775. SYS_LOG_INF("file %s: file_id %d, write offset 0 by default\n",
  776. file->name, file->file_id);
  777. need_erase = 1;
  778. break;
  779. }
  780. cur_storage_id = ota_storage_get_storage_id(storage);
  781. if (part->storage_id != cur_storage_id) {
  782. SYS_LOG_ERR("BUG: file_id %d storage_id %d not current storage_id %d",
  783. part->file_id, part->storage_id, cur_storage_id);
  784. err = -EINVAL;
  785. goto failed;
  786. }
  787. if (ota_use_recovery(ota) || ota_erase_part_for_upg(ota)) {
  788. /* we can erase flash in recovery app */
  789. if (need_erase) {
  790. int erase_offset;
  791. if (!ota_use_recovery_app(ota) && !ota_erase_part_for_upg(ota)) {
  792. /* cannot erase flash if not in recovery app or single nor */
  793. if (cur_storage_id == 0) {
  794. SYS_LOG_INF("update file_id %d: storage %d is xip, skip erase\n",
  795. cur_storage_id, file->file_id);
  796. goto skip_erase;
  797. }
  798. }
  799. erase_offset = ROUND_DOWN(file->offset + bp_orig_offset, OTA_ERASE_ALIGN_SIZE);
  800. bp_orig_offset = erase_offset - file->offset;
  801. ota_partition_erase_part(ota, part, erase_offset - part->offset);
  802. SYS_LOG_INF("update file_id %d write_offset from 0x%x to 0x%x\n",
  803. file->file_id, bp->cur_orig_write_offset, bp_orig_offset);
  804. bp->cur_orig_write_offset = bp_orig_offset;
  805. }
  806. }
  807. skip_erase:
  808. if (bp_file_state != OTA_BP_FILE_STATE_WRITE_DONE &&
  809. bp_file_state != OTA_BP_FILE_STATE_VERIFY_PASS) {
  810. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_WRITE_START,
  811. bp_file_offset, bp_orig_offset, 0);
  812. err = ota_write_file(ota, file, bp_file_offset, bp_orig_offset);
  813. if (err) {
  814. SYS_LOG_ERR("failed to write file %s",
  815. file->name);
  816. goto failed;
  817. }
  818. /*base sdfs update need to erase extern sdfs part*/
  819. if (file->file_id == PARTITION_FILE_ID_SDFS_PART_BASE) {
  820. const struct partition_entry *part_fatfs = partition_get_part(PARTITION_FILE_ID_SDFS_PART1);
  821. if (part_fatfs) {
  822. // erase res_b partition
  823. ota_partition_erase_part(ota, part_fatfs, 0);
  824. // clear first 32bytes to 0xff
  825. memset(ota->data_buf, 0xff, 32);
  826. ota_storage_write(ota->storage, part_fatfs->offset, ota->data_buf, 32);
  827. SYS_LOG_INF("clear res_b: offset 0x%x", part_fatfs->offset);
  828. }
  829. }
  830. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_WRITE_DONE, 0, 0, 0);
  831. }
  832. if (need_verify) {
  833. err = ota_verify_file(ota, file);
  834. if (err) {
  835. SYS_LOG_ERR("file %s, verify failed", file->name);
  836. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_VERIFY_FAIL, 0, 0, 0);
  837. goto failed;
  838. }
  839. SYS_LOG_INF("file %s, verify pass", file->name);
  840. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_VERIFY_PASS, 0, 0, 0);
  841. }
  842. return 0;
  843. failed:
  844. if (err != -EIO && err != -EAGAIN) {
  845. /* we assume -EIO error that can be resumed */
  846. ota_breakpoint_update_file_state(bp, file, OTA_BP_FILE_STATE_WRITE_FAIL, 0, 0, 0);
  847. }
  848. return err;
  849. }
  850. static int ota_upgrade_verify_along(struct ota_upgrade_info *ota)
  851. {
  852. const struct partition_entry *part;
  853. struct ota_manifest *manifest = &ota->manifest;
  854. struct ota_file *file;
  855. int i, err;
  856. int cur_file_id;
  857. for (i = 0; i < manifest->file_cnt; i++) {
  858. file = &manifest->wfiles[i];
  859. part = partition_get_mirror_part(file->file_id);
  860. if (part == NULL) {
  861. SYS_LOG_INF("cannt found mirror part entry for file_id %d",
  862. file->file_id);
  863. if (ota_use_recovery(ota)) {
  864. cur_file_id = partition_get_current_file_id();
  865. part = partition_get_part(file->file_id);
  866. if (cur_file_id == file->file_id || part == NULL) {
  867. SYS_LOG_ERR("cannt found part entry for file_id %d, cur_file_id %d",
  868. file->file_id, cur_file_id);
  869. return -EINVAL;
  870. }
  871. SYS_LOG_INF("found part entry for file_id %d, cur_file_id %d",
  872. file->file_id, cur_file_id);
  873. } else {
  874. return -EINVAL;
  875. }
  876. }
  877. /* ignore boot partition */
  878. if (partition_is_boot_part(part))
  879. continue;
  880. if (partition_is_param_part(part))
  881. continue;
  882. err = ota_verify_file(ota, file);
  883. if (err) {
  884. SYS_LOG_ERR("file %s, verify failed", file->name);
  885. ota_breakpoint_update_file_state(&ota->bp, file, OTA_BP_FILE_STATE_VERIFY_FAIL, 0, 0, 0);
  886. return -1;
  887. }
  888. ota_upgrade_file_cb(ota, file->file_id);
  889. SYS_LOG_INF("file %s, verify pass", file->name);
  890. ota_breakpoint_update_file_state(&ota->bp, file, OTA_BP_FILE_STATE_VERIFY_PASS, 0, 0, 0);
  891. }
  892. return 0;
  893. }
  894. #if 0
  895. static int ota_auto_update_version(struct ota_upgrade_info *ota,
  896. const struct partition_entry *part,
  897. struct ota_file *file)
  898. {
  899. struct ota_storage *storage = ota->storage;
  900. const struct fw_version *cur_ver;
  901. struct fw_version *new_ver;
  902. uint32_t start_time, consume_time;
  903. uint32_t addr, len = file->size, wlen;
  904. uint8_t *param_ptr, *temp_param_ptr, *param_map_ptr;
  905. SYS_LOG_INF("write file %s len %d to offset 0x%x by auto update version",
  906. file->name, len, file->offset);
  907. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  908. storage = ota_storage_find(file->storage_id);
  909. #endif
  910. start_time = k_uptime_get_32();
  911. cur_ver = (struct fw_version *)fw_version_get_current();
  912. param_ptr = (uint8_t *)mem_malloc(file->size);
  913. if (!param_ptr) {
  914. SYS_LOG_INF("failed to malloc size %d", file->size);
  915. return -ENOMEM;
  916. }
  917. temp_param_ptr = param_ptr;
  918. param_map_ptr = soc_memctrl_create_temp_mapping(file->offset, false);
  919. memcpy(param_ptr, param_map_ptr, file->size);
  920. soc_memctrl_clear_temp_mapping(param_map_ptr);
  921. new_ver = (struct fw_version *)(param_ptr + SOC_BOOT_FIRMWARE_VERSION_OFFSET);
  922. /* Allow ota upgrade even though new ota version is bigger than the current's */
  923. if (new_ver->version_code > cur_ver->version_code) {
  924. SYS_LOG_WRN("new fw version 0x%x is bigger than current's 0x%x",
  925. new_ver->version_code, cur_ver->version_code);
  926. }
  927. new_ver->version_code = cur_ver->version_code + 1;
  928. new_ver->checksum = utils_crc32(0, (const uint8_t *)new_ver, sizeof(struct fw_version) - 4);
  929. SYS_LOG_INF("current fw version: 0x%x", cur_ver->version_code);
  930. SYS_LOG_INF("current fw version name: %s", cur_ver->version_name);
  931. SYS_LOG_INF("new fw version: 0x%x", new_ver->version_code);
  932. SYS_LOG_INF("new fw version name: %s", new_ver->version_name);
  933. ota_partition_erase_part(ota, part, 0);
  934. addr = file->offset;
  935. /* enable encryption function */
  936. if (part->flag & PARTITION_FLAG_ENABLE_ENCRYPTION) {
  937. SYS_LOG_INF("enable encryption write");
  938. addr |= (1 << 31);
  939. if (len % 32) {
  940. SYS_LOG_ERR("len %d shall align with 32 bytes", len);
  941. mem_free(temp_param_ptr);
  942. return -EINVAL;
  943. }
  944. }
  945. wlen = 32;
  946. while (len) {
  947. if (len < wlen)
  948. wlen = len;
  949. if (ota_storage_write(storage, addr, param_ptr, wlen)) {
  950. SYS_LOG_ERR("storage write failed, offs 0x%x", addr);
  951. mem_free(temp_param_ptr);
  952. return -EFAULT;
  953. }
  954. param_ptr += wlen;
  955. addr += wlen;
  956. len -= wlen;
  957. }
  958. consume_time = k_uptime_get_32() - start_time + 1;
  959. SYS_LOG_INF("write file %s: length %d KB, consume %d ms, %d KB/s\n", file->name, len / 1024,
  960. consume_time, len / consume_time);
  961. mem_free(temp_param_ptr);
  962. return 0;
  963. }
  964. #endif
  965. static int ota_do_upgrade(struct ota_upgrade_info *ota)
  966. {
  967. const struct partition_entry *part, *boot_part = NULL, *param_part = NULL;
  968. struct ota_manifest *manifest = &ota->manifest;
  969. struct ota_file *file, *boot_file = NULL, *param_file = NULL;
  970. int i, err, max_file_size;
  971. int cur_file_id;
  972. int retry_times = 0;
  973. SYS_LOG_INF("ota file_cnt %d", manifest->file_cnt);
  974. try_again:
  975. for (i = 0; i < manifest->file_cnt; i++) {
  976. file = &manifest->wfiles[i];
  977. part = partition_get_mirror_part(file->file_id);
  978. if (part == NULL) {
  979. SYS_LOG_INF("cannt found mirror part entry for file_id %d",
  980. file->file_id);
  981. if (ota_use_recovery(ota)) {
  982. cur_file_id = partition_get_current_file_id();
  983. part = partition_get_part(file->file_id);
  984. if (cur_file_id == file->file_id || part == NULL) {
  985. SYS_LOG_ERR("cannt found part entry for file_id %d, cur_file_id %d",
  986. file->file_id, cur_file_id);
  987. return -EINVAL;
  988. }
  989. SYS_LOG_INF("found part entry for file_id %d, cur_file_id %d",
  990. file->file_id, cur_file_id);
  991. } else {
  992. return -EINVAL;
  993. }
  994. }
  995. max_file_size = partition_get_max_file_size(part);
  996. if (file->orig_size > max_file_size) {
  997. SYS_LOG_ERR("part %s: file size 0x%x > part max file size 0x%x",
  998. part->name, file->orig_size, max_file_size);
  999. return -EINVAL;
  1000. }
  1001. SYS_LOG_INF("[%d]: file %s, file_id %d write to nor addr 0x%x",
  1002. i, file->name, file->file_id, part->file_offset);
  1003. file->offset = part->file_offset;
  1004. if (partition_is_boot_part(part)) {
  1005. boot_file = file;
  1006. boot_part = part;
  1007. continue;
  1008. }
  1009. if (partition_is_param_part(part)) {
  1010. param_file = file;
  1011. param_part = part;
  1012. continue;
  1013. }
  1014. err = ota_write_and_verify_file(ota, part, file, false);
  1015. if (err) {
  1016. return err;
  1017. }
  1018. }
  1019. err = ota_upgrade_verify_along(ota);
  1020. if (err) {
  1021. /* retry upgrade if verify failed */
  1022. if (retry_times < 1) {
  1023. SYS_LOG_ERR("OTA upgrade retry after verify failed");
  1024. retry_times++;
  1025. ota_image_progress_reset(ota->img);
  1026. goto try_again;
  1027. } else {
  1028. return err;
  1029. }
  1030. }
  1031. /* don't upgrade for boot file and para file*/
  1032. #if OTA_FULL_UPGRADE
  1033. /* write boot file at secondly last */
  1034. if (boot_file && boot_part) {
  1035. /* write boot file at mirror part */
  1036. err = ota_write_and_verify_file(ota, boot_part, boot_file, true);
  1037. if (err) {
  1038. return err;
  1039. }
  1040. /* erase boot at current part */
  1041. boot_part = partition_get_part(boot_file->file_id);
  1042. ota_partition_erase_part(ota, boot_part, 0);
  1043. ota_breakpoint_set_file_state(&ota->bp, boot_file->file_id, OTA_BP_FILE_STATE_CLEAN);
  1044. }
  1045. /* write param file at last */
  1046. if (param_file && param_part) {
  1047. /* write param file at mirror part */
  1048. err = ota_write_and_verify_file(ota, param_part, param_file, true);
  1049. if (err) {
  1050. return err;
  1051. }
  1052. /* erase param file at current part */
  1053. param_part = partition_get_part(param_file->file_id);
  1054. ota_partition_erase_part(ota, param_part, 0);
  1055. ota_breakpoint_set_file_state(&ota->bp, param_file->file_id, OTA_BP_FILE_STATE_CLEAN);
  1056. // if (ota_use_no_version_control(ota)) {
  1057. // err = ota_auto_update_version(ota, param_part, param_file);
  1058. // if (err) {
  1059. // return err;
  1060. // }
  1061. // }
  1062. }
  1063. #endif
  1064. /* try to save res version */
  1065. ota_save_res_version(ota);
  1066. return 0;
  1067. }
  1068. static int ota_is_need_upgrade(struct ota_upgrade_info *ota)
  1069. {
  1070. struct ota_breakpoint *bp = &ota->bp;
  1071. const struct fw_version *cur_ver, *img_ver;
  1072. struct ota_backend *backend;
  1073. int backend_type;
  1074. #if defined(CONFIG_OTA_FILE_PATCH) || defined(CONFIG_OTA_RES_PATCH)
  1075. const struct fw_version *patch_old_ver;
  1076. patch_old_ver = &ota->manifest.old_fw_ver;
  1077. SYS_LOG_INF("OTA patch old fw version:");
  1078. fw_version_dump(patch_old_ver);
  1079. #endif
  1080. img_ver = &ota->manifest.fw_ver;
  1081. cur_ver = fw_version_get_current();
  1082. SYS_LOG_INF("ota fw version:");
  1083. fw_version_dump(img_ver);
  1084. SYS_LOG_INF("current fw version:");
  1085. fw_version_dump(cur_ver);
  1086. backend = ota_image_get_backend(ota->img);
  1087. backend_type = ota_backend_get_type(backend);
  1088. if (backend_type != OTA_BACKEND_TYPE_TEMP_PART &&
  1089. !(backend_type == OTA_BACKEND_TYPE_CARD && ota_use_recovery_app(ota)) &&
  1090. bp->backend_type != OTA_BACKEND_TYPE_UNKNOWN &&
  1091. bp->backend_type != backend_type) {
  1092. SYS_LOG_ERR("backend type is chagned(%d -> %d), need erase old firmware",
  1093. bp->backend_type, ota_backend_get_type(backend));
  1094. return -1;
  1095. }
  1096. if (strcmp(cur_ver->board_name, img_ver->board_name)) {
  1097. /* skip */
  1098. SYS_LOG_ERR("unmatched board name, skip ota");
  1099. return -1;
  1100. }
  1101. #ifdef CONFIG_OTA_FILE_PATCH
  1102. if (ota_is_patch_fw(ota)) {
  1103. /* validate ota patch firmware version */
  1104. if (cur_ver->version_code != patch_old_ver->version_code) {
  1105. SYS_LOG_ERR("unmatched fw ver, curr 0x%x but OTA patch old ver is 0x%x",
  1106. cur_ver->version_code, ota->manifest.old_fw_ver.version_code);
  1107. return -1;
  1108. }
  1109. if (ota_use_no_version_control(ota)) {
  1110. SYS_LOG_ERR("Patch FW only support with version control");
  1111. return -1;
  1112. }
  1113. }
  1114. #endif
  1115. #if 0
  1116. //#ifdef CONFIG_OTA_RES_PATCH
  1117. if (ota_is_patch_res(ota)) {
  1118. /* validate ota patch firmware version */
  1119. if (cur_ver->version_res != patch_old_ver->version_res) {
  1120. SYS_LOG_ERR("unmatched fw ver, curr 0x%x but OTA patch old ver is 0x%x",
  1121. cur_ver->version_res, ota->manifest.old_fw_ver.version_res);
  1122. return -1;
  1123. }
  1124. if (ota_use_no_version_control(ota)) {
  1125. SYS_LOG_ERR("Patch FW only support with version control");
  1126. return -1;
  1127. }
  1128. }
  1129. #endif
  1130. if (!ota_use_no_version_control(ota)) {
  1131. if (cur_ver->version_code >= img_ver->version_code) {
  1132. /* skip */
  1133. SYS_LOG_INF("ota image is same or older, skip ota");
  1134. return 0;
  1135. }
  1136. }
  1137. if (bp->state == OTA_BP_STATE_WRITING_IMG ||
  1138. bp->state == OTA_BP_STATE_UPGRADE_WRITING ||
  1139. bp->state == OTA_BP_STATE_UPGRADE_PENDING) {
  1140. if ((bp->new_version != 0 && bp->new_version != img_ver->version_code)
  1141. || (bp->data_checksum != ota_image_get_checksum(ota->img))) {
  1142. /* FIXME: has new version fw, need erase partition */
  1143. SYS_LOG_INF("has new version fw, need erase old firmware");
  1144. return 2;
  1145. }
  1146. }
  1147. return 1;
  1148. }
  1149. static int ota_upgrade_statistics(struct ota_upgrade_info *ota)
  1150. {
  1151. const struct partition_entry *part;
  1152. struct ota_manifest *manifest = &ota->manifest;
  1153. struct ota_file *file;
  1154. struct ota_breakpoint *bp = &ota->bp;
  1155. int i, bp_file_state, start_write_offset = 0, erase_offset;
  1156. int cur_file_id;
  1157. uint32_t total_size = 0;
  1158. for (i = 0; i < manifest->file_cnt; i++) {
  1159. file = &manifest->wfiles[i];
  1160. part = partition_get_mirror_part(file->file_id);
  1161. if (part == NULL) {
  1162. SYS_LOG_INF("cannt found mirror part entry for file_id %d",
  1163. file->file_id);
  1164. if (ota_use_recovery(ota)) {
  1165. cur_file_id = partition_get_current_file_id();
  1166. part = partition_get_part(file->file_id);
  1167. if (cur_file_id == file->file_id || part == NULL) {
  1168. SYS_LOG_ERR("cannt found part entry for file_id %d, cur_file_id %d",
  1169. file->file_id, cur_file_id);
  1170. return -EINVAL;
  1171. }
  1172. SYS_LOG_INF("found part entry for file_id %d, cur_file_id %d",
  1173. file->file_id, cur_file_id);
  1174. } else {
  1175. return -EINVAL;
  1176. }
  1177. }
  1178. bp_file_state = ota_breakpoint_get_file_state(bp, file->file_id);
  1179. if (bp_file_state != OTA_BP_FILE_STATE_WRITE_DONE &&
  1180. bp_file_state != OTA_BP_FILE_STATE_VERIFY_PASS) {
  1181. if (bp_file_state == OTA_BP_FILE_STATE_WRITING_CLEAN
  1182. || bp_file_state == OTA_BP_FILE_STATE_WRITING
  1183. || bp_file_state == OTA_BP_FILE_STATE_WRITE_START) {
  1184. if (file->size == file->orig_size) { // raw file
  1185. /* Align offset with erase size */
  1186. erase_offset = ROUND_DOWN(file->offset + bp->cur_file_write_offset, OTA_ERASE_ALIGN_SIZE);
  1187. start_write_offset += (erase_offset - file->offset);
  1188. } else { // lzma file
  1189. start_write_offset += bp->cur_file_write_offset;
  1190. }
  1191. }
  1192. } else {
  1193. start_write_offset += file->size;
  1194. }
  1195. total_size += file->size;
  1196. SYS_LOG_INF("ota file[%d]%s: total size %d, bp offset 0x%x",
  1197. file->file_id, file->name, file->size, start_write_offset);
  1198. }
  1199. if (total_size)
  1200. ota_image_progress_on(ota->img, total_size, start_write_offset);
  1201. return 0;
  1202. }
  1203. static int ota_temp_part_is_upgrade(struct ota_upgrade_info *ota)
  1204. {
  1205. struct ota_manifest *manifest = &ota->manifest;
  1206. struct ota_file *file;
  1207. for (int i = 0; i < manifest->file_cnt; i++) {
  1208. file = &manifest->wfiles[i];
  1209. if (file->type == PARTITION_TYPE_TEMP && file->file_id == PARTITION_FILE_ID_OTA_TEMP)
  1210. return 1;
  1211. }
  1212. return 0;
  1213. }
  1214. static void ota_upgrade_exit(struct ota_upgrade_info *ota)
  1215. {
  1216. SYS_LOG_INF("exit");
  1217. if (ota) {
  1218. if (ota->img)
  1219. ota_image_exit(ota->img);
  1220. if (ota->storage)
  1221. ota_storage_exit(ota->storage);
  1222. }
  1223. }
  1224. int ota_upgrade_check(struct ota_upgrade_info *ota)
  1225. {
  1226. struct ota_breakpoint *bp = &ota->bp;
  1227. struct ota_backend *backend = NULL;
  1228. int err, need_upgrade;
  1229. int connect_type = 0;
  1230. SYS_LOG_INF("handle upgrade");
  1231. if (ota->state != OTA_INIT) {
  1232. SYS_LOG_ERR("ota state <%d> is not OTA_INIT, skip upgrade", ota->state);
  1233. return -EINVAL;
  1234. }
  1235. if (ota_image_get_backend(ota->img) == NULL) {
  1236. SYS_LOG_ERR("ota backend null\n");
  1237. return -EINVAL;
  1238. }
  1239. backend = ota_image_get_backend(ota->img);
  1240. err = ota_image_open(ota->img);
  1241. if (err) {
  1242. if (ota_backend_get_type(backend) == OTA_BACKEND_TYPE_BLUETOOTH) {
  1243. ota_backend_read_prepare(backend, 0, NULL, 0);
  1244. }
  1245. SYS_LOG_INF("ota image open failed");
  1246. err = -EIO;
  1247. goto exit_invalid;
  1248. }
  1249. if (ota_use_recovery_app(ota)) {
  1250. /* only check data in recovery app to save time */
  1251. err = ota_image_check_data(ota->img);
  1252. if (err) {
  1253. SYS_LOG_ERR("bad data crc");
  1254. ota_breakpoint_update_state(bp, OTA_BP_STATE_WRITING_IMG_FAIL);
  1255. goto exit;
  1256. }
  1257. }
  1258. err = ota_manifest_parse_file(&ota->manifest, ota->img, OTA_MANIFESET_FILE_NAME);
  1259. if (err) {
  1260. SYS_LOG_INF("cannot get manifest file in image");
  1261. err = -EAGAIN;
  1262. goto exit;
  1263. }
  1264. /* need upgrade? */
  1265. need_upgrade = ota_is_need_upgrade(ota);
  1266. if (need_upgrade <= 0) {
  1267. SYS_LOG_INF("skip upgrade");
  1268. ota_breakpoint_update_state(bp, OTA_BP_STATE_WRITING_IMG_FAIL);
  1269. err = -EINVAL;
  1270. goto exit;
  1271. }
  1272. else if(need_upgrade == 2)
  1273. {
  1274. SYS_LOG_INF("bp pending");
  1275. ota_breakpoint_update_state(bp, OTA_BP_STATE_UPGRADE_PENDING);
  1276. ota_update_state(ota, OTA_FAIL);
  1277. ota_partition_update_prepare(ota);
  1278. ota_update_state(ota, OTA_INIT);
  1279. }
  1280. SYS_LOG_INF("burn firmware image");
  1281. if (!ota->data_buf) {
  1282. ota->data_buf = mem_malloc(ota->data_buf_size);
  1283. }
  1284. if (!ota->data_buf) {
  1285. SYS_LOG_ERR("failed to allocate %d bytes", ota->data_buf_size);
  1286. err = -EAGAIN;
  1287. goto exit;
  1288. }
  1289. /* update breakpoint for new firmware */
  1290. bp->backend_type = ota_backend_get_type(backend);
  1291. bp->new_version = ota->manifest.fw_ver.version_code;
  1292. bp->data_checksum = ota_image_get_checksum(ota->img);
  1293. ota_upgrade_statistics(ota);
  1294. ota_update_state(ota, OTA_RUNNING);
  1295. err = ota_rx_init(ota);
  1296. if (err) {
  1297. goto exit;
  1298. }
  1299. if (!ota_use_recovery(ota) || ota_use_recovery_app(ota)) {
  1300. ota_breakpoint_update_state(bp, OTA_BP_STATE_UPGRADE_WRITING);
  1301. err = ota_do_upgrade(ota);
  1302. if (err) {
  1303. SYS_LOG_INF("upgrade failed, err %d", err);
  1304. if (err != -EIO && err != -EAGAIN) {
  1305. ota_breakpoint_update_state(bp, OTA_BP_STATE_UPGRADING_FAIL);
  1306. }
  1307. goto exit;
  1308. }
  1309. /* set version code */
  1310. ota_update_fw_version(ota, PARTITION_FILE_ID_SYSTEM);
  1311. ota_breakpoint_update_state(bp, OTA_BP_STATE_UPGRADE_DONE);
  1312. } else {
  1313. ota_breakpoint_update_state(bp, OTA_BP_STATE_WRITING_IMG);
  1314. err = ota_do_upgrade(ota);
  1315. if (err) {
  1316. SYS_LOG_INF("write ota image failed, err %d", err);
  1317. if (err != -EIO && err != -EAGAIN) {
  1318. ota_breakpoint_update_state(bp, OTA_BP_STATE_WRITING_IMG_FAIL);
  1319. }
  1320. goto exit;
  1321. } else {
  1322. if (ota_temp_part_is_upgrade(ota)) {
  1323. ota_breakpoint_update_state(bp, OTA_BP_STATE_UPGRADE_PENDING);
  1324. } else {
  1325. ota_breakpoint_update_state(bp, OTA_BP_STATE_UPGRADE_DONE);
  1326. }
  1327. }
  1328. }
  1329. SYS_LOG_INF("upgrade successfully!");
  1330. ota_image_report_progress(ota->img, 0, 1);
  1331. ota_image_ioctl(ota->img, OTA_BACKEND_IOCTL_REPORT_IMAGE_VALID, 1);
  1332. ota_update_state(ota, OTA_DONE);
  1333. exit:
  1334. ota_rx_exit(ota);
  1335. if (err) {
  1336. ota_image_ioctl(ota->img, OTA_BACKEND_IOCTL_REPORT_IMAGE_VALID, 0);
  1337. ota_update_state(ota, OTA_FAIL);
  1338. if (err != -EAGAIN) {
  1339. /* upgrade fail need to clear bp and erase dirty part*/
  1340. ota_partition_update_prepare(ota);
  1341. }
  1342. /* wait for upgrade resume */
  1343. SYS_LOG_INF("ota status -> OTA_INIT, wait for upgrading resume!");
  1344. ota_update_state(ota, OTA_INIT);
  1345. }
  1346. if (ota->data_buf) {
  1347. if (ota_backend_get_type(backend) == OTA_BACKEND_TYPE_BLUETOOTH) {
  1348. ota_backend_read_prepare(backend, 0, NULL, 0);
  1349. }
  1350. mem_free(ota->data_buf);
  1351. ota->data_buf = NULL;
  1352. }
  1353. if ((!err) && backend) {
  1354. // delay 500ms to close bt for sending remain data
  1355. if (ota_backend_get_type(backend) == OTA_BACKEND_TYPE_BLUETOOTH) {
  1356. os_sleep(500);
  1357. }
  1358. }
  1359. ota_image_close(ota->img);
  1360. exit_invalid:
  1361. if (err) {
  1362. if (ota_backend_get_type(backend) == OTA_BACKEND_TYPE_BLUETOOTH) {
  1363. /* waiting for 500ms to disconnect ble.*/
  1364. int sp_cnt = 0;
  1365. do {
  1366. ota_backend_ioctl(backend,
  1367. OTA_BACKEND_IOCTL_GET_CONNECT_TYPE, (unsigned int)&connect_type);
  1368. SYS_LOG_INF("connect_type %d sp_cnt %d.",connect_type, sp_cnt);
  1369. #ifdef CONFIG_BT_MANAGER
  1370. if (BLE_CONNECT_TYPE != connect_type) {
  1371. break;
  1372. }
  1373. #endif
  1374. os_sleep(50);
  1375. sp_cnt++;
  1376. if (10 == sp_cnt) {
  1377. ota_backend_ioctl(backend, OTA_BACKEND_IOCTL_EXECUTE_EXIT, 0);
  1378. }
  1379. } while (sp_cnt < 10);
  1380. }
  1381. ota_image_unbind(ota->img, ota_image_get_backend(ota->img));
  1382. } else {
  1383. ota_upgrade_exit(ota);
  1384. }
  1385. return err;
  1386. }
  1387. int ota_upgrade_attach_backend(struct ota_upgrade_info *ota, struct ota_backend *backend)
  1388. {
  1389. struct ota_backend *img_backend = ota_image_get_backend(ota->img);
  1390. SYS_LOG_INF("attach backend type %d", backend->type);
  1391. if (img_backend != NULL && img_backend->type != backend->type) {
  1392. SYS_LOG_ERR("already attached backend %d %d", img_backend->type, backend->type);
  1393. return -EBUSY;
  1394. }
  1395. ota_image_bind(ota->img, backend);
  1396. return 0;
  1397. }
  1398. void ota_upgrade_detach_backend(struct ota_upgrade_info *ota, struct ota_backend *backend)
  1399. {
  1400. SYS_LOG_INF("detach backend %p", backend);
  1401. /* to avoid empty pointer */
  1402. #if 0
  1403. struct ota_backend *img_backend = ota_image_get_backend(ota->img);
  1404. if (img_backend == backend)
  1405. ota_image_unbind(ota->img, backend);
  1406. #endif
  1407. }
  1408. int ota_upgrade_is_in_progress(struct ota_upgrade_info *ota)
  1409. {
  1410. struct ota_breakpoint *bp = &ota->bp;
  1411. int bp_state;
  1412. bp_state = ota_breakpoint_get_current_state(bp);
  1413. switch (bp_state) {
  1414. case OTA_BP_STATE_UPGRADE_PENDING:
  1415. case OTA_BP_STATE_UPGRADE_WRITING:
  1416. case OTA_BP_STATE_UPGRADE_DONE:
  1417. return 1;
  1418. default:
  1419. break;
  1420. }
  1421. return 0;
  1422. }
  1423. int ota_upgrade_set_in_progress(struct ota_upgrade_info *ota)
  1424. {
  1425. if (!ota_upgrade_is_in_progress(ota)) {
  1426. ota_breakpoint_update_state(&ota->bp, OTA_BP_STATE_UPGRADE_PENDING);
  1427. }
  1428. return 0;
  1429. }
  1430. static struct ota_upgrade_info global_ota_upgrade_info;
  1431. struct ota_upgrade_info *ota_upgrade_init(struct ota_upgrade_param *param)
  1432. {
  1433. struct ota_upgrade_info *ota;
  1434. SYS_LOG_INF("init");
  1435. ota = &global_ota_upgrade_info;
  1436. memset(ota, 0x0, sizeof(struct ota_upgrade_info));
  1437. if (param->no_version_control) {
  1438. SYS_LOG_INF("enable no version control");
  1439. ota->flags |= OTA_FLAG_USE_NO_VERSION_CONTROL;
  1440. }
  1441. /* allocate data buffer later */
  1442. ota->data_buf_size = OTA_DATA_BUFFER_SIZE;
  1443. if (!ota->data_buf) {
  1444. ota->data_buf = mem_malloc(ota->data_buf_size);
  1445. }
  1446. ota->storage = ota_storage_init(param->storage_name);
  1447. if (!ota->storage) {
  1448. SYS_LOG_INF("storage open err");
  1449. ota = NULL;
  1450. goto init_exit;
  1451. }
  1452. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  1453. ota->storage_ext = ota_storage_init(param->storage_ext_name);
  1454. if (!ota->storage_ext) {
  1455. SYS_LOG_INF("storage ext open err");
  1456. ota = NULL;
  1457. goto init_exit;
  1458. }
  1459. #endif
  1460. if (param->flag_use_recovery) {
  1461. ota->flags |= OTA_FLAG_USE_RECOVERY;
  1462. }
  1463. if (param->flag_erase_part_for_upg) {
  1464. ota->flags |= OTA_FLAG_ERASE_PART_FOR_UPG;
  1465. }
  1466. if (param->flag_keep_temp_part) {
  1467. ota->flags |= OTA_FLAG_KEEP_TEMP_PART;
  1468. }
  1469. if (param->flag_use_recovery_app) {
  1470. if (!param->flag_use_recovery) {
  1471. SYS_LOG_ERR("invalid flag_is_recovery_app");
  1472. ota = NULL;
  1473. goto init_exit;
  1474. }
  1475. ota->flags |= OTA_FLAG_USE_RECOVERY_APP;
  1476. }
  1477. ota_breakpoint_init(&ota->bp);
  1478. ota_partition_update_prepare(ota);
  1479. ota->img = ota_image_init();
  1480. if (!ota->img) {
  1481. SYS_LOG_ERR("image init failed");
  1482. ota = NULL;
  1483. goto init_exit;
  1484. }
  1485. ota->notify = param->notify;
  1486. ota->file_cb = param->file_cb;
  1487. ota_update_state(ota, OTA_INIT);
  1488. // disable nor-suspend to reduce erase time when erase size >= 256KB
  1489. ota_storage_set_max_erase_seg(ota->storage, OTA_STORAGE_MAX_ERASE_SEGMENT_SIZE);
  1490. init_exit:
  1491. if (ota && ota->data_buf) {
  1492. mem_free(ota->data_buf);
  1493. ota->data_buf = NULL;
  1494. }
  1495. return ota;
  1496. }
  1497. struct ota_storage *ota_upgrade_storage_fine(struct ota_file *file)
  1498. {
  1499. struct ota_storage *storage = global_ota_upgrade_info.storage;
  1500. #ifdef CONFIG_OTA_MUTIPLE_STORAGE
  1501. storage = ota_storage_find(file->storage_id);
  1502. #endif
  1503. return storage;
  1504. }