b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 1 | #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.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 10 | #include "mbtk_type.h" |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 11 | #include "mbtk_device.h" |
| 12 | |
| 13 | static 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.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 18 | {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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 22 | } |
| 23 | }; |
| 24 | |
| 25 | static 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.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 31 | .reboot_flag = MBTK_REBOOT_FLAG_NORMAL, |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 32 | .revision_out = {0}, |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 33 | .revision_in = {0}, |
| 34 | .build_time = {0} |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static 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 | |
| 43 | static 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.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 47 | .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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 49 | .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT, |
| 50 | .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT, |
| 51 | .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT, |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 52 | .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT, |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 53 | .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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static 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 | |
| 66 | static void help() |
| 67 | { |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 68 | 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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int update_and_write_header(int fd, mbtk_device_info_header_t *header) |
| 72 | { |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 73 | 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.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 81 | static int write_item_basic(int fd, uint32 addr, mbtk_device_info_basic_t *item_basic) |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 82 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 83 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 84 | printf("lseek() fail:%d\n", errno); |
| 85 | return -1; |
| 86 | } |
| 87 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 88 | 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.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 96 | static int write_item_fota(int fd, uint32 addr, mbtk_device_info_fota_t *item_fota) |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 97 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 98 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 99 | printf("lseek() fail:%d\n", errno); |
| 100 | return -1; |
| 101 | } |
| 102 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 103 | 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.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 111 | static int write_item_modem(int fd, uint32 addr, mbtk_device_info_modem_t *item_modem) |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 112 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 113 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 114 | printf("lseek() fail:%d\n", errno); |
| 115 | return -1; |
| 116 | } |
| 117 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 118 | 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.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 126 | static int write_item_log(int fd, uint32 addr, mbtk_device_info_log_t *item_log) |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 127 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 128 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 129 | printf("lseek() fail:%d\n", errno); |
| 130 | return -1; |
| 131 | } |
| 132 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 133 | 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.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 141 | static 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.liu | 9f91db3 | 2025-02-13 11:24:18 +0800 | [diff] [blame^] | 178 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 179 | /* |
| 180 | * |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 181 | * 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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 182 | * |
| 183 | */ |
| 184 | int main(int argc, char *argv[]) |
| 185 | { |
| 186 | int ch; |
| 187 | char out_bin[128] = {0}; |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 188 | while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:h:i:o:"))!= -1){ |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 189 | 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.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 224 | item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT; |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 225 | 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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 229 | } 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.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 235 | item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT; |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 236 | 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.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 240 | } 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.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 246 | item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT; |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 247 | 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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 251 | } 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.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 257 | item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT; |
b.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 258 | 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.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 262 | printf("Set to default band.\n"); |
| 263 | } |
| 264 | break; |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 265 | 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.liu | 61ad917 | 2025-01-09 14:33:55 +0800 | [diff] [blame] | 269 | 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.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 274 | case 'g': |
| 275 | if(strlen(optarg) > 0) |
| 276 | memcpy(item_basic.build_time, optarg, strlen(optarg)); |
| 277 | break; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 278 | 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.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 305 | if(write_item_basic(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr, &item_basic)) { |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 306 | printf("update_and_write_item_basic() fail."); |
| 307 | goto fail; |
| 308 | } |
| 309 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 310 | if(write_item_fota(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr, &item_fota)) { |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 311 | printf("update_and_write_item_fota() fail."); |
| 312 | goto fail; |
| 313 | } |
| 314 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 315 | if(write_item_modem(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr, &item_modem)) { |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 316 | printf("update_and_write_item_modem() fail."); |
| 317 | goto fail; |
| 318 | } |
| 319 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 320 | if(write_item_log(fd, info_header.item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr, &item_log)) { |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 321 | 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; |
| 328 | fail: |
| 329 | close(fd); |
| 330 | return -1; |
| 331 | } |
| 332 | |