new more flexible firewall
[openwrt.git] / openwrt / target / utils / src / motorola-bin.c
1 /*
2 * motorola-bin.c
3 *
4 * Copyright (C) 2005 Mike Baker
5 * Openwrt.org
6 *
7 * $Id$
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/mman.h>
30 #include <string.h>
31 #include <netinet/in.h>
32
33 unsigned long *crc32;
34
35 void init_crc32()
36 {
37 unsigned long crc;
38 unsigned long poly = ntohl(0x2083b8ed);
39 int n, bit;
40 if ((crc32 = (unsigned long *) malloc(256 * sizeof(unsigned long))) == (void *)-1) {
41 perror("malloc");
42 exit(1);
43 }
44 for (n = 0; n < 256; n++) {
45 crc = (unsigned long) n;
46 for (bit = 0; bit < 8; bit++)
47 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
48 crc32[n] = crc;
49 }
50 }
51
52 unsigned int crc32buf(char *buf, size_t len)
53 {
54 unsigned int crc = 0xFFFFFFFF;
55 for (; len; len--, buf++)
56 crc = crc32[(crc ^ *buf) & 0xff] ^ (crc >> 8);
57 return crc;
58 }
59
60 struct motorola {
61 unsigned int crc; // crc32 of the remainder
62 unsigned int flags; // unknown, 10577050
63 char *trx; // standard trx
64 };
65
66 int main(int argc, char **argv)
67 {
68 unsigned int len;
69 int fd;
70 void *trx;
71 struct motorola *firmware;
72
73 if (argc<3) {
74 printf("%s <trx> <motorola.bin>\n",argv[0]);
75 exit(0);
76 }
77
78 // mmap trx file
79 if (((fd = open(argv[1], O_RDONLY)) < 0)
80 || ((len = lseek(fd, 0, SEEK_END)) < 0)
81 || ((trx = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1))
82 || (close(fd) < 0)) {
83 perror("open/malloc");
84 exit(-1);
85 }
86
87 // create a firmware image in memory
88 // and copy the trx to it
89 firmware = malloc(len+8);
90 memcpy(&firmware->trx,trx,len);
91 munmap(trx,len);
92
93 // setup the motorola headers
94 init_crc32();
95 firmware->flags = ntohl(0x10577050);
96 firmware->crc = htonl(crc32buf((char *)&firmware->flags,len+4));
97
98 // write the firmware
99 if (((fd = open(argv[2], O_CREAT|O_WRONLY,0644)) < 0)
100 || (write(fd,firmware,len+8) != len+8)
101 || (close(fd) < 0)) {
102 perror("write");
103 exit(-1);
104 }
105
106 free(firmware);
107
108 return 0;
109 }
This page took 0.043407 seconds and 5 git commands to generate.