1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4 #include "2ndboot.h"
5 #include "updater.h"
6
ota_patch_show_percent(int per)7 int ota_patch_show_percent(int per)
8 {
9 int ret = 0;
10 OTA_LOG_I("Show upgrade percent:%d \n", per);
11 return ret;
12 }
13
ota_updater(void)14 int ota_updater(void)
15 {
16 int ret = -1;
17 ota_boot_param_t ota_param = {0};
18 ret = ota_patch_read_param(&ota_param);
19 if(ret < 0) {
20 OTA_LOG_I("read param err.\n");
21 return -1;
22 }
23 ota_param.boot_count++;
24 OTA_LOG_I("ota upg_flag:0x%04x count:%d crc;0x%04x \n", ota_param.upg_flag, ota_param.boot_count, ota_param.param_crc);
25 if(ota_param.upg_flag == OTA_UPGRADE_DIFF) {
26 ret = ota_nbpatch_main();
27 } else if(ota_param.upg_flag == OTA_UPGRADE_XZ) {
28 ret = ota_xz_main();
29 } else if(ota_param.upg_flag == OTA_UPGRADE_ALL) {
30 if(ota_param.boot_type == 0) {
31 ret = ota_image_check(ota_param.src_adr, ota_param.len, ota_param.crc);
32 if(ret < 0) {
33 OTA_LOG_I("CRC fail.\n");
34 goto EXIT;
35 }
36 ret = ota_image_copy(ota_param.dst_adr, ota_param.src_adr, ota_param.len);
37 } else {
38 OTA_LOG_I("dual boot, not copy.\n");
39 }
40 } else {
41 OTA_LOG_I("No OTA upgrade.\n");
42 return 0;
43 }
44
45 EXIT:
46 OTA_LOG_I("ota update complete ret:%d \n", ret);
47 if(ret < 0 && ota_param.boot_count <= 3) {
48 ota_patch_write_param(&ota_param);
49 sys_delayms(100);
50 sys_reboot();
51 } else {
52 if(ota_param.upg_flag != 0) {
53 ota_param.upg_flag = 0;
54 ota_patch_write_param(&ota_param);
55 }
56 if(ota_param.boot_count > 3) { /* todo: rollback old version for dual banker boot. */
57 }
58 }
59 return ret;
60 }
61
ota_2ndboot_error(void * errinfo)62 void ota_2ndboot_error(void *errinfo)
63 {
64 printf(errinfo);
65 sys_reboot();
66 }
67
print_usage(void)68 void print_usage(void)
69 {
70 printf("2ndboot ver: " SYSINFO_2NDBOOT_VERSION "\r\n");
71 #ifdef AOS_2ND_BOOT_AB
72 printf("Please input 1-4 to select functions\r\n");
73 #else
74 printf("Please input 1-2 to select functions\r\n");
75 #endif
76 printf("[1] Uart Ymodem Upgrade \r\n");
77 printf("[2] System Reboot \r\n");
78 #ifdef AOS_2ND_BOOT_AB
79 printf("[3] Show AB Boot \r\n");
80 printf("[4] Switch AB Boot \r\n");
81 #endif
82 printf("[h] Help Info\r\n");
83 }
84
ota_2ndboot_cli_menu(void)85 static void ota_2ndboot_cli_menu(void)
86 {
87 unsigned char c = 0;
88 #ifdef AOS_2ND_BOOT_AB
89 int ab = 0;
90 #endif
91
92 print_usage();
93 printf("\r\naos boot# ");
94
95 while(1) {
96 if(uart_recv_byte(&c)) {
97 if('w' == c) {
98 continue;
99 }
100 if( ('\r' == c) || ('\n' == c) ) {
101 printf("\r\naos boot# ");
102 continue;
103 }
104 printf("%c \r\n", c);
105 switch(c) {
106 case '1' :
107 ymodem_upgrade();
108 break;
109 case '2' :
110 printf("Reboot \r\n");
111 sys_reboot();
112 break;
113 #ifdef AOS_2ND_BOOT_AB
114 case '3' :
115 ab = ota_2ndboot_ab_get();
116 printf("%s Boot .. \r\n", (ab == 0)?"A":"B");
117 break;
118 case '4' :
119 ab = ota_2ndboot_ab_get();
120 printf("Switch %s to %s Boot .. \r\n", (ab == 0)?"A":"B", (ab == 0)?"B":"A");
121 ota_2ndboot_ab_switch();
122 break;
123 #endif
124 default:
125 print_usage();
126 break;
127 }
128 printf("\r\naos boot# ");
129 sys_delayms(1);
130 }
131 }
132 }
133
ota_2ndboot_init()134 static int ota_2ndboot_init()
135 {
136 #ifdef AOS_2ND_BOOT_NO_LDS
137 _rec_heap_start = sys_set_heap(&_rec_heap_len);
138 #endif
139 }
140
ota_2ndboot_main(void)141 int ota_2ndboot_main(void)
142 {
143 int ret = 0;
144 unsigned char c = 0;
145 unsigned int i = 0;
146
147 ota_2ndboot_init();
148 uart_init();
149 printf("\r\nPress key \'w\' to 2ndboot cli menu in 100ms.\r\n");
150 while(1) {
151 if(uart_recv_byte(&c) && ('w' == c)) {
152 ota_2ndboot_cli_menu();
153 return 0;
154 }
155 i ++;
156 if(i >= 100)break;
157 sys_delayms(1);
158 }
159 wdg_init(OTA_2NDBOOT_WDG_TIMEOUT*1000);
160 wdg_feed();
161 /* check OTA upgrade */
162 ret = ota_updater();
163 wdg_finish();
164 return ret;
165 }
166