blob: 5be2289065bf464f4a77e103fa3c406dc3055615 [file] [log] [blame]
b.liu3a41a312024-02-28 09:57:39 +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
12static mbtk_device_info_header_t info_header = {
13 .tag = MBTK_DEVICE_INFO_PARTITION_TAG,
14 .version = MBTK_DEVICE_INFO_CURR_VERSION,
15 .item_count = MBTK_DEVICE_INFO_ITEM_NUM,
16 .item_header = {
17 {MBTK_DEVICE_INFO_ITEM_BASIC, 0},
18 {MBTK_DEVICE_INFO_ITEM_FOTA, 0},
19 {MBTK_DEVICE_INFO_ITEM_MODEM, 0},
20 {MBTK_DEVICE_INFO_ITEM_LOG, 0},
21 }
22};
23
24static mbtk_device_info_basic_t item_basic = {
25 .name = MBTK_DEVICE_INFO_ITEM_STR_BASIC,
26 .version = MBTK_DEVICE_INFO_CURR_VERSION,
27 .project = {0},
28 .project_cust = {0},
29 .ab_support = 1, // Default for ab system.
30 .revision_out = {0},
31 .revision_in = {0}
32};
33
34static mbtk_device_info_fota_t item_fota = {
35 .name = MBTK_DEVICE_INFO_ITEM_STR_FOTA,
36 .version = MBTK_DEVICE_INFO_CURR_VERSION,
37 .state = 0
38};
39
40static mbtk_device_info_modem_t item_modem = {
41 .name = MBTK_DEVICE_INFO_ITEM_STR_MODEM,
42 .version = MBTK_DEVICE_INFO_CURR_VERSION,
43 .band_area = MBTK_MODEM_BAND_AREA_ALL, // Default for all bands.
44 .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT,
45 .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT,
46 .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT,
47 .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT
48};
49
50static mbtk_device_info_log_t item_log = {
51 .name = MBTK_DEVICE_INFO_ITEM_STR_LOG,
52 .version = MBTK_DEVICE_INFO_CURR_VERSION,
53 .state = 0
54};
55
56static void help()
57{
58 printf("device_info_generate -a [a/ab] -b [revision_out] -c [revision_in] -d [project] -e [project_cust] -f [cn/eu/all] -o [out_bin]\n");
59}
60
61static int update_and_write_header(int fd, mbtk_device_info_header_t *header)
62{
63 header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr = sizeof(mbtk_device_info_header_t);
64 header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr + sizeof(mbtk_device_info_basic_t);
65 header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr + sizeof(mbtk_device_info_fota_t);
66 header->item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr + sizeof(mbtk_device_info_modem_t);
67
68 if(sizeof(mbtk_device_info_header_t) != write(fd, header, sizeof(mbtk_device_info_header_t))) {
69 printf("Write header fail:%d\n", errno);
70 return -1;
71 }
72
73 return 0;
74}
75
76static int write_item_basic(int fd, mbtk_device_info_basic_t *item_basic)
77{
78 if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) {
79 printf("Write item basic fail:%d\n", errno);
80 return -1;
81 }
82
83 return 0;
84}
85
86static int write_item_fota(int fd, mbtk_device_info_fota_t *item_fota)
87{
88 if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) {
89 printf("Write item fota fail:%d\n", errno);
90 return -1;
91 }
92
93 return 0;
94}
95
96static int write_item_modem(int fd, mbtk_device_info_modem_t *item_modem)
97{
98 if(sizeof(mbtk_device_info_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) {
99 printf("Write item modem fail:%d\n", errno);
100 return -1;
101 }
102
103 return 0;
104}
105
106static int write_item_log(int fd, mbtk_device_info_log_t *item_log)
107{
108 if(sizeof(mbtk_device_info_log_t) != write(fd, item_log, sizeof(mbtk_device_info_log_t))) {
109 printf("Write item log fail:%d\n", errno);
110 return -1;
111 }
112
113 return 0;
114}
115
116/*
117*
118* device_info_generate -a [a/ab] -b [revision_out] -c [revision_in] -d [project] -e [project_cust] -f [cn/eu/all] -o [out_bin]
119*
120*/
121int main(int argc, char *argv[])
122{
123 int ch;
124 char out_bin[128] = {0};
125 while((ch = getopt(argc, argv, "a:b:c:d:e:f:o:"))!= -1){
126 switch(ch)
127 {
128 case 'a':
129 if(strcmp(optarg, "ab") == 0) {
130 item_basic.ab_support = 1;
131 } else if(strcmp(optarg, "a") == 0) {
132 item_basic.ab_support = 0;
133 } else {
134 printf("Must be a/ab.\n");
135 return -1;
136 }
137 break;
138 case 'b':
139 if(strlen(optarg) > 0)
140 memcpy(item_basic.revision_out, optarg, strlen(optarg));
141 break;
142 case 'c':
143 if(strlen(optarg) > 0)
144 memcpy(item_basic.revision_in, optarg, strlen(optarg));
145 break;
146 case 'd':
147 if(strlen(optarg) > 0)
148 memcpy(item_basic.project, optarg, strlen(optarg));
149 break;
150 case 'e':
151 if(strlen(optarg) > 0)
152 memcpy(item_basic.project_cust, optarg, strlen(optarg));
153 break;
154 case 'f':
155 if(strcmp(optarg, "cn") == 0) {
156 item_modem.band_area = MBTK_MODEM_BAND_AREA_CN;
157 item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT;
158 item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT;
159 item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT;
160 item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT;
161 } else if(strcmp(optarg, "eu") == 0) {
162 item_modem.band_area = MBTK_MODEM_BAND_AREA_EU;
163 item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT;
164 item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT;
165 item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT;
166 item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT;
167 } else {
168 item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL;
169 item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
170 item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
171 item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
172 item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
173 printf("Set to default band.\n");
174 }
175 break;
176 case 'o':
177 if(strlen(optarg) > 0)
178 memcpy(out_bin, optarg, strlen(optarg));
179 break;
180 default:
181 help();
182 return -1;
183 }
184 }
185 if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) {
186 help();
187 return -1;
188 }
189
190 printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin);
191
192 int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644);
193 if(fd < 0) {
194 printf("Open(%s) fail:%d\n", out_bin, errno);
195 return -1;
196 }
197
198 if(update_and_write_header(fd, &info_header)) {
199 printf("update_and_write_header() fail.");
200 goto fail;
201 }
202
203 if(write_item_basic(fd, &item_basic)) {
204 printf("update_and_write_item_basic() fail.");
205 goto fail;
206 }
207
208 if(write_item_fota(fd, &item_fota)) {
209 printf("update_and_write_item_fota() fail.");
210 goto fail;
211 }
212
213 if(write_item_modem(fd, &item_modem)) {
214 printf("update_and_write_item_modem() fail.");
215 goto fail;
216 }
217
218 if(write_item_log(fd, &item_log)) {
219 printf("update_and_write_item_log() fail.");
220 goto fail;
221 }
222
223 printf("Success generate device_info bin:%s\n", out_bin);
224 close(fd);
225 return 0;
226fail:
227 close(fd);
228 return -1;
229}
230