source: clfs-embedded/software/addpattern/addpattern.c@ 5058de7

Last change on this file since 5058de7 was 24e0008, checked in by Jim Gifford <clfs@…>, 18 years ago

Moved Software to svn

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