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 #define MAX_INPUT_FILES 2
32 static char *progname
;
33 static char *ifname
[MAX_INPUT_FILES
];
34 static ssize_t fsize
[MAX_INPUT_FILES
];
39 static char *language
;
41 static char *platform
;
44 static char *minor
= "1.01";
46 static uint32_t base1
;
47 static uint32_t base2
;
48 static char *region_code
;
49 static uint32_t region_mask
;
50 static int num_regions
;
52 void usage(int status
)
54 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
56 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
60 " -a <platform> set platform to <platform>\n"
61 " -b <brand> set brand to <brand>\n"
64 " -f <flag> set flag to <flag>\n"
65 " -i <file> read input from the file <file>\n"
66 " -l <language> set language to <language>\n"
67 " -m <version> set minor version to <version>\n"
68 " -o <file> write output to the file <file>\n"
69 " -p <product> set product to <product>\n"
70 " -r <region> set image region to <region>\n"
71 " valid regions: JP, US, EU, AP, TW, KR, M_\n"
72 " -s skip CRC calculation\n"
73 " -v <version> set major version to <version>\n"
74 " -w <version> set harwdware version to <version>\n"
75 " -h show this screen\n"
81 static int check_params(void)
84 #define CHECKSTR(_var, _name, _len) do { \
85 if ((_var) == NULL) { \
86 ERR("no %s specified", (_name)); \
90 strlen((_var)) > ((_len) - 1)) { \
91 ERR("%s is too long", (_name)); \
97 ERR("no input files specified");
99 CHECKSTR(ofname
, "output file", 0);
100 CHECKSTR(brand
, "brand", TAG_BRAND_LEN
);
101 CHECKSTR(product
, "product", TAG_PRODUCT_LEN
);
102 CHECKSTR(platform
, "platform", TAG_PLATFORM_LEN
);
103 CHECKSTR(major
, "major version", TAG_VERSION_LEN
);
104 CHECKSTR(minor
, "minor version", TAG_VERSION_LEN
);
105 CHECKSTR(language
, "language", TAG_LANGUAGE_LEN
);
108 CHECKSTR(hwver
, "hardware version", 2);
110 if (num_regions
== 0) {
111 ERR("no region code specified");
120 static int process_region(char *reg
)
124 if (strlen(reg
) != 2) {
125 ERR("invalid region code '%s'", reg
);
129 if (strcmp(reg
, "M_") == 0) {
136 for (i
= 0; i
< ARRAY_SIZE(region_table
); i
++)
137 if (strcmp(reg
, region_table
[i
]) == 0) {
139 region_mask
|= 1 << i
;
144 ERR("unknown region code '%s'", reg
);
148 static int process_ifname(char *name
)
150 if (num_files
>= ARRAY_SIZE(ifname
)) {
151 ERR("too many input files specified");
155 ifname
[num_files
++] = name
;
159 static void fixup_tag(unsigned char *buf
, ssize_t buflen
)
161 struct buffalo_tag
*tag
= (struct buffalo_tag
*) buf
;
163 memset(tag
, '\0', sizeof(*tag
));
165 memcpy(tag
->brand
, brand
, strlen(brand
));
166 memcpy(tag
->product
, product
, strlen(product
));
167 memcpy(tag
->platform
, platform
, strlen(platform
));
168 memcpy(tag
->ver_major
, major
, strlen(major
));
169 memcpy(tag
->ver_minor
, minor
, strlen(minor
));
170 memcpy(tag
->language
, language
, strlen(language
));
172 if (num_regions
> 1) {
173 tag
->region_code
[0] = 'M';
174 tag
->region_code
[1] = '_';
175 tag
->region_mask
= htonl(region_mask
);
177 memcpy(tag
->region_code
, region_code
, 2);
180 tag
->len
= htonl(buflen
);
181 tag
->data_len
= htonl(fsize
[0]);
182 tag
->base1
= htonl(base1
);
183 tag
->base2
= htonl(base2
);
187 memcpy(tag
->hwv
, "hwv", 3);
188 memcpy(tag
->hwv_val
, hwver
, strlen(hwver
));
192 tag
->crc
= htonl(buffalo_crc(buf
, buflen
));
195 static void fixup_tag2(unsigned char *buf
, ssize_t buflen
)
197 struct buffalo_tag2
*tag
= (struct buffalo_tag2
*) buf
;
199 memset(tag
, '\0', sizeof(*tag
));
201 memcpy(tag
->brand
, brand
, strlen(brand
));
202 memcpy(tag
->product
, product
, strlen(product
));
203 memcpy(tag
->platform
, platform
, strlen(platform
));
204 memcpy(tag
->ver_major
, major
, strlen(major
));
205 memcpy(tag
->ver_minor
, minor
, strlen(minor
));
206 memcpy(tag
->language
, language
, strlen(language
));
208 if (num_regions
> 1) {
209 tag
->region_code
[0] = 'M';
210 tag
->region_code
[1] = '_';
211 tag
->region_mask
= htonl(region_mask
);
213 memcpy(tag
->region_code
, region_code
, 2);
216 tag
->total_len
= htonl(buflen
);
217 tag
->len1
= htonl(fsize
[0]);
218 tag
->len2
= htonl(fsize
[1]);
222 memcpy(tag
->hwv
, "hwv", 3);
223 memcpy(tag
->hwv_val
, hwver
, strlen(hwver
));
227 tag
->crc
= htonl(buffalo_crc(buf
, buflen
));
230 static int tag_file(void)
241 hdrlen
= sizeof(struct buffalo_tag
);
243 hdrlen
= sizeof(struct buffalo_tag2
);
247 for (i
= 0; i
< num_files
; i
++) {
248 fsize
[i
] = get_file_size(ifname
[i
]);
250 ERR("unable to get size of '%s'", ifname
[i
]);
256 buf
= malloc(buflen
);
258 ERR("no memory for buffer\n");
263 for (i
= 0; i
< num_files
; i
++) {
264 err
= read_file_to_buf(ifname
[i
], buf
+ offset
, fsize
[i
]);
266 ERR("unable to read from file '%s'", ifname
[i
]);
274 fixup_tag(buf
, buflen
);
276 fixup_tag2(buf
, buflen
);
278 err
= write_buf_to_file(ofname
, buf
, buflen
);
280 ERR("unable to write to file '%s'", ofname
);
292 int main(int argc
, char *argv
[])
294 int res
= EXIT_FAILURE
;
297 progname
= basename(argv
[0]);
302 c
= getopt(argc
, argv
, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:");
314 base1
= strtoul(optarg
, NULL
, 16);
317 base2
= strtoul(optarg
, NULL
, 16);
320 flag
= strtoul(optarg
, NULL
, 2);
323 err
= process_ifname(optarg
);
340 err
= process_region(optarg
);
362 err
= check_params();
This page took 0.083828 seconds and 5 git commands to generate.