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_WA7510N_V1 0x75100001
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
53 #define HWID_TL_WR1041N_V2 0x10410002
54 #define HWID_TL_WR2543N_V1 0x25430001
59 char *file_name; /* name of the file */
60 uint32_t file_size; /* length of the file */
64 uint32_t version; /* header version */
67 uint32_t hw_id; /* hardware id */
68 uint32_t hw_rev; /* hardware revision */
70 uint8_t md5sum1[MD5SUM_LEN];
72 uint8_t md5sum2[MD5SUM_LEN];
74 uint32_t kernel_la; /* kernel load address */
75 uint32_t kernel_ep; /* kernel entry point */
76 uint32_t fw_length; /* total length of the firmware */
77 uint32_t kernel_ofs; /* kernel data offset */
78 uint32_t kernel_len; /* kernel data length */
79 uint32_t rootfs_ofs; /* rootfs data offset */
80 uint32_t rootfs_len; /* rootfs data length */
81 uint32_t boot_ofs; /* bootloader data offset */
82 uint32_t boot_len; /* bootloader data length */
87 } __attribute__ ((packed));
108 static char *progname;
109 static char *vendor = "TP-LINK Technologies";
110 static char *version = "ver. 1.0";
111 static char *fw_ver = "0.0.0";
113 static char *board_id;
114 static struct board_info *board;
115 static char *layout_id;
116 static struct flash_layout *layout;
117 static char *opt_hw_id;
118 static uint32_t hw_id;
119 static char *opt_hw_rev;
120 static uint32_t hw_rev;
121 static int fw_ver_lo;
122 static int fw_ver_mid;
123 static int fw_ver_hi;
124 static struct file_info kernel_info;
125 static uint32_t kernel_la = 0;
126 static uint32_t kernel_ep = 0;
127 static uint32_t kernel_len = 0;
128 static struct file_info rootfs_info;
129 static uint32_t rootfs_ofs = 0;
130 static uint32_t rootfs_align;
131 static struct file_info boot_info;
133 static int strip_padding;
134 static int add_jffs2_eof;
135 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
137 static struct file_info inspect_info;
138 static int extract = 0;
140 char md5salt_normal[MD5SUM_LEN] = {
141 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
142 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
145 char md5salt_boot[MD5SUM_LEN] = {
146 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
147 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
150 static struct flash_layout layouts[] = {
153 .fw_max_len = 0x3c0000,
154 .kernel_la = 0x80060000,
155 .kernel_ep = 0x80060000,
156 .rootfs_ofs = 0x140000,
159 .fw_max_len = 0x3c0000,
160 .kernel_la = 0x80060000,
161 .kernel_ep = 0x80060000,
162 .rootfs_ofs = 0x100000,
165 .fw_max_len = 0x7c0000,
166 .kernel_la = 0x80060000,
167 .kernel_ep = 0x80060000,
168 .rootfs_ofs = 0x140000,
171 .fw_max_len = 0x7c0000,
172 .kernel_la = 0x80060000,
173 .kernel_ep = 0x80060000,
174 .rootfs_ofs = 0x100000,
176 /* terminating entry */
180 static struct board_info boards[] = {
183 .hw_id = HWID_TL_MR3020_V1,
185 .layout_id = "4Mlzma",
188 .hw_id = HWID_TL_MR3220_V1,
193 .hw_id = HWID_TL_MR3420_V1,
198 .hw_id = HWID_TL_WA701N_V1,
203 .hw_id = HWID_TL_WA7510N_V1,
207 .id = "TL-WA901NDv1",
208 .hw_id = HWID_TL_WA901ND_V1,
212 .id = "TL-WA901NDv2",
213 .hw_id = HWID_TL_WA901ND_V2,
217 .id = "TL-WR741NDv1",
218 .hw_id = HWID_TL_WR741ND_V1,
222 .id = "TL-WR741NDv4",
223 .hw_id = HWID_TL_WR741ND_V4,
225 .layout_id = "4Mlzma",
228 .hw_id = HWID_TL_WR740N_V1,
233 .hw_id = HWID_TL_WR740N_V3,
237 .id = "TL-WR743NDv1",
238 .hw_id = HWID_TL_WR743ND_V1,
242 .id = "TL-WR841Nv1.5",
243 .hw_id = HWID_TL_WR841N_V1_5,
247 .id = "TL-WR841NDv3",
248 .hw_id = HWID_TL_WR841ND_V3,
252 .id = "TL-WR841NDv5",
253 .hw_id = HWID_TL_WR841ND_V5,
257 .id = "TL-WR841NDv7",
258 .hw_id = HWID_TL_WR841ND_V7,
262 .id = "TL-WR941NDv2",
263 .hw_id = HWID_TL_WR941ND_V2,
267 .id = "TL-WR941NDv4",
268 .hw_id = HWID_TL_WR941ND_V4,
272 .id = "TL-WR1041Nv2",
273 .hw_id = HWID_TL_WR1041N_V2,
275 .layout_id = "4Mlzma",
277 .id = "TL-WR1043NDv1",
278 .hw_id = HWID_TL_WR1043ND_V1,
282 .id = "TL-WR2543Nv1",
283 .hw_id = HWID_TL_WR2543N_V1,
285 .layout_id = "8Mlzma",
288 .hw_id = HWID_TL_WR703N_V1,
290 .layout_id = "4Mlzma",
292 /* terminating entry */
299 #define ERR(fmt, ...) do { \
301 fprintf(stderr, "[%s] *** error: " fmt "\n", \
302 progname, ## __VA_ARGS__ ); \
305 #define ERRS(fmt, ...) do { \
308 fprintf(stderr, "[%s] *** error: " fmt "\n", \
309 progname, ## __VA_ARGS__, strerror(save)); \
312 #define DBG(fmt, ...) do { \
313 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
316 static struct board_info *find_board(char *id)
318 struct board_info *ret;
319 struct board_info *board;
322 for (board = boards; board->id != NULL; board++){
323 if (strcasecmp(id, board->id) == 0) {
332 static struct board_info *find_board_by_hwid(uint32_t hw_id)
334 struct board_info *board;
336 for (board = boards; board->id != NULL; board++) {
337 if (hw_id == board->hw_id)
344 static struct flash_layout *find_layout(char *id)
346 struct flash_layout *ret;
347 struct flash_layout *l;
350 for (l = layouts; l->id != NULL; l++){
351 if (strcasecmp(id, l->id) == 0) {
360 static void usage(int status)
362 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
363 struct board_info *board;
365 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
369 " -B <board> create image for the board specified with <board>\n"
370 " -c use combined kernel image\n"
371 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
372 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
373 " -H <hwid> use hardware id specified with <hwid>\n"
374 " -F <id> use flash layout specified with <id>\n"
375 " -k <file> read kernel image from the file <file>\n"
376 " -r <file> read rootfs image from the file <file>\n"
377 " -a <align> align the rootfs start on an <align> bytes boundary\n"
378 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
379 " -o <file> write output to the file <file>\n"
380 " -s strip padding from the end of the image\n"
381 " -j add jffs2 end-of-filesystem markers\n"
382 " -N <vendor> set image vendor to <vendor>\n"
383 " -V <version> set image version to <version>\n"
384 " -v <version> set firmware version to <version>\n"
385 " -i <file> inspect given firmware file <file>\n"
386 " -x extract kernel and rootfs while inspecting (requires -i)\n"
387 " -h show this screen\n"
393 static int get_md5(char *data, int size, char *md5)
398 MD5_Update(&ctx, data, size);
399 MD5_Final(md5, &ctx);
402 static int get_file_stat(struct file_info *fdata)
407 if (fdata->file_name == NULL)
410 res = stat(fdata->file_name, &st);
412 ERRS("stat failed on %s", fdata->file_name);
416 fdata->file_size = st.st_size;
420 static int read_to_buf(struct file_info *fdata, char *buf)
423 int ret = EXIT_FAILURE;
425 f = fopen(fdata->file_name, "r");
427 ERRS("could not open \"%s\" for reading", fdata->file_name);
432 fread(buf, fdata->file_size, 1, f);
434 ERRS("unable to read from file \"%s\"", fdata->file_name);
446 static int check_options(void)
450 if (inspect_info.file_name) {
451 ret = get_file_stat(&inspect_info);
456 } else if (extract) {
457 ERR("no firmware for inspection specified");
461 if (board_id == NULL && opt_hw_id == NULL) {
462 ERR("either board or hardware id must be specified");
467 board = find_board(board_id);
469 ERR("unknown/unsupported board id \"%s\"", board_id);
472 if (layout_id == NULL)
473 layout_id = board->layout_id;
475 hw_id = board->hw_id;
476 hw_rev = board->hw_rev;
478 if (layout_id == NULL) {
479 ERR("flash layout is not specified");
482 hw_id = strtoul(opt_hw_id, NULL, 0);
485 hw_rev = strtoul(opt_hw_rev, NULL, 0);
490 layout = find_layout(layout_id);
491 if (layout == NULL) {
492 ERR("unknown flash layout \"%s\"", layout_id);
497 kernel_la = layout->kernel_la;
499 kernel_ep = layout->kernel_ep;
501 rootfs_ofs = layout->rootfs_ofs;
503 if (kernel_info.file_name == NULL) {
504 ERR("no kernel image specified");
508 ret = get_file_stat(&kernel_info);
512 kernel_len = kernel_info.file_size;
515 if (kernel_info.file_size >
516 layout->fw_max_len - sizeof(struct fw_header)) {
517 ERR("kernel image is too big");
521 if (rootfs_info.file_name == NULL) {
522 ERR("no rootfs image specified");
526 ret = get_file_stat(&rootfs_info);
531 kernel_len += sizeof(struct fw_header);
532 kernel_len = ALIGN(kernel_len, rootfs_align);
533 kernel_len -= sizeof(struct fw_header);
535 DBG("kernel length aligned to %u", kernel_len);
537 if (kernel_len + rootfs_info.file_size >
538 layout->fw_max_len - sizeof(struct fw_header)) {
539 ERR("images are too big");
543 if (kernel_info.file_size >
544 rootfs_ofs - sizeof(struct fw_header)) {
545 ERR("kernel image is too big");
549 if (rootfs_info.file_size >
550 (layout->fw_max_len - rootfs_ofs)) {
551 ERR("rootfs image is too big");
557 if (ofname == NULL) {
558 ERR("no output file specified");
562 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
564 ERR("invalid firmware version '%s'", fw_ver);
571 static void fill_header(char *buf, int len)
573 struct fw_header *hdr = (struct fw_header *)buf;
575 memset(hdr, 0, sizeof(struct fw_header));
577 hdr->version = htonl(HEADER_VERSION_V1);
578 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
579 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
580 hdr->hw_id = htonl(hw_id);
581 hdr->hw_rev = htonl(hw_rev);
583 if (boot_info.file_size == 0)
584 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
586 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
588 hdr->kernel_la = htonl(kernel_la);
589 hdr->kernel_ep = htonl(kernel_ep);
590 hdr->fw_length = htonl(layout->fw_max_len);
591 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
592 hdr->kernel_len = htonl(kernel_len);
594 hdr->rootfs_ofs = htonl(rootfs_ofs);
595 hdr->rootfs_len = htonl(rootfs_info.file_size);
598 hdr->ver_hi = htons(fw_ver_hi);
599 hdr->ver_mid = htons(fw_ver_mid);
600 hdr->ver_lo = htons(fw_ver_lo);
602 get_md5(buf, len, hdr->md5sum1);
605 static int pad_jffs2(char *buf, int currlen)
611 pad_mask = (64 * 1024);
612 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
616 for (i = 10; i < 32; i++) {
622 len = ALIGN(len, mask);
624 for (i = 10; i < 32; i++) {
626 if ((len & (mask - 1)) == 0)
630 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
631 buf[len + i] = jffs2_eof_mark[i];
633 len += sizeof(jffs2_eof_mark);
639 static int write_fw(char *data, int len)
642 int ret = EXIT_FAILURE;
644 f = fopen(ofname, "w");
646 ERRS("could not open \"%s\" for writing", ofname);
651 fwrite(data, len, 1, f);
653 ERRS("unable to write output file");
657 DBG("firmware file \"%s\" completed", ofname);
664 if (ret != EXIT_SUCCESS) {
671 static int build_fw(void)
676 int ret = EXIT_FAILURE;
679 buflen = layout->fw_max_len;
681 buf = malloc(buflen);
683 ERR("no memory for buffer\n");
687 memset(buf, 0xff, buflen);
688 p = buf + sizeof(struct fw_header);
689 ret = read_to_buf(&kernel_info, p);
693 writelen = sizeof(struct fw_header) + kernel_len;
699 p = buf + rootfs_ofs;
701 ret = read_to_buf(&rootfs_info, p);
706 writelen += rootfs_info.file_size;
708 writelen = rootfs_ofs + rootfs_info.file_size;
711 writelen = pad_jffs2(buf, writelen);
717 fill_header(buf, writelen);
718 ret = write_fw(buf, writelen);
730 /* Helper functions to inspect_fw() representing different output formats */
731 static inline void inspect_fw_pstr(char *label, char *str)
733 printf("%-23s: %s\n", label, str);
736 static inline void inspect_fw_phex(char *label, uint32_t val)
738 printf("%-23s: 0x%08x\n", label, val);
741 static inline void inspect_fw_phexpost(char *label,
742 uint32_t val, char *post)
744 printf("%-23s: 0x%08x (%s)\n", label, val, post);
747 static inline void inspect_fw_phexdef(char *label,
748 uint32_t val, uint32_t defval)
750 printf("%-23s: 0x%08x ", label, val);
753 printf("(== OpenWrt default)\n");
755 printf("(OpenWrt default: 0x%08x)\n", defval);
758 static inline void inspect_fw_phexexp(char *label,
759 uint32_t val, uint32_t expval)
761 printf("%-23s: 0x%08x ", label, val);
766 printf("(expected: 0x%08x)\n", expval);
769 static inline void inspect_fw_phexdec(char *label, uint32_t val)
771 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
774 static inline void inspect_fw_phexdecdef(char *label,
775 uint32_t val, uint32_t defval)
777 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
780 printf("(== OpenWrt default)\n");
782 printf("(OpenWrt default: 0x%08x)\n", defval);
785 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
789 printf("%-23s:", label);
790 for (i=0; i<MD5SUM_LEN; i++)
791 printf(" %02x", val[i]);
792 printf(" %s\n", text);
795 static int inspect_fw(void)
798 struct fw_header *hdr;
799 uint8_t md5sum[MD5SUM_LEN];
800 struct board_info *board;
801 int ret = EXIT_FAILURE;
803 buf = malloc(inspect_info.file_size);
805 ERR("no memory for buffer!\n");
809 ret = read_to_buf(&inspect_info, buf);
812 hdr = (struct fw_header *)buf;
814 inspect_fw_pstr("File name", inspect_info.file_name);
815 inspect_fw_phexdec("File size", inspect_info.file_size);
817 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
818 ERR("file does not seem to have V1 header!\n");
822 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
824 if (ntohl(hdr->unk1) != 0)
825 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
827 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
828 if (ntohl(hdr->boot_len) == 0)
829 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
831 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
832 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
834 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
835 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
836 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
838 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
840 if (ntohl(hdr->unk2) != 0)
841 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
842 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
843 "(purpose yet unknown, unchecked here)");
844 if (ntohl(hdr->unk3) != 0)
845 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
849 inspect_fw_pstr("Vendor name", hdr->vendor_name);
850 inspect_fw_pstr("Firmware version", hdr->fw_version);
851 board = find_board_by_hwid(ntohl(hdr->hw_id));
853 layout = find_layout(board->layout_id);
854 inspect_fw_phexpost("Hardware ID",
855 ntohl(hdr->hw_id), board->id);
856 inspect_fw_phexexp("Hardware Revision",
857 ntohl(hdr->hw_rev), board->hw_rev);
859 inspect_fw_phexpost("Hardware ID",
860 ntohl(hdr->hw_id), "unknown");
861 inspect_fw_phex("Hardware Revision",
867 inspect_fw_phexdec("Kernel data offset",
868 ntohl(hdr->kernel_ofs));
869 inspect_fw_phexdec("Kernel data length",
870 ntohl(hdr->kernel_len));
872 inspect_fw_phexdef("Kernel load address",
873 ntohl(hdr->kernel_la),
874 layout ? layout->kernel_la : 0xffffffff);
875 inspect_fw_phexdef("Kernel entry point",
876 ntohl(hdr->kernel_ep),
877 layout ? layout->kernel_ep : 0xffffffff);
878 inspect_fw_phexdecdef("Rootfs data offset",
879 ntohl(hdr->rootfs_ofs),
880 layout ? layout->rootfs_ofs : 0xffffffff);
882 inspect_fw_phex("Kernel load address",
883 ntohl(hdr->kernel_la));
884 inspect_fw_phex("Kernel entry point",
885 ntohl(hdr->kernel_ep));
886 inspect_fw_phexdec("Rootfs data offset",
887 ntohl(hdr->rootfs_ofs));
889 inspect_fw_phexdec("Rootfs data length",
890 ntohl(hdr->rootfs_len));
891 inspect_fw_phexdec("Boot loader data offset",
892 ntohl(hdr->boot_ofs));
893 inspect_fw_phexdec("Boot loader data length",
894 ntohl(hdr->boot_len));
895 inspect_fw_phexdec("Total firmware length",
896 ntohl(hdr->fw_length));
904 filename = malloc(strlen(inspect_info.file_name) + 8);
905 sprintf(filename, "%s-kernel", inspect_info.file_name);
906 printf("Extracting kernel to \"%s\"...\n", filename);
907 fp = fopen(filename, "w");
909 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
910 ntohl(hdr->kernel_len), 1, fp)) {
911 ERR("error in fwrite(): %s", strerror(errno));
915 ERR("error in fopen(): %s", strerror(errno));
919 filename = malloc(strlen(inspect_info.file_name) + 8);
920 sprintf(filename, "%s-rootfs", inspect_info.file_name);
921 printf("Extracting rootfs to \"%s\"...\n", filename);
922 fp = fopen(filename, "w");
924 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
925 ntohl(hdr->rootfs_len), 1, fp)) {
926 ERR("error in fwrite(): %s", strerror(errno));
930 ERR("error in fopen(): %s", strerror(errno));
941 int main(int argc, char *argv[])
943 int ret = EXIT_FAILURE;
948 progname = basename(argv[0]);
953 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
959 sscanf(optarg, "0x%x", &rootfs_align);
968 sscanf(optarg, "0x%x", &kernel_ep);
977 sscanf(optarg, "0x%x", &kernel_la);
992 kernel_info.file_name = optarg;
995 rootfs_info.file_name = optarg;
998 sscanf(optarg, "0x%x", &rootfs_ofs);
1007 inspect_info.file_name = optarg;
1016 usage(EXIT_SUCCESS);
1019 usage(EXIT_FAILURE);
1024 ret = check_options();
1028 if (!inspect_info.file_name)