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_WR703N_V1 0x07030101
41 #define HWID_TL_WR741ND_V1 0x07410001
42 #define HWID_TL_WR740N_V1 0x07400001
43 #define HWID_TL_WR740N_V3 0x07400003
44 #define HWID_TL_WR743ND_V1 0x07430001
45 #define HWID_TL_WR841N_V1_5 0x08410002
46 #define HWID_TL_WR841ND_V3 0x08410003
47 #define HWID_TL_WR841ND_V5 0x08410005
48 #define HWID_TL_WR841ND_V7 0x08410007
49 #define HWID_TL_WR941ND_V2 0x09410002
50 #define HWID_TL_WR941ND_V4 0x09410004
51 #define HWID_TL_WR1043ND_V1 0x10430001
56 char *file_name
; /* name of the file */
57 uint32_t file_size
; /* length of the file */
61 uint32_t version
; /* header version */
64 uint32_t hw_id
; /* hardware id */
65 uint32_t hw_rev
; /* hardware revision */
67 uint8_t md5sum1
[MD5SUM_LEN
];
69 uint8_t md5sum2
[MD5SUM_LEN
];
71 uint32_t kernel_la
; /* kernel load address */
72 uint32_t kernel_ep
; /* kernel entry point */
73 uint32_t fw_length
; /* total length of the firmware */
74 uint32_t kernel_ofs
; /* kernel data offset */
75 uint32_t kernel_len
; /* kernel data length */
76 uint32_t rootfs_ofs
; /* rootfs data offset */
77 uint32_t rootfs_len
; /* rootfs data length */
78 uint32_t boot_ofs
; /* bootloader data offset */
79 uint32_t boot_len
; /* bootloader data length */
81 } __attribute__ ((packed
));
97 static char *progname
;
98 static char *vendor
= "TP-LINK Technologies";
99 static char *version
= "ver. 1.0";
101 static char *board_id
;
102 static struct board_info
*board
;
103 static struct file_info kernel_info
;
104 static uint32_t kernel_la
= 0;
105 static uint32_t kernel_ep
= 0;
106 static struct file_info rootfs_info
;
107 static uint32_t rootfs_ofs
= 0;
108 static struct file_info boot_info
;
110 static int strip_padding
;
112 static struct file_info inspect_info
;
113 static int extract
= 0;
115 char md5salt_normal
[MD5SUM_LEN
] = {
116 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
117 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
120 char md5salt_boot
[MD5SUM_LEN
] = {
121 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
122 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
125 static struct board_info boards
[] = {
128 .hw_id
= HWID_TL_MR3220_V1
,
130 .fw_max_len
= 0x3c0000,
131 .kernel_la
= 0x80060000,
132 .kernel_ep
= 0x80060000,
133 .rootfs_ofs
= 0x140000,
136 .hw_id
= HWID_TL_MR3420_V1
,
138 .fw_max_len
= 0x3c0000,
139 .kernel_la
= 0x80060000,
140 .kernel_ep
= 0x80060000,
141 .rootfs_ofs
= 0x140000,
143 .id
= "TL-WA901NDv1",
144 .hw_id
= HWID_TL_WA901ND_V1
,
146 .fw_max_len
= 0x3c0000,
147 .kernel_la
= 0x80060000,
148 .kernel_ep
= 0x80060000,
149 .rootfs_ofs
= 0x140000,
151 .id
= "TL-WA901NDv2",
152 .hw_id
= HWID_TL_WA901ND_V2
,
154 .fw_max_len
= 0x3c0000,
155 .kernel_la
= 0x80060000,
156 .kernel_ep
= 0x80060000,
157 .rootfs_ofs
= 0x140000,
159 .id
= "TL-WR741NDv1",
160 .hw_id
= HWID_TL_WR741ND_V1
,
162 .fw_max_len
= 0x3c0000,
163 .kernel_la
= 0x80060000,
164 .kernel_ep
= 0x80060000,
165 .rootfs_ofs
= 0x140000,
168 .hw_id
= HWID_TL_WR740N_V1
,
170 .fw_max_len
= 0x3c0000,
171 .kernel_la
= 0x80060000,
172 .kernel_ep
= 0x80060000,
173 .rootfs_ofs
= 0x140000,
176 .hw_id
= HWID_TL_WR740N_V3
,
178 .fw_max_len
= 0x3c0000,
179 .kernel_la
= 0x80060000,
180 .kernel_ep
= 0x80060000,
181 .rootfs_ofs
= 0x140000,
183 .id
= "TL-WR743NDv1",
184 .hw_id
= HWID_TL_WR743ND_V1
,
186 .fw_max_len
= 0x3c0000,
187 .kernel_la
= 0x80060000,
188 .kernel_ep
= 0x80060000,
189 .rootfs_ofs
= 0x140000,
191 .id
= "TL-WR841Nv1.5",
192 .hw_id
= HWID_TL_WR841N_V1_5
,
194 .fw_max_len
= 0x3c0000,
195 .kernel_la
= 0x80060000,
196 .kernel_ep
= 0x80060000,
197 .rootfs_ofs
= 0x140000,
199 .id
= "TL-WR841NDv3",
200 .hw_id
= HWID_TL_WR841ND_V3
,
202 .fw_max_len
= 0x3c0000,
203 .kernel_la
= 0x80060000,
204 .kernel_ep
= 0x80060000,
205 .rootfs_ofs
= 0x140000,
207 .id
= "TL-WR841NDv5",
208 .hw_id
= HWID_TL_WR841ND_V5
,
210 .fw_max_len
= 0x3c0000,
211 .kernel_la
= 0x80060000,
212 .kernel_ep
= 0x80060000,
213 .rootfs_ofs
= 0x140000,
215 .id
= "TL-WR841NDv7",
216 .hw_id
= HWID_TL_WR841ND_V7
,
218 .fw_max_len
= 0x3c0000,
219 .kernel_la
= 0x80060000,
220 .kernel_ep
= 0x80060000,
221 .rootfs_ofs
= 0x140000,
223 .id
= "TL-WR941NDv2",
224 .hw_id
= HWID_TL_WR941ND_V2
,
226 .fw_max_len
= 0x3c0000,
227 .kernel_la
= 0x80060000,
228 .kernel_ep
= 0x80060000,
229 .rootfs_ofs
= 0x140000,
231 .id
= "TL-WR941NDv4",
232 .hw_id
= HWID_TL_WR941ND_V4
,
234 .fw_max_len
= 0x3c0000,
235 .kernel_la
= 0x80060000,
236 .kernel_ep
= 0x80060000,
237 .rootfs_ofs
= 0x140000,
239 .id
= "TL-WR1043NDv1",
240 .hw_id
= HWID_TL_WR1043ND_V1
,
242 .fw_max_len
= 0x7c0000,
243 .kernel_la
= 0x80060000,
244 .kernel_ep
= 0x80060000,
245 .rootfs_ofs
= 0x140000,
248 .hw_id
= HWID_TL_WR703N_V1
,
250 .fw_max_len
= 0x3c0000,
251 .kernel_la
= 0x80060000,
252 .kernel_ep
= 0x80060000,
253 .rootfs_ofs
= 0x100000,
255 /* terminating entry */
262 #define ERR(fmt, ...) do { \
264 fprintf(stderr, "[%s] *** error: " fmt "\n", \
265 progname, ## __VA_ARGS__ ); \
268 #define ERRS(fmt, ...) do { \
271 fprintf(stderr, "[%s] *** error: " fmt "\n", \
272 progname, ## __VA_ARGS__, strerror(save)); \
275 #define DBG(fmt, ...) do { \
276 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
279 static struct board_info
*find_board(char *id
)
281 struct board_info
*ret
;
282 struct board_info
*board
;
285 for (board
= boards
; board
->id
!= NULL
; board
++){
286 if (strcasecmp(id
, board
->id
) == 0) {
295 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
297 struct board_info
*board
;
299 for (board
= boards
; board
->id
!= NULL
; board
++) {
300 if (hw_id
== board
->hw_id
)
308 static void usage(int status
)
310 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
311 struct board_info
*board
;
313 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
317 " -B <board> create image for the board specified with <board>\n"
318 " -c use combined kernel image\n"
319 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
320 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
321 " -k <file> read kernel image from the file <file>\n"
322 " -r <file> read rootfs image from the file <file>\n"
323 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
324 " -o <file> write output to the file <file>\n"
325 " -s strip padding from the end of the image\n"
326 " -N <vendor> set image vendor to <vendor>\n"
327 " -V <version> set image version to <version>\n"
328 " -i <file> inspect given firmware file <file>\n"
329 " -x extract kernel and rootfs while inspecting (requires -i)\n"
330 " -h show this screen\n"
336 static int get_md5(char *data
, int size
, char *md5
)
341 MD5_Update(&ctx
, data
, size
);
342 MD5_Final(md5
, &ctx
);
345 static int get_file_stat(struct file_info
*fdata
)
350 if (fdata
->file_name
== NULL
)
353 res
= stat(fdata
->file_name
, &st
);
355 ERRS("stat failed on %s", fdata
->file_name
);
359 fdata
->file_size
= st
.st_size
;
363 static int read_to_buf(struct file_info
*fdata
, char *buf
)
366 int ret
= EXIT_FAILURE
;
368 f
= fopen(fdata
->file_name
, "r");
370 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
375 fread(buf
, fdata
->file_size
, 1, f
);
377 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
389 static int check_options(void)
393 if (inspect_info
.file_name
) {
394 ret
= get_file_stat(&inspect_info
);
399 } else if (extract
) {
400 ERR("no firmware for inspection specified");
404 if (board_id
== NULL
) {
405 ERR("no board specified");
409 board
= find_board(board_id
);
411 ERR("unknown/unsupported board id \"%s\"", board_id
);
415 kernel_la
= board
->kernel_la
;
417 kernel_ep
= board
->kernel_ep
;
419 rootfs_ofs
= board
->rootfs_ofs
;
421 if (kernel_info
.file_name
== NULL
) {
422 ERR("no kernel image specified");
426 ret
= get_file_stat(&kernel_info
);
431 if (kernel_info
.file_size
>
432 board
->fw_max_len
- sizeof(struct fw_header
)) {
433 ERR("kernel image is too big");
437 if (kernel_info
.file_size
>
438 rootfs_ofs
- sizeof(struct fw_header
)) {
439 ERR("kernel image is too big");
442 if (rootfs_info
.file_name
== NULL
) {
443 ERR("no rootfs image specified");
447 ret
= get_file_stat(&rootfs_info
);
451 if (rootfs_info
.file_size
>
452 (board
->fw_max_len
- rootfs_ofs
)) {
453 ERR("rootfs image is too big");
458 if (ofname
== NULL
) {
459 ERR("no output file specified");
466 static void fill_header(char *buf
, int len
)
468 struct fw_header
*hdr
= (struct fw_header
*)buf
;
470 memset(hdr
, 0, sizeof(struct fw_header
));
472 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
473 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
474 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
475 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
476 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
478 if (boot_info
.file_size
== 0)
479 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
481 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
483 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
484 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
485 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
486 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
487 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
489 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
490 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
493 get_md5(buf
, len
, hdr
->md5sum1
);
496 static int write_fw(char *data
, int len
)
499 int ret
= EXIT_FAILURE
;
501 f
= fopen(ofname
, "w");
503 ERRS("could not open \"%s\" for writing", ofname
);
508 fwrite(data
, len
, 1, f
);
510 ERRS("unable to write output file");
514 DBG("firmware file \"%s\" completed", ofname
);
521 if (ret
!= EXIT_SUCCESS
) {
528 static int build_fw(void)
533 int ret
= EXIT_FAILURE
;
536 buflen
= board
->fw_max_len
;
538 buf
= malloc(buflen
);
540 ERR("no memory for buffer\n");
544 memset(buf
, 0xff, buflen
);
545 p
= buf
+ sizeof(struct fw_header
);
546 ret
= read_to_buf(&kernel_info
, p
);
550 writelen
= kernel_info
.file_size
;
553 p
= buf
+ rootfs_ofs
;
554 ret
= read_to_buf(&rootfs_info
, p
);
558 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
564 fill_header(buf
, writelen
);
565 ret
= write_fw(buf
, writelen
);
577 /* Helper functions to inspect_fw() representing different output formats */
578 static inline void inspect_fw_pstr(char *label
, char *str
)
580 printf("%-23s: %s\n", label
, str
);
583 static inline void inspect_fw_phex(char *label
, uint32_t val
)
585 printf("%-23s: 0x%08x\n", label
, val
);
588 static inline void inspect_fw_phexpost(char *label
,
589 uint32_t val
, char *post
)
591 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
594 static inline void inspect_fw_phexdef(char *label
,
595 uint32_t val
, uint32_t defval
)
597 printf("%-23s: 0x%08x ", label
, val
);
600 printf("(== OpenWrt default)\n");
602 printf("(OpenWrt default: 0x%08x)\n", defval
);
605 static inline void inspect_fw_phexexp(char *label
,
606 uint32_t val
, uint32_t expval
)
608 printf("%-23s: 0x%08x ", label
, val
);
613 printf("(expected: 0x%08x)\n", expval
);
616 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
618 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
621 static inline void inspect_fw_phexdecdef(char *label
,
622 uint32_t val
, uint32_t defval
)
624 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
627 printf("(== OpenWrt default)\n");
629 printf("(OpenWrt default: 0x%08x)\n", defval
);
632 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
636 printf("%-23s:", label
);
637 for (i
=0; i
<MD5SUM_LEN
; i
++)
638 printf(" %02x", val
[i
]);
639 printf(" %s\n", text
);
642 static int inspect_fw(void)
645 struct fw_header
*hdr
;
646 uint8_t md5sum
[MD5SUM_LEN
];
647 struct board_info
*board
;
648 int ret
= EXIT_FAILURE
;
650 buf
= malloc(inspect_info
.file_size
);
652 ERR("no memory for buffer!\n");
656 ret
= read_to_buf(&inspect_info
, buf
);
659 hdr
= (struct fw_header
*)buf
;
661 inspect_fw_pstr("File name", inspect_info
.file_name
);
662 inspect_fw_phexdec("File size", inspect_info
.file_size
);
664 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
665 ERR("file does not seem to have V1 header!\n");
669 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
671 if (BE32_TO_HOST(hdr
->unk1
) != 0)
672 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
674 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
675 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
676 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
678 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
679 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
681 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
682 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
683 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
685 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
687 if (BE32_TO_HOST(hdr
->unk2
) != 0)
688 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
689 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
690 "(purpose yet unknown, unchecked here)");
691 if (BE32_TO_HOST(hdr
->unk3
) != 0)
692 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
696 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
697 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
698 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
700 inspect_fw_phexpost("Hardware ID",
701 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
702 inspect_fw_phexexp("Hardware Revision",
703 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
705 inspect_fw_phexpost("Hardware ID",
706 BE32_TO_HOST(hdr
->hw_id
), "unknown");
707 inspect_fw_phex("Hardware Revision",
708 BE32_TO_HOST(hdr
->hw_rev
));
713 inspect_fw_phexdec("Kernel data offset",
714 BE32_TO_HOST(hdr
->kernel_ofs
));
715 inspect_fw_phexdec("Kernel data length",
716 BE32_TO_HOST(hdr
->kernel_len
));
718 inspect_fw_phexdef("Kernel load address",
719 BE32_TO_HOST(hdr
->kernel_la
),
721 inspect_fw_phexdef("Kernel entry point",
722 BE32_TO_HOST(hdr
->kernel_ep
),
724 inspect_fw_phexdecdef("Rootfs data offset",
725 BE32_TO_HOST(hdr
->rootfs_ofs
),
728 inspect_fw_phex("Kernel load address",
729 BE32_TO_HOST(hdr
->kernel_la
));
730 inspect_fw_phex("Kernel entry point",
731 BE32_TO_HOST(hdr
->kernel_ep
));
732 inspect_fw_phexdec("Rootfs data offset",
733 BE32_TO_HOST(hdr
->rootfs_ofs
));
735 inspect_fw_phexdec("Rootfs data length",
736 BE32_TO_HOST(hdr
->rootfs_len
));
737 inspect_fw_phexdec("Boot loader data offset",
738 BE32_TO_HOST(hdr
->boot_ofs
));
739 inspect_fw_phexdec("Boot loader data length",
740 BE32_TO_HOST(hdr
->boot_len
));
741 inspect_fw_phexdec("Total firmware length",
742 BE32_TO_HOST(hdr
->fw_length
));
750 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
751 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
752 printf("Extracting kernel to \"%s\"...\n", filename
);
753 fp
= fopen(filename
, "w");
755 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
756 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
757 ERR("error in fwrite(): %s", strerror(errno
));
761 ERR("error in fopen(): %s", strerror(errno
));
765 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
766 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
767 printf("Extracting rootfs to \"%s\"...\n", filename
);
768 fp
= fopen(filename
, "w");
770 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
771 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
772 ERR("error in fwrite(): %s", strerror(errno
));
776 ERR("error in fopen(): %s", strerror(errno
));
787 int main(int argc
, char *argv
[])
789 int ret
= EXIT_FAILURE
;
794 progname
= basename(argv
[0]);
799 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
808 sscanf(optarg
, "0x%x", &kernel_ep
);
811 sscanf(optarg
, "0x%x", &kernel_la
);
823 kernel_info
.file_name
= optarg
;
826 rootfs_info
.file_name
= optarg
;
829 sscanf(optarg
, "0x%x", &rootfs_ofs
);
838 inspect_info
.file_name
= optarg
;
852 ret
= check_options();
856 if (!inspect_info
.file_name
)
This page took 0.084896 seconds and 5 git commands to generate.