source: branches/backfire/tools/firmware-utils/src/wndr3700.c @ 26111

Last change on this file since 26111 was 26111, checked in by juhosg, 5 years ago

backfire: firmware-utils/wndr3700: allow to specify image magic via command line (backport of r24980)

File size: 3.6 KB
Line 
1/*
2 * wndr3700.c - partially based on OpenWrt's add_header.c
3 *
4 * Copyright (C) 2009 Anael Orlinski  <naouel@naouel.org>
5 *
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.
9 *
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.
14 *
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.
18 */
19
20/*
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.
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <stddef.h>
30#include <unistd.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <sys/mman.h>
34#include <string.h>
35#include <netinet/in.h>
36#include <inttypes.h>
37
38#define BPB 8 /* bits/byte */
39
40#define WNDR3700_MAGIC_LEN      4
41
42static uint32_t crc32[1<<BPB];
43static char *magic = "3700";
44
45static void init_crc32()
46{
47        const uint32_t poly = ntohl(0x2083b8ed);
48        int n;
49
50        for (n = 0; n < 1<<BPB; n++) {
51                uint32_t crc = n;
52                int bit;
53
54                for (bit = 0; bit < BPB; bit++)
55                        crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
56                crc32[n] = crc;
57        }
58}
59
60static uint32_t crc32buf(unsigned char *buf, size_t len)
61{
62        uint32_t crc = 0xFFFFFFFF;
63
64        for (; len; len--, buf++)
65                crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
66        return ~crc;
67}
68
69struct header {
70    unsigned char magic[WNDR3700_MAGIC_LEN];
71    uint32_t crc;
72    unsigned char stuff[56];
73};
74
75static void usage(const char *) __attribute__ (( __noreturn__ ));
76
77static void usage(const char *mess)
78{
79        fprintf(stderr, "Error: %s\n", mess);
80        fprintf(stderr, "Usage: wndr3700 input_file output_file [magic]\n");
81        fprintf(stderr, "\n");
82        exit(1);
83}
84
85int main(int argc, char **argv)
86{
87        off_t len;                      // of original buf
88        off_t buflen;                   // of the output file
89        int fd;
90        void *input_file;               // pointer to the input file (mmmapped)
91        struct header header;
92        unsigned char *buf;     // pointer to prefix + copy of original buf
93
94        // verify parameters
95
96        if (argc < 3)
97                usage("wrong number of arguments");
98
99        if (argc > 3)
100                magic = argv[3];
101
102        if (strlen(magic) != WNDR3700_MAGIC_LEN) {
103                fprintf(stderr, "Invalid magic: '%s'\n", magic);
104                exit(1);
105        }
106
107        // mmap input_file
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)
111        || close(fd) < 0)
112        {
113                fprintf(stderr, "Error loading file %s: %s\n", argv[1], strerror(errno));
114                exit(1);
115        }
116
117        buflen = len;
118
119        init_crc32();
120
121        // preload header
122        memcpy(&header, input_file, sizeof(header));
123
124        memcpy(header.magic, magic, WNDR3700_MAGIC_LEN);
125        header.crc = 0;
126
127        // create a firmware image in memory and copy the input_file to it
128        buf = malloc(buflen);
129        memcpy(buf, input_file, len);
130
131        // CRC of temporary header
132        header.crc = htonl(crc32buf((unsigned char*)&header, sizeof(header)));
133
134        memcpy(buf, &header, sizeof(header));
135
136        // write the buf
137        if ((fd = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0
138        || write(fd, buf, buflen) != buflen
139        || close(fd) < 0)
140        {
141                fprintf(stderr, "Error storing file %s: %s\n", argv[2], strerror(errno));
142                exit(2);
143        }
144
145        free(buf);
146
147        munmap(input_file,len);
148
149        return 0;
150}
Note: See TracBrowser for help on using the repository browser.