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.
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.
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
44 * Modified by juan.i.gonzalez at subdown dot net
45 * Support added for the AG241v2 and similar
48 * -r #.# adds revision hardware flags. AG241v2 and similar.
50 * AG241V2 firmware sets the hw_ver to 0x44.
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
65 /**********************************************************************/
67 #define CODE_ID "U2ND" /* from code_pattern.h */
68 #define CODE_PATTERN "W54S" /* from code_pattern.h */
69 #define PBOT_PATTERN "PBOT"
71 #define CYBERTAN_VERSION "v3.37.2" /* from cyutils.h */
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
78 struct code_header { /* from cyutils.h */
80 char res1[4]; /* for extra magic */
83 char id[4]; /* U2ND */
84 char hw_ver; /* 0: for 4702, 1: for 4712 -- new in 2.04.3 */
86 unsigned char flags[2]; /* SUPPORT_ flags new for 3.37.2 (WRT54G v2.2 and WRT54GS v1.1) */
87 unsigned char res2[10];
90 /**********************************************************************/
92 void usage(void) __attribute__ (( __noreturn__ ));
96 fprintf(stderr, "Usage: addpattern [-i trxfile] [-o binfile] [-p pattern] [-g] [-b] [-v v#.#.#] [-r #.#] [-{0|1|2|4}] -h\n");
100 int main(int argc, char **argv)
102 char buf[1024]; /* keep this at 1k or adjust garbage calc below */
103 struct code_header *hdr;
108 char *pattern = CODE_PATTERN;
109 char *pbotpat = PBOT_PATTERN;
110 char *version = CYBERTAN_VERSION;
119 fprintf(stderr, "mjn3's addpattern replacement - v0.81\n");
121 hdr = (struct code_header *) buf;
122 memset(hdr, 0, sizeof(struct code_header));
124 while ((c = getopt(argc, argv, "i:o:p:gbv:0124hr:")) != -1) {
141 case 'v': /* extension to allow setting version */
150 case '2': /* new 54G v2.2 and 54GS v1.1 flags */
152 hdr->flags[0] |= SUPPORT_4712_CHIP;
153 hdr->flags[0] |= SUPPORT_INTEL_FLASH;
154 hdr->flags[0] |= SUPPORT_5325E_SWITCH;
157 /* V4 firmware sets the flags to 0x1f */
159 hdr->flags[0] = 0x1f;
162 hdr->hw_ver = (char)(atof(optarg)*10)+0x30;
171 if (optind != argc || optind == 1) {
172 fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
176 if (strlen(pattern) != 4) {
177 fprintf(stderr, "illegal pattern \"%s\": length != 4\n", pattern);
181 if (ifn && !(in = fopen(ifn, "r"))) {
182 fprintf(stderr, "can not open \"%s\" for reading\n", ifn);
186 if (ofn && !(out = fopen(ofn, "w"))) {
187 fprintf(stderr, "can not open \"%s\" for writing\n", ofn);
191 if (time(&t) == (time_t)(-1)) {
192 fprintf(stderr, "time call failed\n");
198 if (3 != sscanf(version, "v%d.%d.%d", &v0, &v1, &v2)) {
199 fprintf(stderr, "bad version string \"%s\"\n", version);
203 memcpy(&hdr->magic, pattern, 4);
205 memcpy(&hdr->res1, pbotpat, 4);
206 hdr->fwdate[0] = ptm->tm_year % 100;
207 hdr->fwdate[1] = ptm->tm_mon + 1;
208 hdr->fwdate[2] = ptm->tm_mday;
212 memcpy(&hdr->id, CODE_ID, strlen(CODE_ID));
214 off = sizeof(struct code_header);
216 fprintf(stderr, "writing firmware v%d.%d.%d on %d/%d/%d (y/m/d)\n",
218 hdr->fwdate[0], hdr->fwdate[1], hdr->fwdate[2]);
221 while ((n = fread(buf + off, 1, sizeof(buf)-off, in) + off) > 0) {
223 if (n < sizeof(buf)) {
226 fprintf(stderr, "fread error\n");
230 gflag = sizeof(buf) - n;
231 memset(buf + n, 0xff, gflag);
232 fprintf(stderr, "adding %d bytes of garbage\n", gflag);
236 if (!fwrite(buf, n, 1, out)) {
238 fprintf(stderr, "fwrite error\n");