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_WA901ND_V1 0x09010001
38 #define HWID_TL_WA901ND_V2 0x09010002
39 #define HWID_TL_WR703N_V1 0x07030101
40 #define HWID_TL_WR741ND_V1 0x07410001
41 #define HWID_TL_WR741ND_V4 0x07410004
42 #define HWID_TL_WR740N_V1 0x07400001
43 #define HWID_TL_WR740N_V3 0x07400003
44 #define HWID_TL_WR743ND_V1 0x07430001
45 #define HWID_TL_WR841N_V1_5 0x08410002
46 #define HWID_TL_WR841ND_V3 0x08410003
47 #define HWID_TL_WR841ND_V5 0x08410005
48 #define HWID_TL_WR841ND_V7 0x08410007
49 #define HWID_TL_WR941ND_V2 0x09410002
50 #define HWID_TL_WR941ND_V4 0x09410004
51 #define HWID_TL_WR1043ND_V1 0x10430001
52 #define HWID_TL_WR1041N_V2 0x10410002
53 #define HWID_TL_WR2543N_V1 0x25430001
58 char *file_name; /* name of the file */
59 uint32_t file_size; /* length of the file */
63 uint32_t version; /* header version */
66 uint32_t hw_id; /* hardware id */
67 uint32_t hw_rev; /* hardware revision */
69 uint8_t md5sum1[MD5SUM_LEN];
71 uint8_t md5sum2[MD5SUM_LEN];
73 uint32_t kernel_la; /* kernel load address */
74 uint32_t kernel_ep; /* kernel entry point */
75 uint32_t fw_length; /* total length of the firmware */
76 uint32_t kernel_ofs; /* kernel data offset */
77 uint32_t kernel_len; /* kernel data length */
78 uint32_t rootfs_ofs; /* rootfs data offset */
79 uint32_t rootfs_len; /* rootfs data length */
80 uint32_t boot_ofs; /* bootloader data offset */
81 uint32_t boot_len; /* bootloader data length */
83 } __attribute__ ((packed));
104 static char *progname;
105 static char *vendor = "TP-LINK Technologies";
106 static char *version = "ver. 1.0";
108 static char *board_id;
109 static struct board_info *board;
110 static char *layout_id;
111 static struct flash_layout *layout;
112 static char *opt_hw_id;
113 static uint32_t hw_id;
114 static char *opt_hw_rev;
115 static uint32_t hw_rev;
116 static struct file_info kernel_info;
117 static uint32_t kernel_la = 0;
118 static uint32_t kernel_ep = 0;
119 static uint32_t kernel_len = 0;
120 static struct file_info rootfs_info;
121 static uint32_t rootfs_ofs = 0;
122 static uint32_t rootfs_align;
123 static struct file_info boot_info;
125 static int strip_padding;
126 static int add_jffs2_eof;
127 static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
129 static struct file_info inspect_info;
130 static int extract = 0;
132 char md5salt_normal[MD5SUM_LEN] = {
133 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
134 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
137 char md5salt_boot[MD5SUM_LEN] = {
138 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
139 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
142 static struct flash_layout layouts[] = {
145 .fw_max_len = 0x3c0000,
146 .kernel_la = 0x80060000,
147 .kernel_ep = 0x80060000,
148 .rootfs_ofs = 0x140000,
151 .fw_max_len = 0x3c0000,
152 .kernel_la = 0x80060000,
153 .kernel_ep = 0x80060000,
154 .rootfs_ofs = 0x100000,
157 .fw_max_len = 0x7c0000,
158 .kernel_la = 0x80060000,
159 .kernel_ep = 0x80060000,
160 .rootfs_ofs = 0x140000,
163 .fw_max_len = 0x7c0000,
164 .kernel_la = 0x80060000,
165 .kernel_ep = 0x80060000,
166 .rootfs_ofs = 0x100000,
168 /* terminating entry */
172 static struct board_info boards[] = {
175 .hw_id = HWID_TL_MR3020_V1,
177 .layout_id = "4Mlzma",
180 .hw_id = HWID_TL_MR3220_V1,
185 .hw_id = HWID_TL_MR3420_V1,
190 .hw_id = HWID_TL_WA701N_V1,
194 .id = "TL-WA901NDv1",
195 .hw_id = HWID_TL_WA901ND_V1,
199 .id = "TL-WA901NDv2",
200 .hw_id = HWID_TL_WA901ND_V2,
204 .id = "TL-WR741NDv1",
205 .hw_id = HWID_TL_WR741ND_V1,
209 .id = "TL-WR741NDv4",
210 .hw_id = HWID_TL_WR741ND_V4,
212 .layout_id = "4Mlzma",
215 .hw_id = HWID_TL_WR740N_V1,
220 .hw_id = HWID_TL_WR740N_V3,
224 .id = "TL-WR743NDv1",
225 .hw_id = HWID_TL_WR743ND_V1,
229 .id = "TL-WR841Nv1.5",
230 .hw_id = HWID_TL_WR841N_V1_5,
234 .id = "TL-WR841NDv3",
235 .hw_id = HWID_TL_WR841ND_V3,
239 .id = "TL-WR841NDv5",
240 .hw_id = HWID_TL_WR841ND_V5,
244 .id = "TL-WR841NDv7",
245 .hw_id = HWID_TL_WR841ND_V7,
249 .id = "TL-WR941NDv2",
250 .hw_id = HWID_TL_WR941ND_V2,
254 .id = "TL-WR941NDv4",
255 .hw_id = HWID_TL_WR941ND_V4,
259 .id = "TL-WR1041Nv2",
260 .hw_id = HWID_TL_WR1041N_V2,
262 .layout_id = "4Mlzma",
264 .id = "TL-WR1043NDv1",
265 .hw_id = HWID_TL_WR1043ND_V1,
269 .id = "TL-WR2543Nv1",
270 .hw_id = HWID_TL_WR2543N_V1,
272 .layout_id = "8Mlzma",
275 .hw_id = HWID_TL_WR703N_V1,
277 .layout_id = "4Mlzma",
279 /* terminating entry */
286 #define ERR(fmt, ...) do { \
288 fprintf(stderr, "[%s] *** error: " fmt "\n", \
289 progname, ## __VA_ARGS__ ); \
292 #define ERRS(fmt, ...) do { \
295 fprintf(stderr, "[%s] *** error: " fmt "\n", \
296 progname, ## __VA_ARGS__, strerror(save)); \
299 #define DBG(fmt, ...) do { \
300 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
303 static struct board_info *find_board(char *id)
305 struct board_info *ret;
306 struct board_info *board;
309 for (board = boards; board->id != NULL; board++){
310 if (strcasecmp(id, board->id) == 0) {
319 static struct board_info *find_board_by_hwid(uint32_t hw_id)
321 struct board_info *board;
323 for (board = boards; board->id != NULL; board++) {
324 if (hw_id == board->hw_id)
331 static struct flash_layout *find_layout(char *id)
333 struct flash_layout *ret;
334 struct flash_layout *l;
337 for (l = layouts; l->id != NULL; l++){
338 if (strcasecmp(id, l->id) == 0) {
347 static void usage(int status)
349 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
350 struct board_info *board;
352 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
356 " -B <board> create image for the board specified with <board>\n"
357 " -c use combined kernel image\n"
358 " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
359 " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
360 " -H <hwid> use hardware id specified with <hwid>\n"
361 " -F <id> use flash layout specified with <id>\n"
362 " -k <file> read kernel image from the file <file>\n"
363 " -r <file> read rootfs image from the file <file>\n"
364 " -a <align> align the rootfs start on an <align> bytes boundary\n"
365 " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
366 " -o <file> write output to the file <file>\n"
367 " -s strip padding from the end of the image\n"
368 " -j add jffs2 end-of-filesystem markers\n"
369 " -N <vendor> set image vendor to <vendor>\n"
370 " -V <version> set image version to <version>\n"
371 " -i <file> inspect given firmware file <file>\n"
372 " -x extract kernel and rootfs while inspecting (requires -i)\n"
373 " -h show this screen\n"
379 static int get_md5(char *data, int size, char *md5)
384 MD5_Update(&ctx, data, size);
385 MD5_Final(md5, &ctx);
388 static int get_file_stat(struct file_info *fdata)
393 if (fdata->file_name == NULL)
396 res = stat(fdata->file_name, &st);
398 ERRS("stat failed on %s", fdata->file_name);
402 fdata->file_size = st.st_size;
406 static int read_to_buf(struct file_info *fdata, char *buf)
409 int ret = EXIT_FAILURE;
411 f = fopen(fdata->file_name, "r");
413 ERRS("could not open \"%s\" for reading", fdata->file_name);
418 fread(buf, fdata->file_size, 1, f);
420 ERRS("unable to read from file \"%s\"", fdata->file_name);
432 static int check_options(void)
436 if (inspect_info.file_name) {
437 ret = get_file_stat(&inspect_info);
442 } else if (extract) {
443 ERR("no firmware for inspection specified");
447 if (board_id == NULL && opt_hw_id == NULL) {
448 ERR("either board or hardware id must be specified");
453 board = find_board(board_id);
455 ERR("unknown/unsupported board id \"%s\"", board_id);
458 if (layout_id == NULL)
459 layout_id = board->layout_id;
461 hw_id = board->hw_id;
462 hw_rev = board->hw_rev;
464 if (layout_id == NULL) {
465 ERR("flash layout is not specified");
468 hw_id = strtoul(opt_hw_id, NULL, 0);
471 hw_rev = strtoul(opt_hw_rev, NULL, 0);
476 layout = find_layout(layout_id);
477 if (layout == NULL) {
478 ERR("unknown flash layout \"%s\"", layout_id);
483 kernel_la = layout->kernel_la;
485 kernel_ep = layout->kernel_ep;
487 rootfs_ofs = layout->rootfs_ofs;
489 if (kernel_info.file_name == NULL) {
490 ERR("no kernel image specified");
494 ret = get_file_stat(&kernel_info);
498 kernel_len = kernel_info.file_size;
501 if (kernel_info.file_size >
502 layout->fw_max_len - sizeof(struct fw_header)) {
503 ERR("kernel image is too big");
507 if (rootfs_info.file_name == NULL) {
508 ERR("no rootfs image specified");
512 ret = get_file_stat(&rootfs_info);
517 kernel_len += sizeof(struct fw_header);
518 kernel_len = ALIGN(kernel_len, rootfs_align);
519 kernel_len -= sizeof(struct fw_header);
521 DBG("kernel length aligned to %u", kernel_len);
523 if (kernel_len + rootfs_info.file_size >
524 layout->fw_max_len - sizeof(struct fw_header)) {
525 ERR("images are too big");
529 if (kernel_info.file_size >
530 rootfs_ofs - sizeof(struct fw_header)) {
531 ERR("kernel image is too big");
535 if (rootfs_info.file_size >
536 (layout->fw_max_len - rootfs_ofs)) {
537 ERR("rootfs image is too big");
543 if (ofname == NULL) {
544 ERR("no output file specified");
551 static void fill_header(char *buf, int len)
553 struct fw_header *hdr = (struct fw_header *)buf;
555 memset(hdr, 0, sizeof(struct fw_header));
557 hdr->version = htonl(HEADER_VERSION_V1);
558 strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
559 strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
560 hdr->hw_id = htonl(hw_id);
561 hdr->hw_rev = htonl(hw_rev);
563 if (boot_info.file_size == 0)
564 memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
566 memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
568 hdr->kernel_la = htonl(kernel_la);
569 hdr->kernel_ep = htonl(kernel_ep);
570 hdr->fw_length = htonl(layout->fw_max_len);
571 hdr->kernel_ofs = htonl(sizeof(struct fw_header));
572 hdr->kernel_len = htonl(kernel_len);
574 hdr->rootfs_ofs = htonl(rootfs_ofs);
575 hdr->rootfs_len = htonl(rootfs_info.file_size);
578 get_md5(buf, len, hdr->md5sum1);
581 static int pad_jffs2(char *buf, int currlen)
587 pad_mask = (64 * 1024);
588 while ((len < layout->fw_max_len) && (pad_mask != 0)) {
592 for (i = 10; i < 32; i++) {
598 len = ALIGN(len, mask);
600 for (i = 10; i < 32; i++) {
602 if ((len & (mask - 1)) == 0)
606 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
607 buf[len + i] = jffs2_eof_mark[i];
609 len += sizeof(jffs2_eof_mark);
615 static int write_fw(char *data, int len)
618 int ret = EXIT_FAILURE;
620 f = fopen(ofname, "w");
622 ERRS("could not open \"%s\" for writing", ofname);
627 fwrite(data, len, 1, f);
629 ERRS("unable to write output file");
633 DBG("firmware file \"%s\" completed", ofname);
640 if (ret != EXIT_SUCCESS) {
647 static int build_fw(void)
652 int ret = EXIT_FAILURE;
655 buflen = layout->fw_max_len;
657 buf = malloc(buflen);
659 ERR("no memory for buffer\n");
663 memset(buf, 0xff, buflen);
664 p = buf + sizeof(struct fw_header);
665 ret = read_to_buf(&kernel_info, p);
669 writelen = sizeof(struct fw_header) + kernel_len;
675 p = buf + rootfs_ofs;
677 ret = read_to_buf(&rootfs_info, p);
682 writelen += rootfs_info.file_size;
684 writelen = rootfs_ofs + rootfs_info.file_size;
687 writelen = pad_jffs2(buf, writelen);
693 fill_header(buf, writelen);
694 ret = write_fw(buf, writelen);
706 /* Helper functions to inspect_fw() representing different output formats */
707 static inline void inspect_fw_pstr(char *label, char *str)
709 printf("%-23s: %s\n", label, str);
712 static inline void inspect_fw_phex(char *label, uint32_t val)
714 printf("%-23s: 0x%08x\n", label, val);
717 static inline void inspect_fw_phexpost(char *label,
718 uint32_t val, char *post)
720 printf("%-23s: 0x%08x (%s)\n", label, val, post);
723 static inline void inspect_fw_phexdef(char *label,
724 uint32_t val, uint32_t defval)
726 printf("%-23s: 0x%08x ", label, val);
729 printf("(== OpenWrt default)\n");
731 printf("(OpenWrt default: 0x%08x)\n", defval);
734 static inline void inspect_fw_phexexp(char *label,
735 uint32_t val, uint32_t expval)
737 printf("%-23s: 0x%08x ", label, val);
742 printf("(expected: 0x%08x)\n", expval);
745 static inline void inspect_fw_phexdec(char *label, uint32_t val)
747 printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
750 static inline void inspect_fw_phexdecdef(char *label,
751 uint32_t val, uint32_t defval)
753 printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
756 printf("(== OpenWrt default)\n");
758 printf("(OpenWrt default: 0x%08x)\n", defval);
761 static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
765 printf("%-23s:", label);
766 for (i=0; i<MD5SUM_LEN; i++)
767 printf(" %02x", val[i]);
768 printf(" %s\n", text);
771 static int inspect_fw(void)
774 struct fw_header *hdr;
775 uint8_t md5sum[MD5SUM_LEN];
776 struct board_info *board;
777 int ret = EXIT_FAILURE;
779 buf = malloc(inspect_info.file_size);
781 ERR("no memory for buffer!\n");
785 ret = read_to_buf(&inspect_info, buf);
788 hdr = (struct fw_header *)buf;
790 inspect_fw_pstr("File name", inspect_info.file_name);
791 inspect_fw_phexdec("File size", inspect_info.file_size);
793 if (ntohl(hdr->version) != HEADER_VERSION_V1) {
794 ERR("file does not seem to have V1 header!\n");
798 inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
800 if (ntohl(hdr->unk1) != 0)
801 inspect_fw_phexdec("Unknown value 1", hdr->unk1);
803 memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
804 if (ntohl(hdr->boot_len) == 0)
805 memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
807 memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
808 get_md5(buf, inspect_info.file_size, hdr->md5sum1);
810 if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
811 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
812 inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
814 inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
816 if (ntohl(hdr->unk2) != 0)
817 inspect_fw_phexdec("Unknown value 2", hdr->unk2);
818 inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
819 "(purpose yet unknown, unchecked here)");
820 if (ntohl(hdr->unk3) != 0)
821 inspect_fw_phexdec("Unknown value 3", hdr->unk3);
825 inspect_fw_pstr("Vendor name", hdr->vendor_name);
826 inspect_fw_pstr("Firmware version", hdr->fw_version);
827 board = find_board_by_hwid(ntohl(hdr->hw_id));
829 layout = find_layout(board->layout_id);
830 inspect_fw_phexpost("Hardware ID",
831 ntohl(hdr->hw_id), board->id);
832 inspect_fw_phexexp("Hardware Revision",
833 ntohl(hdr->hw_rev), board->hw_rev);
835 inspect_fw_phexpost("Hardware ID",
836 ntohl(hdr->hw_id), "unknown");
837 inspect_fw_phex("Hardware Revision",
843 inspect_fw_phexdec("Kernel data offset",
844 ntohl(hdr->kernel_ofs));
845 inspect_fw_phexdec("Kernel data length",
846 ntohl(hdr->kernel_len));
848 inspect_fw_phexdef("Kernel load address",
849 ntohl(hdr->kernel_la),
850 layout ? layout->kernel_la : 0xffffffff);
851 inspect_fw_phexdef("Kernel entry point",
852 ntohl(hdr->kernel_ep),
853 layout ? layout->kernel_ep : 0xffffffff);
854 inspect_fw_phexdecdef("Rootfs data offset",
855 ntohl(hdr->rootfs_ofs),
856 layout ? layout->rootfs_ofs : 0xffffffff);
858 inspect_fw_phex("Kernel load address",
859 ntohl(hdr->kernel_la));
860 inspect_fw_phex("Kernel entry point",
861 ntohl(hdr->kernel_ep));
862 inspect_fw_phexdec("Rootfs data offset",
863 ntohl(hdr->rootfs_ofs));
865 inspect_fw_phexdec("Rootfs data length",
866 ntohl(hdr->rootfs_len));
867 inspect_fw_phexdec("Boot loader data offset",
868 ntohl(hdr->boot_ofs));
869 inspect_fw_phexdec("Boot loader data length",
870 ntohl(hdr->boot_len));
871 inspect_fw_phexdec("Total firmware length",
872 ntohl(hdr->fw_length));
880 filename = malloc(strlen(inspect_info.file_name) + 8);
881 sprintf(filename, "%s-kernel", inspect_info.file_name);
882 printf("Extracting kernel to \"%s\"...\n", filename);
883 fp = fopen(filename, "w");
885 if (!fwrite(buf + ntohl(hdr->kernel_ofs),
886 ntohl(hdr->kernel_len), 1, fp)) {
887 ERR("error in fwrite(): %s", strerror(errno));
891 ERR("error in fopen(): %s", strerror(errno));
895 filename = malloc(strlen(inspect_info.file_name) + 8);
896 sprintf(filename, "%s-rootfs", inspect_info.file_name);
897 printf("Extracting rootfs to \"%s\"...\n", filename);
898 fp = fopen(filename, "w");
900 if (!fwrite(buf + ntohl(hdr->rootfs_ofs),
901 ntohl(hdr->rootfs_len), 1, fp)) {
902 ERR("error in fwrite(): %s", strerror(errno));
906 ERR("error in fopen(): %s", strerror(errno));
917 int main(int argc, char *argv[])
919 int ret = EXIT_FAILURE;
924 progname = basename(argv[0]);
929 c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsj");
935 sscanf(optarg, "0x%x", &rootfs_align);
944 sscanf(optarg, "0x%x", &kernel_ep);
953 sscanf(optarg, "0x%x", &kernel_la);
965 kernel_info.file_name = optarg;
968 rootfs_info.file_name = optarg;
971 sscanf(optarg, "0x%x", &rootfs_ofs);
980 inspect_info.file_name = optarg;
997 ret = check_options();
1001 if (!inspect_info.file_name)