bb763420e57fff369ade95b151a2ed23a464093e
2 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
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.
15 #include <getopt.h> /* for getopt() */
16 #include <netinet/in.h>
18 #include "buffalo-lib.h"
20 #define ERR(fmt, ...) do { \
22 fprintf(stderr, "[%s] *** error: " fmt "\n", \
23 progname, ## __VA_ARGS__ ); \
26 static char *region_table
[] = {
27 "JP", "US", "EU", "AP", "TW", "KR"
30 static char *progname
;
35 static char *language
;
37 static char *platform
;
40 static char *minor
= "1.01";
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
;
48 void usage(int status
)
50 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
52 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
56 " -a <platform> set platform to <platform>\n"
57 " -b <brand> set brand to <brand>\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"
77 static int check_params(void)
80 #define CHECKSTR(_var, _name, _len) do { \
81 if ((_var) == NULL) { \
82 ERR("no %s specified", (_name)); \
86 strlen((_var)) > ((_len) - 1)) { \
87 ERR("%s is too long", (_name)); \
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
);
102 CHECKSTR(hwver
, "hardware version", 2);
104 if (num_regions
== 0) {
105 ERR("no region code specified");
114 static int process_region(char *reg
)
118 if (strlen(reg
) != 2) {
119 ERR("invalid region code '%s'", reg
);
123 if (strcmp(reg
, "M_") == 0) {
130 for (i
= 0; i
< ARRAY_SIZE(region_table
); i
++)
131 if (strcmp(reg
, region_table
[i
]) == 0) {
133 region_mask
|= 1 << i
;
138 ERR("unknown region code '%s'", reg
);
142 static void fixup_tag(unsigned char *buf
, ssize_t buflen
, ssize_t datalen
)
144 struct buffalo_tag
*tag
= (struct buffalo_tag
*) buf
;
146 memset(tag
, '\0', sizeof(*tag
));
148 memcpy(tag
->brand
, brand
, strlen(brand
));
149 memcpy(tag
->product
, product
, strlen(product
));
150 memcpy(tag
->platform
, platform
, strlen(platform
));
151 memcpy(tag
->ver_major
, major
, strlen(major
));
152 memcpy(tag
->ver_minor
, minor
, strlen(minor
));
153 memcpy(tag
->language
, language
, strlen(language
));
155 if (num_regions
> 1) {
156 tag
->region_code
[0] = 'M';
157 tag
->region_code
[1] = '_';
158 tag
->region_mask
= htonl(region_mask
);
160 memcpy(tag
->region_code
, region_code
, 2);
163 tag
->len
= htonl(buflen
);
164 tag
->data_len
= htonl(datalen
);
165 tag
->base1
= htonl(base1
);
166 tag
->base2
= htonl(base2
);
170 memcpy(tag
->hwv
, "hwv", 3);
171 memcpy(tag
->hwv_val
, hwver
, strlen(hwver
));
175 tag
->crc
= htonl(buffalo_crc(buf
, buflen
));
178 static int tag_file(void)
186 fsize
= get_file_size(ifname
);
188 ERR("unable to get size of '%s'", ifname
);
192 buflen
= fsize
+ sizeof(struct buffalo_tag
);
193 buf
= malloc(buflen
);
195 ERR("no memory for buffer\n");
199 err
= read_file_to_buf(ifname
, buf
+ sizeof(struct buffalo_tag
),
202 ERR("unable to read from file '%s'", ifname
);
206 fixup_tag(buf
, buflen
, fsize
);
208 err
= write_buf_to_file(ofname
, buf
, buflen
);
210 ERR("unable to write to file '%s'", ofname
);
222 int main(int argc
, char *argv
[])
224 int res
= EXIT_FAILURE
;
227 progname
= basename(argv
[0]);
232 c
= getopt(argc
, argv
, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:");
244 base1
= strtoul(optarg
, NULL
, 16);
247 base2
= strtoul(optarg
, NULL
, 16);
250 flag
= strtoul(optarg
, NULL
, 2);
268 err
= process_region(optarg
);
290 err
= check_params();
This page took 0.056043 seconds and 3 git commands to generate.