build asterisk correctly with DEVELOPER=1 and not all config options enabled
[openwrt.git] / openwrt / target / 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 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <sys/stat.h>
48
49 /**********************************************************************/
50
51 #define CODE_ID "U2ND" /* from code_pattern.h */
52 #define CODE_PATTERN "W54S" /* from code_pattern.h */
53
54 #define CYBERTAN_VERSION "v3.37.2" /* from cyutils.h */
55
56 /* WRT54G v2.2 and WRT54GS v1.1 "flags" (from 3.37.32 firmware cyutils.h) */
57 #define SUPPORT_4712_CHIP 0x0001
58 #define SUPPORT_INTEL_FLASH 0x0002
59 #define SUPPORT_5325E_SWITCH 0x0004
60
61 struct code_header { /* from cyutils.h */
62 char magic[4];
63 char res1[4]; /* for extra magic */
64 char fwdate[3];
65 char fwvern[3];
66 char id[4]; /* U2ND */
67 char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
68 unsigned short flags; /* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
69 unsigned char res2[10];
70 } ;
71
72 /**********************************************************************/
73
74 void usage(void) __attribute__ (( __noreturn__ ));
75
76 void usage(void)
77 {
78 fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-p pattern] [-g] [-v v#.#.#] [-{0|1|2}]\n");
79 exit(EXIT_FAILURE);
80 }
81
82 int main(int argc, char **argv)
83 {
84 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
85 struct code_header *hdr;
86 FILE *in = stdin;
87 FILE *out = stdout;
88 char *ifn = NULL;
89 char *ofn = NULL;
90 char *pattern = CODE_PATTERN;
91 char *version = CYBERTAN_VERSION;
92 int gflag = 0;
93 int c;
94 int v0, v1, v2;
95 size_t off, n;
96 time_t t;
97 struct tm *ptm;
98
99 fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
100
101 hdr = (struct code_header *) buf;
102 memset(hdr, 0, sizeof(struct code_header));
103
104 while ((c = getopt(argc, argv, "i:o:p:gv:012")) != -1) {
105 switch (c) {
106 case 'i':
107 ifn = optarg;
108 break;
109 case 'o':
110 ofn = optarg;
111 break;
112 case 'p':
113 pattern = optarg;
114 break;
115 case 'g':
116 gflag = 1;
117 break;
118 case 'v': /* extension to allow setting version */
119 version = optarg;
120 break;
121 case '0':
122 hdr->hw_ver = 0;
123 break;
124 case '1':
125 hdr->hw_ver = 1;
126 break;
127 case '2': /* new 54G v2.2 and 54GS v1.1 flags */
128 hdr->hw_ver = 1;
129 hdr->flags |= SUPPORT_4712_CHIP;
130 hdr->flags |= SUPPORT_INTEL_FLASH;
131 hdr->flags |= SUPPORT_5325E_SWITCH;
132 break;
133
134 default:
135 usage();
136 }
137 }
138
139 if (optind != argc) {
140 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
141 usage();
142 }
143
144 if (strlen(pattern) != 4) {
145 fprintf(stderr, "illegal pattern \"%s\": length != 4\n", pattern);
146 usage();
147 }
148
149 if (ifn && !(in = fopen(ifn, "r"))) {
150 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
151 usage();
152 }
153
154 if (ofn && !(out = fopen(ofn, "w"))) {
155 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
156 usage();
157 }
158
159 if (time(&t) == (time_t)(-1)) {
160 fprintf(stderr, "time call failed\n");
161 return EXIT_FAILURE;
162 }
163
164 ptm = localtime(&t);
165
166 if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
167 fprintf(stderr, "bad version string \"%s\"\n", version);
168 return EXIT_FAILURE;
169 }
170
171 memcpy(&hdr->magic, pattern, 4);
172 hdr->fwdate[0] = ptm->tm_year % 100;
173 hdr->fwdate[1] = ptm->tm_mon + 1;
174 hdr->fwdate[2] = ptm->tm_mday;
175 hdr->fwvern[0] = v0;
176 hdr->fwvern[1] = v1;
177 hdr->fwvern[2] = v2;
178 memcpy(&hdr->id, CODE_ID, strlen(CODE_ID));
179
180 off = sizeof(struct code_header);
181
182 fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
183 v0, v1, v2,
184 hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
185
186
187 while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
188 off = 0;
189 if (n < sizeof(buf)) {
190 if (ferror(in)) {
191 FREAD_ERROR:
192 fprintf(stderr, "fread error\n");
193 return EXIT_FAILURE;
194 }
195 if (gflag) {
196 gflag = sizeof(buf) - n;
197 memset(buf + n, 0xff, gflag);
198 fprintf(stderr, "adding %d bytes of garbage\n", gflag);
199 n = sizeof(buf);
200 }
201 }
202 if (!fwrite(buf, n, 1, out)) {
203 FWRITE_ERROR:
204 fprintf(stderr, "fwrite error\n");
205 return EXIT_FAILURE;
206 }
207 }
208
209 if (ferror(in)) {
210 goto FREAD_ERROR;
211 }
212
213 if (fflush(out)) {
214 goto FWRITE_ERROR;
215 }
216
217 fclose(in);
218 fclose(out);
219
220 return EXIT_SUCCESS;
221 }
This page took 0.053694 seconds and 5 git commands to generate.