blob: 4d4ec37dbf53773a40178dd9f1fb81cf151c0dea [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.liu3a41a312024-02-28 09:57:39 +080011#include "mbtk_device.h"
12
13static mbtk_device_info_header_t info_header = {
14 .tag = MBTK_DEVICE_INFO_PARTITION_TAG,
15 .version = MBTK_DEVICE_INFO_CURR_VERSION,
16 .item_count = MBTK_DEVICE_INFO_ITEM_NUM,
17 .item_header = {
b.liu472cfaf2024-12-19 19:08:19 +080018 {MBTK_DEVICE_INFO_ITEM_BASIC, MBTK_DEVICE_INFO_ITEM_ADDR_BASIC},
19 {MBTK_DEVICE_INFO_ITEM_FOTA, MBTK_DEVICE_INFO_ITEM_ADDR_FOTA},
20 {MBTK_DEVICE_INFO_ITEM_MODEM, MBTK_DEVICE_INFO_ITEM_ADDR_MODEM},
21 {MBTK_DEVICE_INFO_ITEM_LOG, MBTK_DEVICE_INFO_ITEM_ADDR_LOG},
b.liu3a41a312024-02-28 09:57:39 +080022 }
23};
24
25static mbtk_device_info_basic_t item_basic = {
26 .name = MBTK_DEVICE_INFO_ITEM_STR_BASIC,
27 .version = MBTK_DEVICE_INFO_CURR_VERSION,
28 .project = {0},
29 .project_cust = {0},
30 .ab_support = 1, // Default for ab system.
b.liuf678f992024-05-08 15:23:10 +080031 .reboot_flag = MBTK_REBOOT_FLAG_NORMAL,
b.liu3a41a312024-02-28 09:57:39 +080032 .revision_out = {0},
b.liu8e0743a2024-08-29 16:53:16 +080033 .revision_in = {0},
34 .build_time = {0}
b.liu3a41a312024-02-28 09:57:39 +080035};
36
37static mbtk_device_info_fota_t item_fota = {
38 .name = MBTK_DEVICE_INFO_ITEM_STR_FOTA,
39 .version = MBTK_DEVICE_INFO_CURR_VERSION,
40 .state = 0
41};
42
43static mbtk_device_info_modem_t item_modem = {
44 .name = MBTK_DEVICE_INFO_ITEM_STR_MODEM,
45 .version = MBTK_DEVICE_INFO_CURR_VERSION,
46 .band_area = MBTK_MODEM_BAND_AREA_ALL, // Default for all bands.
b.liu61ad9172025-01-09 14:33:55 +080047 .net_pref = 15, // Default *band is 15
48 .net_support = MBTK_NET_SUPPORT_2G | MBTK_NET_SUPPORT_3G | MBTK_NET_SUPPORT_4G, // Default support 2G/3G/4G
b.liu3a41a312024-02-28 09:57:39 +080049 .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT,
50 .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT,
51 .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT,
b.liuf678f992024-05-08 15:23:10 +080052 .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT,
b.liu61ad9172025-01-09 14:33:55 +080053 .band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT,
54 .band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT,
55 .band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT,
56 .band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT,
57 .band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT
b.liu3a41a312024-02-28 09:57:39 +080058};
59
60static mbtk_device_info_log_t item_log = {
61 .name = MBTK_DEVICE_INFO_ITEM_STR_LOG,
62 .version = MBTK_DEVICE_INFO_CURR_VERSION,
63 .state = 0
64};
65
66static void help()
67{
b.liu61ad9172025-01-09 14:33:55 +080068 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.liu3a41a312024-02-28 09:57:39 +080069}
70
71static int update_and_write_header(int fd, mbtk_device_info_header_t *header)
72{
b.liu3a41a312024-02-28 09:57:39 +080073 if(sizeof(mbtk_device_info_header_t) != write(fd, header, sizeof(mbtk_device_info_header_t))) {
74 printf("Write header fail:%d\n", errno);
75 return -1;
76 }
77
78 return 0;
79}
80
b.liuec4485e2024-12-20 10:28:40 +080081static int write_item_basic(int fd, uint32 addr, mbtk_device_info_basic_t *item_basic)
b.liu3a41a312024-02-28 09:57:39 +080082{
b.liuec4485e2024-12-20 10:28:40 +080083 if(-1 == lseek(fd, addr, SEEK_SET)) {
84 printf("lseek() fail:%d\n", errno);
85 return -1;
86 }
87
b.liu3a41a312024-02-28 09:57:39 +080088 if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) {
89 printf("Write item basic fail:%d\n", errno);
90 return -1;
91 }
92
93 return 0;
94}
95
b.liuec4485e2024-12-20 10:28:40 +080096static int write_item_fota(int fd, uint32 addr, mbtk_device_info_fota_t *item_fota)
b.liu3a41a312024-02-28 09:57:39 +080097{
b.liuec4485e2024-12-20 10:28:40 +080098 if(-1 == lseek(fd, addr, SEEK_SET)) {
99 printf("lseek() fail:%d\n", errno);
100 return -1;
101 }
102
b.liu3a41a312024-02-28 09:57:39 +0800103 if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) {
104 printf("Write item fota 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_modem(int fd, uint32 addr, mbtk_device_info_modem_t *item_modem)
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_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) {
119 printf("Write item modem 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_log(int fd, uint32 addr, mbtk_device_info_log_t *item_log)
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_log_t) != write(fd, item_log, sizeof(mbtk_device_info_log_t))) {
134 printf("Write item log fail:%d\n", errno);
135 return -1;
136 }
137
138 return 0;
139}
140
b.liu61ad9172025-01-09 14:33:55 +0800141static char* net_support_str_get(uint32 net_support)
142{
143 static char net_str[100] = {0};
144
145 if(net_support & 0x01) { // GSM
146 if(strlen(net_str) > 0) {
147 strcat(net_str, "/2G");
148 } else {
149 strcat(net_str, "2G");
150 }
151 }
152
153 if(net_support & 0x02) { // WCDMA
154 if(strlen(net_str) > 0) {
155 strcat(net_str, "/3G");
156 } else {
157 strcat(net_str, "3G");
158 }
159 }
160
161 if(net_support & 0x04) { // LTE
162 if(strlen(net_str) > 0) {
163 strcat(net_str, "/4G");
164 } else {
165 strcat(net_str, "4G");
166 }
167 }
168
169 if(net_support & 0x08) { // NR
170 if(strlen(net_str) > 0) {
171 strcat(net_str, "/5G");
172 } else {
173 strcat(net_str, "5G");
174 }
175 }
176 return net_str;
177}
b.liu9f91db32025-02-13 11:24:18 +0800178
b.liu3a41a312024-02-28 09:57:39 +0800179/*
180*
b.liuf678f992024-05-08 15:23:10 +0800181* 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 +0800182*
183*/
184int main(int argc, char *argv[])
185{
186 int ch;
187 char out_bin[128] = {0};
b.liu61ad9172025-01-09 14:33:55 +0800188 while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:h:i:o:"))!= -1){
b.liu3a41a312024-02-28 09:57:39 +0800189 switch(ch)
190 {
191 case 'a':
192 if(strcmp(optarg, "ab") == 0) {
193 item_basic.ab_support = 1;
194 } else if(strcmp(optarg, "a") == 0) {
195 item_basic.ab_support = 0;
196 } else {
197 printf("Must be a/ab.\n");
198 return -1;
199 }
200 break;
201 case 'b':
202 if(strlen(optarg) > 0)
203 memcpy(item_basic.revision_out, optarg, strlen(optarg));
204 break;
205 case 'c':
206 if(strlen(optarg) > 0)
207 memcpy(item_basic.revision_in, optarg, strlen(optarg));
208 break;
209 case 'd':
210 if(strlen(optarg) > 0)
211 memcpy(item_basic.project, optarg, strlen(optarg));
212 break;
213 case 'e':
214 if(strlen(optarg) > 0)
215 memcpy(item_basic.project_cust, optarg, strlen(optarg));
216 break;
217 case 'f':
218 if(strcmp(optarg, "cn") == 0) {
219 item_modem.band_area = MBTK_MODEM_BAND_AREA_CN;
220 item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT;
221 item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT;
222 item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT;
223 item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800224 item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800225 item_modem.band_nr_3 = MBTK_BAND_CN_NR_3_DEFAULT;
226 item_modem.band_nr_2 = MBTK_BAND_CN_NR_2_DEFAULT;
227 item_modem.band_nr_1 = MBTK_BAND_CN_NR_1_DEFAULT;
228 item_modem.band_nr_0 = MBTK_BAND_CN_NR_0_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800229 } else if(strcmp(optarg, "eu") == 0) {
230 item_modem.band_area = MBTK_MODEM_BAND_AREA_EU;
231 item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT;
232 item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT;
233 item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT;
234 item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800235 item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800236 item_modem.band_nr_3 = MBTK_BAND_EU_NR_3_DEFAULT;
237 item_modem.band_nr_2 = MBTK_BAND_EU_NR_2_DEFAULT;
238 item_modem.band_nr_1 = MBTK_BAND_EU_NR_1_DEFAULT;
239 item_modem.band_nr_0 = MBTK_BAND_EU_NR_0_DEFAULT;
b.liuf678f992024-05-08 15:23:10 +0800240 } else if(strcmp(optarg, "sa") == 0) {
241 item_modem.band_area = MBTK_MODEM_BAND_AREA_SA;
242 item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT;
243 item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT;
244 item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT;
245 item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800246 item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800247 item_modem.band_nr_3 = MBTK_BAND_SA_NR_3_DEFAULT;
248 item_modem.band_nr_2 = MBTK_BAND_SA_NR_2_DEFAULT;
249 item_modem.band_nr_1 = MBTK_BAND_SA_NR_1_DEFAULT;
250 item_modem.band_nr_0 = MBTK_BAND_SA_NR_0_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800251 } else {
252 item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL;
253 item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
254 item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
255 item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
256 item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800257 item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800258 item_modem.band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT;
259 item_modem.band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT;
260 item_modem.band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT;
261 item_modem.band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800262 printf("Set to default band.\n");
263 }
264 break;
b.liu472cfaf2024-12-19 19:08:19 +0800265 case 'h':
266 item_modem.net_pref = (uint32)atoi(optarg);
267 printf("Set net_pref to %d success.\n", item_modem.net_pref);
268 break;
b.liu61ad9172025-01-09 14:33:55 +0800269 case 'i':
270 item_modem.net_support = (uint32)atoi(optarg);
271 printf("Set net_support to %d(%s) success.\n", item_modem.net_support,
272 net_support_str_get(item_modem.net_support));
273 break;
b.liu8e0743a2024-08-29 16:53:16 +0800274 case 'g':
275 if(strlen(optarg) > 0)
276 memcpy(item_basic.build_time, optarg, strlen(optarg));
277 break;
b.liu3a41a312024-02-28 09:57:39 +0800278 case 'o':
279 if(strlen(optarg) > 0)
280 memcpy(out_bin, optarg, strlen(optarg));
281 break;
282 default:
283 help();
284 return -1;
285 }
286 }
287 if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) {
288 help();
289 return -1;
290 }
291
292 printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin);
293
294 int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644);
295 if(fd < 0) {
296 printf("Open(%s) fail:%d\n", out_bin, errno);
297 return -1;
298 }
299
300 if(update_and_write_header(fd, &info_header)) {
301 printf("update_and_write_header() fail.");
302 goto fail;
303 }
304
b.liuec4485e2024-12-20 10:28:40 +0800305 if(write_item_basic(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, &item_basic)) {
b.liu3a41a312024-02-28 09:57:39 +0800306 printf("update_and_write_item_basic() fail.");
307 goto fail;
308 }
309
b.liuec4485e2024-12-20 10:28:40 +0800310 if(write_item_fota(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr, &item_fota)) {
b.liu3a41a312024-02-28 09:57:39 +0800311 printf("update_and_write_item_fota() fail.");
312 goto fail;
313 }
314
b.liuec4485e2024-12-20 10:28:40 +0800315 if(write_item_modem(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr, &item_modem)) {
b.liu3a41a312024-02-28 09:57:39 +0800316 printf("update_and_write_item_modem() fail.");
317 goto fail;
318 }
319
b.liuec4485e2024-12-20 10:28:40 +0800320 if(write_item_log(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr, &item_log)) {
b.liu3a41a312024-02-28 09:57:39 +0800321 printf("update_and_write_item_log() fail.");
322 goto fail;
323 }
324
325 printf("Success generate device_info bin:%s\n", out_bin);
326 close(fd);
327 return 0;
328fail:
329 close(fd);
330 return -1;
331}
332