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_MR3020_V1 0x30200001
34 #define HWID_TL_MR3220_V1 0x32200001
35 #define HWID_TL_MR3420_V1 0x34200001
36 #define HWID_TL_WA701N_V1 0x07010001
37 #define HWID_TL_WA901ND_V1 0x09010001
38 #define HWID_TL_WA901ND_V2 0x09010002
39 #define HWID_TL_WR703N_V1 0x07030101
40 #define HWID_TL_WR741ND_V1 0x07410001
41 #define HWID_TL_WR741ND_V4 0x07410004
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
52 #define HWID_TL_WR2543N_V1 0x25430001
57 char *file_name
; /* name of the file */
58 uint32_t file_size
; /* length of the file */
62 uint32_t version
; /* header version */
65 uint32_t hw_id
; /* hardware id */
66 uint32_t hw_rev
; /* hardware revision */
68 uint8_t md5sum1
[MD5SUM_LEN
];
70 uint8_t md5sum2
[MD5SUM_LEN
];
72 uint32_t kernel_la
; /* kernel load address */
73 uint32_t kernel_ep
; /* kernel entry point */
74 uint32_t fw_length
; /* total length of the firmware */
75 uint32_t kernel_ofs
; /* kernel data offset */
76 uint32_t kernel_len
; /* kernel data length */
77 uint32_t rootfs_ofs
; /* rootfs data offset */
78 uint32_t rootfs_len
; /* rootfs data length */
79 uint32_t boot_ofs
; /* bootloader data offset */
80 uint32_t boot_len
; /* bootloader data length */
82 } __attribute__ ((packed
));
103 static char *progname
;
104 static char *vendor
= "TP-LINK Technologies";
105 static char *version
= "ver. 1.0";
107 static char *board_id
;
108 static struct board_info
*board
;
109 static char *layout_id
;
110 static struct flash_layout
*layout
;
111 static char *opt_hw_id
;
112 static uint32_t hw_id
;
113 static char *opt_hw_rev
;
114 static uint32_t hw_rev
;
115 static struct file_info kernel_info
;
116 static uint32_t kernel_la
= 0;
117 static uint32_t kernel_ep
= 0;
118 static uint32_t kernel_len
= 0;
119 static struct file_info rootfs_info
;
120 static uint32_t rootfs_ofs
= 0;
121 static uint32_t rootfs_align
;
122 static struct file_info boot_info
;
124 static int strip_padding
;
125 static int add_jffs2_eof
;
126 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
128 static struct file_info inspect_info
;
129 static int extract
= 0;
131 char md5salt_normal
[MD5SUM_LEN
] = {
132 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
133 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
136 char md5salt_boot
[MD5SUM_LEN
] = {
137 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
138 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
141 static struct flash_layout layouts
[] = {
144 .fw_max_len
= 0x3c0000,
145 .kernel_la
= 0x80060000,
146 .kernel_ep
= 0x80060000,
147 .rootfs_ofs
= 0x140000,
150 .fw_max_len
= 0x3c0000,
151 .kernel_la
= 0x80060000,
152 .kernel_ep
= 0x80060000,
153 .rootfs_ofs
= 0x100000,
156 .fw_max_len
= 0x7c0000,
157 .kernel_la
= 0x80060000,
158 .kernel_ep
= 0x80060000,
159 .rootfs_ofs
= 0x140000,
162 .fw_max_len
= 0x7c0000,
163 .kernel_la
= 0x80060000,
164 .kernel_ep
= 0x80060000,
165 .rootfs_ofs
= 0x100000,
167 /* terminating entry */
171 static struct board_info boards
[] = {
174 .hw_id
= HWID_TL_MR3020_V1
,
176 .layout_id
= "4Mlzma",
179 .hw_id
= HWID_TL_MR3220_V1
,
184 .hw_id
= HWID_TL_MR3420_V1
,
189 .hw_id
= HWID_TL_WA701N_V1
,
193 .id
= "TL-WA901NDv1",
194 .hw_id
= HWID_TL_WA901ND_V1
,
198 .id
= "TL-WA901NDv2",
199 .hw_id
= HWID_TL_WA901ND_V2
,
203 .id
= "TL-WR741NDv1",
204 .hw_id
= HWID_TL_WR741ND_V1
,
208 .id
= "TL-WR741NDv4",
209 .hw_id
= HWID_TL_WR741ND_V4
,
211 .layout_id
= "4Mlzma",
214 .hw_id
= HWID_TL_WR740N_V1
,
219 .hw_id
= HWID_TL_WR740N_V3
,
223 .id
= "TL-WR743NDv1",
224 .hw_id
= HWID_TL_WR743ND_V1
,
228 .id
= "TL-WR841Nv1.5",
229 .hw_id
= HWID_TL_WR841N_V1_5
,
233 .id
= "TL-WR841NDv3",
234 .hw_id
= HWID_TL_WR841ND_V3
,
238 .id
= "TL-WR841NDv5",
239 .hw_id
= HWID_TL_WR841ND_V5
,
243 .id
= "TL-WR841NDv7",
244 .hw_id
= HWID_TL_WR841ND_V7
,
248 .id
= "TL-WR941NDv2",
249 .hw_id
= HWID_TL_WR941ND_V2
,
253 .id
= "TL-WR941NDv4",
254 .hw_id
= HWID_TL_WR941ND_V4
,
258 .id
= "TL-WR1043NDv1",
259 .hw_id
= HWID_TL_WR1043ND_V1
,
263 .id
= "TL-WR2543Nv1",
264 .hw_id
= HWID_TL_WR2543N_V1
,
266 .layout_id
= "8Mlzma",
269 .hw_id
= HWID_TL_WR703N_V1
,
271 .layout_id
= "4Mlzma",
273 /* terminating entry */
280 #define ERR(fmt, ...) do { \
282 fprintf(stderr, "[%s] *** error: " fmt "\n", \
283 progname, ## __VA_ARGS__ ); \
286 #define ERRS(fmt, ...) do { \
289 fprintf(stderr, "[%s] *** error: " fmt "\n", \
290 progname, ## __VA_ARGS__, strerror(save)); \
293 #define DBG(fmt, ...) do { \
294 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
297 static struct board_info
*find_board(char *id
)
299 struct board_info
*ret
;
300 struct board_info
*board
;
303 for (board
= boards
; board
->id
!= NULL
; board
++){
304 if (strcasecmp(id
, board
->id
) == 0) {
313 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
315 struct board_info
*board
;
317 for (board
= boards
; board
->id
!= NULL
; board
++) {
318 if (hw_id
== board
->hw_id
)
325 static struct flash_layout
*find_layout(char *id
)
327 struct flash_layout
*ret
;
328 struct flash_layout
*l
;
331 for (l
= layouts
; l
->id
!= NULL
; l
++){
332 if (strcasecmp(id
, l
->id
) == 0) {
341 static void usage(int status
)
343 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
344 struct board_info
*board
;
346 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
350 " -B <board> create image for the board specified with <board>\n"
351 " -c use combined kernel image\n"
352 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
353 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
354 " -H <hwid> use hardware id specified with <hwid>\n"
355 " -F <id> use flash layout specified with <id>\n"
356 " -k <file> read kernel image from the file <file>\n"
357 " -r <file> read rootfs image from the file <file>\n"
358 " -a <align> align the rootfs start on an <align> bytes boundary\n"
359 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
360 " -o <file> write output to the file <file>\n"
361 " -s strip padding from the end of the image\n"
362 " -j add jffs2 end-of-filesystem markers\n"
363 " -N <vendor> set image vendor to <vendor>\n"
364 " -V <version> set image version to <version>\n"
365 " -i <file> inspect given firmware file <file>\n"
366 " -x extract kernel and rootfs while inspecting (requires -i)\n"
367 " -h show this screen\n"
373 static int get_md5(char *data
, int size
, char *md5
)
378 MD5_Update(&ctx
, data
, size
);
379 MD5_Final(md5
, &ctx
);
382 static int get_file_stat(struct file_info
*fdata
)
387 if (fdata
->file_name
== NULL
)
390 res
= stat(fdata
->file_name
, &st
);
392 ERRS("stat failed on %s", fdata
->file_name
);
396 fdata
->file_size
= st
.st_size
;
400 static int read_to_buf(struct file_info
*fdata
, char *buf
)
403 int ret
= EXIT_FAILURE
;
405 f
= fopen(fdata
->file_name
, "r");
407 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
412 fread(buf
, fdata
->file_size
, 1, f
);
414 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
426 static int check_options(void)
430 if (inspect_info
.file_name
) {
431 ret
= get_file_stat(&inspect_info
);
436 } else if (extract
) {
437 ERR("no firmware for inspection specified");
441 if (board_id
== NULL
&& opt_hw_id
== NULL
) {
442 ERR("either board or hardware id must be specified");
447 board
= find_board(board_id
);
449 ERR("unknown/unsupported board id \"%s\"", board_id
);
452 if (layout_id
== NULL
)
453 layout_id
= board
->layout_id
;
455 hw_id
= board
->hw_id
;
456 hw_rev
= board
->hw_rev
;
458 if (layout_id
== NULL
) {
459 ERR("flash layout is not specified");
462 hw_id
= strtoul(opt_hw_id
, NULL
, 0);
465 hw_rev
= strtoul(opt_hw_rev
, NULL
, 0);
470 layout
= find_layout(layout_id
);
471 if (layout
== NULL
) {
472 ERR("unknown flash layout \"%s\"", layout_id
);
477 kernel_la
= layout
->kernel_la
;
479 kernel_ep
= layout
->kernel_ep
;
481 rootfs_ofs
= layout
->rootfs_ofs
;
483 if (kernel_info
.file_name
== NULL
) {
484 ERR("no kernel image specified");
488 ret
= get_file_stat(&kernel_info
);
492 kernel_len
= kernel_info
.file_size
;
495 if (kernel_info
.file_size
>
496 layout
->fw_max_len
- sizeof(struct fw_header
)) {
497 ERR("kernel image is too big");
501 if (rootfs_info
.file_name
== NULL
) {
502 ERR("no rootfs image specified");
506 ret
= get_file_stat(&rootfs_info
);
511 kernel_len
+= sizeof(struct fw_header
);
512 kernel_len
= ALIGN(kernel_len
, rootfs_align
);
513 kernel_len
-= sizeof(struct fw_header
);
515 DBG("kernel length aligned to %u", kernel_len
);
517 if (kernel_len
+ rootfs_info
.file_size
>
518 layout
->fw_max_len
- sizeof(struct fw_header
)) {
519 ERR("images are too big");
523 if (kernel_info
.file_size
>
524 rootfs_ofs
- sizeof(struct fw_header
)) {
525 ERR("kernel image is too big");
529 if (rootfs_info
.file_size
>
530 (layout
->fw_max_len
- rootfs_ofs
)) {
531 ERR("rootfs image is too big");
537 if (ofname
== NULL
) {
538 ERR("no output file specified");
545 static void fill_header(char *buf
, int len
)
547 struct fw_header
*hdr
= (struct fw_header
*)buf
;
549 memset(hdr
, 0, sizeof(struct fw_header
));
551 hdr
->version
= htonl(HEADER_VERSION_V1
);
552 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
553 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
554 hdr
->hw_id
= htonl(hw_id
);
555 hdr
->hw_rev
= htonl(hw_rev
);
557 if (boot_info
.file_size
== 0)
558 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
560 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
562 hdr
->kernel_la
= htonl(kernel_la
);
563 hdr
->kernel_ep
= htonl(kernel_ep
);
564 hdr
->fw_length
= htonl(layout
->fw_max_len
);
565 hdr
->kernel_ofs
= htonl(sizeof(struct fw_header
));
566 hdr
->kernel_len
= htonl(kernel_len
);
568 hdr
->rootfs_ofs
= htonl(rootfs_ofs
);
569 hdr
->rootfs_len
= htonl(rootfs_info
.file_size
);
572 get_md5(buf
, len
, hdr
->md5sum1
);
575 static int pad_jffs2(char *buf
, int currlen
)
581 pad_mask
= (64 * 1024);
582 while ((len
< layout
->fw_max_len
) && (pad_mask
!= 0)) {
586 for (i
= 10; i
< 32; i
++) {
592 len
= ALIGN(len
, mask
);
594 for (i
= 10; i
< 32; i
++) {
596 if ((len
& (mask
- 1)) == 0)
600 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
601 buf
[len
+ i
] = jffs2_eof_mark
[i
];
603 len
+= sizeof(jffs2_eof_mark
);
609 static int write_fw(char *data
, int len
)
612 int ret
= EXIT_FAILURE
;
614 f
= fopen(ofname
, "w");
616 ERRS("could not open \"%s\" for writing", ofname
);
621 fwrite(data
, len
, 1, f
);
623 ERRS("unable to write output file");
627 DBG("firmware file \"%s\" completed", ofname
);
634 if (ret
!= EXIT_SUCCESS
) {
641 static int build_fw(void)
646 int ret
= EXIT_FAILURE
;
649 buflen
= layout
->fw_max_len
;
651 buf
= malloc(buflen
);
653 ERR("no memory for buffer\n");
657 memset(buf
, 0xff, buflen
);
658 p
= buf
+ sizeof(struct fw_header
);
659 ret
= read_to_buf(&kernel_info
, p
);
663 writelen
= sizeof(struct fw_header
) + kernel_len
;
669 p
= buf
+ rootfs_ofs
;
671 ret
= read_to_buf(&rootfs_info
, p
);
676 writelen
+= rootfs_info
.file_size
;
678 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
681 writelen
= pad_jffs2(buf
, writelen
);
687 fill_header(buf
, writelen
);
688 ret
= write_fw(buf
, writelen
);
700 /* Helper functions to inspect_fw() representing different output formats */
701 static inline void inspect_fw_pstr(char *label
, char *str
)
703 printf("%-23s: %s\n", label
, str
);
706 static inline void inspect_fw_phex(char *label
, uint32_t val
)
708 printf("%-23s: 0x%08x\n", label
, val
);
711 static inline void inspect_fw_phexpost(char *label
,
712 uint32_t val
, char *post
)
714 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
717 static inline void inspect_fw_phexdef(char *label
,
718 uint32_t val
, uint32_t defval
)
720 printf("%-23s: 0x%08x ", label
, val
);
723 printf("(== OpenWrt default)\n");
725 printf("(OpenWrt default: 0x%08x)\n", defval
);
728 static inline void inspect_fw_phexexp(char *label
,
729 uint32_t val
, uint32_t expval
)
731 printf("%-23s: 0x%08x ", label
, val
);
736 printf("(expected: 0x%08x)\n", expval
);
739 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
741 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
744 static inline void inspect_fw_phexdecdef(char *label
,
745 uint32_t val
, uint32_t defval
)
747 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
750 printf("(== OpenWrt default)\n");
752 printf("(OpenWrt default: 0x%08x)\n", defval
);
755 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
759 printf("%-23s:", label
);
760 for (i
=0; i
<MD5SUM_LEN
; i
++)
761 printf(" %02x", val
[i
]);
762 printf(" %s\n", text
);
765 static int inspect_fw(void)
768 struct fw_header
*hdr
;
769 uint8_t md5sum
[MD5SUM_LEN
];
770 struct board_info
*board
;
771 int ret
= EXIT_FAILURE
;
773 buf
= malloc(inspect_info
.file_size
);
775 ERR("no memory for buffer!\n");
779 ret
= read_to_buf(&inspect_info
, buf
);
782 hdr
= (struct fw_header
*)buf
;
784 inspect_fw_pstr("File name", inspect_info
.file_name
);
785 inspect_fw_phexdec("File size", inspect_info
.file_size
);
787 if (ntohl(hdr
->version
) != HEADER_VERSION_V1
) {
788 ERR("file does not seem to have V1 header!\n");
792 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
794 if (ntohl(hdr
->unk1
) != 0)
795 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
797 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
798 if (ntohl(hdr
->boot_len
) == 0)
799 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
801 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
802 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
804 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
805 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
806 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
808 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
810 if (ntohl(hdr
->unk2
) != 0)
811 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
812 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
813 "(purpose yet unknown, unchecked here)");
814 if (ntohl(hdr
->unk3
) != 0)
815 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
819 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
820 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
821 board
= find_board_by_hwid(ntohl(hdr
->hw_id
));
823 layout
= find_layout(board
->layout_id
);
824 inspect_fw_phexpost("Hardware ID",
825 ntohl(hdr
->hw_id
), board
->id
);
826 inspect_fw_phexexp("Hardware Revision",
827 ntohl(hdr
->hw_rev
), board
->hw_rev
);
829 inspect_fw_phexpost("Hardware ID",
830 ntohl(hdr
->hw_id
), "unknown");
831 inspect_fw_phex("Hardware Revision",
837 inspect_fw_phexdec("Kernel data offset",
838 ntohl(hdr
->kernel_ofs
));
839 inspect_fw_phexdec("Kernel data length",
840 ntohl(hdr
->kernel_len
));
842 inspect_fw_phexdef("Kernel load address",
843 ntohl(hdr
->kernel_la
),
844 layout
? layout
->kernel_la
: 0xffffffff);
845 inspect_fw_phexdef("Kernel entry point",
846 ntohl(hdr
->kernel_ep
),
847 layout
? layout
->kernel_ep
: 0xffffffff);
848 inspect_fw_phexdecdef("Rootfs data offset",
849 ntohl(hdr
->rootfs_ofs
),
850 layout
? layout
->rootfs_ofs
: 0xffffffff);
852 inspect_fw_phex("Kernel load address",
853 ntohl(hdr
->kernel_la
));
854 inspect_fw_phex("Kernel entry point",
855 ntohl(hdr
->kernel_ep
));
856 inspect_fw_phexdec("Rootfs data offset",
857 ntohl(hdr
->rootfs_ofs
));
859 inspect_fw_phexdec("Rootfs data length",
860 ntohl(hdr
->rootfs_len
));
861 inspect_fw_phexdec("Boot loader data offset",
862 ntohl(hdr
->boot_ofs
));
863 inspect_fw_phexdec("Boot loader data length",
864 ntohl(hdr
->boot_len
));
865 inspect_fw_phexdec("Total firmware length",
866 ntohl(hdr
->fw_length
));
874 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
875 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
876 printf("Extracting kernel to \"%s\"...\n", filename
);
877 fp
= fopen(filename
, "w");
879 if (!fwrite(buf
+ ntohl(hdr
->kernel_ofs
),
880 ntohl(hdr
->kernel_len
), 1, fp
)) {
881 ERR("error in fwrite(): %s", strerror(errno
));
885 ERR("error in fopen(): %s", strerror(errno
));
889 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
890 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
891 printf("Extracting rootfs to \"%s\"...\n", filename
);
892 fp
= fopen(filename
, "w");
894 if (!fwrite(buf
+ ntohl(hdr
->rootfs_ofs
),
895 ntohl(hdr
->rootfs_len
), 1, fp
)) {
896 ERR("error in fwrite(): %s", strerror(errno
));
900 ERR("error in fopen(): %s", strerror(errno
));
911 int main(int argc
, char *argv
[])
913 int ret
= EXIT_FAILURE
;
918 progname
= basename(argv
[0]);
923 c
= getopt(argc
, argv
, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsj");
929 sscanf(optarg
, "0x%x", &rootfs_align
);
938 sscanf(optarg
, "0x%x", &kernel_ep
);
947 sscanf(optarg
, "0x%x", &kernel_la
);
959 kernel_info
.file_name
= optarg
;
962 rootfs_info
.file_name
= optarg
;
965 sscanf(optarg
, "0x%x", &rootfs_ofs
);
974 inspect_info
.file_name
= optarg
;
991 ret
= check_options();
995 if (!inspect_info
.file_name
)
This page took 0.110694 seconds and 5 git commands to generate.