-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfwsplit.c
298 lines (246 loc) · 6.85 KB
/
fwsplit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 2007 Ubiquiti Networks, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <zlib.h>
#include <limits.h>
#include <unistd.h>
#include "fw.h"
static int debug = 0;
static char prefix[PATH_MAX];
#define MAX_PARTS 8
typedef struct fw_part {
part_t* header;
unsigned char* data;
u_int32_t data_size;
part_crc_t* signature;
} fw_part_t;
typedef struct fw {
u_int32_t size;
char version[256];
fw_part_t parts[MAX_PARTS];
int part_count;
} fw_t;
static int
fw_check_header(const header_t* h) {
u_int32_t crc;
int len = sizeof(header_t) - 2 * sizeof(u_int32_t);
crc = crc32(0L, (unsigned char*)h, len);
DEBUG("Calculated CRC: 0x%08X, expected: 0x%08X\n", htonl(crc), h->crc);
if (htonl(crc) != h->crc)
return -1;
return 0;
}
static int
fw_parse(const unsigned char* base, unsigned long size, fw_t* fw) {
const header_t* h = (const header_t*)base;
part_t* p;
signature_t* sig;
u_int32_t crc;
int i = 0;
if (fw == NULL)
return -1;
if (fw_check_header(h)) {
return -2;
}
memset(fw, 0, sizeof(fw_t));
fw->size = size;
memcpy(fw->version, h->version, sizeof(fw->version));
INFO("Firmware version: '%s'\n", fw->version);
p = (part_t*)(base + sizeof(header_t));
for (i = 0; strncmp(p->magic, MAGIC_END, MAGIC_LENGTH) != 0 && i < MAX_PARTS; i++) {
DEBUG("Partition (%c%c%c%c): %s [%u]\n",
p->magic[0], p->magic[1], p->magic[2], p->magic[3],
p->name, ntohl(p->index));
DEBUG(" Partition size: 0x%X\n", ntohl(p->part_size));
DEBUG(" Data size: %u\n", ntohl(p->data_size));
fw_part_t* fwp = &fw->parts[i];
fwp->header = p;
fwp->data = (unsigned char*)p + sizeof(part_t);
fwp->data_size = ntohl(p->data_size);
fwp->signature =
(part_crc_t*)(fwp->data + fwp->data_size);
crc = htonl(crc32(0L, (unsigned char*)p,
fwp->data_size + sizeof(part_t)));
if (crc != fwp->signature->crc) {
WARN("Invalid '%s' CRC (claims: %u, but is %u)\n",
fwp->header->name, fwp->signature->crc, crc);
}
p = (part_t*)((unsigned char*)p + sizeof(part_t) +
ntohl(p->data_size) + sizeof(part_crc_t));
/* check bounds */
if (((unsigned char*)p - base) >= size) {
return -3;
}
}
fw->part_count = i;
sig = (signature_t*)p;
if (strncmp(sig->magic, MAGIC_END, MAGIC_LENGTH) != 0) {
ERROR("Bad firmware signature\n");
return -4;
}
crc = htonl(crc32(0L, base, (unsigned char*)sig - base));
if (crc != sig->crc) {
WARN("Invalid signature CRC (claims: %u, but is %u)\n",
sig->crc, crc);
}
return 0;
}
static int
fw_split(const fw_t* fw, const char* prefix) {
int i;
const fw_part_t* fwp;
FILE* f;
char filename[PATH_MAX];
snprintf(filename, sizeof(filename), "%s.txt", prefix);
INFO("Creating descriptor file:\n\t%s\n", filename);
/* write descriptor file */
f = fopen(filename, "w");
if (f == NULL) {
ERROR("Couldn't open file '%s' for writing!\n", filename);
return -1;
}
for (i = 0; i < fw->part_count; ++i) {
fwp = &fw->parts[i];
fprintf(f, "%s\t\t0x%02X\t0x%08X\t0x%08X\t0x%08X\t0x%08X\t%s.%s\n",
fwp->header->name,
ntohl(fwp->header->index),
ntohl(fwp->header->baseaddr),
ntohl(fwp->header->part_size),
ntohl(fwp->header->memaddr),
ntohl(fwp->header->entryaddr),
prefix, fwp->header->name);
}
fclose(f);
INFO("Creating partition data files: \n");
for (i = 0; i < fw->part_count; ++i) {
fwp = &fw->parts[i];
snprintf(filename, sizeof(filename), "%s.%s",
prefix, fwp->header->name);
f = fopen(filename, "w");
if (f == NULL) {
ERROR("Failed opening file '%s' for writing: %s\n",
filename, strerror(errno));
continue;
}
INFO("\t%s\n", filename);
if (fwrite(fwp->data, fwp->data_size, 1, f) != 1) {
ERROR("Failed writing to file '%s': %s\n",
filename, strerror(errno));
fclose(f);
continue;
}
fclose(f);
}
return 0;
}
static void
usage(const char* progname) {
INFO("Version %s\n"
"Usage: %s [options] <firmware file> [<fw file2> ... <fw fileN>]\n"
"\t-o <output file prefix>\t"
" - output file prefix, default: firmware version\n"
"\t-d\t\t\t - turn debug output on\n"
"\t-h\t\t\t - this help\n", VERSION, progname);
}
static int
do_fwsplit(const char* filename) {
int rc;
int fd;
struct stat st;
fw_t fw;
unsigned char* addr;
INFO("Firmware file: '%s'\n", filename);
rc = stat(filename, &st);
if (rc) {
ERROR("Couldn't stat() file '%s': %s\n",
filename, strerror(errno));
return -2;
}
if (st.st_size < sizeof(header_t) + sizeof(signature_t)) {
ERROR("File '%s' is too short\n", filename);
return -3;
}
fd = open(filename, O_RDONLY);
if (fd < 0) {
ERROR("Couldn't open file '%s': %s\n",
filename, strerror(errno));
return -4;
}
addr=(unsigned char*)mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
ERROR("Failed mmaping memory for file '%s'\n", filename);
close(fd);
return -5;
}
// parse & validate fw
rc = fw_parse(addr, st.st_size, &fw);
if (rc) {
ERROR("Invalid firmware file '%s'!\n", filename);
munmap(addr, st.st_size);
close(fd);
return -6;
}
if (strlen(prefix) == 0) {
strncpy(prefix, fw.version, sizeof(prefix));
}
fw_split(&fw, prefix);
munmap(addr, st.st_size);
close(fd);
return 0;
}
int
main(int argc, char* argv[]) {
int o, i;
memset(prefix, 0, sizeof(prefix));
while ((o = getopt(argc, argv, "hdo:")) != -1) {
switch (o) {
case 'd':
debug++;
break;
case 'o':
if (optarg) {
strncpy(prefix, optarg, sizeof(prefix));
}
break;
case 'h':
usage(argv[0]);
return -1;
}
}
if (optind >= argc) {
usage(argv[0]);
return -1;
}
if (strlen(prefix) != 0 && (optind + 1) < argc) {
WARN("Prefix overridden - will process only the first firmware file\n");
do_fwsplit(argv[optind]);
} else {
for (i = optind; i < argc; ++i) {
do_fwsplit(argv[i]);
}
}
return 0;
}