consider virtual dependencies (generated by using PROVIDES:=) when generating the...
[openwrt.git] / tools / firmware-utils / src / motorola-bin.c
1 /*
2 * motorola-bin.c
3 *
4 * Copyright (C) 2005-2006 Mike Baker,
5 * Imre Kaloz <kaloz@openwrt.org>
6 * D. Hugh Redelmeier
7 * OpenWrt.org
8 *
9 * $Id$
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27 /*
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
31 * with this header:
32 * motorola-bin --wr850g WR850G_V403.stripped.trx WR850G_V403.trx
33 *
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
38 */
39
40 /*
41 * February 1, 2006
42 *
43 * Add support for for creating WA840G and WE800G images
44 */
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stddef.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <sys/mman.h>
53 #include <string.h>
54 #include <netinet/in.h>
55 #include <inttypes.h>
56
57 #define BPB 8 /* bits/byte */
58
59 static uint32_t crc32[1<<BPB];
60
61 static void init_crc32()
62 {
63 const uint32_t poly = ntohl(0x2083b8ed);
64 int n;
65
66 for (n = 0; n < 1<<BPB; n++) {
67 uint32_t crc = n;
68 int bit;
69
70 for (bit = 0; bit < BPB; bit++)
71 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
72 crc32[n] = crc;
73 }
74 }
75
76 static uint32_t crc32buf(unsigned char *buf, size_t len)
77 {
78 uint32_t crc = 0xFFFFFFFF;
79
80 for (; len; len--, buf++)
81 crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
82 return crc;
83 }
84
85 struct motorola {
86 uint32_t crc; // crc32 of the remainder
87 uint32_t flags; // unknown, 105770*
88 };
89
90 static const struct model {
91 char digit; /* a digit signifying model (historical) */
92 const char *name;
93 uint32_t flags;
94 } models[] = {
95 { '1', "WR850G", 0x10577050LU },
96 { '2', "WA840G", 0x10577040LU },
97 { '3', "WE800G", 0x10577000LU },
98 { '\0', NULL, 0 }
99 };
100
101 static void usage(const char *) __attribute__ (( __noreturn__ ));
102
103 static void usage(const char *mess)
104 {
105 const struct model *m;
106
107 fprintf(stderr, "Error: %s\n", mess);
108 fprintf(stderr, "Usage: motorola-bin -device|--strip infile outfile\n");
109 fprintf(stderr, "Known devices: ");
110
111 for (m = models; m->digit != '\0'; m++)
112 fprintf(stderr, " %c - %s", m->digit, m->name);
113
114 fprintf(stderr, "\n");
115 exit(1);
116 }
117
118 int main(int argc, char **argv)
119 {
120 off_t len; // of original firmware
121 int fd;
122 void *trx; // pointer to original firmware (mmmapped)
123 struct motorola *firmware; // pionter to prefix + copy of original firmware
124 uint32_t flags;
125
126 // verify parameters
127
128 if (argc != 4)
129 usage("wrong number of arguments");
130
131 // mmap trx file
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)
135 || close(fd) < 0)
136 {
137 fprintf(stderr, "Error loading file %s: %s\n", argv[2], strerror(errno));
138 exit(1);
139 }
140
141 init_crc32();
142
143 if (strcmp(argv[1], "--strip") == 0)
144 {
145 const char *ugh = NULL;
146
147 if (len < sizeof(struct motorola)) {
148 ugh = "input file too short";
149 } else {
150 const struct model *m;
151
152 firmware = trx;
153 if (htonl(crc32buf(trx + offsetof(struct motorola, flags), len - offsetof(struct motorola, flags))) != firmware->crc)
154 ugh = "Invalid CRC";
155 for (m = models; ; m++) {
156 if (m->digit == '\0') {
157 if (ugh == NULL)
158 ugh = "unrecognized flags field";
159 break;
160 }
161 if (firmware->flags == htonl(m->flags)) {
162 fprintf(stderr, "Firmware for Motorola %s\n", m->name);
163 break;
164 }
165 }
166 }
167
168 if (ugh != NULL) {
169 fprintf(stderr, "%s\n", ugh);
170 exit(3);
171 } else {
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)
175 || close(fd) < 0)
176 {
177 fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
178 exit(2);
179 }
180 }
181
182 } else {
183 // setup the firmware flags magic number
184 const struct model *m;
185 const char *df = argv[1];
186
187 if (*df != '-')
188 usage("first argument must start with -");
189 if (*++df == '-')
190 ++df; /* allow but don't require second - */
191
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) {
196 flags = m->flags;
197 break;
198 }
199 }
200
201
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);
206
207 // setup the motorola headers
208 firmware->flags = htonl(flags);
209
210 // CRC of flags + firmware
211 firmware->crc = htonl(crc32buf((unsigned char *)&firmware->flags, sizeof(firmware->flags) + len));
212
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
216 || close(fd) < 0)
217 {
218 fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
219 exit(2);
220 }
221
222 free(firmware);
223 }
224
225 munmap(trx,len);
226
227 return 0;
228 }
This page took 0.05012 seconds and 5 git commands to generate.