2 * wndr3700.c - partially based on OpenWrt's add_header.c
4 * Copyright (C) 2009 Anael Orlinski <naouel@naouel.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License,
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * The add_header utility used by various vendors preprends the buf
22 * image with a header containing a CRC32 value which is generated for the
23 * model id + reserved space for CRC32 + buf, then replaces the reserved
24 * area with the actual CRC32. This replacement tool mimics this behavior.
35 #include <netinet/in.h>
38 #define BPB 8 /* bits/byte */
40 #define WNDR3700_MAGIC_LEN 4
42 static uint32_t crc32
[1<<BPB
];
43 static char *magic
= "3700";
45 static void init_crc32()
47 const uint32_t poly
= ntohl(0x2083b8ed);
50 for (n
= 0; n
< 1<<BPB
; n
++) {
54 for (bit
= 0; bit
< BPB
; bit
++)
55 crc
= (crc
& 1) ? (poly
^ (crc
>> 1)) : (crc
>> 1);
60 static uint32_t crc32buf(unsigned char *buf
, size_t len
)
62 uint32_t crc
= 0xFFFFFFFF;
64 for (; len
; len
--, buf
++)
65 crc
= crc32
[(uint8_t)crc
^ *buf
] ^ (crc
>> BPB
);
70 unsigned char magic
[WNDR3700_MAGIC_LEN
];
72 unsigned char stuff
[56];
75 static void usage(const char *) __attribute__ (( __noreturn__
));
77 static void usage(const char *mess
)
79 fprintf(stderr
, "Error: %s\n", mess
);
80 fprintf(stderr
, "Usage: wndr3700 input_file output_file [magic]\n");
81 fprintf(stderr
, "\n");
85 int main(int argc
, char **argv
)
87 off_t len
; // of original buf
88 off_t buflen
; // of the output file
90 void *input_file
; // pointer to the input file (mmmapped)
92 unsigned char *buf
; // pointer to prefix + copy of original buf
97 usage("wrong number of arguments");
102 if (strlen(magic
) != WNDR3700_MAGIC_LEN
) {
103 fprintf(stderr
, "Invalid magic: '%s'\n", magic
);
108 if ((fd
= open(argv
[1], O_RDONLY
)) < 0
109 || (len
= lseek(fd
, 0, SEEK_END
)) < 0
110 || (input_file
= mmap(0, len
, PROT_READ
, MAP_SHARED
, fd
, 0)) == (void *) (-1)
113 fprintf(stderr
, "Error loading file %s: %s\n", argv
[1], strerror(errno
));
122 memcpy(&header
, input_file
, sizeof(header
));
124 memcpy(header
.magic
, magic
, WNDR3700_MAGIC_LEN
);
127 // create a firmware image in memory and copy the input_file to it
128 buf
= malloc(buflen
);
129 memcpy(buf
, input_file
, len
);
131 // CRC of temporary header
132 header
.crc
= htonl(crc32buf((unsigned char*)&header
, sizeof(header
)));
134 memcpy(buf
, &header
, sizeof(header
));
137 if ((fd
= open(argv
[2], O_CREAT
|O_WRONLY
|O_TRUNC
,0644)) < 0
138 || write(fd
, buf
, buflen
) != buflen
141 fprintf(stderr
, "Error storing file %s: %s\n", argv
[2], strerror(errno
));
147 munmap(input_file
,len
);
This page took 0.050014 seconds and 5 git commands to generate.