bb5ebd7fdc967d4f0bdd9dd88a69ccb12b7b5166
[openwrt/svn-archive/archive.git] / tools / firmware-utils / src / mktplinkfw.c
1 /*
2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This tool was based on:
5 * TP-Link WR941 V2 firmware checksum fixing tool.
6 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <unistd.h> /* for unlink() */
19 #include <libgen.h>
20 #include <getopt.h> /* for getopt() */
21 #include <stdarg.h>
22 #include <errno.h>
23 #include <sys/stat.h>
24
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
27
28 #include "md5.h"
29
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
31
32 #define HEADER_VERSION_V1 0x01000000
33 #define HWID_GS_OOLITE_V1 0x3C000101
34 #define HWID_TL_MR10U_V1 0x00100101
35 #define HWID_TL_MR13U_V1 0x00130101
36 #define HWID_TL_MR3020_V1 0x30200001
37 #define HWID_TL_MR3220_V1 0x32200001
38 #define HWID_TL_MR3220_V2 0x32200002
39 #define HWID_TL_MR3420_V1 0x34200001
40 #define HWID_TL_MR3420_V2 0x34200002
41 #define HWID_TL_WA701N_V1 0x07010001
42 #define HWID_TL_WA7510N_V1 0x75100001
43 #define HWID_TL_WA801ND_V1 0x08010001
44 #define HWID_TL_WA830RE_V1 0x08300010
45 #define HWID_TL_WA830RE_V2 0x08300002
46 #define HWID_TL_WA801ND_V2 0x08010002
47 #define HWID_TL_WA901ND_V1 0x09010001
48 #define HWID_TL_WA901ND_V2 0x09010002
49 #define HWID_TL_WDR4900_V1 0x49000001
50 #define HWID_TL_WR703N_V1 0x07030101
51 #define HWID_TL_WR720N_V3 0x07200103
52 #define HWID_TL_WR741ND_V1 0x07410001
53 #define HWID_TL_WR741ND_V4 0x07410004
54 #define HWID_TL_WR740N_V1 0x07400001
55 #define HWID_TL_WR740N_V3 0x07400003
56 #define HWID_TL_WR743ND_V1 0x07430001
57 #define HWID_TL_WR743ND_V2 0x07430002
58 #define HWID_TL_WR841N_V1_5 0x08410002
59 #define HWID_TL_WR841ND_V3 0x08410003
60 #define HWID_TL_WR841ND_V5 0x08410005
61 #define HWID_TL_WR841ND_V7 0x08410007
62 #define HWID_TL_WR941ND_V2 0x09410002
63 #define HWID_TL_WR941ND_V4 0x09410004
64 #define HWID_TL_WR1043ND_V1 0x10430001
65 #define HWID_TL_WR1043ND_V2 0x10430002
66 #define HWID_TL_WR1041N_V2 0x10410002
67 #define HWID_TL_WR2543N_V1 0x25430001
68
69 #define MD5SUM_LEN 16
70
71 struct file_info {
72 char *file_name; /* name of the file */
73 uint32_t file_size; /* length of the file */
74 };
75
76 struct fw_header {
77 uint32_t version; /* header version */
78 char vendor_name[24];
79 char fw_version[36];
80 uint32_t hw_id; /* hardware id */
81 uint32_t hw_rev; /* hardware revision */
82 uint32_t unk1;
83 uint8_t md5sum1[MD5SUM_LEN];
84 uint32_t unk2;
85 uint8_t md5sum2[MD5SUM_LEN];
86 uint32_t unk3;
87 uint32_t kernel_la; /* kernel load address */
88 uint32_t kernel_ep; /* kernel entry point */
89 uint32_t fw_length; /* total length of the firmware */
90 uint32_t kernel_ofs; /* kernel data offset */
91 uint32_t kernel_len; /* kernel data length */
92 uint32_t rootfs_ofs; /* rootfs data offset */
93 uint32_t rootfs_len; /* rootfs data length */
94 uint32_t boot_ofs; /* bootloader data offset */
95 uint32_t boot_len; /* bootloader data length */
96 uint16_t ver_hi;
97 uint16_t ver_mid;
98 uint16_t ver_lo;
99 uint8_t pad[354];
100 } __attribute__ ((packed));
101
102 struct flash_layout {
103 char *id;
104 uint32_t fw_max_len;
105 uint32_t kernel_la;
106 uint32_t kernel_ep;
107 uint32_t rootfs_ofs;
108 };
109
110 struct board_info {
111 char *id;
112 uint32_t hw_id;
113 uint32_t hw_rev;
114 char *layout_id;
115 };
116
117 /*
118 * Globals
119 */
120 static char *ofname;
121 static char *progname;
122 static char *vendor = "TP-LINK Technologies";
123 static char *version = "ver. 1.0";
124 static char *fw_ver = "0.0.0";
125
126 static char *board_id;
127 static struct board_info *board;
128 static char *layout_id;
129 static struct flash_layout *layout;
130 static char *opt_hw_id;
131 static uint32_t hw_id;
132 static char *opt_hw_rev;
133 static uint32_t hw_rev;
134 static int fw_ver_lo;
135 static int fw_ver_mid;
136 static int fw_ver_hi;
137 static struct file_info kernel_info;
138 static uint32_t kernel_la = 0;
139 static uint32_t kernel_ep = 0;
140 static uint32_t kernel_len = 0;
141 static struct file_info rootfs_info;
142 static uint32_t rootfs_ofs = 0;
143 static uint32_t rootfs_align;
144 static struct file_info boot_info;
145 static int combined;
146 static int strip_padding;
147 static int add_jffs2_eof;
148 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
149 static uint32_t fw_max_len;
150 static uint32_t reserved_space;
151
152 static struct file_info inspect_info;
153 static int extract = 0;
154
155 char md5salt_normal[MD5SUM_LEN] = {
156 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
157 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
158 };
159
160 char md5salt_boot[MD5SUM_LEN] = {
161 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
162 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
163 };
164
165 static struct flash_layout layouts[] = {
166 {
167 .id = "4M",
168 .fw_max_len = 0x3c0000,
169 .kernel_la = 0x80060000,
170 .kernel_ep = 0x80060000,
171 .rootfs_ofs = 0x140000,
172 }, {
173 .id = "4Mlzma",
174 .fw_max_len = 0x3c0000,
175 .kernel_la = 0x80060000,
176 .kernel_ep = 0x80060000,
177 .rootfs_ofs = 0x100000,
178 }, {
179 .id = "8M",
180 .fw_max_len = 0x7c0000,
181 .kernel_la = 0x80060000,
182 .kernel_ep = 0x80060000,
183 .rootfs_ofs = 0x140000,
184 }, {
185 .id = "8Mlzma",
186 .fw_max_len = 0x7c0000,
187 .kernel_la = 0x80060000,
188 .kernel_ep = 0x80060000,
189 .rootfs_ofs = 0x100000,
190 }, {
191 .id = "16M",
192 .fw_max_len = 0xf80000,
193 .kernel_la = 0x80060000,
194 .kernel_ep = 0x80060000,
195 .rootfs_ofs = 0x140000,
196 }, {
197 .id = "16Mlzma",
198 .fw_max_len = 0xf80000,
199 .kernel_la = 0x80060000,
200 .kernel_ep = 0x80060000,
201 .rootfs_ofs = 0x100000,
202 }, {
203 .id = "16Mppc",
204 .fw_max_len = 0xf80000,
205 .kernel_la = 0x00000000,
206 .kernel_ep = 0xc0000000,
207 .rootfs_ofs = 0x2a0000,
208 }, {
209 /* terminating entry */
210 }
211 };
212
213 static struct board_info boards[] = {
214 {
215 .id = "TL-MR10Uv1",
216 .hw_id = HWID_TL_MR10U_V1,
217 .hw_rev = 1,
218 .layout_id = "4Mlzma",
219 }, {
220 .id = "TL-MR13Uv1",
221 .hw_id = HWID_TL_MR13U_V1,
222 .hw_rev = 1,
223 .layout_id = "4Mlzma",
224 }, {
225 .id = "TL-MR3020v1",
226 .hw_id = HWID_TL_MR3020_V1,
227 .hw_rev = 1,
228 .layout_id = "4Mlzma",
229 }, {
230 .id = "TL-MR3220v1",
231 .hw_id = HWID_TL_MR3220_V1,
232 .hw_rev = 1,
233 .layout_id = "4M",
234 }, {
235 .id = "TL-MR3220v2",
236 .hw_id = HWID_TL_MR3220_V2,
237 .hw_rev = 1,
238 .layout_id = "4Mlzma",
239 }, {
240 .id = "TL-MR3420v1",
241 .hw_id = HWID_TL_MR3420_V1,
242 .hw_rev = 1,
243 .layout_id = "4M",
244 }, {
245 .id = "TL-MR3420v2",
246 .hw_id = HWID_TL_MR3420_V2,
247 .hw_rev = 1,
248 .layout_id = "4Mlzma",
249 }, {
250 .id = "TL-WA701Nv1",
251 .hw_id = HWID_TL_WA701N_V1,
252 .hw_rev = 1,
253 .layout_id = "4M",
254 }, {
255 .id = "TL-WA7510N",
256 .hw_id = HWID_TL_WA7510N_V1,
257 .hw_rev = 1,
258 .layout_id = "4M",
259 }, {
260 .id = "TL-WA801NDv1",
261 .hw_id = HWID_TL_WA801ND_V1,
262 .hw_rev = 1,
263 .layout_id = "4M",
264 }, {
265 .id = "TL-WA830REv1",
266 .hw_id = HWID_TL_WA830RE_V1,
267 .hw_rev = 1,
268 .layout_id = "4M",
269 }, {
270 .id = "TL-WA830REv2",
271 .hw_id = HWID_TL_WA830RE_V2,
272 .hw_rev = 1,
273 .layout_id = "4M",
274 }, {
275 .id = "TL-WA801NDv2",
276 .hw_id = HWID_TL_WA801ND_V2,
277 .hw_rev = 1,
278 .layout_id = "4Mlzma",
279 }, {
280 .id = "TL-WA901NDv1",
281 .hw_id = HWID_TL_WA901ND_V1,
282 .hw_rev = 1,
283 .layout_id = "4M",
284 }, {
285 .id = "TL-WA901NDv2",
286 .hw_id = HWID_TL_WA901ND_V2,
287 .hw_rev = 1,
288 .layout_id = "4M",
289 }, {
290 .id = "TL-WDR4900v1",
291 .hw_id = HWID_TL_WDR4900_V1,
292 .hw_rev = 1,
293 .layout_id = "16Mppc",
294 }, {
295 .id = "TL-WR741NDv1",
296 .hw_id = HWID_TL_WR741ND_V1,
297 .hw_rev = 1,
298 .layout_id = "4M",
299 }, {
300 .id = "TL-WR741NDv4",
301 .hw_id = HWID_TL_WR741ND_V4,
302 .hw_rev = 1,
303 .layout_id = "4Mlzma",
304 }, {
305 .id = "TL-WR740Nv1",
306 .hw_id = HWID_TL_WR740N_V1,
307 .hw_rev = 1,
308 .layout_id = "4M",
309 }, {
310 .id = "TL-WR740Nv3",
311 .hw_id = HWID_TL_WR740N_V3,
312 .hw_rev = 1,
313 .layout_id = "4M",
314 }, {
315 .id = "TL-WR743NDv1",
316 .hw_id = HWID_TL_WR743ND_V1,
317 .hw_rev = 1,
318 .layout_id = "4M",
319 }, {
320 .id = "TL-WR743NDv2",
321 .hw_id = HWID_TL_WR743ND_V2,
322 .hw_rev = 1,
323 .layout_id = "4Mlzma",
324 }, {
325 .id = "TL-WR841Nv1.5",
326 .hw_id = HWID_TL_WR841N_V1_5,
327 .hw_rev = 2,
328 .layout_id = "4M",
329 }, {
330 .id = "TL-WR841NDv3",
331 .hw_id = HWID_TL_WR841ND_V3,
332 .hw_rev = 3,
333 .layout_id = "4M",
334 }, {
335 .id = "TL-WR841NDv5",
336 .hw_id = HWID_TL_WR841ND_V5,
337 .hw_rev = 1,
338 .layout_id = "4M",
339 }, {
340 .id = "TL-WR841NDv7",
341 .hw_id = HWID_TL_WR841ND_V7,
342 .hw_rev = 1,
343 .layout_id = "4M",
344 }, {
345 .id = "TL-WR941NDv2",
346 .hw_id = HWID_TL_WR941ND_V2,
347 .hw_rev = 2,
348 .layout_id = "4M",
349 }, {
350 .id = "TL-WR941NDv4",
351 .hw_id = HWID_TL_WR941ND_V4,
352 .hw_rev = 1,
353 .layout_id = "4M",
354 }, {
355 .id = "TL-WR1041Nv2",
356 .hw_id = HWID_TL_WR1041N_V2,
357 .hw_rev = 1,
358 .layout_id = "4Mlzma",
359 }, {
360 .id = "TL-WR1043NDv1",
361 .hw_id = HWID_TL_WR1043ND_V1,
362 .hw_rev = 1,
363 .layout_id = "8M",
364 }, {
365 .id = "TL-WR1043NDv2",
366 .hw_id = HWID_TL_WR1043ND_V2,
367 .hw_rev = 1,
368 .layout_id = "8Mlzma",
369 }, {
370 .id = "TL-WR2543Nv1",
371 .hw_id = HWID_TL_WR2543N_V1,
372 .hw_rev = 1,
373 .layout_id = "8Mlzma",
374 }, {
375 .id = "TL-WR703Nv1",
376 .hw_id = HWID_TL_WR703N_V1,
377 .hw_rev = 1,
378 .layout_id = "4Mlzma",
379 }, {
380 .id = "TL-WR720Nv3",
381 .hw_id = HWID_TL_WR720N_V3,
382 .hw_rev = 1,
383 .layout_id = "4Mlzma",
384 }, {
385 .id = "GS-OOLITEv1",
386 .hw_id = HWID_GS_OOLITE_V1,
387 .hw_rev = 1,
388 .layout_id = "16Mlzma",
389 }, {
390 /* terminating entry */
391 }
392 };
393
394 /*
395 * Message macros
396 */
397 #define ERR(fmt, ...) do { \
398 fflush(0); \
399 fprintf(stderr, "[%s] *** error: " fmt "\n", \
400 progname, ## __VA_ARGS__ ); \
401 } while (0)
402
403 #define ERRS(fmt, ...) do { \
404 int save = errno; \
405 fflush(0); \
406 fprintf(stderr, "[%s] *** error: " fmt "\n", \
407 progname, ## __VA_ARGS__, strerror(save)); \
408 } while (0)
409
410 #define DBG(fmt, ...) do { \
411 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
412 } while (0)
413
414 static struct board_info *find_board(char *id)
415 {
416 struct board_info *ret;
417 struct board_info *board;
418
419 ret = NULL;
420 for (board = boards; board->id != NULL; board++){
421 if (strcasecmp(id, board->id) == 0) {
422 ret = board;
423 break;
424 }
425 };
426
427 return ret;
428 }
429
430 static struct board_info *find_board_by_hwid(uint32_t hw_id)
431 {
432 struct board_info *board;
433
434 for (board = boards; board->id != NULL; board++) {
435 if (hw_id == board->hw_id)
436 return board;
437 };
438
439 return NULL;
440 }
441
442 static struct flash_layout *find_layout(char *id)
443 {
444 struct flash_layout *ret;
445 struct flash_layout *l;
446
447 ret = NULL;
448 for (l = layouts; l->id != NULL; l++){
449 if (strcasecmp(id, l->id) == 0) {
450 ret = l;
451 break;
452 }
453 };
454
455 return ret;
456 }
457
458 static void usage(int status)
459 {
460 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
461 struct board_info *board;
462
463 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
464 fprintf(stream,
465 "\n"
466 "Options:\n"
467 " -B <board> create image for the board specified with <board>\n"
468 " -c use combined kernel image\n"
469 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
470 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
471 " -H <hwid> use hardware id specified with <hwid>\n"
472 " -W <hwrev> use hardware revision specified with <hwrev>\n"
473 " -F <id> use flash layout specified with <id>\n"
474 " -k <file> read kernel image from the file <file>\n"
475 " -r <file> read rootfs image from the file <file>\n"
476 " -a <align> align the rootfs start on an <align> bytes boundary\n"
477 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
478 " -o <file> write output to the file <file>\n"
479 " -s strip padding from the end of the image\n"
480 " -j add jffs2 end-of-filesystem markers\n"
481 " -N <vendor> set image vendor to <vendor>\n"
482 " -V <version> set image version to <version>\n"
483 " -v <version> set firmware version to <version>\n"
484 " -i <file> inspect given firmware file <file>\n"
485 " -x extract kernel and rootfs while inspecting (requires -i)\n"
486 " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
487 " -h show this screen\n"
488 );
489
490 exit(status);
491 }
492
493 static int get_md5(char *data, int size, char *md5)
494 {
495 MD5_CTX ctx;
496
497 MD5_Init(&ctx);
498 MD5_Update(&ctx, data, size);
499 MD5_Final(md5, &ctx);
500 }
501
502 static int get_file_stat(struct file_info *fdata)
503 {
504 struct stat st;
505 int res;
506
507 if (fdata->file_name == NULL)
508 return 0;
509
510 res = stat(fdata->file_name, &st);
511 if (res){
512 ERRS("stat failed on %s", fdata->file_name);
513 return res;
514 }
515
516 fdata->file_size = st.st_size;
517 return 0;
518 }
519
520 static int read_to_buf(struct file_info *fdata, char *buf)
521 {
522 FILE *f;
523 int ret = EXIT_FAILURE;
524
525 f = fopen(fdata->file_name, "r");
526 if (f == NULL) {
527 ERRS("could not open \"%s\" for reading", fdata->file_name);
528 goto out;
529 }
530
531 errno = 0;
532 fread(buf, fdata->file_size, 1, f);
533 if (errno != 0) {
534 ERRS("unable to read from file \"%s\"", fdata->file_name);
535 goto out_close;
536 }
537
538 ret = EXIT_SUCCESS;
539
540 out_close:
541 fclose(f);
542 out:
543 return ret;
544 }
545
546 static int check_options(void)
547 {
548 int ret;
549
550 if (inspect_info.file_name) {
551 ret = get_file_stat(&inspect_info);
552 if (ret)
553 return ret;
554
555 return 0;
556 } else if (extract) {
557 ERR("no firmware for inspection specified");
558 return -1;
559 }
560
561 if (board_id == NULL && opt_hw_id == NULL) {
562 ERR("either board or hardware id must be specified");
563 return -1;
564 }
565
566 if (board_id) {
567 board = find_board(board_id);
568 if (board == NULL) {
569 ERR("unknown/unsupported board id \"%s\"", board_id);
570 return -1;
571 }
572 if (layout_id == NULL)
573 layout_id = board->layout_id;
574
575 hw_id = board->hw_id;
576 hw_rev = board->hw_rev;
577 } else {
578 if (layout_id == NULL) {
579 ERR("flash layout is not specified");
580 return -1;
581 }
582 hw_id = strtoul(opt_hw_id, NULL, 0);
583
584 if (opt_hw_rev)
585 hw_rev = strtoul(opt_hw_rev, NULL, 0);
586 else
587 hw_rev = 1;
588 }
589
590 layout = find_layout(layout_id);
591 if (layout == NULL) {
592 ERR("unknown flash layout \"%s\"", layout_id);
593 return -1;
594 }
595
596 if (!kernel_la)
597 kernel_la = layout->kernel_la;
598 if (!kernel_ep)
599 kernel_ep = layout->kernel_ep;
600 if (!rootfs_ofs)
601 rootfs_ofs = layout->rootfs_ofs;
602
603 if (reserved_space > layout->fw_max_len) {
604 ERR("reserved space is not valid");
605 return -1;
606 }
607
608 fw_max_len = layout->fw_max_len - reserved_space;
609
610 if (kernel_info.file_name == NULL) {
611 ERR("no kernel image specified");
612 return -1;
613 }
614
615 ret = get_file_stat(&kernel_info);
616 if (ret)
617 return ret;
618
619 kernel_len = kernel_info.file_size;
620
621 if (combined) {
622 if (kernel_info.file_size >
623 fw_max_len - sizeof(struct fw_header)) {
624 ERR("kernel image is too big");
625 return -1;
626 }
627 } else {
628 if (rootfs_info.file_name == NULL) {
629 ERR("no rootfs image specified");
630 return -1;
631 }
632
633 ret = get_file_stat(&rootfs_info);
634 if (ret)
635 return ret;
636
637 if (rootfs_align) {
638 kernel_len += sizeof(struct fw_header);
639 kernel_len = ALIGN(kernel_len, rootfs_align);
640 kernel_len -= sizeof(struct fw_header);
641
642 DBG("kernel length aligned to %u", kernel_len);
643
644 if (kernel_len + rootfs_info.file_size >
645 fw_max_len - sizeof(struct fw_header)) {
646 ERR("images are too big");
647 return -1;
648 }
649 } else {
650 if (kernel_info.file_size >
651 rootfs_ofs - sizeof(struct fw_header)) {
652 ERR("kernel image is too big");
653 return -1;
654 }
655
656 if (rootfs_info.file_size >
657 (fw_max_len - rootfs_ofs)) {
658 ERR("rootfs image is too big");
659 return -1;
660 }
661 }
662 }
663
664 if (ofname == NULL) {
665 ERR("no output file specified");
666 return -1;
667 }
668
669 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
670 if (ret != 3) {
671 ERR("invalid firmware version '%s'", fw_ver);
672 return -1;
673 }
674
675 return 0;
676 }
677
678 static void fill_header(char *buf, int len)
679 {
680 struct fw_header *hdr = (struct fw_header *)buf;
681
682 memset(hdr, 0, sizeof(struct fw_header));
683
684 hdr->version = htonl(HEADER_VERSION_V1);
685 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
686 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
687 hdr->hw_id = htonl(hw_id);
688 hdr->hw_rev = htonl(hw_rev);
689
690 if (boot_info.file_size == 0)
691 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
692 else
693 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
694
695 hdr->kernel_la = htonl(kernel_la);
696 hdr->kernel_ep = htonl(kernel_ep);
697 hdr->fw_length = htonl(layout->fw_max_len);
698 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
699 hdr->kernel_len = htonl(kernel_len);
700 if (!combined) {
701 hdr->rootfs_ofs = htonl(rootfs_ofs);
702 hdr->rootfs_len = htonl(rootfs_info.file_size);
703 }
704
705 hdr->ver_hi = htons(fw_ver_hi);
706 hdr->ver_mid = htons(fw_ver_mid);
707 hdr->ver_lo = htons(fw_ver_lo);
708
709 get_md5(buf, len, hdr->md5sum1);
710 }
711
712 static int pad_jffs2(char *buf, int currlen)
713 {
714 int len;
715 uint32_t pad_mask;
716
717 len = currlen;
718 pad_mask = (64 * 1024);
719 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
720 uint32_t mask;
721 int i;
722
723 for (i = 10; i < 32; i++) {
724 mask = 1 << i;
725 if (pad_mask & mask)
726 break;
727 }
728
729 len = ALIGN(len, mask);
730
731 for (i = 10; i < 32; i++) {
732 mask = 1 << i;
733 if ((len & (mask - 1)) == 0)
734 pad_mask &= ~mask;
735 }
736
737 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
738 buf[len + i] = jffs2_eof_mark[i];
739
740 len += sizeof(jffs2_eof_mark);
741 }
742
743 return len;
744 }
745
746 static int write_fw(char *data, int len)
747 {
748 FILE *f;
749 int ret = EXIT_FAILURE;
750
751 f = fopen(ofname, "w");
752 if (f == NULL) {
753 ERRS("could not open \"%s\" for writing", ofname);
754 goto out;
755 }
756
757 errno = 0;
758 fwrite(data, len, 1, f);
759 if (errno) {
760 ERRS("unable to write output file");
761 goto out_flush;
762 }
763
764 DBG("firmware file \"%s\" completed", ofname);
765
766 ret = EXIT_SUCCESS;
767
768 out_flush:
769 fflush(f);
770 fclose(f);
771 if (ret != EXIT_SUCCESS) {
772 unlink(ofname);
773 }
774 out:
775 return ret;
776 }
777
778 static int build_fw(void)
779 {
780 int buflen;
781 char *buf;
782 char *p;
783 int ret = EXIT_FAILURE;
784 int writelen = 0;
785
786 buflen = layout->fw_max_len;
787
788 buf = malloc(buflen);
789 if (!buf) {
790 ERR("no memory for buffer\n");
791 goto out;
792 }
793
794 memset(buf, 0xff, buflen);
795 p = buf + sizeof(struct fw_header);
796 ret = read_to_buf(&kernel_info, p);
797 if (ret)
798 goto out_free_buf;
799
800 writelen = sizeof(struct fw_header) + kernel_len;
801
802 if (!combined) {
803 if (rootfs_align)
804 p = buf + writelen;
805 else
806 p = buf + rootfs_ofs;
807
808 ret = read_to_buf(&rootfs_info, p);
809 if (ret)
810 goto out_free_buf;
811
812 if (rootfs_align)
813 writelen += rootfs_info.file_size;
814 else
815 writelen = rootfs_ofs + rootfs_info.file_size;
816
817 if (add_jffs2_eof)
818 writelen = pad_jffs2(buf, writelen);
819 }
820
821 if (!strip_padding)
822 writelen = buflen;
823
824 fill_header(buf, writelen);
825 ret = write_fw(buf, writelen);
826 if (ret)
827 goto out_free_buf;
828
829 ret = EXIT_SUCCESS;
830
831 out_free_buf:
832 free(buf);
833 out:
834 return ret;
835 }
836
837 /* Helper functions to inspect_fw() representing different output formats */
838 static inline void inspect_fw_pstr(char *label, char *str)
839 {
840 printf("%-23s: %s\n", label, str);
841 }
842
843 static inline void inspect_fw_phex(char *label, uint32_t val)
844 {
845 printf("%-23s: 0x%08x\n", label, val);
846 }
847
848 static inline void inspect_fw_phexpost(char *label,
849 uint32_t val, char *post)
850 {
851 printf("%-23s: 0x%08x (%s)\n", label, val, post);
852 }
853
854 static inline void inspect_fw_phexdef(char *label,
855 uint32_t val, uint32_t defval)
856 {
857 printf("%-23s: 0x%08x ", label, val);
858
859 if (val == defval)
860 printf("(== OpenWrt default)\n");
861 else
862 printf("(OpenWrt default: 0x%08x)\n", defval);
863 }
864
865 static inline void inspect_fw_phexexp(char *label,
866 uint32_t val, uint32_t expval)
867 {
868 printf("%-23s: 0x%08x ", label, val);
869
870 if (val == expval)
871 printf("(ok)\n");
872 else
873 printf("(expected: 0x%08x)\n", expval);
874 }
875
876 static inline void inspect_fw_phexdec(char *label, uint32_t val)
877 {
878 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
879 }
880
881 static inline void inspect_fw_phexdecdef(char *label,
882 uint32_t val, uint32_t defval)
883 {
884 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
885
886 if (val == defval)
887 printf("(== OpenWrt default)\n");
888 else
889 printf("(OpenWrt default: 0x%08x)\n", defval);
890 }
891
892 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
893 {
894 int i;
895
896 printf("%-23s:", label);
897 for (i=0; i<MD5SUM_LEN; i++)
898 printf(" %02x", val[i]);
899 printf(" %s\n", text);
900 }
901
902 static int inspect_fw(void)
903 {
904 char *buf;
905 struct fw_header *hdr;
906 uint8_t md5sum[MD5SUM_LEN];
907 struct board_info *board;
908 int ret = EXIT_FAILURE;
909
910 buf = malloc(inspect_info.file_size);
911 if (!buf) {
912 ERR("no memory for buffer!\n");
913 goto out;
914 }
915
916 ret = read_to_buf(&inspect_info, buf);
917 if (ret)
918 goto out_free_buf;
919 hdr = (struct fw_header *)buf;
920
921 inspect_fw_pstr("File name", inspect_info.file_name);
922 inspect_fw_phexdec("File size", inspect_info.file_size);
923
924 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
925 ERR("file does not seem to have V1 header!\n");
926 goto out_free_buf;
927 }
928
929 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
930
931 if (ntohl(hdr->unk1) != 0)
932 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
933
934 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
935 if (ntohl(hdr->boot_len) == 0)
936 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
937 else
938 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
939 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
940
941 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
942 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
943 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
944 } else {
945 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
946 }
947 if (ntohl(hdr->unk2) != 0)
948 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
949 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
950 "(purpose yet unknown, unchecked here)");
951 if (ntohl(hdr->unk3) != 0)
952 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
953
954 printf("\n");
955
956 inspect_fw_pstr("Vendor name", hdr->vendor_name);
957 inspect_fw_pstr("Firmware version", hdr->fw_version);
958 board = find_board_by_hwid(ntohl(hdr->hw_id));
959 if (board) {
960 layout = find_layout(board->layout_id);
961 inspect_fw_phexpost("Hardware ID",
962 ntohl(hdr->hw_id), board->id);
963 inspect_fw_phexexp("Hardware Revision",
964 ntohl(hdr->hw_rev), board->hw_rev);
965 } else {
966 inspect_fw_phexpost("Hardware ID",
967 ntohl(hdr->hw_id), "unknown");
968 inspect_fw_phex("Hardware Revision",
969 ntohl(hdr->hw_rev));
970 }
971
972 printf("\n");
973
974 inspect_fw_phexdec("Kernel data offset",
975 ntohl(hdr->kernel_ofs));
976 inspect_fw_phexdec("Kernel data length",
977 ntohl(hdr->kernel_len));
978 if (board) {
979 inspect_fw_phexdef("Kernel load address",
980 ntohl(hdr->kernel_la),
981 layout ? layout->kernel_la : 0xffffffff);
982 inspect_fw_phexdef("Kernel entry point",
983 ntohl(hdr->kernel_ep),
984 layout ? layout->kernel_ep : 0xffffffff);
985 inspect_fw_phexdecdef("Rootfs data offset",
986 ntohl(hdr->rootfs_ofs),
987 layout ? layout->rootfs_ofs : 0xffffffff);
988 } else {
989 inspect_fw_phex("Kernel load address",
990 ntohl(hdr->kernel_la));
991 inspect_fw_phex("Kernel entry point",
992 ntohl(hdr->kernel_ep));
993 inspect_fw_phexdec("Rootfs data offset",
994 ntohl(hdr->rootfs_ofs));
995 }
996 inspect_fw_phexdec("Rootfs data length",
997 ntohl(hdr->rootfs_len));
998 inspect_fw_phexdec("Boot loader data offset",
999 ntohl(hdr->boot_ofs));
1000 inspect_fw_phexdec("Boot loader data length",
1001 ntohl(hdr->boot_len));
1002 inspect_fw_phexdec("Total firmware length",
1003 ntohl(hdr->fw_length));
1004
1005 if (extract) {
1006 FILE *fp;
1007 char *filename;
1008
1009 printf("\n");
1010
1011 filename = malloc(strlen(inspect_info.file_name) + 8);
1012 sprintf(filename, "%s-kernel", inspect_info.file_name);
1013 printf("Extracting kernel to \"%s\"...\n", filename);
1014 fp = fopen(filename, "w");
1015 if (fp) {
1016 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
1017 ntohl(hdr->kernel_len), 1, fp)) {
1018 ERR("error in fwrite(): %s", strerror(errno));
1019 }
1020 fclose(fp);
1021 } else {
1022 ERR("error in fopen(): %s", strerror(errno));
1023 }
1024 free(filename);
1025
1026 filename = malloc(strlen(inspect_info.file_name) + 8);
1027 sprintf(filename, "%s-rootfs", inspect_info.file_name);
1028 printf("Extracting rootfs to \"%s\"...\n", filename);
1029 fp = fopen(filename, "w");
1030 if (fp) {
1031 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
1032 ntohl(hdr->rootfs_len), 1, fp)) {
1033 ERR("error in fwrite(): %s", strerror(errno));
1034 }
1035 fclose(fp);
1036 } else {
1037 ERR("error in fopen(): %s", strerror(errno));
1038 }
1039 free(filename);
1040 }
1041
1042 out_free_buf:
1043 free(buf);
1044 out:
1045 return ret;
1046 }
1047
1048 int main(int argc, char *argv[])
1049 {
1050 int ret = EXIT_FAILURE;
1051 int err;
1052
1053 FILE *outfile;
1054
1055 progname = basename(argv[0]);
1056
1057 while ( 1 ) {
1058 int c;
1059
1060 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsjv:");
1061 if (c == -1)
1062 break;
1063
1064 switch (c) {
1065 case 'a':
1066 sscanf(optarg, "0x%x", &rootfs_align);
1067 break;
1068 case 'B':
1069 board_id = optarg;
1070 break;
1071 case 'H':
1072 opt_hw_id = optarg;
1073 break;
1074 case 'E':
1075 sscanf(optarg, "0x%x", &kernel_ep);
1076 break;
1077 case 'F':
1078 layout_id = optarg;
1079 break;
1080 case 'W':
1081 opt_hw_rev = optarg;
1082 break;
1083 case 'L':
1084 sscanf(optarg, "0x%x", &kernel_la);
1085 break;
1086 case 'V':
1087 version = optarg;
1088 break;
1089 case 'v':
1090 fw_ver = optarg;
1091 break;
1092 case 'N':
1093 vendor = optarg;
1094 break;
1095 case 'c':
1096 combined++;
1097 break;
1098 case 'k':
1099 kernel_info.file_name = optarg;
1100 break;
1101 case 'r':
1102 rootfs_info.file_name = optarg;
1103 break;
1104 case 'R':
1105 sscanf(optarg, "0x%x", &rootfs_ofs);
1106 break;
1107 case 'o':
1108 ofname = optarg;
1109 break;
1110 case 's':
1111 strip_padding = 1;
1112 break;
1113 case 'i':
1114 inspect_info.file_name = optarg;
1115 break;
1116 case 'j':
1117 add_jffs2_eof = 1;
1118 break;
1119 case 'x':
1120 extract = 1;
1121 break;
1122 case 'h':
1123 usage(EXIT_SUCCESS);
1124 break;
1125 case 'X':
1126 sscanf(optarg, "0x%x", &reserved_space);
1127 break;
1128 default:
1129 usage(EXIT_FAILURE);
1130 break;
1131 }
1132 }
1133
1134 ret = check_options();
1135 if (ret)
1136 goto out;
1137
1138 if (!inspect_info.file_name)
1139 ret = build_fw();
1140 else
1141 ret = inspect_fw();
1142
1143 out:
1144 return ret;
1145 }
1146