[backfire] tools: backport r23137
[openwrt-10.03/.git] / tools / firmware-utils / src / mktplinkfw.c
index c7bf9b1945d53bc9f5bcc6f7f6a99ff87160460a..d32da3743be2470a764791bbde3211062eaaae87 100644 (file)
 #endif
 
 #define HEADER_VERSION_V1      0x01000000
+#define HWID_TL_MR3420_V1      0x34200001
 #define HWID_TL_WR741ND_V1     0x07410001
+#define HWID_TL_WR740N_V1      0x07400001
+#define HWID_TL_WR841N_V1_5    0x08410002
 #define HWID_TL_WR841ND_V3     0x08410003
+#define HWID_TL_WR841ND_V5     0x08410005
 #define HWID_TL_WR941ND_V2     0x09410002
+#define HWID_TL_WR941ND_V4     0x09410004
+#define HWID_TL_WR1043ND_V1    0x10430001
 
 #define MD5SUM_LEN     16
 
@@ -90,6 +96,8 @@ static struct board_info *board;
 static struct file_info kernel_info;
 static struct file_info rootfs_info;
 static struct file_info boot_info;
+static int combined;
+static int strip_padding;
 
 char md5salt_normal[MD5SUM_LEN] = {
        0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
@@ -103,13 +111,37 @@ char md5salt_boot[MD5SUM_LEN] = {
 
 static struct board_info boards[] = {
        {
+               .id             = "TL-MR3420v1",
+               .hw_id          = HWID_TL_MR3420_V1,
+               .hw_rev         = 1,
+               .fw_max_len     = 0x3c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
+       }, {
                .id             = "TL-WR741NDv1",
                .hw_id          = HWID_TL_WR741ND_V1,
                .hw_rev         = 1,
                .fw_max_len     = 0x3c0000,
                .kernel_la      = 0x80060000,
                .kernel_ep      = 0x80060000,
-               .rootfs_ofs     = 0x120000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "TL-WR740Nv1",
+               .hw_id          = HWID_TL_WR740N_V1,
+               .hw_rev         = 1,
+               .fw_max_len     = 0x3c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "TL-WR841Nv1.5",
+               .hw_id          = HWID_TL_WR841N_V1_5,
+               .hw_rev         = 2,
+               .fw_max_len     = 0x3c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
        }, {
                .id             = "TL-WR841NDv3",
                .hw_id          = HWID_TL_WR841ND_V3,
@@ -117,7 +149,15 @@ static struct board_info boards[] = {
                .fw_max_len     = 0x3c0000,
                .kernel_la      = 0x80060000,
                .kernel_ep      = 0x80060000,
-               .rootfs_ofs     = 0x120000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "TL-WR841NDv5",
+               .hw_id          = HWID_TL_WR841ND_V5,
+               .hw_rev         = 1,
+               .fw_max_len     = 0x3c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
        }, {
                .id             = "TL-WR941NDv2",
                .hw_id          = HWID_TL_WR941ND_V2,
@@ -125,7 +165,23 @@ static struct board_info boards[] = {
                .fw_max_len     = 0x3c0000,
                .kernel_la      = 0x80060000,
                .kernel_ep      = 0x80060000,
-               .rootfs_ofs     = 0x120000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "TL-WR941NDv4",
+               .hw_id          = HWID_TL_WR941ND_V4,
+               .hw_rev         = 1,
+               .fw_max_len     = 0x3c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "TL-WR1043NDv1",
+               .hw_id          = HWID_TL_WR1043ND_V1,
+               .hw_rev         = 1,
+               .fw_max_len     = 0x7c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
        }, {
                /* terminating entry */
        }
@@ -177,10 +233,13 @@ static void usage(int status)
 "\n"
 "Options:\n"
 "  -B <board>      create image for the board specified with <board>\n"
+"  -c              use combined kernel image\n"
 "  -k <file>       read kernel image from the file <file>\n"
 "  -r <file>       read rootfs image from the file <file>\n"
 "  -o <file>       write output to the file <file>\n"
-"  -v <version>    set image version to <version>\n"
+"  -s              strip padding from the end of the image\n"
+"  -N <vendor>     set image vendor to <vendor>\n"
+"  -V <version>    set image version to <version>\n"
 "  -h              show this screen\n"
        );
 
@@ -264,23 +323,32 @@ static int check_options(void)
        if (ret)
                return ret;
 
-       if (kernel_info.file_size > board->rootfs_ofs - sizeof(struct fw_header)) {
-               ERR("kernel image is too big");
-               return -1;
-       }
-
-       if (rootfs_info.file_name == NULL) {
-               ERR("no rootfs image specified");
-               return -1;
-       }
+       if (combined) {
+               if (kernel_info.file_size >
+                   board->fw_max_len - sizeof(struct fw_header)) {
+                       ERR("kernel image is too big");
+                       return -1;
+               }
+       } else {
+               if (kernel_info.file_size >
+                   board->rootfs_ofs - sizeof(struct fw_header)) {
+                       ERR("kernel image is too big");
+                       return -1;
+               }
+               if (rootfs_info.file_name == NULL) {
+                       ERR("no rootfs image specified");
+                       return -1;
+               }
 
-       ret = get_file_stat(&rootfs_info);
-       if (ret)
-               return ret;
+               ret = get_file_stat(&rootfs_info);
+               if (ret)
+                       return ret;
 
-       if (rootfs_info.file_size > (board->fw_max_len - board->rootfs_ofs)) {
-               ERR("rootfs image is too big");
-               return -1;
+               if (rootfs_info.file_size >
+                    (board->fw_max_len - board->rootfs_ofs)) {
+                       ERR("rootfs image is too big");
+                       return -1;
+               }
        }
 
        if (ofname == NULL) {
@@ -313,8 +381,10 @@ static void fill_header(char *buf, int len)
        hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
        hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
        hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
-       hdr->rootfs_ofs = HOST_TO_BE32(board->rootfs_ofs);
-       hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
+       if (!combined) {
+               hdr->rootfs_ofs = HOST_TO_BE32(board->rootfs_ofs);
+               hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
+       }
 
        get_md5(buf, len, hdr->md5sum1);
 }
@@ -357,6 +427,7 @@ static int build_fw(void)
        char *buf;
        char *p;
        int ret = EXIT_FAILURE;
+       int writelen = 0;
 
        buflen = board->fw_max_len;
 
@@ -372,14 +443,22 @@ static int build_fw(void)
        if (ret)
                goto out_free_buf;
 
-       p = buf + board->rootfs_ofs;
-       ret = read_to_buf(&rootfs_info, p);
-       if (ret)
-               goto out_free_buf;
+       writelen = kernel_info.file_size;
+
+       if (!combined) {
+               p = buf + board->rootfs_ofs;
+               ret = read_to_buf(&rootfs_info, p);
+               if (ret)
+                       goto out_free_buf;
+
+               writelen = board->rootfs_ofs + rootfs_info.file_size;
+       }
 
-       fill_header(buf, buflen);
+       if (!strip_padding)
+               writelen = buflen;
 
-       ret = write_fw(buf, buflen);
+       fill_header(buf, writelen);
+       ret = write_fw(buf, writelen);
        if (ret)
                goto out_free_buf;
 
@@ -403,7 +482,7 @@ int main(int argc, char *argv[])
        while ( 1 ) {
                int c;
 
-               c = getopt(argc, argv, "B:V:N:k:r:o:v:h:");
+               c = getopt(argc, argv, "B:V:N:ck:r:o:hs");
                if (c == -1)
                        break;
 
@@ -417,6 +496,9 @@ int main(int argc, char *argv[])
                case 'N':
                        vendor = optarg;
                        break;
+               case 'c':
+                       combined++;
+                       break;
                case 'k':
                        kernel_info.file_name = optarg;
                        break;
@@ -426,8 +508,8 @@ int main(int argc, char *argv[])
                case 'o':
                        ofname = optarg;
                        break;
-               case 'v':
-                       version = optarg;
+               case 's':
+                       strip_padding = 1;
                        break;
                case 'h':
                        usage(EXIT_SUCCESS);