92e5be081c1d9050c6b234c6014fbacb5c1380a6
2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
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>
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.
18 #include <unistd.h> /* for unlink() */
20 #include <getopt.h> /* for getopt() */
27 #if (__BYTE_ORDER == __BIG_ENDIAN)
28 # define HOST_TO_BE32(x) (x)
29 # define BE32_TO_HOST(x) (x)
31 # define HOST_TO_BE32(x) bswap_32(x)
32 # define BE32_TO_HOST(x) bswap_32(x)
35 #define HEADER_VERSION_V1 0x01000000
36 #define HWID_TL_WA901ND_V1 0x09010001
37 #define HWID_TL_WR741ND_V1 0x07410001
38 #define HWID_TL_WR841N_V1_5 0x08410002
39 #define HWID_TL_WR841ND_V3 0x08410003
40 #define HWID_TL_WR841ND_V5 0x08410005
41 #define HWID_TL_WR841ND_V7 0x08410007
42 #define HWID_TL_WR941ND_V2 0x09410002
43 #define HWID_TL_WR941ND_V4 0x09410004
44 #define HWID_TL_WR1043ND_V1 0x10430001
49 char *file_name
; /* name of the file */
50 uint32_t file_size
; /* length of the file */
54 uint32_t version
; /* header version */
57 uint32_t hw_id
; /* hardware id */
58 uint32_t hw_rev
; /* hardware revision */
60 uint8_t md5sum1
[MD5SUM_LEN
];
62 uint8_t md5sum2
[MD5SUM_LEN
];
64 uint32_t kernel_la
; /* kernel load address */
65 uint32_t kernel_ep
; /* kernel entry point */
66 uint32_t fw_length
; /* total length of the firmware */
67 uint32_t kernel_ofs
; /* kernel data offset */
68 uint32_t kernel_len
; /* kernel data length */
69 uint32_t rootfs_ofs
; /* rootfs data offset */
70 uint32_t rootfs_len
; /* rootfs data length */
71 uint32_t boot_ofs
; /* bootloader data offset */
72 uint32_t boot_len
; /* bootloader data length */
74 } __attribute__ ((packed
));
90 static char *progname
;
91 static char *vendor
= "TP-LINK Technologies";
92 static char *version
= "ver. 1.0";
94 static char *board_id
;
95 static struct board_info
*board
;
96 static struct file_info kernel_info
;
97 static uint32_t kernel_la
= 0;
98 static uint32_t kernel_ep
= 0;
99 static struct file_info rootfs_info
;
100 static uint32_t rootfs_ofs
= 0;
101 static struct file_info boot_info
;
103 static int strip_padding
;
105 static struct file_info inspect_info
;
106 static int extract
= 0;
108 char md5salt_normal
[MD5SUM_LEN
] = {
109 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
110 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
113 char md5salt_boot
[MD5SUM_LEN
] = {
114 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
115 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
118 static struct board_info boards
[] = {
120 .id
= "TL-WA901NDv1",
121 .hw_id
= HWID_TL_WA901ND_V1
,
123 .fw_max_len
= 0x3c0000,
124 .kernel_la
= 0x80060000,
125 .kernel_ep
= 0x80060000,
126 .rootfs_ofs
= 0x140000,
128 .id
= "TL-WR741NDv1",
129 .hw_id
= HWID_TL_WR741ND_V1
,
131 .fw_max_len
= 0x3c0000,
132 .kernel_la
= 0x80060000,
133 .kernel_ep
= 0x80060000,
134 .rootfs_ofs
= 0x140000,
136 .id
= "TL-WR841Nv1.5",
137 .hw_id
= HWID_TL_WR841N_V1_5
,
139 .fw_max_len
= 0x3c0000,
140 .kernel_la
= 0x80060000,
141 .kernel_ep
= 0x80060000,
142 .rootfs_ofs
= 0x140000,
144 .id
= "TL-WR841NDv3",
145 .hw_id
= HWID_TL_WR841ND_V3
,
147 .fw_max_len
= 0x3c0000,
148 .kernel_la
= 0x80060000,
149 .kernel_ep
= 0x80060000,
150 .rootfs_ofs
= 0x140000,
152 .id
= "TL-WR841NDv5",
153 .hw_id
= HWID_TL_WR841ND_V5
,
155 .fw_max_len
= 0x3c0000,
156 .kernel_la
= 0x80060000,
157 .kernel_ep
= 0x80060000,
158 .rootfs_ofs
= 0x140000,
160 .id
= "TL-WR841NDv7",
161 .hw_id
= HWID_TL_WR841ND_V7
,
163 .fw_max_len
= 0x3c0000,
164 .kernel_la
= 0x80060000,
165 .kernel_ep
= 0x80060000,
166 .rootfs_ofs
= 0x140000,
168 .id
= "TL-WR941NDv2",
169 .hw_id
= HWID_TL_WR941ND_V2
,
171 .fw_max_len
= 0x3c0000,
172 .kernel_la
= 0x80060000,
173 .kernel_ep
= 0x80060000,
174 .rootfs_ofs
= 0x140000,
176 .id
= "TL-WR941NDv4",
177 .hw_id
= HWID_TL_WR941ND_V4
,
179 .fw_max_len
= 0x3c0000,
180 .kernel_la
= 0x80060000,
181 .kernel_ep
= 0x80060000,
182 .rootfs_ofs
= 0x140000,
184 .id
= "TL-WR1043NDv1",
185 .hw_id
= HWID_TL_WR1043ND_V1
,
187 .fw_max_len
= 0x7c0000,
188 .kernel_la
= 0x80060000,
189 .kernel_ep
= 0x80060000,
190 .rootfs_ofs
= 0x140000,
192 /* terminating entry */
199 #define ERR(fmt, ...) do { \
201 fprintf(stderr, "[%s] *** error: " fmt "\n", \
202 progname, ## __VA_ARGS__ ); \
205 #define ERRS(fmt, ...) do { \
208 fprintf(stderr, "[%s] *** error: " fmt "\n", \
209 progname, ## __VA_ARGS__, strerror(save)); \
212 #define DBG(fmt, ...) do { \
213 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
216 static struct board_info
*find_board(char *id
)
218 struct board_info
*ret
;
219 struct board_info
*board
;
222 for (board
= boards
; board
->id
!= NULL
; board
++){
223 if (strcasecmp(id
, board
->id
) == 0) {
232 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
234 struct board_info
*board
;
236 for (board
= boards
; board
->id
!= NULL
; board
++) {
237 if (hw_id
== board
->hw_id
)
245 static void usage(int status
)
247 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
248 struct board_info
*board
;
250 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
254 " -B <board> create image for the board specified with <board>\n"
255 " -c use combined kernel image\n"
256 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
257 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
258 " -k <file> read kernel image from the file <file>\n"
259 " -r <file> read rootfs image from the file <file>\n"
260 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
261 " -o <file> write output to the file <file>\n"
262 " -s strip padding from the end of the image\n"
263 " -N <vendor> set image vendor to <vendor>\n"
264 " -V <version> set image version to <version>\n"
265 " -i <file> inspect given firmware file <file>\n"
266 " -x extract kernel and rootfs while inspecting (requires -i)\n"
267 " -h show this screen\n"
273 static int get_md5(char *data
, int size
, char *md5
)
278 MD5_Update(&ctx
, data
, size
);
279 MD5_Final(md5
, &ctx
);
282 static int get_file_stat(struct file_info
*fdata
)
287 if (fdata
->file_name
== NULL
)
290 res
= stat(fdata
->file_name
, &st
);
292 ERRS("stat failed on %s", fdata
->file_name
);
296 fdata
->file_size
= st
.st_size
;
300 static int read_to_buf(struct file_info
*fdata
, char *buf
)
303 int ret
= EXIT_FAILURE
;
305 f
= fopen(fdata
->file_name
, "r");
307 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
312 fread(buf
, fdata
->file_size
, 1, f
);
314 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
326 static int check_options(void)
330 if (inspect_info
.file_name
) {
331 ret
= get_file_stat(&inspect_info
);
336 } else if (extract
) {
337 ERR("no firmware for inspection specified");
341 if (board_id
== NULL
) {
342 ERR("no board specified");
346 board
= find_board(board_id
);
348 ERR("unknown/unsupported board id \"%s\"", board_id
);
352 kernel_la
= board
->kernel_la
;
354 kernel_ep
= board
->kernel_ep
;
356 rootfs_ofs
= board
->rootfs_ofs
;
358 if (kernel_info
.file_name
== NULL
) {
359 ERR("no kernel image specified");
363 ret
= get_file_stat(&kernel_info
);
368 if (kernel_info
.file_size
>
369 board
->fw_max_len
- sizeof(struct fw_header
)) {
370 ERR("kernel image is too big");
374 if (kernel_info
.file_size
>
375 rootfs_ofs
- sizeof(struct fw_header
)) {
376 ERR("kernel image is too big");
379 if (rootfs_info
.file_name
== NULL
) {
380 ERR("no rootfs image specified");
384 ret
= get_file_stat(&rootfs_info
);
388 if (rootfs_info
.file_size
>
389 (board
->fw_max_len
- rootfs_ofs
)) {
390 ERR("rootfs image is too big");
395 if (ofname
== NULL
) {
396 ERR("no output file specified");
403 static void fill_header(char *buf
, int len
)
405 struct fw_header
*hdr
= (struct fw_header
*)buf
;
407 memset(hdr
, 0, sizeof(struct fw_header
));
409 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
410 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
411 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
412 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
413 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
415 if (boot_info
.file_size
== 0)
416 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
418 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
420 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
421 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
422 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
423 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
424 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
426 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
427 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
430 get_md5(buf
, len
, hdr
->md5sum1
);
433 static int write_fw(char *data
, int len
)
436 int ret
= EXIT_FAILURE
;
438 f
= fopen(ofname
, "w");
440 ERRS("could not open \"%s\" for writing", ofname
);
445 fwrite(data
, len
, 1, f
);
447 ERRS("unable to write output file");
451 DBG("firmware file \"%s\" completed", ofname
);
458 if (ret
!= EXIT_SUCCESS
) {
465 static int build_fw(void)
470 int ret
= EXIT_FAILURE
;
473 buflen
= board
->fw_max_len
;
475 buf
= malloc(buflen
);
477 ERR("no memory for buffer\n");
481 memset(buf
, 0xff, buflen
);
482 p
= buf
+ sizeof(struct fw_header
);
483 ret
= read_to_buf(&kernel_info
, p
);
487 writelen
= kernel_info
.file_size
;
490 p
= buf
+ rootfs_ofs
;
491 ret
= read_to_buf(&rootfs_info
, p
);
495 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
501 fill_header(buf
, writelen
);
502 ret
= write_fw(buf
, writelen
);
514 /* Helper functions to inspect_fw() representing different output formats */
515 static inline void inspect_fw_pstr(char *label
, char *str
)
517 printf("%-23s: %s\n", label
, str
);
520 static inline void inspect_fw_phex(char *label
, uint32_t val
)
522 printf("%-23s: 0x%08x\n", label
, val
);
525 static inline void inspect_fw_phexpost(char *label
,
526 uint32_t val
, char *post
)
528 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
531 static inline void inspect_fw_phexdef(char *label
,
532 uint32_t val
, uint32_t defval
)
534 printf("%-23s: 0x%08x ", label
, val
);
537 printf("(== OpenWrt default)\n");
539 printf("(OpenWrt default: 0x%08x)\n", defval
);
542 static inline void inspect_fw_phexexp(char *label
,
543 uint32_t val
, uint32_t expval
)
545 printf("%-23s: 0x%08x ", label
, val
);
550 printf("(expected: 0x%08x)\n", expval
);
553 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
555 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
558 static inline void inspect_fw_phexdecdef(char *label
,
559 uint32_t val
, uint32_t defval
)
561 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
564 printf("(== OpenWrt default)\n");
566 printf("(OpenWrt default: 0x%08x)\n", defval
);
569 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
573 printf("%-23s:", label
);
574 for (i
=0; i
<MD5SUM_LEN
; i
++)
575 printf(" %02x", val
[i
]);
576 printf(" %s\n", text
);
579 static int inspect_fw(void)
582 struct fw_header
*hdr
;
583 uint8_t md5sum
[MD5SUM_LEN
];
584 struct board_info
*board
;
585 int ret
= EXIT_FAILURE
;
587 buf
= malloc(inspect_info
.file_size
);
589 ERR("no memory for buffer!\n");
593 ret
= read_to_buf(&inspect_info
, buf
);
596 hdr
= (struct fw_header
*)buf
;
598 inspect_fw_pstr("File name", inspect_info
.file_name
);
599 inspect_fw_phexdec("File size", inspect_info
.file_size
);
601 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
602 ERR("file does not seem to have V1 header!\n");
606 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
608 if (BE32_TO_HOST(hdr
->unk1
) != 0)
609 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
611 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
612 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
613 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
615 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
616 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
618 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
619 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
620 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
622 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
624 if (BE32_TO_HOST(hdr
->unk2
) != 0)
625 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
626 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
627 "(purpose yet unknown, unchecked here)");
628 if (BE32_TO_HOST(hdr
->unk3
) != 0)
629 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
633 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
634 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
635 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
637 inspect_fw_phexpost("Hardware ID",
638 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
639 inspect_fw_phexexp("Hardware Revision",
640 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
642 inspect_fw_phexpost("Hardware ID",
643 BE32_TO_HOST(hdr
->hw_id
), "unknown");
644 inspect_fw_phex("Hardware Revision",
645 BE32_TO_HOST(hdr
->hw_rev
));
650 inspect_fw_phexdec("Kernel data offset",
651 BE32_TO_HOST(hdr
->kernel_ofs
));
652 inspect_fw_phexdec("Kernel data length",
653 BE32_TO_HOST(hdr
->kernel_len
));
655 inspect_fw_phexdef("Kernel load address",
656 BE32_TO_HOST(hdr
->kernel_la
),
658 inspect_fw_phexdef("Kernel entry point",
659 BE32_TO_HOST(hdr
->kernel_ep
),
661 inspect_fw_phexdecdef("Rootfs data offset",
662 BE32_TO_HOST(hdr
->rootfs_ofs
),
665 inspect_fw_phex("Kernel load address",
666 BE32_TO_HOST(hdr
->kernel_la
));
667 inspect_fw_phex("Kernel entry point",
668 BE32_TO_HOST(hdr
->kernel_ep
));
669 inspect_fw_phexdec("Rootfs data offset",
670 BE32_TO_HOST(hdr
->rootfs_ofs
));
672 inspect_fw_phexdec("Rootfs data length",
673 BE32_TO_HOST(hdr
->rootfs_len
));
674 inspect_fw_phexdec("Boot loader data offset",
675 BE32_TO_HOST(hdr
->boot_ofs
));
676 inspect_fw_phexdec("Boot loader data length",
677 BE32_TO_HOST(hdr
->boot_len
));
678 inspect_fw_phexdec("Total firmware length",
679 BE32_TO_HOST(hdr
->fw_length
));
687 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
688 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
689 printf("Extracting kernel to \"%s\"...\n", filename
);
690 fp
= fopen(filename
, "w");
692 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
693 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
694 ERR("error in fwrite(): %s", strerror(errno
));
698 ERR("error in fopen(): %s", strerror(errno
));
702 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
703 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
704 printf("Extracting rootfs to \"%s\"...\n", filename
);
705 fp
= fopen(filename
, "w");
707 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
708 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
709 ERR("error in fwrite(): %s", strerror(errno
));
713 ERR("error in fopen(): %s", strerror(errno
));
724 int main(int argc
, char *argv
[])
726 int ret
= EXIT_FAILURE
;
731 progname
= basename(argv
[0]);
736 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
745 sscanf(optarg
, "0x%x", &kernel_ep
);
748 sscanf(optarg
, "0x%x", &kernel_la
);
760 kernel_info
.file_name
= optarg
;
763 rootfs_info
.file_name
= optarg
;
766 sscanf(optarg
, "0x%x", &rootfs_ofs
);
775 inspect_info
.file_name
= optarg
;
789 ret
= check_options();
793 if (!inspect_info
.file_name
)
This page took 0.089989 seconds and 3 git commands to generate.