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_WA901ND_V1 0x09010001
37 #define HWID_TL_WR741ND_V1 0x07410001
38 #define HWID_TL_WR841N_V1_5 0x08410002
39 #define HWID_TL_WR841ND_V3 0x08410003
40 #define HWID_TL_WR841ND_V5 0x08410005
41 #define HWID_TL_WR941ND_V2 0x09410002
42 #define HWID_TL_WR941ND_V4 0x09410004
43 #define HWID_TL_WR1043ND_V1 0x10430001
48 char *file_name
; /* name of the file */
49 uint32_t file_size
; /* length of the file */
53 uint32_t version
; /* header version */
56 uint32_t hw_id
; /* hardware id */
57 uint32_t hw_rev
; /* hardware revision */
59 uint8_t md5sum1
[MD5SUM_LEN
];
61 uint8_t md5sum2
[MD5SUM_LEN
];
63 uint32_t kernel_la
; /* kernel load address */
64 uint32_t kernel_ep
; /* kernel entry point */
65 uint32_t fw_length
; /* total length of the firmware */
66 uint32_t kernel_ofs
; /* kernel data offset */
67 uint32_t kernel_len
; /* kernel data length */
68 uint32_t rootfs_ofs
; /* rootfs data offset */
69 uint32_t rootfs_len
; /* rootfs data length */
70 uint32_t boot_ofs
; /* bootloader data offset */
71 uint32_t boot_len
; /* bootloader data length */
73 } __attribute__ ((packed
));
89 static char *progname
;
90 static char *vendor
= "TP-LINK Technologies";
91 static char *version
= "ver. 1.0";
93 static char *board_id
;
94 static struct board_info
*board
;
95 static struct file_info kernel_info
;
96 static uint32_t kernel_la
= 0;
97 static uint32_t kernel_ep
= 0;
98 static struct file_info rootfs_info
;
99 static uint32_t rootfs_ofs
= 0;
100 static struct file_info boot_info
;
102 static int strip_padding
;
104 static struct file_info inspect_info
;
105 static int extract
= 0;
107 char md5salt_normal
[MD5SUM_LEN
] = {
108 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
109 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
112 char md5salt_boot
[MD5SUM_LEN
] = {
113 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
114 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
117 static struct board_info boards
[] = {
119 .id
= "TL-WA901NDv1",
120 .hw_id
= HWID_TL_WA901ND_V1
,
122 .fw_max_len
= 0x3c0000,
123 .kernel_la
= 0x80060000,
124 .kernel_ep
= 0x80060000,
125 .rootfs_ofs
= 0x140000,
127 .id
= "TL-WR741NDv1",
128 .hw_id
= HWID_TL_WR741ND_V1
,
130 .fw_max_len
= 0x3c0000,
131 .kernel_la
= 0x80060000,
132 .kernel_ep
= 0x80060000,
133 .rootfs_ofs
= 0x140000,
135 .id
= "TL-WR841Nv1.5",
136 .hw_id
= HWID_TL_WR841N_V1_5
,
138 .fw_max_len
= 0x3c0000,
139 .kernel_la
= 0x80060000,
140 .kernel_ep
= 0x80060000,
141 .rootfs_ofs
= 0x140000,
143 .id
= "TL-WR841NDv3",
144 .hw_id
= HWID_TL_WR841ND_V3
,
146 .fw_max_len
= 0x3c0000,
147 .kernel_la
= 0x80060000,
148 .kernel_ep
= 0x80060000,
149 .rootfs_ofs
= 0x140000,
151 .id
= "TL-WR841NDv5",
152 .hw_id
= HWID_TL_WR841ND_V5
,
154 .fw_max_len
= 0x3c0000,
155 .kernel_la
= 0x80060000,
156 .kernel_ep
= 0x80060000,
157 .rootfs_ofs
= 0x140000,
159 .id
= "TL-WR941NDv2",
160 .hw_id
= HWID_TL_WR941ND_V2
,
162 .fw_max_len
= 0x3c0000,
163 .kernel_la
= 0x80060000,
164 .kernel_ep
= 0x80060000,
165 .rootfs_ofs
= 0x140000,
167 .id
= "TL-WR941NDv4",
168 .hw_id
= HWID_TL_WR941ND_V4
,
170 .fw_max_len
= 0x3c0000,
171 .kernel_la
= 0x80060000,
172 .kernel_ep
= 0x80060000,
173 .rootfs_ofs
= 0x140000,
175 .id
= "TL-WR1043NDv1",
176 .hw_id
= HWID_TL_WR1043ND_V1
,
178 .fw_max_len
= 0x7c0000,
179 .kernel_la
= 0x80060000,
180 .kernel_ep
= 0x80060000,
181 .rootfs_ofs
= 0x140000,
183 /* terminating entry */
190 #define ERR(fmt, ...) do { \
192 fprintf(stderr, "[%s] *** error: " fmt "\n", \
193 progname, ## __VA_ARGS__ ); \
196 #define ERRS(fmt, ...) do { \
199 fprintf(stderr, "[%s] *** error: " fmt "\n", \
200 progname, ## __VA_ARGS__, strerror(save)); \
203 #define DBG(fmt, ...) do { \
204 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
207 static struct board_info
*find_board(char *id
)
209 struct board_info
*ret
;
210 struct board_info
*board
;
213 for (board
= boards
; board
->id
!= NULL
; board
++){
214 if (strcasecmp(id
, board
->id
) == 0) {
223 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
225 struct board_info
*board
;
227 for (board
= boards
; board
->id
!= NULL
; board
++) {
228 if (hw_id
== board
->hw_id
)
236 static void usage(int status
)
238 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
239 struct board_info
*board
;
241 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
245 " -B <board> create image for the board specified with <board>\n"
246 " -c use combined kernel image\n"
247 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
248 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
249 " -k <file> read kernel image from the file <file>\n"
250 " -r <file> read rootfs image from the file <file>\n"
251 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
252 " -o <file> write output to the file <file>\n"
253 " -s strip padding from the end of the image\n"
254 " -N <vendor> set image vendor to <vendor>\n"
255 " -V <version> set image version to <version>\n"
256 " -i <file> inspect given firmware file <file>\n"
257 " -x extract kernel and rootfs while inspecting (requires -i)\n"
258 " -h show this screen\n"
264 static int get_md5(char *data
, int size
, char *md5
)
269 MD5_Update(&ctx
, data
, size
);
270 MD5_Final(md5
, &ctx
);
273 static int get_file_stat(struct file_info
*fdata
)
278 if (fdata
->file_name
== NULL
)
281 res
= stat(fdata
->file_name
, &st
);
283 ERRS("stat failed on %s", fdata
->file_name
);
287 fdata
->file_size
= st
.st_size
;
291 static int read_to_buf(struct file_info
*fdata
, char *buf
)
294 int ret
= EXIT_FAILURE
;
296 f
= fopen(fdata
->file_name
, "r");
298 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
303 fread(buf
, fdata
->file_size
, 1, f
);
305 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
317 static int check_options(void)
321 if (inspect_info
.file_name
) {
322 ret
= get_file_stat(&inspect_info
);
327 } else if (extract
) {
328 ERR("no firmware for inspection specified");
332 if (board_id
== NULL
) {
333 ERR("no board specified");
337 board
= find_board(board_id
);
339 ERR("unknown/unsupported board id \"%s\"", board_id
);
343 kernel_la
= board
->kernel_la
;
345 kernel_ep
= board
->kernel_ep
;
347 rootfs_ofs
= board
->rootfs_ofs
;
349 if (kernel_info
.file_name
== NULL
) {
350 ERR("no kernel image specified");
354 ret
= get_file_stat(&kernel_info
);
359 if (kernel_info
.file_size
>
360 board
->fw_max_len
- sizeof(struct fw_header
)) {
361 ERR("kernel image is too big");
365 if (kernel_info
.file_size
>
366 rootfs_ofs
- sizeof(struct fw_header
)) {
367 ERR("kernel image is too big");
370 if (rootfs_info
.file_name
== NULL
) {
371 ERR("no rootfs image specified");
375 ret
= get_file_stat(&rootfs_info
);
379 if (rootfs_info
.file_size
>
380 (board
->fw_max_len
- rootfs_ofs
)) {
381 ERR("rootfs image is too big");
386 if (ofname
== NULL
) {
387 ERR("no output file specified");
394 static void fill_header(char *buf
, int len
)
396 struct fw_header
*hdr
= (struct fw_header
*)buf
;
398 memset(hdr
, 0, sizeof(struct fw_header
));
400 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
401 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
402 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
403 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
404 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
406 if (boot_info
.file_size
== 0)
407 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
409 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
411 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
412 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
413 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
414 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
415 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
417 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
418 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
421 get_md5(buf
, len
, hdr
->md5sum1
);
424 static int write_fw(char *data
, int len
)
427 int ret
= EXIT_FAILURE
;
429 f
= fopen(ofname
, "w");
431 ERRS("could not open \"%s\" for writing", ofname
);
436 fwrite(data
, len
, 1, f
);
438 ERRS("unable to write output file");
442 DBG("firmware file \"%s\" completed", ofname
);
449 if (ret
!= EXIT_SUCCESS
) {
456 static int build_fw(void)
461 int ret
= EXIT_FAILURE
;
464 buflen
= board
->fw_max_len
;
466 buf
= malloc(buflen
);
468 ERR("no memory for buffer\n");
472 memset(buf
, 0xff, buflen
);
473 p
= buf
+ sizeof(struct fw_header
);
474 ret
= read_to_buf(&kernel_info
, p
);
478 writelen
= kernel_info
.file_size
;
481 p
= buf
+ rootfs_ofs
;
482 ret
= read_to_buf(&rootfs_info
, p
);
486 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
492 fill_header(buf
, writelen
);
493 ret
= write_fw(buf
, writelen
);
505 /* Helper functions to inspect_fw() representing different output formats */
506 static inline void inspect_fw_pstr(char *label
, char *str
)
508 printf("%-23s: %s\n", label
, str
);
511 static inline void inspect_fw_phex(char *label
, uint32_t val
)
513 printf("%-23s: 0x%08x\n", label
, val
);
516 static inline void inspect_fw_phexpost(char *label
,
517 uint32_t val
, char *post
)
519 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
522 static inline void inspect_fw_phexdef(char *label
,
523 uint32_t val
, uint32_t defval
)
525 printf("%-23s: 0x%08x ", label
, val
);
528 printf("(== OpenWrt default)\n");
530 printf("(OpenWrt default: 0x%08x)\n", defval
);
533 static inline void inspect_fw_phexexp(char *label
,
534 uint32_t val
, uint32_t expval
)
536 printf("%-23s: 0x%08x ", label
, val
);
541 printf("(expected: 0x%08x)\n", expval
);
544 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
546 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
549 static inline void inspect_fw_phexdecdef(char *label
,
550 uint32_t val
, uint32_t defval
)
552 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
555 printf("(== OpenWrt default)\n");
557 printf("(OpenWrt default: 0x%08x)\n", defval
);
560 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
564 printf("%-23s:", label
);
565 for (i
=0; i
<MD5SUM_LEN
; i
++)
566 printf(" %02x", val
[i
]);
567 printf(" %s\n", text
);
570 static int inspect_fw(void)
573 struct fw_header
*hdr
;
574 uint8_t md5sum
[MD5SUM_LEN
];
575 struct board_info
*board
;
576 int ret
= EXIT_FAILURE
;
578 buf
= malloc(inspect_info
.file_size
);
580 ERR("no memory for buffer!\n");
584 ret
= read_to_buf(&inspect_info
, buf
);
587 hdr
= (struct fw_header
*)buf
;
589 inspect_fw_pstr("File name", inspect_info
.file_name
);
590 inspect_fw_phexdec("File size", inspect_info
.file_size
);
592 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
593 ERR("file does not seem to have V1 header!\n");
597 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
599 if (BE32_TO_HOST(hdr
->unk1
) != 0)
600 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
602 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
603 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
604 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
606 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
607 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
609 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
610 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
611 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
613 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
615 if (BE32_TO_HOST(hdr
->unk2
) != 0)
616 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
617 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
618 "(purpose yet unknown, unchecked here)");
619 if (BE32_TO_HOST(hdr
->unk3
) != 0)
620 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
624 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
625 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
626 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
628 inspect_fw_phexpost("Hardware ID",
629 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
630 inspect_fw_phexexp("Hardware Revision",
631 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
633 inspect_fw_phexpost("Hardware ID",
634 BE32_TO_HOST(hdr
->hw_id
), "unknown");
635 inspect_fw_phex("Hardware Revision",
636 BE32_TO_HOST(hdr
->hw_rev
));
641 inspect_fw_phexdec("Kernel data offset",
642 BE32_TO_HOST(hdr
->kernel_ofs
));
643 inspect_fw_phexdec("Kernel data length",
644 BE32_TO_HOST(hdr
->kernel_len
));
646 inspect_fw_phexdef("Kernel load address",
647 BE32_TO_HOST(hdr
->kernel_la
),
649 inspect_fw_phexdef("Kernel entry point",
650 BE32_TO_HOST(hdr
->kernel_ep
),
652 inspect_fw_phexdecdef("Rootfs data offset",
653 BE32_TO_HOST(hdr
->rootfs_ofs
),
656 inspect_fw_phex("Kernel load address",
657 BE32_TO_HOST(hdr
->kernel_la
));
658 inspect_fw_phex("Kernel entry point",
659 BE32_TO_HOST(hdr
->kernel_ep
));
660 inspect_fw_phexdec("Rootfs data offset",
661 BE32_TO_HOST(hdr
->rootfs_ofs
));
663 inspect_fw_phexdec("Rootfs data length",
664 BE32_TO_HOST(hdr
->rootfs_len
));
665 inspect_fw_phexdec("Boot loader data offset",
666 BE32_TO_HOST(hdr
->boot_ofs
));
667 inspect_fw_phexdec("Boot loader data length",
668 BE32_TO_HOST(hdr
->boot_len
));
669 inspect_fw_phexdec("Total firmware length",
670 BE32_TO_HOST(hdr
->fw_length
));
678 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
679 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
680 printf("Extracting kernel to \"%s\"...\n", filename
);
681 fp
= fopen(filename
, "w");
683 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
684 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
685 ERR("error in fwrite(): %s", strerror(errno
));
689 ERR("error in fopen(): %s", strerror(errno
));
693 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
694 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
695 printf("Extracting rootfs to \"%s\"...\n", filename
);
696 fp
= fopen(filename
, "w");
698 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
699 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
700 ERR("error in fwrite(): %s", strerror(errno
));
704 ERR("error in fopen(): %s", strerror(errno
));
715 int main(int argc
, char *argv
[])
717 int ret
= EXIT_FAILURE
;
722 progname
= basename(argv
[0]);
727 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
736 sscanf(optarg
, "0x%x", &kernel_ep
);
739 sscanf(optarg
, "0x%x", &kernel_la
);
751 kernel_info
.file_name
= optarg
;
754 rootfs_info
.file_name
= optarg
;
757 sscanf(optarg
, "0x%x", &rootfs_ofs
);
766 inspect_info
.file_name
= optarg
;
780 ret
= check_options();
784 if (!inspect_info
.file_name
)
This page took 0.088334 seconds and 5 git commands to generate.