0f1e20f1422cbf660906c9c4b33fd2e4402a5166
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() */
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
32 #define HEADER_VERSION_V1 0x01000000
33 #define HWID_TL_MR3220_V1 0x32200001
34 #define HWID_TL_MR3420_V1 0x34200001
35 #define HWID_TL_WA901ND_V1 0x09010001
36 #define HWID_TL_WA901ND_V2 0x09010002
37 #define HWID_TL_WR703N_V1 0x07030101
38 #define HWID_TL_WR741ND_V1 0x07410001
39 #define HWID_TL_WR741ND_V4 0x07410004
40 #define HWID_TL_WR740N_V1 0x07400001
41 #define HWID_TL_WR740N_V3 0x07400003
42 #define HWID_TL_WR743ND_V1 0x07430001
43 #define HWID_TL_WR841N_V1_5 0x08410002
44 #define HWID_TL_WR841ND_V3 0x08410003
45 #define HWID_TL_WR841ND_V5 0x08410005
46 #define HWID_TL_WR841ND_V7 0x08410007
47 #define HWID_TL_WR941ND_V2 0x09410002
48 #define HWID_TL_WR941ND_V4 0x09410004
49 #define HWID_TL_WR1043ND_V1 0x10430001
54 char *file_name
; /* name of the file */
55 uint32_t file_size
; /* length of the file */
59 uint32_t version
; /* header version */
62 uint32_t hw_id
; /* hardware id */
63 uint32_t hw_rev
; /* hardware revision */
65 uint8_t md5sum1
[MD5SUM_LEN
];
67 uint8_t md5sum2
[MD5SUM_LEN
];
69 uint32_t kernel_la
; /* kernel load address */
70 uint32_t kernel_ep
; /* kernel entry point */
71 uint32_t fw_length
; /* total length of the firmware */
72 uint32_t kernel_ofs
; /* kernel data offset */
73 uint32_t kernel_len
; /* kernel data length */
74 uint32_t rootfs_ofs
; /* rootfs data offset */
75 uint32_t rootfs_len
; /* rootfs data length */
76 uint32_t boot_ofs
; /* bootloader data offset */
77 uint32_t boot_len
; /* bootloader data length */
79 } __attribute__ ((packed
));
95 static char *progname
;
96 static char *vendor
= "TP-LINK Technologies";
97 static char *version
= "ver. 1.0";
99 static char *board_id
;
100 static struct board_info
*board
;
101 static struct file_info kernel_info
;
102 static uint32_t kernel_la
= 0;
103 static uint32_t kernel_ep
= 0;
104 static uint32_t kernel_len
= 0;
105 static struct file_info rootfs_info
;
106 static uint32_t rootfs_ofs
= 0;
107 static uint32_t rootfs_align
;
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,
167 .id
= "TL-WR741NDv4",
168 .hw_id
= HWID_TL_WR741ND_V4
,
170 .fw_max_len
= 0x3c0000,
171 .kernel_la
= 0x80060000,
172 .kernel_ep
= 0x80060000,
173 .rootfs_ofs
= 0x100000,
176 .hw_id
= HWID_TL_WR740N_V1
,
178 .fw_max_len
= 0x3c0000,
179 .kernel_la
= 0x80060000,
180 .kernel_ep
= 0x80060000,
181 .rootfs_ofs
= 0x140000,
184 .hw_id
= HWID_TL_WR740N_V3
,
186 .fw_max_len
= 0x3c0000,
187 .kernel_la
= 0x80060000,
188 .kernel_ep
= 0x80060000,
189 .rootfs_ofs
= 0x140000,
191 .id
= "TL-WR743NDv1",
192 .hw_id
= HWID_TL_WR743ND_V1
,
194 .fw_max_len
= 0x3c0000,
195 .kernel_la
= 0x80060000,
196 .kernel_ep
= 0x80060000,
197 .rootfs_ofs
= 0x140000,
199 .id
= "TL-WR841Nv1.5",
200 .hw_id
= HWID_TL_WR841N_V1_5
,
202 .fw_max_len
= 0x3c0000,
203 .kernel_la
= 0x80060000,
204 .kernel_ep
= 0x80060000,
205 .rootfs_ofs
= 0x140000,
207 .id
= "TL-WR841NDv3",
208 .hw_id
= HWID_TL_WR841ND_V3
,
210 .fw_max_len
= 0x3c0000,
211 .kernel_la
= 0x80060000,
212 .kernel_ep
= 0x80060000,
213 .rootfs_ofs
= 0x140000,
215 .id
= "TL-WR841NDv5",
216 .hw_id
= HWID_TL_WR841ND_V5
,
218 .fw_max_len
= 0x3c0000,
219 .kernel_la
= 0x80060000,
220 .kernel_ep
= 0x80060000,
221 .rootfs_ofs
= 0x140000,
223 .id
= "TL-WR841NDv7",
224 .hw_id
= HWID_TL_WR841ND_V7
,
226 .fw_max_len
= 0x3c0000,
227 .kernel_la
= 0x80060000,
228 .kernel_ep
= 0x80060000,
229 .rootfs_ofs
= 0x140000,
231 .id
= "TL-WR941NDv2",
232 .hw_id
= HWID_TL_WR941ND_V2
,
234 .fw_max_len
= 0x3c0000,
235 .kernel_la
= 0x80060000,
236 .kernel_ep
= 0x80060000,
237 .rootfs_ofs
= 0x140000,
239 .id
= "TL-WR941NDv4",
240 .hw_id
= HWID_TL_WR941ND_V4
,
242 .fw_max_len
= 0x3c0000,
243 .kernel_la
= 0x80060000,
244 .kernel_ep
= 0x80060000,
245 .rootfs_ofs
= 0x140000,
247 .id
= "TL-WR1043NDv1",
248 .hw_id
= HWID_TL_WR1043ND_V1
,
250 .fw_max_len
= 0x7c0000,
251 .kernel_la
= 0x80060000,
252 .kernel_ep
= 0x80060000,
253 .rootfs_ofs
= 0x140000,
256 .hw_id
= HWID_TL_WR703N_V1
,
258 .fw_max_len
= 0x3c0000,
259 .kernel_la
= 0x80060000,
260 .kernel_ep
= 0x80060000,
261 .rootfs_ofs
= 0x100000,
263 /* terminating entry */
270 #define ERR(fmt, ...) do { \
272 fprintf(stderr, "[%s] *** error: " fmt "\n", \
273 progname, ## __VA_ARGS__ ); \
276 #define ERRS(fmt, ...) do { \
279 fprintf(stderr, "[%s] *** error: " fmt "\n", \
280 progname, ## __VA_ARGS__, strerror(save)); \
283 #define DBG(fmt, ...) do { \
284 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
287 static struct board_info
*find_board(char *id
)
289 struct board_info
*ret
;
290 struct board_info
*board
;
293 for (board
= boards
; board
->id
!= NULL
; board
++){
294 if (strcasecmp(id
, board
->id
) == 0) {
303 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
305 struct board_info
*board
;
307 for (board
= boards
; board
->id
!= NULL
; board
++) {
308 if (hw_id
== board
->hw_id
)
316 static void usage(int status
)
318 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
319 struct board_info
*board
;
321 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
325 " -B <board> create image for the board specified with <board>\n"
326 " -c use combined kernel image\n"
327 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
328 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
329 " -k <file> read kernel image from the file <file>\n"
330 " -r <file> read rootfs image from the file <file>\n"
331 " -a <align> align the rootfs start on an <align> bytes boundary\n"
332 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
333 " -o <file> write output to the file <file>\n"
334 " -s strip padding from the end of the image\n"
335 " -N <vendor> set image vendor to <vendor>\n"
336 " -V <version> set image version to <version>\n"
337 " -i <file> inspect given firmware file <file>\n"
338 " -x extract kernel and rootfs while inspecting (requires -i)\n"
339 " -h show this screen\n"
345 static int get_md5(char *data
, int size
, char *md5
)
350 MD5_Update(&ctx
, data
, size
);
351 MD5_Final(md5
, &ctx
);
354 static int get_file_stat(struct file_info
*fdata
)
359 if (fdata
->file_name
== NULL
)
362 res
= stat(fdata
->file_name
, &st
);
364 ERRS("stat failed on %s", fdata
->file_name
);
368 fdata
->file_size
= st
.st_size
;
372 static int read_to_buf(struct file_info
*fdata
, char *buf
)
375 int ret
= EXIT_FAILURE
;
377 f
= fopen(fdata
->file_name
, "r");
379 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
384 fread(buf
, fdata
->file_size
, 1, f
);
386 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
398 static int check_options(void)
402 if (inspect_info
.file_name
) {
403 ret
= get_file_stat(&inspect_info
);
408 } else if (extract
) {
409 ERR("no firmware for inspection specified");
413 if (board_id
== NULL
) {
414 ERR("no board specified");
418 board
= find_board(board_id
);
420 ERR("unknown/unsupported board id \"%s\"", board_id
);
424 kernel_la
= board
->kernel_la
;
426 kernel_ep
= board
->kernel_ep
;
428 rootfs_ofs
= board
->rootfs_ofs
;
430 if (kernel_info
.file_name
== NULL
) {
431 ERR("no kernel image specified");
435 ret
= get_file_stat(&kernel_info
);
439 kernel_len
= kernel_info
.file_size
;
442 if (kernel_info
.file_size
>
443 board
->fw_max_len
- sizeof(struct fw_header
)) {
444 ERR("kernel image is too big");
448 if (rootfs_info
.file_name
== NULL
) {
449 ERR("no rootfs image specified");
453 ret
= get_file_stat(&rootfs_info
);
458 kernel_len
+= sizeof(struct fw_header
);
459 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
460 kernel_len
-= sizeof(struct fw_header
);
462 DBG("kernel length aligned to %u", kernel_len
);
464 if (kernel_len
+ rootfs_info
.file_size
>
465 board
->fw_max_len
- sizeof(struct fw_header
)) {
466 ERR("images are too big");
470 if (kernel_info
.file_size
>
471 rootfs_ofs
- sizeof(struct fw_header
)) {
472 ERR("kernel image is too big");
476 if (rootfs_info
.file_size
>
477 (board
->fw_max_len
- rootfs_ofs
)) {
478 ERR("rootfs image is too big");
484 if (ofname
== NULL
) {
485 ERR("no output file specified");
492 static void fill_header(char *buf
, int len
)
494 struct fw_header
*hdr
= (struct fw_header
*)buf
;
496 memset(hdr
, 0, sizeof(struct fw_header
));
498 hdr
->version
= htonl(HEADER_VERSION_V1
);
499 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
500 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
501 hdr
->hw_id
= htonl(board
->hw_id
);
502 hdr
->hw_rev
= htonl(board
->hw_rev
);
504 if (boot_info
.file_size
== 0)
505 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
507 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
509 hdr
->kernel_la
= htonl(kernel_la
);
510 hdr
->kernel_ep
= htonl(kernel_ep
);
511 hdr
->fw_length
= htonl(board
->fw_max_len
);
512 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
513 hdr
->kernel_len
= htonl(kernel_len
);
515 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
516 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
519 get_md5(buf
, len
, hdr
->md5sum1
);
522 static int write_fw(char *data
, int len
)
525 int ret
= EXIT_FAILURE
;
527 f
= fopen(ofname
, "w");
529 ERRS("could not open \"%s\" for writing", ofname
);
534 fwrite(data
, len
, 1, f
);
536 ERRS("unable to write output file");
540 DBG("firmware file \"%s\" completed", ofname
);
547 if (ret
!= EXIT_SUCCESS
) {
554 static int build_fw(void)
559 int ret
= EXIT_FAILURE
;
562 buflen
= board
->fw_max_len
;
564 buf
= malloc(buflen
);
566 ERR("no memory for buffer\n");
570 memset(buf
, 0xff, buflen
);
571 p
= buf
+ sizeof(struct fw_header
);
572 ret
= read_to_buf(&kernel_info
, p
);
576 writelen
= sizeof(struct fw_header
) + kernel_len
;
582 p
= buf
+ rootfs_ofs
;
583 ret
= read_to_buf(&rootfs_info
, p
);
588 writelen
+= rootfs_info
.file_size
;
590 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
596 fill_header(buf
, writelen
);
597 ret
= write_fw(buf
, writelen
);
609 /* Helper functions to inspect_fw() representing different output formats */
610 static inline void inspect_fw_pstr(char *label
, char *str
)
612 printf("%-23s: %s\n", label
, str
);
615 static inline void inspect_fw_phex(char *label
, uint32_t val
)
617 printf("%-23s: 0x%08x\n", label
, val
);
620 static inline void inspect_fw_phexpost(char *label
,
621 uint32_t val
, char *post
)
623 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
626 static inline void inspect_fw_phexdef(char *label
,
627 uint32_t val
, uint32_t defval
)
629 printf("%-23s: 0x%08x ", label
, val
);
632 printf("(== OpenWrt default)\n");
634 printf("(OpenWrt default: 0x%08x)\n", defval
);
637 static inline void inspect_fw_phexexp(char *label
,
638 uint32_t val
, uint32_t expval
)
640 printf("%-23s: 0x%08x ", label
, val
);
645 printf("(expected: 0x%08x)\n", expval
);
648 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
650 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
653 static inline void inspect_fw_phexdecdef(char *label
,
654 uint32_t val
, uint32_t defval
)
656 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
659 printf("(== OpenWrt default)\n");
661 printf("(OpenWrt default: 0x%08x)\n", defval
);
664 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
668 printf("%-23s:", label
);
669 for (i
=0; i
<MD5SUM_LEN
; i
++)
670 printf(" %02x", val
[i
]);
671 printf(" %s\n", text
);
674 static int inspect_fw(void)
677 struct fw_header
*hdr
;
678 uint8_t md5sum
[MD5SUM_LEN
];
679 struct board_info
*board
;
680 int ret
= EXIT_FAILURE
;
682 buf
= malloc(inspect_info
.file_size
);
684 ERR("no memory for buffer!\n");
688 ret
= read_to_buf(&inspect_info
, buf
);
691 hdr
= (struct fw_header
*)buf
;
693 inspect_fw_pstr("File name", inspect_info
.file_name
);
694 inspect_fw_phexdec("File size", inspect_info
.file_size
);
696 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
697 ERR("file does not seem to have V1 header!\n");
701 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
703 if (ntohl(hdr
->unk1
) != 0)
704 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
706 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
707 if (ntohl(hdr
->boot_len
) == 0)
708 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
710 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
711 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
713 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
714 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
715 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
717 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
719 if (ntohl(hdr
->unk2
) != 0)
720 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
721 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
722 "(purpose yet unknown, unchecked here)");
723 if (ntohl(hdr
->unk3
) != 0)
724 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
728 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
729 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
730 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
732 inspect_fw_phexpost("Hardware ID",
733 ntohl(hdr
->hw_id
), board
->id
);
734 inspect_fw_phexexp("Hardware Revision",
735 ntohl(hdr
->hw_rev
), board
->hw_rev
);
737 inspect_fw_phexpost("Hardware ID",
738 ntohl(hdr
->hw_id
), "unknown");
739 inspect_fw_phex("Hardware Revision",
745 inspect_fw_phexdec("Kernel data offset",
746 ntohl(hdr
->kernel_ofs
));
747 inspect_fw_phexdec("Kernel data length",
748 ntohl(hdr
->kernel_len
));
750 inspect_fw_phexdef("Kernel load address",
751 ntohl(hdr
->kernel_la
),
753 inspect_fw_phexdef("Kernel entry point",
754 ntohl(hdr
->kernel_ep
),
756 inspect_fw_phexdecdef("Rootfs data offset",
757 ntohl(hdr
->rootfs_ofs
),
760 inspect_fw_phex("Kernel load address",
761 ntohl(hdr
->kernel_la
));
762 inspect_fw_phex("Kernel entry point",
763 ntohl(hdr
->kernel_ep
));
764 inspect_fw_phexdec("Rootfs data offset",
765 ntohl(hdr
->rootfs_ofs
));
767 inspect_fw_phexdec("Rootfs data length",
768 ntohl(hdr
->rootfs_len
));
769 inspect_fw_phexdec("Boot loader data offset",
770 ntohl(hdr
->boot_ofs
));
771 inspect_fw_phexdec("Boot loader data length",
772 ntohl(hdr
->boot_len
));
773 inspect_fw_phexdec("Total firmware length",
774 ntohl(hdr
->fw_length
));
782 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
783 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
784 printf("Extracting kernel to \"%s\"...\n", filename
);
785 fp
= fopen(filename
, "w");
787 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
788 ntohl(hdr
->kernel_len
), 1, fp
)) {
789 ERR("error in fwrite(): %s", strerror(errno
));
793 ERR("error in fopen(): %s", strerror(errno
));
797 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
798 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
799 printf("Extracting rootfs to \"%s\"...\n", filename
);
800 fp
= fopen(filename
, "w");
802 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
803 ntohl(hdr
->rootfs_len
), 1, fp
)) {
804 ERR("error in fwrite(): %s", strerror(errno
));
808 ERR("error in fopen(): %s", strerror(errno
));
819 int main(int argc
, char *argv
[])
821 int ret
= EXIT_FAILURE
;
826 progname
= basename(argv
[0]);
831 c
= getopt(argc
, argv
, "a:B:E:L:V:N:ci:k:r:R:o:xhs");
837 sscanf(optarg
, "0x%x", &rootfs_align
);
843 sscanf(optarg
, "0x%x", &kernel_ep
);
846 sscanf(optarg
, "0x%x", &kernel_la
);
858 kernel_info
.file_name
= optarg
;
861 rootfs_info
.file_name
= optarg
;
864 sscanf(optarg
, "0x%x", &rootfs_ofs
);
873 inspect_info
.file_name
= optarg
;
887 ret
= check_options();
891 if (!inspect_info
.file_name
)
This page took 0.07639 seconds and 3 git commands to generate.