blob: f8764ed21a57455c30393a48bd3a9a0afa305d37 [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},
b.liu166be482025-02-27 15:23:29 +080034 .build_time = {0},
35 .asr_baseline = {0}
b.liu3a41a312024-02-28 09:57:39 +080036};
37
38static mbtk_device_info_fota_t item_fota = {
39 .name = MBTK_DEVICE_INFO_ITEM_STR_FOTA,
40 .version = MBTK_DEVICE_INFO_CURR_VERSION,
41 .state = 0
42};
43
44static mbtk_device_info_modem_t item_modem = {
45 .name = MBTK_DEVICE_INFO_ITEM_STR_MODEM,
46 .version = MBTK_DEVICE_INFO_CURR_VERSION,
47 .band_area = MBTK_MODEM_BAND_AREA_ALL, // Default for all bands.
b.liu61ad9172025-01-09 14:33:55 +080048 .net_pref = 15, // Default *band is 15
49 .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 +080050 .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT,
51 .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT,
52 .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT,
b.liuf678f992024-05-08 15:23:10 +080053 .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT,
b.liu61ad9172025-01-09 14:33:55 +080054 .band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT,
55 .band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT,
56 .band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT,
57 .band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT,
58 .band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT
b.liu3a41a312024-02-28 09:57:39 +080059};
60
61static mbtk_device_info_log_t item_log = {
62 .name = MBTK_DEVICE_INFO_ITEM_STR_LOG,
63 .version = MBTK_DEVICE_INFO_CURR_VERSION,
64 .state = 0
65};
66
67static void help()
68{
b.liu166be482025-02-27 15:23:29 +080069 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] -j [asr_baseline] -o [out_bin]\n");
b.liu3a41a312024-02-28 09:57:39 +080070}
71
72static int update_and_write_header(int fd, mbtk_device_info_header_t *header)
73{
b.liu3a41a312024-02-28 09:57:39 +080074 if(sizeof(mbtk_device_info_header_t) != write(fd, header, sizeof(mbtk_device_info_header_t))) {
75 printf("Write header fail:%d\n", errno);
76 return -1;
77 }
78
79 return 0;
80}
81
b.liuec4485e2024-12-20 10:28:40 +080082static int write_item_basic(int fd, uint32 addr, mbtk_device_info_basic_t *item_basic)
b.liu3a41a312024-02-28 09:57:39 +080083{
b.liuec4485e2024-12-20 10:28:40 +080084 if(-1 == lseek(fd, addr, SEEK_SET)) {
85 printf("lseek() fail:%d\n", errno);
86 return -1;
87 }
88
b.liu3a41a312024-02-28 09:57:39 +080089 if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) {
90 printf("Write item basic fail:%d\n", errno);
91 return -1;
92 }
93
94 return 0;
95}
96
b.liuec4485e2024-12-20 10:28:40 +080097static int write_item_fota(int fd, uint32 addr, mbtk_device_info_fota_t *item_fota)
b.liu3a41a312024-02-28 09:57:39 +080098{
b.liuec4485e2024-12-20 10:28:40 +080099 if(-1 == lseek(fd, addr, SEEK_SET)) {
100 printf("lseek() fail:%d\n", errno);
101 return -1;
102 }
103
b.liu3a41a312024-02-28 09:57:39 +0800104 if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) {
105 printf("Write item fota fail:%d\n", errno);
106 return -1;
107 }
108
109 return 0;
110}
111
b.liuec4485e2024-12-20 10:28:40 +0800112static int write_item_modem(int fd, uint32 addr, mbtk_device_info_modem_t *item_modem)
b.liu3a41a312024-02-28 09:57:39 +0800113{
b.liuec4485e2024-12-20 10:28:40 +0800114 if(-1 == lseek(fd, addr, SEEK_SET)) {
115 printf("lseek() fail:%d\n", errno);
116 return -1;
117 }
118
b.liu3a41a312024-02-28 09:57:39 +0800119 if(sizeof(mbtk_device_info_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) {
120 printf("Write item modem fail:%d\n", errno);
121 return -1;
122 }
123
124 return 0;
125}
126
b.liuec4485e2024-12-20 10:28:40 +0800127static int write_item_log(int fd, uint32 addr, mbtk_device_info_log_t *item_log)
b.liu3a41a312024-02-28 09:57:39 +0800128{
b.liuec4485e2024-12-20 10:28:40 +0800129 if(-1 == lseek(fd, addr, SEEK_SET)) {
130 printf("lseek() fail:%d\n", errno);
131 return -1;
132 }
133
b.liu3a41a312024-02-28 09:57:39 +0800134 if(sizeof(mbtk_device_info_log_t) != write(fd, item_log, sizeof(mbtk_device_info_log_t))) {
135 printf("Write item log fail:%d\n", errno);
136 return -1;
137 }
138
139 return 0;
140}
141
b.liu61ad9172025-01-09 14:33:55 +0800142static char* net_support_str_get(uint32 net_support)
143{
144 static char net_str[100] = {0};
145
146 if(net_support & 0x01) { // GSM
147 if(strlen(net_str) > 0) {
148 strcat(net_str, "/2G");
149 } else {
150 strcat(net_str, "2G");
151 }
152 }
153
154 if(net_support & 0x02) { // WCDMA
155 if(strlen(net_str) > 0) {
156 strcat(net_str, "/3G");
157 } else {
158 strcat(net_str, "3G");
159 }
160 }
161
162 if(net_support & 0x04) { // LTE
163 if(strlen(net_str) > 0) {
164 strcat(net_str, "/4G");
165 } else {
166 strcat(net_str, "4G");
167 }
168 }
169
170 if(net_support & 0x08) { // NR
171 if(strlen(net_str) > 0) {
172 strcat(net_str, "/5G");
173 } else {
174 strcat(net_str, "5G");
175 }
176 }
177 return net_str;
178}
b.liu9f91db32025-02-13 11:24:18 +0800179
b.liu3a41a312024-02-28 09:57:39 +0800180/*
181*
b.liuf678f992024-05-08 15:23:10 +0800182* 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 +0800183*
184*/
185int main(int argc, char *argv[])
186{
187 int ch;
188 char out_bin[128] = {0};
b.liu166be482025-02-27 15:23:29 +0800189 while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:h:i:j:o:"))!= -1){
b.liu3a41a312024-02-28 09:57:39 +0800190 switch(ch)
191 {
192 case 'a':
193 if(strcmp(optarg, "ab") == 0) {
194 item_basic.ab_support = 1;
195 } else if(strcmp(optarg, "a") == 0) {
196 item_basic.ab_support = 0;
197 } else {
198 printf("Must be a/ab.\n");
199 return -1;
200 }
201 break;
202 case 'b':
203 if(strlen(optarg) > 0)
204 memcpy(item_basic.revision_out, optarg, strlen(optarg));
205 break;
206 case 'c':
207 if(strlen(optarg) > 0)
208 memcpy(item_basic.revision_in, optarg, strlen(optarg));
209 break;
210 case 'd':
211 if(strlen(optarg) > 0)
212 memcpy(item_basic.project, optarg, strlen(optarg));
213 break;
214 case 'e':
215 if(strlen(optarg) > 0)
216 memcpy(item_basic.project_cust, optarg, strlen(optarg));
217 break;
218 case 'f':
219 if(strcmp(optarg, "cn") == 0) {
220 item_modem.band_area = MBTK_MODEM_BAND_AREA_CN;
221 item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT;
222 item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT;
223 item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT;
224 item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800225 item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800226 item_modem.band_nr_3 = MBTK_BAND_CN_NR_3_DEFAULT;
227 item_modem.band_nr_2 = MBTK_BAND_CN_NR_2_DEFAULT;
228 item_modem.band_nr_1 = MBTK_BAND_CN_NR_1_DEFAULT;
229 item_modem.band_nr_0 = MBTK_BAND_CN_NR_0_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800230 } else if(strcmp(optarg, "eu") == 0) {
231 item_modem.band_area = MBTK_MODEM_BAND_AREA_EU;
232 item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT;
233 item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT;
234 item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT;
235 item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800236 item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800237 item_modem.band_nr_3 = MBTK_BAND_EU_NR_3_DEFAULT;
238 item_modem.band_nr_2 = MBTK_BAND_EU_NR_2_DEFAULT;
239 item_modem.band_nr_1 = MBTK_BAND_EU_NR_1_DEFAULT;
240 item_modem.band_nr_0 = MBTK_BAND_EU_NR_0_DEFAULT;
b.liuf678f992024-05-08 15:23:10 +0800241 } else if(strcmp(optarg, "sa") == 0) {
242 item_modem.band_area = MBTK_MODEM_BAND_AREA_SA;
243 item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT;
244 item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT;
245 item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT;
246 item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800247 item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800248 item_modem.band_nr_3 = MBTK_BAND_SA_NR_3_DEFAULT;
249 item_modem.band_nr_2 = MBTK_BAND_SA_NR_2_DEFAULT;
250 item_modem.band_nr_1 = MBTK_BAND_SA_NR_1_DEFAULT;
251 item_modem.band_nr_0 = MBTK_BAND_SA_NR_0_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800252 } else {
253 item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL;
254 item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
255 item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
256 item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
257 item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800258 item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +0800259 item_modem.band_nr_3 = MBTK_BAND_ALL_NR_3_DEFAULT;
260 item_modem.band_nr_2 = MBTK_BAND_ALL_NR_2_DEFAULT;
261 item_modem.band_nr_1 = MBTK_BAND_ALL_NR_1_DEFAULT;
262 item_modem.band_nr_0 = MBTK_BAND_ALL_NR_0_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800263 printf("Set to default band.\n");
264 }
265 break;
b.liu472cfaf2024-12-19 19:08:19 +0800266 case 'h':
267 item_modem.net_pref = (uint32)atoi(optarg);
268 printf("Set net_pref to %d success.\n", item_modem.net_pref);
269 break;
b.liu61ad9172025-01-09 14:33:55 +0800270 case 'i':
271 item_modem.net_support = (uint32)atoi(optarg);
272 printf("Set net_support to %d(%s) success.\n", item_modem.net_support,
273 net_support_str_get(item_modem.net_support));
274 break;
b.liu8e0743a2024-08-29 16:53:16 +0800275 case 'g':
276 if(strlen(optarg) > 0)
277 memcpy(item_basic.build_time, optarg, strlen(optarg));
278 break;
b.liu166be482025-02-27 15:23:29 +0800279 case 'j':
280 if(strlen(optarg) > 0)
281 memcpy(item_basic.asr_baseline, optarg, strlen(optarg));
282 break;
b.liu3a41a312024-02-28 09:57:39 +0800283 case 'o':
284 if(strlen(optarg) > 0)
285 memcpy(out_bin, optarg, strlen(optarg));
286 break;
287 default:
288 help();
289 return -1;
290 }
291 }
292 if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) {
293 help();
294 return -1;
295 }
296
297 printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin);
298
299 int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644);
300 if(fd < 0) {
301 printf("Open(%s) fail:%d\n", out_bin, errno);
302 return -1;
303 }
304
305 if(update_and_write_header(fd, &info_header)) {
306 printf("update_and_write_header() fail.");
307 goto fail;
308 }
309
b.liuec4485e2024-12-20 10:28:40 +0800310 if(write_item_basic(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, &item_basic)) {
b.liu3a41a312024-02-28 09:57:39 +0800311 printf("update_and_write_item_basic() fail.");
312 goto fail;
313 }
314
b.liuec4485e2024-12-20 10:28:40 +0800315 if(write_item_fota(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr, &item_fota)) {
b.liu3a41a312024-02-28 09:57:39 +0800316 printf("update_and_write_item_fota() fail.");
317 goto fail;
318 }
319
b.liuec4485e2024-12-20 10:28:40 +0800320 if(write_item_modem(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr, &item_modem)) {
b.liu3a41a312024-02-28 09:57:39 +0800321 printf("update_and_write_item_modem() fail.");
322 goto fail;
323 }
324
b.liuec4485e2024-12-20 10:28:40 +0800325 if(write_item_log(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr, &item_log)) {
b.liu3a41a312024-02-28 09:57:39 +0800326 printf("update_and_write_item_log() fail.");
327 goto fail;
328 }
329
330 printf("Success generate device_info bin:%s\n", out_bin);
331 close(fd);
332 return 0;
333fail:
334 close(fd);
335 return -1;
336}
337