blob: 6b9fdddc953845c5a2964403ddb9e251798b51a4 [file] [log] [blame]
xf.li3dd53742024-09-27 00:06:23 -07001#include "hd8040_upgrade.h"
2#include "port.h"
3#include <getopt.h>
4#include <include/lynq-gpio.h>
5#define READ_MAX_LENGTH 128
6#define PORT_NAME ("/dev/ttyS2")
7uint8_t g_bin_buff[500*1024] = {0};
8uint32_t g_bin_buff_size = 0;
9extern int get_mon_ver(int uart_fd,uint8_t *ver_buf, int ver_len);
10
11#define READ_LEN_MAX 1024 //BOOT_UPGRADE_BUFF_MAX_1
12
13int read_bin_file(uint8_t *path, uint8_t *buff)
14{
15 int fp = -1;
16 int ret = 0;
17 int i = 0;
18 int size = 0;
19
20 if (NULL == path || NULL == buff)
21 {
22 printf("[%s %d] str error", __FUNCTION__, __LINE__);
23 return -1;
24 }
25
26 fp = open((char *)path, O_RDONLY);
27 if(fp < 0)
28 {
29 printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno);
30 return -1;
31 }
32
33 size = lseek(fp, 0x00, SEEK_END);
34 if(size <= 0)
35 {
36 printf( "[%s %d]file is empty\n", __FUNCTION__, __LINE__);
37 return -1;
38 }
39
40 printf( "[%s %d]file size is:%d\n", __FUNCTION__, __LINE__, size);
41 lseek(fp, 0x00, SEEK_SET);
42 while(1)
43 {
44 ret = read(fp, buff, READ_LEN_MAX);
45 i += ret;
46 if(ret == READ_LEN_MAX)
47 {
48 buff += READ_LEN_MAX;
49 }
50 else
51 {
52 break;
53 }
54 }
55
56 printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);
57 close(fp);
58 if(size != i)
59 {
60 return -1;
61 }
62 g_bin_buff_size = size;
63 return 0;
64}
65
66static void print_usage(const char *prog)
67{
68 printf("Usage: %s [-BUTVh]\n", prog);
69 puts(" -B --boot boot mode select\n"
70 " -U --user user mode select \n"
71 " -T --usertoboot usertoboot mode select \n"
72 " -V --version firmware version get \n"
73 " -h --help help message print \n");
74 exit(1);
75}
76
77struct option long_options[] = {
78{ "name", 0, NULL, 'n' },
79{ "bf_name", 0, NULL, 'b' },
80{ "love", 1, NULL, 'l' },
81{ 0, 0, 0, 0},
82};
83
84static const struct option lopts[] = {
85{ "boot", required_argument, NULL, 'B' },//required_argument
86{ "user", required_argument, NULL, 'U' },
87{ "usertoboot", required_argument, NULL, 'T' },
88{ "version", 0, NULL, 'V' },
89{ "help", 0, NULL, 'h' },
90{ 0, 0, 0, 0 },
91};
92char* const short_options1 = "B:U:T:Vh";
93
94int write_file(uint8_t *path, uint8_t *buff, int len)
95{
96 int fp = -1;
97 int ret = 0;
98 int i = 0;
99 int size = 0;
100
101 if (NULL == path || NULL == buff)
102 {
103 printf("[%s %d] str error", __FUNCTION__, __LINE__);
104 return -1;
105 }
106
107 fp = open((char *)path, O_RDWR );
108 if(fp < 0)
109 {
110 printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno);
111 return -1;
112 }
113
114 size = lseek(fp, 0x00, SEEK_END);
115
116 write(fp, buff, len);
117
118 printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);
119 close(fp);
120 return 0;
121}
122
123//example //升级过程中,禁止其他程序操作该串口,禁止断电复位等操作
124int main(int argc, char *argv[])
125{
126 uint8_t newVersionBuf[READ_MAX_LENGTH] = {0};
127 uint8_t cleintID[READ_MAX_LENGTH] = "999999";
128 int fd = -1; // uart handle
129 int ret = -1;
130 uint8_t filePath[256] = {0};
131 uint16_t filePtahLen = 0;
132
133 if (argc == 1)
134 {
135 printf("This program needs arguments....\n\n");
136 print_usage(argv[0]);
137 }
138
139 int num;
140 while ((num = getopt_long(argc, argv, short_options1, lopts, NULL)) != -1)
141 {
142 printf("getopt_long C:%d\n", num);
143 switch (num)
144 {
145
146 case 'B':
147 {
148 printf("optarg:%s\n", optarg);
149 if ( optarg == NULL )
150 {
151 printf("%s: option 'B' requires argument...\n", argv[0]);
152 break;
153 }
154 filePtahLen = strlen(optarg);
155 if(filePtahLen < sizeof(filePath))
156 {
157 memcpy((char *)filePath, optarg, filePtahLen);
158 goto HD_UPG;
159 }
160
161 //sleep(1);
162 }
163 break;
164 case 'V':
165 {
166 printf("check version\n");
167 fd = OpenUart((char *)PORT_NAME);
168 if (fd < 0)
169 {
170
171 printf("open uart failed %d\n", fd);
172 return -1;
173 }
174 get_mon_ver(fd, newVersionBuf, sizeof(newVersionBuf));
175 printf("version is: %s\n", newVersionBuf);
176 uart_close(fd);
177 return 0;
178 }
179 break;
180
181 default:
182 {
183 printf("please input correct parameters\n");
184 }
185 break;
186 }
187 }
188
189 //芯片上电
190HD_UPG:
191 fd = OpenUart((char *)PORT_NAME);
192 if (fd < 0)
193 {
194
195 printf("open uart failed %d\n", fd);
196 return -1;
197 }
198 ret = read_bin_file(filePath, g_bin_buff);
199 if (ret < 0)
200 {
201 printf("open file failed %d\n", fd);
202 return -1;
203 }
204
205 /*go to boot-mode need change pin status*/
206 lynq_gpio_init(15,1,0,0);
207 lynq_gpio_value_set(15, 0);
208 usleep(100000);//100ms
209 lynq_gpio_init(126,1,1,0);
210 lynq_gpio_value_set(126, 1);
211 usleep(1000000);//1s
212 lynq_gpio_value_set(126, 0);
213 usleep(100000);//100ms
214 lynq_gpio_direction_set(15, 0);
215 lynq_gpio_pullsel_set(15, 0);
xf.li3dd53742024-09-27 00:06:23 -0700216 lynq_gpio_deinit(15);
217 lynq_gpio_deinit(126);
218
219 ret = fw_update_boot(fd, g_bin_buff, g_bin_buff_size);
220 if (ret < 0)
221 {
222 printf("open file failed %d\n", fd);
223 return -1;
224 }
225 sleep(1);
226 if(ret == HDBD_UPG_SUCESS)
227 {
228 printf("upgrade sucess!\r\n");
229 }
230 else
231 {
232 printf("upgrade FAIL, fail style:%d\r\n",ret);
233 }
234 uart_close(fd);
235 return ret;
236}