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
);
100 CHECKSTR(hwver
, "hardware version", 2);
102 if (num_regions
== 0) {
103 ERR("no region code specified");
112 static int process_region(char *reg
)
116 if (strlen(reg
) != 2) {
117 ERR("invalid region code '%s'", reg
);
121 if (strcmp(reg
, "M_") == 0) {
128 for (i
= 0; i
< ARRAY_SIZE(region_table
); i
++)
129 if (strcmp(reg
, region_table
[i
]) == 0) {
131 region_mask
|= 1 << i
;
136 ERR("unknown region code '%s'", reg
);
140 static void fixup_tag(unsigned char *buf
, ssize_t buflen
, ssize_t datalen
)
142 struct buffalo_tag
*tag
= (struct buffalo_tag
*) buf
;
144 memset(tag
, '\0', sizeof(*tag
));
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
));
153 if (num_regions
> 1) {
154 tag
->region_code
[0] = 'M';
155 tag
->region_code
[1] = '_';
156 tag
->region_mask
= htonl(region_mask
);
158 memcpy(tag
->region_code
, region_code
, 2);
161 tag
->len
= htonl(buflen
);
162 tag
->data_len
= htonl(datalen
);
163 tag
->base1
= htonl(base1
);
164 tag
->base2
= htonl(base2
);
168 memcpy(tag
->hwv
, "hwv", 3);
169 memcpy(tag
->hwv_val
, hwver
, strlen(hwver
));
173 tag
->crc
= htonl(buffalo_crc(buf
, buflen
));
176 static int tag_file(void)
184 fsize
= get_file_size(ifname
);
186 ERR("unable to get size of '%s'", ifname
);
190 buflen
= fsize
+ sizeof(struct buffalo_tag
);
191 buf
= malloc(buflen
);
193 ERR("no memory for buffer\n");
197 err
= read_file_to_buf(ifname
, buf
+ sizeof(struct buffalo_tag
),
200 ERR("unable to read from file '%s'", ifname
);
204 fixup_tag(buf
, buflen
, fsize
);
206 err
= write_buf_to_file(ofname
, buf
, buflen
);
208 ERR("unable to write to file '%s'", ofname
);
220 int main(int argc
, char *argv
[])
222 int res
= EXIT_FAILURE
;
225 progname
= basename(argv
[0]);
230 c
= getopt(argc
, argv
, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:");
242 base1
= strtoul(optarg
, NULL
, 16);
245 base2
= strtoul(optarg
, NULL
, 16);
248 flag
= strtoul(optarg
, NULL
, 2);
266 err
= process_region(optarg
);
288 err
= check_params();
This page took 0.058469 seconds and 5 git commands to generate.