1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2005-2006 Mike Baker,
6 * Imre Kaloz <kaloz@openwrt.org>
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
16 * motorola-bin --wr850g WR850G_V403.stripped.trx WR850G_V403.trx
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
27 * Add support for for creating WA840G and WE800G images
38 #include <netinet/in.h>
41 #define BPB 8 /* bits/byte */
43 static uint32_t crc32
[1<<BPB
];
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 uint32_t crc
; // crc32 of the remainder
71 uint32_t flags
; // unknown, 105770*
74 static const struct model
{
75 char digit
; /* a digit signifying model (historical) */
79 { '1', "WR850G", 0x10577050LU
},
80 { '2', "WA840G", 0x10577040LU
},
81 { '3', "WE800G", 0x10577000LU
},
85 static void usage(const char *) __attribute__ (( __noreturn__
));
87 static void usage(const char *mess
)
89 const struct model
*m
;
91 fprintf(stderr
, "Error: %s\n", mess
);
92 fprintf(stderr
, "Usage: motorola-bin -device|--strip infile outfile\n");
93 fprintf(stderr
, "Known devices: ");
95 for (m
= models
; m
->digit
!= '\0'; m
++)
96 fprintf(stderr
, " %c - %s", m
->digit
, m
->name
);
98 fprintf(stderr
, "\n");
102 int main(int argc
, char **argv
)
104 off_t len
; // of original firmware
106 void *trx
; // pointer to original firmware (mmmapped)
107 struct motorola
*firmware
; // pionter to prefix + copy of original firmware
113 usage("wrong number of arguments");
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)
121 fprintf(stderr
, "Error loading file %s: %s\n", argv
[2], strerror(errno
));
127 if (strcmp(argv
[1], "--strip") == 0)
129 const char *ugh
= NULL
;
131 if (len
< sizeof(struct motorola
)) {
132 ugh
= "input file too short";
134 const struct model
*m
;
137 if (htonl(crc32buf(trx
+ offsetof(struct motorola
, flags
), len
- offsetof(struct motorola
, flags
))) != firmware
->crc
)
139 for (m
= models
; ; m
++) {
140 if (m
->digit
== '\0') {
142 ugh
= "unrecognized flags field";
145 if (firmware
->flags
== htonl(m
->flags
)) {
146 fprintf(stderr
, "Firmware for Motorola %s\n", m
->name
);
153 fprintf(stderr
, "%s\n", ugh
);
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
)
161 fprintf(stderr
, "Error storing file %s: %s\n", argv
[3], strerror(errno
));
167 // setup the firmware flags magic number
168 const struct model
*m
;
169 const char *df
= argv
[1];
172 usage("first argument must start with -");
174 ++df
; /* allow but don't require second - */
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) {
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
);
191 // setup the motorola headers
192 firmware
->flags
= htonl(flags
);
194 // CRC of flags + firmware
195 firmware
->crc
= htonl(crc32buf((unsigned char *)&firmware
->flags
, sizeof(firmware
->flags
) + len
));
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
202 fprintf(stderr
, "Error storing file %s: %s\n", argv
[3], strerror(errno
));