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_WR841N_V1_5 0x08410002
49 #define HWID_TL_WR841ND_V3 0x08410003
50 #define HWID_TL_WR841ND_V5 0x08410005
51 #define HWID_TL_WR841ND_V7 0x08410007
52 #define HWID_TL_WR941ND_V2 0x09410002
53 #define HWID_TL_WR941ND_V4 0x09410004
54 #define HWID_TL_WR1043ND_V1 0x10430001
55 #define HWID_TL_WR1041N_V2 0x10410002
56 #define HWID_TL_WR2543N_V1 0x25430001
61 char *file_name; /* name of the file */
62 uint32_t file_size; /* length of the file */
66 uint32_t version; /* header version */
69 uint32_t hw_id; /* hardware id */
70 uint32_t hw_rev; /* hardware revision */
72 uint8_t md5sum1[MD5SUM_LEN];
74 uint8_t md5sum2[MD5SUM_LEN];
76 uint32_t kernel_la; /* kernel load address */
77 uint32_t kernel_ep; /* kernel entry point */
78 uint32_t fw_length; /* total length of the firmware */
79 uint32_t kernel_ofs; /* kernel data offset */
80 uint32_t kernel_len; /* kernel data length */
81 uint32_t rootfs_ofs; /* rootfs data offset */
82 uint32_t rootfs_len; /* rootfs data length */
83 uint32_t boot_ofs; /* bootloader data offset */
84 uint32_t boot_len; /* bootloader data length */
89 } __attribute__ ((packed));
110 static char *progname;
111 static char *vendor = "TP-LINK Technologies";
112 static char *version = "ver. 1.0";
113 static char *fw_ver = "0.0.0";
115 static char *board_id;
116 static struct board_info *board;
117 static char *layout_id;
118 static struct flash_layout *layout;
119 static char *opt_hw_id;
120 static uint32_t hw_id;
121 static char *opt_hw_rev;
122 static uint32_t hw_rev;
123 static int fw_ver_lo;
124 static int fw_ver_mid;
125 static int fw_ver_hi;
126 static struct file_info kernel_info;
127 static uint32_t kernel_la = 0;
128 static uint32_t kernel_ep = 0;
129 static uint32_t kernel_len = 0;
130 static struct file_info rootfs_info;
131 static uint32_t rootfs_ofs = 0;
132 static uint32_t rootfs_align;
133 static struct file_info boot_info;
135 static int strip_padding;
136 static int add_jffs2_eof;
137 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
139 static struct file_info inspect_info;
140 static int extract = 0;
142 char md5salt_normal[MD5SUM_LEN] = {
143 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
144 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
147 char md5salt_boot[MD5SUM_LEN] = {
148 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
149 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
152 static struct flash_layout layouts[] = {
155 .fw_max_len = 0x3c0000,
156 .kernel_la = 0x80060000,
157 .kernel_ep = 0x80060000,
158 .rootfs_ofs = 0x140000,
161 .fw_max_len = 0x3c0000,
162 .kernel_la = 0x80060000,
163 .kernel_ep = 0x80060000,
164 .rootfs_ofs = 0x100000,
167 .fw_max_len = 0x7c0000,
168 .kernel_la = 0x80060000,
169 .kernel_ep = 0x80060000,
170 .rootfs_ofs = 0x140000,
173 .fw_max_len = 0x7c0000,
174 .kernel_la = 0x80060000,
175 .kernel_ep = 0x80060000,
176 .rootfs_ofs = 0x100000,
178 /* terminating entry */
182 static struct board_info boards[] = {
185 .hw_id = HWID_TL_MR3020_V1,
187 .layout_id = "4Mlzma",
190 .hw_id = HWID_TL_MR3220_V1,
195 .hw_id = HWID_TL_MR3220_V2,
197 .layout_id = "4Mlzma",
200 .hw_id = HWID_TL_MR3420_V1,
205 .hw_id = HWID_TL_WA701N_V1,
210 .hw_id = HWID_TL_WA7510N_V1,
214 .id = "TL-WA801NDv1",
215 .hw_id = HWID_TL_WA801ND_V1,
219 .id = "TL-WA901NDv1",
220 .hw_id = HWID_TL_WA901ND_V1,
224 .id = "TL-WA901NDv2",
225 .hw_id = HWID_TL_WA901ND_V2,
229 .id = "TL-WR741NDv1",
230 .hw_id = HWID_TL_WR741ND_V1,
234 .id = "TL-WR741NDv4",
235 .hw_id = HWID_TL_WR741ND_V4,
237 .layout_id = "4Mlzma",
240 .hw_id = HWID_TL_WR740N_V1,
245 .hw_id = HWID_TL_WR740N_V3,
249 .id = "TL-WR743NDv1",
250 .hw_id = HWID_TL_WR743ND_V1,
254 .id = "TL-WR841Nv1.5",
255 .hw_id = HWID_TL_WR841N_V1_5,
259 .id = "TL-WR841NDv3",
260 .hw_id = HWID_TL_WR841ND_V3,
264 .id = "TL-WR841NDv5",
265 .hw_id = HWID_TL_WR841ND_V5,
269 .id = "TL-WR841NDv7",
270 .hw_id = HWID_TL_WR841ND_V7,
274 .id = "TL-WR941NDv2",
275 .hw_id = HWID_TL_WR941ND_V2,
279 .id = "TL-WR941NDv4",
280 .hw_id = HWID_TL_WR941ND_V4,
284 .id = "TL-WR1041Nv2",
285 .hw_id = HWID_TL_WR1041N_V2,
287 .layout_id = "4Mlzma",
289 .id = "TL-WR1043NDv1",
290 .hw_id = HWID_TL_WR1043ND_V1,
294 .id = "TL-WR2543Nv1",
295 .hw_id = HWID_TL_WR2543N_V1,
297 .layout_id = "8Mlzma",
300 .hw_id = HWID_TL_WR703N_V1,
302 .layout_id = "4Mlzma",
304 /* terminating entry */
311 #define ERR(fmt, ...) do { \
313 fprintf(stderr, "[%s] *** error: " fmt "\n", \
314 progname, ## __VA_ARGS__ ); \
317 #define ERRS(fmt, ...) do { \
320 fprintf(stderr, "[%s] *** error: " fmt "\n", \
321 progname, ## __VA_ARGS__, strerror(save)); \
324 #define DBG(fmt, ...) do { \
325 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
328 static struct board_info *find_board(char *id)
330 struct board_info *ret;
331 struct board_info *board;
334 for (board = boards; board->id != NULL; board++){
335 if (strcasecmp(id, board->id) == 0) {
344 static struct board_info *find_board_by_hwid(uint32_t hw_id)
346 struct board_info *board;
348 for (board = boards; board->id != NULL; board++) {
349 if (hw_id == board->hw_id)
356 static struct flash_layout *find_layout(char *id)
358 struct flash_layout *ret;
359 struct flash_layout *l;
362 for (l = layouts; l->id != NULL; l++){
363 if (strcasecmp(id, l->id) == 0) {
372 static void usage(int status)
374 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
375 struct board_info *board;
377 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
381 " -B <board> create image for the board specified with <board>\n"
382 " -c use combined kernel image\n"
383 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
384 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
385 " -H <hwid> use hardware id specified with <hwid>\n"
386 " -F <id> use flash layout specified with <id>\n"
387 " -k <file> read kernel image from the file <file>\n"
388 " -r <file> read rootfs image from the file <file>\n"
389 " -a <align> align the rootfs start on an <align> bytes boundary\n"
390 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
391 " -o <file> write output to the file <file>\n"
392 " -s strip padding from the end of the image\n"
393 " -j add jffs2 end-of-filesystem markers\n"
394 " -N <vendor> set image vendor to <vendor>\n"
395 " -V <version> set image version to <version>\n"
396 " -v <version> set firmware version to <version>\n"
397 " -i <file> inspect given firmware file <file>\n"
398 " -x extract kernel and rootfs while inspecting (requires -i)\n"
399 " -h show this screen\n"
405 static int get_md5(char *data, int size, char *md5)
410 MD5_Update(&ctx, data, size);
411 MD5_Final(md5, &ctx);
414 static int get_file_stat(struct file_info *fdata)
419 if (fdata->file_name == NULL)
422 res = stat(fdata->file_name, &st);
424 ERRS("stat failed on %s", fdata->file_name);
428 fdata->file_size = st.st_size;
432 static int read_to_buf(struct file_info *fdata, char *buf)
435 int ret = EXIT_FAILURE;
437 f = fopen(fdata->file_name, "r");
439 ERRS("could not open \"%s\" for reading", fdata->file_name);
444 fread(buf, fdata->file_size, 1, f);
446 ERRS("unable to read from file \"%s\"", fdata->file_name);
458 static int check_options(void)
462 if (inspect_info.file_name) {
463 ret = get_file_stat(&inspect_info);
468 } else if (extract) {
469 ERR("no firmware for inspection specified");
473 if (board_id == NULL && opt_hw_id == NULL) {
474 ERR("either board or hardware id must be specified");
479 board = find_board(board_id);
481 ERR("unknown/unsupported board id \"%s\"", board_id);
484 if (layout_id == NULL)
485 layout_id = board->layout_id;
487 hw_id = board->hw_id;
488 hw_rev = board->hw_rev;
490 if (layout_id == NULL) {
491 ERR("flash layout is not specified");
494 hw_id = strtoul(opt_hw_id, NULL, 0);
497 hw_rev = strtoul(opt_hw_rev, NULL, 0);
502 layout = find_layout(layout_id);
503 if (layout == NULL) {
504 ERR("unknown flash layout \"%s\"", layout_id);
509 kernel_la = layout->kernel_la;
511 kernel_ep = layout->kernel_ep;
513 rootfs_ofs = layout->rootfs_ofs;
515 if (kernel_info.file_name == NULL) {
516 ERR("no kernel image specified");
520 ret = get_file_stat(&kernel_info);
524 kernel_len = kernel_info.file_size;
527 if (kernel_info.file_size >
528 layout->fw_max_len - sizeof(struct fw_header)) {
529 ERR("kernel image is too big");
533 if (rootfs_info.file_name == NULL) {
534 ERR("no rootfs image specified");
538 ret = get_file_stat(&rootfs_info);
543 kernel_len += sizeof(struct fw_header);
544 kernel_len = ALIGN(kernel_len, rootfs_align);
545 kernel_len -= sizeof(struct fw_header);
547 DBG("kernel length aligned to %u", kernel_len);
549 if (kernel_len + rootfs_info.file_size >
550 layout->fw_max_len - sizeof(struct fw_header)) {
551 ERR("images are too big");
555 if (kernel_info.file_size >
556 rootfs_ofs - sizeof(struct fw_header)) {
557 ERR("kernel image is too big");
561 if (rootfs_info.file_size >
562 (layout->fw_max_len - rootfs_ofs)) {
563 ERR("rootfs image is too big");
569 if (ofname == NULL) {
570 ERR("no output file specified");
574 ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
576 ERR("invalid firmware version '%s'", fw_ver);
583 static void fill_header(char *buf, int len)
585 struct fw_header *hdr = (struct fw_header *)buf;
587 memset(hdr, 0, sizeof(struct fw_header));
589 hdr->version = htonl(HEADER_VERSION_V1);
590 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
591 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
592 hdr->hw_id = htonl(hw_id);
593 hdr->hw_rev = htonl(hw_rev);
595 if (boot_info.file_size == 0)
596 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
598 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
600 hdr->kernel_la = htonl(kernel_la);
601 hdr->kernel_ep = htonl(kernel_ep);
602 hdr->fw_length = htonl(layout->fw_max_len);
603 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
604 hdr->kernel_len = htonl(kernel_len);
606 hdr->rootfs_ofs = htonl(rootfs_ofs);
607 hdr->rootfs_len = htonl(rootfs_info.file_size);
610 hdr->ver_hi = htons(fw_ver_hi);
611 hdr->ver_mid = htons(fw_ver_mid);
612 hdr->ver_lo = htons(fw_ver_lo);
614 get_md5(buf, len, hdr->md5sum1);
617 static int pad_jffs2(char *buf, int currlen)
623 pad_mask = (64 * 1024);
624 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
628 for (i = 10; i < 32; i++) {
634 len = ALIGN(len, mask);
636 for (i = 10; i < 32; i++) {
638 if ((len & (mask - 1)) == 0)
642 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
643 buf[len + i] = jffs2_eof_mark[i];
645 len += sizeof(jffs2_eof_mark);
651 static int write_fw(char *data, int len)
654 int ret = EXIT_FAILURE;
656 f = fopen(ofname, "w");
658 ERRS("could not open \"%s\" for writing", ofname);
663 fwrite(data, len, 1, f);
665 ERRS("unable to write output file");
669 DBG("firmware file \"%s\" completed", ofname);
676 if (ret != EXIT_SUCCESS) {
683 static int build_fw(void)
688 int ret = EXIT_FAILURE;
691 buflen = layout->fw_max_len;
693 buf = malloc(buflen);
695 ERR("no memory for buffer\n");
699 memset(buf, 0xff, buflen);
700 p = buf + sizeof(struct fw_header);
701 ret = read_to_buf(&kernel_info, p);
705 writelen = sizeof(struct fw_header) + kernel_len;
711 p = buf + rootfs_ofs;
713 ret = read_to_buf(&rootfs_info, p);
718 writelen += rootfs_info.file_size;
720 writelen = rootfs_ofs + rootfs_info.file_size;
723 writelen = pad_jffs2(buf, writelen);
729 fill_header(buf, writelen);
730 ret = write_fw(buf, writelen);
742 /* Helper functions to inspect_fw() representing different output formats */
743 static inline void inspect_fw_pstr(char *label, char *str)
745 printf("%-23s: %s\n", label, str);
748 static inline void inspect_fw_phex(char *label, uint32_t val)
750 printf("%-23s: 0x%08x\n", label, val);
753 static inline void inspect_fw_phexpost(char *label,
754 uint32_t val, char *post)
756 printf("%-23s: 0x%08x (%s)\n", label, val, post);
759 static inline void inspect_fw_phexdef(char *label,
760 uint32_t val, uint32_t defval)
762 printf("%-23s: 0x%08x ", label, val);
765 printf("(== OpenWrt default)\n");
767 printf("(OpenWrt default: 0x%08x)\n", defval);
770 static inline void inspect_fw_phexexp(char *label,
771 uint32_t val, uint32_t expval)
773 printf("%-23s: 0x%08x ", label, val);
778 printf("(expected: 0x%08x)\n", expval);
781 static inline void inspect_fw_phexdec(char *label, uint32_t val)
783 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
786 static inline void inspect_fw_phexdecdef(char *label,
787 uint32_t val, uint32_t defval)
789 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
792 printf("(== OpenWrt default)\n");
794 printf("(OpenWrt default: 0x%08x)\n", defval);
797 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
801 printf("%-23s:", label);
802 for (i=0; i<MD5SUM_LEN; i++)
803 printf(" %02x", val[i]);
804 printf(" %s\n", text);
807 static int inspect_fw(void)
810 struct fw_header *hdr;
811 uint8_t md5sum[MD5SUM_LEN];
812 struct board_info *board;
813 int ret = EXIT_FAILURE;
815 buf = malloc(inspect_info.file_size);
817 ERR("no memory for buffer!\n");
821 ret = read_to_buf(&inspect_info, buf);
824 hdr = (struct fw_header *)buf;
826 inspect_fw_pstr("File name", inspect_info.file_name);
827 inspect_fw_phexdec("File size", inspect_info.file_size);
829 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
830 ERR("file does not seem to have V1 header!\n");
834 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
836 if (ntohl(hdr->unk1) != 0)
837 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
839 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
840 if (ntohl(hdr->boot_len) == 0)
841 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
843 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
844 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
846 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
847 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
848 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
850 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
852 if (ntohl(hdr->unk2) != 0)
853 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
854 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
855 "(purpose yet unknown, unchecked here)");
856 if (ntohl(hdr->unk3) != 0)
857 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
861 inspect_fw_pstr("Vendor name", hdr->vendor_name);
862 inspect_fw_pstr("Firmware version", hdr->fw_version);
863 board = find_board_by_hwid(ntohl(hdr->hw_id));
865 layout = find_layout(board->layout_id);
866 inspect_fw_phexpost("Hardware ID",
867 ntohl(hdr->hw_id), board->id);
868 inspect_fw_phexexp("Hardware Revision",
869 ntohl(hdr->hw_rev), board->hw_rev);
871 inspect_fw_phexpost("Hardware ID",
872 ntohl(hdr->hw_id), "unknown");
873 inspect_fw_phex("Hardware Revision",
879 inspect_fw_phexdec("Kernel data offset",
880 ntohl(hdr->kernel_ofs));
881 inspect_fw_phexdec("Kernel data length",
882 ntohl(hdr->kernel_len));
884 inspect_fw_phexdef("Kernel load address",
885 ntohl(hdr->kernel_la),
886 layout ? layout->kernel_la : 0xffffffff);
887 inspect_fw_phexdef("Kernel entry point",
888 ntohl(hdr->kernel_ep),
889 layout ? layout->kernel_ep : 0xffffffff);
890 inspect_fw_phexdecdef("Rootfs data offset",
891 ntohl(hdr->rootfs_ofs),
892 layout ? layout->rootfs_ofs : 0xffffffff);
894 inspect_fw_phex("Kernel load address",
895 ntohl(hdr->kernel_la));
896 inspect_fw_phex("Kernel entry point",
897 ntohl(hdr->kernel_ep));
898 inspect_fw_phexdec("Rootfs data offset",
899 ntohl(hdr->rootfs_ofs));
901 inspect_fw_phexdec("Rootfs data length",
902 ntohl(hdr->rootfs_len));
903 inspect_fw_phexdec("Boot loader data offset",
904 ntohl(hdr->boot_ofs));
905 inspect_fw_phexdec("Boot loader data length",
906 ntohl(hdr->boot_len));
907 inspect_fw_phexdec("Total firmware length",
908 ntohl(hdr->fw_length));
916 filename = malloc(strlen(inspect_info.file_name) + 8);
917 sprintf(filename, "%s-kernel", inspect_info.file_name);
918 printf("Extracting kernel to \"%s\"...\n", filename);
919 fp = fopen(filename, "w");
921 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
922 ntohl(hdr->kernel_len), 1, fp)) {
923 ERR("error in fwrite(): %s", strerror(errno));
927 ERR("error in fopen(): %s", strerror(errno));
931 filename = malloc(strlen(inspect_info.file_name) + 8);
932 sprintf(filename, "%s-rootfs", inspect_info.file_name);
933 printf("Extracting rootfs to \"%s\"...\n", filename);
934 fp = fopen(filename, "w");
936 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
937 ntohl(hdr->rootfs_len), 1, fp)) {
938 ERR("error in fwrite(): %s", strerror(errno));
942 ERR("error in fopen(): %s", strerror(errno));
953 int main(int argc, char *argv[])
955 int ret = EXIT_FAILURE;
960 progname = basename(argv[0]);
965 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:");
971 sscanf(optarg, "0x%x", &rootfs_align);
980 sscanf(optarg, "0x%x", &kernel_ep);
989 sscanf(optarg, "0x%x", &kernel_la);
1004 kernel_info.file_name = optarg;
1007 rootfs_info.file_name = optarg;
1010 sscanf(optarg, "0x%x", &rootfs_ofs);
1019 inspect_info.file_name = optarg;
1028 usage(EXIT_SUCCESS);
1031 usage(EXIT_FAILURE);
1036 ret = check_options();
1040 if (!inspect_info.file_name)