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_MR3220_V1 0x32200001
37 #define HWID_TL_MR3420_V1 0x34200001
38 #define HWID_TL_WA901ND_V1 0x09010001
39 #define HWID_TL_WA901ND_V2 0x09010002
40 #define HWID_TL_WR741ND_V1 0x07410001
41 #define HWID_TL_WR740N_V1 0x07400001
42 #define HWID_TL_WR841N_V1_5 0x08410002
43 #define HWID_TL_WR841ND_V3 0x08410003
44 #define HWID_TL_WR841ND_V5 0x08410005
45 #define HWID_TL_WR841ND_V7 0x08410007
46 #define HWID_TL_WR941ND_V2 0x09410002
47 #define HWID_TL_WR941ND_V4 0x09410004
48 #define HWID_TL_WR1043ND_V1 0x10430001
53 char *file_name
; /* name of the file */
54 uint32_t file_size
; /* length of the file */
58 uint32_t version
; /* header version */
61 uint32_t hw_id
; /* hardware id */
62 uint32_t hw_rev
; /* hardware revision */
64 uint8_t md5sum1
[MD5SUM_LEN
];
66 uint8_t md5sum2
[MD5SUM_LEN
];
68 uint32_t kernel_la
; /* kernel load address */
69 uint32_t kernel_ep
; /* kernel entry point */
70 uint32_t fw_length
; /* total length of the firmware */
71 uint32_t kernel_ofs
; /* kernel data offset */
72 uint32_t kernel_len
; /* kernel data length */
73 uint32_t rootfs_ofs
; /* rootfs data offset */
74 uint32_t rootfs_len
; /* rootfs data length */
75 uint32_t boot_ofs
; /* bootloader data offset */
76 uint32_t boot_len
; /* bootloader data length */
78 } __attribute__ ((packed
));
94 static char *progname
;
95 static char *vendor
= "TP-LINK Technologies";
96 static char *version
= "ver. 1.0";
98 static char *board_id
;
99 static struct board_info
*board
;
100 static struct file_info kernel_info
;
101 static uint32_t kernel_la
= 0;
102 static uint32_t kernel_ep
= 0;
103 static struct file_info rootfs_info
;
104 static uint32_t rootfs_ofs
= 0;
105 static struct file_info boot_info
;
107 static int strip_padding
;
109 static struct file_info inspect_info
;
110 static int extract
= 0;
112 char md5salt_normal
[MD5SUM_LEN
] = {
113 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
114 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
117 char md5salt_boot
[MD5SUM_LEN
] = {
118 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
119 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
122 static struct board_info boards
[] = {
125 .hw_id
= HWID_TL_MR3220_V1
,
127 .fw_max_len
= 0x3c0000,
128 .kernel_la
= 0x80060000,
129 .kernel_ep
= 0x80060000,
130 .rootfs_ofs
= 0x140000,
133 .hw_id
= HWID_TL_MR3420_V1
,
135 .fw_max_len
= 0x3c0000,
136 .kernel_la
= 0x80060000,
137 .kernel_ep
= 0x80060000,
138 .rootfs_ofs
= 0x140000,
140 .id
= "TL-WA901NDv1",
141 .hw_id
= HWID_TL_WA901ND_V1
,
143 .fw_max_len
= 0x3c0000,
144 .kernel_la
= 0x80060000,
145 .kernel_ep
= 0x80060000,
146 .rootfs_ofs
= 0x140000,
148 .id
= "TL-WA901NDv2",
149 .hw_id
= HWID_TL_WA901ND_V2
,
151 .fw_max_len
= 0x3c0000,
152 .kernel_la
= 0x80060000,
153 .kernel_ep
= 0x80060000,
154 .rootfs_ofs
= 0x140000,
156 .id
= "TL-WR741NDv1",
157 .hw_id
= HWID_TL_WR741ND_V1
,
159 .fw_max_len
= 0x3c0000,
160 .kernel_la
= 0x80060000,
161 .kernel_ep
= 0x80060000,
162 .rootfs_ofs
= 0x140000,
165 .hw_id
= HWID_TL_WR740N_V1
,
167 .fw_max_len
= 0x3c0000,
168 .kernel_la
= 0x80060000,
169 .kernel_ep
= 0x80060000,
170 .rootfs_ofs
= 0x140000,
172 .id
= "TL-WR841Nv1.5",
173 .hw_id
= HWID_TL_WR841N_V1_5
,
175 .fw_max_len
= 0x3c0000,
176 .kernel_la
= 0x80060000,
177 .kernel_ep
= 0x80060000,
178 .rootfs_ofs
= 0x140000,
180 .id
= "TL-WR841NDv3",
181 .hw_id
= HWID_TL_WR841ND_V3
,
183 .fw_max_len
= 0x3c0000,
184 .kernel_la
= 0x80060000,
185 .kernel_ep
= 0x80060000,
186 .rootfs_ofs
= 0x140000,
188 .id
= "TL-WR841NDv5",
189 .hw_id
= HWID_TL_WR841ND_V5
,
191 .fw_max_len
= 0x3c0000,
192 .kernel_la
= 0x80060000,
193 .kernel_ep
= 0x80060000,
194 .rootfs_ofs
= 0x140000,
196 .id
= "TL-WR841NDv7",
197 .hw_id
= HWID_TL_WR841ND_V7
,
199 .fw_max_len
= 0x3c0000,
200 .kernel_la
= 0x80060000,
201 .kernel_ep
= 0x80060000,
202 .rootfs_ofs
= 0x140000,
204 .id
= "TL-WR941NDv2",
205 .hw_id
= HWID_TL_WR941ND_V2
,
207 .fw_max_len
= 0x3c0000,
208 .kernel_la
= 0x80060000,
209 .kernel_ep
= 0x80060000,
210 .rootfs_ofs
= 0x140000,
212 .id
= "TL-WR941NDv4",
213 .hw_id
= HWID_TL_WR941ND_V4
,
215 .fw_max_len
= 0x3c0000,
216 .kernel_la
= 0x80060000,
217 .kernel_ep
= 0x80060000,
218 .rootfs_ofs
= 0x140000,
220 .id
= "TL-WR1043NDv1",
221 .hw_id
= HWID_TL_WR1043ND_V1
,
223 .fw_max_len
= 0x7c0000,
224 .kernel_la
= 0x80060000,
225 .kernel_ep
= 0x80060000,
226 .rootfs_ofs
= 0x140000,
228 /* terminating entry */
235 #define ERR(fmt, ...) do { \
237 fprintf(stderr, "[%s] *** error: " fmt "\n", \
238 progname, ## __VA_ARGS__ ); \
241 #define ERRS(fmt, ...) do { \
244 fprintf(stderr, "[%s] *** error: " fmt "\n", \
245 progname, ## __VA_ARGS__, strerror(save)); \
248 #define DBG(fmt, ...) do { \
249 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
252 static struct board_info
*find_board(char *id
)
254 struct board_info
*ret
;
255 struct board_info
*board
;
258 for (board
= boards
; board
->id
!= NULL
; board
++){
259 if (strcasecmp(id
, board
->id
) == 0) {
268 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
270 struct board_info
*board
;
272 for (board
= boards
; board
->id
!= NULL
; board
++) {
273 if (hw_id
== board
->hw_id
)
281 static void usage(int status
)
283 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
284 struct board_info
*board
;
286 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
290 " -B <board> create image for the board specified with <board>\n"
291 " -c use combined kernel image\n"
292 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
293 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
294 " -k <file> read kernel image from the file <file>\n"
295 " -r <file> read rootfs image from the file <file>\n"
296 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
297 " -o <file> write output to the file <file>\n"
298 " -s strip padding from the end of the image\n"
299 " -N <vendor> set image vendor to <vendor>\n"
300 " -V <version> set image version to <version>\n"
301 " -i <file> inspect given firmware file <file>\n"
302 " -x extract kernel and rootfs while inspecting (requires -i)\n"
303 " -h show this screen\n"
309 static int get_md5(char *data
, int size
, char *md5
)
314 MD5_Update(&ctx
, data
, size
);
315 MD5_Final(md5
, &ctx
);
318 static int get_file_stat(struct file_info
*fdata
)
323 if (fdata
->file_name
== NULL
)
326 res
= stat(fdata
->file_name
, &st
);
328 ERRS("stat failed on %s", fdata
->file_name
);
332 fdata
->file_size
= st
.st_size
;
336 static int read_to_buf(struct file_info
*fdata
, char *buf
)
339 int ret
= EXIT_FAILURE
;
341 f
= fopen(fdata
->file_name
, "r");
343 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
348 fread(buf
, fdata
->file_size
, 1, f
);
350 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
362 static int check_options(void)
366 if (inspect_info
.file_name
) {
367 ret
= get_file_stat(&inspect_info
);
372 } else if (extract
) {
373 ERR("no firmware for inspection specified");
377 if (board_id
== NULL
) {
378 ERR("no board specified");
382 board
= find_board(board_id
);
384 ERR("unknown/unsupported board id \"%s\"", board_id
);
388 kernel_la
= board
->kernel_la
;
390 kernel_ep
= board
->kernel_ep
;
392 rootfs_ofs
= board
->rootfs_ofs
;
394 if (kernel_info
.file_name
== NULL
) {
395 ERR("no kernel image specified");
399 ret
= get_file_stat(&kernel_info
);
404 if (kernel_info
.file_size
>
405 board
->fw_max_len
- sizeof(struct fw_header
)) {
406 ERR("kernel image is too big");
410 if (kernel_info
.file_size
>
411 rootfs_ofs
- sizeof(struct fw_header
)) {
412 ERR("kernel image is too big");
415 if (rootfs_info
.file_name
== NULL
) {
416 ERR("no rootfs image specified");
420 ret
= get_file_stat(&rootfs_info
);
424 if (rootfs_info
.file_size
>
425 (board
->fw_max_len
- rootfs_ofs
)) {
426 ERR("rootfs image is too big");
431 if (ofname
== NULL
) {
432 ERR("no output file specified");
439 static void fill_header(char *buf
, int len
)
441 struct fw_header
*hdr
= (struct fw_header
*)buf
;
443 memset(hdr
, 0, sizeof(struct fw_header
));
445 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
446 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
447 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
448 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
449 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
451 if (boot_info
.file_size
== 0)
452 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
454 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
456 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
457 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
458 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
459 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
460 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
462 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
463 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
466 get_md5(buf
, len
, hdr
->md5sum1
);
469 static int write_fw(char *data
, int len
)
472 int ret
= EXIT_FAILURE
;
474 f
= fopen(ofname
, "w");
476 ERRS("could not open \"%s\" for writing", ofname
);
481 fwrite(data
, len
, 1, f
);
483 ERRS("unable to write output file");
487 DBG("firmware file \"%s\" completed", ofname
);
494 if (ret
!= EXIT_SUCCESS
) {
501 static int build_fw(void)
506 int ret
= EXIT_FAILURE
;
509 buflen
= board
->fw_max_len
;
511 buf
= malloc(buflen
);
513 ERR("no memory for buffer\n");
517 memset(buf
, 0xff, buflen
);
518 p
= buf
+ sizeof(struct fw_header
);
519 ret
= read_to_buf(&kernel_info
, p
);
523 writelen
= kernel_info
.file_size
;
526 p
= buf
+ rootfs_ofs
;
527 ret
= read_to_buf(&rootfs_info
, p
);
531 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
537 fill_header(buf
, writelen
);
538 ret
= write_fw(buf
, writelen
);
550 /* Helper functions to inspect_fw() representing different output formats */
551 static inline void inspect_fw_pstr(char *label
, char *str
)
553 printf("%-23s: %s\n", label
, str
);
556 static inline void inspect_fw_phex(char *label
, uint32_t val
)
558 printf("%-23s: 0x%08x\n", label
, val
);
561 static inline void inspect_fw_phexpost(char *label
,
562 uint32_t val
, char *post
)
564 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
567 static inline void inspect_fw_phexdef(char *label
,
568 uint32_t val
, uint32_t defval
)
570 printf("%-23s: 0x%08x ", label
, val
);
573 printf("(== OpenWrt default)\n");
575 printf("(OpenWrt default: 0x%08x)\n", defval
);
578 static inline void inspect_fw_phexexp(char *label
,
579 uint32_t val
, uint32_t expval
)
581 printf("%-23s: 0x%08x ", label
, val
);
586 printf("(expected: 0x%08x)\n", expval
);
589 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
591 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
594 static inline void inspect_fw_phexdecdef(char *label
,
595 uint32_t val
, uint32_t defval
)
597 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
600 printf("(== OpenWrt default)\n");
602 printf("(OpenWrt default: 0x%08x)\n", defval
);
605 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
609 printf("%-23s:", label
);
610 for (i
=0; i
<MD5SUM_LEN
; i
++)
611 printf(" %02x", val
[i
]);
612 printf(" %s\n", text
);
615 static int inspect_fw(void)
618 struct fw_header
*hdr
;
619 uint8_t md5sum
[MD5SUM_LEN
];
620 struct board_info
*board
;
621 int ret
= EXIT_FAILURE
;
623 buf
= malloc(inspect_info
.file_size
);
625 ERR("no memory for buffer!\n");
629 ret
= read_to_buf(&inspect_info
, buf
);
632 hdr
= (struct fw_header
*)buf
;
634 inspect_fw_pstr("File name", inspect_info
.file_name
);
635 inspect_fw_phexdec("File size", inspect_info
.file_size
);
637 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
638 ERR("file does not seem to have V1 header!\n");
642 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
644 if (BE32_TO_HOST(hdr
->unk1
) != 0)
645 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
647 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
648 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
649 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
651 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
652 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
654 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
655 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
656 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
658 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
660 if (BE32_TO_HOST(hdr
->unk2
) != 0)
661 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
662 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
663 "(purpose yet unknown, unchecked here)");
664 if (BE32_TO_HOST(hdr
->unk3
) != 0)
665 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
669 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
670 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
671 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
673 inspect_fw_phexpost("Hardware ID",
674 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
675 inspect_fw_phexexp("Hardware Revision",
676 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
678 inspect_fw_phexpost("Hardware ID",
679 BE32_TO_HOST(hdr
->hw_id
), "unknown");
680 inspect_fw_phex("Hardware Revision",
681 BE32_TO_HOST(hdr
->hw_rev
));
686 inspect_fw_phexdec("Kernel data offset",
687 BE32_TO_HOST(hdr
->kernel_ofs
));
688 inspect_fw_phexdec("Kernel data length",
689 BE32_TO_HOST(hdr
->kernel_len
));
691 inspect_fw_phexdef("Kernel load address",
692 BE32_TO_HOST(hdr
->kernel_la
),
694 inspect_fw_phexdef("Kernel entry point",
695 BE32_TO_HOST(hdr
->kernel_ep
),
697 inspect_fw_phexdecdef("Rootfs data offset",
698 BE32_TO_HOST(hdr
->rootfs_ofs
),
701 inspect_fw_phex("Kernel load address",
702 BE32_TO_HOST(hdr
->kernel_la
));
703 inspect_fw_phex("Kernel entry point",
704 BE32_TO_HOST(hdr
->kernel_ep
));
705 inspect_fw_phexdec("Rootfs data offset",
706 BE32_TO_HOST(hdr
->rootfs_ofs
));
708 inspect_fw_phexdec("Rootfs data length",
709 BE32_TO_HOST(hdr
->rootfs_len
));
710 inspect_fw_phexdec("Boot loader data offset",
711 BE32_TO_HOST(hdr
->boot_ofs
));
712 inspect_fw_phexdec("Boot loader data length",
713 BE32_TO_HOST(hdr
->boot_len
));
714 inspect_fw_phexdec("Total firmware length",
715 BE32_TO_HOST(hdr
->fw_length
));
723 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
724 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
725 printf("Extracting kernel to \"%s\"...\n", filename
);
726 fp
= fopen(filename
, "w");
728 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
729 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
730 ERR("error in fwrite(): %s", strerror(errno
));
734 ERR("error in fopen(): %s", strerror(errno
));
738 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
739 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
740 printf("Extracting rootfs to \"%s\"...\n", filename
);
741 fp
= fopen(filename
, "w");
743 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
744 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
745 ERR("error in fwrite(): %s", strerror(errno
));
749 ERR("error in fopen(): %s", strerror(errno
));
760 int main(int argc
, char *argv
[])
762 int ret
= EXIT_FAILURE
;
767 progname
= basename(argv
[0]);
772 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
781 sscanf(optarg
, "0x%x", &kernel_ep
);
784 sscanf(optarg
, "0x%x", &kernel_la
);
796 kernel_info
.file_name
= optarg
;
799 rootfs_info
.file_name
= optarg
;
802 sscanf(optarg
, "0x%x", &rootfs_ofs
);
811 inspect_info
.file_name
= optarg
;
825 ret
= check_options();
829 if (!inspect_info
.file_name
)
This page took 0.113781 seconds and 5 git commands to generate.