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_WA901ND_V1 0x09010001
37 #define HWID_TL_WA901ND_V2 0x09010002
38 #define HWID_TL_WR703N_V1 0x07030101
39 #define HWID_TL_WR741ND_V1 0x07410001
40 #define HWID_TL_WR741ND_V4 0x07410004
41 #define HWID_TL_WR740N_V1 0x07400001
42 #define HWID_TL_WR740N_V3 0x07400003
43 #define HWID_TL_WR743ND_V1 0x07430001
44 #define HWID_TL_WR841N_V1_5 0x08410002
45 #define HWID_TL_WR841ND_V3 0x08410003
46 #define HWID_TL_WR841ND_V5 0x08410005
47 #define HWID_TL_WR841ND_V7 0x08410007
48 #define HWID_TL_WR941ND_V2 0x09410002
49 #define HWID_TL_WR941ND_V4 0x09410004
50 #define HWID_TL_WR1043ND_V1 0x10430001
55 char *file_name; /* name of the file */
56 uint32_t file_size; /* length of the file */
60 uint32_t version; /* header version */
63 uint32_t hw_id; /* hardware id */
64 uint32_t hw_rev; /* hardware revision */
66 uint8_t md5sum1[MD5SUM_LEN];
68 uint8_t md5sum2[MD5SUM_LEN];
70 uint32_t kernel_la; /* kernel load address */
71 uint32_t kernel_ep; /* kernel entry point */
72 uint32_t fw_length; /* total length of the firmware */
73 uint32_t kernel_ofs; /* kernel data offset */
74 uint32_t kernel_len; /* kernel data length */
75 uint32_t rootfs_ofs; /* rootfs data offset */
76 uint32_t rootfs_len; /* rootfs data length */
77 uint32_t boot_ofs; /* bootloader data offset */
78 uint32_t boot_len; /* bootloader data length */
80 } __attribute__ ((packed));
101 static char *progname;
102 static char *vendor = "TP-LINK Technologies";
103 static char *version = "ver. 1.0";
105 static char *board_id;
106 static struct board_info *board;
107 static char *layout_id;
108 static struct flash_layout *layout;
109 static char *opt_hw_id;
110 static uint32_t hw_id;
111 static char *opt_hw_rev;
112 static uint32_t hw_rev;
113 static struct file_info kernel_info;
114 static uint32_t kernel_la = 0;
115 static uint32_t kernel_ep = 0;
116 static uint32_t kernel_len = 0;
117 static struct file_info rootfs_info;
118 static uint32_t rootfs_ofs = 0;
119 static uint32_t rootfs_align;
120 static struct file_info boot_info;
122 static int strip_padding;
123 static int add_jffs2_eof;
124 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
126 static struct file_info inspect_info;
127 static int extract = 0;
129 char md5salt_normal[MD5SUM_LEN] = {
130 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
131 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
134 char md5salt_boot[MD5SUM_LEN] = {
135 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
136 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
139 static struct flash_layout layouts[] = {
142 .fw_max_len = 0x3c0000,
143 .kernel_la = 0x80060000,
144 .kernel_ep = 0x80060000,
145 .rootfs_ofs = 0x140000,
148 .fw_max_len = 0x3c0000,
149 .kernel_la = 0x80060000,
150 .kernel_ep = 0x80060000,
151 .rootfs_ofs = 0x100000,
154 .fw_max_len = 0x7c0000,
155 .kernel_la = 0x80060000,
156 .kernel_ep = 0x80060000,
157 .rootfs_ofs = 0x140000,
159 /* terminating entry */
163 static struct board_info boards[] = {
166 .hw_id = HWID_TL_MR3020_V1,
168 .layout_id = "4Mlzma",
171 .hw_id = HWID_TL_MR3220_V1,
176 .hw_id = HWID_TL_MR3420_V1,
180 .id = "TL-WA901NDv1",
181 .hw_id = HWID_TL_WA901ND_V1,
185 .id = "TL-WA901NDv2",
186 .hw_id = HWID_TL_WA901ND_V2,
190 .id = "TL-WR741NDv1",
191 .hw_id = HWID_TL_WR741ND_V1,
195 .id = "TL-WR741NDv4",
196 .hw_id = HWID_TL_WR741ND_V4,
198 .layout_id = "4Mlzma",
201 .hw_id = HWID_TL_WR740N_V1,
206 .hw_id = HWID_TL_WR740N_V3,
210 .id = "TL-WR743NDv1",
211 .hw_id = HWID_TL_WR743ND_V1,
215 .id = "TL-WR841Nv1.5",
216 .hw_id = HWID_TL_WR841N_V1_5,
220 .id = "TL-WR841NDv3",
221 .hw_id = HWID_TL_WR841ND_V3,
225 .id = "TL-WR841NDv5",
226 .hw_id = HWID_TL_WR841ND_V5,
230 .id = "TL-WR841NDv7",
231 .hw_id = HWID_TL_WR841ND_V7,
235 .id = "TL-WR941NDv2",
236 .hw_id = HWID_TL_WR941ND_V2,
240 .id = "TL-WR941NDv4",
241 .hw_id = HWID_TL_WR941ND_V4,
245 .id = "TL-WR1043NDv1",
246 .hw_id = HWID_TL_WR1043ND_V1,
251 .hw_id = HWID_TL_WR703N_V1,
253 .layout_id = "4Mlzma",
255 /* terminating entry */
262 #define ERR(fmt, ...) do { \
264 fprintf(stderr, "[%s] *** error: " fmt "\n", \
265 progname, ## __VA_ARGS__ ); \
268 #define ERRS(fmt, ...) do { \
271 fprintf(stderr, "[%s] *** error: " fmt "\n", \
272 progname, ## __VA_ARGS__, strerror(save)); \
275 #define DBG(fmt, ...) do { \
276 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
279 static struct board_info *find_board(char *id)
281 struct board_info *ret;
282 struct board_info *board;
285 for (board = boards; board->id != NULL; board++){
286 if (strcasecmp(id, board->id) == 0) {
295 static struct board_info *find_board_by_hwid(uint32_t hw_id)
297 struct board_info *board;
299 for (board = boards; board->id != NULL; board++) {
300 if (hw_id == board->hw_id)
307 static struct flash_layout *find_layout(char *id)
309 struct flash_layout *ret;
310 struct flash_layout *l;
313 for (l = layouts; l->id != NULL; l++){
314 if (strcasecmp(id, l->id) == 0) {
323 static void usage(int status)
325 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
326 struct board_info *board;
328 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
332 " -B <board> create image for the board specified with <board>\n"
333 " -c use combined kernel image\n"
334 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
335 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
336 " -H <hwid> use hardware id specified with <hwid>\n"
337 " -F <id> use flash layout specified with <id>\n"
338 " -k <file> read kernel image from the file <file>\n"
339 " -r <file> read rootfs image from the file <file>\n"
340 " -a <align> align the rootfs start on an <align> bytes boundary\n"
341 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
342 " -o <file> write output to the file <file>\n"
343 " -s strip padding from the end of the image\n"
344 " -j add jffs2 end-of-filesystem markers\n"
345 " -N <vendor> set image vendor to <vendor>\n"
346 " -V <version> set image version to <version>\n"
347 " -i <file> inspect given firmware file <file>\n"
348 " -x extract kernel and rootfs while inspecting (requires -i)\n"
349 " -h show this screen\n"
355 static int get_md5(char *data, int size, char *md5)
360 MD5_Update(&ctx, data, size);
361 MD5_Final(md5, &ctx);
364 static int get_file_stat(struct file_info *fdata)
369 if (fdata->file_name == NULL)
372 res = stat(fdata->file_name, &st);
374 ERRS("stat failed on %s", fdata->file_name);
378 fdata->file_size = st.st_size;
382 static int read_to_buf(struct file_info *fdata, char *buf)
385 int ret = EXIT_FAILURE;
387 f = fopen(fdata->file_name, "r");
389 ERRS("could not open \"%s\" for reading", fdata->file_name);
394 fread(buf, fdata->file_size, 1, f);
396 ERRS("unable to read from file \"%s\"", fdata->file_name);
408 static int check_options(void)
412 if (inspect_info.file_name) {
413 ret = get_file_stat(&inspect_info);
418 } else if (extract) {
419 ERR("no firmware for inspection specified");
423 if (board_id == NULL && opt_hw_id == NULL) {
424 ERR("either board or hardware id must be specified");
429 board = find_board(board_id);
431 ERR("unknown/unsupported board id \"%s\"", board_id);
434 if (layout_id == NULL)
435 layout_id = board->layout_id;
437 hw_id = board->hw_id;
438 hw_rev = board->hw_rev;
440 if (layout_id == NULL) {
441 ERR("flash layout is not specified");
444 hw_id = strtoul(opt_hw_id, NULL, 0);
447 hw_rev = strtoul(opt_hw_rev, NULL, 0);
452 layout = find_layout(layout_id);
453 if (layout == NULL) {
454 ERR("unknown flash layout \"%s\"", layout_id);
459 kernel_la = layout->kernel_la;
461 kernel_ep = layout->kernel_ep;
463 rootfs_ofs = layout->rootfs_ofs;
465 if (kernel_info.file_name == NULL) {
466 ERR("no kernel image specified");
470 ret = get_file_stat(&kernel_info);
474 kernel_len = kernel_info.file_size;
477 if (kernel_info.file_size >
478 layout->fw_max_len - sizeof(struct fw_header)) {
479 ERR("kernel image is too big");
483 if (rootfs_info.file_name == NULL) {
484 ERR("no rootfs image specified");
488 ret = get_file_stat(&rootfs_info);
493 kernel_len += sizeof(struct fw_header);
494 kernel_len = ALIGN(kernel_len, rootfs_align);
495 kernel_len -= sizeof(struct fw_header);
497 DBG("kernel length aligned to %u", kernel_len);
499 if (kernel_len + rootfs_info.file_size >
500 layout->fw_max_len - sizeof(struct fw_header)) {
501 ERR("images are too big");
505 if (kernel_info.file_size >
506 rootfs_ofs - sizeof(struct fw_header)) {
507 ERR("kernel image is too big");
511 if (rootfs_info.file_size >
512 (layout->fw_max_len - rootfs_ofs)) {
513 ERR("rootfs image is too big");
519 if (ofname == NULL) {
520 ERR("no output file specified");
527 static void fill_header(char *buf, int len)
529 struct fw_header *hdr = (struct fw_header *)buf;
531 memset(hdr, 0, sizeof(struct fw_header));
533 hdr->version = htonl(HEADER_VERSION_V1);
534 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
535 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
536 hdr->hw_id = htonl(hw_id);
537 hdr->hw_rev = htonl(hw_rev);
539 if (boot_info.file_size == 0)
540 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
542 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
544 hdr->kernel_la = htonl(kernel_la);
545 hdr->kernel_ep = htonl(kernel_ep);
546 hdr->fw_length = htonl(layout->fw_max_len);
547 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
548 hdr->kernel_len = htonl(kernel_len);
550 hdr->rootfs_ofs = htonl(rootfs_ofs);
551 hdr->rootfs_len = htonl(rootfs_info.file_size);
554 get_md5(buf, len, hdr->md5sum1);
557 static int pad_jffs2(char *buf, int currlen)
563 pad_mask = (64 * 1024);
564 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
568 for (i = 10; i < 32; i++) {
574 len = ALIGN(len, mask);
576 for (i = 10; i < 32; i++) {
578 if ((len & (mask - 1)) == 0)
582 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
583 buf[len + i] = jffs2_eof_mark[i];
585 len += sizeof(jffs2_eof_mark);
591 static int write_fw(char *data, int len)
594 int ret = EXIT_FAILURE;
596 f = fopen(ofname, "w");
598 ERRS("could not open \"%s\" for writing", ofname);
603 fwrite(data, len, 1, f);
605 ERRS("unable to write output file");
609 DBG("firmware file \"%s\" completed", ofname);
616 if (ret != EXIT_SUCCESS) {
623 static int build_fw(void)
628 int ret = EXIT_FAILURE;
631 buflen = layout->fw_max_len;
633 buf = malloc(buflen);
635 ERR("no memory for buffer\n");
639 memset(buf, 0xff, buflen);
640 p = buf + sizeof(struct fw_header);
641 ret = read_to_buf(&kernel_info, p);
645 writelen = sizeof(struct fw_header) + kernel_len;
651 p = buf + rootfs_ofs;
653 ret = read_to_buf(&rootfs_info, p);
658 writelen += rootfs_info.file_size;
660 writelen = rootfs_ofs + rootfs_info.file_size;
663 writelen = pad_jffs2(buf, writelen);
669 fill_header(buf, writelen);
670 ret = write_fw(buf, writelen);
682 /* Helper functions to inspect_fw() representing different output formats */
683 static inline void inspect_fw_pstr(char *label, char *str)
685 printf("%-23s: %s\n", label, str);
688 static inline void inspect_fw_phex(char *label, uint32_t val)
690 printf("%-23s: 0x%08x\n", label, val);
693 static inline void inspect_fw_phexpost(char *label,
694 uint32_t val, char *post)
696 printf("%-23s: 0x%08x (%s)\n", label, val, post);
699 static inline void inspect_fw_phexdef(char *label,
700 uint32_t val, uint32_t defval)
702 printf("%-23s: 0x%08x ", label, val);
705 printf("(== OpenWrt default)\n");
707 printf("(OpenWrt default: 0x%08x)\n", defval);
710 static inline void inspect_fw_phexexp(char *label,
711 uint32_t val, uint32_t expval)
713 printf("%-23s: 0x%08x ", label, val);
718 printf("(expected: 0x%08x)\n", expval);
721 static inline void inspect_fw_phexdec(char *label, uint32_t val)
723 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
726 static inline void inspect_fw_phexdecdef(char *label,
727 uint32_t val, uint32_t defval)
729 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
732 printf("(== OpenWrt default)\n");
734 printf("(OpenWrt default: 0x%08x)\n", defval);
737 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
741 printf("%-23s:", label);
742 for (i=0; i<MD5SUM_LEN; i++)
743 printf(" %02x", val[i]);
744 printf(" %s\n", text);
747 static int inspect_fw(void)
750 struct fw_header *hdr;
751 uint8_t md5sum[MD5SUM_LEN];
752 struct board_info *board;
753 int ret = EXIT_FAILURE;
755 buf = malloc(inspect_info.file_size);
757 ERR("no memory for buffer!\n");
761 ret = read_to_buf(&inspect_info, buf);
764 hdr = (struct fw_header *)buf;
766 inspect_fw_pstr("File name", inspect_info.file_name);
767 inspect_fw_phexdec("File size", inspect_info.file_size);
769 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
770 ERR("file does not seem to have V1 header!\n");
774 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
776 if (ntohl(hdr->unk1) != 0)
777 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
779 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
780 if (ntohl(hdr->boot_len) == 0)
781 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
783 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
784 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
786 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
787 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
788 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
790 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
792 if (ntohl(hdr->unk2) != 0)
793 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
794 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
795 "(purpose yet unknown, unchecked here)");
796 if (ntohl(hdr->unk3) != 0)
797 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
801 inspect_fw_pstr("Vendor name", hdr->vendor_name);
802 inspect_fw_pstr("Firmware version", hdr->fw_version);
803 board = find_board_by_hwid(ntohl(hdr->hw_id));
805 layout = find_layout(board->layout_id);
806 inspect_fw_phexpost("Hardware ID",
807 ntohl(hdr->hw_id), board->id);
808 inspect_fw_phexexp("Hardware Revision",
809 ntohl(hdr->hw_rev), board->hw_rev);
811 inspect_fw_phexpost("Hardware ID",
812 ntohl(hdr->hw_id), "unknown");
813 inspect_fw_phex("Hardware Revision",
819 inspect_fw_phexdec("Kernel data offset",
820 ntohl(hdr->kernel_ofs));
821 inspect_fw_phexdec("Kernel data length",
822 ntohl(hdr->kernel_len));
824 inspect_fw_phexdef("Kernel load address",
825 ntohl(hdr->kernel_la),
826 layout ? layout->kernel_la : 0xffffffff);
827 inspect_fw_phexdef("Kernel entry point",
828 ntohl(hdr->kernel_ep),
829 layout ? layout->kernel_ep : 0xffffffff);
830 inspect_fw_phexdecdef("Rootfs data offset",
831 ntohl(hdr->rootfs_ofs),
832 layout ? layout->rootfs_ofs : 0xffffffff);
834 inspect_fw_phex("Kernel load address",
835 ntohl(hdr->kernel_la));
836 inspect_fw_phex("Kernel entry point",
837 ntohl(hdr->kernel_ep));
838 inspect_fw_phexdec("Rootfs data offset",
839 ntohl(hdr->rootfs_ofs));
841 inspect_fw_phexdec("Rootfs data length",
842 ntohl(hdr->rootfs_len));
843 inspect_fw_phexdec("Boot loader data offset",
844 ntohl(hdr->boot_ofs));
845 inspect_fw_phexdec("Boot loader data length",
846 ntohl(hdr->boot_len));
847 inspect_fw_phexdec("Total firmware length",
848 ntohl(hdr->fw_length));
856 filename = malloc(strlen(inspect_info.file_name) + 8);
857 sprintf(filename, "%s-kernel", inspect_info.file_name);
858 printf("Extracting kernel to \"%s\"...\n", filename);
859 fp = fopen(filename, "w");
861 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
862 ntohl(hdr->kernel_len), 1, fp)) {
863 ERR("error in fwrite(): %s", strerror(errno));
867 ERR("error in fopen(): %s", strerror(errno));
871 filename = malloc(strlen(inspect_info.file_name) + 8);
872 sprintf(filename, "%s-rootfs", inspect_info.file_name);
873 printf("Extracting rootfs to \"%s\"...\n", filename);
874 fp = fopen(filename, "w");
876 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
877 ntohl(hdr->rootfs_len), 1, fp)) {
878 ERR("error in fwrite(): %s", strerror(errno));
882 ERR("error in fopen(): %s", strerror(errno));
893 int main(int argc, char *argv[])
895 int ret = EXIT_FAILURE;
900 progname = basename(argv[0]);
905 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsj");
911 sscanf(optarg, "0x%x", &rootfs_align);
920 sscanf(optarg, "0x%x", &kernel_ep);
929 sscanf(optarg, "0x%x", &kernel_la);
941 kernel_info.file_name = optarg;
944 rootfs_info.file_name = optarg;
947 sscanf(optarg, "0x%x", &rootfs_ofs);
956 inspect_info.file_name = optarg;
973 ret = check_options();
977 if (!inspect_info.file_name)