1/*
2 * Copyright (C) 2004  Manuel Novoa III  <mjn3@codepoet.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19/* July 29, 2004
20 *
21 * This is a hacked replacement for the 'addpattern' utility used to
22 * create wrt54g .bin firmware files.  It isn't pretty, but it does
23 * the job for me.
24 *
25 * Extensions:
26 *  -v allows setting the version string on the command line.
27 *  -{0|1} sets the (currently ignored) hw_ver flag in the header
28 *      to 0 or 1 respectively.
29 */
30
31/* January 12, 2005
32 *
33 * Modified by rodent at rodent dot za dot net
34 * Support added for the new WRT54G v2.2 and WRT54GS v1.1 "flags"
35 * Without the flags set to 0x7, the above units will refuse to flash.
36 *
37 * Extensions:
38 *  -{0|1|2} sets {0|1} sets hw_ver flag to 0/1. {2} sets hw_ver to 1
39 *     and adds the new hardware "flags" for the v2.2/v1.1 units
40*/
41
42/* January 1, 2007
43 *
44 * Modified by juan.i.gonzalez at subdown dot net
45 * Support added for the AG241v2  and similar
46 *
47 * Extensions:
48 *  -r #.# adds revision hardware flags. AG241v2 and similar.
49 *
50 * AG241V2 firmware sets the hw_ver to 0x44.
51 *
52 * Example: -r 2.0
53 *
54 * Convert 2.0 to 20 to be an integer, and add 0x30 to skip special ASCII
55 * #define HW_Version ((HW_REV * 10) + 0x30)  -> from cyutils.h
56*/
57
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <time.h>
62#include <unistd.h>
63#include <sys/stat.h>
64
65/**********************************************************************/
66
67#define CODE_ID		"U2ND"		/* from code_pattern.h */
68#define CODE_PATTERN   "W54S"	/* from code_pattern.h */
69#define PBOT_PATTERN   "PBOT"
70
71#define CYBERTAN_VERSION	"v3.37.2" /* from cyutils.h */
72
73/* WRT54G v2.2 and WRT54GS v1.1 "flags" (from 3.37.32 firmware cyutils.h) */
74#define SUPPORT_4712_CHIP      0x0001
75#define SUPPORT_INTEL_FLASH    0x0002
76#define SUPPORT_5325E_SWITCH   0x0004
77/* (from 3.00.24 firmware cyutils.h) */
78#define SUPPORT_4704_CHIP      0x0008
79#define SUPPORT_5352E_CHIP     0x0010
80/* (from WD My Net Wi-Fi Range Extender's cyutils.s) */
81#define SUPPORT_4703_CHIP      0x0020
82
83struct code_header {			/* from cyutils.h */
84	char magic[8];
85	char fwdate[3];
86	char fwvern[3];
87	char id[4];					/* U2ND */
88	char hw_ver;    			/* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
89
90	unsigned char  sn;		// Serial Number
91	unsigned char  flags[2];	/* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
92	unsigned char  stable[2];	// The image is stable (for dual image)
93	unsigned char  try1[2];		// Try to boot image first time (for dual image)
94	unsigned char  try2[2];		// Try to boot image second time (for dual image)
95	unsigned char  try3[2];		// Try to boot image third time (for dual_image)
96	unsigned char  res3[2];
97} ;
98
99struct board_info {
100	char	*id;
101	char	*pattern;
102	char	hw_ver;
103	char	sn;
104	char	flags[2];
105};
106
107struct board_info boards[] = {
108	{
109		.id		= "WRT160NL",
110		.pattern	= "NL16",
111		.hw_ver		= 0x00,
112		.sn		= 0x0f,
113		.flags		= {0x3f, 0x00},
114	},
115	{
116		.id             = "E2100L",
117		.pattern        = "NL1X",
118		.hw_ver         = 0x00,
119		.sn             = 0x0f,
120		.flags          = {0x3f, 0x00},
121	},
122	{
123		.id		= "mynet-rext",
124		.pattern	= "WDHNSTFH",
125		.hw_ver		= 0x00,
126		.sn		= 0x00,
127		.flags		= {0x3f, 0x00},
128	}, {
129		/* Terminating entry */
130		.id	= NULL,
131	}
132};
133
134/**********************************************************************/
135
136void usage(void) __attribute__ (( __noreturn__ ));
137
138void usage(void)
139{
140	fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-B board_id] [-p pattern] [-s serial] [-g] [-b] [-v v#.#.#] [-r #.#] [-{0|1|2|4|5}] -h\n");
141	exit(EXIT_FAILURE);
142}
143
144struct board_info *find_board(char *id)
145{
146	struct board_info *board;
147
148	for (board = boards; board->id != NULL; board++)
149		if (strcasecmp(id, board->id) == 0)
150			return board;
151
152	return NULL;
153}
154
155int main(int argc, char **argv)
156{
157	char buf[1024];	/* keep this at 1k or adjust garbage calc below */
158	struct code_header *hdr;
159	FILE *in = stdin;
160	FILE *out = stdout;
161	char *ifn = NULL;
162	char *ofn = NULL;
163	char *pattern = CODE_PATTERN;
164	char *pbotpat = PBOT_PATTERN;
165	char *version = CYBERTAN_VERSION;
166	char *board_id = NULL;
167	struct board_info *board = NULL;
168	int gflag = 0;
169	int pbotflag = 0;
170	int c;
171	int v0, v1, v2;
172	size_t off, n;
173	time_t t;
174	struct tm *ptm;
175
176	fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
177
178	hdr = (struct code_header *) buf;
179	memset(hdr, 0, sizeof(struct code_header));
180
181	while ((c = getopt(argc, argv, "i:o:p:s:gbv:01245hr:B:")) != -1) {
182		switch (c) {
183			case 'i':
184				ifn = optarg;
185				break;
186			case 'o':
187				ofn = optarg;
188				break;
189			case 'p':
190				pattern = optarg;
191				break;
192			case 's':
193				hdr->sn = (unsigned char) atoi (optarg);
194				break;
195			case 'g':
196				gflag = 1;
197				break;
198			case 'b':
199				pbotflag = 1;
200				break;
201			case 'v':			/* extension to allow setting version */
202				version = optarg;
203				break;
204			case '0':
205				hdr->hw_ver = 0;
206				break;
207			case '1':
208				hdr->hw_ver = 1;
209				break;
210			case '2': 			/* new 54G v2.2 and 54GS v1.1 flags */
211				hdr->hw_ver = 1;
212				hdr->flags[0] |= SUPPORT_4712_CHIP;
213				hdr->flags[0] |= SUPPORT_INTEL_FLASH;
214				hdr->flags[0] |= SUPPORT_5325E_SWITCH;
215				break;
216			case '4':
217				/* V4 firmware sets the flags to 0x1f */
218				hdr->hw_ver = 0;
219				hdr->flags[0] = 0x1f;
220				break;
221			case '5':
222				/* V5 is appended to trxV2 image */
223				hdr->stable[0] = 0x73; // force image to be stable
224				hdr->stable[1] = 0x00;
225				hdr->try1[0]   = 0x74; // force try1 to be set
226				hdr->try1[1]   = 0x00;
227				hdr->try2[0]   = hdr->try2[1]   = 0xFF;
228				hdr->try3[0]   = hdr->try3[1]   = 0xFF;
229				break;
230                        case 'r':
231                                hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
232                                break;
233                        case 'B':
234                                board_id = optarg;
235                                break;
236
237                        case 'h':
238			default:
239				usage();
240		}
241	}
242
243    	if (optind != argc || optind == 1) {
244		fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
245		usage();
246	}
247
248	if (board_id) {
249		board = find_board(board_id);
250		if (board == NULL) {
251			fprintf(stderr, "unknown board \"%s\"\n", board_id);
252			usage();
253		}
254		pattern = board->pattern;
255		hdr->hw_ver = board->hw_ver;
256		hdr->sn = board->sn;
257		hdr->flags[0] = board->flags[0];
258		hdr->flags[1] = board->flags[1];
259	}
260
261	if (strlen(pattern) > 8) {
262		fprintf(stderr, "illegal pattern \"%s\"\n", pattern);
263		usage();
264	}
265
266	if (ifn && !(in = fopen(ifn, "r"))) {
267		fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
268		usage();
269	}
270
271	if (ofn && !(out = fopen(ofn, "w"))) {
272		fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
273		usage();
274	}
275
276	if (time(&t) == (time_t)(-1)) {
277		fprintf(stderr, "time call failed\n");
278		return EXIT_FAILURE;
279	}
280
281	ptm = localtime(&t);
282
283	if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
284		fprintf(stderr, "bad version string \"%s\"\n", version);
285		return EXIT_FAILURE;
286	}
287
288	memcpy(hdr->magic, pattern, strlen(pattern));
289	if (pbotflag)
290		memcpy(&hdr->magic[4], pbotpat, 4);
291	hdr->fwdate[0] = ptm->tm_year % 100;
292	hdr->fwdate[1] = ptm->tm_mon + 1;
293	hdr->fwdate[2] = ptm->tm_mday;
294	hdr->fwvern[0] = v0;
295	hdr->fwvern[1] = v1;
296	hdr->fwvern[2] = v2;
297	memcpy(hdr->id, CODE_ID, strlen(CODE_ID));
298
299	off = sizeof(struct code_header);
300
301	fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
302			v0, v1, v2,
303			hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
304
305
306	while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
307		off = 0;
308		if (n < sizeof(buf)) {
309			if (ferror(in)) {
310			FREAD_ERROR:
311				fprintf(stderr, "fread error\n");
312				return EXIT_FAILURE;
313			}
314			if (gflag) {
315				gflag = sizeof(buf) - n;
316				memset(buf + n, 0xff, gflag);
317				fprintf(stderr, "adding %d bytes of garbage\n", gflag);
318				n = sizeof(buf);
319			}
320		}
321		if (!fwrite(buf, n, 1, out)) {
322		FWRITE_ERROR:
323			fprintf(stderr, "fwrite error\n");
324			return EXIT_FAILURE;
325		}
326	}
327
328	if (ferror(in)) {
329		goto FREAD_ERROR;
330	}
331
332	if (fflush(out)) {
333		goto FWRITE_ERROR;
334	}
335
336	fclose(in);
337	fclose(out);
338
339	return EXIT_SUCCESS;
340}
341