firmware-utils: replace GPL 2.0+ boilerplate/reference with SPDX
[openwrt/openwrt.git] / tools / firmware-utils / src / motorola-bin.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * motorola-bin.c
4 *
5 * Copyright (C) 2005-2006 Mike Baker,
6 * Imre Kaloz <kaloz@openwrt.org>
7 * D. Hugh Redelmeier
8 * OpenWrt.org
9 */
10
11 /*
12 * Motorola's firmware flashing code requires an extra header.
13 * The header is eight bytes (see struct motorola below).
14 * This program will take a firmware file and create a new one
15 * with this header:
16 * motorola-bin --wr850g WR850G_V403.stripped.trx WR850G_V403.trx
17 *
18 * Note: Motorola's firmware is distributed with this header.
19 * If you need to flash Motorola firmware on a router running OpenWRT,
20 * you will to remove this header. Use the --strip flag:
21 * motorola-bin --strip WR850G_V403.trx WR850G_V403.stripped.trx
22 */
23
24 /*
25 * February 1, 2006
26 *
27 * Add support for for creating WA840G and WE800G images
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stddef.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <sys/mman.h>
37 #include <string.h>
38 #include <netinet/in.h>
39 #include <inttypes.h>
40
41 #define BPB 8 /* bits/byte */
42
43 static uint32_t crc32[1<<BPB];
44
45 static 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
60 static 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
69 struct motorola {
70 uint32_t crc; // crc32 of the remainder
71 uint32_t flags; // unknown, 105770*
72 };
73
74 static const struct model {
75 char digit; /* a digit signifying model (historical) */
76 const char *name;
77 uint32_t flags;
78 } models[] = {
79 { '1', "WR850G", 0x10577050LU },
80 { '2', "WA840G", 0x10577040LU },
81 { '3', "WE800G", 0x10577000LU },
82 { '\0', NULL, 0 }
83 };
84
85 static void usage(const char *) __attribute__ (( __noreturn__ ));
86
87 static void usage(const char *mess)
88 {
89 const struct model *m;
90
91 fprintf(stderr, "Error: %s\n", mess);
92 fprintf(stderr, "Usage: motorola-bin -device|--strip infile outfile\n");
93 fprintf(stderr, "Known devices: ");
94
95 for (m = models; m->digit != '\0'; m++)
96 fprintf(stderr, " %c - %s", m->digit, m->name);
97
98 fprintf(stderr, "\n");
99 exit(1);
100 }
101
102 int main(int argc, char **argv)
103 {
104 off_t len; // of original firmware
105 int fd;
106 void *trx; // pointer to original firmware (mmmapped)
107 struct motorola *firmware; // pionter to prefix + copy of original firmware
108 uint32_t flags;
109
110 // verify parameters
111
112 if (argc != 4)
113 usage("wrong number of arguments");
114
115 // mmap trx file
116 if ((fd = open(argv[2], O_RDONLY)) < 0
117 || (len = lseek(fd, 0, SEEK_END)) < 0
118 || (trx = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
119 || close(fd) < 0)
120 {
121 fprintf(stderr, "Error loading file %s: %s\n", argv[2], strerror(errno));
122 exit(1);
123 }
124
125 init_crc32();
126
127 if (strcmp(argv[1], "--strip") == 0)
128 {
129 const char *ugh = NULL;
130
131 if (len < sizeof(struct motorola)) {
132 ugh = "input file too short";
133 } else {
134 const struct model *m;
135
136 firmware = trx;
137 if (htonl(crc32buf(trx + offsetof(struct motorola, flags), len - offsetof(struct motorola, flags))) != firmware->crc)
138 ugh = "Invalid CRC";
139 for (m = models; ; m++) {
140 if (m->digit == '\0') {
141 if (ugh == NULL)
142 ugh = "unrecognized flags field";
143 break;
144 }
145 if (firmware->flags == htonl(m->flags)) {
146 fprintf(stderr, "Firmware for Motorola %s\n", m->name);
147 break;
148 }
149 }
150 }
151
152 if (ugh != NULL) {
153 fprintf(stderr, "%s\n", ugh);
154 exit(3);
155 } else {
156 // all is well, write the file without the prefix
157 if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0
158 || write(fd, trx + sizeof(struct motorola), len - sizeof(struct motorola)) != len - sizeof(struct motorola)
159 || close(fd) < 0)
160 {
161 fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
162 exit(2);
163 }
164 }
165
166 } else {
167 // setup the firmware flags magic number
168 const struct model *m;
169 const char *df = argv[1];
170
171 if (*df != '-')
172 usage("first argument must start with -");
173 if (*++df == '-')
174 ++df; /* allow but don't require second - */
175
176 for (m = models; ; m++) {
177 if (m->digit == '\0')
178 usage("unrecognized device specified");
179 if ((df[0] == m->digit && df[1] == '\0') || strcasecmp(df, m->name) == 0) {
180 flags = m->flags;
181 break;
182 }
183 }
184
185
186 // create a firmware image in memory
187 // and copy the trx to it
188 firmware = malloc(sizeof(struct motorola) + len);
189 memcpy(&firmware[1], trx, len);
190
191 // setup the motorola headers
192 firmware->flags = htonl(flags);
193
194 // CRC of flags + firmware
195 firmware->crc = htonl(crc32buf((unsigned char *)&firmware->flags, sizeof(firmware->flags) + len));
196
197 // write the firmware
198 if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0
199 || write(fd, firmware, sizeof(struct motorola) + len) != sizeof(struct motorola) + len
200 || close(fd) < 0)
201 {
202 fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
203 exit(2);
204 }
205
206 free(firmware);
207 }
208
209 munmap(trx,len);
210
211 return 0;
212 }