blob: 190e8c0917342f999221f0f416c7e941ac862e1b [file] [log] [blame]
#include "hd8040_upgrade.h"
#include "port.h"
#include <getopt.h>
#define READ_MAX_LENGTH 128
#define PORT_NAME ("/dev/ttyS2")
uint8_t g_bin_buff[500*1024] = {0};
uint32_t g_bin_buff_size = 0;
extern int get_mon_ver(int uart_fd,uint8_t *ver_buf, int ver_len);
#define READ_LEN_MAX 1024 //BOOT_UPGRADE_BUFF_MAX_1
int read_bin_file(uint8_t *path, uint8_t *buff)
{
int fp = -1;
int ret = 0;
int i = 0;
int size = 0;
if (NULL == path || NULL == buff)
{
printf("[%s %d] str error", __FUNCTION__, __LINE__);
return -1;
}
fp = open((char *)path, O_RDONLY);
if(fp < 0)
{
printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno);
return -1;
}
size = lseek(fp, 0x00, SEEK_END);
if(size <= 0)
{
printf( "[%s %d]file is empty\n", __FUNCTION__, __LINE__);
return -1;
}
printf( "[%s %d]file size is:%d\n", __FUNCTION__, __LINE__, size);
lseek(fp, 0x00, SEEK_SET);
while(1)
{
ret = read(fp, buff, READ_LEN_MAX);
//HDBD_LOG( "[%s %d]ret:%d\n", __FUNCTION__, __LINE__, ret);
i += ret;
if(ret == READ_LEN_MAX)
{
buff += READ_LEN_MAX;
}
else
{
break;
}
}
printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);
close(fp);
if(size != i)
{
return -1;
}
g_bin_buff_size = size;
return 0;
}
static void print_usage(const char *prog)
{
printf("Usage: %s [-BUTVh]\n", prog);
puts(" -B --boot boot mode select\n"
" -U --user user mode select \n"
" -T --usertoboot usertoboot mode select \n"
" -V --version firmware version get \n"
" -h --help help message print \n");
exit(1);
}
struct option long_options[] = {
{ "name", 0, NULL, 'n' },
{ "bf_name", 0, NULL, 'b' },
{ "love", 1, NULL, 'l' },
{ 0, 0, 0, 0},
};
static const struct option lopts[] = {
{ "boot", required_argument, NULL, 'B' },//required_argument
{ "user", required_argument, NULL, 'U' },
{ "usertoboot", required_argument, NULL, 'T' },
{ "version", 0, NULL, 'V' },
{ "help", 0, NULL, 'h' },
{ 0, 0, 0, 0 },
};
char* const short_options1 = "B:U:T:Vh";
int write_file(uint8_t *path, uint8_t *buff, int len)
{
int fp = -1;
int ret = 0;
int i = 0;
int size = 0;
if (NULL == path || NULL == buff)
{
printf("[%s %d] str error", __FUNCTION__, __LINE__);
return -1;
}
fp = open((char *)path, O_RDWR );
if(fp < 0)
{
printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno);
return -1;
}
size = lseek(fp, 0x00, SEEK_END);
write(fp, buff, len);
printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);
close(fp);
return 0;
}
//example //升级过程中,禁止其他程序操作该串口,禁止断电复位等操作
int main(int argc, char *argv[])
{
uint8_t newVersionBuf[READ_MAX_LENGTH] = {0};
uint8_t cleintID[READ_MAX_LENGTH] = "999999";
int fd = -1; // uart handle
int ret = -1;
uint8_t filePath[256] = {0};
uint16_t filePtahLen = 0;
if (argc == 1)
{
printf("This program needs arguments....\n\n");
print_usage(argv[0]);
}
int num;
while ((num = getopt_long(argc, argv, short_options1, lopts, NULL)) != -1)
{
printf("getopt_long C:%d\n", num);
switch (num)
{
case 'B':
{
printf("optarg:%s\n", optarg);
if ( optarg == NULL )
{
printf("%s: option 'B' requires argument...\n", argv[0]);
break;
}
filePtahLen = strlen(optarg);
if(filePtahLen < sizeof(filePath))
{
memcpy((char *)filePath, optarg, filePtahLen);
goto HD_UPG;
}
//sleep(1);
}
break;
case 'U':
{
printf("userUpgMode\n");
}
break;
case 'T':
{
printf("userToBootMode\n");
}
break;
case 'V':
{
printf("check version\n");
//fd = uart_open((unsigned char *)PORT_NAME);//PORT_NAME234
fd = OpenUart((char *)PORT_NAME);//PORT_NAME234
if (fd < 0)
{
printf("open uart failed %d\n", fd);
return -1;
}
get_mon_ver(fd, newVersionBuf, sizeof(newVersionBuf));
printf("version is: %s\n", newVersionBuf);
uart_close(fd);
return 0;
}
break;
case 'h':
{
printf("333333333333\n");
//print_usage(argv[0]);
}
break;
default:
{
//print_usage(argv[0]);
}
break;
}
}
//芯片上电
HD_UPG:
//...
//fd = uart_open((unsigned char *)PORT_NAME);//PORT_NAME234
fd = OpenUart((char *)PORT_NAME);//PORT_NAME234
if (fd < 0)
{
printf("open uart failed %d\n", fd);
return -1;
}
ret = read_bin_file(filePath, g_bin_buff);
if (ret < 0)
{
printf("open file failed %d\n", fd);
return -1;
}
// int runCount = 1;
// uint8_t resultBuf[256] = {0x00};
// //write_file("result.txt", "*********************start***********************************************", strlen("*********************start***********************************************"));
// while(runCount <= 2)
// {
// memset(g_bin_buff, 0x00, sizeof(g_bin_buff));
// if(runCount%2 == 0)
// {
// //ret = read_bin_file("HD8120.user2boot.9989.d4cae.e97e7.0620.bin", g_bin_buff);
// ret = read_bin_file("HD8125.ZTE.GN3.115200.0090.a2ff3.682bc.0815.GBQ.CFG.PPS13.ANT.LDO.JAMDetection.NAVPVT.bin", g_bin_buff);
// }
// else
// {
// //ret = read_bin_file("HD8120.user2boot.9989.d4cae.e97e7.0619.bin", g_bin_buff);
// ret = read_bin_file("HD8125.GHT.115200.9956.8da5b.7ecbc.230105R1.PPS9.LDO.GBQB1C.bin", g_bin_buff);
// }
// if (ret < 0)
// {
// HDBD_LOG("open file failed %d\n", fd);
// return -1;
// }
ret = fw_update_boot(fd, g_bin_buff, g_bin_buff_size);
if (ret < 0)
{
printf("open file failed %d\n", fd);
return -1;
}
//check result get version
/*
gnss_reset();
get_mon_ver(......)
check version
*/
memset(resultBuf, 0x00, sizeof(resultBuf));
if(ret == HDBD_UPG_SUCESS)
{
sprintf(resultBuf, "result count num:%d, result:success! newVersion:%s.\r\n", runCount, newVersionBuf);
}
else
{
sprintf(resultBuf, "result count num:%d, result:failed! newVersion:%s.\r\n", runCount, newVersionBuf);
}
//write_file("result.txt", resultBuf, strlen(resultBuf));
runCount ++;
sleep(5);
write_file("result1.txt", "*********************end***********************************************", strlen("*********************end***********************************************"));
//ret = hdbd_enter_boot(fd, 115200, g_bin_buff_size, cleintID, newVersionBuf);
if(ret == HDBD_UPG_SUCESS)
{
printf("upgrade sucess!\r\n");
}
else
{
printf("upgrade FAIL, fail style:%d\r\n", ret);
}
uart_close(fd);
return ret;
}
/*
HDBD_UPG_SUCESS:升级成功
HDBD_UPG_ENTER_BOOTMODE_ERROR:进入boot失败(读取不到版本号或者串口发送时序不正确)
HDBD_UPG_FAIL:升级失败
HDBD_UPG_GETVER_ERROR:升级之后读取版本号失败
HDBD_UPG_COM_CFG_ERROR:串口配置错误
HDBD_UPG_IN_INFO_ERROR:输入的clientID错误或者输入的版本参数不正确
HDBD_BIN_FILE_ERROR:Bin文件错误*/