blob: 58afe45076f2d26253dbab01cfb28199ca340a72 [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
b.liuf678f992024-05-08 15:23:10 +080010#include "mbtk_type.h"
b.liu472cfaf2024-12-19 19:08:19 +080011
12#ifdef MBTK_DEV_INFO_VERSION_2
13#include "mbtk_device_v2.h"
14#else
b.liu3a41a312024-02-28 09:57:39 +080015#include "mbtk_device.h"
b.liu472cfaf2024-12-19 19:08:19 +080016#endif
17
b.liu3a41a312024-02-28 09:57:39 +080018
19static mbtk_device_info_header_t info_header = {
20 .tag = MBTK_DEVICE_INFO_PARTITION_TAG,
21 .version = MBTK_DEVICE_INFO_CURR_VERSION,
22 .item_count = MBTK_DEVICE_INFO_ITEM_NUM,
23 .item_header = {
b.liu472cfaf2024-12-19 19:08:19 +080024#ifdef MBTK_DEV_INFO_VERSION_2
25 {MBTK_DEVICE_INFO_ITEM_BASIC, MBTK_DEVICE_INFO_ITEM_ADDR_BASIC},
26 {MBTK_DEVICE_INFO_ITEM_FOTA, MBTK_DEVICE_INFO_ITEM_ADDR_FOTA},
27 {MBTK_DEVICE_INFO_ITEM_MODEM, MBTK_DEVICE_INFO_ITEM_ADDR_MODEM},
28 {MBTK_DEVICE_INFO_ITEM_LOG, MBTK_DEVICE_INFO_ITEM_ADDR_LOG},
29#else
b.liu3a41a312024-02-28 09:57:39 +080030 {MBTK_DEVICE_INFO_ITEM_BASIC, 0},
31 {MBTK_DEVICE_INFO_ITEM_FOTA, 0},
32 {MBTK_DEVICE_INFO_ITEM_MODEM, 0},
33 {MBTK_DEVICE_INFO_ITEM_LOG, 0},
b.liu472cfaf2024-12-19 19:08:19 +080034#endif
b.liu3a41a312024-02-28 09:57:39 +080035 }
36};
37
38static mbtk_device_info_basic_t item_basic = {
39 .name = MBTK_DEVICE_INFO_ITEM_STR_BASIC,
40 .version = MBTK_DEVICE_INFO_CURR_VERSION,
41 .project = {0},
42 .project_cust = {0},
43 .ab_support = 1, // Default for ab system.
b.liuf678f992024-05-08 15:23:10 +080044 .reboot_flag = MBTK_REBOOT_FLAG_NORMAL,
b.liu3a41a312024-02-28 09:57:39 +080045 .revision_out = {0},
b.liu8e0743a2024-08-29 16:53:16 +080046 .revision_in = {0},
47 .build_time = {0}
b.liu3a41a312024-02-28 09:57:39 +080048};
49
50static mbtk_device_info_fota_t item_fota = {
51 .name = MBTK_DEVICE_INFO_ITEM_STR_FOTA,
52 .version = MBTK_DEVICE_INFO_CURR_VERSION,
53 .state = 0
54};
55
56static mbtk_device_info_modem_t item_modem = {
57 .name = MBTK_DEVICE_INFO_ITEM_STR_MODEM,
58 .version = MBTK_DEVICE_INFO_CURR_VERSION,
59 .band_area = MBTK_MODEM_BAND_AREA_ALL, // Default for all bands.
b.liuf2ea8bf2025-01-09 15:07:34 +080060#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +080061 .net_pref = 15, // Default *band is 15
62 .net_support = MBTK_NET_SUPPORT_2G | MBTK_NET_SUPPORT_3G | MBTK_NET_SUPPORT_4G, // Default support 2G/3G/4G
b.liuf2ea8bf2025-01-09 15:07:34 +080063#endif
b.liu3a41a312024-02-28 09:57:39 +080064 .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT,
65 .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT,
66 .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT,
b.liuf678f992024-05-08 15:23:10 +080067 .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT,
b.liu61ad9172025-01-09 14:33:55 +080068 .band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT,
b.liuf2ea8bf2025-01-09 15:07:34 +080069#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +080070 .band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT,
71 .band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT,
72 .band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT,
73 .band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT
b.liuf2ea8bf2025-01-09 15:07:34 +080074#endif
b.liu3a41a312024-02-28 09:57:39 +080075};
76
77static mbtk_device_info_log_t item_log = {
78 .name = MBTK_DEVICE_INFO_ITEM_STR_LOG,
79 .version = MBTK_DEVICE_INFO_CURR_VERSION,
80 .state = 0
81};
82
83static void help()
84{
b.liu472cfaf2024-12-19 19:08:19 +080085#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +080086 printf("device_info_generate -a [a/ab] -b [revision_out] -c [revision_in] -d [project] -e [project_cust] -f [cn/eu/all] -g [build_time] -h [net_pref] -i [net_support] -o [out_bin]\n");
b.liu472cfaf2024-12-19 19:08:19 +080087#else
b.liu8e0743a2024-08-29 16:53:16 +080088 printf("device_info_generate -a [a/ab] -b [revision_out] -c [revision_in] -d [project] -e [project_cust] -f [cn/eu/all] -g [build_time] -o [out_bin]\n");
b.liu472cfaf2024-12-19 19:08:19 +080089#endif
b.liu3a41a312024-02-28 09:57:39 +080090}
91
92static int update_and_write_header(int fd, mbtk_device_info_header_t *header)
93{
b.liu472cfaf2024-12-19 19:08:19 +080094#ifdef MBTK_DEV_INFO_VERSION_2
95
96#else
b.liu3a41a312024-02-28 09:57:39 +080097 header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr = sizeof(mbtk_device_info_header_t);
98 header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr + sizeof(mbtk_device_info_basic_t);
99 header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr + sizeof(mbtk_device_info_fota_t);
100 header->item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr + sizeof(mbtk_device_info_modem_t);
b.liu472cfaf2024-12-19 19:08:19 +0800101#endif
b.liu3a41a312024-02-28 09:57:39 +0800102
103 if(sizeof(mbtk_device_info_header_t) != write(fd, header, sizeof(mbtk_device_info_header_t))) {
104 printf("Write header fail:%d\n", errno);
105 return -1;
106 }
107
108 return 0;
109}
110
b.liuec4485e2024-12-20 10:28:40 +0800111static int write_item_basic(int fd, uint32 addr, mbtk_device_info_basic_t *item_basic)
b.liu3a41a312024-02-28 09:57:39 +0800112{
b.liuec4485e2024-12-20 10:28:40 +0800113 if(-1 == lseek(fd, addr, SEEK_SET)) {
114 printf("lseek() fail:%d\n", errno);
115 return -1;
116 }
117
b.liu3a41a312024-02-28 09:57:39 +0800118 if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) {
119 printf("Write item basic fail:%d\n", errno);
120 return -1;
121 }
122
123 return 0;
124}
125
b.liuec4485e2024-12-20 10:28:40 +0800126static int write_item_fota(int fd, uint32 addr, mbtk_device_info_fota_t *item_fota)
b.liu3a41a312024-02-28 09:57:39 +0800127{
b.liuec4485e2024-12-20 10:28:40 +0800128 if(-1 == lseek(fd, addr, SEEK_SET)) {
129 printf("lseek() fail:%d\n", errno);
130 return -1;
131 }
132
b.liu3a41a312024-02-28 09:57:39 +0800133 if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) {
134 printf("Write item fota fail:%d\n", errno);
135 return -1;
136 }
137
138 return 0;
139}
140
b.liuec4485e2024-12-20 10:28:40 +0800141static int write_item_modem(int fd, uint32 addr, mbtk_device_info_modem_t *item_modem)
b.liu3a41a312024-02-28 09:57:39 +0800142{
b.liuec4485e2024-12-20 10:28:40 +0800143 if(-1 == lseek(fd, addr, SEEK_SET)) {
144 printf("lseek() fail:%d\n", errno);
145 return -1;
146 }
147
b.liu3a41a312024-02-28 09:57:39 +0800148 if(sizeof(mbtk_device_info_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) {
149 printf("Write item modem fail:%d\n", errno);
150 return -1;
151 }
152
153 return 0;
154}
155
b.liuec4485e2024-12-20 10:28:40 +0800156static int write_item_log(int fd, uint32 addr, mbtk_device_info_log_t *item_log)
b.liu3a41a312024-02-28 09:57:39 +0800157{
b.liuec4485e2024-12-20 10:28:40 +0800158 if(-1 == lseek(fd, addr, SEEK_SET)) {
159 printf("lseek() fail:%d\n", errno);
160 return -1;
161 }
162
b.liu3a41a312024-02-28 09:57:39 +0800163 if(sizeof(mbtk_device_info_log_t) != write(fd, item_log, sizeof(mbtk_device_info_log_t))) {
164 printf("Write item log fail:%d\n", errno);
165 return -1;
166 }
167
168 return 0;
169}
170
b.liuf2ea8bf2025-01-09 15:07:34 +0800171#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +0800172static char* net_support_str_get(uint32 net_support)
173{
174 static char net_str[100] = {0};
175
176 if(net_support & 0x01) { // GSM
177 if(strlen(net_str) > 0) {
178 strcat(net_str, "/2G");
179 } else {
180 strcat(net_str, "2G");
181 }
182 }
183
184 if(net_support & 0x02) { // WCDMA
185 if(strlen(net_str) > 0) {
186 strcat(net_str, "/3G");
187 } else {
188 strcat(net_str, "3G");
189 }
190 }
191
192 if(net_support & 0x04) { // LTE
193 if(strlen(net_str) > 0) {
194 strcat(net_str, "/4G");
195 } else {
196 strcat(net_str, "4G");
197 }
198 }
199
200 if(net_support & 0x08) { // NR
201 if(strlen(net_str) > 0) {
202 strcat(net_str, "/5G");
203 } else {
204 strcat(net_str, "5G");
205 }
206 }
207 return net_str;
208}
b.liuf2ea8bf2025-01-09 15:07:34 +0800209#endif
b.liu3a41a312024-02-28 09:57:39 +0800210/*
211*
b.liuf678f992024-05-08 15:23:10 +0800212* device_info_generate -a [a/ab] -b [revision_out] -c [revision_in] -d [project] -e [project_cust] -f [cn/eu/sa/all] -o [out_bin]
b.liu3a41a312024-02-28 09:57:39 +0800213*
214*/
215int main(int argc, char *argv[])
216{
217 int ch;
218 char out_bin[128] = {0};
b.liu472cfaf2024-12-19 19:08:19 +0800219#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +0800220 while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:h:i:o:"))!= -1){
b.liu472cfaf2024-12-19 19:08:19 +0800221#else
b.liu8e0743a2024-08-29 16:53:16 +0800222 while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:o:"))!= -1){
b.liu472cfaf2024-12-19 19:08:19 +0800223#endif
b.liu3a41a312024-02-28 09:57:39 +0800224 switch(ch)
225 {
226 case 'a':
227 if(strcmp(optarg, "ab") == 0) {
228 item_basic.ab_support = 1;
229 } else if(strcmp(optarg, "a") == 0) {
230 item_basic.ab_support = 0;
231 } else {
232 printf("Must be a/ab.\n");
233 return -1;
234 }
235 break;
236 case 'b':
237 if(strlen(optarg) > 0)
238 memcpy(item_basic.revision_out, optarg, strlen(optarg));
239 break;
240 case 'c':
241 if(strlen(optarg) > 0)
242 memcpy(item_basic.revision_in, optarg, strlen(optarg));
243 break;
244 case 'd':
245 if(strlen(optarg) > 0)
246 memcpy(item_basic.project, optarg, strlen(optarg));
247 break;
248 case 'e':
249 if(strlen(optarg) > 0)
250 memcpy(item_basic.project_cust, optarg, strlen(optarg));
251 break;
252 case 'f':
253 if(strcmp(optarg, "cn") == 0) {
254 item_modem.band_area = MBTK_MODEM_BAND_AREA_CN;
255 item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT;
256 item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT;
257 item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT;
258 item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800259 item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800260#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +0800261 item_modem.band_nr_3 = MBTK_BAND_CN_NR_3_DEFAULT;
262 item_modem.band_nr_2 = MBTK_BAND_CN_NR_2_DEFAULT;
263 item_modem.band_nr_1 = MBTK_BAND_CN_NR_1_DEFAULT;
264 item_modem.band_nr_0 = MBTK_BAND_CN_NR_0_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800265#endif
b.liu3a41a312024-02-28 09:57:39 +0800266 } else if(strcmp(optarg, "eu") == 0) {
267 item_modem.band_area = MBTK_MODEM_BAND_AREA_EU;
268 item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT;
269 item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT;
270 item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT;
271 item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800272 item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800273#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +0800274 item_modem.band_nr_3 = MBTK_BAND_EU_NR_3_DEFAULT;
275 item_modem.band_nr_2 = MBTK_BAND_EU_NR_2_DEFAULT;
276 item_modem.band_nr_1 = MBTK_BAND_EU_NR_1_DEFAULT;
277 item_modem.band_nr_0 = MBTK_BAND_EU_NR_0_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800278#endif
b.liuf678f992024-05-08 15:23:10 +0800279 } else if(strcmp(optarg, "sa") == 0) {
280 item_modem.band_area = MBTK_MODEM_BAND_AREA_SA;
281 item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT;
282 item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT;
283 item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT;
284 item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800285 item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800286#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +0800287 item_modem.band_nr_3 = MBTK_BAND_SA_NR_3_DEFAULT;
288 item_modem.band_nr_2 = MBTK_BAND_SA_NR_2_DEFAULT;
289 item_modem.band_nr_1 = MBTK_BAND_SA_NR_1_DEFAULT;
290 item_modem.band_nr_0 = MBTK_BAND_SA_NR_0_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800291#endif
b.liu3a41a312024-02-28 09:57:39 +0800292 } else {
293 item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL;
294 item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
295 item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
296 item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
297 item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800298 item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800299#ifdef MBTK_DEV_INFO_VERSION_2
b.liu61ad9172025-01-09 14:33:55 +0800300 item_modem.band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT;
301 item_modem.band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT;
302 item_modem.band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT;
303 item_modem.band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT;
b.liuf2ea8bf2025-01-09 15:07:34 +0800304#endif
b.liu3a41a312024-02-28 09:57:39 +0800305 printf("Set to default band.\n");
306 }
307 break;
b.liu472cfaf2024-12-19 19:08:19 +0800308#ifdef MBTK_DEV_INFO_VERSION_2
309 case 'h':
310 item_modem.net_pref = (uint32)atoi(optarg);
311 printf("Set net_pref to %d success.\n", item_modem.net_pref);
312 break;
b.liu61ad9172025-01-09 14:33:55 +0800313 case 'i':
314 item_modem.net_support = (uint32)atoi(optarg);
315 printf("Set net_support to %d(%s) success.\n", item_modem.net_support,
316 net_support_str_get(item_modem.net_support));
317 break;
b.liu472cfaf2024-12-19 19:08:19 +0800318#endif
b.liu8e0743a2024-08-29 16:53:16 +0800319 case 'g':
320 if(strlen(optarg) > 0)
321 memcpy(item_basic.build_time, optarg, strlen(optarg));
322 break;
b.liu3a41a312024-02-28 09:57:39 +0800323 case 'o':
324 if(strlen(optarg) > 0)
325 memcpy(out_bin, optarg, strlen(optarg));
326 break;
327 default:
328 help();
329 return -1;
330 }
331 }
332 if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) {
333 help();
334 return -1;
335 }
336
337 printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin);
338
339 int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644);
340 if(fd < 0) {
341 printf("Open(%s) fail:%d\n", out_bin, errno);
342 return -1;
343 }
344
345 if(update_and_write_header(fd, &info_header)) {
346 printf("update_and_write_header() fail.");
347 goto fail;
348 }
349
b.liuec4485e2024-12-20 10:28:40 +0800350 if(write_item_basic(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, &item_basic)) {
b.liu3a41a312024-02-28 09:57:39 +0800351 printf("update_and_write_item_basic() fail.");
352 goto fail;
353 }
354
b.liuec4485e2024-12-20 10:28:40 +0800355 if(write_item_fota(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr, &item_fota)) {
b.liu3a41a312024-02-28 09:57:39 +0800356 printf("update_and_write_item_fota() fail.");
357 goto fail;
358 }
359
b.liuec4485e2024-12-20 10:28:40 +0800360 if(write_item_modem(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr, &item_modem)) {
b.liu3a41a312024-02-28 09:57:39 +0800361 printf("update_and_write_item_modem() fail.");
362 goto fail;
363 }
364
b.liuec4485e2024-12-20 10:28:40 +0800365 if(write_item_log(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr, &item_log)) {
b.liu3a41a312024-02-28 09:57:39 +0800366 printf("update_and_write_item_log() fail.");
367 goto fail;
368 }
369
370 printf("Success generate device_info bin:%s\n", out_bin);
371 close(fd);
372 return 0;
373fail:
374 close(fd);
375 return -1;
376}
377