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_WA901ND_V2 0x09010002
40 #define HWID_TL_WR703N_V1 0x07030101
41 #define HWID_TL_WR741ND_V1 0x07410001
42 #define HWID_TL_WR741ND_V4 0x07410004
43 #define HWID_TL_WR740N_V1 0x07400001
44 #define HWID_TL_WR740N_V3 0x07400003
45 #define HWID_TL_WR743ND_V1 0x07430001
46 #define HWID_TL_WR841N_V1_5 0x08410002
47 #define HWID_TL_WR841ND_V3 0x08410003
48 #define HWID_TL_WR841ND_V5 0x08410005
49 #define HWID_TL_WR841ND_V7 0x08410007
50 #define HWID_TL_WR941ND_V2 0x09410002
51 #define HWID_TL_WR941ND_V4 0x09410004
52 #define HWID_TL_WR1043ND_V1 0x10430001
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));
98 static char *progname;
99 static char *vendor = "TP-LINK Technologies";
100 static char *version = "ver. 1.0";
102 static char *board_id;
103 static struct board_info *board;
104 static struct file_info kernel_info;
105 static uint32_t kernel_la = 0;
106 static uint32_t kernel_ep = 0;
107 static struct file_info rootfs_info;
108 static uint32_t rootfs_ofs = 0;
109 static struct file_info boot_info;
111 static int strip_padding;
113 static struct file_info inspect_info;
114 static int extract = 0;
116 char md5salt_normal[MD5SUM_LEN] = {
117 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
118 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
121 char md5salt_boot[MD5SUM_LEN] = {
122 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
123 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
126 static struct board_info boards[] = {
129 .hw_id = HWID_TL_MR3220_V1,
131 .fw_max_len = 0x3c0000,
132 .kernel_la = 0x80060000,
133 .kernel_ep = 0x80060000,
134 .rootfs_ofs = 0x140000,
137 .hw_id = HWID_TL_MR3420_V1,
139 .fw_max_len = 0x3c0000,
140 .kernel_la = 0x80060000,
141 .kernel_ep = 0x80060000,
142 .rootfs_ofs = 0x140000,
144 .id = "TL-WA901NDv1",
145 .hw_id = HWID_TL_WA901ND_V1,
147 .fw_max_len = 0x3c0000,
148 .kernel_la = 0x80060000,
149 .kernel_ep = 0x80060000,
150 .rootfs_ofs = 0x140000,
152 .id = "TL-WA901NDv2",
153 .hw_id = HWID_TL_WA901ND_V2,
155 .fw_max_len = 0x3c0000,
156 .kernel_la = 0x80060000,
157 .kernel_ep = 0x80060000,
158 .rootfs_ofs = 0x140000,
160 .id = "TL-WR741NDv1",
161 .hw_id = HWID_TL_WR741ND_V1,
163 .fw_max_len = 0x3c0000,
164 .kernel_la = 0x80060000,
165 .kernel_ep = 0x80060000,
166 .rootfs_ofs = 0x140000,
168 .id = "TL-WR741NDv4",
169 .hw_id = HWID_TL_WR741ND_V4,
171 .fw_max_len = 0x3c0000,
172 .kernel_la = 0x80060000,
173 .kernel_ep = 0x80060000,
174 .rootfs_ofs = 0x100000,
177 .hw_id = HWID_TL_WR740N_V1,
179 .fw_max_len = 0x3c0000,
180 .kernel_la = 0x80060000,
181 .kernel_ep = 0x80060000,
182 .rootfs_ofs = 0x140000,
185 .hw_id = HWID_TL_WR740N_V3,
187 .fw_max_len = 0x3c0000,
188 .kernel_la = 0x80060000,
189 .kernel_ep = 0x80060000,
190 .rootfs_ofs = 0x140000,
192 .id = "TL-WR743NDv1",
193 .hw_id = HWID_TL_WR743ND_V1,
195 .fw_max_len = 0x3c0000,
196 .kernel_la = 0x80060000,
197 .kernel_ep = 0x80060000,
198 .rootfs_ofs = 0x140000,
200 .id = "TL-WR841Nv1.5",
201 .hw_id = HWID_TL_WR841N_V1_5,
203 .fw_max_len = 0x3c0000,
204 .kernel_la = 0x80060000,
205 .kernel_ep = 0x80060000,
206 .rootfs_ofs = 0x140000,
208 .id = "TL-WR841NDv3",
209 .hw_id = HWID_TL_WR841ND_V3,
211 .fw_max_len = 0x3c0000,
212 .kernel_la = 0x80060000,
213 .kernel_ep = 0x80060000,
214 .rootfs_ofs = 0x140000,
216 .id = "TL-WR841NDv5",
217 .hw_id = HWID_TL_WR841ND_V5,
219 .fw_max_len = 0x3c0000,
220 .kernel_la = 0x80060000,
221 .kernel_ep = 0x80060000,
222 .rootfs_ofs = 0x140000,
224 .id = "TL-WR841NDv7",
225 .hw_id = HWID_TL_WR841ND_V7,
227 .fw_max_len = 0x3c0000,
228 .kernel_la = 0x80060000,
229 .kernel_ep = 0x80060000,
230 .rootfs_ofs = 0x140000,
232 .id = "TL-WR941NDv2",
233 .hw_id = HWID_TL_WR941ND_V2,
235 .fw_max_len = 0x3c0000,
236 .kernel_la = 0x80060000,
237 .kernel_ep = 0x80060000,
238 .rootfs_ofs = 0x140000,
240 .id = "TL-WR941NDv4",
241 .hw_id = HWID_TL_WR941ND_V4,
243 .fw_max_len = 0x3c0000,
244 .kernel_la = 0x80060000,
245 .kernel_ep = 0x80060000,
246 .rootfs_ofs = 0x140000,
248 .id = "TL-WR1043NDv1",
249 .hw_id = HWID_TL_WR1043ND_V1,
251 .fw_max_len = 0x7c0000,
252 .kernel_la = 0x80060000,
253 .kernel_ep = 0x80060000,
254 .rootfs_ofs = 0x140000,
257 .hw_id = HWID_TL_WR703N_V1,
259 .fw_max_len = 0x3c0000,
260 .kernel_la = 0x80060000,
261 .kernel_ep = 0x80060000,
262 .rootfs_ofs = 0x100000,
264 /* terminating entry */
271 #define ERR(fmt, ...) do { \
273 fprintf(stderr, "[%s] *** error: " fmt "\n", \
274 progname, ## __VA_ARGS__ ); \
277 #define ERRS(fmt, ...) do { \
280 fprintf(stderr, "[%s] *** error: " fmt "\n", \
281 progname, ## __VA_ARGS__, strerror(save)); \
284 #define DBG(fmt, ...) do { \
285 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
288 static struct board_info *find_board(char *id)
290 struct board_info *ret;
291 struct board_info *board;
294 for (board = boards; board->id != NULL; board++){
295 if (strcasecmp(id, board->id) == 0) {
304 static struct board_info *find_board_by_hwid(uint32_t hw_id)
306 struct board_info *board;
308 for (board = boards; board->id != NULL; board++) {
309 if (hw_id == board->hw_id)
317 static void usage(int status)
319 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
320 struct board_info *board;
322 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
326 " -B <board> create image for the board specified with <board>\n"
327 " -c use combined kernel image\n"
328 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
329 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
330 " -k <file> read kernel image from the file <file>\n"
331 " -r <file> read rootfs image from the file <file>\n"
332 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
333 " -o <file> write output to the file <file>\n"
334 " -s strip padding from the end of the image\n"
335 " -N <vendor> set image vendor to <vendor>\n"
336 " -V <version> set image version to <version>\n"
337 " -i <file> inspect given firmware file <file>\n"
338 " -x extract kernel and rootfs while inspecting (requires -i)\n"
339 " -h show this screen\n"
345 static int get_md5(char *data, int size, char *md5)
350 MD5_Update(&ctx, data, size);
351 MD5_Final(md5, &ctx);
354 static int get_file_stat(struct file_info *fdata)
359 if (fdata->file_name == NULL)
362 res = stat(fdata->file_name, &st);
364 ERRS("stat failed on %s", fdata->file_name);
368 fdata->file_size = st.st_size;
372 static int read_to_buf(struct file_info *fdata, char *buf)
375 int ret = EXIT_FAILURE;
377 f = fopen(fdata->file_name, "r");
379 ERRS("could not open \"%s\" for reading", fdata->file_name);
384 fread(buf, fdata->file_size, 1, f);
386 ERRS("unable to read from file \"%s\"", fdata->file_name);
398 static int check_options(void)
402 if (inspect_info.file_name) {
403 ret = get_file_stat(&inspect_info);
408 } else if (extract) {
409 ERR("no firmware for inspection specified");
413 if (board_id == NULL) {
414 ERR("no board specified");
418 board = find_board(board_id);
420 ERR("unknown/unsupported board id \"%s\"", board_id);
424 kernel_la = board->kernel_la;
426 kernel_ep = board->kernel_ep;
428 rootfs_ofs = board->rootfs_ofs;
430 if (kernel_info.file_name == NULL) {
431 ERR("no kernel image specified");
435 ret = get_file_stat(&kernel_info);
440 if (kernel_info.file_size >
441 board->fw_max_len - sizeof(struct fw_header)) {
442 ERR("kernel image is too big");
446 if (kernel_info.file_size >
447 rootfs_ofs - sizeof(struct fw_header)) {
448 ERR("kernel image is too big");
451 if (rootfs_info.file_name == NULL) {
452 ERR("no rootfs image specified");
456 ret = get_file_stat(&rootfs_info);
460 if (rootfs_info.file_size >
461 (board->fw_max_len - rootfs_ofs)) {
462 ERR("rootfs image is too big");
467 if (ofname == NULL) {
468 ERR("no output file specified");
475 static void fill_header(char *buf, int len)
477 struct fw_header *hdr = (struct fw_header *)buf;
479 memset(hdr, 0, sizeof(struct fw_header));
481 hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
482 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
483 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
484 hdr->hw_id = HOST_TO_BE32(board->hw_id);
485 hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
487 if (boot_info.file_size == 0)
488 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
490 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
492 hdr->kernel_la = HOST_TO_BE32(kernel_la);
493 hdr->kernel_ep = HOST_TO_BE32(kernel_ep);
494 hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
495 hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
496 hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
498 hdr->rootfs_ofs = HOST_TO_BE32(rootfs_ofs);
499 hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
502 get_md5(buf, len, hdr->md5sum1);
505 static int write_fw(char *data, int len)
508 int ret = EXIT_FAILURE;
510 f = fopen(ofname, "w");
512 ERRS("could not open \"%s\" for writing", ofname);
517 fwrite(data, len, 1, f);
519 ERRS("unable to write output file");
523 DBG("firmware file \"%s\" completed", ofname);
530 if (ret != EXIT_SUCCESS) {
537 static int build_fw(void)
542 int ret = EXIT_FAILURE;
545 buflen = board->fw_max_len;
547 buf = malloc(buflen);
549 ERR("no memory for buffer\n");
553 memset(buf, 0xff, buflen);
554 p = buf + sizeof(struct fw_header);
555 ret = read_to_buf(&kernel_info, p);
559 writelen = kernel_info.file_size;
562 p = buf + rootfs_ofs;
563 ret = read_to_buf(&rootfs_info, p);
567 writelen = rootfs_ofs + rootfs_info.file_size;
573 fill_header(buf, writelen);
574 ret = write_fw(buf, writelen);
586 /* Helper functions to inspect_fw() representing different output formats */
587 static inline void inspect_fw_pstr(char *label, char *str)
589 printf("%-23s: %s\n", label, str);
592 static inline void inspect_fw_phex(char *label, uint32_t val)
594 printf("%-23s: 0x%08x\n", label, val);
597 static inline void inspect_fw_phexpost(char *label,
598 uint32_t val, char *post)
600 printf("%-23s: 0x%08x (%s)\n", label, val, post);
603 static inline void inspect_fw_phexdef(char *label,
604 uint32_t val, uint32_t defval)
606 printf("%-23s: 0x%08x ", label, val);
609 printf("(== OpenWrt default)\n");
611 printf("(OpenWrt default: 0x%08x)\n", defval);
614 static inline void inspect_fw_phexexp(char *label,
615 uint32_t val, uint32_t expval)
617 printf("%-23s: 0x%08x ", label, val);
622 printf("(expected: 0x%08x)\n", expval);
625 static inline void inspect_fw_phexdec(char *label, uint32_t val)
627 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
630 static inline void inspect_fw_phexdecdef(char *label,
631 uint32_t val, uint32_t defval)
633 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
636 printf("(== OpenWrt default)\n");
638 printf("(OpenWrt default: 0x%08x)\n", defval);
641 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
645 printf("%-23s:", label);
646 for (i=0; i<MD5SUM_LEN; i++)
647 printf(" %02x", val[i]);
648 printf(" %s\n", text);
651 static int inspect_fw(void)
654 struct fw_header *hdr;
655 uint8_t md5sum[MD5SUM_LEN];
656 struct board_info *board;
657 int ret = EXIT_FAILURE;
659 buf = malloc(inspect_info.file_size);
661 ERR("no memory for buffer!\n");
665 ret = read_to_buf(&inspect_info, buf);
668 hdr = (struct fw_header *)buf;
670 inspect_fw_pstr("File name", inspect_info.file_name);
671 inspect_fw_phexdec("File size", inspect_info.file_size);
673 if (BE32_TO_HOST(hdr->version) != HEADER_VERSION_V1) {
674 ERR("file does not seem to have V1 header!\n");
678 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
680 if (BE32_TO_HOST(hdr->unk1) != 0)
681 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
683 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
684 if (BE32_TO_HOST(hdr->boot_len) == 0)
685 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
687 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
688 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
690 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
691 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
692 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
694 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
696 if (BE32_TO_HOST(hdr->unk2) != 0)
697 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
698 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
699 "(purpose yet unknown, unchecked here)");
700 if (BE32_TO_HOST(hdr->unk3) != 0)
701 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
705 inspect_fw_pstr("Vendor name", hdr->vendor_name);
706 inspect_fw_pstr("Firmware version", hdr->fw_version);
707 board = find_board_by_hwid(BE32_TO_HOST(hdr->hw_id));
709 inspect_fw_phexpost("Hardware ID",
710 BE32_TO_HOST(hdr->hw_id), board->id);
711 inspect_fw_phexexp("Hardware Revision",
712 BE32_TO_HOST(hdr->hw_rev), board->hw_rev);
714 inspect_fw_phexpost("Hardware ID",
715 BE32_TO_HOST(hdr->hw_id), "unknown");
716 inspect_fw_phex("Hardware Revision",
717 BE32_TO_HOST(hdr->hw_rev));
722 inspect_fw_phexdec("Kernel data offset",
723 BE32_TO_HOST(hdr->kernel_ofs));
724 inspect_fw_phexdec("Kernel data length",
725 BE32_TO_HOST(hdr->kernel_len));
727 inspect_fw_phexdef("Kernel load address",
728 BE32_TO_HOST(hdr->kernel_la),
730 inspect_fw_phexdef("Kernel entry point",
731 BE32_TO_HOST(hdr->kernel_ep),
733 inspect_fw_phexdecdef("Rootfs data offset",
734 BE32_TO_HOST(hdr->rootfs_ofs),
737 inspect_fw_phex("Kernel load address",
738 BE32_TO_HOST(hdr->kernel_la));
739 inspect_fw_phex("Kernel entry point",
740 BE32_TO_HOST(hdr->kernel_ep));
741 inspect_fw_phexdec("Rootfs data offset",
742 BE32_TO_HOST(hdr->rootfs_ofs));
744 inspect_fw_phexdec("Rootfs data length",
745 BE32_TO_HOST(hdr->rootfs_len));
746 inspect_fw_phexdec("Boot loader data offset",
747 BE32_TO_HOST(hdr->boot_ofs));
748 inspect_fw_phexdec("Boot loader data length",
749 BE32_TO_HOST(hdr->boot_len));
750 inspect_fw_phexdec("Total firmware length",
751 BE32_TO_HOST(hdr->fw_length));
759 filename = malloc(strlen(inspect_info.file_name) + 8);
760 sprintf(filename, "%s-kernel", inspect_info.file_name);
761 printf("Extracting kernel to \"%s\"...\n", filename);
762 fp = fopen(filename, "w");
764 if (!fwrite(buf + BE32_TO_HOST(hdr->kernel_ofs),
765 BE32_TO_HOST(hdr->kernel_len), 1, fp)) {
766 ERR("error in fwrite(): %s", strerror(errno));
770 ERR("error in fopen(): %s", strerror(errno));
774 filename = malloc(strlen(inspect_info.file_name) + 8);
775 sprintf(filename, "%s-rootfs", inspect_info.file_name);
776 printf("Extracting rootfs to \"%s\"...\n", filename);
777 fp = fopen(filename, "w");
779 if (!fwrite(buf + BE32_TO_HOST(hdr->rootfs_ofs),
780 BE32_TO_HOST(hdr->rootfs_len), 1, fp)) {
781 ERR("error in fwrite(): %s", strerror(errno));
785 ERR("error in fopen(): %s", strerror(errno));
796 int main(int argc, char *argv[])
798 int ret = EXIT_FAILURE;
803 progname = basename(argv[0]);
808 c = getopt(argc, argv, "B:E:L:V:N:ci:k:r:R:o:xhs");
817 sscanf(optarg, "0x%x", &kernel_ep);
820 sscanf(optarg, "0x%x", &kernel_la);
832 kernel_info.file_name = optarg;
835 rootfs_info.file_name = optarg;
838 sscanf(optarg, "0x%x", &rootfs_ofs);
847 inspect_info.file_name = optarg;
861 ret = check_options();
865 if (!inspect_info.file_name)