4 * Copyright (C) 2005-2006 Mike Baker,
5 * Imre Kaloz <kaloz@openwrt.org>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * Motorola's firmware flashing code requires an extra header.
29 * The header is eight bytes (see struct motorola below).
30 * This program will take a firmware file and create a new one
32 * motorola-bin --wr850g WR850G_V403.stripped.trx WR850G_V403.trx
34 * Note: Motorola's firmware is distributed with this header.
35 * If you need to flash Motorola firmware on a router running OpenWRT,
36 * you will to remove this header. Use the --strip flag:
37 * motorola-bin --strip WR850G_V403.trx WR850G_V403.stripped.trx
43 * Add support for for creating WA840G and WE800G images
54 #include <netinet/in.h>
57 #define BPB 8 /* bits/byte */
59 static uint32_t crc32
[1<<BPB
];
61 static void init_crc32()
63 const uint32_t poly
= ntohl(0x2083b8ed);
66 for (n
= 0; n
< 1<<BPB
; n
++) {
70 for (bit
= 0; bit
< BPB
; bit
++)
71 crc
= (crc
& 1) ? (poly
^ (crc
>> 1)) : (crc
>> 1);
76 static uint32_t crc32buf(unsigned char *buf
, size_t len
)
78 uint32_t crc
= 0xFFFFFFFF;
80 for (; len
; len
--, buf
++)
81 crc
= crc32
[(uint8_t)crc
^ *buf
] ^ (crc
>> BPB
);
86 uint32_t crc
; // crc32 of the remainder
87 uint32_t flags
; // unknown, 105770*
90 static const struct model
{
91 char digit
; /* a digit signifying model (historical) */
95 { '1', "WR850G", 0x10577050LU
},
96 { '2', "WA840G", 0x10577040LU
},
97 { '3', "WE800G", 0x10577000LU
},
101 static void usage(const char *) __attribute__ (( __noreturn__
));
103 static void usage(const char *mess
)
105 const struct model
*m
;
107 fprintf(stderr
, "Error: %s\n", mess
);
108 fprintf(stderr
, "Usage: motorola-bin -device|--strip infile outfile\n");
109 fprintf(stderr
, "Known devices: ");
111 for (m
= models
; m
->digit
!= '\0'; m
++)
112 fprintf(stderr
, " %c - %s", m
->digit
, m
->name
);
114 fprintf(stderr
, "\n");
118 int main(int argc
, char **argv
)
120 off_t len
; // of original firmware
122 void *trx
; // pointer to original firmware (mmmapped)
123 struct motorola
*firmware
; // pionter to prefix + copy of original firmware
129 usage("wrong number of arguments");
132 if ((fd
= open(argv
[2], O_RDONLY
)) < 0
133 || (len
= lseek(fd
, 0, SEEK_END
)) < 0
134 || (trx
= mmap(0, len
, PROT_READ
, MAP_SHARED
, fd
, 0)) == (void *) (-1)
137 fprintf(stderr
, "Error loading file %s: %s\n", argv
[2], strerror(errno
));
143 if (strcmp(argv
[1], "--strip") == 0)
145 const char *ugh
= NULL
;
147 if (len
< sizeof(struct motorola
)) {
148 ugh
= "input file too short";
150 const struct model
*m
;
153 if (htonl(crc32buf(trx
+ offsetof(struct motorola
, flags
), len
- offsetof(struct motorola
, flags
))) != firmware
->crc
)
155 for (m
= models
; ; m
++) {
156 if (m
->digit
== '\0') {
158 ugh
= "unrecognized flags field";
161 if (firmware
->flags
== htonl(m
->flags
)) {
162 fprintf(stderr
, "Firmware for Motorola %s\n", m
->name
);
169 fprintf(stderr
, "%s\n", ugh
);
172 // all is well, write the file without the prefix
173 if ((fd
= open(argv
[3], O_CREAT
|O_WRONLY
,0644)) < 0
174 || write(fd
, trx
+ sizeof(struct motorola
), len
- sizeof(struct motorola
)) != len
- sizeof(struct motorola
)
177 fprintf(stderr
, "Error storing file %s: %s\n", argv
[3], strerror(errno
));
183 // setup the firmware flags magic number
184 const struct model
*m
;
185 const char *df
= argv
[1];
188 usage("first argument must start with -");
190 ++df
; /* allow but don't require second - */
192 for (m
= models
; ; m
++) {
193 if (m
->digit
== '\0')
194 usage("unrecognized device specified");
195 if ((df
[0] == m
->digit
&& df
[1] == '\0') || strcasecmp(df
, m
->name
) == 0) {
202 // create a firmware image in memory
203 // and copy the trx to it
204 firmware
= malloc(sizeof(struct motorola
) + len
);
205 memcpy(&firmware
[1], trx
, len
);
207 // setup the motorola headers
208 firmware
->flags
= htonl(flags
);
210 // CRC of flags + firmware
211 firmware
->crc
= htonl(crc32buf((unsigned char *)&firmware
->flags
, sizeof(firmware
->flags
) + len
));
213 // write the firmware
214 if ((fd
= open(argv
[3], O_CREAT
|O_WRONLY
,0644)) < 0
215 || write(fd
, firmware
, sizeof(struct motorola
) + len
) != sizeof(struct motorola
) + len
218 fprintf(stderr
, "Error storing file %s: %s\n", argv
[3], strerror(errno
));
This page took 0.060095 seconds and 5 git commands to generate.