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 | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 11 | |
| 12 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 13 | #include "mbtk_device_v2.h" |
| 14 | #else |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 15 | #include "mbtk_device.h" |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 16 | #endif |
| 17 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 18 | |
| 19 | static mbtk_device_info_header_t info_header = { |
| 20 | .tag = MBTK_DEVICE_INFO_PARTITION_TAG, |
| 21 | .version = MBTK_DEVICE_INFO_CURR_VERSION, |
| 22 | .item_count = MBTK_DEVICE_INFO_ITEM_NUM, |
| 23 | .item_header = { |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 24 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 25 | {MBTK_DEVICE_INFO_ITEM_BASIC, MBTK_DEVICE_INFO_ITEM_ADDR_BASIC}, |
| 26 | {MBTK_DEVICE_INFO_ITEM_FOTA, MBTK_DEVICE_INFO_ITEM_ADDR_FOTA}, |
| 27 | {MBTK_DEVICE_INFO_ITEM_MODEM, MBTK_DEVICE_INFO_ITEM_ADDR_MODEM}, |
| 28 | {MBTK_DEVICE_INFO_ITEM_LOG, MBTK_DEVICE_INFO_ITEM_ADDR_LOG}, |
| 29 | #else |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 30 | {MBTK_DEVICE_INFO_ITEM_BASIC, 0}, |
| 31 | {MBTK_DEVICE_INFO_ITEM_FOTA, 0}, |
| 32 | {MBTK_DEVICE_INFO_ITEM_MODEM, 0}, |
| 33 | {MBTK_DEVICE_INFO_ITEM_LOG, 0}, |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 34 | #endif |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 35 | } |
| 36 | }; |
| 37 | |
| 38 | static mbtk_device_info_basic_t item_basic = { |
| 39 | .name = MBTK_DEVICE_INFO_ITEM_STR_BASIC, |
| 40 | .version = MBTK_DEVICE_INFO_CURR_VERSION, |
| 41 | .project = {0}, |
| 42 | .project_cust = {0}, |
| 43 | .ab_support = 1, // Default for ab system. |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 44 | .reboot_flag = MBTK_REBOOT_FLAG_NORMAL, |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 45 | .revision_out = {0}, |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 46 | .revision_in = {0}, |
| 47 | .build_time = {0} |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static mbtk_device_info_fota_t item_fota = { |
| 51 | .name = MBTK_DEVICE_INFO_ITEM_STR_FOTA, |
| 52 | .version = MBTK_DEVICE_INFO_CURR_VERSION, |
| 53 | .state = 0 |
| 54 | }; |
| 55 | |
| 56 | static mbtk_device_info_modem_t item_modem = { |
| 57 | .name = MBTK_DEVICE_INFO_ITEM_STR_MODEM, |
| 58 | .version = MBTK_DEVICE_INFO_CURR_VERSION, |
| 59 | .band_area = MBTK_MODEM_BAND_AREA_ALL, // Default for all bands. |
| 60 | .band_gsm = MBTK_BAND_ALL_GSM_DEFAULT, |
| 61 | .band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT, |
| 62 | .band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT, |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 63 | .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT, |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 64 | .band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static mbtk_device_info_log_t item_log = { |
| 68 | .name = MBTK_DEVICE_INFO_ITEM_STR_LOG, |
| 69 | .version = MBTK_DEVICE_INFO_CURR_VERSION, |
| 70 | .state = 0 |
| 71 | }; |
| 72 | |
| 73 | static void help() |
| 74 | { |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 75 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 76 | 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] -o [out_bin]\n"); |
| 77 | #else |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 78 | 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.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 79 | #endif |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int update_and_write_header(int fd, mbtk_device_info_header_t *header) |
| 83 | { |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 84 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 85 | |
| 86 | #else |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 87 | header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr = sizeof(mbtk_device_info_header_t); |
| 88 | header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_BASIC].addr + sizeof(mbtk_device_info_basic_t); |
| 89 | header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_FOTA].addr + sizeof(mbtk_device_info_fota_t); |
| 90 | header->item_header[MBTK_DEVICE_INFO_ITEM_LOG].addr = header->item_header[MBTK_DEVICE_INFO_ITEM_MODEM].addr + sizeof(mbtk_device_info_modem_t); |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 91 | #endif |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 92 | |
| 93 | if(sizeof(mbtk_device_info_header_t) != write(fd, header, sizeof(mbtk_device_info_header_t))) { |
| 94 | printf("Write header fail:%d\n", errno); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int write_item_basic(int fd, mbtk_device_info_basic_t *item_basic) |
| 102 | { |
| 103 | if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) { |
| 104 | printf("Write item basic fail:%d\n", errno); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int write_item_fota(int fd, mbtk_device_info_fota_t *item_fota) |
| 112 | { |
| 113 | if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) { |
| 114 | printf("Write item fota fail:%d\n", errno); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int write_item_modem(int fd, mbtk_device_info_modem_t *item_modem) |
| 122 | { |
| 123 | if(sizeof(mbtk_device_info_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) { |
| 124 | printf("Write item modem fail:%d\n", errno); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int write_item_log(int fd, mbtk_device_info_log_t *item_log) |
| 132 | { |
| 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 | |
| 141 | /* |
| 142 | * |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 143 | * 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] | 144 | * |
| 145 | */ |
| 146 | int main(int argc, char *argv[]) |
| 147 | { |
| 148 | int ch; |
| 149 | char out_bin[128] = {0}; |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 150 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 151 | while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:h:o:"))!= -1){ |
| 152 | #else |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 153 | while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:o:"))!= -1){ |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 154 | #endif |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 155 | switch(ch) |
| 156 | { |
| 157 | case 'a': |
| 158 | if(strcmp(optarg, "ab") == 0) { |
| 159 | item_basic.ab_support = 1; |
| 160 | } else if(strcmp(optarg, "a") == 0) { |
| 161 | item_basic.ab_support = 0; |
| 162 | } else { |
| 163 | printf("Must be a/ab.\n"); |
| 164 | return -1; |
| 165 | } |
| 166 | break; |
| 167 | case 'b': |
| 168 | if(strlen(optarg) > 0) |
| 169 | memcpy(item_basic.revision_out, optarg, strlen(optarg)); |
| 170 | break; |
| 171 | case 'c': |
| 172 | if(strlen(optarg) > 0) |
| 173 | memcpy(item_basic.revision_in, optarg, strlen(optarg)); |
| 174 | break; |
| 175 | case 'd': |
| 176 | if(strlen(optarg) > 0) |
| 177 | memcpy(item_basic.project, optarg, strlen(optarg)); |
| 178 | break; |
| 179 | case 'e': |
| 180 | if(strlen(optarg) > 0) |
| 181 | memcpy(item_basic.project_cust, optarg, strlen(optarg)); |
| 182 | break; |
| 183 | case 'f': |
| 184 | if(strcmp(optarg, "cn") == 0) { |
| 185 | item_modem.band_area = MBTK_MODEM_BAND_AREA_CN; |
| 186 | item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT; |
| 187 | item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT; |
| 188 | item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT; |
| 189 | item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 190 | item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 191 | } else if(strcmp(optarg, "eu") == 0) { |
| 192 | item_modem.band_area = MBTK_MODEM_BAND_AREA_EU; |
| 193 | item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT; |
| 194 | item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT; |
| 195 | item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT; |
| 196 | item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 197 | item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT; |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 198 | } else if(strcmp(optarg, "sa") == 0) { |
| 199 | item_modem.band_area = MBTK_MODEM_BAND_AREA_SA; |
| 200 | item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT; |
| 201 | item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT; |
| 202 | item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT; |
| 203 | item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 204 | item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 205 | } else { |
| 206 | item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL; |
| 207 | item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT; |
| 208 | item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT; |
| 209 | item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT; |
| 210 | item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 211 | item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 212 | printf("Set to default band.\n"); |
| 213 | } |
| 214 | break; |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 215 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 216 | case 'h': |
| 217 | item_modem.net_pref = (uint32)atoi(optarg); |
| 218 | printf("Set net_pref to %d success.\n", item_modem.net_pref); |
| 219 | break; |
| 220 | #endif |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 221 | case 'g': |
| 222 | if(strlen(optarg) > 0) |
| 223 | memcpy(item_basic.build_time, optarg, strlen(optarg)); |
| 224 | break; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 225 | case 'o': |
| 226 | if(strlen(optarg) > 0) |
| 227 | memcpy(out_bin, optarg, strlen(optarg)); |
| 228 | break; |
| 229 | default: |
| 230 | help(); |
| 231 | return -1; |
| 232 | } |
| 233 | } |
| 234 | if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) { |
| 235 | help(); |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin); |
| 240 | |
| 241 | int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644); |
| 242 | if(fd < 0) { |
| 243 | printf("Open(%s) fail:%d\n", out_bin, errno); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | if(update_and_write_header(fd, &info_header)) { |
| 248 | printf("update_and_write_header() fail."); |
| 249 | goto fail; |
| 250 | } |
| 251 | |
| 252 | if(write_item_basic(fd, &item_basic)) { |
| 253 | printf("update_and_write_item_basic() fail."); |
| 254 | goto fail; |
| 255 | } |
| 256 | |
| 257 | if(write_item_fota(fd, &item_fota)) { |
| 258 | printf("update_and_write_item_fota() fail."); |
| 259 | goto fail; |
| 260 | } |
| 261 | |
| 262 | if(write_item_modem(fd, &item_modem)) { |
| 263 | printf("update_and_write_item_modem() fail."); |
| 264 | goto fail; |
| 265 | } |
| 266 | |
| 267 | if(write_item_log(fd, &item_log)) { |
| 268 | printf("update_and_write_item_log() fail."); |
| 269 | goto fail; |
| 270 | } |
| 271 | |
| 272 | printf("Success generate device_info bin:%s\n", out_bin); |
| 273 | close(fd); |
| 274 | return 0; |
| 275 | fail: |
| 276 | close(fd); |
| 277 | return -1; |
| 278 | } |
| 279 | |