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