2 * Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
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.
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.
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
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
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.
38 /**********************************************************************/
40 #define CODE_ID "U2ND" /* from code_pattern.h */
41 #define CODE_PATTERN "W54S" /* from code_pattern.h */
43 #define CYBERTAN_VERSION "v2.07.1" /* from cyutils.h */
44 /* #define CYBERTAN_VERSION "v2.04.3" */
46 struct code_header { /* from cyutils.h */
48 char res1[4]; /* for extra magic */
51 char id[4]; /* U2ND */
53 unsigned char res2[14];
55 char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
56 unsigned char res2[13];
60 /**********************************************************************/
62 void usage(void) __attribute__ (( __noreturn__ ));
66 fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-p pattern] [-g] [-v v#.#.#] [-{0|1}]\n");
70 int main(int argc, char **argv)
72 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
73 struct code_header *hdr;
78 char *pattern = CODE_PATTERN;
79 char *version = CYBERTAN_VERSION;
87 fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
89 hdr = (struct code_header *) buf;
91 while ((c = getopt(argc, argv, "i:o:p:gv:01")) != -1) {
105 case 'v': /* extension to allow setting version */
119 if (optind != argc) {
120 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
124 if (strlen(pattern) != 4) {
125 fprintf(stderr, "illegal pattern \"%s\": length != 4\n", pattern);
129 if (ifn && !(in = fopen(ifn, "r"))) {
130 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
134 if (ofn && !(out = fopen(ofn, "w"))) {
135 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
139 if (time(&t) == (time_t)(-1)) {
140 fprintf(stderr, "time call failed\n");
146 if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
147 fprintf(stderr, "bad version string \"%s\"\n", version);
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;
159 memcpy(&hdr->id, CODE_ID, strlen(CODE_ID));
161 off = sizeof(struct code_header);
163 fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
165 hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
168 while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
170 if (n < sizeof(buf)) {
173 fprintf(stderr, "fread error\n");
177 gflag = sizeof(buf) - n;
178 memset(buf + n, 0xff, gflag);
179 fprintf(stderr, "adding %d bytes of garbage\n", gflag);
183 if (!fwrite(buf, n, 1, out)) {
185 fprintf(stderr, "fwrite error\n");