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 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 101 | 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] | 102 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 103 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 104 | printf("lseek() fail:%d\n", errno); |
| 105 | return -1; |
| 106 | } |
| 107 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 108 | if(sizeof(mbtk_device_info_basic_t) != write(fd, item_basic, sizeof(mbtk_device_info_basic_t))) { |
| 109 | printf("Write item basic fail:%d\n", errno); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 116 | 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] | 117 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 118 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 119 | printf("lseek() fail:%d\n", errno); |
| 120 | return -1; |
| 121 | } |
| 122 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 123 | if(sizeof(mbtk_device_info_fota_t) != write(fd, item_fota, sizeof(mbtk_device_info_fota_t))) { |
| 124 | printf("Write item fota fail:%d\n", errno); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 131 | 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] | 132 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 133 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 134 | printf("lseek() fail:%d\n", errno); |
| 135 | return -1; |
| 136 | } |
| 137 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 138 | if(sizeof(mbtk_device_info_modem_t) != write(fd, item_modem, sizeof(mbtk_device_info_modem_t))) { |
| 139 | printf("Write item modem fail:%d\n", errno); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 146 | 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] | 147 | { |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 148 | if(-1 == lseek(fd, addr, SEEK_SET)) { |
| 149 | printf("lseek() fail:%d\n", errno); |
| 150 | return -1; |
| 151 | } |
| 152 | |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 153 | if(sizeof(mbtk_device_info_log_t) != write(fd, item_log, sizeof(mbtk_device_info_log_t))) { |
| 154 | printf("Write item log fail:%d\n", errno); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 163 | * 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] | 164 | * |
| 165 | */ |
| 166 | int main(int argc, char *argv[]) |
| 167 | { |
| 168 | int ch; |
| 169 | char out_bin[128] = {0}; |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 170 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 171 | while((ch = getopt(argc, argv, "a:b:c:d:e:f:g:h:o:"))!= -1){ |
| 172 | #else |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 173 | 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] | 174 | #endif |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 175 | switch(ch) |
| 176 | { |
| 177 | case 'a': |
| 178 | if(strcmp(optarg, "ab") == 0) { |
| 179 | item_basic.ab_support = 1; |
| 180 | } else if(strcmp(optarg, "a") == 0) { |
| 181 | item_basic.ab_support = 0; |
| 182 | } else { |
| 183 | printf("Must be a/ab.\n"); |
| 184 | return -1; |
| 185 | } |
| 186 | break; |
| 187 | case 'b': |
| 188 | if(strlen(optarg) > 0) |
| 189 | memcpy(item_basic.revision_out, optarg, strlen(optarg)); |
| 190 | break; |
| 191 | case 'c': |
| 192 | if(strlen(optarg) > 0) |
| 193 | memcpy(item_basic.revision_in, optarg, strlen(optarg)); |
| 194 | break; |
| 195 | case 'd': |
| 196 | if(strlen(optarg) > 0) |
| 197 | memcpy(item_basic.project, optarg, strlen(optarg)); |
| 198 | break; |
| 199 | case 'e': |
| 200 | if(strlen(optarg) > 0) |
| 201 | memcpy(item_basic.project_cust, optarg, strlen(optarg)); |
| 202 | break; |
| 203 | case 'f': |
| 204 | if(strcmp(optarg, "cn") == 0) { |
| 205 | item_modem.band_area = MBTK_MODEM_BAND_AREA_CN; |
| 206 | item_modem.band_gsm = MBTK_BAND_CN_GSM_DEFAULT; |
| 207 | item_modem.band_wcdma = MBTK_BAND_CN_WCDMA_DEFAULT; |
| 208 | item_modem.band_tdlte = MBTK_BAND_CN_TDLTE_DEFAULT; |
| 209 | item_modem.band_fddlte = MBTK_BAND_CN_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 210 | item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 211 | } else if(strcmp(optarg, "eu") == 0) { |
| 212 | item_modem.band_area = MBTK_MODEM_BAND_AREA_EU; |
| 213 | item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT; |
| 214 | item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT; |
| 215 | item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT; |
| 216 | item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 217 | item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT; |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 218 | } else if(strcmp(optarg, "sa") == 0) { |
| 219 | item_modem.band_area = MBTK_MODEM_BAND_AREA_SA; |
| 220 | item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT; |
| 221 | item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT; |
| 222 | item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT; |
| 223 | item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 224 | item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 225 | } else { |
| 226 | item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL; |
| 227 | item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT; |
| 228 | item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT; |
| 229 | item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT; |
| 230 | item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame] | 231 | item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 232 | printf("Set to default band.\n"); |
| 233 | } |
| 234 | break; |
b.liu | 472cfaf | 2024-12-19 19:08:19 +0800 | [diff] [blame] | 235 | #ifdef MBTK_DEV_INFO_VERSION_2 |
| 236 | case 'h': |
| 237 | item_modem.net_pref = (uint32)atoi(optarg); |
| 238 | printf("Set net_pref to %d success.\n", item_modem.net_pref); |
| 239 | break; |
| 240 | #endif |
b.liu | 8e0743a | 2024-08-29 16:53:16 +0800 | [diff] [blame] | 241 | case 'g': |
| 242 | if(strlen(optarg) > 0) |
| 243 | memcpy(item_basic.build_time, optarg, strlen(optarg)); |
| 244 | break; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 245 | case 'o': |
| 246 | if(strlen(optarg) > 0) |
| 247 | memcpy(out_bin, optarg, strlen(optarg)); |
| 248 | break; |
| 249 | default: |
| 250 | help(); |
| 251 | return -1; |
| 252 | } |
| 253 | } |
| 254 | if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) { |
| 255 | help(); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin); |
| 260 | |
| 261 | int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644); |
| 262 | if(fd < 0) { |
| 263 | printf("Open(%s) fail:%d\n", out_bin, errno); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | if(update_and_write_header(fd, &info_header)) { |
| 268 | printf("update_and_write_header() fail."); |
| 269 | goto fail; |
| 270 | } |
| 271 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 272 | 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] | 273 | printf("update_and_write_item_basic() fail."); |
| 274 | goto fail; |
| 275 | } |
| 276 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 277 | 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] | 278 | printf("update_and_write_item_fota() fail."); |
| 279 | goto fail; |
| 280 | } |
| 281 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 282 | 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] | 283 | printf("update_and_write_item_modem() fail."); |
| 284 | goto fail; |
| 285 | } |
| 286 | |
b.liu | ec4485e | 2024-12-20 10:28:40 +0800 | [diff] [blame] | 287 | 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] | 288 | printf("update_and_write_item_log() fail."); |
| 289 | goto fail; |
| 290 | } |
| 291 | |
| 292 | printf("Success generate device_info bin:%s\n", out_bin); |
| 293 | close(fd); |
| 294 | return 0; |
| 295 | fail: |
| 296 | close(fd); |
| 297 | return -1; |
| 298 | } |
| 299 | |