tools: adapt addpattern for WD's Range Extender
[openwrt/openwrt.git] / tools / firmware-utils / src / addpattern.c
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
83 struct 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
99 struct board_info {
100 char *id;
101 char *pattern;
102 char hw_ver;
103 char sn;
104 char flags[2];
105 };
106
107 struct 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 = "mynet-rext",
117 .pattern = "WDHNSTFH",
118 .hw_ver = 0x00,
119 .sn = 0x00,
120 .flags = {0x3f, 0x00},
121 }, {
122 /* Terminating entry */
123 .id = NULL,
124 }
125 };
126
127 /**********************************************************************/
128
129 void usage(void) __attribute__ (( __noreturn__ ));
130
131 void usage(void)
132 {
133 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");
134 exit(EXIT_FAILURE);
135 }
136
137 struct board_info *find_board(char *id)
138 {
139 struct board_info *board;
140
141 for (board = boards; board->id != NULL; board++)
142 if (strcasecmp(id, board->id) == 0)
143 return board;
144
145 return NULL;
146 }
147
148 int main(int argc, char **argv)
149 {
150 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
151 struct code_header *hdr;
152 FILE *in = stdin;
153 FILE *out = stdout;
154 char *ifn = NULL;
155 char *ofn = NULL;
156 char *pattern = CODE_PATTERN;
157 char *pbotpat = PBOT_PATTERN;
158 char *version = CYBERTAN_VERSION;
159 char *board_id = NULL;
160 struct board_info *board = NULL;
161 int gflag = 0;
162 int pbotflag = 0;
163 int c;
164 int v0, v1, v2;
165 size_t off, n;
166 time_t t;
167 struct tm *ptm;
168
169 fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
170
171 hdr = (struct code_header *) buf;
172 memset(hdr, 0, sizeof(struct code_header));
173
174 while ((c = getopt(argc, argv, "i:o:p:s:gbv:01245hr:B:")) != -1) {
175 switch (c) {
176 case 'i':
177 ifn = optarg;
178 break;
179 case 'o':
180 ofn = optarg;
181 break;
182 case 'p':
183 pattern = optarg;
184 break;
185 case 's':
186 hdr->sn = (unsigned char) atoi (optarg);
187 break;
188 case 'g':
189 gflag = 1;
190 break;
191 case 'b':
192 pbotflag = 1;
193 break;
194 case 'v': /* extension to allow setting version */
195 version = optarg;
196 break;
197 case '0':
198 hdr->hw_ver = 0;
199 break;
200 case '1':
201 hdr->hw_ver = 1;
202 break;
203 case '2': /* new 54G v2.2 and 54GS v1.1 flags */
204 hdr->hw_ver = 1;
205 hdr->flags[0] |= SUPPORT_4712_CHIP;
206 hdr->flags[0] |= SUPPORT_INTEL_FLASH;
207 hdr->flags[0] |= SUPPORT_5325E_SWITCH;
208 break;
209 case '4':
210 /* V4 firmware sets the flags to 0x1f */
211 hdr->hw_ver = 0;
212 hdr->flags[0] = 0x1f;
213 break;
214 case '5':
215 /* V5 is appended to trxV2 image */
216 hdr->stable[0] = 0x73; // force image to be stable
217 hdr->stable[1] = 0x00;
218 hdr->try1[0] = 0x74; // force try1 to be set
219 hdr->try1[1] = 0x00;
220 hdr->try2[0] = hdr->try2[1] = 0xFF;
221 hdr->try3[0] = hdr->try3[1] = 0xFF;
222 break;
223 case 'r':
224 hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
225 break;
226 case 'B':
227 board_id = optarg;
228 break;
229
230 case 'h':
231 default:
232 usage();
233 }
234 }
235
236 if (optind != argc || optind == 1) {
237 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
238 usage();
239 }
240
241 if (board_id) {
242 board = find_board(board_id);
243 if (board == NULL) {
244 fprintf(stderr, "unknown board \"%s\"\n", board_id);
245 usage();
246 }
247 pattern = board->pattern;
248 hdr->hw_ver = board->hw_ver;
249 hdr->sn = board->sn;
250 hdr->flags[0] = board->flags[0];
251 hdr->flags[1] = board->flags[1];
252 }
253
254 if (strlen(pattern) > 8) {
255 fprintf(stderr, "illegal pattern \"%s\"\n", pattern);
256 usage();
257 }
258
259 if (ifn && !(in = fopen(ifn, "r"))) {
260 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
261 usage();
262 }
263
264 if (ofn && !(out = fopen(ofn, "w"))) {
265 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
266 usage();
267 }
268
269 if (time(&t) == (time_t)(-1)) {
270 fprintf(stderr, "time call failed\n");
271 return EXIT_FAILURE;
272 }
273
274 ptm = localtime(&t);
275
276 if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
277 fprintf(stderr, "bad version string \"%s\"\n", version);
278 return EXIT_FAILURE;
279 }
280
281 memcpy(hdr->magic, pattern, strlen(pattern));
282 if (pbotflag)
283 memcpy(&hdr->magic[4], pbotpat, 4);
284 hdr->fwdate[0] = ptm->tm_year % 100;
285 hdr->fwdate[1] = ptm->tm_mon + 1;
286 hdr->fwdate[2] = ptm->tm_mday;
287 hdr->fwvern[0] = v0;
288 hdr->fwvern[1] = v1;
289 hdr->fwvern[2] = v2;
290 memcpy(hdr->id, CODE_ID, strlen(CODE_ID));
291
292 off = sizeof(struct code_header);
293
294 fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
295 v0, v1, v2,
296 hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
297
298
299 while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
300 off = 0;
301 if (n < sizeof(buf)) {
302 if (ferror(in)) {
303 FREAD_ERROR:
304 fprintf(stderr, "fread error\n");
305 return EXIT_FAILURE;
306 }
307 if (gflag) {
308 gflag = sizeof(buf) - n;
309 memset(buf + n, 0xff, gflag);
310 fprintf(stderr, "adding %d bytes of garbage\n", gflag);
311 n = sizeof(buf);
312 }
313 }
314 if (!fwrite(buf, n, 1, out)) {
315 FWRITE_ERROR:
316 fprintf(stderr, "fwrite error\n");
317 return EXIT_FAILURE;
318 }
319 }
320
321 if (ferror(in)) {
322 goto FREAD_ERROR;
323 }
324
325 if (fflush(out)) {
326 goto FWRITE_ERROR;
327 }
328
329 fclose(in);
330 fclose(out);
331
332 return EXIT_SUCCESS;
333 }