tools/firmware-utils: allow to create buffalo tags w/o hw version
[openwrt/svn-archive/archive.git] / tools / firmware-utils / src / buffalo-tag.c
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
101 if (hwver)
102 CHECKSTR(hwver, "hardware version", 2);
103
104 if (num_regions == 0) {
105 ERR("no region code specified");
106 return -1;
107 }
108
109 return 0;
110
111 #undef CHECKSTR
112 }
113
114 static int process_region(char *reg)
115 {
116 int i;
117
118 if (strlen(reg) != 2) {
119 ERR("invalid region code '%s'", reg);
120 return -1;
121 }
122
123 if (strcmp(reg, "M_") == 0) {
124 region_code = reg;
125 region_mask |= ~0;
126 num_regions = 32;
127 return 0;
128 }
129
130 for (i = 0; i < ARRAY_SIZE(region_table); i++)
131 if (strcmp(reg, region_table[i]) == 0) {
132 region_code = reg;
133 region_mask |= 1 << i;
134 num_regions++;
135 return 0;
136 }
137
138 ERR("unknown region code '%s'", reg);
139 return -1;
140 }
141
142 static void fixup_tag(unsigned char *buf, ssize_t buflen, ssize_t datalen)
143 {
144 struct buffalo_tag *tag = (struct buffalo_tag *) buf;
145
146 memset(tag, '\0', sizeof(*tag));
147
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));
154
155 if (num_regions > 1) {
156 tag->region_code[0] = 'M';
157 tag->region_code[1] = '_';
158 tag->region_mask = htonl(region_mask);
159 } else {
160 memcpy(tag->region_code, region_code, 2);
161 }
162
163 tag->len = htonl(buflen);
164 tag->data_len = htonl(datalen);
165 tag->base1 = htonl(base1);
166 tag->base2 = htonl(base2);
167 tag->flag = flag;
168
169 if (hwver) {
170 memcpy(tag->hwv, "hwv", 3);
171 memcpy(tag->hwv_val, hwver, strlen(hwver));
172 }
173
174 if (!skipcrc)
175 tag->crc = htonl(buffalo_crc(buf, buflen));
176 }
177
178 static int tag_file(void)
179 {
180 unsigned char *buf;
181 ssize_t fsize;
182 ssize_t buflen;
183 int err;
184 int ret = -1;
185
186 fsize = get_file_size(ifname);
187 if (fsize < 0) {
188 ERR("unable to get size of '%s'", ifname);
189 goto out;
190 }
191
192 buflen = fsize + sizeof(struct buffalo_tag);
193 buf = malloc(buflen);
194 if (!buf) {
195 ERR("no memory for buffer\n");
196 goto out;
197 }
198
199 err = read_file_to_buf(ifname, buf + sizeof(struct buffalo_tag),
200 fsize);
201 if (err) {
202 ERR("unable to read from file '%s'", ifname);
203 goto free_buf;
204 }
205
206 fixup_tag(buf, buflen, fsize);
207
208 err = write_buf_to_file(ofname, buf, buflen);
209 if (err) {
210 ERR("unable to write to file '%s'", ofname);
211 goto free_buf;
212 }
213
214 ret = 0;
215
216 free_buf:
217 free(buf);
218 out:
219 return ret;
220 }
221
222 int main(int argc, char *argv[])
223 {
224 int res = EXIT_FAILURE;
225 int err;
226
227 progname = basename(argv[0]);
228
229 while ( 1 ) {
230 int c;
231
232 c = getopt(argc, argv, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:");
233 if (c == -1)
234 break;
235
236 switch (c) {
237 case 'a':
238 platform = optarg;
239 break;
240 case 'b':
241 brand = optarg;
242 break;
243 case 'c':
244 base1 = strtoul(optarg, NULL, 16);
245 break;
246 case 'd':
247 base2 = strtoul(optarg, NULL, 16);
248 break;
249 case 'f':
250 flag = strtoul(optarg, NULL, 2);
251 break;
252 case 'i':
253 ifname = optarg;
254 break;
255 case 'l':
256 language = optarg;
257 break;
258 case 'm':
259 minor = optarg;
260 break;
261 case 'o':
262 ofname = optarg;
263 break;
264 case 'p':
265 product = optarg;
266 break;
267 case 'r':
268 err = process_region(optarg);
269 if (err)
270 goto out;
271 break;
272 case 's':
273 skipcrc = 1;
274 break;
275 case 'v':
276 major = optarg;
277 break;
278 case 'w':
279 hwver = optarg;
280 break;
281 case 'h':
282 usage(EXIT_SUCCESS);
283 break;
284 default:
285 usage(EXIT_FAILURE);
286 break;
287 }
288 }
289
290 err = check_params();
291 if (err)
292 goto out;
293
294 err = tag_file();
295 if (err)
296 goto out;
297
298 res = EXIT_SUCCESS;
299
300 out:
301 return res;
302 }