| 1 | /* |
| 2 | * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 as published |
| 6 | * by the Free Software Foundation. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdint.h> |
| 13 | #include <string.h> |
| 14 | #include <libgen.h> |
| 15 | #include <getopt.h> /* for getopt() */ |
| 16 | #include <netinet/in.h> |
| 17 | |
| 18 | #include "buffalo-lib.h" |
| 19 | |
| 20 | #define ERR(fmt, ...) do { \ |
| 21 | fflush(0); \ |
| 22 | fprintf(stderr, "[%s] *** error: " fmt "\n", \ |
| 23 | progname, ## __VA_ARGS__ ); \ |
| 24 | } while (0) |
| 25 | |
| 26 | static char *region_table[] = { |
| 27 | "JP", "US", "EU", "AP", "TW", "KR" |
| 28 | }; |
| 29 | |
| 30 | static char *progname; |
| 31 | static char *ifname; |
| 32 | static char *ofname; |
| 33 | static char *product; |
| 34 | static char *brand; |
| 35 | static char *language; |
| 36 | static char *hwver; |
| 37 | static char *platform; |
| 38 | static int flag; |
| 39 | static char *major; |
| 40 | static char *minor = "1.01"; |
| 41 | static int skipcrc; |
| 42 | static uint32_t base1; |
| 43 | static uint32_t base2; |
| 44 | static char *region_code; |
| 45 | static uint32_t region_mask; |
| 46 | static int num_regions; |
| 47 | |
| 48 | void usage(int status) |
| 49 | { |
| 50 | FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout; |
| 51 | |
| 52 | fprintf(stream, "Usage: %s [OPTIONS...]\n", progname); |
| 53 | fprintf(stream, |
| 54 | "\n" |
| 55 | "Options:\n" |
| 56 | " -a <platform> set platform to <platform>\n" |
| 57 | " -b <brand> set brand to <brand>\n" |
| 58 | " -c <base1>\n" |
| 59 | " -d <base2>\n" |
| 60 | " -f <flag> set flag to <flag>\n" |
| 61 | " -i <file> read input from the file <file>\n" |
| 62 | " -l <language> set language to <language>\n" |
| 63 | " -m <version> set minor version to <version>\n" |
| 64 | " -o <file> write output to the file <file>\n" |
| 65 | " -p <product> set product to <product>\n" |
| 66 | " -r <region> set image region to <region>\n" |
| 67 | " valid regions: JP, US, EU, AP, TW, KR, M_\n" |
| 68 | " -s skip CRC calculation\n" |
| 69 | " -v <version> set major version to <version>\n" |
| 70 | " -w <version> set harwdware version to <version>\n" |
| 71 | " -h show this screen\n" |
| 72 | ); |
| 73 | |
| 74 | exit(status); |
| 75 | } |
| 76 | |
| 77 | static int check_params(void) |
| 78 | { |
| 79 | |
| 80 | #define CHECKSTR(_var, _name, _len) do { \ |
| 81 | if ((_var) == NULL) { \ |
| 82 | ERR("no %s specified", (_name)); \ |
| 83 | return -1; \ |
| 84 | } \ |
| 85 | if ((_len) > 0 && \ |
| 86 | strlen((_var)) > ((_len) - 1)) { \ |
| 87 | ERR("%s is too long", (_name)); \ |
| 88 | return -1; \ |
| 89 | } \ |
| 90 | } while (0) |
| 91 | |
| 92 | CHECKSTR(ifname, "input file", 0); |
| 93 | CHECKSTR(ofname, "output file", 0); |
| 94 | CHECKSTR(brand, "brand", TAG_BRAND_LEN); |
| 95 | CHECKSTR(product, "product", TAG_PRODUCT_LEN); |
| 96 | CHECKSTR(platform, "platform", TAG_PLATFORM_LEN); |
| 97 | CHECKSTR(major, "major version", TAG_VERSION_LEN); |
| 98 | CHECKSTR(minor, "minor version", TAG_VERSION_LEN); |
| 99 | CHECKSTR(language, "language", TAG_LANGUAGE_LEN); |
| 100 | CHECKSTR(hwver, "hardware version", 2); |
| 101 | |
| 102 | if (num_regions == 0) { |
| 103 | ERR("no region code specified"); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | |
| 109 | #undef CHECKSTR |
| 110 | } |
| 111 | |
| 112 | static int process_region(char *reg) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | if (strlen(reg) != 2) { |
| 117 | ERR("invalid region code '%s'", reg); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | if (strcmp(reg, "M_") == 0) { |
| 122 | region_code = reg; |
| 123 | region_mask |= ~0; |
| 124 | num_regions = 32; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | for (i = 0; i < ARRAY_SIZE(region_table); i++) |
| 129 | if (strcmp(reg, region_table[i]) == 0) { |
| 130 | region_code = reg; |
| 131 | region_mask |= 1 << i; |
| 132 | num_regions++; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | ERR("unknown region code '%s'", reg); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | static void fixup_tag(unsigned char *buf, ssize_t buflen, ssize_t datalen) |
| 141 | { |
| 142 | struct buffalo_tag *tag = (struct buffalo_tag *) buf; |
| 143 | |
| 144 | memset(tag, '\0', sizeof(*tag)); |
| 145 | |
| 146 | memcpy(tag->brand, brand, strlen(brand)); |
| 147 | memcpy(tag->product, product, strlen(product)); |
| 148 | memcpy(tag->platform, platform, strlen(platform)); |
| 149 | memcpy(tag->ver_major, major, strlen(major)); |
| 150 | memcpy(tag->ver_minor, minor, strlen(minor)); |
| 151 | memcpy(tag->language, language, strlen(language)); |
| 152 | |
| 153 | if (num_regions > 1) { |
| 154 | tag->region_code[0] = 'M'; |
| 155 | tag->region_code[1] = '_'; |
| 156 | tag->region_mask = htonl(region_mask); |
| 157 | } else { |
| 158 | memcpy(tag->region_code, region_code, 2); |
| 159 | } |
| 160 | |
| 161 | tag->len = htonl(buflen); |
| 162 | tag->data_len = htonl(datalen); |
| 163 | tag->base1 = htonl(base1); |
| 164 | tag->base2 = htonl(base2); |
| 165 | tag->flag = flag; |
| 166 | |
| 167 | if (hwver) { |
| 168 | memcpy(tag->hwv, "hwv", 3); |
| 169 | memcpy(tag->hwv_val, hwver, strlen(hwver)); |
| 170 | } |
| 171 | |
| 172 | if (!skipcrc) |
| 173 | tag->crc = htonl(buffalo_crc(buf, buflen)); |
| 174 | } |
| 175 | |
| 176 | static int tag_file(void) |
| 177 | { |
| 178 | unsigned char *buf; |
| 179 | ssize_t fsize; |
| 180 | ssize_t buflen; |
| 181 | int err; |
| 182 | int ret = -1; |
| 183 | |
| 184 | fsize = get_file_size(ifname); |
| 185 | if (fsize < 0) { |
| 186 | ERR("unable to get size of '%s'", ifname); |
| 187 | goto out; |
| 188 | } |
| 189 | |
| 190 | buflen = fsize + sizeof(struct buffalo_tag); |
| 191 | buf = malloc(buflen); |
| 192 | if (!buf) { |
| 193 | ERR("no memory for buffer\n"); |
| 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | err = read_file_to_buf(ifname, buf + sizeof(struct buffalo_tag), |
| 198 | fsize); |
| 199 | if (err) { |
| 200 | ERR("unable to read from file '%s'", ifname); |
| 201 | goto free_buf; |
| 202 | } |
| 203 | |
| 204 | fixup_tag(buf, buflen, fsize); |
| 205 | |
| 206 | err = write_buf_to_file(ofname, buf, buflen); |
| 207 | if (err) { |
| 208 | ERR("unable to write to file '%s'", ofname); |
| 209 | goto free_buf; |
| 210 | } |
| 211 | |
| 212 | ret = 0; |
| 213 | |
| 214 | free_buf: |
| 215 | free(buf); |
| 216 | out: |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | int main(int argc, char *argv[]) |
| 221 | { |
| 222 | int res = EXIT_FAILURE; |
| 223 | int err; |
| 224 | |
| 225 | progname = basename(argv[0]); |
| 226 | |
| 227 | while ( 1 ) { |
| 228 | int c; |
| 229 | |
| 230 | c = getopt(argc, argv, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:"); |
| 231 | if (c == -1) |
| 232 | break; |
| 233 | |
| 234 | switch (c) { |
| 235 | case 'a': |
| 236 | platform = optarg; |
| 237 | break; |
| 238 | case 'b': |
| 239 | brand = optarg; |
| 240 | break; |
| 241 | case 'c': |
| 242 | base1 = strtoul(optarg, NULL, 16); |
| 243 | break; |
| 244 | case 'd': |
| 245 | base2 = strtoul(optarg, NULL, 16); |
| 246 | break; |
| 247 | case 'f': |
| 248 | flag = strtoul(optarg, NULL, 2); |
| 249 | break; |
| 250 | case 'i': |
| 251 | ifname = optarg; |
| 252 | break; |
| 253 | case 'l': |
| 254 | language = optarg; |
| 255 | break; |
| 256 | case 'm': |
| 257 | minor = optarg; |
| 258 | break; |
| 259 | case 'o': |
| 260 | ofname = optarg; |
| 261 | break; |
| 262 | case 'p': |
| 263 | product = optarg; |
| 264 | break; |
| 265 | case 'r': |
| 266 | err = process_region(optarg); |
| 267 | if (err) |
| 268 | goto out; |
| 269 | break; |
| 270 | case 's': |
| 271 | skipcrc = 1; |
| 272 | break; |
| 273 | case 'v': |
| 274 | major = optarg; |
| 275 | break; |
| 276 | case 'w': |
| 277 | hwver = optarg; |
| 278 | break; |
| 279 | case 'h': |
| 280 | usage(EXIT_SUCCESS); |
| 281 | break; |
| 282 | default: |
| 283 | usage(EXIT_FAILURE); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | err = check_params(); |
| 289 | if (err) |
| 290 | goto out; |
| 291 | |
| 292 | err = tag_file(); |
| 293 | if (err) |
| 294 | goto out; |
| 295 | |
| 296 | res = EXIT_SUCCESS; |
| 297 | |
| 298 | out: |
| 299 | return res; |
| 300 | } |
| 301 | |