blob: 190e8c0917342f999221f0f416c7e941ac862e1b [file] [log] [blame]
b.liu778645e2024-06-21 16:47:42 +08001#include "hd8040_upgrade.h"
2#include "port.h"
3#include <getopt.h>
4
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;
9
10extern int get_mon_ver(int uart_fd,uint8_t *ver_buf, int ver_len);
11
12#define READ_LEN_MAX 1024 //BOOT_UPGRADE_BUFF_MAX_1
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 //HDBD_LOG( "[%s %d]ret:%d\n", __FUNCTION__, __LINE__, ret);
46 i += ret;
47 if(ret == READ_LEN_MAX)
48 {
49 buff += READ_LEN_MAX;
50 }
51 else
52 {
53 break;
54 }
55 }
56
57 printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);
58 close(fp);
59 if(size != i)
60 {
61 return -1;
62 }
63 g_bin_buff_size = size;
64 return 0;
65}
66
67static void print_usage(const char *prog)
68{
69 printf("Usage: %s [-BUTVh]\n", prog);
70 puts(" -B --boot boot mode select\n"
71 " -U --user user mode select \n"
72 " -T --usertoboot usertoboot mode select \n"
73 " -V --version firmware version get \n"
74 " -h --help help message print \n");
75 exit(1);
76}
77
78struct option long_options[] = {
79{ "name", 0, NULL, 'n' },
80{ "bf_name", 0, NULL, 'b' },
81{ "love", 1, NULL, 'l' },
82{ 0, 0, 0, 0},
83};
84
85static const struct option lopts[] = {
86 { "boot", required_argument, NULL, 'B' },//required_argument
87 { "user", required_argument, NULL, 'U' },
88 { "usertoboot", required_argument, NULL, 'T' },
89 { "version", 0, NULL, 'V' },
90 { "help", 0, NULL, 'h' },
91 { 0, 0, 0, 0 },
92};
93char* const short_options1 = "B:U:T:Vh";
94
95int write_file(uint8_t *path, uint8_t *buff, int len)
96{
97 int fp = -1;
98 int ret = 0;
99 int i = 0;
100 int size = 0;
101
102 if (NULL == path || NULL == buff)
103 {
104 printf("[%s %d] str error", __FUNCTION__, __LINE__);
105 return -1;
106 }
107
108 fp = open((char *)path, O_RDWR );
109 if(fp < 0)
110 {
111 printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno);
112 return -1;
113 }
114
115 size = lseek(fp, 0x00, SEEK_END);
116
117 write(fp, buff, len);
118
119 printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);
120 close(fp);
121 return 0;
122}
123
124//example //升级过程中,禁止其他程序操作该串口,禁止断电复位等操作
125int main(int argc, char *argv[])
126{
127 uint8_t newVersionBuf[READ_MAX_LENGTH] = {0};
128 uint8_t cleintID[READ_MAX_LENGTH] = "999999";
129 int fd = -1; // uart handle
130 int ret = -1;
131 uint8_t filePath[256] = {0};
132 uint16_t filePtahLen = 0;
133
134 if (argc == 1)
135 {
136 printf("This program needs arguments....\n\n");
137 print_usage(argv[0]);
138 }
139
140 int num;
141 while ((num = getopt_long(argc, argv, short_options1, lopts, NULL)) != -1)
142 {
143 printf("getopt_long C:%d\n", num);
144 switch (num)
145 {
146
147 case 'B':
148 {
149 printf("optarg:%s\n", optarg);
150 if ( optarg == NULL )
151 {
152 printf("%s: option 'B' requires argument...\n", argv[0]);
153 break;
154 }
155 filePtahLen = strlen(optarg);
156 if(filePtahLen < sizeof(filePath))
157 {
158 memcpy((char *)filePath, optarg, filePtahLen);
159 goto HD_UPG;
160 }
161
162 //sleep(1);
163 }
164 break;
165
166 case 'U':
167 {
168 printf("userUpgMode\n");
169 }
170 break;
171 case 'T':
172 {
173 printf("userToBootMode\n");
174 }
175 break;
176 case 'V':
177 {
178 printf("check version\n");
179 //fd = uart_open((unsigned char *)PORT_NAME);//PORT_NAME234
180 fd = OpenUart((char *)PORT_NAME);//PORT_NAME234
181 if (fd < 0)
182 {
183
184 printf("open uart failed %d\n", fd);
185 return -1;
186 }
187 get_mon_ver(fd, newVersionBuf, sizeof(newVersionBuf));
188 printf("version is: %s\n", newVersionBuf);
189 uart_close(fd);
190 return 0;
191 }
192 break;
193 case 'h':
194 {
195 printf("333333333333\n");
196 //print_usage(argv[0]);
197 }
198 break;
199 default:
200 {
201 //print_usage(argv[0]);
202 }
203 break;
204 }
205 }
206
207 //芯片上电
208HD_UPG:
209 //...
210 //fd = uart_open((unsigned char *)PORT_NAME);//PORT_NAME234
211 fd = OpenUart((char *)PORT_NAME);//PORT_NAME234
212 if (fd < 0)
213 {
214
215 printf("open uart failed %d\n", fd);
216 return -1;
217 }
218 ret = read_bin_file(filePath, g_bin_buff);
219 if (ret < 0)
220 {
221 printf("open file failed %d\n", fd);
222 return -1;
223 }
224
225 // int runCount = 1;
226 // uint8_t resultBuf[256] = {0x00};
227 // //write_file("result.txt", "*********************start***********************************************", strlen("*********************start***********************************************"));
228 // while(runCount <= 2)
229 // {
230 // memset(g_bin_buff, 0x00, sizeof(g_bin_buff));
231 // if(runCount%2 == 0)
232 // {
233 // //ret = read_bin_file("HD8120.user2boot.9989.d4cae.e97e7.0620.bin", g_bin_buff);
234 // ret = read_bin_file("HD8125.ZTE.GN3.115200.0090.a2ff3.682bc.0815.GBQ.CFG.PPS13.ANT.LDO.JAMDetection.NAVPVT.bin", g_bin_buff);
235
236 // }
237 // else
238 // {
239 // //ret = read_bin_file("HD8120.user2boot.9989.d4cae.e97e7.0619.bin", g_bin_buff);
240 // ret = read_bin_file("HD8125.GHT.115200.9956.8da5b.7ecbc.230105R1.PPS9.LDO.GBQB1C.bin", g_bin_buff);
241 // }
242 // if (ret < 0)
243 // {
244 // HDBD_LOG("open file failed %d\n", fd);
245 // return -1;
246 // }
247
248 ret = fw_update_boot(fd, g_bin_buff, g_bin_buff_size);
249 if (ret < 0)
250 {
251 printf("open file failed %d\n", fd);
252 return -1;
253 }
254 //check result get version
255 /*
256 gnss_reset();
257 get_mon_ver(......)
258 check version
259
260
261 */
262 memset(resultBuf, 0x00, sizeof(resultBuf));
263 if(ret == HDBD_UPG_SUCESS)
264 {
265 sprintf(resultBuf, "result count num:%d, result:success! newVersion:%s.\r\n", runCount, newVersionBuf);
266 }
267 else
268 {
269 sprintf(resultBuf, "result count num:%d, result:failed! newVersion:%s.\r\n", runCount, newVersionBuf);
270 }
271 //write_file("result.txt", resultBuf, strlen(resultBuf));
272 runCount ++;
273 sleep(5);
274
275 write_file("result1.txt", "*********************end***********************************************", strlen("*********************end***********************************************"));
276 //ret = hdbd_enter_boot(fd, 115200, g_bin_buff_size, cleintID, newVersionBuf);
277 if(ret == HDBD_UPG_SUCESS)
278 {
279 printf("upgrade sucess!\r\n");
280 }
281 else
282 {
283 printf("upgrade FAIL, fail style:%d\r\n", ret);
284 }
285 uart_close(fd);
286 return ret;
287}
288/*
289HDBD_UPG_SUCESS:升级成功
290HDBD_UPG_ENTER_BOOTMODE_ERROR:进入boot失败(读取不到版本号或者串口发送时序不正确)
291HDBD_UPG_FAIL:升级失败
292HDBD_UPG_GETVER_ERROR:升级之后读取版本号失败
293HDBD_UPG_COM_CFG_ERROR:串口配置错误
294HDBD_UPG_IN_INFO_ERROR:输入的clientID错误或者输入的版本参数不正确
295HDBD_BIN_FILE_ERROR:Bin文件错误*/