blob: 50ef62a81ffda5d20ca0a3437c0e4510c4905820 [file] [log] [blame]
b.liu4d51b682024-04-07 16:31:09 +08001#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <errno.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9
10#include "mbtk_device.h"
11
12/*
13* revision_out start from 0x1000.
14*/
15#define REVISION_OUT_ADDR 0x1000
16#define DEV_INFO_FILE_NAME "dev_info.bin"
17
18static void help()
19{
20 printf("ota_update -f [ota_bin] -v [new_revision_dir]\n");
21}
22
23/*
24*
25* ota_update -f [ota_bin] -v [new_revision_dir]
26*
27*/
28int main(int argc, char *argv[])
29{
30 int ch;
31 char ota_bin[128] = {0};
32 char dev_info_file[128] = {0};
33 while((ch = getopt(argc, argv, "f:v:"))!= -1)
34 {
35 switch(ch)
36 {
37 case 'f':
38 if(strlen(optarg) > 0)
39 memcpy(ota_bin, optarg, strlen(optarg));
40 break;
41 case 'v':
42 if(strlen(optarg) > 0)
43 memcpy(dev_info_file, optarg, strlen(optarg));
44 break;
45 default:
46 help();
47 return -1;
48 }
49 }
50 if(strlen(ota_bin) == 0 || strlen(dev_info_file) == 0)
51 {
52 help();
53 return -1;
54 }
55
56 printf("Ota Bin:%s, Revision Dir:%s\n", ota_bin, dev_info_file);
57
58 sprintf(dev_info_file + strlen(dev_info_file), "/%s", DEV_INFO_FILE_NAME);
59
60 if(access(ota_bin, F_OK))
61 {
62 printf("%s not exist.", ota_bin);
63 return -1;
64 }
65
66 if(access(dev_info_file, F_OK))
67 {
68 printf("%s not exist.", dev_info_file);
69 return -1;
70 }
71
72 int fd = open(dev_info_file, O_RDONLY);
73 if(fd < 0)
74 {
75 printf("Open(%s) fail:%d\n", dev_info_file, errno);
76 return -1;
77 }
78 mbtk_device_info_header_t info_header;
79 memset(&info_header, 0, sizeof(mbtk_device_info_header_t));
80 if(read(fd, &info_header, sizeof(mbtk_device_info_header_t)) != sizeof(mbtk_device_info_header_t)) {
81 printf("Read mbtk_device_info_header_t fail:%d\n", errno);
82 goto fail;
83 }
84
85 if(lseek(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, SEEK_SET) < 0)
86 {
87 printf("seek failed\n");
88 goto fail;
89 }
90
91 mbtk_device_info_basic_t info_basic;
92 memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
93 if(read(fd, &info_basic, sizeof(mbtk_device_info_basic_t)) != sizeof(mbtk_device_info_basic_t)) {
94 printf("Read mbtk_device_info_basic_t fail:%d\n", errno);
95 goto fail;
96 }
97
98 printf("New Revision:%s\n", info_basic.revision_out);
99
100 close(fd);
101 fd = open(ota_bin, O_WRONLY, 0644);
102 if(fd < 0)
103 {
104 printf("Open(%s) fail:%d\n", ota_bin, errno);
105 return -1;
106 }
107
108 if(lseek(fd, REVISION_OUT_ADDR, SEEK_SET) < 0)
109 {
110 printf("seek failed\n");
111 goto fail;
112 }
113
114 if(write(fd, info_basic.revision_out, strlen(info_basic.revision_out)) != strlen(info_basic.revision_out)) {
115 printf("Write revision failed\n");
116 goto fail;
117 }
118
119 printf("%s revision update to:%s\n", ota_bin, info_basic.revision_out);
120 close(fd);
121 return 0;
122fail:
123 close(fd);
124 return -1;
125}
126