4 * Copyright (C) 2005-2006 Mike Baker,
5 * Imre Kaloz <kaloz@openwrt.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 * Add support for for creating WA840G and WE800G images
38 #include <netinet/in.h>
45 unsigned long poly = ntohl(0x2083b8ed);
47 if ((crc32 = (unsigned long *) malloc(256 * sizeof(unsigned long))) == (void *)-1) {
51 for (n = 0; n < 256; n++) {
52 crc = (unsigned long) n;
53 for (bit = 0; bit < 8; bit++)
54 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
59 unsigned int crc32buf(char *buf, size_t len)
61 unsigned int crc = 0xFFFFFFFF;
62 for (; len; len--, buf++)
63 crc = crc32[(crc ^ *buf) & 0xff] ^ (crc >> 8);
68 unsigned int crc; // crc32 of the remainder
69 unsigned int flags; // unknown, 105770*
70 char *trx; // standard trx
73 void usage(void) __attribute__ (( __noreturn__ ));
77 printf("Usage: motorola-bin [-device] [trxfile] [binfile]\n\n");
78 printf("Known devices: 1 - WR850G | 2 - WA840G | 3 - WE800G\n");
82 int main(int argc, char **argv)
88 struct motorola *firmware;
98 if (((fd = open(argv[2], O_RDONLY)) < 0)
99 || ((len = lseek(fd, 0, SEEK_END)) < 0)
100 || ((trx = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1))
101 || (close(fd) < 0)) {
102 perror("open/malloc");
106 // create a firmware image in memory
107 // and copy the trx to it
108 firmware = malloc(len+8);
109 memcpy(&firmware->trx,trx,len);
112 // setup the motorola headers
115 // setup the firmware magic
117 while ((c = getopt(argc, argv, "123")) !=-1) {
120 firmware->flags = ntohl(0x10577050); // Motorola WR850G
123 firmware->flags = ntohl(0x10577040); // Motorola WA840G
126 firmware->flags = ntohl(0x10577000); // Motorola WE800G
133 firmware->crc = htonl(crc32buf((char *)&firmware->flags,len+4));
135 // write the firmware
136 if (((fd = open(argv[3], O_CREAT|O_WRONLY,0644)) < 0)
137 || (write(fd,firmware,len+8) != len+8)
138 || (close(fd) < 0)) {