blob: fda756f08d9169adf9bfba2bfce22b2f7d91126d [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 = {
18 {MBTK_DEVICE_INFO_ITEM_BASIC, 0},
19 {MBTK_DEVICE_INFO_ITEM_FOTA, 0},
20 {MBTK_DEVICE_INFO_ITEM_MODEM, 0},
21 {MBTK_DEVICE_INFO_ITEM_LOG, 0},
22 }
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.
47 .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT,
48 .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT,
49 .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT,
b.liuf678f992024-05-08 15:23:10 +080050 .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT,
b.liu288093c2024-05-09 17:02:57 +080051 .band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT
b.liu3a41a312024-02-28 09:57:39 +080052};
53
54static mbtk_device_info_log_t item_log = {
55 .name = MBTK_DEVICE_INFO_ITEM_STR_LOG,
56 .version = MBTK_DEVICE_INFO_CURR_VERSION,
57 .state = 0
58};
59
60static void help()
61{
b.liu8e0743a2024-08-29 16:53:16 +080062 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.liu3a41a312024-02-28 09:57:39 +080063}
64
65static int update_and_write_header(int fd, mbtk_device_info_header_t *header)
66{
67 header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr = sizeof(mbtk_device_info_header_t);
68 header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr + sizeof(mbtk_device_info_basic_t);
69 header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr + sizeof(mbtk_device_info_fota_t);
70 header->item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr + sizeof(mbtk_device_info_modem_t);
71
72 if(sizeof(mbtk_device_info_header_t) != write(fd, header, sizeof(mbtk_device_info_header_t))) {
73 printf("Write header fail:%d\n", errno);
74 return -1;
75 }
76
77 return 0;
78}
79
80static int write_item_basic(int fd, mbtk_device_info_basic_t *item_basic)
81{
82 if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) {
83 printf("Write item basic fail:%d\n", errno);
84 return -1;
85 }
86
87 return 0;
88}
89
90static int write_item_fota(int fd, mbtk_device_info_fota_t *item_fota)
91{
92 if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) {
93 printf("Write item fota fail:%d\n", errno);
94 return -1;
95 }
96
97 return 0;
98}
99
100static int write_item_modem(int fd, mbtk_device_info_modem_t *item_modem)
101{
102 if(sizeof(mbtk_device_info_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) {
103 printf("Write item modem fail:%d\n", errno);
104 return -1;
105 }
106
107 return 0;
108}
109
110static int write_item_log(int fd, mbtk_device_info_log_t *item_log)
111{
112 if(sizeof(mbtk_device_info_log_t) != write(fd, item_log, sizeof(mbtk_device_info_log_t))) {
113 printf("Write item log fail:%d\n", errno);
114 return -1;
115 }
116
117 return 0;
118}
119
120/*
121*
b.liuf678f992024-05-08 15:23:10 +0800122* 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 +0800123*
124*/
125int main(int argc, char *argv[])
126{
127 int ch;
128 char out_bin[128] = {0};
b.liu8e0743a2024-08-29 16:53:16 +0800129 while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:o:"))!= -1){
b.liu3a41a312024-02-28 09:57:39 +0800130 switch(ch)
131 {
132 case 'a':
133 if(strcmp(optarg, "ab") == 0) {
134 item_basic.ab_support = 1;
135 } else if(strcmp(optarg, "a") == 0) {
136 item_basic.ab_support = 0;
137 } else {
138 printf("Must be a/ab.\n");
139 return -1;
140 }
141 break;
142 case 'b':
143 if(strlen(optarg) > 0)
144 memcpy(item_basic.revision_out, optarg, strlen(optarg));
145 break;
146 case 'c':
147 if(strlen(optarg) > 0)
148 memcpy(item_basic.revision_in, optarg, strlen(optarg));
149 break;
150 case 'd':
151 if(strlen(optarg) > 0)
152 memcpy(item_basic.project, optarg, strlen(optarg));
153 break;
154 case 'e':
155 if(strlen(optarg) > 0)
156 memcpy(item_basic.project_cust, optarg, strlen(optarg));
157 break;
158 case 'f':
159 if(strcmp(optarg, "cn") == 0) {
160 item_modem.band_area = MBTK_MODEM_BAND_AREA_CN;
161 item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT;
162 item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT;
163 item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT;
164 item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800165 item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800166 } else if(strcmp(optarg, "eu") == 0) {
167 item_modem.band_area = MBTK_MODEM_BAND_AREA_EU;
168 item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT;
169 item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT;
170 item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT;
171 item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800172 item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT;
b.liuf678f992024-05-08 15:23:10 +0800173 } else if(strcmp(optarg, "sa") == 0) {
174 item_modem.band_area = MBTK_MODEM_BAND_AREA_SA;
175 item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT;
176 item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT;
177 item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT;
178 item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800179 item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800180 } else {
181 item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL;
182 item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
183 item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
184 item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
185 item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +0800186 item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liu3a41a312024-02-28 09:57:39 +0800187 printf("Set to default band.\n");
188 }
189 break;
b.liu8e0743a2024-08-29 16:53:16 +0800190 case 'g':
191 if(strlen(optarg) > 0)
192 memcpy(item_basic.build_time, optarg, strlen(optarg));
193 break;
b.liu3a41a312024-02-28 09:57:39 +0800194 case 'o':
195 if(strlen(optarg) > 0)
196 memcpy(out_bin, optarg, strlen(optarg));
197 break;
198 default:
199 help();
200 return -1;
201 }
202 }
203 if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) {
204 help();
205 return -1;
206 }
207
208 printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin);
209
210 int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644);
211 if(fd < 0) {
212 printf("Open(%s) fail:%d\n", out_bin, errno);
213 return -1;
214 }
215
216 if(update_and_write_header(fd, &info_header)) {
217 printf("update_and_write_header() fail.");
218 goto fail;
219 }
220
221 if(write_item_basic(fd, &item_basic)) {
222 printf("update_and_write_item_basic() fail.");
223 goto fail;
224 }
225
226 if(write_item_fota(fd, &item_fota)) {
227 printf("update_and_write_item_fota() fail.");
228 goto fail;
229 }
230
231 if(write_item_modem(fd, &item_modem)) {
232 printf("update_and_write_item_modem() fail.");
233 goto fail;
234 }
235
236 if(write_item_log(fd, &item_log)) {
237 printf("update_and_write_item_log() fail.");
238 goto fail;
239 }
240
241 printf("Success generate device_info bin:%s\n", out_bin);
242 close(fd);
243 return 0;
244fail:
245 close(fd);
246 return -1;
247}
248