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 = { |
| 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 | |
| 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}, |
| 33 | .revision_in = {0} |
| 34 | }; |
| 35 | |
| 36 | static 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 | |
| 42 | static 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.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 49 | .band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT, |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame^] | 50 | .band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static 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 | |
| 59 | static 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 | |
| 64 | static 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 | |
| 79 | static 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 | |
| 89 | static 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 | |
| 99 | static 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 | |
| 109 | static 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.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 121 | * 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] | 122 | * |
| 123 | */ |
| 124 | int 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; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame^] | 164 | item_modem.band_lte_ext = MBTK_BAND_CN_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 165 | } else if(strcmp(optarg, "eu") == 0) { |
| 166 | item_modem.band_area = MBTK_MODEM_BAND_AREA_EU; |
| 167 | item_modem.band_gsm = MBTK_BAND_EU_GSM_DEFAULT; |
| 168 | item_modem.band_wcdma = MBTK_BAND_EU_WCDMA_DEFAULT; |
| 169 | item_modem.band_tdlte = MBTK_BAND_EU_TDLTE_DEFAULT; |
| 170 | item_modem.band_fddlte = MBTK_BAND_EU_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame^] | 171 | item_modem.band_lte_ext = MBTK_BAND_EU_EXT_LTE_DEFAULT; |
b.liu | f678f99 | 2024-05-08 15:23:10 +0800 | [diff] [blame] | 172 | } else if(strcmp(optarg, "sa") == 0) { |
| 173 | item_modem.band_area = MBTK_MODEM_BAND_AREA_SA; |
| 174 | item_modem.band_gsm = MBTK_BAND_SA_GSM_DEFAULT; |
| 175 | item_modem.band_wcdma = MBTK_BAND_SA_WCDMA_DEFAULT; |
| 176 | item_modem.band_tdlte = MBTK_BAND_SA_TDLTE_DEFAULT; |
| 177 | item_modem.band_fddlte = MBTK_BAND_SA_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame^] | 178 | item_modem.band_lte_ext = MBTK_BAND_SA_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 179 | } else { |
| 180 | item_modem.band_area = MBTK_MODEM_BAND_AREA_ALL; |
| 181 | item_modem.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT; |
| 182 | item_modem.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT; |
| 183 | item_modem.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT; |
| 184 | item_modem.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT; |
b.liu | 288093c | 2024-05-09 17:02:57 +0800 | [diff] [blame^] | 185 | item_modem.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT; |
b.liu | 3a41a31 | 2024-02-28 09:57:39 +0800 | [diff] [blame] | 186 | printf("Set to default band.\n"); |
| 187 | } |
| 188 | break; |
| 189 | case 'o': |
| 190 | if(strlen(optarg) > 0) |
| 191 | memcpy(out_bin, optarg, strlen(optarg)); |
| 192 | break; |
| 193 | default: |
| 194 | help(); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | if(strlen(item_basic.revision_out) == 0 || strlen(out_bin) == 0) { |
| 199 | help(); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | printf("Version:%s, Bin:%s\n", item_basic.revision_out, out_bin); |
| 204 | |
| 205 | int fd = open(out_bin, O_WRONLY | O_TRUNC | O_CREAT, 0644); |
| 206 | if(fd < 0) { |
| 207 | printf("Open(%s) fail:%d\n", out_bin, errno); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if(update_and_write_header(fd, &info_header)) { |
| 212 | printf("update_and_write_header() fail."); |
| 213 | goto fail; |
| 214 | } |
| 215 | |
| 216 | if(write_item_basic(fd, &item_basic)) { |
| 217 | printf("update_and_write_item_basic() fail."); |
| 218 | goto fail; |
| 219 | } |
| 220 | |
| 221 | if(write_item_fota(fd, &item_fota)) { |
| 222 | printf("update_and_write_item_fota() fail."); |
| 223 | goto fail; |
| 224 | } |
| 225 | |
| 226 | if(write_item_modem(fd, &item_modem)) { |
| 227 | printf("update_and_write_item_modem() fail."); |
| 228 | goto fail; |
| 229 | } |
| 230 | |
| 231 | if(write_item_log(fd, &item_log)) { |
| 232 | printf("update_and_write_item_log() fail."); |
| 233 | goto fail; |
| 234 | } |
| 235 | |
| 236 | printf("Success generate device_info bin:%s\n", out_bin); |
| 237 | close(fd); |
| 238 | return 0; |
| 239 | fail: |
| 240 | close(fd); |
| 241 | return -1; |
| 242 | } |
| 243 | |