blob: 23f85f505004ac34f4931b7405bbbddcd61ce429 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// 2017-07-31 carota Rock FOTA ua porting file
2
3
4#define ROCK_FOTA_SUPPORT
5
6#ifdef ROCK_FOTA_SUPPORT
7
8
9#include "iot_rock.h"
10#include "iot_rock_ipl.h"
11#include "sha.h"
12
13#include <stdarg.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <unistd.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20//#include <hardware/hardware.h>
21
22//#define off64_t __off64_t
23
24
25#include <hardware/boot_control.h>
26#include <hardware/hardware.h>
27#include <sys/reboot.h>
28
29
30
31#include "liblog/lynq_deflog.h"
32#include "mtk_device_wrap.h"
33
34#define ROCK_DEFAULT_BLOCK_SIZE 0x20000
35#define ROCK_RAM_LEN (1024*1024)
36
37
38#define ROCK_BACKUP_LEN ROCK_DEFAULT_BLOCK_SIZE
39
40
41#define DEV_SYSTEM_A "/dev/disk/by-partlabel/system_a"
42#define DEV_SYSTEM_B "/dev/disk/by-partlabel/system_b"
43#define DEV_BOOT_A "/dev/disk/by-partlabel/boot_a"
44#define DEV_BOOT_B "/dev/disk/by-partlabel/boot_b"
45
46#define DEV_MD1IMG_A "/dev/disk/by-partlabel/md1img_a"
47#define DEV_MD1IMG_B "/dev/disk/by-partlabel/md1img_b"
48
49#define DEV_TEE_A "/dev/disk/by-partlabel/tee_a"
50#define DEV_TEE_B "/dev/disk/by-partlabel/tee_b"
51
52#define DEV_VBMETA_A "/dev/disk/by-partlabel/vbmeta_a"
53#define DEV_VBMETA_B "/dev/disk/by-partlabel/vbmeta_b"
54#define DEV_BL2 "/dev/disk/by-partlabel/bl2"
55#define DEV_BL33 "/dev/disk/by-partlabel/bl33"
56
57#define DEV_MISC "/dev/disk/by-partlabel/misc"
58
59
60
61//#define DEV_DELTA "/dev/disk/by-partlabel/delta"
62#define DEV_DELTA "/dev/disk/by-partlabel/delta"
63
64
65
66#define FILE_UPDATE_STATE "/data/.update_status"
67#define FILE_FOTA_STATE "/data/.fota_status"
68
69#define NAND_PAGE_SIZE 2048
70
71
72#define BOOTDEV_TYPE_NAND 1
73
74#define BACKUP_ADDR_FLAG 0xffffffff
75#define FILE_BACKUP "/data/.backup"
76
77#define SLOT_A 0
78#define SLOT_B 1
79
80#define FULL_HEAD "full-ota"
81
82
83int fd_system_a,fd_system_b,fd_boot_a,fd_boot_b,fd_tee_a,fd_tee_b,fd_bl2,fd_bl33,fd_delta,fd_curr,fd_log,fd_update_status,fd_md1img_a,fd_md1img_b,fd_fota_status,fd_vbmeta_a,fd_vbmeta_b;
84int fd_write,fd_read;
85
86static unsigned int delta_offset = 0;
87static unsigned int now_patch = 0;
88unsigned int current_slot = 0;
89unsigned char rock_debug_buffer[512];
90
91DELTA_HEAD da_head;
92
93OTA_STATUS fota_status;
94
95
96extern const hw_module_t HAL_MODULE_INFO_SYM;
97boot_control_module_t* module;
98
99
100int rock_mismatch(void* ctx, unsigned char* buf, unsigned int start, unsigned int size,
101 unsigned int source_hash,unsigned int target_hash) {
102
103 return 0;
104}
105
106int rock_fatal(void* ctx, int error_code) {
107 return 0;
108}
109
110void rock_trace(void* ctx, const char* fmt, ...) {
111
112 va_list ap;
113 memset(rock_debug_buffer,0x0,sizeof(rock_debug_buffer));
114 va_start (ap, fmt);
115
116
117 vsnprintf(rock_debug_buffer,sizeof(rock_debug_buffer),fmt,ap);
118 LYDBGLOG("+[UA]: %s",rock_debug_buffer);
119
120
121 va_end (ap);
122
123}
124
125static int save_fota_status()
126{
127 int err;
128 fd_fota_status = open(FILE_FOTA_STATE,O_RDWR | O_CREAT,0777);
129 //fd_update_status = open(FILE_UPDATE_STATE,O_RDWR);
130
131 if (fd_fota_status < 0) {
132 err = errno;
133 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
134 return -err;
135 }
136
137 write(fd_fota_status, &fota_status,sizeof(fota_status));
138 sync();
139
140 close(fd_fota_status);
141}
142
143
144void rock_progress(void* ctx, int percent) {
145 int i = 0;
146 rock_trace(ctx, "rock update progress %d\n", percent);
147 if (percent > 20) {
148 i = fota_status.ota_run;
149 if (fota_status.update_status[i-1].check_delta != PASS) {
150 fota_status.update_status[i-1].check_delta = PASS;
151 fota_status.update_status[i-1].check_rom= PASS;
152 save_fota_status();
153 }
154 }
155}
156
157int rock_process_block(void* ctx, unsigned char* data, unsigned int start, unsigned int size){
158 //rock_trace(ctx, "rock update progress block %d\n", size);
159
160 int writen = 0;
161 int ret,err;
162
163
164 if (start == BACKUP_ADDR_FLAG) {
165 int fd_backup = open(FILE_BACKUP,O_RDWR | O_CREAT,0777);
166 while (writen < size) {
167 write(fd_backup,data,ROCK_DEFAULT_BLOCK_SIZE);
168 sync();
169 writen += ROCK_DEFAULT_BLOCK_SIZE;
170 }
171 close(fd_backup);
172 return size;
173 }
174
175
176
177 writen = 0;
178
179 if (mtk_device_wrap_seek(fd_write, start, SEEK_SET) < 0) {
180 err = errno;
181 rock_trace(ctx, "mtk_device_wrap_seek write\n");
182 return err;
183 }
184
185 while (writen < size) {
186 ret = mtk_device_wrap_write(fd_write,data+writen, ROCK_DEFAULT_BLOCK_SIZE);
187 writen += ROCK_DEFAULT_BLOCK_SIZE;
188 }
189
190
191 return size;
192}
193int rock_write_block(void* ctx, unsigned char* src, unsigned int start, unsigned int size){
194
195#if 0
196
197 int writen = 0;
198 int ret,err;
199
200
201 if (start == BACKUP_ADDR_FLAG) {
202 int fd_backup = open(FILE_BACKUP,O_RDWR | O_CREAT,0777);
203 while (writen < size) {
204 write(fd_backup,src,ROCK_DEFAULT_BLOCK_SIZE);
205 sync();
206 writen += ROCK_DEFAULT_BLOCK_SIZE;
207 }
208 close(fd_backup);
209 return size;
210 }
211
212 writen = 0;
213
214 if (mtk_device_wrap_seek(fd_curr, start, SEEK_SET) < 0) {
215 err = errno;
216 rock_trace(ctx, "mtk_device_wrap_seek write\n");
217 return err;
218 }
219
220 while (writen < size) {
221 ret = mtk_device_wrap_write(fd_curr,src+writen, ROCK_DEFAULT_BLOCK_SIZE);
222 writen += ROCK_DEFAULT_BLOCK_SIZE;
223 }
224
225#endif
226
227 return size;
228
229}
230
231int rock_read_block(void* ctx, unsigned char* dest, unsigned int start, unsigned int size){
232
233
234 int ret,err;
235
236
237 if (start == BACKUP_ADDR_FLAG) {
238 int fd_backup = open(FILE_BACKUP,O_RDONLY);
239 read(fd_backup,dest,size);
240 sync();
241 close(fd_backup);
242 return size;
243 }
244
245
246
247 if (mtk_device_wrap_seek(fd_read, start, SEEK_SET) < 0) {
248 err = errno;
249 rock_trace(ctx, "mtk_device_wrap_seek read block err\n");
250 }
251
252 do {
253
254 ret = mtk_device_wrap_read(fd_read, dest, size);
255
256 if (ret == 0) {
257 break;
258 } else if (ret < 0) {
259 if (errno == EINTR) {
260 continue;
261 }
262 err = -errno;
263 rock_trace(ctx,"%s Error reading metadata file\n");
264
265 mtk_device_wrap_close(fd_read);
266
267 return err;
268 }
269 size -= ret;
270 dest += ret;
271 } while(size > 0);
272
273
274 return ret;
275
276
277}
278
279
280int rock_read_delta(void* ctx, unsigned char* dest, unsigned int offset, unsigned int size){
281
282 int ret = 0,err = 0;
283
284
285 if (mtk_device_wrap_seek(fd_delta, offset + delta_offset, SEEK_SET) < 0) {
286 err = -errno;
287 rock_trace(ctx, "mtk_device_wrap_seek df_delta err\n");
288 return err;
289 }
290
291 do {
292
293 ret = mtk_device_wrap_read(fd_delta, dest, size);
294
295 if (ret == 0) {
296 break;
297 } else if (ret < 0) {
298 if (errno == EINTR) {
299 continue;
300 }
301 err = -errno;
302 rock_trace(ctx," Error reading metadata file\n");
303
304 mtk_device_wrap_close(fd_delta);
305
306 return err;
307 }
308 size -= ret;
309 dest += ret;
310 } while(size > 0);
311
312 return ret;
313
314}
315
316int rock_get_blocksize(void* ctx) {
317 return ROCK_DEFAULT_BLOCK_SIZE;
318}
319
320int rock_read_file(void* ctx, void* name, unsigned char* dest, unsigned int offset, unsigned int size){return 0;}
321int rock_write_file(void* ctx, void* name, unsigned char* src, unsigned int offset, unsigned int size){return 0;}
322int rock_delete_file(void* ctx, void* name){return 0;}
323/* ROCK IPL end */
324
325
326
327static int init_dev_fd()
328{
329
330 int err;
331
332 //fd_delta = mtk_device_wrap_open(DEV_DELTA,O_RDONLY);
333 fd_delta = mtk_device_wrap_open(DEV_DELTA,O_RDWR);
334
335 if (fd_delta < 0) {
336 err = errno;
337 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
338 return -err;
339 }
340
341 fd_update_status = open(FILE_UPDATE_STATE,O_RDWR | O_CREAT,0777);
342 //fd_update_status = open(FILE_UPDATE_STATE,O_RDWR);
343
344 if (fd_update_status < 0) {
345 err = errno;
346 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
347 return -err;
348 }
349
350 return 0;
351}
352
353
354
355
356
357static int close_dev_fd(int fd)
358{
359 mtk_device_wrap_close(fd);
360}
361
362
363
364static int reboot_device() {
365
366 reboot(RB_AUTOBOOT);
367
368 while (1) pause();
369}
370
371
372
373int test_write_delta(char *source, char *target)
374{
375 int fd_source,fd_target,size;
376
377 char delta_data[ROCK_DEFAULT_BLOCK_SIZE];
378
379 fd_source = open(source,O_RDONLY);
380
381 if (fd_source < 0) {
382 LYERRLOG("+[UA]: open source error\n");
383 return 1;
384 }
385
386 fd_target = mtk_device_wrap_open(target,O_RDWR);
387
388 if (fd_target < 0) {
389 mtk_device_wrap_close(fd_target);
390 close(fd_source);
391 LYERRLOG("+[UA]: open target error\n");
392 return 1;
393 }
394
395 while(( size = read(fd_source,delta_data,ROCK_DEFAULT_BLOCK_SIZE))>0) {
396 mtk_device_wrap_write(fd_target,delta_data,ROCK_DEFAULT_BLOCK_SIZE);
397 }
398
399 mtk_device_wrap_close(fd_target);
400 close(fd_source);
401 return 0;
402}
403
404
405int nand_copyto_nand(char *source, char *target)
406{
407 int fd_source,fd_target,size;
408
409 char delta_data[ROCK_DEFAULT_BLOCK_SIZE];
410
411 fd_source = mtk_device_wrap_open(source,O_RDONLY);
412
413 if (fd_source < 0) {
414 LYERRLOG("+[UA]: open source error\n");
415 return 1;
416 }
417
418 fd_target = mtk_device_wrap_open(target,O_RDWR);
419
420 if (fd_target < 0) {
421 mtk_device_wrap_close(fd_target);
422 mtk_device_wrap_close(fd_source);
423 LYERRLOG("+[UA]: open target error\n");
424 return 1;
425 }
426
427 while(( size = mtk_device_wrap_read(fd_source,delta_data,ROCK_DEFAULT_BLOCK_SIZE))>0) {
428 mtk_device_wrap_write(fd_target,delta_data,ROCK_DEFAULT_BLOCK_SIZE);
429 }
430
431 mtk_device_wrap_close(fd_target);
432 mtk_device_wrap_close(fd_source);
433 return 0;
434}
435
436int delta_copyto_nand(unsigned int start,int size)
437{
438
439 char delta_data[NAND_PAGE_SIZE];
440 unsigned int ret = 0;
441 int err;
442
443
444 if (mtk_device_wrap_seek(fd_delta, start, SEEK_SET) < 0) {
445 LYERRLOG("+[UA]: delta_copyto_nand seek err\n");
446 return -1;
447 }
448
449 if (mtk_device_wrap_seek(fd_curr, 0, SEEK_SET) < 0) {
450 LYERRLOG("+[UA]: delta_copyto_nand seek err\n");
451 return -1;
452 }
453
454
455 do {
456
457 ret = mtk_device_wrap_read(fd_delta, delta_data, NAND_PAGE_SIZE);
458
459 if (ret == 0) {
460 break;
461 } else if (ret < 0) {
462 if (errno == EINTR) {
463 continue;
464 }
465 err = -errno;
466
467 return err;
468 }
469
470 size -= NAND_PAGE_SIZE;
471 //mtk_device_wrap_write(fd_curr,delta_data,NAND_PAGE_SIZE);
472 mtk_device_wrap_write(fd_curr,delta_data,ret);
473
474 } while(size > 0);
475
476
477 return 0;
478
479}
480
481
482
483
484
485unsigned char ram_buffer[ROCK_RAM_LEN];
486
487
488
489
490static int rock_update_main(unsigned int rom_base, unsigned int backup_base, unsigned int backup_len, int read_rom_directly, int first_run) {
491 int status,err,start;
492 int ret = 0;
493 int i = 0;
494 int retry_cnt = 0;
495
496 IOT_UPDATA_CONTEXT ctx;
497 UPDATE_INFO up_info;
498 //OTA_STATUS fota_status;
499
500
501 const hw_module_t* hw_module;
502 unsigned int slot;
503 unsigned int update_mode = MODE_NORMAL;
504 unsigned int delta_size;
505 unsigned char full_header[9];
506
507 char digest_s[SHA_DIGEST_SIZE];
508 char digest_t[SHA_DIGEST_SIZE];
509
510
511 hw_module = &HAL_MODULE_INFO_SYM;
512
513 if (!hw_module ||
514 strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) {
515 ret = -EINVAL;
516 }
517 if (ret != 0) {
518 LYERRLOG("+[UA]: Error loading boot_control HAL implementation.\n");
519 return -1;
520 }
521
522 module = (boot_control_module_t*)hw_module;
523 module->init(module);
524
525
526 if (module == NULL) {
527 LYERRLOG("+[UA]: Error getting bootctrl module.\n");
528 return -1;
529 }
530 /************* Bootctrl Init End *************/
531
532 current_slot = module->getCurrentSlot(module);
533
534 int is_successful = module->isSlotMarkedSuccessful(module, current_slot);
535
536 LYVERBLOG("Booting slot = %d, : isSlotMarkedSuccessful= %d\n",current_slot,is_successful);
537
538
539
540 memset(&ctx, 0, sizeof(ctx));
541 ctx.rom_base = 0;
542 ctx.ram_base =(unsigned char *)&ram_buffer[0];
543 ctx.ram_len = ROCK_RAM_LEN;
544 ctx.backup_base = BACKUP_ADDR_FLAG;
545 ctx.backup_len = ROCK_DEFAULT_BLOCK_SIZE;
546 ctx.update_nvram = 0;
547 ctx.read_rom_directly = read_rom_directly;
548 //ctx.first_run = first_run;
549 ctx.first_run = 1;
550
551
552 init_dev_fd();
553
554
555 memset(&up_info, 0, sizeof(up_info));
556
557 memset(&fota_status,0,sizeof(fota_status));
558
559
560 lseek(fd_update_status,0,SEEK_SET);
561 read(fd_update_status,(unsigned char *)&up_info,sizeof(up_info));
562
563 LYVERBLOG("+[UA]: up_info.ota_run = %d\n",up_info.ota_run);
564
565
566 if ((up_info.ota_run>PATCH_BL33)||(up_info.ota_run<PATCH_SYSTEM))
567 {
568 up_info.ota_run = 0;
569 }
570
571 up_info.ota_run = 0;
572
573 if((up_info.fota_flag[0]=='A')&&(up_info.fota_flag[1]=='-')&&(up_info.fota_flag[2]=='B')){
574 update_mode = MODE_A2B;
575 }else if((up_info.fota_flag[0]=='B')&&(up_info.fota_flag[1]=='-')&&(up_info.fota_flag[2]=='A')){
576 update_mode = MODE_B2A;
577 }else{
578 update_mode = MODE_NORMAL;
579 }
580
581 LYVERBLOG("+[UA]: up_info.fota_flag = %s\n",up_info.fota_flag);
582 LYVERBLOG("+[UA]: update_mode = %d\n",update_mode);
583
584 memset(&da_head, 0, sizeof(da_head));
585
586 mtk_device_wrap_read(fd_delta, (unsigned char*)&da_head, sizeof(da_head));
587
588
589 rock_trace(&ctx, "da_head.sys:%d,da_head.boot:%d,da_head.tee:%d,da_head.md1img=%d,da_head.vbmeta=%d,da_head.bl33=%d\n", da_head.sys, da_head.boot,da_head.tee,da_head.md1img,da_head.vbmeta,da_head.bl33);
590 rock_trace(&ctx, "da_head.fullsys:%d,da_head.fullboot:%d,da_head.fulltee:%d,da_head.fullmd1img=%d,da_head.fullvbmeta=%d,da_head.fullbl33=%d\n", da_head.full_sys, da_head.full_boot,da_head.full_tee, da_head.full_md1img,da_head.full_vbmeta,da_head.full_bl33);
591
592
593 if (da_head.sys>0) {
594 fota_status.update_status[PATCH_SYSTEM - 1].need_update = 1;
595 }
596 if (da_head.boot>0) {
597 fota_status.update_status[PATCH_BOOT - 1].need_update = 1;
598 }
599 if (da_head.tee>0) {
600 fota_status.update_status[PATCH_TEE - 1].need_update = 1;
601 }
602 if (da_head.md1img>0) {
603 fota_status.update_status[PATCH_MD1IMG - 1].need_update = 1;
604 }
605
606 if (da_head.vbmeta>0) {
607 fota_status.update_status[PATCH_VBMETA - 1].need_update = 1;
608 }
609 if (da_head.bl33>0) {
610 fota_status.update_status[PATCH_BL33 - 1].need_update = 1;
611 }
612
613 if (da_head.full_sys>0) {
614 fota_status.update_status[FULL_SYSTEM - 1].need_update = 1;
615 }
616 if (da_head.full_boot>0) {
617 fota_status.update_status[FULL_BOOT - 1].need_update = 1;
618 }
619 if (da_head.full_tee>0) {
620 fota_status.update_status[FULL_TEE - 1].need_update = 1;
621 }
622 if (da_head.full_md1img>0) {
623 fota_status.update_status[FULL_MD1IMG - 1].need_update = 1;
624 }
625 if (da_head.full_bl33>0) {
626 fota_status.update_status[FULL_BL33 - 1].need_update = 1;
627 }
628
629 if(da_head.full_vbmeta>0) {
630 fota_status.update_status[FULL_VBMETA - 1].need_update = 1;
631 }
632 fota_status.switch_slot = WAIT;
633 save_fota_status();
634
635 delta_size = da_head.sys + da_head.boot + da_head.tee + da_head.md1img + da_head.vbmeta + da_head.bl33;
636
637
638 if ((da_head.sys>0) && (up_info.ota_run <= PATCH_SYSTEM))
639 {
640
641 now_patch = PATCH_SYSTEM;
642 delta_offset = DELTA_HEARD_SIZE;
643
644#if 0
645 if (up_info.ota_run == PATCH_SYSTEM)
646 {
647 ctx.first_run = 0;
648
649 }else{
650 ctx.first_run = 1;
651 }
652
653 //up_info.ota_run = 0;
654
655
656
657 if(current_slot==SLOT_B) {
658 fd_system_a = mtk_device_wrap_open(DEV_SYSTEM_A,O_RDWR);
659 if (fd_system_a < 0) {
660 err = errno;
661 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
662 return -err;
663 }
664 fd_curr = fd_system_a;
665 }else{
666 fd_system_b = mtk_device_wrap_open(DEV_SYSTEM_B,O_RDWR);
667 if (fd_system_b < 0) {
668 err = errno;
669 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
670 return -err;
671 }
672 fd_curr = fd_system_b;
673 }
674
675#endif
676
677
678 ctx.first_run = 1; // always
679
680
681
682 if(current_slot==SLOT_B) {
683 system("flash_eraseall /dev/disk/by-partlabel/system_a");
684 } else {
685 system("flash_eraseall /dev/disk/by-partlabel/system_b");
686 }
687
688
689// if(current_slot==SLOT_B) {
690 fd_system_a = mtk_device_wrap_open(DEV_SYSTEM_A,O_RDWR);
691 if (fd_system_a < 0) {
692 err = errno;
693 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
694 return -err;
695 }
696// fd_curr = fd_system_a;
697// }else{
698 fd_system_b = mtk_device_wrap_open(DEV_SYSTEM_B,O_RDWR);
699 if (fd_system_b < 0) {
700 err = errno;
701 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
702 return -err;
703 }
704// fd_curr = fd_system_b;
705// }
706
707 if(current_slot==SLOT_B){
708 fd_read = fd_system_b;
709 fd_write = fd_system_a;
710 } else {
711 fd_read = fd_system_a;
712 fd_write = fd_system_b;
713 }
714
715
716 fota_status.ota_run = PATCH_SYSTEM;
717 fota_status.update_status[PATCH_SYSTEM -1].check_delta = WAIT;
718 fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
719 fota_status.update_status[PATCH_SYSTEM-1].update_result= WAIT;
720
721 save_fota_status();
722
723 up_info.ota_run = PATCH_SYSTEM;
724 lseek(fd_update_status,0,SEEK_SET);
725 write(fd_update_status, &up_info,sizeof(up_info));
726 sync();
727 LYVERBLOG("+[UA]: Start upgrading system.\n");
728 status = iot_patch(&ctx);
729 LYVERBLOG("+[UA]: system upgrade result:%d\n",status);
730
731 //fota_status.ota_run = 0;
732 fota_status.update_status[PATCH_SYSTEM -1].update_result= status;
733 fota_status.update_result= status;
734
735 if((status == 0)||(status ==1))
736 {
737
738 fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
739 fota_status.update_status[PATCH_SYSTEM -1].check_rom = PASS;
740
741 }else if(status == E_ROCK_INVALID_DELTA) {
742 fota_status.update_status[PATCH_SYSTEM -1].check_delta = ERROR;
743 fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
744 }else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
745 fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
746 fota_status.update_status[PATCH_SYSTEM -1].check_rom = ERROR;
747
748 }else{
749
750 //fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
751 //fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
752 }
753
754 save_fota_status();
755
756
757 if ((status != 0) &&(status != 1))
758 {
759
760 up_info.fota_flag[0] = 'e';
761 up_info.fota_flag[1] = 'n';
762 up_info.fota_flag[2] = 'd';
763 up_info.update_result = status;
764 up_info.ota_run = 0;
765 lseek(fd_update_status,0,SEEK_SET);
766 write(fd_update_status, &up_info,sizeof(up_info));
767 sync();
768 close(fd_update_status);
769
770 mtk_device_wrap_close(fd_read);
771 mtk_device_wrap_close(fd_write);
772
773 return status;
774 }
775 mtk_device_wrap_close(fd_read);
776 mtk_device_wrap_close(fd_write);
777
778
779 }
780
781
782
783
784
785 if ((da_head.boot>0) && (up_info.ota_run <= PATCH_BOOT))
786 {
787
788 now_patch = PATCH_BOOT;
789 delta_offset = DELTA_HEARD_SIZE + da_head.sys;
790
791
792 if (up_info.ota_run == PATCH_BOOT)
793 {
794 ctx.first_run = 0;
795
796 }else{
797 ctx.first_run = 1;
798 }
799
800 if(current_slot==SLOT_B) {
801 system("flash_eraseall /dev/disk/by-partlabel/boot_a");
802 } else {
803 system("flash_eraseall /dev/disk/by-partlabel/boot_b");
804 }
805
806 //if(current_slot==SLOT_B) {
807 fd_boot_a = mtk_device_wrap_open(DEV_BOOT_A,O_RDWR);
808 if (fd_boot_a < 0) {
809 err = errno;
810 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
811 return -err;
812 }
813// fd_curr = fd_boot_a;
814// }else{
815 fd_boot_b = mtk_device_wrap_open(DEV_BOOT_B,O_RDWR);
816 if (fd_boot_b < 0) {
817 err = errno;
818 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
819 return -err;
820 }
821// fd_curr = fd_boot_b;
822// }
823
824
825 if(current_slot==SLOT_B){
826 fd_read = fd_boot_b;
827 fd_write = fd_boot_a;
828 } else {
829 fd_read = fd_boot_a;
830 fd_write = fd_boot_b;
831 }
832
833 fota_status.ota_run = PATCH_BOOT;
834 fota_status.update_status[PATCH_BOOT-1].check_delta = WAIT;
835 fota_status.update_status[PATCH_BOOT-1].check_rom = WAIT;
836 fota_status.update_status[PATCH_BOOT-1].update_result= WAIT;
837
838 save_fota_status();
839
840
841 up_info.ota_run = PATCH_BOOT;
842 lseek(fd_update_status,0,SEEK_SET);
843 write(fd_update_status, &up_info,sizeof(up_info));
844 sync();
845
846
847
848 LYVERBLOG("+[UA]: Start upgrading boot.\n");
849 status = iot_patch(&ctx);
850 LYVERBLOG("+[UA]: boot upgrade result:%d\n",status);
851 //up_info.ota_run = 0;
852
853 //fota_status.ota_run = 0;
854 fota_status.update_status[PATCH_BOOT-1].update_result= status;
855 fota_status.update_result= status;
856
857 if((status == 0)||(status ==1))
858 {
859
860 fota_status.update_status[PATCH_BOOT-1].check_delta = PASS;
861 fota_status.update_status[PATCH_BOOT-1].check_rom = PASS;
862
863 }else if(status == E_ROCK_INVALID_DELTA) {
864 fota_status.update_status[PATCH_BOOT-1].check_delta = ERROR;
865 fota_status.update_status[PATCH_BOOT-1].check_rom = WAIT;
866 }else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
867 fota_status.update_status[PATCH_BOOT-1].check_delta = PASS;
868 fota_status.update_status[PATCH_BOOT-1].check_rom = ERROR;
869
870 }else{
871
872 //fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
873 //fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
874 }
875
876 save_fota_status();
877
878
879
880 if ((status != 0) &&(status != 1))
881 {
882
883 up_info.fota_flag[0] = 'e';
884 up_info.fota_flag[1] = 'n';
885 up_info.fota_flag[2] = 'd';
886 up_info.update_result = status;
887 up_info.ota_run = 0;
888 lseek(fd_update_status,0,SEEK_SET);
889 write(fd_update_status, &up_info,sizeof(up_info));
890 sync();
891 close(fd_update_status);
892 mtk_device_wrap_close(fd_read);
893 mtk_device_wrap_close(fd_write);
894
895 return status;
896 }
897 mtk_device_wrap_close(fd_read);
898 mtk_device_wrap_close(fd_write);
899
900 }
901
902
903
904
905
906 if ((da_head.tee>0) && (up_info.ota_run <= PATCH_TEE))
907 {
908
909 now_patch = PATCH_TEE;
910 delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot;
911
912
913 if (up_info.ota_run == PATCH_TEE)
914 {
915 ctx.first_run = 0;
916
917 }else{
918 ctx.first_run = 1;
919 }
920
921
922 if(current_slot==SLOT_B) {
923 system("flash_eraseall /dev/disk/by-partlabel/tee_a");
924 } else {
925 system("flash_eraseall /dev/disk/by-partlabel/tee_b");
926 }
927// if(current_slot==SLOT_B) {
928 fd_tee_a = mtk_device_wrap_open(DEV_TEE_A,O_RDWR);
929 if (fd_tee_a < 0) {
930 err = errno;
931 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
932 return -err;
933 }
934// fd_curr = fd_tee_a;
935// }else{
936 fd_tee_b = mtk_device_wrap_open(DEV_TEE_B,O_RDWR);
937 if (fd_tee_b < 0) {
938 err = errno;
939 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
940 return -err;
941 }
942// fd_curr = fd_tee_b;
943// }
944
945
946 if(current_slot==SLOT_B){
947 fd_read = fd_tee_b;
948 fd_write = fd_tee_a;
949 } else {
950 fd_read = fd_tee_a;
951 fd_write = fd_tee_b;
952 }
953
954 fota_status.ota_run = PATCH_TEE;
955 fota_status.update_status[PATCH_TEE-1].check_delta = WAIT;
956 fota_status.update_status[PATCH_TEE-1].check_rom = WAIT;
957 fota_status.update_status[PATCH_TEE-1].update_result= WAIT;
958
959 save_fota_status();
960
961 up_info.ota_run = PATCH_TEE;
962
963 lseek(fd_update_status,0,SEEK_SET);
964 write(fd_update_status, &up_info,sizeof(up_info));
965 sync();
966
967 LYVERBLOG("+[UA]: Start upgrading tee.\n");
968 status = iot_patch(&ctx);
969 LYVERBLOG("+[UA]: tee upgrade result:%d\n",status);
970 //up_info.ota_run = 0;
971 //fota_status.ota_run = 0;
972 fota_status.update_status[PATCH_TEE-1].update_result= status;
973 fota_status.update_result= status;
974
975 if((status == 0)||(status ==1))
976 {
977
978 fota_status.update_status[PATCH_TEE-1].check_delta = PASS;
979 fota_status.update_status[PATCH_TEE-1].check_rom = PASS;
980
981 }else if(status == E_ROCK_INVALID_DELTA) {
982 fota_status.update_status[PATCH_TEE-1].check_delta = ERROR;
983 fota_status.update_status[PATCH_TEE-1].check_rom = WAIT;
984 }else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
985 fota_status.update_status[PATCH_TEE-1].check_delta = PASS;
986 fota_status.update_status[PATCH_TEE-1].check_rom = ERROR;
987
988 }else{
989
990 //fota_status.update_status[PATCH_SYSTEM -1].check_delta = PASS;
991 //fota_status.update_status[PATCH_SYSTEM -1].check_rom = WAIT;
992 }
993
994 save_fota_status();
995
996
997 if ((status != 0) &&(status != 1))
998 {
999
1000 up_info.fota_flag[0] = 'e';
1001 up_info.fota_flag[1] = 'n';
1002 up_info.fota_flag[2] = 'd';
1003 up_info.update_result = status;
1004 up_info.ota_run = 0;
1005 lseek(fd_update_status,0,SEEK_SET);
1006 write(fd_update_status, &up_info,sizeof(up_info));
1007 sync();
1008 close(fd_update_status);
1009 mtk_device_wrap_close(fd_read);
1010 mtk_device_wrap_close(fd_write);
1011 return status;
1012 }
1013 mtk_device_wrap_close(fd_read);
1014 mtk_device_wrap_close(fd_write);
1015
1016 }
1017
1018
1019
1020
1021 if ((da_head.md1img>0) && (up_info.ota_run <= PATCH_MD1IMG))
1022 {
1023
1024 now_patch = PATCH_MD1IMG;
1025 delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot + da_head.tee;
1026
1027
1028 if (up_info.ota_run == PATCH_MD1IMG)
1029 {
1030 ctx.first_run = 0;
1031
1032 }else{
1033 ctx.first_run = 1;
1034 }
1035
1036
1037 if(current_slot==SLOT_B) {
1038 system("flash_eraseall /dev/disk/by-partlabel/md1img_a");
1039 } else {
1040 system("flash_eraseall /dev/disk/by-partlabel/md1img_b");
1041 }
1042// if(current_slot==SLOT_B) {
1043 fd_md1img_a = mtk_device_wrap_open(DEV_MD1IMG_A,O_RDWR);
1044 if (fd_md1img_a < 0) {
1045 err = errno;
1046 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1047 return -err;
1048 }
1049// fd_curr = fd_md1img_a;
1050// }else{
1051 fd_md1img_b = mtk_device_wrap_open(DEV_MD1IMG_B,O_RDWR);
1052 if (fd_md1img_b < 0) {
1053 err = errno;
1054 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1055 return -err;
1056 }
1057// fd_curr = fd_md1img_b;
1058// }
1059
1060
1061 if(current_slot==SLOT_B){
1062 fd_read = fd_md1img_b;
1063 fd_write = fd_md1img_a;
1064 } else {
1065 fd_read = fd_md1img_a;
1066 fd_write = fd_md1img_b;
1067 }
1068
1069 fota_status.ota_run = PATCH_MD1IMG;
1070 fota_status.update_status[PATCH_MD1IMG-1].check_delta = WAIT;
1071 fota_status.update_status[PATCH_MD1IMG-1].check_rom = WAIT;
1072 fota_status.update_status[PATCH_MD1IMG-1].update_result= WAIT;
1073
1074 save_fota_status();
1075
1076 up_info.ota_run = PATCH_MD1IMG;
1077
1078 lseek(fd_update_status,0,SEEK_SET);
1079 write(fd_update_status, &up_info,sizeof(up_info));
1080 sync();
1081
1082
1083 LYVERBLOG("+[UA]: Start upgrading md1img.\n");
1084 status = iot_patch(&ctx);
1085 LYVERBLOG("+[UA]: md1img upgrade result:%d\n",status);
1086
1087 //fota_status.ota_run = 0;
1088 fota_status.update_status[PATCH_MD1IMG-1].update_result= status;
1089 fota_status.update_result= status;
1090
1091 if((status == 0)||(status ==1))
1092 {
1093
1094 fota_status.update_status[PATCH_MD1IMG-1].check_delta = PASS;
1095 fota_status.update_status[PATCH_MD1IMG-1].check_rom = PASS;
1096
1097 }else if(status == E_ROCK_INVALID_DELTA) {
1098 fota_status.update_status[PATCH_MD1IMG-1].check_delta = ERROR;
1099 fota_status.update_status[PATCH_MD1IMG-1].check_rom = WAIT;
1100 }else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
1101 fota_status.update_status[PATCH_MD1IMG-1].check_delta = PASS;
1102 fota_status.update_status[PATCH_MD1IMG-1].check_rom = ERROR;
1103
1104 }else{
1105
1106 //fota_status.update_status[PATCH_MD1IMG -1].check_delta = PASS;
1107 //fota_status.update_status[PATCH_MD1IMG -1].check_rom = WAIT;
1108 }
1109
1110 save_fota_status();
1111
1112
1113 if ((status != 0) &&(status != 1))
1114 {
1115
1116 up_info.fota_flag[0] = 'e';
1117 up_info.fota_flag[1] = 'n';
1118 up_info.fota_flag[2] = 'd';
1119 up_info.update_result = status;
1120 up_info.ota_run = 0;
1121 lseek(fd_update_status,0,SEEK_SET);
1122 write(fd_update_status, &up_info,sizeof(up_info));
1123 sync();
1124 close(fd_update_status);
1125 mtk_device_wrap_close(fd_read);
1126 mtk_device_wrap_close(fd_write);
1127 return status;
1128 }
1129 mtk_device_wrap_close(fd_read);
1130 mtk_device_wrap_close(fd_write);
1131
1132
1133 }
1134
1135
1136
1137
1138
1139
1140 if ((da_head.vbmeta>0) && (up_info.ota_run <= PATCH_VBMETA))
1141 {
1142
1143 now_patch = PATCH_VBMETA;
1144 delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot + da_head.tee + da_head.md1img;
1145
1146
1147 if (up_info.ota_run == PATCH_VBMETA)
1148 {
1149 ctx.first_run = 0;
1150
1151 }else{
1152 ctx.first_run = 1;
1153
1154 }
1155
1156 if(current_slot==SLOT_B) {
1157 system("flash_eraseall /dev/disk/by-partlabel/vbmeta_a");
1158 } else {
1159 system("flash_eraseall /dev/disk/by-partlabel/vbmeta_b");
1160 }
1161// if(current_slot==SLOT_B) {
1162 fd_vbmeta_a = mtk_device_wrap_open(DEV_VBMETA_A,O_RDWR);
1163 if (fd_vbmeta_a < 0) {
1164 err = errno;
1165 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1166 return -err;
1167 }
1168// fd_curr = fd_vbmeta_a;
1169// }else{
1170 fd_vbmeta_b = mtk_device_wrap_open(DEV_VBMETA_B,O_RDWR);
1171 if (fd_vbmeta_b < 0) {
1172 err = errno;
1173 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1174 return -err;
1175 }
1176// fd_curr = fd_vbmeta_b;
1177// }
1178
1179 if(current_slot==SLOT_B){
1180 fd_read = fd_vbmeta_b;
1181 fd_write = fd_vbmeta_a;
1182 } else {
1183 fd_read = fd_vbmeta_a;
1184 fd_write = fd_vbmeta_b;
1185 }
1186 fota_status.ota_run = PATCH_VBMETA;
1187 fota_status.update_status[PATCH_VBMETA-1].check_delta = WAIT;
1188 fota_status.update_status[PATCH_VBMETA-1].check_rom = WAIT;
1189 fota_status.update_status[PATCH_VBMETA-1].update_result= WAIT;
1190
1191 save_fota_status();
1192
1193 up_info.ota_run = PATCH_VBMETA;
1194
1195 lseek(fd_update_status,0,SEEK_SET);
1196 write(fd_update_status, &up_info,sizeof(up_info));
1197 sync();
1198
1199 LYVERBLOG("+[UA]: Start upgrading vbmeta.\n");
1200 status = iot_patch(&ctx);
1201 LYVERBLOG("+[UA]: vbmeta upgrade result:%d\n",status);
1202 up_info.ota_run = 0;
1203 //fota_status.ota_run = 0;
1204 fota_status.update_status[PATCH_VBMETA-1].update_result= status;
1205 fota_status.update_result= status;
1206
1207
1208 if((status == 0)||(status ==1))
1209 {
1210 fota_status.update_status[PATCH_VBMETA-1].check_delta = PASS;
1211 fota_status.update_status[PATCH_VBMETA-1].check_rom = PASS;
1212
1213 }else if(status == E_ROCK_INVALID_DELTA) {
1214 fota_status.update_status[PATCH_VBMETA-1].check_delta = ERROR;
1215 fota_status.update_status[PATCH_VBMETA-1].check_rom = WAIT;
1216 }else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
1217 fota_status.update_status[PATCH_VBMETA-1].check_delta = PASS;
1218 fota_status.update_status[PATCH_VBMETA-1].check_rom = ERROR;
1219 }else{
1220
1221 //fota_status.update_status[PATCH_MD1IMG -1].check_delta = PASS;
1222 //fota_status.update_status[PATCH_MD1IMG -1].check_rom = WAIT;
1223 }
1224
1225 save_fota_status();
1226 if ((status != 0) &&(status != 1))
1227 {
1228
1229 up_info.fota_flag[0] = 'e';
1230 up_info.fota_flag[1] = 'n';
1231 up_info.fota_flag[2] = 'd';
1232 up_info.update_result = status;
1233 up_info.ota_run = 0;
1234
1235 lseek(fd_update_status,0,SEEK_SET);
1236 write(fd_update_status, &up_info,sizeof(up_info));
1237 sync();
1238 close(fd_update_status);
1239 mtk_device_wrap_close(fd_read);
1240 mtk_device_wrap_close(fd_write);
1241 return status;
1242 }
1243 mtk_device_wrap_close(fd_read);
1244 mtk_device_wrap_close(fd_write);
1245
1246
1247 }
1248
1249 if ((da_head.bl33>0) && (up_info.ota_run <= PATCH_BL33))
1250 {
1251
1252 now_patch = PATCH_BL33;
1253 delta_offset = DELTA_HEARD_SIZE + da_head.sys + da_head.boot + da_head.tee + da_head.md1img + da_head.vbmeta;
1254
1255 if (up_info.ota_run == PATCH_BL33)
1256 {
1257 ctx.first_run = 0;
1258
1259 }else{
1260 ctx.first_run = 1;
1261 }
1262
1263 fd_bl33 = mtk_device_wrap_open(DEV_BL33,O_RDWR);
1264 if (fd_bl33 < 0) {
1265 err = errno;
1266 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1267 return -err;
1268 }
1269 fd_curr = fd_bl33;
1270
1271 fota_status.ota_run = PATCH_BL33;
1272 fota_status.update_status[PATCH_BL33-1].check_delta = WAIT;
1273 fota_status.update_status[PATCH_BL33-1].check_rom = WAIT;
1274 fota_status.update_status[PATCH_BL33-1].update_result= WAIT;
1275
1276 save_fota_status();
1277 up_info.ota_run = PATCH_BL33;
1278
1279 lseek(fd_update_status,0,SEEK_SET);
1280 write(fd_update_status, &up_info,sizeof(up_info));
1281 sync();
1282 LYVERBLOG("+[UA]: Start upgrading bl33.\n");
1283 status = iot_patch(&ctx);
1284 LYVERBLOG("+[UA]: bl33 upgrade result:%d\n",status);
1285
1286 up_info.ota_run = 0;
1287 //fota_status.ota_run = 0;
1288 fota_status.update_status[PATCH_BL33-1].update_result= status;
1289 fota_status.update_result= status;
1290 if((status == 0)||(status ==1))
1291 {
1292
1293 fota_status.update_status[PATCH_BL33-1].check_delta = PASS;
1294 fota_status.update_status[PATCH_BL33-1].check_rom = PASS;
1295
1296 }else if(status == E_ROCK_INVALID_DELTA) {
1297 fota_status.update_status[PATCH_BL33-1].check_delta = ERROR;
1298 fota_status.update_status[PATCH_BL33-1].check_rom = WAIT;
1299 }else if((status == E_ROCK_DELTA_MISMATCH)||(status == E_ROCK_DELTA_CHUNK_MISMATCH)) {
1300 fota_status.update_status[PATCH_BL33-1].check_delta = PASS;
1301 fota_status.update_status[PATCH_BL33-1].check_rom = ERROR;
1302
1303 }else{
1304
1305 //fota_status.update_status[PATCH_BL33 -1].check_delta = PASS;
1306 //fota_status.update_status[PATCH_BL33 -1].check_rom = WAIT;
1307 }
1308
1309 save_fota_status();
1310
1311 if ((status != 0) &&(status != 1))
1312 {
1313
1314 up_info.fota_flag[0] = 'e';
1315 up_info.fota_flag[1] = 'n';
1316 up_info.fota_flag[2] = 'd';
1317 up_info.update_result = status;
1318 up_info.ota_run = 0;
1319 lseek(fd_update_status,0,SEEK_SET);
1320 write(fd_update_status, &up_info,sizeof(up_info));
1321 mtk_device_wrap_close(fd_curr);
1322 sync();
1323 close(fd_update_status);
1324 return status;
1325 }
1326 mtk_device_wrap_close(fd_curr);
1327
1328 }
1329
1330
1331
1332 //if(current_slot==SLOT_B)
1333
1334
1335
1336 if ((da_head.full_sys>0)|| (da_head.full_boot>0)|| (da_head.full_tee>0)||(da_head.full_md1img>0)||(da_head.full_vbmeta>0)||(da_head.full_bl33>0))
1337 {
1338
1339 now_patch = 0;
1340 up_info.ota_run = 0;
1341
1342 memset(&fota_status,0,sizeof(fota_status));
1343 fota_status.switch_slot = WAIT;
1344 save_fota_status();
1345
1346 if (mtk_device_wrap_seek(fd_delta, DELTA_HEARD_SIZE + delta_size, SEEK_SET) < 0) {
1347 err = errno;
1348 LYERRLOG("+[UA]: mtk_device_wrap_seek df_delta err\n");
1349 return -1;
1350 }
1351
1352 mtk_device_wrap_read(fd_delta, full_header, DELTA_FULL_HEARD_SIZE);
1353
1354
1355 if (memcmp(full_header, "full-ota", DELTA_FULL_HEARD_SIZE) != 0) {
1356 LYERRLOG("+[UA]: invalid full delta header\r\n");
1357 up_info.fota_flag[0] = 'e';
1358 up_info.fota_flag[1] = 'n';
1359 up_info.fota_flag[2] = 'd';
1360 up_info.update_result = -1;
1361 up_info.ota_run = 0;
1362 lseek(fd_update_status,0,SEEK_SET);
1363 write(fd_update_status, &up_info,sizeof(up_info));
1364 //mtk_device_wrap_close(fd_curr);
1365 sync();
1366 close(fd_update_status);
1367
1368 for (i = FULL_SYSTEM;i<=FULL_BL33;i++){
1369 if (fota_status.update_status[i-1].need_update ==1) {
1370 fota_status.ota_run = i;
1371 fota_status.update_status[i-1].check_delta = ERROR;
1372 fota_status.update_status[i-1].check_rom= WAIT;
1373 fota_status.update_status[i-1].update_result= ERROR;
1374 }
1375 }
1376 fota_status.update_result = ERROR;
1377 save_fota_status();
1378 return -1;
1379 }
1380 }
1381
1382
1383
1384
1385
1386 if(da_head.full_sys>0) {
1387
1388 delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE;
1389
1390 if(current_slot==SLOT_B) {
1391 fd_system_a = mtk_device_wrap_open(DEV_SYSTEM_A,O_RDWR);
1392 if (fd_system_a < 0) {
1393 err = errno;
1394 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1395 return -err;
1396 }
1397 fd_curr = fd_system_a;
1398 }else{
1399 fd_system_b = mtk_device_wrap_open(DEV_SYSTEM_B,O_RDWR);
1400 if (fd_system_b < 0) {
1401 err = errno;
1402 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1403 return -err;
1404 }
1405 fd_curr = fd_system_b;
1406 }
1407 fota_status.ota_run = FULL_SYSTEM;
1408 save_fota_status();
1409
1410 retry_cnt = 0;
1411 LYVERBLOG("+[UA]: Start upgrading system full.\n");
1412 do {
1413 status = delta_copyto_nand(delta_offset,da_head.full_sys);
1414
1415 ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_sys,digest_s);
1416 ROCK_SHA_FILE(fd_curr,0,da_head.full_sys,digest_t);
1417 retry_cnt++;
1418 }while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
1419
1420 mtk_device_wrap_close(fd_curr);
1421
1422 LYVERBLOG("+[UA]: system full retry_cnt = %d\n",retry_cnt);
1423
1424 if (retry_cnt>3) {
1425 if (status == 0) {
1426 status = retry_cnt;
1427 }
1428 if(current_slot==SLOT_B) {
1429 nand_copyto_nand(DEV_SYSTEM_B,DEV_SYSTEM_A);
1430 }
1431 else{
1432 nand_copyto_nand(DEV_SYSTEM_A,DEV_SYSTEM_B);
1433 }
1434 }
1435
1436 LYVERBLOG("+[UA]: system full upgrade result:%d\n",status);
1437
1438 fota_status.update_result = status;
1439 fota_status.update_status[FULL_SYSTEM-1].update_result = status;
1440 save_fota_status();
1441
1442 if (status != 0)
1443 {
1444
1445 up_info.fota_flag[0] = 'e';
1446 up_info.fota_flag[1] = 'n';
1447 up_info.fota_flag[2] = 'd';
1448 up_info.update_result = status;
1449 up_info.ota_run = 0;
1450 lseek(fd_update_status,0,SEEK_SET);
1451 write(fd_update_status, &up_info,sizeof(up_info));
1452 sync();
1453 close(fd_update_status);
1454 return status;
1455 }
1456
1457
1458 }
1459
1460
1461
1462 if(da_head.full_boot>0) {
1463
1464 delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys;
1465
1466 if(current_slot==SLOT_B) {
1467 fd_boot_a = mtk_device_wrap_open(DEV_BOOT_A,O_RDWR);
1468 if (fd_boot_a < 0) {
1469 err = errno;
1470 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1471 return -err;
1472 }
1473 fd_curr = fd_boot_a;
1474 }else{
1475 fd_boot_b = mtk_device_wrap_open(DEV_BOOT_B,O_RDWR);
1476 if (fd_boot_b < 0) {
1477 err = errno;
1478 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1479 return -err;
1480 }
1481 fd_curr = fd_boot_b;
1482 }
1483 fota_status.ota_run = FULL_BOOT;
1484 save_fota_status();
1485 retry_cnt = 0;
1486 LYVERBLOG("+[UA]: Start upgrading boot full.\n");
1487 do {
1488 status = delta_copyto_nand(delta_offset,da_head.full_boot);
1489 ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_boot,digest_s);
1490 ROCK_SHA_FILE(fd_curr,0,da_head.full_boot,digest_t);
1491 retry_cnt++;
1492 }while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
1493
1494 LYVERBLOG("+[UA]: boot full retry_cnt = %d\n",retry_cnt);
1495 mtk_device_wrap_close(fd_curr);
1496
1497 if (retry_cnt>3) {
1498 if (status == 0) {
1499 status = retry_cnt;
1500 }
1501 if(current_slot==SLOT_B) {
1502 nand_copyto_nand(DEV_BOOT_B,DEV_BOOT_A);
1503 }
1504 else{
1505 nand_copyto_nand(DEV_BOOT_A,DEV_BOOT_B);
1506 }
1507 }
1508
1509
1510 LYVERBLOG("+[UA]: boot full upgrade result:%d\n",status);
1511 fota_status.update_result = status;
1512 fota_status.update_status[FULL_BOOT-1].update_result = status;
1513 save_fota_status();
1514
1515
1516 if (status != 0)
1517 {
1518
1519 up_info.fota_flag[0] = 'e';
1520 up_info.fota_flag[1] = 'n';
1521 up_info.fota_flag[2] = 'd';
1522 up_info.update_result = status;
1523 up_info.ota_run = 0;
1524 lseek(fd_update_status,0,SEEK_SET);
1525 write(fd_update_status, &up_info,sizeof(up_info));
1526 sync();
1527 close(fd_update_status);
1528 return status;
1529 }
1530
1531 }
1532
1533
1534 if(da_head.full_tee>0) {
1535
1536 delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys + da_head.full_boot;
1537
1538 if(current_slot==SLOT_B) {
1539 fd_tee_a = mtk_device_wrap_open(DEV_TEE_A,O_RDWR);
1540 if (fd_tee_a < 0) {
1541 err = errno;
1542 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1543 return -err;
1544 }
1545 fd_curr = fd_tee_a;
1546 }else{
1547 fd_tee_b = mtk_device_wrap_open(DEV_TEE_B,O_RDWR);
1548 if (fd_tee_b < 0) {
1549 err = errno;
1550 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1551 return -err;
1552 }
1553 fd_curr = fd_tee_b;
1554 }
1555 fota_status.ota_run = FULL_TEE;
1556 save_fota_status();
1557 retry_cnt = 0;
1558 LYVERBLOG("+[UA]: Start upgrading tee full.\n");
1559 do {
1560 status = delta_copyto_nand(delta_offset,da_head.full_tee);
1561 ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_tee,digest_s);
1562 ROCK_SHA_FILE(fd_curr,0,da_head.full_tee,digest_t);
1563 retry_cnt++;
1564 }while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
1565 mtk_device_wrap_close(fd_curr);
1566 LYVERBLOG("+[UA]: tee full retry_cnt = %d\n",retry_cnt);
1567 if (retry_cnt>3) {
1568 if (status == 0) {
1569 status = retry_cnt;
1570 }
1571 if(current_slot==SLOT_B) {
1572 nand_copyto_nand(DEV_TEE_B,DEV_TEE_A);
1573 }
1574 else{
1575 nand_copyto_nand(DEV_TEE_A,DEV_TEE_B);
1576 }
1577 }
1578
1579 printf("+[UA] tee full upgrade result:%d\n",status);
1580 fota_status.update_result = status;
1581 fota_status.update_status[FULL_TEE-1].update_result = status;
1582 save_fota_status();
1583
1584 if (status != 0)
1585 {
1586
1587 up_info.fota_flag[0] = 'e';
1588 up_info.fota_flag[1] = 'n';
1589 up_info.fota_flag[2] = 'd';
1590 up_info.update_result = status;
1591 up_info.ota_run = 0;
1592 lseek(fd_update_status,0,SEEK_SET);
1593 write(fd_update_status, &up_info,sizeof(up_info));
1594 sync();
1595 close(fd_update_status);
1596 return status;
1597 }
1598
1599
1600 }
1601
1602
1603 if(da_head.full_md1img>0) {
1604
1605 delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys + da_head.full_tee;
1606
1607 if(current_slot==SLOT_B) {
1608 fd_md1img_a = mtk_device_wrap_open(DEV_MD1IMG_A,O_RDWR);
1609 if (fd_md1img_a < 0) {
1610 err = errno;
1611 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1612 return -err;
1613 }
1614 fd_curr = fd_md1img_a;
1615 }else{
1616 fd_md1img_b = mtk_device_wrap_open(DEV_MD1IMG_B,O_RDWR);
1617 if (fd_md1img_b < 0) {
1618 err = errno;
1619 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1620 return -err;
1621 }
1622 fd_curr = fd_md1img_b;
1623 }
1624 fota_status.ota_run = FULL_MD1IMG;
1625 save_fota_status();
1626 retry_cnt = 0;
1627 LYVERBLOG("+[UA]: Start upgrading md1img full.\n");
1628 do {
1629 status = delta_copyto_nand(delta_offset,da_head.full_md1img);
1630 ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_md1img,digest_s);
1631 ROCK_SHA_FILE(fd_curr,0,da_head.full_md1img,digest_t);
1632 retry_cnt++;
1633 }while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
1634 mtk_device_wrap_close(fd_curr);
1635
1636 LYVERBLOG("+[UA]: md1img full retry_cnt = %d\n",retry_cnt);
1637 if (retry_cnt>3) {
1638 if (status == 0) {
1639 status = retry_cnt;
1640 }
1641 if(current_slot==SLOT_B) {
1642 nand_copyto_nand(DEV_MD1IMG_B,DEV_MD1IMG_A);
1643 }
1644 else{
1645 nand_copyto_nand(DEV_MD1IMG_A,DEV_MD1IMG_B);
1646 }
1647 }
1648
1649 LYVERBLOG("+[UA]: md1img upgrade result:%d\n",status);
1650 fota_status.update_result = status;
1651 fota_status.update_status[FULL_MD1IMG-1].update_result = status;
1652 save_fota_status();
1653
1654 if (status != 0)
1655 {
1656
1657 up_info.fota_flag[0] = 'e';
1658 up_info.fota_flag[1] = 'n';
1659 up_info.fota_flag[2] = 'd';
1660 up_info.update_result = status;
1661 up_info.ota_run = 0;
1662 lseek(fd_update_status,0,SEEK_SET);
1663 write(fd_update_status, &up_info,sizeof(up_info));
1664 sync();
1665 close(fd_update_status);
1666 return status;
1667 }
1668
1669
1670 }
1671
1672
1673 if(da_head.full_vbmeta>0) {
1674
1675 delta_offset = DELTA_HEARD_SIZE + delta_size + DELTA_FULL_HEARD_SIZE + da_head.full_sys + da_head.full_tee + da_head.full_md1img;
1676
1677 if(current_slot==SLOT_B) {
1678 fd_vbmeta_a = mtk_device_wrap_open(DEV_VBMETA_A,O_RDWR);
1679 if (fd_vbmeta_a < 0) {
1680 err = errno;
1681 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1682 return -err;
1683 }
1684 fd_curr = fd_vbmeta_a;
1685 }else{
1686 fd_vbmeta_b = mtk_device_wrap_open(DEV_VBMETA_B,O_RDWR);
1687 if (fd_vbmeta_b < 0) {
1688 err = errno;
1689 LYERRLOG("+[UA]: Error opening metadata file: %s\n",strerror(errno));
1690 return -err;
1691 }
1692 fd_curr = fd_vbmeta_b;
1693 }
1694 fota_status.ota_run = FULL_VBMETA;
1695 save_fota_status();
1696 retry_cnt = 0;
1697 LYVERBLOG("+[UA]: Start upgrading vbmeta full.\n");
1698 do {
1699 status = delta_copyto_nand(delta_offset,da_head.full_vbmeta);
1700 ROCK_SHA_FILE(fd_delta,delta_offset,da_head.full_vbmeta,digest_s);
1701 ROCK_SHA_FILE(fd_curr,0,da_head.full_vbmeta,digest_t);
1702 retry_cnt++;
1703 }while((strncmp(digest_s,digest_t,SHA_DIGEST_SIZE)!=0)&&(retry_cnt <= 3));
1704 mtk_device_wrap_close(fd_curr);
1705
1706 LYVERBLOG("+[UA]: vbmeta full retry_cnt = %d\n",retry_cnt);
1707 if (retry_cnt>3) {
1708 if (status == 0) {
1709 status = retry_cnt;
1710 }
1711 if(current_slot==SLOT_B) {
1712 nand_copyto_nand(DEV_VBMETA_B,DEV_VBMETA_A);
1713 }
1714 else{
1715 nand_copyto_nand(DEV_VBMETA_A,DEV_VBMETA_B);
1716 }
1717 }
1718
1719 LYVERBLOG("+[UA]: vbmeta upgrade result:%d\n",status);
1720 fota_status.update_result = status;
1721 fota_status.update_status[FULL_VBMETA-1].update_result = status;
1722 save_fota_status();
1723
1724 if (status != 0)
1725 {
1726
1727 up_info.fota_flag[0] = 'e';
1728 up_info.fota_flag[1] = 'n';
1729 up_info.fota_flag[2] = 'd';
1730 up_info.update_result = status;
1731 up_info.ota_run = 0;
1732 lseek(fd_update_status,0,SEEK_SET);
1733 write(fd_update_status, &up_info,sizeof(up_info));
1734 sync();
1735 close(fd_update_status);
1736 return status;
1737 }
1738
1739
1740 }
1741
1742 if(da_head.full_bl33>0) {
1743
1744 }
1745
1746
1747
1748 if(update_mode == MODE_NORMAL){ //need A VS B
1749 if(current_slot == SLOT_A) {
1750
1751 up_info.fota_flag[0] = 'B';
1752 up_info.fota_flag[1] = '-';
1753 up_info.fota_flag[2] = 'A';
1754
1755 }else{
1756
1757 up_info.fota_flag[0] = 'A';
1758 up_info.fota_flag[1] = '-';
1759 up_info.fota_flag[2] = 'B';
1760
1761 }
1762
1763 }else{
1764 up_info.fota_flag[0] = 'e';
1765 up_info.fota_flag[1] = 'n';
1766 up_info.fota_flag[2] = 'd';
1767
1768 }
1769
1770
1771 up_info.update_result = status;
1772 up_info.ota_run = 0;
1773 lseek(fd_update_status,0,SEEK_SET);
1774 write(fd_update_status, &up_info,sizeof(up_info));
1775 sync();
1776
1777 close_dev_fd(fd_delta);
1778
1779 //close_dev_fd(fd_curr);
1780
1781
1782
1783 close(fd_update_status);
1784 sync();
1785
1786
1787 slot = (current_slot == 0) ? 1 : 0;
1788
1789 LYVERBLOG("+[UA]: slot SLOT = %d\n\n",slot);
1790
1791 if(update_mode==MODE_NORMAL){
1792 module->setActiveBootSlot(module,slot);
1793 LYVERBLOG("+[UA]: upgrade is success!!!!\n");
1794 }
1795
1796 fota_status.ota_run = 0;
1797 fota_status.switch_slot = PASS;
1798 fota_status.update_result = status;
1799 save_fota_status();
1800
1801 if(update_mode==MODE_NORMAL){
1802 LYVERBLOG("+[UA]: reboot_device\n");
1803 reboot_device();
1804 }
1805
1806 return status;
1807}
1808
1809
1810
1811static void rock_fail_handler() {
1812
1813}
1814
1815
1816
1817
1818/* main entrpoint */
1819int lynq_rock_main(int first_run) {
1820
1821 int ret = 0;
1822
1823#if 0
1824
1825 printf("-********copy delta ***-\n");
1826 test_write_delta("/data/delta",DEV_DELTA);
1827
1828#endif
1829
1830 ret = rock_update_main(0, 0, 0, 0, first_run);
1831 if(ret) {
1832 rock_fail_handler();
1833 }
1834 return ret;
1835}
1836
1837#endif
1838
1839//lt add @2021.9.23 for deal with power down \ backup or upgrade.
1840int lynq_fota_func(void)
1841{
1842
1843 int fd;
1844 int first_run = 1;
1845 UPDATE_INFO lynq_up_info;
1846
1847 memset(&lynq_up_info, 0, sizeof(lynq_up_info));
1848
1849 fd = open(FILE_UPDATE_STATE,O_RDWR | O_CREAT,0777);
1850
1851 if (fd < 0) {
1852 return -1;
1853 }
1854
1855 read(fd,(unsigned char *)&lynq_up_info,sizeof(lynq_up_info));
1856 close(fd);
1857
1858 if(lynq_up_info.ota_run != 0) {
1859 //Power off, call UA
1860 LYVERBLOG("[+UP]: ***Power off, call UA***\n");
1861 lynq_rock_main(first_run);
1862 }else if (((lynq_up_info.fota_flag[0]=='A')&&(lynq_up_info.fota_flag[1]=='-')&&(lynq_up_info.fota_flag[2]=='B'))||((lynq_up_info.fota_flag[0]=='B')&&(lynq_up_info.fota_flag[1]=='-')&&(lynq_up_info.fota_flag[2]=='A'))){
1863 //Upgrade the other side and call UA
1864 LYVERBLOG("[+UP]: ***Upgrade the other side and call UA***\n");
1865 lynq_rock_main(first_run);
1866 }
1867/*
1868 else{
1869 //Normal procedure before, to check the upgrade package
1870// LYVERBLOG("[+UP]: ***Normal procedure before, to check the upgrade package***\n");
1871// lynq_rock_main(first_run);
1872 }*/
1873 return 0;
1874}