summaryrefslogtreecommitdiff
path: root/tools/firmware-utils/src/buffalo-tag.c
blob: b5db72ef968d66fb34a0175adb4b44b14791641f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 *  Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <libgen.h>
#include <getopt.h>     /* for getopt() */
#include <netinet/in.h>

#include "buffalo-lib.h"

#define ERR(fmt, ...) do { \
	fflush(0); \
	fprintf(stderr, "[%s] *** error: " fmt "\n", \
			progname, ## __VA_ARGS__ ); \
} while (0)

static char *region_table[] = {
	"JP", "US", "EU", "AP", "TW", "KR"
};

#define MAX_INPUT_FILES	2

static char *progname;
static char *ifname[MAX_INPUT_FILES];
static ssize_t fsize[MAX_INPUT_FILES];
static int num_files;
static char *ofname;
static char *product;
static char *brand;
static char *language;
static char *hwver;
static char *platform;
static int flag;
static char *major;
static char *minor = "1.01";
static int skipcrc;
static uint32_t base1;
static uint32_t base2;
static char *region_code;
static uint32_t region_mask;
static int num_regions;

void usage(int status)
{
	FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;

	fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
	fprintf(stream,
"\n"
"Options:\n"
"  -a <platform>   set platform to <platform>\n"
"  -b <brand>      set brand to <brand>\n"
"  -c <base1>\n"
"  -d <base2>\n"
"  -f <flag>       set flag to <flag>\n"
"  -i <file>       read input from the file <file>\n"
"  -l <language>   set language to <language>\n"
"  -m <version>    set minor version to <version>\n"
"  -o <file>       write output to the file <file>\n"
"  -p <product>    set product to <product>\n"
"  -r <region>     set image region to <region>\n"
"		   valid regions: JP, US, EU, AP, TW, KR, M_\n"
"  -s              skip CRC calculation\n"
"  -v <version>    set major version to <version>\n"
"  -w <version>    set harwdware version to <version>\n"
"  -h              show this screen\n"
	);

	exit(status);
}

static int check_params(void)
{

#define CHECKSTR(_var, _name, _len)	do {		\
	if ((_var) == NULL) {				\
		ERR("no %s specified", (_name));	\
		return -1;				\
	}						\
	if ((_len) > 0 &&				\
	    strlen((_var)) > ((_len) - 1)) {		\
		ERR("%s is too long", (_name));		\
		return -1;				\
	}						\
} while (0)

	if (num_files == 0)
		ERR("no input files specified");

	CHECKSTR(ofname, "output file", 0);
	CHECKSTR(brand, "brand", TAG_BRAND_LEN);
	CHECKSTR(product, "product", TAG_PRODUCT_LEN);
	CHECKSTR(platform, "platform", TAG_PLATFORM_LEN);
	CHECKSTR(major, "major version", TAG_VERSION_LEN);
	CHECKSTR(minor, "minor version", TAG_VERSION_LEN);
	CHECKSTR(language, "language", TAG_LANGUAGE_LEN);

	if (hwver)
		CHECKSTR(hwver, "hardware version", 2);

	if (num_regions == 0) {
		ERR("no region code specified");
		return -1;
	}

	return 0;

#undef CHECKSTR
}

static int process_region(char *reg)
{
	int i;

	if (strlen(reg) != 2) {
		ERR("invalid region code '%s'", reg);
		return -1;
	}

	if (strcmp(reg, "M_") == 0) {
		region_code = reg;
		region_mask |= ~0;
		num_regions = 32;
		return 0;
	}

	for (i = 0; i < ARRAY_SIZE(region_table); i++)
		if (strcmp(reg, region_table[i]) == 0) {
			region_code = reg;
			region_mask |= 1 << i;
			num_regions++;
			return 0;
		}

	ERR("unknown region code '%s'", reg);
	return -1;
}

static int process_ifname(char *name)
{
	if (num_files >= ARRAY_SIZE(ifname)) {
		ERR("too many input files specified");
		return -1;
	}

	ifname[num_files++] = name;
	return 0;
}

static void fixup_tag(unsigned char *buf, ssize_t buflen)
{
	struct buffalo_tag *tag = (struct buffalo_tag *) buf;

	memset(tag, '\0', sizeof(*tag));

	memcpy(tag->brand, brand, strlen(brand));
	memcpy(tag->product, product, strlen(product));
	memcpy(tag->platform, platform, strlen(platform));
	memcpy(tag->ver_major, major, strlen(major));
	memcpy(tag->ver_minor, minor, strlen(minor));
	memcpy(tag->language, language, strlen(language));

	if (num_regions > 1) {
		tag->region_code[0] = 'M';
		tag->region_code[1] = '_';
		tag->region_mask = htonl(region_mask);
	} else {
		memcpy(tag->region_code, region_code, 2);
	}

	tag->len = htonl(buflen);
	tag->data_len = htonl(fsize[0]);
	tag->base1 = htonl(base1);
	tag->base2 = htonl(base2);
	tag->flag = flag;

	if (hwver) {
		memcpy(tag->hwv, "hwv", 3);
		memcpy(tag->hwv_val, hwver, strlen(hwver));
	}

	if (!skipcrc)
		tag->crc = htonl(buffalo_crc(buf, buflen));
}

