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_WA801ND_V1 0x08010001
39 #define HWID_TL_WA901ND_V1 0x09010001
40 #define HWID_TL_WA901ND_V2 0x09010002
41 #define HWID_TL_WR703N_V1 0x07030101
42 #define HWID_TL_WR741ND_V1 0x07410001
43 #define HWID_TL_WR741ND_V4 0x07410004
44 #define HWID_TL_WR740N_V1 0x07400001
45 #define HWID_TL_WR740N_V3 0x07400003
46 #define HWID_TL_WR743ND_V1 0x07430001
47 #define HWID_TL_WR841N_V1_5 0x08410002
48 #define HWID_TL_WR841ND_V3 0x08410003
49 #define HWID_TL_WR841ND_V5 0x08410005
50 #define HWID_TL_WR841ND_V7 0x08410007
51 #define HWID_TL_WR941ND_V2 0x09410002
52 #define HWID_TL_WR941ND_V4 0x09410004
53 #define HWID_TL_WR1043ND_V1 0x10430001
54 #define HWID_TL_WR1041N_V2 0x10410002
55 #define HWID_TL_WR2543N_V1 0x25430001
60 char *file_name; /* name of the file */
61 uint32_t file_size; /* length of the file */
65 uint32_t version; /* header version */
68 uint32_t hw_id; /* hardware id */
69 uint32_t hw_rev; /* hardware revision */
71 uint8_t md5sum1[MD5SUM_LEN];
73 uint8_t md5sum2[MD5SUM_LEN];
75 uint32_t kernel_la; /* kernel load address */
76 uint32_t kernel_ep; /* kernel entry point */
77 uint32_t fw_length; /* total length of the firmware */
78 uint32_t kernel_ofs; /* kernel data offset */
79 uint32_t kernel_len; /* kernel data length */
80 uint32_t rootfs_ofs; /* rootfs data offset */
81 uint32_t rootfs_len; /* rootfs data length */
82 uint32_t boot_ofs; /* bootloader data offset */
83 uint32_t boot_len; /* bootloader data length */
88 } __attribute__ ((packed));
109 static char *progname;
110 static char *vendor = "TP-LINK Technologies";
111 static char *version = "ver. 1.0";
112 static char *fw_ver = "0.0.0";
114 static char *board_id;
115 static struct board_info *board;
116 static char *layout_id;
117 static struct flash_layout *layout;
118 static char *opt_hw_id;
119 static uint32_t hw_id;
120 static char *opt_hw_rev;
121 static uint32_t hw_rev;
122 static int fw_ver_lo;
123 static int fw_ver_mid;
124 static int fw_ver_hi;
125 static struct file_info kernel_info;
126 static uint32_t kernel_la = 0;
127 static uint32_t kernel_ep = 0;
128 static uint32_t kernel_len = 0;
129 static struct file_info rootfs_info;
130 static uint32_t rootfs_ofs = 0;
131 static uint32_t rootfs_align;
132 static struct file_info boot_info;
134 static int strip_padding;
135 static int add_jffs2_eof;
136 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
138 static struct file_info inspect_info;
139 static int extract = 0;
141 char md5salt_normal[MD5SUM_LEN] = {
142 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
143 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
146 char md5salt_boot[MD5SUM_LEN] = {
147 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
148 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
151 static struct flash_layout layouts[] = {
154 .fw_max_len = 0x3c0000,
155 .kernel_la = 0x80060000,
156 .kernel_ep = 0x80060000,
157 .rootfs_ofs = 0x140000,
160 .fw_max_len = 0x3c0000,
161 .kernel_la = 0x80060000,
162 .kernel_ep = 0x80060000,
163 .rootfs_ofs = 0x100000,
166 .fw_max_len = 0x7c0000,
167 .kernel_la = 0x80060000,
168 .kernel_ep = 0x80060000,
169 .rootfs_ofs = 0x140000,
172 .fw_max_len = 0x7c0000,
173 .kernel_la = 0x80060000,
174 .kernel_ep = 0x80060000,
175 .rootfs_ofs = 0x100000,
177 /* terminating entry */
181 static struct board_info boards[] = {
184 .hw_id = HWID_TL_MR3020_V1,
186 .layout_id = "4Mlzma",
189 .hw_id = HWID_TL_MR3220_V1,
194 .hw_id = HWID_TL_MR3420_V1,
199 .hw_id = HWID_TL_WA701N_V1,
204 .hw_id = HWID_TL_WA7510N_V1,
208 .id = "TL-WA801NDv1",
209 .hw_id = HWID_TL_WA801ND_V1,
213 .id = "TL-WA901NDv1",
214 .hw_id = HWID_TL_WA901ND_V1,
218 .id = "TL-WA901NDv2",
219 .hw_id = HWID_TL_WA901ND_V2,
223 .id = "TL-WR741NDv1",
224 .hw_id = HWID_TL_WR741ND_V1,
228 .id = "TL-WR741NDv4",
229 .hw_id = HWID_TL_WR741ND_V4,
231 .layout_id = "4Mlzma",
234 .hw_id = HWID_TL_WR740N_V1,
239 .hw_id = HWID_TL_WR740N_V3,
243 .id = "TL-WR743NDv1",
244 .hw_id = HWID_TL_WR743ND_V1,
248 .id = "TL-WR841Nv1.5",
249 .hw_id = HWID_TL_WR841N_V1_5,
253 .id = "TL-WR841NDv3",
254 .hw_id = HWID_TL_WR841ND_V3,
258 .id = "TL-WR841NDv5",
259 .hw_id = HWID_TL_WR841ND_V5,
263 .id = "TL-WR841NDv7",
264 .hw_id = HWID_TL_WR841ND_V7,
268 .id = "TL-WR941NDv2",
269 .hw_id = HWID_TL_WR941ND_V2,
273 .id = "TL-WR941NDv4",
274 .hw_id = HWID_TL_WR941ND_V4,
278 .id = "TL-WR1041Nv2",
279 .hw_id = HWID_TL_WR1041N_V2,
281 .layout_id = "4Mlzma",
283 .id = "TL-WR1043NDv1",
284 .hw_id = HWID_TL_WR1043ND_V1,
288 .id = "TL-WR2543Nv1",
289 .hw_id = HWID_TL_WR2543N_V1,
291 .layout_id = "8Mlzma",
294 .hw_id = HWID_TL_WR703N_V1,
296 .layout_id = "4Mlzma",
298 /* terminating entry */
305 #define ERR(fmt, ...) do { \
307 fprintf(stderr, "[%s] *** error: " fmt "\n", \
308 progname, ## __VA_ARGS__ ); \
311 #define ERRS(fmt, ...) do { \
314 fprintf(stderr, "[%s] *** error: " fmt "\n", \
315 progname, ## __VA_ARGS__, strerror(save)); \
318 #define DBG(fmt, ...) do { \
319 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
322 static struct board_info *find_board(char *id)
324 struct board_info *ret;
325 struct board_info *board;
328 for (board = boards; board->id != NULL; board++){
329 if (strcasecmp(id, board->id) == 0) {
338 static struct board_info *find_board_by_hwid(uint32_t hw_id)
340 struct board_info *board;
342 for (board = boards; board->id != NULL; board++) {
343 if (hw_id == board->hw_id)
350 static struct flash_layout *find_layout(char *id)
352 struct flash_layout *ret;
353 struct flash_layout *l;
356 for (l = layouts; l->id != NULL; l++){
357 if (strcasecmp(id, l->id) == 0) {
366 static void usage(int status)
368 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
369 struct board_info *board;
371 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
375 " -B <board> create image for the board specified with <board>\n"
376 " -c use combined kernel image\n"
377 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
378 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
379 " -H <hwid> use hardware id specified with <hwid>\n"
380 " -F <id> use flash layout specified with <id>\n"
381 " -k <file> read kernel image from the file <file>\n"
382 " -r <file> read rootfs image from the file <file>\n"
383 " -a <align> align the rootfs start on an <align> bytes boundary\n"
384 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
385 " -o <file> write output to the file <file>\n"
386 " -s strip padding from the end of the image\n"
387 " -j add jffs2 end-of-filesystem markers\n"
388 " -N <vendor> set image vendor to <vendor>\n"
389 " -V <version> set image version to <version>\n"
390 " -v <version> set firmware version to <version>\n"
391 " -i <file> inspect given firmware file <file>\n"
392 " -x extract kernel and rootfs while inspecting (requires -i)\n"
393 " -h show this screen\n"
399 static int get_md5(char *data, int size, char *md5)
404 MD5_Update(&ctx, data, size);
405 MD5_Final(md5, &ctx);
408 static int get_file_stat(struct file_info *fdata)
413 if (fdata->file_name == NULL)
416 res = stat(fdata->file_name, &st);
418 ERRS("stat failed on %s", fdata->file_name);
422 fdata->file_size = st.st_size;
426 static int read_to_buf(struct file_info *fdata, char *buf)
429 int ret = EXIT_FAILURE;
431 f = fopen(fdata->file_name, "r");
433 ERRS("could not open \"%s\" for reading", fdata->file_name);
438 fread(buf, fdata->file_size, 1, f);
440 ERRS("unable to read from file \"%s\"", fdata->file_name);
452 static int check_options(void)
456 if (inspect_info.file_name) {
457 ret = get_file_stat(&inspect_info);
462 } else if (extract) {
463 ERR("no firmware for inspection specified");
467 if (board_id == NULL && opt_hw_id == NULL) {
468 ERR("either board or hardware id must be specified");
473 board = find_board(board_id);
475 ERR("unknown/unsupported board id \"%s\"", board_id);
478 if (layout_id == NULL)
479 layout_id = board->layout_id;
481 hw_id = board->hw_id;
482 hw_rev = board->hw_rev;
484 if (layout_id == NULL) {
485 ERR("flash layout is not specified");
488 hw_id = strtoul(opt_hw_id, NULL, 0);
491 hw_rev = strtoul(opt_hw_rev, NULL, 0);
496 layout = find_layout(layout_id);
497 if (layout == NULL) {
498 ERR("unknown flash layout \"%s\"", layout_id);
503 kernel_la = layout->kernel_la;
505 kernel_ep = layout->kernel_ep;
507 rootfs_ofs = layout->rootfs_ofs;
509 if (kernel_info.file_name == NULL) {
510 ERR("no kernel image specified");
514 ret = get_file_stat(&kernel_info);
518 kernel_len = kernel_info.file_size;
521 if (kernel_info.file_size >
522 layout->fw_max_len - sizeof(struct fw_header)) {
523 ERR("kernel image is too big");
527 if (rootfs_info.file_name == NULL) {
528 ERR("no rootfs image specified");
532 ret = get_file_stat(&rootfs_info);
537 kernel_len += sizeof(struct fw_header);
538 kernel_len = ALIGN(kernel_len, rootfs_align);
539 kernel_len -= sizeof(struct fw_header);
541 DBG("kernel length aligned to %u", kernel_len);
543 if (kernel_len + rootfs_info.file_size >
544 layout->fw_max_len - sizeof(struct fw_header)) {
545 ERR("images are too big");
549 if (kernel_info.file_size >
550 rootfs_ofs - sizeof(struct fw_header)) {
551 ERR("kernel image is too big");
555 if (rootfs_info.file_size >
556 (layout->fw_max_len - rootfs_ofs)) {
557 ERR("rootfs image is too big");
563 if (ofname == NULL) {
564 ERR("no output file specified");
568 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
570 ERR("invalid firmware version '%s'", fw_ver);
577 static void fill_header(char *buf, int len)
579 struct fw_header *hdr = (struct fw_header *)buf;
581 memset(hdr, 0, sizeof(struct fw_header));
583 hdr->version = htonl(HEADER_VERSION_V1);
584 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
585 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
586 hdr->hw_id = htonl(hw_id);
587 hdr->hw_rev = htonl(hw_rev);
589 if (boot_info.file_size == 0)
590 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
592 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
594 hdr->kernel_la = htonl(kernel_la);
595 hdr->kernel_ep = htonl(kernel_ep);
596 hdr->fw_length = htonl(layout->fw_max_len);
597 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
598 hdr->kernel_len = htonl(kernel_len);
600 hdr->rootfs_ofs = htonl(rootfs_ofs);
601 hdr->rootfs_len = htonl(rootfs_info.file_size);
604 hdr->ver_hi = htons(fw_ver_hi);
605 hdr->ver_mid = htons(fw_ver_mid);
606 hdr->ver_lo = htons(fw_ver_lo);
608 get_md5(buf, len, hdr->md5sum1);
611 static int pad_jffs2(char *buf, int currlen)
617 pad_mask = (64 * 1024);
618 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
622 for (i = 10; i < 32; i++) {
628 len = ALIGN(len, mask);
630 for (i = 10; i < 32; i++) {
632 if ((len & (mask - 1)) == 0)
636 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
637 buf[len + i] = jffs2_eof_mark[i];
639 len += sizeof(jffs2_eof_mark);
645 static int write_fw(char *data, int len)
648 int ret = EXIT_FAILURE;
650 f = fopen(ofname, "w");
652 ERRS("could not open \"%s\" for writing", ofname);
657 fwrite(data, len, 1, f);
659 ERRS("unable to write output file");
663 DBG("firmware file \"%s\" completed", ofname);
670 if (ret != EXIT_SUCCESS) {
677 static int build_fw(void)
682 int ret = EXIT_FAILURE;
685 buflen = layout->fw_max_len;
687 buf = malloc(buflen);
689 ERR("no memory for buffer\n");
693 memset(buf, 0xff, buflen);
694 p = buf + sizeof(struct fw_header);
695 ret = read_to_buf(&kernel_info, p);
699 writelen = sizeof(struct fw_header) + kernel_len;
705 p = buf + rootfs_ofs;
707 ret = read_to_buf(&rootfs_info, p);
712 writelen += rootfs_info.file_size;
714 writelen = rootfs_ofs + rootfs_info.file_size;
717 writelen = pad_jffs2(buf, writelen);
723 fill_header(buf, writelen);
724 ret = write_fw(buf, writelen);
736 /* Helper functions to inspect_fw() representing different output formats */
737 static inline void inspect_fw_pstr(char *label, char *str)
739 printf("%-23s: %s\n", label, str);
742 static inline void inspect_fw_phex(char *label, uint32_t val)
744 printf("%-23s: 0x%08x\n", label, val);
747 static inline void inspect_fw_phexpost(char *label,
748 uint32_t val, char *post)
750 printf("%-23s: 0x%08x (%s)\n", label, val, post);
753 static inline void inspect_fw_phexdef(char *label,
754 uint32_t val, uint32_t defval)
756 printf("%-23s: 0x%08x ", label, val);
759 printf("(== OpenWrt default)\n");
761 printf("(OpenWrt default: 0x%08x)\n", defval);
764 static inline void inspect_fw_phexexp(char *label,
765 uint32_t val, uint32_t expval)
767 printf("%-23s: 0x%08x ", label, val);
772 printf("(expected: 0x%08x)\n", expval);
775 static inline void inspect_fw_phexdec(char *label, uint32_t val)
777 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
780 static inline void inspect_fw_phexdecdef(char *label,
781 uint32_t val, uint32_t defval)
783 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
786 printf("(== OpenWrt default)\n");
788 printf("(OpenWrt default: 0x%08x)\n", defval);
791 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
795 printf("%-23s:", label);
796 for (i=0; i<MD5SUM_LEN; i++)
797 printf(" %02x", val[i]);
798 printf(" %s\n", text);
801 static int inspect_fw(void)
804 struct fw_header *hdr;
805 uint8_t md5sum[MD5SUM_LEN];
806 struct board_info *board;
807 int ret = EXIT_FAILURE;
809 buf = malloc(inspect_info.file_size);
811 ERR("no memory for buffer!\n");
815 ret = read_to_buf(&inspect_info, buf);
818 hdr = (struct fw_header *)buf;
820 inspect_fw_pstr("File name", inspect_info.file_name);
821 inspect_fw_phexdec("File size", inspect_info.file_size);
823 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
824 ERR("file does not seem to have V1 header!\n");
828 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
830 if (ntohl(hdr->unk1) != 0)
831 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
833 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
834 if (ntohl(hdr->boot_len) == 0)
835 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
837 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
838 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
840 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
841 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
842 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
844 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
846 if (ntohl(hdr->unk2) != 0)
847 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
848 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
849 "(purpose yet unknown, unchecked here)");
850 if (ntohl(hdr->unk3) != 0)
851 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
855 inspect_fw_pstr("Vendor name", hdr->vendor_name);
856 inspect_fw_pstr("Firmware version", hdr->fw_version);
857 board = find_board_by_hwid(ntohl(hdr->hw_id));
859 layout = find_layout(board->layout_id);
860 inspect_fw_phexpost("Hardware ID",
861 ntohl(hdr->hw_id), board->id);
862 inspect_fw_phexexp("Hardware Revision",
863 ntohl(hdr->hw_rev), board->hw_rev);
865 inspect_fw_phexpost("Hardware ID",
866 ntohl(hdr->hw_id), "unknown");
867 inspect_fw_phex("Hardware Revision",
873 inspect_fw_phexdec("Kernel data offset",
874 ntohl(hdr->kernel_ofs));
875 inspect_fw_phexdec("Kernel data length",
876 ntohl(hdr->kernel_len));
878 inspect_fw_phexdef("Kernel load address",
879 ntohl(hdr->kernel_la),
880 layout ? layout->kernel_la : 0xffffffff);
881 inspect_fw_phexdef("Kernel entry point",
882 ntohl(hdr->kernel_ep),
883 layout ? layout->kernel_ep : 0xffffffff);
884 inspect_fw_phexdecdef("Rootfs data offset",
885 ntohl(hdr->rootfs_ofs),
886 layout ? layout->rootfs_ofs : 0xffffffff);
888 inspect_fw_phex("Kernel load address",
889 ntohl(hdr->kernel_la));
890 inspect_fw_phex("Kernel entry point",
891 ntohl(hdr->kernel_ep));
892 inspect_fw_phexdec("Rootfs data offset",
893 ntohl(hdr->rootfs_ofs));
895 inspect_fw_phexdec("Rootfs data length",
896 ntohl(hdr->rootfs_len));
897 inspect_fw_phexdec("Boot loader data offset",
898 ntohl(hdr->boot_ofs));
899 inspect_fw_phexdec("Boot loader data length",
900 ntohl(hdr->boot_len));
901 inspect_fw_phexdec("Total firmware length",
902 ntohl(hdr->fw_length));
910 filename = malloc(strlen(inspect_info.file_name) + 8);
911 sprintf(filename, "%s-kernel", inspect_info.file_name);
912 printf("Extracting kernel to \"%s\"...\n", filename);
913 fp = fopen(filename, "w");
915 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
916 ntohl(hdr->kernel_len), 1, fp)) {
917 ERR("error in fwrite(): %s", strerror(errno));
921 ERR("error in fopen(): %s", strerror(errno));
925 filename = malloc(strlen(inspect_info.file_name) + 8);
926 sprintf(filename, "%s-rootfs", inspect_info.file_name);
927 printf("Extracting rootfs to \"%s\"...\n", filename);
928 fp = fopen(filename, "w");
930 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
931 ntohl(hdr->rootfs_len), 1, fp)) {
932 ERR("error in fwrite(): %s", strerror(errno));
936 ERR("error in fopen(): %s", strerror(errno));
947 int main(int argc, char *argv[])
949 int ret = EXIT_FAILURE;
954 progname = basename(argv[0]);
959 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
965 sscanf(optarg, "0x%x", &rootfs_align);
974 sscanf(optarg, "0x%x", &kernel_ep);
983 sscanf(optarg, "0x%x", &kernel_la);
998 kernel_info.file_name = optarg;
1001 rootfs_info.file_name = optarg;
1004 sscanf(optarg, "0x%x", &rootfs_ofs);
1013 inspect_info.file_name = optarg;
1022 usage(EXIT_SUCCESS);
1025 usage(EXIT_FAILURE);
1030 ret = check_options();
1034 if (!inspect_info.file_name)