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_WR841N_V1_5 0x08410002
41 #define HWID_TL_WR841ND_V3 0x08410003
42 #define HWID_TL_WR841ND_V5 0x08410005
43 #define HWID_TL_WR841ND_V7 0x08410007
44 #define HWID_TL_WR941ND_V2 0x09410002
45 #define HWID_TL_WR941ND_V4 0x09410004
46 #define HWID_TL_WR1043ND_V1 0x10430001
51 char *file_name; /* name of the file */
52 uint32_t file_size; /* length of the file */
56 uint32_t version; /* header version */
59 uint32_t hw_id; /* hardware id */
60 uint32_t hw_rev; /* hardware revision */
62 uint8_t md5sum1[MD5SUM_LEN];
64 uint8_t md5sum2[MD5SUM_LEN];
66 uint32_t kernel_la; /* kernel load address */
67 uint32_t kernel_ep; /* kernel entry point */
68 uint32_t fw_length; /* total length of the firmware */
69 uint32_t kernel_ofs; /* kernel data offset */
70 uint32_t kernel_len; /* kernel data length */
71 uint32_t rootfs_ofs; /* rootfs data offset */
72 uint32_t rootfs_len; /* rootfs data length */
73 uint32_t boot_ofs; /* bootloader data offset */
74 uint32_t boot_len; /* bootloader data length */
76 } __attribute__ ((packed));
92 static char *progname;
93 static char *vendor = "TP-LINK Technologies";
94 static char *version = "ver. 1.0";
96 static char *board_id;
97 static struct board_info *board;
98 static struct file_info kernel_info;
99 static uint32_t kernel_la = 0;
100 static uint32_t kernel_ep = 0;
101 static struct file_info rootfs_info;
102 static uint32_t rootfs_ofs = 0;
103 static struct file_info boot_info;
105 static int strip_padding;
107 static struct file_info inspect_info;
108 static int extract = 0;
110 char md5salt_normal[MD5SUM_LEN] = {
111 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
112 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
115 char md5salt_boot[MD5SUM_LEN] = {
116 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
117 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
120 static struct board_info boards[] = {
123 .hw_id = HWID_TL_MR3220_V1,
125 .fw_max_len = 0x3c0000,
126 .kernel_la = 0x80060000,
127 .kernel_ep = 0x80060000,
128 .rootfs_ofs = 0x140000,
131 .hw_id = HWID_TL_MR3420_V1,
133 .fw_max_len = 0x3c0000,
134 .kernel_la = 0x80060000,
135 .kernel_ep = 0x80060000,
136 .rootfs_ofs = 0x140000,
138 .id = "TL-WA901NDv1",
139 .hw_id = HWID_TL_WA901ND_V1,
141 .fw_max_len = 0x3c0000,
142 .kernel_la = 0x80060000,
143 .kernel_ep = 0x80060000,
144 .rootfs_ofs = 0x140000,
146 .id = "TL-WR741NDv1",
147 .hw_id = HWID_TL_WR741ND_V1,
149 .fw_max_len = 0x3c0000,
150 .kernel_la = 0x80060000,
151 .kernel_ep = 0x80060000,
152 .rootfs_ofs = 0x140000,
154 .id = "TL-WR841Nv1.5",
155 .hw_id = HWID_TL_WR841N_V1_5,
157 .fw_max_len = 0x3c0000,
158 .kernel_la = 0x80060000,
159 .kernel_ep = 0x80060000,
160 .rootfs_ofs = 0x140000,
162 .id = "TL-WR841NDv3",
163 .hw_id = HWID_TL_WR841ND_V3,
165 .fw_max_len = 0x3c0000,
166 .kernel_la = 0x80060000,
167 .kernel_ep = 0x80060000,
168 .rootfs_ofs = 0x140000,
170 .id = "TL-WR841NDv5",
171 .hw_id = HWID_TL_WR841ND_V5,
173 .fw_max_len = 0x3c0000,
174 .kernel_la = 0x80060000,
175 .kernel_ep = 0x80060000,
176 .rootfs_ofs = 0x140000,
178 .id = "TL-WR841NDv7",
179 .hw_id = HWID_TL_WR841ND_V7,
181 .fw_max_len = 0x3c0000,
182 .kernel_la = 0x80060000,
183 .kernel_ep = 0x80060000,
184 .rootfs_ofs = 0x140000,
186 .id = "TL-WR941NDv2",
187 .hw_id = HWID_TL_WR941ND_V2,
189 .fw_max_len = 0x3c0000,
190 .kernel_la = 0x80060000,
191 .kernel_ep = 0x80060000,
192 .rootfs_ofs = 0x140000,
194 .id = "TL-WR941NDv4",
195 .hw_id = HWID_TL_WR941ND_V4,
197 .fw_max_len = 0x3c0000,
198 .kernel_la = 0x80060000,
199 .kernel_ep = 0x80060000,
200 .rootfs_ofs = 0x140000,
202 .id = "TL-WR1043NDv1",
203 .hw_id = HWID_TL_WR1043ND_V1,
205 .fw_max_len = 0x7c0000,
206 .kernel_la = 0x80060000,
207 .kernel_ep = 0x80060000,
208 .rootfs_ofs = 0x140000,
210 /* terminating entry */
217 #define ERR(fmt, ...) do { \
219 fprintf(stderr, "[%s] *** error: " fmt "\n", \
220 progname, ## __VA_ARGS__ ); \
223 #define ERRS(fmt, ...) do { \
226 fprintf(stderr, "[%s] *** error: " fmt "\n", \
227 progname, ## __VA_ARGS__, strerror(save)); \
230 #define DBG(fmt, ...) do { \
231 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
234 static struct board_info *find_board(char *id)
236 struct board_info *ret;
237 struct board_info *board;
240 for (board = boards; board->id != NULL; board++){
241 if (strcasecmp(id, board->id) == 0) {
250 static struct board_info *find_board_by_hwid(uint32_t hw_id)
252 struct board_info *board;
254 for (board = boards; board->id != NULL; board++) {
255 if (hw_id == board->hw_id)
263 static void usage(int status)
265 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
266 struct board_info *board;
268 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
272 " -B <board> create image for the board specified with <board>\n"
273 " -c use combined kernel image\n"
274 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
275 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
276 " -k <file> read kernel image from the file <file>\n"
277 " -r <file> read rootfs image from the file <file>\n"
278 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
279 " -o <file> write output to the file <file>\n"
280 " -s strip padding from the end of the image\n"
281 " -N <vendor> set image vendor to <vendor>\n"
282 " -V <version> set image version to <version>\n"
283 " -i <file> inspect given firmware file <file>\n"
284 " -x extract kernel and rootfs while inspecting (requires -i)\n"
285 " -h show this screen\n"
291 static int get_md5(char *data, int size, char *md5)
296 MD5_Update(&ctx, data, size);
297 MD5_Final(md5, &ctx);
300 static int get_file_stat(struct file_info *fdata)
305 if (fdata->file_name == NULL)
308 res = stat(fdata->file_name, &st);
310 ERRS("stat failed on %s", fdata->file_name);
314 fdata->file_size = st.st_size;
318 static int read_to_buf(struct file_info *fdata, char *buf)
321 int ret = EXIT_FAILURE;
323 f = fopen(fdata->file_name, "r");
325 ERRS("could not open \"%s\" for reading", fdata->file_name);
330 fread(buf, fdata->file_size, 1, f);
332 ERRS("unable to read from file \"%s\"", fdata->file_name);
344 static int check_options(void)
348 if (inspect_info.file_name) {
349 ret = get_file_stat(&inspect_info);
354 } else if (extract) {
355 ERR("no firmware for inspection specified");
359 if (board_id == NULL) {
360 ERR("no board specified");
364 board = find_board(board_id);
366 ERR("unknown/unsupported board id \"%s\"", board_id);
370 kernel_la = board->kernel_la;
372 kernel_ep = board->kernel_ep;
374 rootfs_ofs = board->rootfs_ofs;
376 if (kernel_info.file_name == NULL) {
377 ERR("no kernel image specified");
381 ret = get_file_stat(&kernel_info);
386 if (kernel_info.file_size >
387 board->fw_max_len - sizeof(struct fw_header)) {
388 ERR("kernel image is too big");
392 if (kernel_info.file_size >
393 rootfs_ofs - sizeof(struct fw_header)) {
394 ERR("kernel image is too big");
397 if (rootfs_info.file_name == NULL) {
398 ERR("no rootfs image specified");
402 ret = get_file_stat(&rootfs_info);
406 if (rootfs_info.file_size >
407 (board->fw_max_len - rootfs_ofs)) {
408 ERR("rootfs image is too big");
413 if (ofname == NULL) {
414 ERR("no output file specified");
421 static void fill_header(char *buf, int len)
423 struct fw_header *hdr = (struct fw_header *)buf;
425 memset(hdr, 0, sizeof(struct fw_header));
427 hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
428 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
429 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
430 hdr->hw_id = HOST_TO_BE32(board->hw_id);
431 hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
433 if (boot_info.file_size == 0)
434 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
436 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
438 hdr->kernel_la = HOST_TO_BE32(kernel_la);
439 hdr->kernel_ep = HOST_TO_BE32(kernel_ep);
440 hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
441 hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
442 hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
444 hdr->rootfs_ofs = HOST_TO_BE32(rootfs_ofs);
445 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
448 get_md5(buf, len, hdr->md5sum1);
451 static int write_fw(char *data, int len)
454 int ret = EXIT_FAILURE;
456 f = fopen(ofname, "w");
458 ERRS("could not open \"%s\" for writing", ofname);
463 fwrite(data, len, 1, f);
465 ERRS("unable to write output file");
469 DBG("firmware file \"%s\" completed", ofname);
476 if (ret != EXIT_SUCCESS) {
483 static int build_fw(void)
488 int ret = EXIT_FAILURE;
491 buflen = board->fw_max_len;
493 buf = malloc(buflen);
495 ERR("no memory for buffer\n");
499 memset(buf, 0xff, buflen);
500 p = buf + sizeof(struct fw_header);
501 ret = read_to_buf(&kernel_info, p);
505 writelen = kernel_info.file_size;
508 p = buf + rootfs_ofs;
509 ret = read_to_buf(&rootfs_info, p);
513 writelen = rootfs_ofs + rootfs_info.file_size;
519 fill_header(buf, writelen);
520 ret = write_fw(buf, writelen);
532 /* Helper functions to inspect_fw() representing different output formats */
533 static inline void inspect_fw_pstr(char *label, char *str)
535 printf("%-23s: %s\n", label, str);
538 static inline void inspect_fw_phex(char *label, uint32_t val)
540 printf("%-23s: 0x%08x\n", label, val);
543 static inline void inspect_fw_phexpost(char *label,
544 uint32_t val, char *post)
546 printf("%-23s: 0x%08x (%s)\n", label, val, post);
549 static inline void inspect_fw_phexdef(char *label,
550 uint32_t val, uint32_t defval)
552 printf("%-23s: 0x%08x ", label, val);
555 printf("(== OpenWrt default)\n");
557 printf("(OpenWrt default: 0x%08x)\n", defval);
560 static inline void inspect_fw_phexexp(char *label,
561 uint32_t val, uint32_t expval)
563 printf("%-23s: 0x%08x ", label, val);
568 printf("(expected: 0x%08x)\n", expval);
571 static inline void inspect_fw_phexdec(char *label, uint32_t val)
573 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
576 static inline void inspect_fw_phexdecdef(char *label,
577 uint32_t val, uint32_t defval)
579 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
582 printf("(== OpenWrt default)\n");
584 printf("(OpenWrt default: 0x%08x)\n", defval);
587 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
591 printf("%-23s:", label);
592 for (i=0; i<MD5SUM_LEN; i++)
593 printf(" %02x", val[i]);
594 printf(" %s\n", text);
597 static int inspect_fw(void)
600 struct fw_header *hdr;
601 uint8_t md5sum[MD5SUM_LEN];
602 struct board_info *board;
603 int ret = EXIT_FAILURE;
605 buf = malloc(inspect_info.file_size);
607 ERR("no memory for buffer!\n");
611 ret = read_to_buf(&inspect_info, buf);
614 hdr = (struct fw_header *)buf;
616 inspect_fw_pstr("File name", inspect_info.file_name);
617 inspect_fw_phexdec("File size", inspect_info.file_size);
619 if (BE32_TO_HOST(hdr->version) != HEADER_VERSION_V1) {
620 ERR("file does not seem to have V1 header!\n");
624 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
626 if (BE32_TO_HOST(hdr->unk1) != 0)
627 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
629 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
630 if (BE32_TO_HOST(hdr->boot_len) == 0)
631 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
633 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
634 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
636 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
637 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
638 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
640 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
642 if (BE32_TO_HOST(hdr->unk2) != 0)
643 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
644 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
645 "(purpose yet unknown, unchecked here)");
646 if (BE32_TO_HOST(hdr->unk3) != 0)
647 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
651 inspect_fw_pstr("Vendor name", hdr->vendor_name);
652 inspect_fw_pstr("Firmware version", hdr->fw_version);
653 board = find_board_by_hwid(BE32_TO_HOST(hdr->hw_id));
655 inspect_fw_phexpost("Hardware ID",
656 BE32_TO_HOST(hdr->hw_id), board->id);
657 inspect_fw_phexexp("Hardware Revision",
658 BE32_TO_HOST(hdr->hw_rev), board->hw_rev);
660 inspect_fw_phexpost("Hardware ID",
661 BE32_TO_HOST(hdr->hw_id), "unknown");
662 inspect_fw_phex("Hardware Revision",
663 BE32_TO_HOST(hdr->hw_rev));
668 inspect_fw_phexdec("Kernel data offset",
669 BE32_TO_HOST(hdr->kernel_ofs));
670 inspect_fw_phexdec("Kernel data length",
671 BE32_TO_HOST(hdr->kernel_len));
673 inspect_fw_phexdef("Kernel load address",
674 BE32_TO_HOST(hdr->kernel_la),
676 inspect_fw_phexdef("Kernel entry point",
677 BE32_TO_HOST(hdr->kernel_ep),
679 inspect_fw_phexdecdef("Rootfs data offset",
680 BE32_TO_HOST(hdr->rootfs_ofs),
683 inspect_fw_phex("Kernel load address",
684 BE32_TO_HOST(hdr->kernel_la));
685 inspect_fw_phex("Kernel entry point",
686 BE32_TO_HOST(hdr->kernel_ep));
687 inspect_fw_phexdec("Rootfs data offset",
688 BE32_TO_HOST(hdr->rootfs_ofs));
690 inspect_fw_phexdec("Rootfs data length",
691 BE32_TO_HOST(hdr->rootfs_len));
692 inspect_fw_phexdec("Boot loader data offset",
693 BE32_TO_HOST(hdr->boot_ofs));
694 inspect_fw_phexdec("Boot loader data length",
695 BE32_TO_HOST(hdr->boot_len));
696 inspect_fw_phexdec("Total firmware length",
697 BE32_TO_HOST(hdr->fw_length));
705 filename = malloc(strlen(inspect_info.file_name) + 8);
706 sprintf(filename, "%s-kernel", inspect_info.file_name);
707 printf("Extracting kernel to \"%s\"...\n", filename);
708 fp = fopen(filename, "w");
710 if (!fwrite(buf + BE32_TO_HOST(hdr->kernel_ofs),
711 BE32_TO_HOST(hdr->kernel_len), 1, fp)) {
712 ERR("error in fwrite(): %s", strerror(errno));
716 ERR("error in fopen(): %s", strerror(errno));
720 filename = malloc(strlen(inspect_info.file_name) + 8);
721 sprintf(filename, "%s-rootfs", inspect_info.file_name);
722 printf("Extracting rootfs to \"%s\"...\n", filename);
723 fp = fopen(filename, "w");
725 if (!fwrite(buf + BE32_TO_HOST(hdr->rootfs_ofs),
726 BE32_TO_HOST(hdr->rootfs_len), 1, fp)) {
727 ERR("error in fwrite(): %s", strerror(errno));
731 ERR("error in fopen(): %s", strerror(errno));
742 int main(int argc, char *argv[])
744 int ret = EXIT_FAILURE;
749 progname = basename(argv[0]);
754 c = getopt(argc, argv, "B:E:L:V:N:ci:k:r:R:o:xhs");
763 sscanf(optarg, "0x%x", &kernel_ep);
766 sscanf(optarg, "0x%x", &kernel_la);
778 kernel_info.file_name = optarg;
781 rootfs_info.file_name = optarg;
784 sscanf(optarg, "0x%x", &rootfs_ofs);
793 inspect_info.file_name = optarg;
807 ret = check_options();
811 if (!inspect_info.file_name)