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_WR740N_V3 0x07400300
43 #define HWID_TL_WR743ND_V1 0x07430001
44 #define HWID_TL_WR841N_V1_5 0x08410002
45 #define HWID_TL_WR841ND_V3 0x08410003
46 #define HWID_TL_WR841ND_V5 0x08410005
47 #define HWID_TL_WR841ND_V7 0x08410007
48 #define HWID_TL_WR941ND_V2 0x09410002
49 #define HWID_TL_WR941ND_V4 0x09410004
50 #define HWID_TL_WR1043ND_V1 0x10430001
55 char *file_name
; /* name of the file */
56 uint32_t file_size
; /* length of the file */
60 uint32_t version
; /* header version */
63 uint32_t hw_id
; /* hardware id */
64 uint32_t hw_rev
; /* hardware revision */
66 uint8_t md5sum1
[MD5SUM_LEN
];
68 uint8_t md5sum2
[MD5SUM_LEN
];
70 uint32_t kernel_la
; /* kernel load address */
71 uint32_t kernel_ep
; /* kernel entry point */
72 uint32_t fw_length
; /* total length of the firmware */
73 uint32_t kernel_ofs
; /* kernel data offset */
74 uint32_t kernel_len
; /* kernel data length */
75 uint32_t rootfs_ofs
; /* rootfs data offset */
76 uint32_t rootfs_len
; /* rootfs data length */
77 uint32_t boot_ofs
; /* bootloader data offset */
78 uint32_t boot_len
; /* bootloader data length */
80 } __attribute__ ((packed
));
96 static char *progname
;
97 static char *vendor
= "TP-LINK Technologies";
98 static char *version
= "ver. 1.0";
100 static char *board_id
;
101 static struct board_info
*board
;
102 static struct file_info kernel_info
;
103 static uint32_t kernel_la
= 0;
104 static uint32_t kernel_ep
= 0;
105 static struct file_info rootfs_info
;
106 static uint32_t rootfs_ofs
= 0;
107 static struct file_info boot_info
;
109 static int strip_padding
;
111 static struct file_info inspect_info
;
112 static int extract
= 0;
114 char md5salt_normal
[MD5SUM_LEN
] = {
115 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
116 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
119 char md5salt_boot
[MD5SUM_LEN
] = {
120 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
121 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
124 static struct board_info boards
[] = {
127 .hw_id
= HWID_TL_MR3220_V1
,
129 .fw_max_len
= 0x3c0000,
130 .kernel_la
= 0x80060000,
131 .kernel_ep
= 0x80060000,
132 .rootfs_ofs
= 0x140000,
135 .hw_id
= HWID_TL_MR3420_V1
,
137 .fw_max_len
= 0x3c0000,
138 .kernel_la
= 0x80060000,
139 .kernel_ep
= 0x80060000,
140 .rootfs_ofs
= 0x140000,
142 .id
= "TL-WA901NDv1",
143 .hw_id
= HWID_TL_WA901ND_V1
,
145 .fw_max_len
= 0x3c0000,
146 .kernel_la
= 0x80060000,
147 .kernel_ep
= 0x80060000,
148 .rootfs_ofs
= 0x140000,
150 .id
= "TL-WA901NDv2",
151 .hw_id
= HWID_TL_WA901ND_V2
,
153 .fw_max_len
= 0x3c0000,
154 .kernel_la
= 0x80060000,
155 .kernel_ep
= 0x80060000,
156 .rootfs_ofs
= 0x140000,
158 .id
= "TL-WR741NDv1",
159 .hw_id
= HWID_TL_WR741ND_V1
,
161 .fw_max_len
= 0x3c0000,
162 .kernel_la
= 0x80060000,
163 .kernel_ep
= 0x80060000,
164 .rootfs_ofs
= 0x140000,
167 .hw_id
= HWID_TL_WR740N_V1
,
169 .fw_max_len
= 0x3c0000,
170 .kernel_la
= 0x80060000,
171 .kernel_ep
= 0x80060000,
172 .rootfs_ofs
= 0x140000,
175 .hw_id
= HWID_TL_WR740N_V3
,
177 .fw_max_len
= 0x3c0000,
178 .kernel_la
= 0x80060000,
179 .kernel_ep
= 0x80060000,
180 .rootfs_ofs
= 0x140000,
182 .id
= "TL-WR743NDv1",
183 .hw_id
= HWID_TL_WR743ND_V1
,
185 .fw_max_len
= 0x3c0000,
186 .kernel_la
= 0x80060000,
187 .kernel_ep
= 0x80060000,
188 .rootfs_ofs
= 0x140000,
190 .id
= "TL-WR841Nv1.5",
191 .hw_id
= HWID_TL_WR841N_V1_5
,
193 .fw_max_len
= 0x3c0000,
194 .kernel_la
= 0x80060000,
195 .kernel_ep
= 0x80060000,
196 .rootfs_ofs
= 0x140000,
198 .id
= "TL-WR841NDv3",
199 .hw_id
= HWID_TL_WR841ND_V3
,
201 .fw_max_len
= 0x3c0000,
202 .kernel_la
= 0x80060000,
203 .kernel_ep
= 0x80060000,
204 .rootfs_ofs
= 0x140000,
206 .id
= "TL-WR841NDv5",
207 .hw_id
= HWID_TL_WR841ND_V5
,
209 .fw_max_len
= 0x3c0000,
210 .kernel_la
= 0x80060000,
211 .kernel_ep
= 0x80060000,
212 .rootfs_ofs
= 0x140000,
214 .id
= "TL-WR841NDv7",
215 .hw_id
= HWID_TL_WR841ND_V7
,
217 .fw_max_len
= 0x3c0000,
218 .kernel_la
= 0x80060000,
219 .kernel_ep
= 0x80060000,
220 .rootfs_ofs
= 0x140000,
222 .id
= "TL-WR941NDv2",
223 .hw_id
= HWID_TL_WR941ND_V2
,
225 .fw_max_len
= 0x3c0000,
226 .kernel_la
= 0x80060000,
227 .kernel_ep
= 0x80060000,
228 .rootfs_ofs
= 0x140000,
230 .id
= "TL-WR941NDv4",
231 .hw_id
= HWID_TL_WR941ND_V4
,
233 .fw_max_len
= 0x3c0000,
234 .kernel_la
= 0x80060000,
235 .kernel_ep
= 0x80060000,
236 .rootfs_ofs
= 0x140000,
238 .id
= "TL-WR1043NDv1",
239 .hw_id
= HWID_TL_WR1043ND_V1
,
241 .fw_max_len
= 0x7c0000,
242 .kernel_la
= 0x80060000,
243 .kernel_ep
= 0x80060000,
244 .rootfs_ofs
= 0x140000,
246 /* terminating entry */
253 #define ERR(fmt, ...) do { \
255 fprintf(stderr, "[%s] *** error: " fmt "\n", \
256 progname, ## __VA_ARGS__ ); \
259 #define ERRS(fmt, ...) do { \
262 fprintf(stderr, "[%s] *** error: " fmt "\n", \
263 progname, ## __VA_ARGS__, strerror(save)); \
266 #define DBG(fmt, ...) do { \
267 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
270 static struct board_info
*find_board(char *id
)
272 struct board_info
*ret
;
273 struct board_info
*board
;
276 for (board
= boards
; board
->id
!= NULL
; board
++){
277 if (strcasecmp(id
, board
->id
) == 0) {
286 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
288 struct board_info
*board
;
290 for (board
= boards
; board
->id
!= NULL
; board
++) {
291 if (hw_id
== board
->hw_id
)
299 static void usage(int status
)
301 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
302 struct board_info
*board
;
304 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
308 " -B <board> create image for the board specified with <board>\n"
309 " -c use combined kernel image\n"
310 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
311 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
312 " -k <file> read kernel image from the file <file>\n"
313 " -r <file> read rootfs image from the file <file>\n"
314 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
315 " -o <file> write output to the file <file>\n"
316 " -s strip padding from the end of the image\n"
317 " -N <vendor> set image vendor to <vendor>\n"
318 " -V <version> set image version to <version>\n"
319 " -i <file> inspect given firmware file <file>\n"
320 " -x extract kernel and rootfs while inspecting (requires -i)\n"
321 " -h show this screen\n"
327 static int get_md5(char *data
, int size
, char *md5
)
332 MD5_Update(&ctx
, data
, size
);
333 MD5_Final(md5
, &ctx
);
336 static int get_file_stat(struct file_info
*fdata
)
341 if (fdata
->file_name
== NULL
)
344 res
= stat(fdata
->file_name
, &st
);
346 ERRS("stat failed on %s", fdata
->file_name
);
350 fdata
->file_size
= st
.st_size
;
354 static int read_to_buf(struct file_info
*fdata
, char *buf
)
357 int ret
= EXIT_FAILURE
;
359 f
= fopen(fdata
->file_name
, "r");
361 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
366 fread(buf
, fdata
->file_size
, 1, f
);
368 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
380 static int check_options(void)
384 if (inspect_info
.file_name
) {
385 ret
= get_file_stat(&inspect_info
);
390 } else if (extract
) {
391 ERR("no firmware for inspection specified");
395 if (board_id
== NULL
) {
396 ERR("no board specified");
400 board
= find_board(board_id
);
402 ERR("unknown/unsupported board id \"%s\"", board_id
);
406 kernel_la
= board
->kernel_la
;
408 kernel_ep
= board
->kernel_ep
;
410 rootfs_ofs
= board
->rootfs_ofs
;
412 if (kernel_info
.file_name
== NULL
) {
413 ERR("no kernel image specified");
417 ret
= get_file_stat(&kernel_info
);
422 if (kernel_info
.file_size
>
423 board
->fw_max_len
- sizeof(struct fw_header
)) {
424 ERR("kernel image is too big");
428 if (kernel_info
.file_size
>
429 rootfs_ofs
- sizeof(struct fw_header
)) {
430 ERR("kernel image is too big");
433 if (rootfs_info
.file_name
== NULL
) {
434 ERR("no rootfs image specified");
438 ret
= get_file_stat(&rootfs_info
);
442 if (rootfs_info
.file_size
>
443 (board
->fw_max_len
- rootfs_ofs
)) {
444 ERR("rootfs image is too big");
449 if (ofname
== NULL
) {
450 ERR("no output file specified");
457 static void fill_header(char *buf
, int len
)
459 struct fw_header
*hdr
= (struct fw_header
*)buf
;
461 memset(hdr
, 0, sizeof(struct fw_header
));
463 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
464 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
465 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
466 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
467 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
469 if (boot_info
.file_size
== 0)
470 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
472 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
474 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
475 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
476 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
477 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
478 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
480 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
481 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
484 get_md5(buf
, len
, hdr
->md5sum1
);
487 static int write_fw(char *data
, int len
)
490 int ret
= EXIT_FAILURE
;
492 f
= fopen(ofname
, "w");
494 ERRS("could not open \"%s\" for writing", ofname
);
499 fwrite(data
, len
, 1, f
);
501 ERRS("unable to write output file");
505 DBG("firmware file \"%s\" completed", ofname
);
512 if (ret
!= EXIT_SUCCESS
) {
519 static int build_fw(void)
524 int ret
= EXIT_FAILURE
;
527 buflen
= board
->fw_max_len
;
529 buf
= malloc(buflen
);
531 ERR("no memory for buffer\n");
535 memset(buf
, 0xff, buflen
);
536 p
= buf
+ sizeof(struct fw_header
);
537 ret
= read_to_buf(&kernel_info
, p
);
541 writelen
= kernel_info
.file_size
;
544 p
= buf
+ rootfs_ofs
;
545 ret
= read_to_buf(&rootfs_info
, p
);
549 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
555 fill_header(buf
, writelen
);
556 ret
= write_fw(buf
, writelen
);
568 /* Helper functions to inspect_fw() representing different output formats */
569 static inline void inspect_fw_pstr(char *label
, char *str
)
571 printf("%-23s: %s\n", label
, str
);
574 static inline void inspect_fw_phex(char *label
, uint32_t val
)
576 printf("%-23s: 0x%08x\n", label
, val
);
579 static inline void inspect_fw_phexpost(char *label
,
580 uint32_t val
, char *post
)
582 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
585 static inline void inspect_fw_phexdef(char *label
,
586 uint32_t val
, uint32_t defval
)
588 printf("%-23s: 0x%08x ", label
, val
);
591 printf("(== OpenWrt default)\n");
593 printf("(OpenWrt default: 0x%08x)\n", defval
);
596 static inline void inspect_fw_phexexp(char *label
,
597 uint32_t val
, uint32_t expval
)
599 printf("%-23s: 0x%08x ", label
, val
);
604 printf("(expected: 0x%08x)\n", expval
);
607 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
609 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
612 static inline void inspect_fw_phexdecdef(char *label
,
613 uint32_t val
, uint32_t defval
)
615 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
618 printf("(== OpenWrt default)\n");
620 printf("(OpenWrt default: 0x%08x)\n", defval
);
623 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
627 printf("%-23s:", label
);
628 for (i
=0; i
<MD5SUM_LEN
; i
++)
629 printf(" %02x", val
[i
]);
630 printf(" %s\n", text
);
633 static int inspect_fw(void)
636 struct fw_header
*hdr
;
637 uint8_t md5sum
[MD5SUM_LEN
];
638 struct board_info
*board
;
639 int ret
= EXIT_FAILURE
;
641 buf
= malloc(inspect_info
.file_size
);
643 ERR("no memory for buffer!\n");
647 ret
= read_to_buf(&inspect_info
, buf
);
650 hdr
= (struct fw_header
*)buf
;
652 inspect_fw_pstr("File name", inspect_info
.file_name
);
653 inspect_fw_phexdec("File size", inspect_info
.file_size
);
655 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
656 ERR("file does not seem to have V1 header!\n");
660 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
662 if (BE32_TO_HOST(hdr
->unk1
) != 0)
663 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
665 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
666 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
667 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
669 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
670 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
672 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
673 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
674 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
676 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
678 if (BE32_TO_HOST(hdr
->unk2
) != 0)
679 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
680 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
681 "(purpose yet unknown, unchecked here)");
682 if (BE32_TO_HOST(hdr
->unk3
) != 0)
683 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
687 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
688 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
689 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
691 inspect_fw_phexpost("Hardware ID",
692 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
693 inspect_fw_phexexp("Hardware Revision",
694 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
696 inspect_fw_phexpost("Hardware ID",
697 BE32_TO_HOST(hdr
->hw_id
), "unknown");
698 inspect_fw_phex("Hardware Revision",
699 BE32_TO_HOST(hdr
->hw_rev
));
704 inspect_fw_phexdec("Kernel data offset",
705 BE32_TO_HOST(hdr
->kernel_ofs
));
706 inspect_fw_phexdec("Kernel data length",
707 BE32_TO_HOST(hdr
->kernel_len
));
709 inspect_fw_phexdef("Kernel load address",
710 BE32_TO_HOST(hdr
->kernel_la
),
712 inspect_fw_phexdef("Kernel entry point",
713 BE32_TO_HOST(hdr
->kernel_ep
),
715 inspect_fw_phexdecdef("Rootfs data offset",
716 BE32_TO_HOST(hdr
->rootfs_ofs
),
719 inspect_fw_phex("Kernel load address",
720 BE32_TO_HOST(hdr
->kernel_la
));
721 inspect_fw_phex("Kernel entry point",
722 BE32_TO_HOST(hdr
->kernel_ep
));
723 inspect_fw_phexdec("Rootfs data offset",
724 BE32_TO_HOST(hdr
->rootfs_ofs
));
726 inspect_fw_phexdec("Rootfs data length",
727 BE32_TO_HOST(hdr
->rootfs_len
));
728 inspect_fw_phexdec("Boot loader data offset",
729 BE32_TO_HOST(hdr
->boot_ofs
));
730 inspect_fw_phexdec("Boot loader data length",
731 BE32_TO_HOST(hdr
->boot_len
));
732 inspect_fw_phexdec("Total firmware length",
733 BE32_TO_HOST(hdr
->fw_length
));
741 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
742 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
743 printf("Extracting kernel to \"%s\"...\n", filename
);
744 fp
= fopen(filename
, "w");
746 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
747 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
748 ERR("error in fwrite(): %s", strerror(errno
));
752 ERR("error in fopen(): %s", strerror(errno
));
756 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
757 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
758 printf("Extracting rootfs to \"%s\"...\n", filename
);
759 fp
= fopen(filename
, "w");
761 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
762 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
763 ERR("error in fwrite(): %s", strerror(errno
));
767 ERR("error in fopen(): %s", strerror(errno
));
778 int main(int argc
, char *argv
[])
780 int ret
= EXIT_FAILURE
;
785 progname
= basename(argv
[0]);
790 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
799 sscanf(optarg
, "0x%x", &kernel_ep
);
802 sscanf(optarg
, "0x%x", &kernel_la
);
814 kernel_info
.file_name
= optarg
;
817 rootfs_info
.file_name
= optarg
;
820 sscanf(optarg
, "0x%x", &rootfs_ofs
);
829 inspect_info
.file_name
= optarg
;
843 ret
= check_options();
847 if (!inspect_info
.file_name
)