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_MR3220_V2 0x32200002
36 #define HWID_TL_MR3420_V1 0x34200001
37 #define HWID_TL_WA701N_V1 0x07010001
38 #define HWID_TL_WA7510N_V1 0x75100001
39 #define HWID_TL_WA801ND_V1 0x08010001
40 #define HWID_TL_WA901ND_V1 0x09010001
41 #define HWID_TL_WA901ND_V2 0x09010002
42 #define HWID_TL_WR703N_V1 0x07030101
43 #define HWID_TL_WR741ND_V1 0x07410001
44 #define HWID_TL_WR741ND_V4 0x07410004
45 #define HWID_TL_WR740N_V1 0x07400001
46 #define HWID_TL_WR740N_V3 0x07400003
47 #define HWID_TL_WR743ND_V1 0x07430001
48 #define HWID_TL_WR743ND_V2 0x07430002
49 #define HWID_TL_WR841N_V1_5 0x08410002
50 #define HWID_TL_WR841ND_V3 0x08410003
51 #define HWID_TL_WR841ND_V5 0x08410005
52 #define HWID_TL_WR841ND_V7 0x08410007
53 #define HWID_TL_WR941ND_V2 0x09410002
54 #define HWID_TL_WR941ND_V4 0x09410004
55 #define HWID_TL_WR1043ND_V1 0x10430001
56 #define HWID_TL_WR1041N_V2 0x10410002
57 #define HWID_TL_WR2543N_V1 0x25430001
62 char *file_name; /* name of the file */
63 uint32_t file_size; /* length of the file */
67 uint32_t version; /* header version */
70 uint32_t hw_id; /* hardware id */
71 uint32_t hw_rev; /* hardware revision */
73 uint8_t md5sum1[MD5SUM_LEN];
75 uint8_t md5sum2[MD5SUM_LEN];
77 uint32_t kernel_la; /* kernel load address */
78 uint32_t kernel_ep; /* kernel entry point */
79 uint32_t fw_length; /* total length of the firmware */
80 uint32_t kernel_ofs; /* kernel data offset */
81 uint32_t kernel_len; /* kernel data length */
82 uint32_t rootfs_ofs; /* rootfs data offset */
83 uint32_t rootfs_len; /* rootfs data length */
84 uint32_t boot_ofs; /* bootloader data offset */
85 uint32_t boot_len; /* bootloader data length */
90 } __attribute__ ((packed));
111 static char *progname;
112 static char *vendor = "TP-LINK Technologies";
113 static char *version = "ver. 1.0";
114 static char *fw_ver = "0.0.0";
116 static char *board_id;
117 static struct board_info *board;
118 static char *layout_id;
119 static struct flash_layout *layout;
120 static char *opt_hw_id;
121 static uint32_t hw_id;
122 static char *opt_hw_rev;
123 static uint32_t hw_rev;
124 static int fw_ver_lo;
125 static int fw_ver_mid;
126 static int fw_ver_hi;
127 static struct file_info kernel_info;
128 static uint32_t kernel_la = 0;
129 static uint32_t kernel_ep = 0;
130 static uint32_t kernel_len = 0;
131 static struct file_info rootfs_info;
132 static uint32_t rootfs_ofs = 0;
133 static uint32_t rootfs_align;
134 static struct file_info boot_info;
136 static int strip_padding;
137 static int add_jffs2_eof;
138 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
140 static struct file_info inspect_info;
141 static int extract = 0;
143 char md5salt_normal[MD5SUM_LEN] = {
144 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
145 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
148 char md5salt_boot[MD5SUM_LEN] = {
149 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
150 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
153 static struct flash_layout layouts[] = {
156 .fw_max_len = 0x3c0000,
157 .kernel_la = 0x80060000,
158 .kernel_ep = 0x80060000,
159 .rootfs_ofs = 0x140000,
162 .fw_max_len = 0x3c0000,
163 .kernel_la = 0x80060000,
164 .kernel_ep = 0x80060000,
165 .rootfs_ofs = 0x100000,
168 .fw_max_len = 0x7c0000,
169 .kernel_la = 0x80060000,
170 .kernel_ep = 0x80060000,
171 .rootfs_ofs = 0x140000,
174 .fw_max_len = 0x7c0000,
175 .kernel_la = 0x80060000,
176 .kernel_ep = 0x80060000,
177 .rootfs_ofs = 0x100000,
179 /* terminating entry */
183 static struct board_info boards[] = {
186 .hw_id = HWID_TL_MR3020_V1,
188 .layout_id = "4Mlzma",
191 .hw_id = HWID_TL_MR3220_V1,
196 .hw_id = HWID_TL_MR3220_V2,
198 .layout_id = "4Mlzma",
201 .hw_id = HWID_TL_MR3420_V1,
206 .hw_id = HWID_TL_WA701N_V1,
211 .hw_id = HWID_TL_WA7510N_V1,
215 .id = "TL-WA801NDv1",
216 .hw_id = HWID_TL_WA801ND_V1,
220 .id = "TL-WA901NDv1",
221 .hw_id = HWID_TL_WA901ND_V1,
225 .id = "TL-WA901NDv2",
226 .hw_id = HWID_TL_WA901ND_V2,
230 .id = "TL-WR741NDv1",
231 .hw_id = HWID_TL_WR741ND_V1,
235 .id = "TL-WR741NDv4",
236 .hw_id = HWID_TL_WR741ND_V4,
238 .layout_id = "4Mlzma",
241 .hw_id = HWID_TL_WR740N_V1,
246 .hw_id = HWID_TL_WR740N_V3,
250 .id = "TL-WR743NDv1",
251 .hw_id = HWID_TL_WR743ND_V1,
255 .id = "TL-WR743NDv2",
256 .hw_id = HWID_TL_WR743ND_V2,
258 .layout_id = "4Mlzma",
260 .id = "TL-WR841Nv1.5",
261 .hw_id = HWID_TL_WR841N_V1_5,
265 .id = "TL-WR841NDv3",
266 .hw_id = HWID_TL_WR841ND_V3,
270 .id = "TL-WR841NDv5",
271 .hw_id = HWID_TL_WR841ND_V5,
275 .id = "TL-WR841NDv7",
276 .hw_id = HWID_TL_WR841ND_V7,
280 .id = "TL-WR941NDv2",
281 .hw_id = HWID_TL_WR941ND_V2,
285 .id = "TL-WR941NDv4",
286 .hw_id = HWID_TL_WR941ND_V4,
290 .id = "TL-WR1041Nv2",
291 .hw_id = HWID_TL_WR1041N_V2,
293 .layout_id = "4Mlzma",
295 .id = "TL-WR1043NDv1",
296 .hw_id = HWID_TL_WR1043ND_V1,
300 .id = "TL-WR2543Nv1",
301 .hw_id = HWID_TL_WR2543N_V1,
303 .layout_id = "8Mlzma",
306 .hw_id = HWID_TL_WR703N_V1,
308 .layout_id = "4Mlzma",
310 /* terminating entry */
317 #define ERR(fmt, ...) do { \
319 fprintf(stderr, "[%s] *** error: " fmt "\n", \
320 progname, ## __VA_ARGS__ ); \
323 #define ERRS(fmt, ...) do { \
326 fprintf(stderr, "[%s] *** error: " fmt "\n", \
327 progname, ## __VA_ARGS__, strerror(save)); \
330 #define DBG(fmt, ...) do { \
331 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
334 static struct board_info *find_board(char *id)
336 struct board_info *ret;
337 struct board_info *board;
340 for (board = boards; board->id != NULL; board++){
341 if (strcasecmp(id, board->id) == 0) {
350 static struct board_info *find_board_by_hwid(uint32_t hw_id)
352 struct board_info *board;
354 for (board = boards; board->id != NULL; board++) {
355 if (hw_id == board->hw_id)
362 static struct flash_layout *find_layout(char *id)
364 struct flash_layout *ret;
365 struct flash_layout *l;
368 for (l = layouts; l->id != NULL; l++){
369 if (strcasecmp(id, l->id) == 0) {
378 static void usage(int status)
380 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
381 struct board_info *board;
383 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
387 " -B <board> create image for the board specified with <board>\n"
388 " -c use combined kernel image\n"
389 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
390 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
391 " -H <hwid> use hardware id specified with <hwid>\n"
392 " -F <id> use flash layout specified with <id>\n"
393 " -k <file> read kernel image from the file <file>\n"
394 " -r <file> read rootfs image from the file <file>\n"
395 " -a <align> align the rootfs start on an <align> bytes boundary\n"
396 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
397 " -o <file> write output to the file <file>\n"
398 " -s strip padding from the end of the image\n"
399 " -j add jffs2 end-of-filesystem markers\n"
400 " -N <vendor> set image vendor to <vendor>\n"
401 " -V <version> set image version to <version>\n"
402 " -v <version> set firmware version to <version>\n"
403 " -i <file> inspect given firmware file <file>\n"
404 " -x extract kernel and rootfs while inspecting (requires -i)\n"
405 " -h show this screen\n"
411 static int get_md5(char *data, int size, char *md5)
416 MD5_Update(&ctx, data, size);
417 MD5_Final(md5, &ctx);
420 static int get_file_stat(struct file_info *fdata)
425 if (fdata->file_name == NULL)
428 res = stat(fdata->file_name, &st);
430 ERRS("stat failed on %s", fdata->file_name);
434 fdata->file_size = st.st_size;
438 static int read_to_buf(struct file_info *fdata, char *buf)
441 int ret = EXIT_FAILURE;
443 f = fopen(fdata->file_name, "r");
445 ERRS("could not open \"%s\" for reading", fdata->file_name);
450 fread(buf, fdata->file_size, 1, f);
452 ERRS("unable to read from file \"%s\"", fdata->file_name);
464 static int check_options(void)
468 if (inspect_info.file_name) {
469 ret = get_file_stat(&inspect_info);
474 } else if (extract) {
475 ERR("no firmware for inspection specified");
479 if (board_id == NULL && opt_hw_id == NULL) {
480 ERR("either board or hardware id must be specified");
485 board = find_board(board_id);
487 ERR("unknown/unsupported board id \"%s\"", board_id);
490 if (layout_id == NULL)
491 layout_id = board->layout_id;
493 hw_id = board->hw_id;
494 hw_rev = board->hw_rev;
496 if (layout_id == NULL) {
497 ERR("flash layout is not specified");
500 hw_id = strtoul(opt_hw_id, NULL, 0);
503 hw_rev = strtoul(opt_hw_rev, NULL, 0);
508 layout = find_layout(layout_id);
509 if (layout == NULL) {
510 ERR("unknown flash layout \"%s\"", layout_id);
515 kernel_la = layout->kernel_la;
517 kernel_ep = layout->kernel_ep;
519 rootfs_ofs = layout->rootfs_ofs;
521 if (kernel_info.file_name == NULL) {
522 ERR("no kernel image specified");
526 ret = get_file_stat(&kernel_info);
530 kernel_len = kernel_info.file_size;
533 if (kernel_info.file_size >
534 layout->fw_max_len - sizeof(struct fw_header)) {
535 ERR("kernel image is too big");
539 if (rootfs_info.file_name == NULL) {
540 ERR("no rootfs image specified");
544 ret = get_file_stat(&rootfs_info);
549 kernel_len += sizeof(struct fw_header);
550 kernel_len = ALIGN(kernel_len, rootfs_align);
551 kernel_len -= sizeof(struct fw_header);
553 DBG("kernel length aligned to %u", kernel_len);
555 if (kernel_len + rootfs_info.file_size >
556 layout->fw_max_len - sizeof(struct fw_header)) {
557 ERR("images are too big");
561 if (kernel_info.file_size >
562 rootfs_ofs - sizeof(struct fw_header)) {
563 ERR("kernel image is too big");
567 if (rootfs_info.file_size >
568 (layout->fw_max_len - rootfs_ofs)) {
569 ERR("rootfs image is too big");
575 if (ofname == NULL) {
576 ERR("no output file specified");
580 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
582 ERR("invalid firmware version '%s'", fw_ver);
589 static void fill_header(char *buf, int len)
591 struct fw_header *hdr = (struct fw_header *)buf;
593 memset(hdr, 0, sizeof(struct fw_header));
595 hdr->version = htonl(HEADER_VERSION_V1);
596 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
597 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
598 hdr->hw_id = htonl(hw_id);
599 hdr->hw_rev = htonl(hw_rev);
601 if (boot_info.file_size == 0)
602 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
604 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
606 hdr->kernel_la = htonl(kernel_la);
607 hdr->kernel_ep = htonl(kernel_ep);
608 hdr->fw_length = htonl(layout->fw_max_len);
609 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
610 hdr->kernel_len = htonl(kernel_len);
612 hdr->rootfs_ofs = htonl(rootfs_ofs);
613 hdr->rootfs_len = htonl(rootfs_info.file_size);
616 hdr->ver_hi = htons(fw_ver_hi);
617 hdr->ver_mid = htons(fw_ver_mid);
618 hdr->ver_lo = htons(fw_ver_lo);
620 get_md5(buf, len, hdr->md5sum1);
623 static int pad_jffs2(char *buf, int currlen)
629 pad_mask = (64 * 1024);
630 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
634 for (i = 10; i < 32; i++) {
640 len = ALIGN(len, mask);
642 for (i = 10; i < 32; i++) {
644 if ((len & (mask - 1)) == 0)
648 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
649 buf[len + i] = jffs2_eof_mark[i];
651 len += sizeof(jffs2_eof_mark);
657 static int write_fw(char *data, int len)
660 int ret = EXIT_FAILURE;
662 f = fopen(ofname, "w");
664 ERRS("could not open \"%s\" for writing", ofname);
669 fwrite(data, len, 1, f);
671 ERRS("unable to write output file");
675 DBG("firmware file \"%s\" completed", ofname);
682 if (ret != EXIT_SUCCESS) {
689 static int build_fw(void)
694 int ret = EXIT_FAILURE;
697 buflen = layout->fw_max_len;
699 buf = malloc(buflen);
701 ERR("no memory for buffer\n");
705 memset(buf, 0xff, buflen);
706 p = buf + sizeof(struct fw_header);
707 ret = read_to_buf(&kernel_info, p);
711 writelen = sizeof(struct fw_header) + kernel_len;
717 p = buf + rootfs_ofs;
719 ret = read_to_buf(&rootfs_info, p);
724 writelen += rootfs_info.file_size;
726 writelen = rootfs_ofs + rootfs_info.file_size;
729 writelen = pad_jffs2(buf, writelen);
735 fill_header(buf, writelen);
736 ret = write_fw(buf, writelen);
748 /* Helper functions to inspect_fw() representing different output formats */
749 static inline void inspect_fw_pstr(char *label, char *str)
751 printf("%-23s: %s\n", label, str);
754 static inline void inspect_fw_phex(char *label, uint32_t val)
756 printf("%-23s: 0x%08x\n", label, val);
759 static inline void inspect_fw_phexpost(char *label,
760 uint32_t val, char *post)
762 printf("%-23s: 0x%08x (%s)\n", label, val, post);
765 static inline void inspect_fw_phexdef(char *label,
766 uint32_t val, uint32_t defval)
768 printf("%-23s: 0x%08x ", label, val);
771 printf("(== OpenWrt default)\n");
773 printf("(OpenWrt default: 0x%08x)\n", defval);
776 static inline void inspect_fw_phexexp(char *label,
777 uint32_t val, uint32_t expval)
779 printf("%-23s: 0x%08x ", label, val);
784 printf("(expected: 0x%08x)\n", expval);
787 static inline void inspect_fw_phexdec(char *label, uint32_t val)
789 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
792 static inline void inspect_fw_phexdecdef(char *label,
793 uint32_t val, uint32_t defval)
795 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
798 printf("(== OpenWrt default)\n");
800 printf("(OpenWrt default: 0x%08x)\n", defval);
803 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
807 printf("%-23s:", label);
808 for (i=0; i<MD5SUM_LEN; i++)
809 printf(" %02x", val[i]);
810 printf(" %s\n", text);
813 static int inspect_fw(void)
816 struct fw_header *hdr;
817 uint8_t md5sum[MD5SUM_LEN];
818 struct board_info *board;
819 int ret = EXIT_FAILURE;
821 buf = malloc(inspect_info.file_size);
823 ERR("no memory for buffer!\n");
827 ret = read_to_buf(&inspect_info, buf);
830 hdr = (struct fw_header *)buf;
832 inspect_fw_pstr("File name", inspect_info.file_name);
833 inspect_fw_phexdec("File size", inspect_info.file_size);
835 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
836 ERR("file does not seem to have V1 header!\n");
840 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
842 if (ntohl(hdr->unk1) != 0)
843 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
845 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
846 if (ntohl(hdr->boot_len) == 0)
847 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
849 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
850 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
852 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
853 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
854 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
856 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
858 if (ntohl(hdr->unk2) != 0)
859 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
860 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
861 "(purpose yet unknown, unchecked here)");
862 if (ntohl(hdr->unk3) != 0)
863 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
867 inspect_fw_pstr("Vendor name", hdr->vendor_name);
868 inspect_fw_pstr("Firmware version", hdr->fw_version);
869 board = find_board_by_hwid(ntohl(hdr->hw_id));
871 layout = find_layout(board->layout_id);
872 inspect_fw_phexpost("Hardware ID",
873 ntohl(hdr->hw_id), board->id);
874 inspect_fw_phexexp("Hardware Revision",
875 ntohl(hdr->hw_rev), board->hw_rev);
877 inspect_fw_phexpost("Hardware ID",
878 ntohl(hdr->hw_id), "unknown");
879 inspect_fw_phex("Hardware Revision",
885 inspect_fw_phexdec("Kernel data offset",
886 ntohl(hdr->kernel_ofs));
887 inspect_fw_phexdec("Kernel data length",
888 ntohl(hdr->kernel_len));
890 inspect_fw_phexdef("Kernel load address",
891 ntohl(hdr->kernel_la),
892 layout ? layout->kernel_la : 0xffffffff);
893 inspect_fw_phexdef("Kernel entry point",
894 ntohl(hdr->kernel_ep),
895 layout ? layout->kernel_ep : 0xffffffff);
896 inspect_fw_phexdecdef("Rootfs data offset",
897 ntohl(hdr->rootfs_ofs),
898 layout ? layout->rootfs_ofs : 0xffffffff);
900 inspect_fw_phex("Kernel load address",
901 ntohl(hdr->kernel_la));
902 inspect_fw_phex("Kernel entry point",
903 ntohl(hdr->kernel_ep));
904 inspect_fw_phexdec("Rootfs data offset",
905 ntohl(hdr->rootfs_ofs));
907 inspect_fw_phexdec("Rootfs data length",
908 ntohl(hdr->rootfs_len));
909 inspect_fw_phexdec("Boot loader data offset",
910 ntohl(hdr->boot_ofs));
911 inspect_fw_phexdec("Boot loader data length",
912 ntohl(hdr->boot_len));
913 inspect_fw_phexdec("Total firmware length",
914 ntohl(hdr->fw_length));
922 filename = malloc(strlen(inspect_info.file_name) + 8);
923 sprintf(filename, "%s-kernel", inspect_info.file_name);
924 printf("Extracting kernel to \"%s\"...\n", filename);
925 fp = fopen(filename, "w");
927 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
928 ntohl(hdr->kernel_len), 1, fp)) {
929 ERR("error in fwrite(): %s", strerror(errno));
933 ERR("error in fopen(): %s", strerror(errno));
937 filename = malloc(strlen(inspect_info.file_name) + 8);
938 sprintf(filename, "%s-rootfs", inspect_info.file_name);
939 printf("Extracting rootfs to \"%s\"...\n", filename);
940 fp = fopen(filename, "w");
942 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
943 ntohl(hdr->rootfs_len), 1, fp)) {
944 ERR("error in fwrite(): %s", strerror(errno));
948 ERR("error in fopen(): %s", strerror(errno));
959 int main(int argc, char *argv[])
961 int ret = EXIT_FAILURE;
966 progname = basename(argv[0]);
971 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
977 sscanf(optarg, "0x%x", &rootfs_align);
986 sscanf(optarg, "0x%x", &kernel_ep);
995 sscanf(optarg, "0x%x", &kernel_la);
1010 kernel_info.file_name = optarg;
1013 rootfs_info.file_name = optarg;
1016 sscanf(optarg, "0x%x", &rootfs_ofs);
1025 inspect_info.file_name = optarg;
1034 usage(EXIT_SUCCESS);
1037 usage(EXIT_FAILURE);
1042 ret = check_options();
1046 if (!inspect_info.file_name)