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_MR3220_V1 0x32200001
37 #define HWID_TL_MR3420_V1 0x34200001
38 #define HWID_TL_WA901ND_V1 0x09010001
39 #define HWID_TL_WR741ND_V1 0x07410001
40 #define HWID_TL_WR740N_V1 0x07400001
41 #define HWID_TL_WR841N_V1_5 0x08410002
42 #define HWID_TL_WR841ND_V3 0x08410003
43 #define HWID_TL_WR841ND_V5 0x08410005
44 #define HWID_TL_WR841ND_V7 0x08410007
45 #define HWID_TL_WR941ND_V2 0x09410002
46 #define HWID_TL_WR941ND_V4 0x09410004
47 #define HWID_TL_WR1043ND_V1 0x10430001
52 char *file_name
; /* name of the file */
53 uint32_t file_size
; /* length of the file */
57 uint32_t version
; /* header version */
60 uint32_t hw_id
; /* hardware id */
61 uint32_t hw_rev
; /* hardware revision */
63 uint8_t md5sum1
[MD5SUM_LEN
];
65 uint8_t md5sum2
[MD5SUM_LEN
];
67 uint32_t kernel_la
; /* kernel load address */
68 uint32_t kernel_ep
; /* kernel entry point */
69 uint32_t fw_length
; /* total length of the firmware */
70 uint32_t kernel_ofs
; /* kernel data offset */
71 uint32_t kernel_len
; /* kernel data length */
72 uint32_t rootfs_ofs
; /* rootfs data offset */
73 uint32_t rootfs_len
; /* rootfs data length */
74 uint32_t boot_ofs
; /* bootloader data offset */
75 uint32_t boot_len
; /* bootloader data length */
77 } __attribute__ ((packed
));
93 static char *progname
;
94 static char *vendor
= "TP-LINK Technologies";
95 static char *version
= "ver. 1.0";
97 static char *board_id
;
98 static struct board_info
*board
;
99 static struct file_info kernel_info
;
100 static uint32_t kernel_la
= 0;
101 static uint32_t kernel_ep
= 0;
102 static struct file_info rootfs_info
;
103 static uint32_t rootfs_ofs
= 0;
104 static struct file_info boot_info
;
106 static int strip_padding
;
108 static struct file_info inspect_info
;
109 static int extract
= 0;
111 char md5salt_normal
[MD5SUM_LEN
] = {
112 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
113 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
116 char md5salt_boot
[MD5SUM_LEN
] = {
117 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
118 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
121 static struct board_info boards
[] = {
124 .hw_id
= HWID_TL_MR3220_V1
,
126 .fw_max_len
= 0x3c0000,
127 .kernel_la
= 0x80060000,
128 .kernel_ep
= 0x80060000,
129 .rootfs_ofs
= 0x140000,
132 .hw_id
= HWID_TL_MR3420_V1
,
134 .fw_max_len
= 0x3c0000,
135 .kernel_la
= 0x80060000,
136 .kernel_ep
= 0x80060000,
137 .rootfs_ofs
= 0x140000,
139 .id
= "TL-WA901NDv1",
140 .hw_id
= HWID_TL_WA901ND_V1
,
142 .fw_max_len
= 0x3c0000,
143 .kernel_la
= 0x80060000,
144 .kernel_ep
= 0x80060000,
145 .rootfs_ofs
= 0x140000,
147 .id
= "TL-WR741NDv1",
148 .hw_id
= HWID_TL_WR741ND_V1
,
150 .fw_max_len
= 0x3c0000,
151 .kernel_la
= 0x80060000,
152 .kernel_ep
= 0x80060000,
153 .rootfs_ofs
= 0x140000,
156 .hw_id
= HWID_TL_WR740N_V1
,
158 .fw_max_len
= 0x3c0000,
159 .kernel_la
= 0x80060000,
160 .kernel_ep
= 0x80060000,
161 .rootfs_ofs
= 0x140000,
163 .id
= "TL-WR841Nv1.5",
164 .hw_id
= HWID_TL_WR841N_V1_5
,
166 .fw_max_len
= 0x3c0000,
167 .kernel_la
= 0x80060000,
168 .kernel_ep
= 0x80060000,
169 .rootfs_ofs
= 0x140000,
171 .id
= "TL-WR841NDv3",
172 .hw_id
= HWID_TL_WR841ND_V3
,
174 .fw_max_len
= 0x3c0000,
175 .kernel_la
= 0x80060000,
176 .kernel_ep
= 0x80060000,
177 .rootfs_ofs
= 0x140000,
179 .id
= "TL-WR841NDv5",
180 .hw_id
= HWID_TL_WR841ND_V5
,
182 .fw_max_len
= 0x3c0000,
183 .kernel_la
= 0x80060000,
184 .kernel_ep
= 0x80060000,
185 .rootfs_ofs
= 0x140000,
187 .id
= "TL-WR841NDv7",
188 .hw_id
= HWID_TL_WR841ND_V7
,
190 .fw_max_len
= 0x3c0000,
191 .kernel_la
= 0x80060000,
192 .kernel_ep
= 0x80060000,
193 .rootfs_ofs
= 0x140000,
195 .id
= "TL-WR941NDv2",
196 .hw_id
= HWID_TL_WR941ND_V2
,
198 .fw_max_len
= 0x3c0000,
199 .kernel_la
= 0x80060000,
200 .kernel_ep
= 0x80060000,
201 .rootfs_ofs
= 0x140000,
203 .id
= "TL-WR941NDv4",
204 .hw_id
= HWID_TL_WR941ND_V4
,
206 .fw_max_len
= 0x3c0000,
207 .kernel_la
= 0x80060000,
208 .kernel_ep
= 0x80060000,
209 .rootfs_ofs
= 0x140000,
211 .id
= "TL-WR1043NDv1",
212 .hw_id
= HWID_TL_WR1043ND_V1
,
214 .fw_max_len
= 0x7c0000,
215 .kernel_la
= 0x80060000,
216 .kernel_ep
= 0x80060000,
217 .rootfs_ofs
= 0x140000,
219 /* terminating entry */
226 #define ERR(fmt, ...) do { \
228 fprintf(stderr, "[%s] *** error: " fmt "\n", \
229 progname, ## __VA_ARGS__ ); \
232 #define ERRS(fmt, ...) do { \
235 fprintf(stderr, "[%s] *** error: " fmt "\n", \
236 progname, ## __VA_ARGS__, strerror(save)); \
239 #define DBG(fmt, ...) do { \
240 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
243 static struct board_info
*find_board(char *id
)
245 struct board_info
*ret
;
246 struct board_info
*board
;
249 for (board
= boards
; board
->id
!= NULL
; board
++){
250 if (strcasecmp(id
, board
->id
) == 0) {
259 static struct board_info
*find_board_by_hwid(uint32_t hw_id
)
261 struct board_info
*board
;
263 for (board
= boards
; board
->id
!= NULL
; board
++) {
264 if (hw_id
== board
->hw_id
)
272 static void usage(int status
)
274 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
275 struct board_info
*board
;
277 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
281 " -B <board> create image for the board specified with <board>\n"
282 " -c use combined kernel image\n"
283 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
284 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
285 " -k <file> read kernel image from the file <file>\n"
286 " -r <file> read rootfs image from the file <file>\n"
287 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
288 " -o <file> write output to the file <file>\n"
289 " -s strip padding from the end of the image\n"
290 " -N <vendor> set image vendor to <vendor>\n"
291 " -V <version> set image version to <version>\n"
292 " -i <file> inspect given firmware file <file>\n"
293 " -x extract kernel and rootfs while inspecting (requires -i)\n"
294 " -h show this screen\n"
300 static int get_md5(char *data
, int size
, char *md5
)
305 MD5_Update(&ctx
, data
, size
);
306 MD5_Final(md5
, &ctx
);
309 static int get_file_stat(struct file_info
*fdata
)
314 if (fdata
->file_name
== NULL
)
317 res
= stat(fdata
->file_name
, &st
);
319 ERRS("stat failed on %s", fdata
->file_name
);
323 fdata
->file_size
= st
.st_size
;
327 static int read_to_buf(struct file_info
*fdata
, char *buf
)
330 int ret
= EXIT_FAILURE
;
332 f
= fopen(fdata
->file_name
, "r");
334 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
339 fread(buf
, fdata
->file_size
, 1, f
);
341 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
353 static int check_options(void)
357 if (inspect_info
.file_name
) {
358 ret
= get_file_stat(&inspect_info
);
363 } else if (extract
) {
364 ERR("no firmware for inspection specified");
368 if (board_id
== NULL
) {
369 ERR("no board specified");
373 board
= find_board(board_id
);
375 ERR("unknown/unsupported board id \"%s\"", board_id
);
379 kernel_la
= board
->kernel_la
;
381 kernel_ep
= board
->kernel_ep
;
383 rootfs_ofs
= board
->rootfs_ofs
;
385 if (kernel_info
.file_name
== NULL
) {
386 ERR("no kernel image specified");
390 ret
= get_file_stat(&kernel_info
);
395 if (kernel_info
.file_size
>
396 board
->fw_max_len
- sizeof(struct fw_header
)) {
397 ERR("kernel image is too big");
401 if (kernel_info
.file_size
>
402 rootfs_ofs
- sizeof(struct fw_header
)) {
403 ERR("kernel image is too big");
406 if (rootfs_info
.file_name
== NULL
) {
407 ERR("no rootfs image specified");
411 ret
= get_file_stat(&rootfs_info
);
415 if (rootfs_info
.file_size
>
416 (board
->fw_max_len
- rootfs_ofs
)) {
417 ERR("rootfs image is too big");
422 if (ofname
== NULL
) {
423 ERR("no output file specified");
430 static void fill_header(char *buf
, int len
)
432 struct fw_header
*hdr
= (struct fw_header
*)buf
;
434 memset(hdr
, 0, sizeof(struct fw_header
));
436 hdr
->version
= HOST_TO_BE32(HEADER_VERSION_V1
);
437 strncpy(hdr
->vendor_name
, vendor
, sizeof(hdr
->vendor_name
));
438 strncpy(hdr
->fw_version
, version
, sizeof(hdr
->fw_version
));
439 hdr
->hw_id
= HOST_TO_BE32(board
->hw_id
);
440 hdr
->hw_rev
= HOST_TO_BE32(board
->hw_rev
);
442 if (boot_info
.file_size
== 0)
443 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(hdr
->md5sum1
));
445 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(hdr
->md5sum1
));
447 hdr
->kernel_la
= HOST_TO_BE32(kernel_la
);
448 hdr
->kernel_ep
= HOST_TO_BE32(kernel_ep
);
449 hdr
->fw_length
= HOST_TO_BE32(board
->fw_max_len
);
450 hdr
->kernel_ofs
= HOST_TO_BE32(sizeof(struct fw_header
));
451 hdr
->kernel_len
= HOST_TO_BE32(kernel_info
.file_size
);
453 hdr
->rootfs_ofs
= HOST_TO_BE32(rootfs_ofs
);
454 hdr
->rootfs_len
= HOST_TO_BE32(rootfs_info
.file_size
);
457 get_md5(buf
, len
, hdr
->md5sum1
);
460 static int write_fw(char *data
, int len
)
463 int ret
= EXIT_FAILURE
;
465 f
= fopen(ofname
, "w");
467 ERRS("could not open \"%s\" for writing", ofname
);
472 fwrite(data
, len
, 1, f
);
474 ERRS("unable to write output file");
478 DBG("firmware file \"%s\" completed", ofname
);
485 if (ret
!= EXIT_SUCCESS
) {
492 static int build_fw(void)
497 int ret
= EXIT_FAILURE
;
500 buflen
= board
->fw_max_len
;
502 buf
= malloc(buflen
);
504 ERR("no memory for buffer\n");
508 memset(buf
, 0xff, buflen
);
509 p
= buf
+ sizeof(struct fw_header
);
510 ret
= read_to_buf(&kernel_info
, p
);
514 writelen
= kernel_info
.file_size
;
517 p
= buf
+ rootfs_ofs
;
518 ret
= read_to_buf(&rootfs_info
, p
);
522 writelen
= rootfs_ofs
+ rootfs_info
.file_size
;
528 fill_header(buf
, writelen
);
529 ret
= write_fw(buf
, writelen
);
541 /* Helper functions to inspect_fw() representing different output formats */
542 static inline void inspect_fw_pstr(char *label
, char *str
)
544 printf("%-23s: %s\n", label
, str
);
547 static inline void inspect_fw_phex(char *label
, uint32_t val
)
549 printf("%-23s: 0x%08x\n", label
, val
);
552 static inline void inspect_fw_phexpost(char *label
,
553 uint32_t val
, char *post
)
555 printf("%-23s: 0x%08x (%s)\n", label
, val
, post
);
558 static inline void inspect_fw_phexdef(char *label
,
559 uint32_t val
, uint32_t defval
)
561 printf("%-23s: 0x%08x ", label
, val
);
564 printf("(== OpenWrt default)\n");
566 printf("(OpenWrt default: 0x%08x)\n", defval
);
569 static inline void inspect_fw_phexexp(char *label
,
570 uint32_t val
, uint32_t expval
)
572 printf("%-23s: 0x%08x ", label
, val
);
577 printf("(expected: 0x%08x)\n", expval
);
580 static inline void inspect_fw_phexdec(char *label
, uint32_t val
)
582 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
585 static inline void inspect_fw_phexdecdef(char *label
,
586 uint32_t val
, uint32_t defval
)
588 printf("%-23s: 0x%08x / %8u bytes ", label
, val
, val
);
591 printf("(== OpenWrt default)\n");
593 printf("(OpenWrt default: 0x%08x)\n", defval
);
596 static inline void inspect_fw_pmd5sum(char *label
, uint8_t *val
, char *text
)
600 printf("%-23s:", label
);
601 for (i
=0; i
<MD5SUM_LEN
; i
++)
602 printf(" %02x", val
[i
]);
603 printf(" %s\n", text
);
606 static int inspect_fw(void)
609 struct fw_header
*hdr
;
610 uint8_t md5sum
[MD5SUM_LEN
];
611 struct board_info
*board
;
612 int ret
= EXIT_FAILURE
;
614 buf
= malloc(inspect_info
.file_size
);
616 ERR("no memory for buffer!\n");
620 ret
= read_to_buf(&inspect_info
, buf
);
623 hdr
= (struct fw_header
*)buf
;
625 inspect_fw_pstr("File name", inspect_info
.file_name
);
626 inspect_fw_phexdec("File size", inspect_info
.file_size
);
628 if (BE32_TO_HOST(hdr
->version
) != HEADER_VERSION_V1
) {
629 ERR("file does not seem to have V1 header!\n");
633 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header
));
635 if (BE32_TO_HOST(hdr
->unk1
) != 0)
636 inspect_fw_phexdec("Unknown value 1", hdr
->unk1
);
638 memcpy(md5sum
, hdr
->md5sum1
, sizeof(md5sum
));
639 if (BE32_TO_HOST(hdr
->boot_len
) == 0)
640 memcpy(hdr
->md5sum1
, md5salt_normal
, sizeof(md5sum
));
642 memcpy(hdr
->md5sum1
, md5salt_boot
, sizeof(md5sum
));
643 get_md5(buf
, inspect_info
.file_size
, hdr
->md5sum1
);
645 if (memcmp(md5sum
, hdr
->md5sum1
, sizeof(md5sum
))) {
646 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(*ERROR*)");
647 inspect_fw_pmd5sum(" --> expected", hdr
->md5sum1
, "");
649 inspect_fw_pmd5sum("Header MD5Sum1", md5sum
, "(ok)");
651 if (BE32_TO_HOST(hdr
->unk2
) != 0)
652 inspect_fw_phexdec("Unknown value 2", hdr
->unk2
);
653 inspect_fw_pmd5sum("Header MD5Sum2", hdr
->md5sum2
,
654 "(purpose yet unknown, unchecked here)");
655 if (BE32_TO_HOST(hdr
->unk3
) != 0)
656 inspect_fw_phexdec("Unknown value 3", hdr
->unk3
);
660 inspect_fw_pstr("Vendor name", hdr
->vendor_name
);
661 inspect_fw_pstr("Firmware version", hdr
->fw_version
);
662 board
= find_board_by_hwid(BE32_TO_HOST(hdr
->hw_id
));
664 inspect_fw_phexpost("Hardware ID",
665 BE32_TO_HOST(hdr
->hw_id
), board
->id
);
666 inspect_fw_phexexp("Hardware Revision",
667 BE32_TO_HOST(hdr
->hw_rev
), board
->hw_rev
);
669 inspect_fw_phexpost("Hardware ID",
670 BE32_TO_HOST(hdr
->hw_id
), "unknown");
671 inspect_fw_phex("Hardware Revision",
672 BE32_TO_HOST(hdr
->hw_rev
));
677 inspect_fw_phexdec("Kernel data offset",
678 BE32_TO_HOST(hdr
->kernel_ofs
));
679 inspect_fw_phexdec("Kernel data length",
680 BE32_TO_HOST(hdr
->kernel_len
));
682 inspect_fw_phexdef("Kernel load address",
683 BE32_TO_HOST(hdr
->kernel_la
),
685 inspect_fw_phexdef("Kernel entry point",
686 BE32_TO_HOST(hdr
->kernel_ep
),
688 inspect_fw_phexdecdef("Rootfs data offset",
689 BE32_TO_HOST(hdr
->rootfs_ofs
),
692 inspect_fw_phex("Kernel load address",
693 BE32_TO_HOST(hdr
->kernel_la
));
694 inspect_fw_phex("Kernel entry point",
695 BE32_TO_HOST(hdr
->kernel_ep
));
696 inspect_fw_phexdec("Rootfs data offset",
697 BE32_TO_HOST(hdr
->rootfs_ofs
));
699 inspect_fw_phexdec("Rootfs data length",
700 BE32_TO_HOST(hdr
->rootfs_len
));
701 inspect_fw_phexdec("Boot loader data offset",
702 BE32_TO_HOST(hdr
->boot_ofs
));
703 inspect_fw_phexdec("Boot loader data length",
704 BE32_TO_HOST(hdr
->boot_len
));
705 inspect_fw_phexdec("Total firmware length",
706 BE32_TO_HOST(hdr
->fw_length
));
714 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
715 sprintf(filename
, "%s-kernel", inspect_info
.file_name
);
716 printf("Extracting kernel to \"%s\"...\n", filename
);
717 fp
= fopen(filename
, "w");
719 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->kernel_ofs
),
720 BE32_TO_HOST(hdr
->kernel_len
), 1, fp
)) {
721 ERR("error in fwrite(): %s", strerror(errno
));
725 ERR("error in fopen(): %s", strerror(errno
));
729 filename
= malloc(strlen(inspect_info
.file_name
) + 8);
730 sprintf(filename
, "%s-rootfs", inspect_info
.file_name
);
731 printf("Extracting rootfs to \"%s\"...\n", filename
);
732 fp
= fopen(filename
, "w");
734 if (!fwrite(buf
+ BE32_TO_HOST(hdr
->rootfs_ofs
),
735 BE32_TO_HOST(hdr
->rootfs_len
), 1, fp
)) {
736 ERR("error in fwrite(): %s", strerror(errno
));
740 ERR("error in fopen(): %s", strerror(errno
));
751 int main(int argc
, char *argv
[])
753 int ret
= EXIT_FAILURE
;
758 progname
= basename(argv
[0]);
763 c
= getopt(argc
, argv
, "B:E:L:V:N:ci:k:r:R:o:xhs");
772 sscanf(optarg
, "0x%x", &kernel_ep
);
775 sscanf(optarg
, "0x%x", &kernel_la
);
787 kernel_info
.file_name
= optarg
;
790 rootfs_info
.file_name
= optarg
;
793 sscanf(optarg
, "0x%x", &rootfs_ofs
);
802 inspect_info
.file_name
= optarg
;
816 ret
= check_options();
820 if (!inspect_info
.file_name
)
This page took 0.076795 seconds and 5 git commands to generate.