mac80211: fix handling of vif mac addresses in /lib/wifi/mac80211.sh (patch from...
[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
78 struct code_header { /* from cyutils.h */
79 char magic[4];
80 char res1[4]; /* for extra magic */
81 char fwdate[3];
82 char fwvern[3];
83 char id[4]; /* U2ND */
84 char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
85 char unused;
86 unsigned char flags[2]; /* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
87 unsigned char res2[10];
88 } ;
89
90 struct board_info {
91 char *id;
92 char *pattern;
93 char hw_ver;
94 char unused;
95 char flags[2];
96 };
97
98 struct board_info boards[] = {
99 {
100 .id = "WRT160NL",
101 .pattern = "NL16",
102 .hw_ver = 0x00,
103 .unused = 0x0f,
104 .flags = {0x3f, 0x00},
105 }, {
106 /* Terminating entry */
107 .id = NULL,
108 }
109 };
110
111 /**********************************************************************/
112
113 void usage(void) __attribute__ (( __noreturn__ ));
114
115 void usage(void)
116 {
117 fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-B board_id] [-p pattern] [-g] [-b] [-v v#.#.#] [-r #.#] [-{0|1|2|4}] -h\n");
118 exit(EXIT_FAILURE);
119 }
120
121 struct board_info *find_board(char *id)
122 {
123 struct board_info *board;
124
125 for (board = boards; board->id != NULL; board++)
126 if (strcasecmp(id, board->id) == 0)
127 return board;
128
129 return NULL;
130 }
131
132 int main(int argc, char **argv)
133 {
134 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
135 struct code_header *hdr;
136 FILE *in = stdin;
137 FILE *out = stdout;
138 char *ifn = NULL;
139 char *ofn = NULL;
140 char *pattern = CODE_PATTERN;
141 char *pbotpat = PBOT_PATTERN;
142 char *version = CYBERTAN_VERSION;
143 char *board_id = NULL;
144 struct board_info *board = NULL;
145 int gflag = 0;
146 int pbotflag = 0;
147 int c;
148 int v0, v1, v2;
149 size_t off, n;
150 time_t t;
151 struct tm *ptm;
152
153 fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
154
155 hdr = (struct code_header *) buf;
156 memset(hdr, 0, sizeof(struct code_header));
157
158 while ((c = getopt(argc, argv, "i:o:p:gbv:0124hr:B:")) != -1) {
159 switch (c) {
160 case 'i':
161 ifn = optarg;
162 break;
163 case 'o':
164 ofn = optarg;
165 break;
166 case 'p':
167 pattern = optarg;
168 break;
169 case 'g':
170 gflag = 1;
171 break;
172 case 'b':
173 pbotflag = 1;
174 break;
175 case 'v': /* extension to allow setting version */
176 version = optarg;
177 break;
178 case '0':
179 hdr->hw_ver = 0;
180 break;
181 case '1':
182 hdr->hw_ver = 1;
183 break;
184 case '2': /* new 54G v2.2 and 54GS v1.1 flags */
185 hdr->hw_ver = 1;
186 hdr->flags[0] |= SUPPORT_4712_CHIP;
187 hdr->flags[0] |= SUPPORT_INTEL_FLASH;
188 hdr->flags[0] |= SUPPORT_5325E_SWITCH;
189 break;
190 case '4':
191 /* V4 firmware sets the flags to 0x1f */
192 hdr->hw_ver = 0;
193 hdr->flags[0] = 0x1f;
194 break;
195 case 'r':
196 hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
197 break;
198 case 'B':
199 board_id = optarg;
200 break;
201
202 case 'h':
203 default:
204 usage();
205 }
206 }
207
208 if (optind != argc || optind == 1) {
209 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
210 usage();
211 }
212
213 if (board_id) {
214 board = find_board(board_id);
215 if (board == NULL) {
216 fprintf(stderr, "unknown board \"%s\"\n", board_id);
217 usage();
218 }
219 pattern = board->pattern;
220 hdr->hw_ver = board->hw_ver;
221 hdr->unused = board->unused;
222 hdr->flags[0] = board->flags[0];
223 hdr->flags[1] = board->flags[1];
224 }
225
226 if (strlen(pattern) != 4) {
227 fprintf(stderr, "illegal pattern \"%s\": length != 4\n", pattern);
228 usage();
229 }
230
231 if (ifn && !(in = fopen(ifn, "r"))) {
232 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
233 usage();
234 }
235
236 if (ofn && !(out = fopen(ofn, "w"))) {
237 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
238 usage();
239 }
240
241 if (time(&t) == (time_t)(-1)) {
242 fprintf(stderr, "time call failed\n");
243 return EXIT_FAILURE;
244 }
245
246 ptm = localtime(&t);
247
248 if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
249 fprintf(stderr, "bad version string \"%s\"\n", version);
250 return EXIT_FAILURE;
251 }
252
253 memcpy(&hdr->magic, pattern, 4);
254 if (pbotflag)
255 memcpy(&hdr->res1, pbotpat, 4);
256 hdr->fwdate[0] = ptm->tm_year % 100;
257 hdr->fwdate[1] = ptm->tm_mon + 1;
258 hdr->fwdate[2] = ptm->tm_mday;
259 hdr->fwvern[0] = v0;
260 hdr->fwvern[1] = v1;
261 hdr->fwvern[2] = v2;
262 memcpy(&hdr->id, CODE_ID, strlen(CODE_ID));
263
264 off = sizeof(struct code_header);
265
266 fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
267 v0, v1, v2,
268 hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
269
270
271 while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
272 off = 0;
273 if (n < sizeof(buf)) {
274 if (ferror(in)) {
275 FREAD_ERROR:
276 fprintf(stderr, "fread error\n");
277 return EXIT_FAILURE;
278 }
279 if (gflag) {
280 gflag = sizeof(buf) - n;
281 memset(buf + n, 0xff, gflag);
282 fprintf(stderr, "adding %d bytes of garbage\n", gflag);
283 n = sizeof(buf);
284 }
285 }
286 if (!fwrite(buf, n, 1, out)) {
287 FWRITE_ERROR:
288 fprintf(stderr, "fwrite error\n");
289 return EXIT_FAILURE;
290 }
291 }
292
293 if (ferror(in)) {
294 goto FREAD_ERROR;
295 }
296
297 if (fflush(out)) {
298 goto FWRITE_ERROR;
299 }
300
301 fclose(in);
302 fclose(out);
303
304 return EXIT_SUCCESS;
305 }
This page took 0.060889 seconds and 5 git commands to generate.