static void fixup_tag2(unsigned char *buf, ssize_t buflen)
{
	struct buffalo_tag2 *tag = (struct buffalo_tag2 *) buf;

	memset(tag, '\0', sizeof(*tag));

	memcpy(tag->brand, brand, strlen(brand));
	memcpy(tag->product, product, strlen(product));
	memcpy(tag->platform, platform, strlen(platform));
	memcpy(tag->ver_major, major, strlen(major));
	memcpy(tag->ver_minor, minor, strlen(minor));
	memcpy(tag->language, language, strlen(language));

	if (num_regions > 1) {
		tag->region_code[0] = 'M';
		tag->region_code[1] = '_';
		tag->region_mask = htonl(region_mask);
	} else {
		memcpy(tag->region_code, region_code, 2);
	}

	tag->total_len = htonl(buflen);
	tag->len1 = htonl(fsize[0]);
	tag->len2 = htonl(fsize[1]);
	tag->flag = flag;

	if (hwver) {
		memcpy(tag->hwv, "hwv", 3);
		memcpy(tag->hwv_val, hwver, strlen(hwver));
	}

	if (!skipcrc)
		tag->crc = htonl(buffalo_crc(buf, buflen));
}

static int tag_file(void)
{
	unsigned char *buf;
	ssize_t offset;
	ssize_t hdrlen;
	ssize_t buflen;
	int err;
	int ret = -1;
	int i;

	if (num_files == 1)
		hdrlen = sizeof(struct buffalo_tag);
	else
		hdrlen = sizeof(struct buffalo_tag2);

	buflen = hdrlen;

	for (i = 0; i < num_files; i++) {
		fsize[i] = get_file_size(ifname[i]);
		if (fsize[i] < 0) {
			ERR("unable to get size of '%s'", ifname[i]);
			goto out;
		}
		buflen += fsize[i];
	}

	buf = malloc(buflen);
	if (!buf) {
		ERR("no memory for buffer\n");
		goto out;
	}

	offset = hdrlen;
	for (i = 0; i < num_files; i++) {
		err = read_file_to_buf(ifname[i], buf + offset, fsize[i]);
		if (err) {
			ERR("unable to read from file '%s'", ifname[i]);
			goto free_buf;
		}

		offset += fsize[i];
	}

	if (num_files == 1)
		fixup_tag(buf, buflen);
	else
		fixup_tag2(buf, buflen);

	err = write_buf_to_file(ofname, buf, buflen);
	if (err) {
		ERR("unable to write to file '%s'", ofname);
		goto free_buf;
	}

	ret = 0;

free_buf:
	free(buf);
out:
	return ret;
}

int main(int argc, char *argv[])
{
	int res = EXIT_FAILURE;
	int err;

	progname = basename(argv[0]);

	while ( 1 ) {
		int c;

		c = getopt(argc, argv, "a:b:c:d:f:hi:l:m:o:p:r:sv:w:");
		if (c == -1)
			break;

		switch (c) {
		case 'a':
			platform = optarg;
			break;
		case 'b':
			brand = optarg;
			break;
		case 'c':
			base1 = strtoul(optarg, NULL, 16);
			break;
		case 'd':
			base2 = strtoul(optarg, NULL, 16);
			break;
		case 'f':
			flag = strtoul(optarg, NULL, 2);
			break;
		case 'i':
			err = process_ifname(optarg);
			if (err)
				goto out;
			break;
		case 'l':
			language = optarg;
			break;
		case 'm':
			minor = optarg;
			break;
		case 'o':
			ofname = optarg;
			break;
		case 'p':
			product = optarg;
			break;
		case 'r':
			err = process_region(optarg);
			if (err)
				goto out;
			break;
		case 's':
			skipcrc = 1;
			break;
		case 'v':
			major = optarg;
			break;
		case 'w':
			hwver = optarg;
			break;
		case 'h':
			usage(EXIT_SUCCESS);
			break;
		default:
			usage(EXIT_FAILURE);
			break;
		}
	}

	err = check_params();
	if (err)
		goto out;

	err = tag_file();
	if (err)
		goto out;

	res = EXIT_SUCCESS;

out:
	return res;
}