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