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