aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/mkheader_gemtek.c
blob: 9e618efbadd0a10f8e20c8fa4c983a3af05acfe9 (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
/*
 * Copyright (C) 2014  Claudio Leite <leitec@staticky.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * Builds a proper flash image for routers using some Gemtek
 * OEM boards. These include the Airlink101 AR725W, the
 * Asante SmartHub 600 (AWRT-600N), and Linksys WRT100/110.
 *
 * The resulting image is compatible with the factory firmware
 * web upgrade and TFTP interface.
 *
 * To build:
 *  gcc -O2 -o mkheader_gemtek mkheader_gemtek.c -lz
 *
 * Claudio Leite <leitec@staticky.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <zlib.h>		/* for crc32() */

/*
 * The header is in little-endian format. In case
 * we are on a BE host, we need to swap binary
 * values.
 */
#ifdef __APPLE__
# include <libkern/OSByteOrder.h>
# define le32 OSSwapHostToLittleInt32
#else
# if defined(__linux__)
#  include <endian.h>
#  if __BYTE_ORDER == __BIG_ENDIAN
#   define CPU_BIG_ENDIAN
#  endif
# else
#  include <sys/endian.h>		/* BSD's should have this */
#  if _BYTE_ORDER == _BIG_ENDIAN
#   define CPU_BIG_ENDIAN
#  endif
# endif
# ifdef CPU_BIG_ENDIAN
#  define le32(x) (((x & 0xff000000) >> 24) | \
                   ((x & 0x00ff0000) >> 8)  | \
                   ((x & 0x0000ff00) << 8)  | \
                   ((x & 0x000000ff) << 24))
# else
#  define le32(x) (x)
# endif
#endif

struct gemtek_header {
	uint8_t 	magic[4];
	uint8_t 	version[4];
	uint32_t 	product_id;
	uint32_t 	imagesz;
	uint32_t 	checksum;
	uint32_t 	fast_checksum;
	uint8_t 	build[4];
	uint8_t 	lang[4];
};

#define HDRLEN	sizeof(struct gemtek_header)

struct machines {
	char           *desc;
	char           *id;
	uint32_t 	maxsize;
	struct gemtek_header header;
};

struct machines mach_def[] = {
	{"Airlink101 AR725W", "ar725w", 0x340000,
		{"GMTK", "1003", le32(0x03000001), 0, 0,
		  0, "01\0\0", "EN\0\0"}},
	{"Asante AWRT-600N", "awrt600n", 0x340000,
		{"A600", "1005", le32(0x03000001), 0, 0,
		  0, "01\0\0", "EN\0\0"}},
	{"Linksys WRT100", "wrt100", 0x320000,
		{"GMTK", "1007", le32(0x03040001), 0, 0,
		  0, "2\0\0\0", "EN\0\0"}},
	{"Linksys WRT110", "wrt110", 0x320000,
		{"GMTK", "1007", le32(0x03040001), 0, 0,
		  0, "2\0\0\0", "EN\0\0"}},
	{0}
};

int
main(int argc, char *argv[])
{
	unsigned long 	res, flen;
	struct gemtek_header my_hdr;
	FILE           *f, *f_out;
	int 		image_type = -1, index;
	uint8_t        *buf;
	uint32_t 	crc;

	if (argc < 3) {
		fprintf(stderr, "mkheader_gemtek <uImage> <webflash image> [machine ID]\n");
		fprintf(stderr, "  where [machine ID] is one of:\n");
		for (index = 0; mach_def[index].desc != 0; index++) {
			fprintf(stderr, "    %-10s  %s", mach_def[index].id, mach_def[index].desc);
			if (index == 0)
				fprintf(stderr, " (default)\n");
			else
				fprintf(stderr, "\n");
		}

		exit(-1);
	}

	if (argc == 4) {
		for(index = 0; mach_def[index].id != 0; index++) {
			if(strcmp(mach_def[index].id, argv[3]) == 0) {
				image_type = index;
				break;
			}
		}

		if(image_type == -1) {
			fprintf(stderr, "\nERROR: invalid machine type\n");
			exit(-1);
		}
	} else
		image_type = 0;

	printf("Opening %s...\n", argv[1]);

	f = fopen(argv[1], "r");
	if(!f) {
		fprintf(stderr, "\nERROR: couldn't open input image\n");
		exit(-1);
	}

	fseek(f, 0, SEEK_END);
	flen = (unsigned long) ftell(f);

	printf("  %lu (0x%lX) bytes long\n", flen, flen);

	if (flen > mach_def[image_type].maxsize) {
		fprintf(stderr, "\nERROR: image exceeds maximum compatible size\n");
		goto f_error;
	}

	buf = malloc(flen + HDRLEN);
	if (!buf) {
		fprintf(stderr, "\nERROR: couldn't allocate buffer\n");
		goto f_error;
	}
	rewind(f);
	res = fread(buf + HDRLEN, 1, flen, f);
	if (res != flen) {
		perror("Couldn't read entire file: fread()");
		goto f_error;
	}
	fclose(f);

	printf("\nCreating %s...\n", argv[2]);

	memcpy(&my_hdr, &mach_def[image_type].header, HDRLEN);

	printf("  Using %s magic\n", mach_def[image_type].desc);

	my_hdr.imagesz = le32(flen + HDRLEN);
	memcpy(my_hdr.lang, "EN", 2);

	memcpy(buf, &my_hdr, HDRLEN);

	crc = crc32(0, buf, flen + HDRLEN);
	printf("  CRC32: %08X\n", crc);

	my_hdr.checksum = le32(crc);
	memcpy(buf, &my_hdr, HDRLEN);

	printf("  Writing...\n");

	f_out = fopen(argv[2], "w");
	if(!f_out) {
		fprintf(stderr, "\nERROR: couldn't open output image\n");
		exit(-1);
	}

	fwrite(buf, 1, flen + HDRLEN, f_out);

	fclose(f_out);

	free(buf);
	return 0;

f_error:
	fclose(f);
	exit(-1);
}