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_MR3420_V1 0x34200001
37 #define HWID_TL_WA901ND_V1 0x09010001
38 #define HWID_TL_WR741ND_V1 0x07410001
39 #define HWID_TL_WR841N_V1_5 0x08410002
40 #define HWID_TL_WR841ND_V3 0x08410003
41 #define HWID_TL_WR841ND_V5 0x08410005
42 #define HWID_TL_WR841ND_V7 0x08410007
43 #define HWID_TL_WR941ND_V2 0x09410002
44 #define HWID_TL_WR941ND_V4 0x09410004
45 #define HWID_TL_WR1043ND_V1 0x10430001
50 char *file_name
; /* name of the file */
51 uint32_t file_size
; /* length of the file */
55 uint32_t version
; /* header version */
58 uint32_t hw_id
; /* hardware id */
59 uint32_t hw_rev
; /* hardware revision */
61 uint8_t md5sum1
[MD5SUM_LEN
];
63 uint8_t md5sum2
[MD5SUM_LEN
];
65 uint32_t kernel_la
; /* kernel load address */
66 uint32_t kernel_ep
; /* kernel entry point */
67 uint32_t fw_length
; /* total length of the firmware */
68 uint32_t kernel_ofs
; /* kernel data offset */
69 uint32_t kernel_len
; /* kernel data length */
70 uint32_t rootfs_ofs
; /* rootfs data offset */
71 uint32_t rootfs_len
; /* rootfs data length */
72 uint32_t boot_ofs
; /* bootloader data offset */
73 uint32_t boot_len
; /* bootloader data length */
75 } __attribute__ ((packed
));
91 static char *progname
;
92 static char *vendor
= "TP-LINK Technologies";
93 static char *version
= "ver. 1.0";
95 static char *board_id
;
96 static struct board_info
*board
;
97 static struct file_info kernel_info
;
98 static uint32_t kernel_la
= 0;
99 static uint32_t kernel_ep
= 0;
100 static struct file_info rootfs_info
;
101 static uint32_t rootfs_ofs
= 0;
102 static struct file_info boot_info
;
104 static int strip_padding
;
106 static struct file_info inspect_info
;
107 static int extract
= 0;
109 char md5salt_normal
[MD5SUM_LEN
] = {
110 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
111 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
114 char md5salt_boot
[MD5SUM_LEN
] = {
115 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
116 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
119 static struct board_info boards
[] = {
122 .hw_id
= HWID_TL_MR3420_V1
,
124 .fw_max_len
= 0x3c0000,
125 .kernel_la
= 0x80060000,
126 .kernel_ep
= 0x80060000,
127 .rootfs_ofs
= 0x140000,
129 .id
= "TL-WA901NDv1",
130 .hw_id
= HWID_TL_WA901ND_V1
,
132 .fw_max_len
= 0x3c0000,
133 .kernel_la
= 0x80060000,
134 .kernel_ep
= 0x80060000,
135 .rootfs_ofs
= 0x140000,
137 .id
= "TL-WR741NDv1",
138 .hw_id
= HWID_TL_WR741ND_V1
,
140 .fw_max_len
= 0x3c0000,
141 .kernel_la
= 0x80060000,
142 .kernel_ep
= 0x80060000,
143 .rootfs_ofs
= 0x140000,
145 .id
= "TL-WR841Nv1.5",
146 .hw_id
= HWID_TL_WR841N_V1_5
,
148 .fw_max_len
= 0x3c0000,
149 .kernel_la
= 0x80060000,
150 .kernel_ep
= 0x80060000,
151 .rootfs_ofs
= 0x140000,
153 .id
= "TL-WR841NDv3",
154 .hw_id
= HWID_TL_WR841ND_V3
,
156 .fw_max_len
= 0x3c0000,
157 .kernel_la
= 0x80060000,
158 .kernel_ep
= 0x80060000,
159 .rootfs_ofs
= 0x140000,
161 .id
= "TL-WR841NDv5",
162 .hw_id
= HWID_TL_WR841ND_V5
,
164 .fw_max_len
= 0x3c0000,
165 .kernel_la
= 0x80060000,
166 .kernel_ep
= 0x80060000,
167 .rootfs_ofs
= 0x140000,
169 .id
= "TL-WR841NDv7",
170 .hw_id
= HWID_TL_WR841ND_V7
,
172 .fw_max_len
= 0x3c0000,
173 .kernel_la
= 0x80060000,
174 .kernel_ep
= 0x80060000,
175 .rootfs_ofs
= 0x140000,
177 .id
= "TL-WR941NDv2",
178 .hw_id
= HWID_TL_WR941ND_V2
,
180 .fw_max_len
= 0x3c0000,
181 .kernel_la
= 0x80060000,
182 .kernel_ep
= 0x80060000,
183 .rootfs_ofs
= 0x140000,
185 .id
= "TL-WR941NDv4",
186 .hw_id
= HWID_TL_WR941ND_V4
,
188 .fw_max_len
= 0x3c0000,
189 .kernel_la
= 0x80060000,
190 .kernel_ep
= 0x80060000,
191 .rootfs_ofs
= 0x140000,
193 .id
= "TL-WR1043NDv1",
194 .hw_id
= HWID_TL_WR1043ND_V1
,
196 .fw_max_len
= 0x7c0000,
197 .kernel_la
= 0x80060000,
198 .kernel_ep
= 0x80060000,
199 .rootfs_ofs
= 0x140000,
201 /* terminating entry */
208 #define ERR(fmt, ...) do { \
210 fprintf(stderr, "[%s] *** error: " fmt "\n", \
211 progname, ## __VA_ARGS__ ); \
214 #define ERRS(fmt, ...) do { \
217 fprintf(stderr, "[%s] *** error: " fmt "\n", \
218 progname, ## __VA_ARGS__, strerror(save)); \
221 #define DBG(fmt, ...) do { \
222 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
225 static struct board_info
*find_board(char *id
)
227 struct board_info
*ret
;
228 struct board_info
*board
;
231 for (board
= boards
; board
->id
!= NULL
; board
++){
232 if (strcasecmp(id
, board
->id
) == 0) {
241 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
243 struct board_info
*board
;
245 for (board
= boards
; board
->id
!= NULL
; board
++) {
246 if (hw_id
== board
->hw_id
)
254 static void usage(int status
)
256 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
257 struct board_info
*board
;
259 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
263 " -B <board> create image for the board specified with <board>\n"
264 " -c use combined kernel image\n"
265 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
266 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
267 " -k <file> read kernel image from the file <file>\n"
268 " -r <file> read rootfs image from the file <file>\n"
269 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
270 " -o <file> write output to the file <file>\n"
271 " -s strip padding from the end of the image\n"
272 " -N <vendor> set image vendor to <vendor>\n"
273 " -V <version> set image version to <version>\n"
274 " -i <file> inspect given firmware file <file>\n"
275 " -x extract kernel and rootfs while inspecting (requires -i)\n"
276 " -h show this screen\n"
282 static int get_md5(char *data
, int size
, char *md5
)
287 MD5_Update(&ctx
, data
, size
);
288 MD5_Final(md5
, &ctx
);
291 static int get_file_stat(struct file_info
*fdata
)
296 if (fdata
->file_name
== NULL
)
299 res
= stat(fdata
->file_name
, &st
);
301 ERRS("stat failed on %s", fdata
->file_name
);
305 fdata
->file_size
= st
.st_size
;
309 static int read_to_buf(struct file_info
*fdata
, char *buf
)
312 int ret
= EXIT_FAILURE
;
314 f
= fopen(fdata
->file_name
, "r");
316 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
321 fread(buf
, fdata
->file_size
, 1, f
);
323 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
335 static int check_options(void)
339 if (inspect_info
.file_name
) {
340 ret
= get_file_stat(&inspect_info
);
345 } else if (extract
) {
346 ERR("no firmware for inspection specified");
350 if (board_id
== NULL
) {
351 ERR("no board specified");
355 board
= find_board(board_id
);
357 ERR("unknown/unsupported board id \"%s\"", board_id
);
361 kernel_la
= board
->kernel_la
;
363 kernel_ep
= board
->kernel_ep
;
365 rootfs_ofs
= board
->rootfs_ofs
;
367 if (kernel_info
.file_name
== NULL
) {
368 ERR("no kernel image specified");
372 ret
= get_file_stat(&kernel_info
);
377 if (kernel_info
.file_size
>
378 board
->fw_max_len
- sizeof(struct fw_header
)) {
379 ERR("kernel image is too big");
383 if (kernel_info
.file_size
>
384 rootfs_ofs
- sizeof(struct fw_header
)) {
385 ERR("kernel image is too big");
388 if (rootfs_info
.file_name
== NULL
) {
389 ERR("no rootfs image specified");
393 ret
= get_file_stat(&rootfs_info
);
397 if (rootfs_info
.file_size
>
398 (board
->fw_max_len
- rootfs_ofs
)) {
399 ERR("rootfs image is too big");
404 if (ofname
== NULL
) {
405 ERR("no output file specified");
412 static void fill_header(char *buf
, int len
)
414 struct fw_header
*hdr
= (struct fw_header
*)buf
;
416 memset(hdr
, 0, sizeof(struct fw_header
));
418 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
419 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
420 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
421 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
422 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
424 if (boot_info
.file_size
== 0)
425 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
427 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
429 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
430 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
431 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
432 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
433 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
435 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
436 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
439 get_md5(buf
, len
, hdr
->md5sum1
);
442 static int write_fw(char *data
, int len
)
445 int ret
= EXIT_FAILURE
;
447 f
= fopen(ofname
, "w");
449 ERRS("could not open \"%s\" for writing", ofname
);
454 fwrite(data
, len
, 1, f
);
456 ERRS("unable to write output file");
460 DBG("firmware file \"%s\" completed", ofname
);
467 if (ret
!= EXIT_SUCCESS
) {
474 static int build_fw(void)
479 int ret
= EXIT_FAILURE
;
482 buflen
= board
->fw_max_len
;
484 buf
= malloc(buflen
);
486 ERR("no memory for buffer\n");
490 memset(buf
, 0xff, buflen
);
491 p
= buf
+ sizeof(struct fw_header
);
492 ret
= read_to_buf(&kernel_info
, p
);
496 writelen
= kernel_info
.file_size
;
499 p
= buf
+ rootfs_ofs
;
500 ret
= read_to_buf(&rootfs_info
, p
);
504 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
510 fill_header(buf
, writelen
);
511 ret
= write_fw(buf
, writelen
);
523 /* Helper functions to inspect_fw() representing different output formats */
524 static inline void inspect_fw_pstr(char *label
, char *str
)
526 printf("%-23s: %s\n", label
, str
);
529 static inline void inspect_fw_phex(char *label
, uint32_t val
)
531 printf("%-23s: 0x%08x\n", label
, val
);
534 static inline void inspect_fw_phexpost(char *label
,
535 uint32_t val
, char *post
)
537 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
540 static inline void inspect_fw_phexdef(char *label
,
541 uint32_t val
, uint32_t defval
)
543 printf("%-23s: 0x%08x ", label
, val
);
546 printf("(== OpenWrt default)\n");
548 printf("(OpenWrt default: 0x%08x)\n", defval
);
551 static inline void inspect_fw_phexexp(char *label
,
552 uint32_t val
, uint32_t expval
)
554 printf("%-23s: 0x%08x ", label
, val
);
559 printf("(expected: 0x%08x)\n", expval
);
562 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
564 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
567 static inline void inspect_fw_phexdecdef(char *label
,
568 uint32_t val
, uint32_t defval
)
570 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
573 printf("(== OpenWrt default)\n");
575 printf("(OpenWrt default: 0x%08x)\n", defval
);
578 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
582 printf("%-23s:", label
);
583 for (i
=0; i
<MD5SUM_LEN
; i
++)
584 printf(" %02x", val
[i
]);
585 printf(" %s\n", text
);
588 static int inspect_fw(void)
591 struct fw_header
*hdr
;
592 uint8_t md5sum
[MD5SUM_LEN
];
593 struct board_info
*board
;
594 int ret
= EXIT_FAILURE
;
596 buf
= malloc(inspect_info
.file_size
);
598 ERR("no memory for buffer!\n");
602 ret
= read_to_buf(&inspect_info
, buf
);
605 hdr
= (struct fw_header
*)buf
;
607 inspect_fw_pstr("File name", inspect_info
.file_name
);
608 inspect_fw_phexdec("File size", inspect_info
.file_size
);
610 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
611 ERR("file does not seem to have V1 header!\n");
615 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
617 if (BE32_TO_HOST(hdr
->unk1
) != 0)
618 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
620 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
621 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
622 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
624 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
625 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
627 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
628 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
629 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
631 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
633 if (BE32_TO_HOST(hdr
->unk2
) != 0)
634 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
635 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
636 "(purpose yet unknown, unchecked here)");
637 if (BE32_TO_HOST(hdr
->unk3
) != 0)
638 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
642 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
643 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
644 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
646 inspect_fw_phexpost("Hardware ID",
647 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
648 inspect_fw_phexexp("Hardware Revision",
649 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
651 inspect_fw_phexpost("Hardware ID",
652 BE32_TO_HOST(hdr
->hw_id
), "unknown");
653 inspect_fw_phex("Hardware Revision",
654 BE32_TO_HOST(hdr
->hw_rev
));
659 inspect_fw_phexdec("Kernel data offset",
660 BE32_TO_HOST(hdr
->kernel_ofs
));
661 inspect_fw_phexdec("Kernel data length",
662 BE32_TO_HOST(hdr
->kernel_len
));
664 inspect_fw_phexdef("Kernel load address",
665 BE32_TO_HOST(hdr
->kernel_la
),
667 inspect_fw_phexdef("Kernel entry point",
668 BE32_TO_HOST(hdr
->kernel_ep
),
670 inspect_fw_phexdecdef("Rootfs data offset",
671 BE32_TO_HOST(hdr
->rootfs_ofs
),
674 inspect_fw_phex("Kernel load address",
675 BE32_TO_HOST(hdr
->kernel_la
));
676 inspect_fw_phex("Kernel entry point",
677 BE32_TO_HOST(hdr
->kernel_ep
));
678 inspect_fw_phexdec("Rootfs data offset",
679 BE32_TO_HOST(hdr
->rootfs_ofs
));
681 inspect_fw_phexdec("Rootfs data length",
682 BE32_TO_HOST(hdr
->rootfs_len
));
683 inspect_fw_phexdec("Boot loader data offset",
684 BE32_TO_HOST(hdr
->boot_ofs
));
685 inspect_fw_phexdec("Boot loader data length",
686 BE32_TO_HOST(hdr
->boot_len
));
687 inspect_fw_phexdec("Total firmware length",
688 BE32_TO_HOST(hdr
->fw_length
));
696 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
697 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
698 printf("Extracting kernel to \"%s\"...\n", filename
);
699 fp
= fopen(filename
, "w");
701 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
702 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
703 ERR("error in fwrite(): %s", strerror(errno
));
707 ERR("error in fopen(): %s", strerror(errno
));
711 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
712 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
713 printf("Extracting rootfs to \"%s\"...\n", filename
);
714 fp
= fopen(filename
, "w");
716 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
717 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
718 ERR("error in fwrite(): %s", strerror(errno
));
722 ERR("error in fopen(): %s", strerror(errno
));
733 int main(int argc
, char *argv
[])
735 int ret
= EXIT_FAILURE
;
740 progname
= basename(argv
[0]);
745 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
754 sscanf(optarg
, "0x%x", &kernel_ep
);
757 sscanf(optarg
, "0x%x", &kernel_la
);
769 kernel_info
.file_name
= optarg
;
772 rootfs_info
.file_name
= optarg
;
775 sscanf(optarg
, "0x%x", &rootfs_ofs
);
784 inspect_info
.file_name
= optarg
;
798 ret
= check_options();
802 if (!inspect_info
.file_name
)
This page took 0.11546 seconds and 5 git commands to generate.