summaryrefslogtreecommitdiff
path: root/tools/firmware-utils/src/motorola-bin.c
blob: 680249c0072c5f7ca4348a7c57716abdea8dabe2 (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
/*
 * motorola-bin.c
 *
 * Copyright (C) 2005-2006 Mike Baker,
 *                         Imre Kaloz <kaloz@openwrt.org>
 *                         D. Hugh Redelmeier
 *                         OpenWrt.org
 *
 * $Id$
 *
 * 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.
 *
 */

/*
 * Motorola's firmware flashing code requires an extra header.
 * The header is eight bytes (see struct motorola below).
 * This program will take a firmware file and create a new one
 * with this header:
 *	motorola-bin --wr850g WR850G_V403.stripped.trx WR850G_V403.trx
 *
 * Note: Motorola's firmware is distributed with this header.
 * If you need to flash Motorola firmware on a router running OpenWRT,
 * you will to remove this header.  Use the --strip flag:
 *	motorola-bin --strip WR850G_V403.trx WR850G_V403.stripped.trx
 */

/*
 * February 1, 2006
 *
 * Add support for for creating WA840G and WE800G images
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <netinet/in.h>
#include <inttypes.h>

#define BPB 8 /* bits/byte */

static uint32_t crc32[1<<BPB];

static void init_crc32()
{
	const uint32_t poly = ntohl(0x2083b8ed);
	int n;

	for (n = 0; n < 1<<BPB; n++) {
		uint32_t crc = n;
		int bit;

		for (bit = 0; bit < BPB; bit++)
			crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
		crc32[n] = crc;
	}
}

static uint32_t crc32buf(unsigned char *buf, size_t len)
{
	uint32_t crc = 0xFFFFFFFF;

	for (; len; len--, buf++)
		crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
	return crc;
}

struct motorola {
	uint32_t crc;	// crc32 of the remainder
	uint32_t flags;	// unknown, 105770*
};

static const struct model {
	char digit;	/* a digit signifying model (historical) */
	const char *name;
	uint32_t flags;
} models[] = {
	{ '1', "WR850G", 0x10577050LU },
	{ '2', "WA840G", 0x10577040LU },
	{ '3', "WE800G", 0x10577000LU },
	{ '\0', NULL, 0 }
};

static void usage(const char *) __attribute__ (( __noreturn__ ));

static void usage(const char *mess)
{
	const struct model *m;

	fprintf(stderr, "Error: %s\n", mess);
	fprintf(stderr, "Usage: motorola-bin -device|--strip infile outfile\n");
	fprintf(stderr, "Known devices: ");

	for (m = models; m->digit != '\0'; m++)
		fprintf(stderr, " %c - %s", m->digit, m->name);

	fprintf(stderr, "\n");
	exit(1);
}

int main(int argc, char **argv)
{
	off_t len;	// of original firmware
	int fd;
	void *trx;	// pointer to original firmware (mmmapped)
	struct motorola *firmware;	// pionter to prefix + copy of original firmware
	uint32_t flags;

	// verify parameters

	if (argc != 4)
		usage("wrong number of arguments");

	// mmap trx file
	if ((fd = open(argv[2], O_RDONLY))  < 0
	|| (len = lseek(fd, 0, SEEK_END)) < 0
	|| (trx = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
	|| close(fd) < 0)
	{
		fprintf(stderr, "Error loading file %s: %s\n", argv[2], strerror(errno));
		exit(1);
	}

	init_crc32();

	if (strcmp(argv[1], "--strip") == 0)
	{
		const char *ugh = NULL;

		if (len < sizeof(struct motorola)) {
			ugh = "input file too short";
		} else {
			const struct model *m;

			firmware = trx;
			if (htonl(crc32buf(trx + offsetof(struct motorola, flags), len - offsetof(struct motorola, flags))) != firmware->crc)
				ugh = "Invalid CRC";
			for (m = models; ; m++) {
				if (m->digit == '\0') {
					if (ugh == NULL)
						ugh = "unrecognized flags field";
					break;
				}
				if (firmware->flags == htonl(m->flags)) {
					fprintf(stderr, "Firmware for Motorola %s\n", m->name);
					break;
				}
			}
		}

		if (ugh != NULL) {
			fprintf(stderr, "%s\n", ugh);
			exit(3);
		} else {
			// all is well, write the file without the prefix
			if ((fd = open(argv[3], O_CREAT|O_WRONLY,0644)) < 0
			|| write(fd, trx + sizeof(struct motorola), len - sizeof(struct motorola)) !=  len - sizeof(struct motorola)
			|| close(fd) < 0)
			{
				fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
				exit(2);
			}
		}
		
	} else {
		// setup the firmware flags magic number
		const struct model *m;
		const char *df = argv[1];

		if (*df != '-')
			usage("first argument must start with -");
		if (*++df == '-')
			++df;	/* allow but don't require second - */

		for (m = models; ; m++) {
			if (m->digit == '\0')
				usage("unrecognized device specified");
			if ((df[0] == m->digit && df[1] == '\0') || strcasecmp(df, m->name) == 0) {
				flags = m->flags;
				break;
			}
		}


		// create a firmware image in memory
		// and copy the trx to it
		firmware = malloc(sizeof(struct motorola) + len);
		memcpy(&firmware[1], trx, len);

		// setup the motorola headers
		firmware->flags = htonl(flags);

		// CRC of flags + firmware
		firmware->crc = htonl(crc32buf((unsigned char *)&firmware->flags, sizeof(firmware->flags) + len));

		// write the firmware
		if ((fd = open(argv[3], O_CREAT|O_WRONLY,0644)) < 0
		|| write(fd, firmware, sizeof(struct motorola) + len) != sizeof(struct motorola) + len
		|| close(fd) < 0)
		{
			fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
			exit(2);
		}

		free(firmware);
	}

	munmap(trx,len);

	return 0;
}