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() */
25 #include <arpa/inet.h>
26 #include <netinet/in.h>
30 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
32 #define HEADER_VERSION_V1 0x01000000
33 #define HWID_TL_MR3020_V1 0x30200001
34 #define HWID_TL_MR3220_V1 0x32200001
35 #define HWID_TL_MR3420_V1 0x34200001
36 #define HWID_TL_WA701N_V1 0x07010001
37 #define HWID_TL_WA901ND_V1 0x09010001
38 #define HWID_TL_WA901ND_V2 0x09010002
39 #define HWID_TL_WR703N_V1 0x07030101
40 #define HWID_TL_WR741ND_V1 0x07410001
41 #define HWID_TL_WR741ND_V4 0x07410004
42 #define HWID_TL_WR740N_V1 0x07400001
43 #define HWID_TL_WR740N_V3 0x07400003
44 #define HWID_TL_WR743ND_V1 0x07430001
45 #define HWID_TL_WR841N_V1_5 0x08410002
46 #define HWID_TL_WR841ND_V3 0x08410003
47 #define HWID_TL_WR841ND_V5 0x08410005
48 #define HWID_TL_WR841ND_V7 0x08410007
49 #define HWID_TL_WR941ND_V2 0x09410002
50 #define HWID_TL_WR941ND_V4 0x09410004
51 #define HWID_TL_WR1043ND_V1 0x10430001
56 char *file_name; /* name of the file */
57 uint32_t file_size; /* length of the file */
61 uint32_t version; /* header version */
64 uint32_t hw_id; /* hardware id */
65 uint32_t hw_rev; /* hardware revision */
67 uint8_t md5sum1[MD5SUM_LEN];
69 uint8_t md5sum2[MD5SUM_LEN];
71 uint32_t kernel_la; /* kernel load address */
72 uint32_t kernel_ep; /* kernel entry point */
73 uint32_t fw_length; /* total length of the firmware */
74 uint32_t kernel_ofs; /* kernel data offset */
75 uint32_t kernel_len; /* kernel data length */
76 uint32_t rootfs_ofs; /* rootfs data offset */
77 uint32_t rootfs_len; /* rootfs data length */
78 uint32_t boot_ofs; /* bootloader data offset */
79 uint32_t boot_len; /* bootloader data length */
81 } __attribute__ ((packed));
102 static char *progname;
103 static char *vendor = "TP-LINK Technologies";
104 static char *version = "ver. 1.0";
106 static char *board_id;
107 static struct board_info *board;
108 static char *layout_id;
109 static struct flash_layout *layout;
110 static char *opt_hw_id;
111 static uint32_t hw_id;
112 static char *opt_hw_rev;
113 static uint32_t hw_rev;
114 static struct file_info kernel_info;
115 static uint32_t kernel_la = 0;
116 static uint32_t kernel_ep = 0;
117 static uint32_t kernel_len = 0;
118 static struct file_info rootfs_info;
119 static uint32_t rootfs_ofs = 0;
120 static uint32_t rootfs_align;
121 static struct file_info boot_info;
123 static int strip_padding;
124 static int add_jffs2_eof;
125 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
127 static struct file_info inspect_info;
128 static int extract = 0;
130 char md5salt_normal[MD5SUM_LEN] = {
131 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
132 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
135 char md5salt_boot[MD5SUM_LEN] = {
136 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
137 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
140 static struct flash_layout layouts[] = {
143 .fw_max_len = 0x3c0000,
144 .kernel_la = 0x80060000,
145 .kernel_ep = 0x80060000,
146 .rootfs_ofs = 0x140000,
149 .fw_max_len = 0x3c0000,
150 .kernel_la = 0x80060000,
151 .kernel_ep = 0x80060000,
152 .rootfs_ofs = 0x100000,
155 .fw_max_len = 0x7c0000,
156 .kernel_la = 0x80060000,
157 .kernel_ep = 0x80060000,
158 .rootfs_ofs = 0x140000,
160 /* terminating entry */
164 static struct board_info boards[] = {
167 .hw_id = HWID_TL_MR3020_V1,
169 .layout_id = "4Mlzma",
172 .hw_id = HWID_TL_MR3220_V1,
177 .hw_id = HWID_TL_MR3420_V1,
182 .hw_id = HWID_TL_WA701N_V1,
186 .id = "TL-WA901NDv1",
187 .hw_id = HWID_TL_WA901ND_V1,
191 .id = "TL-WA901NDv2",
192 .hw_id = HWID_TL_WA901ND_V2,
196 .id = "TL-WR741NDv1",
197 .hw_id = HWID_TL_WR741ND_V1,
201 .id = "TL-WR741NDv4",
202 .hw_id = HWID_TL_WR741ND_V4,
204 .layout_id = "4Mlzma",
207 .hw_id = HWID_TL_WR740N_V1,
212 .hw_id = HWID_TL_WR740N_V3,
216 .id = "TL-WR743NDv1",
217 .hw_id = HWID_TL_WR743ND_V1,
221 .id = "TL-WR841Nv1.5",
222 .hw_id = HWID_TL_WR841N_V1_5,
226 .id = "TL-WR841NDv3",
227 .hw_id = HWID_TL_WR841ND_V3,
231 .id = "TL-WR841NDv5",
232 .hw_id = HWID_TL_WR841ND_V5,
236 .id = "TL-WR841NDv7",
237 .hw_id = HWID_TL_WR841ND_V7,
241 .id = "TL-WR941NDv2",
242 .hw_id = HWID_TL_WR941ND_V2,
246 .id = "TL-WR941NDv4",
247 .hw_id = HWID_TL_WR941ND_V4,
251 .id = "TL-WR1043NDv1",
252 .hw_id = HWID_TL_WR1043ND_V1,
257 .hw_id = HWID_TL_WR703N_V1,
259 .layout_id = "4Mlzma",
261 /* terminating entry */
268 #define ERR(fmt, ...) do { \
270 fprintf(stderr, "[%s] *** error: " fmt "\n", \
271 progname, ## __VA_ARGS__ ); \
274 #define ERRS(fmt, ...) do { \
277 fprintf(stderr, "[%s] *** error: " fmt "\n", \
278 progname, ## __VA_ARGS__, strerror(save)); \
281 #define DBG(fmt, ...) do { \
282 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
285 static struct board_info *find_board(char *id)
287 struct board_info *ret;
288 struct board_info *board;
291 for (board = boards; board->id != NULL; board++){
292 if (strcasecmp(id, board->id) == 0) {
301 static struct board_info *find_board_by_hwid(uint32_t hw_id)
303 struct board_info *board;
305 for (board = boards; board->id != NULL; board++) {
306 if (hw_id == board->hw_id)
313 static struct flash_layout *find_layout(char *id)
315 struct flash_layout *ret;
316 struct flash_layout *l;
319 for (l = layouts; l->id != NULL; l++){
320 if (strcasecmp(id, l->id) == 0) {
329 static void usage(int status)
331 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
332 struct board_info *board;
334 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
338 " -B <board> create image for the board specified with <board>\n"
339 " -c use combined kernel image\n"
340 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
341 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
342 " -H <hwid> use hardware id specified with <hwid>\n"
343 " -F <id> use flash layout specified with <id>\n"
344 " -k <file> read kernel image from the file <file>\n"
345 " -r <file> read rootfs image from the file <file>\n"
346 " -a <align> align the rootfs start on an <align> bytes boundary\n"
347 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
348 " -o <file> write output to the file <file>\n"
349 " -s strip padding from the end of the image\n"
350 " -j add jffs2 end-of-filesystem markers\n"
351 " -N <vendor> set image vendor to <vendor>\n"
352 " -V <version> set image version to <version>\n"
353 " -i <file> inspect given firmware file <file>\n"
354 " -x extract kernel and rootfs while inspecting (requires -i)\n"
355 " -h show this screen\n"
361 static int get_md5(char *data, int size, char *md5)
366 MD5_Update(&ctx, data, size);
367 MD5_Final(md5, &ctx);
370 static int get_file_stat(struct file_info *fdata)
375 if (fdata->file_name == NULL)
378 res = stat(fdata->file_name, &st);
380 ERRS("stat failed on %s", fdata->file_name);
384 fdata->file_size = st.st_size;
388 static int read_to_buf(struct file_info *fdata, char *buf)
391 int ret = EXIT_FAILURE;
393 f = fopen(fdata->file_name, "r");
395 ERRS("could not open \"%s\" for reading", fdata->file_name);
400 fread(buf, fdata->file_size, 1, f);
402 ERRS("unable to read from file \"%s\"", fdata->file_name);
414 static int check_options(void)
418 if (inspect_info.file_name) {
419 ret = get_file_stat(&inspect_info);
424 } else if (extract) {
425 ERR("no firmware for inspection specified");
429 if (board_id == NULL && opt_hw_id == NULL) {
430 ERR("either board or hardware id must be specified");
435 board = find_board(board_id);
437 ERR("unknown/unsupported board id \"%s\"", board_id);
440 if (layout_id == NULL)
441 layout_id = board->layout_id;
443 hw_id = board->hw_id;
444 hw_rev = board->hw_rev;
446 if (layout_id == NULL) {
447 ERR("flash layout is not specified");
450 hw_id = strtoul(opt_hw_id, NULL, 0);
453 hw_rev = strtoul(opt_hw_rev, NULL, 0);
458 layout = find_layout(layout_id);
459 if (layout == NULL) {
460 ERR("unknown flash layout \"%s\"", layout_id);
465 kernel_la = layout->kernel_la;
467 kernel_ep = layout->kernel_ep;
469 rootfs_ofs = layout->rootfs_ofs;
471 if (kernel_info.file_name == NULL) {
472 ERR("no kernel image specified");
476 ret = get_file_stat(&kernel_info);
480 kernel_len = kernel_info.file_size;
483 if (kernel_info.file_size >
484 layout->fw_max_len - sizeof(struct fw_header)) {
485 ERR("kernel image is too big");
489 if (rootfs_info.file_name == NULL) {
490 ERR("no rootfs image specified");
494 ret = get_file_stat(&rootfs_info);
499 kernel_len += sizeof(struct fw_header);
500 kernel_len = ALIGN(kernel_len, rootfs_align);
501 kernel_len -= sizeof(struct fw_header);
503 DBG("kernel length aligned to %u", kernel_len);
505 if (kernel_len + rootfs_info.file_size >
506 layout->fw_max_len - sizeof(struct fw_header)) {
507 ERR("images are too big");
511 if (kernel_info.file_size >
512 rootfs_ofs - sizeof(struct fw_header)) {
513 ERR("kernel image is too big");
517 if (rootfs_info.file_size >
518 (layout->fw_max_len - rootfs_ofs)) {
519 ERR("rootfs image is too big");
525 if (ofname == NULL) {
526 ERR("no output file specified");
533 static void fill_header(char *buf, int len)
535 struct fw_header *hdr = (struct fw_header *)buf;
537 memset(hdr, 0, sizeof(struct fw_header));
539 hdr->version = htonl(HEADER_VERSION_V1);
540 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
541 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
542 hdr->hw_id = htonl(hw_id);
543 hdr->hw_rev = htonl(hw_rev);
545 if (boot_info.file_size == 0)
546 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
548 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
550 hdr->kernel_la = htonl(kernel_la);
551 hdr->kernel_ep = htonl(kernel_ep);
552 hdr->fw_length = htonl(layout->fw_max_len);
553 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
554 hdr->kernel_len = htonl(kernel_len);
556 hdr->rootfs_ofs = htonl(rootfs_ofs);
557 hdr->rootfs_len = htonl(rootfs_info.file_size);
560 get_md5(buf, len, hdr->md5sum1);
563 static int pad_jffs2(char *buf, int currlen)
569 pad_mask = (64 * 1024);
570 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
574 for (i = 10; i < 32; i++) {
580 len = ALIGN(len, mask);
582 for (i = 10; i < 32; i++) {
584 if ((len & (mask - 1)) == 0)
588 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
589 buf[len + i] = jffs2_eof_mark[i];
591 len += sizeof(jffs2_eof_mark);
597 static int write_fw(char *data, int len)
600 int ret = EXIT_FAILURE;
602 f = fopen(ofname, "w");
604 ERRS("could not open \"%s\" for writing", ofname);
609 fwrite(data, len, 1, f);
611 ERRS("unable to write output file");
615 DBG("firmware file \"%s\" completed", ofname);
622 if (ret != EXIT_SUCCESS) {
629 static int build_fw(void)
634 int ret = EXIT_FAILURE;
637 buflen = layout->fw_max_len;
639 buf = malloc(buflen);
641 ERR("no memory for buffer\n");
645 memset(buf, 0xff, buflen);
646 p = buf + sizeof(struct fw_header);
647 ret = read_to_buf(&kernel_info, p);
651 writelen = sizeof(struct fw_header) + kernel_len;
657 p = buf + rootfs_ofs;
659 ret = read_to_buf(&rootfs_info, p);
664 writelen += rootfs_info.file_size;
666 writelen = rootfs_ofs + rootfs_info.file_size;
669 writelen = pad_jffs2(buf, writelen);
675 fill_header(buf, writelen);
676 ret = write_fw(buf, writelen);
688 /* Helper functions to inspect_fw() representing different output formats */
689 static inline void inspect_fw_pstr(char *label, char *str)
691 printf("%-23s: %s\n", label, str);
694 static inline void inspect_fw_phex(char *label, uint32_t val)
696 printf("%-23s: 0x%08x\n", label, val);
699 static inline void inspect_fw_phexpost(char *label,
700 uint32_t val, char *post)
702 printf("%-23s: 0x%08x (%s)\n", label, val, post);
705 static inline void inspect_fw_phexdef(char *label,
706 uint32_t val, uint32_t defval)
708 printf("%-23s: 0x%08x ", label, val);
711 printf("(== OpenWrt default)\n");
713 printf("(OpenWrt default: 0x%08x)\n", defval);
716 static inline void inspect_fw_phexexp(char *label,
717 uint32_t val, uint32_t expval)
719 printf("%-23s: 0x%08x ", label, val);
724 printf("(expected: 0x%08x)\n", expval);
727 static inline void inspect_fw_phexdec(char *label, uint32_t val)
729 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
732 static inline void inspect_fw_phexdecdef(char *label,
733 uint32_t val, uint32_t defval)
735 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
738 printf("(== OpenWrt default)\n");
740 printf("(OpenWrt default: 0x%08x)\n", defval);
743 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
747 printf("%-23s:", label);
748 for (i=0; i<MD5SUM_LEN; i++)
749 printf(" %02x", val[i]);
750 printf(" %s\n", text);
753 static int inspect_fw(void)
756 struct fw_header *hdr;
757 uint8_t md5sum[MD5SUM_LEN];
758 struct board_info *board;
759 int ret = EXIT_FAILURE;
761 buf = malloc(inspect_info.file_size);
763 ERR("no memory for buffer!\n");
767 ret = read_to_buf(&inspect_info, buf);
770 hdr = (struct fw_header *)buf;
772 inspect_fw_pstr("File name", inspect_info.file_name);
773 inspect_fw_phexdec("File size", inspect_info.file_size);
775 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
776 ERR("file does not seem to have V1 header!\n");
780 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
782 if (ntohl(hdr->unk1) != 0)
783 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
785 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
786 if (ntohl(hdr->boot_len) == 0)
787 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
789 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
790 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
792 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
793 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
794 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
796 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
798 if (ntohl(hdr->unk2) != 0)
799 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
800 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
801 "(purpose yet unknown, unchecked here)");
802 if (ntohl(hdr->unk3) != 0)
803 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
807 inspect_fw_pstr("Vendor name", hdr->vendor_name);
808 inspect_fw_pstr("Firmware version", hdr->fw_version);
809 board = find_board_by_hwid(ntohl(hdr->hw_id));
811 layout = find_layout(board->layout_id);
812 inspect_fw_phexpost("Hardware ID",
813 ntohl(hdr->hw_id), board->id);
814 inspect_fw_phexexp("Hardware Revision",
815 ntohl(hdr->hw_rev), board->hw_rev);
817 inspect_fw_phexpost("Hardware ID",
818 ntohl(hdr->hw_id), "unknown");
819 inspect_fw_phex("Hardware Revision",
825 inspect_fw_phexdec("Kernel data offset",
826 ntohl(hdr->kernel_ofs));
827 inspect_fw_phexdec("Kernel data length",
828 ntohl(hdr->kernel_len));
830 inspect_fw_phexdef("Kernel load address",
831 ntohl(hdr->kernel_la),
832 layout ? layout->kernel_la : 0xffffffff);
833 inspect_fw_phexdef("Kernel entry point",
834 ntohl(hdr->kernel_ep),
835 layout ? layout->kernel_ep : 0xffffffff);
836 inspect_fw_phexdecdef("Rootfs data offset",
837 ntohl(hdr->rootfs_ofs),
838 layout ? layout->rootfs_ofs : 0xffffffff);
840 inspect_fw_phex("Kernel load address",
841 ntohl(hdr->kernel_la));
842 inspect_fw_phex("Kernel entry point",
843 ntohl(hdr->kernel_ep));
844 inspect_fw_phexdec("Rootfs data offset",
845 ntohl(hdr->rootfs_ofs));
847 inspect_fw_phexdec("Rootfs data length",
848 ntohl(hdr->rootfs_len));
849 inspect_fw_phexdec("Boot loader data offset",
850 ntohl(hdr->boot_ofs));
851 inspect_fw_phexdec("Boot loader data length",
852 ntohl(hdr->boot_len));
853 inspect_fw_phexdec("Total firmware length",
854 ntohl(hdr->fw_length));
862 filename = malloc(strlen(inspect_info.file_name) + 8);
863 sprintf(filename, "%s-kernel", inspect_info.file_name);
864 printf("Extracting kernel to \"%s\"...\n", filename);
865 fp = fopen(filename, "w");
867 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
868 ntohl(hdr->kernel_len), 1, fp)) {
869 ERR("error in fwrite(): %s", strerror(errno));
873 ERR("error in fopen(): %s", strerror(errno));
877 filename = malloc(strlen(inspect_info.file_name) + 8);
878 sprintf(filename, "%s-rootfs", inspect_info.file_name);
879 printf("Extracting rootfs to \"%s\"...\n", filename);
880 fp = fopen(filename, "w");
882 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
883 ntohl(hdr->rootfs_len), 1, fp)) {
884 ERR("error in fwrite(): %s", strerror(errno));
888 ERR("error in fopen(): %s", strerror(errno));
899 int main(int argc, char *argv[])
901 int ret = EXIT_FAILURE;
906 progname = basename(argv[0]);
911 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsj");
917 sscanf(optarg, "0x%x", &rootfs_align);
926 sscanf(optarg, "0x%x", &kernel_ep);
935 sscanf(optarg, "0x%x", &kernel_la);
947 kernel_info.file_name = optarg;
950 rootfs_info.file_name = optarg;
953 sscanf(optarg, "0x%x", &rootfs_ofs);
962 inspect_info.file_name = optarg;
979 ret = check_options();
983 if (!inspect_info.file_name)