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
));
100 static char *progname
;
101 static char *vendor
= "TP-LINK Technologies";
102 static char *version
= "ver. 1.0";
104 static char *board_id
;
105 static struct board_info
*board
;
106 static char *layout_id
;
107 static struct flash_layout
*layout
;
108 static char *opt_hw_id
;
109 static uint32_t hw_id
;
110 static char *opt_hw_rev
;
111 static uint32_t hw_rev
;
112 static struct file_info kernel_info
;
113 static uint32_t kernel_la
= 0;
114 static uint32_t kernel_ep
= 0;
115 static uint32_t kernel_len
= 0;
116 static struct file_info rootfs_info
;
117 static uint32_t rootfs_ofs
= 0;
118 static uint32_t rootfs_align
;
119 static struct file_info boot_info
;
121 static int strip_padding
;
123 static struct file_info inspect_info
;
124 static int extract
= 0;
126 char md5salt_normal
[MD5SUM_LEN
] = {
127 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
128 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
131 char md5salt_boot
[MD5SUM_LEN
] = {
132 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
133 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
136 static struct flash_layout layouts
[] = {
139 .fw_max_len
= 0x3c0000,
140 .kernel_la
= 0x80060000,
141 .kernel_ep
= 0x80060000,
142 .rootfs_ofs
= 0x140000,
145 .fw_max_len
= 0x3c0000,
146 .kernel_la
= 0x80060000,
147 .kernel_ep
= 0x80060000,
148 .rootfs_ofs
= 0x100000,
151 .fw_max_len
= 0x7c0000,
152 .kernel_la
= 0x80060000,
153 .kernel_ep
= 0x80060000,
154 .rootfs_ofs
= 0x140000,
156 /* terminating entry */
160 static struct board_info boards
[] = {
163 .hw_id
= HWID_TL_MR3220_V1
,
168 .hw_id
= HWID_TL_MR3420_V1
,
172 .id
= "TL-WA901NDv1",
173 .hw_id
= HWID_TL_WA901ND_V1
,
177 .id
= "TL-WA901NDv2",
178 .hw_id
= HWID_TL_WA901ND_V2
,
182 .id
= "TL-WR741NDv1",
183 .hw_id
= HWID_TL_WR741ND_V1
,
187 .id
= "TL-WR741NDv4",
188 .hw_id
= HWID_TL_WR741ND_V4
,
190 .layout_id
= "4Mlzma",
193 .hw_id
= HWID_TL_WR740N_V1
,
198 .hw_id
= HWID_TL_WR740N_V3
,
202 .id
= "TL-WR743NDv1",
203 .hw_id
= HWID_TL_WR743ND_V1
,
207 .id
= "TL-WR841Nv1.5",
208 .hw_id
= HWID_TL_WR841N_V1_5
,
212 .id
= "TL-WR841NDv3",
213 .hw_id
= HWID_TL_WR841ND_V3
,
217 .id
= "TL-WR841NDv5",
218 .hw_id
= HWID_TL_WR841ND_V5
,
222 .id
= "TL-WR841NDv7",
223 .hw_id
= HWID_TL_WR841ND_V7
,
227 .id
= "TL-WR941NDv2",
228 .hw_id
= HWID_TL_WR941ND_V2
,
232 .id
= "TL-WR941NDv4",
233 .hw_id
= HWID_TL_WR941ND_V4
,
237 .id
= "TL-WR1043NDv1",
238 .hw_id
= HWID_TL_WR1043ND_V1
,
243 .hw_id
= HWID_TL_WR703N_V1
,
245 .layout_id
= "4Mlzma",
247 /* terminating entry */
254 #define ERR(fmt, ...) do { \
256 fprintf(stderr, "[%s] *** error: " fmt "\n", \
257 progname, ## __VA_ARGS__ ); \
260 #define ERRS(fmt, ...) do { \
263 fprintf(stderr, "[%s] *** error: " fmt "\n", \
264 progname, ## __VA_ARGS__, strerror(save)); \
267 #define DBG(fmt, ...) do { \
268 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
271 static struct board_info
*find_board(char *id
)
273 struct board_info
*ret
;
274 struct board_info
*board
;
277 for (board
= boards
; board
->id
!= NULL
; board
++){
278 if (strcasecmp(id
, board
->id
) == 0) {
287 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
289 struct board_info
*board
;
291 for (board
= boards
; board
->id
!= NULL
; board
++) {
292 if (hw_id
== board
->hw_id
)
299 static struct flash_layout
*find_layout(char *id
)
301 struct flash_layout
*ret
;
302 struct flash_layout
*l
;
305 for (l
= layouts
; l
->id
!= NULL
; l
++){
306 if (strcasecmp(id
, l
->id
) == 0) {
315 static void usage(int status
)
317 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
318 struct board_info
*board
;
320 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
324 " -B <board> create image for the board specified with <board>\n"
325 " -c use combined kernel image\n"
326 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
327 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
328 " -H <hwid> use hardware id specified with <hwid>\n"
329 " -F <id> use flash layout specified with <id>\n"
330 " -k <file> read kernel image from the file <file>\n"
331 " -r <file> read rootfs image from the file <file>\n"
332 " -a <align> align the rootfs start on an <align> bytes boundary\n"
333 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
334 " -o <file> write output to the file <file>\n"
335 " -s strip padding from the end of the image\n"
336 " -N <vendor> set image vendor to <vendor>\n"
337 " -V <version> set image version to <version>\n"
338 " -i <file> inspect given firmware file <file>\n"
339 " -x extract kernel and rootfs while inspecting (requires -i)\n"
340 " -h show this screen\n"
346 static int get_md5(char *data
, int size
, char *md5
)
351 MD5_Update(&ctx
, data
, size
);
352 MD5_Final(md5
, &ctx
);
355 static int get_file_stat(struct file_info
*fdata
)
360 if (fdata
->file_name
== NULL
)
363 res
= stat(fdata
->file_name
, &st
);
365 ERRS("stat failed on %s", fdata
->file_name
);
369 fdata
->file_size
= st
.st_size
;
373 static int read_to_buf(struct file_info
*fdata
, char *buf
)
376 int ret
= EXIT_FAILURE
;
378 f
= fopen(fdata
->file_name
, "r");
380 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
385 fread(buf
, fdata
->file_size
, 1, f
);
387 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
399 static int check_options(void)
403 if (inspect_info
.file_name
) {
404 ret
= get_file_stat(&inspect_info
);
409 } else if (extract
) {
410 ERR("no firmware for inspection specified");
414 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
415 ERR("either board or hardware id must be specified");
420 board
= find_board(board_id
);
422 ERR("unknown/unsupported board id \"%s\"", board_id
);
425 if (layout_id
== NULL
)
426 layout_id
= board
->layout_id
;
428 hw_id
= board
->hw_id
;
429 hw_rev
= board
->hw_rev
;
431 if (layout_id
== NULL
) {
432 ERR("flash layout is not specified");
435 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
438 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
443 layout
= find_layout(layout_id
);
444 if (layout
== NULL
) {
445 ERR("unknown flash layout \"%s\"", layout_id
);
450 kernel_la
= layout
->kernel_la
;
452 kernel_ep
= layout
->kernel_ep
;
454 rootfs_ofs
= layout
->rootfs_ofs
;
456 if (kernel_info
.file_name
== NULL
) {
457 ERR("no kernel image specified");
461 ret
= get_file_stat(&kernel_info
);
465 kernel_len
= kernel_info
.file_size
;
468 if (kernel_info
.file_size
>
469 layout
->fw_max_len
- sizeof(struct fw_header
)) {
470 ERR("kernel image is too big");
474 if (rootfs_info
.file_name
== NULL
) {
475 ERR("no rootfs image specified");
479 ret
= get_file_stat(&rootfs_info
);
484 kernel_len
+= sizeof(struct fw_header
);
485 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
486 kernel_len
-= sizeof(struct fw_header
);
488 DBG("kernel length aligned to %u", kernel_len
);
490 if (kernel_len
+ rootfs_info
.file_size
>
491 layout
->fw_max_len
- sizeof(struct fw_header
)) {
492 ERR("images are too big");
496 if (kernel_info
.file_size
>
497 rootfs_ofs
- sizeof(struct fw_header
)) {
498 ERR("kernel image is too big");
502 if (rootfs_info
.file_size
>
503 (layout
->fw_max_len
- rootfs_ofs
)) {
504 ERR("rootfs image is too big");
510 if (ofname
== NULL
) {
511 ERR("no output file specified");
518 static void fill_header(char *buf
, int len
)
520 struct fw_header
*hdr
= (struct fw_header
*)buf
;
522 memset(hdr
, 0, sizeof(struct fw_header
));
524 hdr
->version
= htonl(HEADER_VERSION_V1
);
525 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
526 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
527 hdr
->hw_id
= htonl(hw_id
);
528 hdr
->hw_rev
= htonl(hw_rev
);
530 if (boot_info
.file_size
== 0)
531 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
533 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
535 hdr
->kernel_la
= htonl(kernel_la
);
536 hdr
->kernel_ep
= htonl(kernel_ep
);
537 hdr
->fw_length
= htonl(layout
->fw_max_len
);
538 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
539 hdr
->kernel_len
= htonl(kernel_len
);
541 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
542 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
545 get_md5(buf
, len
, hdr
->md5sum1
);
548 static int write_fw(char *data
, int len
)
551 int ret
= EXIT_FAILURE
;
553 f
= fopen(ofname
, "w");
555 ERRS("could not open \"%s\" for writing", ofname
);
560 fwrite(data
, len
, 1, f
);
562 ERRS("unable to write output file");
566 DBG("firmware file \"%s\" completed", ofname
);
573 if (ret
!= EXIT_SUCCESS
) {
580 static int build_fw(void)
585 int ret
= EXIT_FAILURE
;
588 buflen
= layout
->fw_max_len
;
590 buf
= malloc(buflen
);
592 ERR("no memory for buffer\n");
596 memset(buf
, 0xff, buflen
);
597 p
= buf
+ sizeof(struct fw_header
);
598 ret
= read_to_buf(&kernel_info
, p
);
602 writelen
= sizeof(struct fw_header
) + kernel_len
;
608 p
= buf
+ rootfs_ofs
;
609 ret
= read_to_buf(&rootfs_info
, p
);
614 writelen
+= rootfs_info
.file_size
;
616 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
622 fill_header(buf
, writelen
);
623 ret
= write_fw(buf
, writelen
);
635 /* Helper functions to inspect_fw() representing different output formats */
636 static inline void inspect_fw_pstr(char *label
, char *str
)
638 printf("%-23s: %s\n", label
, str
);
641 static inline void inspect_fw_phex(char *label
, uint32_t val
)
643 printf("%-23s: 0x%08x\n", label
, val
);
646 static inline void inspect_fw_phexpost(char *label
,
647 uint32_t val
, char *post
)
649 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
652 static inline void inspect_fw_phexdef(char *label
,
653 uint32_t val
, uint32_t defval
)
655 printf("%-23s: 0x%08x ", label
, val
);
658 printf("(== OpenWrt default)\n");
660 printf("(OpenWrt default: 0x%08x)\n", defval
);
663 static inline void inspect_fw_phexexp(char *label
,
664 uint32_t val
, uint32_t expval
)
666 printf("%-23s: 0x%08x ", label
, val
);
671 printf("(expected: 0x%08x)\n", expval
);
674 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
676 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
679 static inline void inspect_fw_phexdecdef(char *label
,
680 uint32_t val
, uint32_t defval
)
682 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
685 printf("(== OpenWrt default)\n");
687 printf("(OpenWrt default: 0x%08x)\n", defval
);
690 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
694 printf("%-23s:", label
);
695 for (i
=0; i
<MD5SUM_LEN
; i
++)
696 printf(" %02x", val
[i
]);
697 printf(" %s\n", text
);
700 static int inspect_fw(void)
703 struct fw_header
*hdr
;
704 uint8_t md5sum
[MD5SUM_LEN
];
705 struct board_info
*board
;
706 int ret
= EXIT_FAILURE
;
708 buf
= malloc(inspect_info
.file_size
);
710 ERR("no memory for buffer!\n");
714 ret
= read_to_buf(&inspect_info
, buf
);
717 hdr
= (struct fw_header
*)buf
;
719 inspect_fw_pstr("File name", inspect_info
.file_name
);
720 inspect_fw_phexdec("File size", inspect_info
.file_size
);
722 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
723 ERR("file does not seem to have V1 header!\n");
727 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
729 if (ntohl(hdr
->unk1
) != 0)
730 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
732 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
733 if (ntohl(hdr
->boot_len
) == 0)
734 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
736 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
737 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
739 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
740 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
741 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
743 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
745 if (ntohl(hdr
->unk2
) != 0)
746 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
747 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
748 "(purpose yet unknown, unchecked here)");
749 if (ntohl(hdr
->unk3
) != 0)
750 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
754 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
755 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
756 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
758 layout
= find_layout(board
->layout_id
);
759 inspect_fw_phexpost("Hardware ID",
760 ntohl(hdr
->hw_id
), board
->id
);
761 inspect_fw_phexexp("Hardware Revision",
762 ntohl(hdr
->hw_rev
), board
->hw_rev
);
764 inspect_fw_phexpost("Hardware ID",
765 ntohl(hdr
->hw_id
), "unknown");
766 inspect_fw_phex("Hardware Revision",
772 inspect_fw_phexdec("Kernel data offset",
773 ntohl(hdr
->kernel_ofs
));
774 inspect_fw_phexdec("Kernel data length",
775 ntohl(hdr
->kernel_len
));
777 inspect_fw_phexdef("Kernel load address",
778 ntohl(hdr
->kernel_la
),
779 layout
? layout
->kernel_la
: 0xffffffff);
780 inspect_fw_phexdef("Kernel entry point",
781 ntohl(hdr
->kernel_ep
),
782 layout
? layout
->kernel_ep
: 0xffffffff);
783 inspect_fw_phexdecdef("Rootfs data offset",
784 ntohl(hdr
->rootfs_ofs
),
785 layout
? layout
->rootfs_ofs
: 0xffffffff);
787 inspect_fw_phex("Kernel load address",
788 ntohl(hdr
->kernel_la
));
789 inspect_fw_phex("Kernel entry point",
790 ntohl(hdr
->kernel_ep
));
791 inspect_fw_phexdec("Rootfs data offset",
792 ntohl(hdr
->rootfs_ofs
));
794 inspect_fw_phexdec("Rootfs data length",
795 ntohl(hdr
->rootfs_len
));
796 inspect_fw_phexdec("Boot loader data offset",
797 ntohl(hdr
->boot_ofs
));
798 inspect_fw_phexdec("Boot loader data length",
799 ntohl(hdr
->boot_len
));
800 inspect_fw_phexdec("Total firmware length",
801 ntohl(hdr
->fw_length
));
809 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
810 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
811 printf("Extracting kernel to \"%s\"...\n", filename
);
812 fp
= fopen(filename
, "w");
814 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
815 ntohl(hdr
->kernel_len
), 1, fp
)) {
816 ERR("error in fwrite(): %s", strerror(errno
));
820 ERR("error in fopen(): %s", strerror(errno
));
824 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
825 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
826 printf("Extracting rootfs to \"%s\"...\n", filename
);
827 fp
= fopen(filename
, "w");
829 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
830 ntohl(hdr
->rootfs_len
), 1, fp
)) {
831 ERR("error in fwrite(): %s", strerror(errno
));
835 ERR("error in fopen(): %s", strerror(errno
));
846 int main(int argc
, char *argv
[])
848 int ret
= EXIT_FAILURE
;
853 progname
= basename(argv
[0]);
858 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhs");
864 sscanf(optarg
, "0x%x", &rootfs_align
);
873 sscanf(optarg
, "0x%x", &kernel_ep
);
882 sscanf(optarg
, "0x%x", &kernel_la
);
894 kernel_info
.file_name
= optarg
;
897 rootfs_info
.file_name
= optarg
;
900 sscanf(optarg
, "0x%x", &rootfs_ofs
);
909 inspect_info
.file_name
= optarg
;
923 ret
= check_options();
927 if (!inspect_info
.file_name
)
This page took 0.109601 seconds and 5 git commands to generate.