170188c95d8df4a9a1e59dcb98f67b32553c0da8
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_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 struct file_info rootfs_info
;
105 static uint32_t rootfs_ofs
= 0;
106 static struct file_info boot_info
;
108 static int strip_padding
;
110 static struct file_info inspect_info
;
111 static int extract
= 0;
113 char md5salt_normal
[MD5SUM_LEN
] = {
114 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
115 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
118 char md5salt_boot
[MD5SUM_LEN
] = {
119 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
120 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
123 static struct board_info boards
[] = {
126 .hw_id
= HWID_TL_MR3220_V1
,
128 .fw_max_len
= 0x3c0000,
129 .kernel_la
= 0x80060000,
130 .kernel_ep
= 0x80060000,
131 .rootfs_ofs
= 0x140000,
134 .hw_id
= HWID_TL_MR3420_V1
,
136 .fw_max_len
= 0x3c0000,
137 .kernel_la
= 0x80060000,
138 .kernel_ep
= 0x80060000,
139 .rootfs_ofs
= 0x140000,
141 .id
= "TL-WA901NDv1",
142 .hw_id
= HWID_TL_WA901ND_V1
,
144 .fw_max_len
= 0x3c0000,
145 .kernel_la
= 0x80060000,
146 .kernel_ep
= 0x80060000,
147 .rootfs_ofs
= 0x140000,
149 .id
= "TL-WA901NDv2",
150 .hw_id
= HWID_TL_WA901ND_V2
,
152 .fw_max_len
= 0x3c0000,
153 .kernel_la
= 0x80060000,
154 .kernel_ep
= 0x80060000,
155 .rootfs_ofs
= 0x140000,
157 .id
= "TL-WR741NDv1",
158 .hw_id
= HWID_TL_WR741ND_V1
,
160 .fw_max_len
= 0x3c0000,
161 .kernel_la
= 0x80060000,
162 .kernel_ep
= 0x80060000,
163 .rootfs_ofs
= 0x140000,
166 .hw_id
= HWID_TL_WR740N_V1
,
168 .fw_max_len
= 0x3c0000,
169 .kernel_la
= 0x80060000,
170 .kernel_ep
= 0x80060000,
171 .rootfs_ofs
= 0x140000,
174 .hw_id
= HWID_TL_WR740N_V3
,
176 .fw_max_len
= 0x3c0000,
177 .kernel_la
= 0x80060000,
178 .kernel_ep
= 0x80060000,
179 .rootfs_ofs
= 0x140000,
181 .id
= "TL-WR841Nv1.5",
182 .hw_id
= HWID_TL_WR841N_V1_5
,
184 .fw_max_len
= 0x3c0000,
185 .kernel_la
= 0x80060000,
186 .kernel_ep
= 0x80060000,
187 .rootfs_ofs
= 0x140000,
189 .id
= "TL-WR841NDv3",
190 .hw_id
= HWID_TL_WR841ND_V3
,
192 .fw_max_len
= 0x3c0000,
193 .kernel_la
= 0x80060000,
194 .kernel_ep
= 0x80060000,
195 .rootfs_ofs
= 0x140000,
197 .id
= "TL-WR841NDv5",
198 .hw_id
= HWID_TL_WR841ND_V5
,
200 .fw_max_len
= 0x3c0000,
201 .kernel_la
= 0x80060000,
202 .kernel_ep
= 0x80060000,
203 .rootfs_ofs
= 0x140000,
205 .id
= "TL-WR841NDv7",
206 .hw_id
= HWID_TL_WR841ND_V7
,
208 .fw_max_len
= 0x3c0000,
209 .kernel_la
= 0x80060000,
210 .kernel_ep
= 0x80060000,
211 .rootfs_ofs
= 0x140000,
213 .id
= "TL-WR941NDv2",
214 .hw_id
= HWID_TL_WR941ND_V2
,
216 .fw_max_len
= 0x3c0000,
217 .kernel_la
= 0x80060000,
218 .kernel_ep
= 0x80060000,
219 .rootfs_ofs
= 0x140000,
221 .id
= "TL-WR941NDv4",
222 .hw_id
= HWID_TL_WR941ND_V4
,
224 .fw_max_len
= 0x3c0000,
225 .kernel_la
= 0x80060000,
226 .kernel_ep
= 0x80060000,
227 .rootfs_ofs
= 0x140000,
229 .id
= "TL-WR1043NDv1",
230 .hw_id
= HWID_TL_WR1043ND_V1
,
232 .fw_max_len
= 0x7c0000,
233 .kernel_la
= 0x80060000,
234 .kernel_ep
= 0x80060000,
235 .rootfs_ofs
= 0x140000,
237 /* terminating entry */
244 #define ERR(fmt, ...) do { \
246 fprintf(stderr, "[%s] *** error: " fmt "\n", \
247 progname, ## __VA_ARGS__ ); \
250 #define ERRS(fmt, ...) do { \
253 fprintf(stderr, "[%s] *** error: " fmt "\n", \
254 progname, ## __VA_ARGS__, strerror(save)); \
257 #define DBG(fmt, ...) do { \
258 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
261 static struct board_info
*find_board(char *id
)
263 struct board_info
*ret
;
264 struct board_info
*board
;
267 for (board
= boards
; board
->id
!= NULL
; board
++){
268 if (strcasecmp(id
, board
->id
) == 0) {
277 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
279 struct board_info
*board
;
281 for (board
= boards
; board
->id
!= NULL
; board
++) {
282 if (hw_id
== board
->hw_id
)
290 static void usage(int status
)
292 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
293 struct board_info
*board
;
295 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
299 " -B <board> create image for the board specified with <board>\n"
300 " -c use combined kernel image\n"
301 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
302 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
303 " -k <file> read kernel image from the file <file>\n"
304 " -r <file> read rootfs image from the file <file>\n"
305 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
306 " -o <file> write output to the file <file>\n"
307 " -s strip padding from the end of the image\n"
308 " -N <vendor> set image vendor to <vendor>\n"
309 " -V <version> set image version to <version>\n"
310 " -i <file> inspect given firmware file <file>\n"
311 " -x extract kernel and rootfs while inspecting (requires -i)\n"
312 " -h show this screen\n"
318 static int get_md5(char *data
, int size
, char *md5
)
323 MD5_Update(&ctx
, data
, size
);
324 MD5_Final(md5
, &ctx
);
327 static int get_file_stat(struct file_info
*fdata
)
332 if (fdata
->file_name
== NULL
)
335 res
= stat(fdata
->file_name
, &st
);
337 ERRS("stat failed on %s", fdata
->file_name
);
341 fdata
->file_size
= st
.st_size
;
345 static int read_to_buf(struct file_info
*fdata
, char *buf
)
348 int ret
= EXIT_FAILURE
;
350 f
= fopen(fdata
->file_name
, "r");
352 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
357 fread(buf
, fdata
->file_size
, 1, f
);
359 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
371 static int check_options(void)
375 if (inspect_info
.file_name
) {
376 ret
= get_file_stat(&inspect_info
);
381 } else if (extract
) {
382 ERR("no firmware for inspection specified");
386 if (board_id
== NULL
) {
387 ERR("no board specified");
391 board
= find_board(board_id
);
393 ERR("unknown/unsupported board id \"%s\"", board_id
);
397 kernel_la
= board
->kernel_la
;
399 kernel_ep
= board
->kernel_ep
;
401 rootfs_ofs
= board
->rootfs_ofs
;
403 if (kernel_info
.file_name
== NULL
) {
404 ERR("no kernel image specified");
408 ret
= get_file_stat(&kernel_info
);
413 if (kernel_info
.file_size
>
414 board
->fw_max_len
- sizeof(struct fw_header
)) {
415 ERR("kernel image is too big");
419 if (kernel_info
.file_size
>
420 rootfs_ofs
- sizeof(struct fw_header
)) {
421 ERR("kernel image is too big");
424 if (rootfs_info
.file_name
== NULL
) {
425 ERR("no rootfs image specified");
429 ret
= get_file_stat(&rootfs_info
);
433 if (rootfs_info
.file_size
>
434 (board
->fw_max_len
- rootfs_ofs
)) {
435 ERR("rootfs image is too big");
440 if (ofname
== NULL
) {
441 ERR("no output file specified");
448 static void fill_header(char *buf
, int len
)
450 struct fw_header
*hdr
= (struct fw_header
*)buf
;
452 memset(hdr
, 0, sizeof(struct fw_header
));
454 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
455 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
456 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
457 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
458 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
460 if (boot_info
.file_size
== 0)
461 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
463 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
465 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
466 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
467 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
468 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
469 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
471 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
472 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
475 get_md5(buf
, len
, hdr
->md5sum1
);
478 static int write_fw(char *data
, int len
)
481 int ret
= EXIT_FAILURE
;
483 f
= fopen(ofname
, "w");
485 ERRS("could not open \"%s\" for writing", ofname
);
490 fwrite(data
, len
, 1, f
);
492 ERRS("unable to write output file");
496 DBG("firmware file \"%s\" completed", ofname
);
503 if (ret
!= EXIT_SUCCESS
) {
510 static int build_fw(void)
515 int ret
= EXIT_FAILURE
;
518 buflen
= board
->fw_max_len
;
520 buf
= malloc(buflen
);
522 ERR("no memory for buffer\n");
526 memset(buf
, 0xff, buflen
);
527 p
= buf
+ sizeof(struct fw_header
);
528 ret
= read_to_buf(&kernel_info
, p
);
532 writelen
= kernel_info
.file_size
;
535 p
= buf
+ rootfs_ofs
;
536 ret
= read_to_buf(&rootfs_info
, p
);
540 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
546 fill_header(buf
, writelen
);
547 ret
= write_fw(buf
, writelen
);
559 /* Helper functions to inspect_fw() representing different output formats */
560 static inline void inspect_fw_pstr(char *label
, char *str
)
562 printf("%-23s: %s\n", label
, str
);
565 static inline void inspect_fw_phex(char *label
, uint32_t val
)
567 printf("%-23s: 0x%08x\n", label
, val
);
570 static inline void inspect_fw_phexpost(char *label
,
571 uint32_t val
, char *post
)
573 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
576 static inline void inspect_fw_phexdef(char *label
,
577 uint32_t val
, uint32_t defval
)
579 printf("%-23s: 0x%08x ", label
, val
);
582 printf("(== OpenWrt default)\n");
584 printf("(OpenWrt default: 0x%08x)\n", defval
);
587 static inline void inspect_fw_phexexp(char *label
,
588 uint32_t val
, uint32_t expval
)
590 printf("%-23s: 0x%08x ", label
, val
);
595 printf("(expected: 0x%08x)\n", expval
);
598 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
600 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
603 static inline void inspect_fw_phexdecdef(char *label
,
604 uint32_t val
, uint32_t defval
)
606 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
609 printf("(== OpenWrt default)\n");
611 printf("(OpenWrt default: 0x%08x)\n", defval
);
614 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
618 printf("%-23s:", label
);
619 for (i
=0; i
<MD5SUM_LEN
; i
++)
620 printf(" %02x", val
[i
]);
621 printf(" %s\n", text
);
624 static int inspect_fw(void)
627 struct fw_header
*hdr
;
628 uint8_t md5sum
[MD5SUM_LEN
];
629 struct board_info
*board
;
630 int ret
= EXIT_FAILURE
;
632 buf
= malloc(inspect_info
.file_size
);
634 ERR("no memory for buffer!\n");
638 ret
= read_to_buf(&inspect_info
, buf
);
641 hdr
= (struct fw_header
*)buf
;
643 inspect_fw_pstr("File name", inspect_info
.file_name
);
644 inspect_fw_phexdec("File size", inspect_info
.file_size
);
646 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
647 ERR("file does not seem to have V1 header!\n");
651 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
653 if (BE32_TO_HOST(hdr
->unk1
) != 0)
654 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
656 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
657 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
658 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
660 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
661 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
663 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
664 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
665 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
667 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
669 if (BE32_TO_HOST(hdr
->unk2
) != 0)
670 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
671 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
672 "(purpose yet unknown, unchecked here)");
673 if (BE32_TO_HOST(hdr
->unk3
) != 0)
674 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
678 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
679 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
680 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
682 inspect_fw_phexpost("Hardware ID",
683 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
684 inspect_fw_phexexp("Hardware Revision",
685 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
687 inspect_fw_phexpost("Hardware ID",
688 BE32_TO_HOST(hdr
->hw_id
), "unknown");
689 inspect_fw_phex("Hardware Revision",
690 BE32_TO_HOST(hdr
->hw_rev
));
695 inspect_fw_phexdec("Kernel data offset",
696 BE32_TO_HOST(hdr
->kernel_ofs
));
697 inspect_fw_phexdec("Kernel data length",
698 BE32_TO_HOST(hdr
->kernel_len
));
700 inspect_fw_phexdef("Kernel load address",
701 BE32_TO_HOST(hdr
->kernel_la
),
703 inspect_fw_phexdef("Kernel entry point",
704 BE32_TO_HOST(hdr
->kernel_ep
),
706 inspect_fw_phexdecdef("Rootfs data offset",
707 BE32_TO_HOST(hdr
->rootfs_ofs
),
710 inspect_fw_phex("Kernel load address",
711 BE32_TO_HOST(hdr
->kernel_la
));
712 inspect_fw_phex("Kernel entry point",
713 BE32_TO_HOST(hdr
->kernel_ep
));
714 inspect_fw_phexdec("Rootfs data offset",
715 BE32_TO_HOST(hdr
->rootfs_ofs
));
717 inspect_fw_phexdec("Rootfs data length",
718 BE32_TO_HOST(hdr
->rootfs_len
));
719 inspect_fw_phexdec("Boot loader data offset",
720 BE32_TO_HOST(hdr
->boot_ofs
));
721 inspect_fw_phexdec("Boot loader data length",
722 BE32_TO_HOST(hdr
->boot_len
));
723 inspect_fw_phexdec("Total firmware length",
724 BE32_TO_HOST(hdr
->fw_length
));
732 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
733 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
734 printf("Extracting kernel to \"%s\"...\n", filename
);
735 fp
= fopen(filename
, "w");
737 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
738 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
739 ERR("error in fwrite(): %s", strerror(errno
));
743 ERR("error in fopen(): %s", strerror(errno
));
747 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
748 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
749 printf("Extracting rootfs to \"%s\"...\n", filename
);
750 fp
= fopen(filename
, "w");
752 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
753 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
754 ERR("error in fwrite(): %s", strerror(errno
));
758 ERR("error in fopen(): %s", strerror(errno
));
769 int main(int argc
, char *argv
[])
771 int ret
= EXIT_FAILURE
;
776 progname
= basename(argv
[0]);
781 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
790 sscanf(optarg
, "0x%x", &kernel_ep
);
793 sscanf(optarg
, "0x%x", &kernel_la
);
805 kernel_info
.file_name
= optarg
;
808 rootfs_info
.file_name
= optarg
;
811 sscanf(optarg
, "0x%x", &rootfs_ofs
);
820 inspect_info
.file_name
= optarg
;
834 ret
= check_options();
838 if (!inspect_info
.file_name
)
This page took 0.075916 seconds and 3 git commands to generate.