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
;
122 static int add_jffs2_eof
;
123 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
125 static struct file_info inspect_info
;
126 static int extract
= 0;
128 char md5salt_normal
[MD5SUM_LEN
] = {
129 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
130 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
133 char md5salt_boot
[MD5SUM_LEN
] = {
134 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
135 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
138 static struct flash_layout layouts
[] = {
141 .fw_max_len
= 0x3c0000,
142 .kernel_la
= 0x80060000,
143 .kernel_ep
= 0x80060000,
144 .rootfs_ofs
= 0x140000,
147 .fw_max_len
= 0x3c0000,
148 .kernel_la
= 0x80060000,
149 .kernel_ep
= 0x80060000,
150 .rootfs_ofs
= 0x100000,
153 .fw_max_len
= 0x7c0000,
154 .kernel_la
= 0x80060000,
155 .kernel_ep
= 0x80060000,
156 .rootfs_ofs
= 0x140000,
158 /* terminating entry */
162 static struct board_info boards
[] = {
165 .hw_id
= HWID_TL_MR3220_V1
,
170 .hw_id
= HWID_TL_MR3420_V1
,
174 .id
= "TL-WA901NDv1",
175 .hw_id
= HWID_TL_WA901ND_V1
,
179 .id
= "TL-WA901NDv2",
180 .hw_id
= HWID_TL_WA901ND_V2
,
184 .id
= "TL-WR741NDv1",
185 .hw_id
= HWID_TL_WR741ND_V1
,
189 .id
= "TL-WR741NDv4",
190 .hw_id
= HWID_TL_WR741ND_V4
,
192 .layout_id
= "4Mlzma",
195 .hw_id
= HWID_TL_WR740N_V1
,
200 .hw_id
= HWID_TL_WR740N_V3
,
204 .id
= "TL-WR743NDv1",
205 .hw_id
= HWID_TL_WR743ND_V1
,
209 .id
= "TL-WR841Nv1.5",
210 .hw_id
= HWID_TL_WR841N_V1_5
,
214 .id
= "TL-WR841NDv3",
215 .hw_id
= HWID_TL_WR841ND_V3
,
219 .id
= "TL-WR841NDv5",
220 .hw_id
= HWID_TL_WR841ND_V5
,
224 .id
= "TL-WR841NDv7",
225 .hw_id
= HWID_TL_WR841ND_V7
,
229 .id
= "TL-WR941NDv2",
230 .hw_id
= HWID_TL_WR941ND_V2
,
234 .id
= "TL-WR941NDv4",
235 .hw_id
= HWID_TL_WR941ND_V4
,
239 .id
= "TL-WR1043NDv1",
240 .hw_id
= HWID_TL_WR1043ND_V1
,
245 .hw_id
= HWID_TL_WR703N_V1
,
247 .layout_id
= "4Mlzma",
249 /* terminating entry */
256 #define ERR(fmt, ...) do { \
258 fprintf(stderr, "[%s] *** error: " fmt "\n", \
259 progname, ## __VA_ARGS__ ); \
262 #define ERRS(fmt, ...) do { \
265 fprintf(stderr, "[%s] *** error: " fmt "\n", \
266 progname, ## __VA_ARGS__, strerror(save)); \
269 #define DBG(fmt, ...) do { \
270 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
273 static struct board_info
*find_board(char *id
)
275 struct board_info
*ret
;
276 struct board_info
*board
;
279 for (board
= boards
; board
->id
!= NULL
; board
++){
280 if (strcasecmp(id
, board
->id
) == 0) {
289 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
291 struct board_info
*board
;
293 for (board
= boards
; board
->id
!= NULL
; board
++) {
294 if (hw_id
== board
->hw_id
)
301 static struct flash_layout
*find_layout(char *id
)
303 struct flash_layout
*ret
;
304 struct flash_layout
*l
;
307 for (l
= layouts
; l
->id
!= NULL
; l
++){
308 if (strcasecmp(id
, l
->id
) == 0) {
317 static void usage(int status
)
319 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
320 struct board_info
*board
;
322 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
326 " -B <board> create image for the board specified with <board>\n"
327 " -c use combined kernel image\n"
328 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
329 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
330 " -H <hwid> use hardware id specified with <hwid>\n"
331 " -F <id> use flash layout specified with <id>\n"
332 " -k <file> read kernel image from the file <file>\n"
333 " -r <file> read rootfs image from the file <file>\n"
334 " -a <align> align the rootfs start on an <align> bytes boundary\n"
335 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
336 " -o <file> write output to the file <file>\n"
337 " -s strip padding from the end of the image\n"
338 " -j add jffs2 end-of-filesystem markers\n"
339 " -N <vendor> set image vendor to <vendor>\n"
340 " -V <version> set image version to <version>\n"
341 " -i <file> inspect given firmware file <file>\n"
342 " -x extract kernel and rootfs while inspecting (requires -i)\n"
343 " -h show this screen\n"
349 static int get_md5(char *data
, int size
, char *md5
)
354 MD5_Update(&ctx
, data
, size
);
355 MD5_Final(md5
, &ctx
);
358 static int get_file_stat(struct file_info
*fdata
)
363 if (fdata
->file_name
== NULL
)
366 res
= stat(fdata
->file_name
, &st
);
368 ERRS("stat failed on %s", fdata
->file_name
);
372 fdata
->file_size
= st
.st_size
;
376 static int read_to_buf(struct file_info
*fdata
, char *buf
)
379 int ret
= EXIT_FAILURE
;
381 f
= fopen(fdata
->file_name
, "r");
383 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
388 fread(buf
, fdata
->file_size
, 1, f
);
390 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
402 static int check_options(void)
406 if (inspect_info
.file_name
) {
407 ret
= get_file_stat(&inspect_info
);
412 } else if (extract
) {
413 ERR("no firmware for inspection specified");
417 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
418 ERR("either board or hardware id must be specified");
423 board
= find_board(board_id
);
425 ERR("unknown/unsupported board id \"%s\"", board_id
);
428 if (layout_id
== NULL
)
429 layout_id
= board
->layout_id
;
431 hw_id
= board
->hw_id
;
432 hw_rev
= board
->hw_rev
;
434 if (layout_id
== NULL
) {
435 ERR("flash layout is not specified");
438 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
441 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
446 layout
= find_layout(layout_id
);
447 if (layout
== NULL
) {
448 ERR("unknown flash layout \"%s\"", layout_id
);
453 kernel_la
= layout
->kernel_la
;
455 kernel_ep
= layout
->kernel_ep
;
457 rootfs_ofs
= layout
->rootfs_ofs
;
459 if (kernel_info
.file_name
== NULL
) {
460 ERR("no kernel image specified");
464 ret
= get_file_stat(&kernel_info
);
468 kernel_len
= kernel_info
.file_size
;
471 if (kernel_info
.file_size
>
472 layout
->fw_max_len
- sizeof(struct fw_header
)) {
473 ERR("kernel image is too big");
477 if (rootfs_info
.file_name
== NULL
) {
478 ERR("no rootfs image specified");
482 ret
= get_file_stat(&rootfs_info
);
487 kernel_len
+= sizeof(struct fw_header
);
488 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
489 kernel_len
-= sizeof(struct fw_header
);
491 DBG("kernel length aligned to %u", kernel_len
);
493 if (kernel_len
+ rootfs_info
.file_size
>
494 layout
->fw_max_len
- sizeof(struct fw_header
)) {
495 ERR("images are too big");
499 if (kernel_info
.file_size
>
500 rootfs_ofs
- sizeof(struct fw_header
)) {
501 ERR("kernel image is too big");
505 if (rootfs_info
.file_size
>
506 (layout
->fw_max_len
- rootfs_ofs
)) {
507 ERR("rootfs image is too big");
513 if (ofname
== NULL
) {
514 ERR("no output file specified");
521 static void fill_header(char *buf
, int len
)
523 struct fw_header
*hdr
= (struct fw_header
*)buf
;
525 memset(hdr
, 0, sizeof(struct fw_header
));
527 hdr
->version
= htonl(HEADER_VERSION_V1
);
528 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
529 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
530 hdr
->hw_id
= htonl(hw_id
);
531 hdr
->hw_rev
= htonl(hw_rev
);
533 if (boot_info
.file_size
== 0)
534 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
536 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
538 hdr
->kernel_la
= htonl(kernel_la
);
539 hdr
->kernel_ep
= htonl(kernel_ep
);
540 hdr
->fw_length
= htonl(layout
->fw_max_len
);
541 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
542 hdr
->kernel_len
= htonl(kernel_len
);
544 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
545 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
548 get_md5(buf
, len
, hdr
->md5sum1
);
551 static int pad_jffs2(char *buf
, int currlen
)
557 pad_mask
= (64 * 1024);
558 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
562 for (i
= 10; i
< 32; i
++) {
568 len
= ALIGN(len
, mask
);
570 for (i
= 10; i
< 32; i
++) {
572 if ((len
& (mask
- 1)) == 0)
576 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
577 buf
[len
+ i
] = jffs2_eof_mark
[i
];
579 len
+= sizeof(jffs2_eof_mark
);
585 static int write_fw(char *data
, int len
)
588 int ret
= EXIT_FAILURE
;
590 f
= fopen(ofname
, "w");
592 ERRS("could not open \"%s\" for writing", ofname
);
597 fwrite(data
, len
, 1, f
);
599 ERRS("unable to write output file");
603 DBG("firmware file \"%s\" completed", ofname
);
610 if (ret
!= EXIT_SUCCESS
) {
617 static int build_fw(void)
622 int ret
= EXIT_FAILURE
;
625 buflen
= layout
->fw_max_len
;
627 buf
= malloc(buflen
);
629 ERR("no memory for buffer\n");
633 memset(buf
, 0xff, buflen
);
634 p
= buf
+ sizeof(struct fw_header
);
635 ret
= read_to_buf(&kernel_info
, p
);
639 writelen
= sizeof(struct fw_header
) + kernel_len
;
645 p
= buf
+ rootfs_ofs
;
647 ret
= read_to_buf(&rootfs_info
, p
);
652 writelen
+= rootfs_info
.file_size
;
654 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
657 writelen
= pad_jffs2(buf
, writelen
);
663 fill_header(buf
, writelen
);
664 ret
= write_fw(buf
, writelen
);
676 /* Helper functions to inspect_fw() representing different output formats */
677 static inline void inspect_fw_pstr(char *label
, char *str
)
679 printf("%-23s: %s\n", label
, str
);
682 static inline void inspect_fw_phex(char *label
, uint32_t val
)
684 printf("%-23s: 0x%08x\n", label
, val
);
687 static inline void inspect_fw_phexpost(char *label
,
688 uint32_t val
, char *post
)
690 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
693 static inline void inspect_fw_phexdef(char *label
,
694 uint32_t val
, uint32_t defval
)
696 printf("%-23s: 0x%08x ", label
, val
);
699 printf("(== OpenWrt default)\n");
701 printf("(OpenWrt default: 0x%08x)\n", defval
);
704 static inline void inspect_fw_phexexp(char *label
,
705 uint32_t val
, uint32_t expval
)
707 printf("%-23s: 0x%08x ", label
, val
);
712 printf("(expected: 0x%08x)\n", expval
);
715 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
717 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
720 static inline void inspect_fw_phexdecdef(char *label
,
721 uint32_t val
, uint32_t defval
)
723 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
726 printf("(== OpenWrt default)\n");
728 printf("(OpenWrt default: 0x%08x)\n", defval
);
731 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
735 printf("%-23s:", label
);
736 for (i
=0; i
<MD5SUM_LEN
; i
++)
737 printf(" %02x", val
[i
]);
738 printf(" %s\n", text
);
741 static int inspect_fw(void)
744 struct fw_header
*hdr
;
745 uint8_t md5sum
[MD5SUM_LEN
];
746 struct board_info
*board
;
747 int ret
= EXIT_FAILURE
;
749 buf
= malloc(inspect_info
.file_size
);
751 ERR("no memory for buffer!\n");
755 ret
= read_to_buf(&inspect_info
, buf
);
758 hdr
= (struct fw_header
*)buf
;
760 inspect_fw_pstr("File name", inspect_info
.file_name
);
761 inspect_fw_phexdec("File size", inspect_info
.file_size
);
763 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
764 ERR("file does not seem to have V1 header!\n");
768 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
770 if (ntohl(hdr
->unk1
) != 0)
771 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
773 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
774 if (ntohl(hdr
->boot_len
) == 0)
775 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
777 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
778 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
780 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
781 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
782 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
784 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
786 if (ntohl(hdr
->unk2
) != 0)
787 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
788 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
789 "(purpose yet unknown, unchecked here)");
790 if (ntohl(hdr
->unk3
) != 0)
791 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
795 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
796 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
797 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
799 layout
= find_layout(board
->layout_id
);
800 inspect_fw_phexpost("Hardware ID",
801 ntohl(hdr
->hw_id
), board
->id
);
802 inspect_fw_phexexp("Hardware Revision",
803 ntohl(hdr
->hw_rev
), board
->hw_rev
);
805 inspect_fw_phexpost("Hardware ID",
806 ntohl(hdr
->hw_id
), "unknown");
807 inspect_fw_phex("Hardware Revision",
813 inspect_fw_phexdec("Kernel data offset",
814 ntohl(hdr
->kernel_ofs
));
815 inspect_fw_phexdec("Kernel data length",
816 ntohl(hdr
->kernel_len
));
818 inspect_fw_phexdef("Kernel load address",
819 ntohl(hdr
->kernel_la
),
820 layout
? layout
->kernel_la
: 0xffffffff);
821 inspect_fw_phexdef("Kernel entry point",
822 ntohl(hdr
->kernel_ep
),
823 layout
? layout
->kernel_ep
: 0xffffffff);
824 inspect_fw_phexdecdef("Rootfs data offset",
825 ntohl(hdr
->rootfs_ofs
),
826 layout
? layout
->rootfs_ofs
: 0xffffffff);
828 inspect_fw_phex("Kernel load address",
829 ntohl(hdr
->kernel_la
));
830 inspect_fw_phex("Kernel entry point",
831 ntohl(hdr
->kernel_ep
));
832 inspect_fw_phexdec("Rootfs data offset",
833 ntohl(hdr
->rootfs_ofs
));
835 inspect_fw_phexdec("Rootfs data length",
836 ntohl(hdr
->rootfs_len
));
837 inspect_fw_phexdec("Boot loader data offset",
838 ntohl(hdr
->boot_ofs
));
839 inspect_fw_phexdec("Boot loader data length",
840 ntohl(hdr
->boot_len
));
841 inspect_fw_phexdec("Total firmware length",
842 ntohl(hdr
->fw_length
));
850 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
851 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
852 printf("Extracting kernel to \"%s\"...\n", filename
);
853 fp
= fopen(filename
, "w");
855 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
856 ntohl(hdr
->kernel_len
), 1, fp
)) {
857 ERR("error in fwrite(): %s", strerror(errno
));
861 ERR("error in fopen(): %s", strerror(errno
));
865 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
866 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
867 printf("Extracting rootfs to \"%s\"...\n", filename
);
868 fp
= fopen(filename
, "w");
870 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
871 ntohl(hdr
->rootfs_len
), 1, fp
)) {
872 ERR("error in fwrite(): %s", strerror(errno
));
876 ERR("error in fopen(): %s", strerror(errno
));
887 int main(int argc
, char *argv
[])
889 int ret
= EXIT_FAILURE
;
894 progname
= basename(argv
[0]);
899 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsj");
905 sscanf(optarg
, "0x%x", &rootfs_align
);
914 sscanf(optarg
, "0x%x", &kernel_ep
);
923 sscanf(optarg
, "0x%x", &kernel_la
);
935 kernel_info
.file_name
= optarg
;
938 rootfs_info
.file_name
= optarg
;
941 sscanf(optarg
, "0x%x", &rootfs_ofs
);
950 inspect_info
.file_name
= optarg
;
967 ret
= check_options();
971 if (!inspect_info
.file_name
)
This page took 0.081302 seconds and 5 git commands to generate.