ar71xx: Add Support for the Bitmain Antminer S1
[openwrt/openwrt.git] / tools / firmware-utils / src / mktplinkfw.c
index 174df957fb57dc241c761163ff43f38917c40bb6..87340db2404f7d00407633f6493d144dd26aba81 100644 (file)
 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
 
 #define HEADER_VERSION_V1      0x01000000
+#define HWID_ANTMINER_S1       0x04440001
+#define HWID_GL_INET_V1                0x08000001
+#define HWID_GS_OOLITE_V1      0x3C000101
+#define HWID_TL_MR10U_V1       0x00100101
+#define HWID_TL_MR13U_V1       0x00130101
+#define HWID_TL_MR3020_V1      0x30200001
 #define HWID_TL_MR3220_V1      0x32200001
+#define HWID_TL_MR3220_V2      0x32200002
 #define HWID_TL_MR3420_V1      0x34200001
+#define HWID_TL_MR3420_V2      0x34200002
+#define HWID_TL_WA701N_V1      0x07010001
+#define HWID_TL_WA701N_V2      0x07010002
+#define HWID_TL_WA7210N_V2     0x72100002
+#define HWID_TL_WA7510N_V1     0x75100001
+#define HWID_TL_WA801ND_V1     0x08010001
+#define HWID_TL_WA830RE_V1     0x08300010
+#define HWID_TL_WA830RE_V2     0x08300002
+#define HWID_TL_WA801ND_V2     0x08010002
 #define HWID_TL_WA901ND_V1     0x09010001
 #define HWID_TL_WA901ND_V2     0x09010002
+#define HWID_TL_WDR4300_V1_IL  0x43008001
+#define HWID_TL_WDR4900_V1     0x49000001
 #define HWID_TL_WR703N_V1      0x07030101
+#define HWID_TL_WR720N_V3      0x07200103
 #define HWID_TL_WR741ND_V1     0x07410001
 #define HWID_TL_WR741ND_V4     0x07410004
 #define HWID_TL_WR740N_V1      0x07400001
 #define HWID_TL_WR740N_V3      0x07400003
 #define HWID_TL_WR743ND_V1     0x07430001
+#define HWID_TL_WR743ND_V2     0x07430002
 #define HWID_TL_WR841N_V1_5    0x08410002
 #define HWID_TL_WR841ND_V3     0x08410003
 #define HWID_TL_WR841ND_V5     0x08410005
@@ -47,6 +67,9 @@
 #define HWID_TL_WR941ND_V2     0x09410002
 #define HWID_TL_WR941ND_V4     0x09410004
 #define HWID_TL_WR1043ND_V1    0x10430001
+#define HWID_TL_WR1043ND_V2    0x10430002
+#define HWID_TL_WR1041N_V2     0x10410002
+#define HWID_TL_WR2543N_V1     0x25430001
 
 #define MD5SUM_LEN     16
 
@@ -75,7 +98,10 @@ struct fw_header {
        uint32_t        rootfs_len;     /* rootfs data length */
        uint32_t        boot_ofs;       /* bootloader data offset */
        uint32_t        boot_len;       /* bootloader data length */
-       uint8_t         pad[360];
+       uint16_t        ver_hi;
+       uint16_t        ver_mid;
+       uint16_t        ver_lo;
+       uint8_t         pad[354];
 } __attribute__ ((packed));
 
 struct flash_layout {
@@ -100,6 +126,7 @@ static char *ofname;
 static char *progname;
 static char *vendor = "TP-LINK Technologies";
 static char *version = "ver. 1.0";
+static char *fw_ver = "0.0.0";
 
 static char *board_id;
 static struct board_info *board;
@@ -109,6 +136,9 @@ static char *opt_hw_id;
 static uint32_t hw_id;
 static char *opt_hw_rev;
 static uint32_t hw_rev;
+static int fw_ver_lo;
+static int fw_ver_mid;
+static int fw_ver_hi;
 static struct file_info kernel_info;
 static uint32_t kernel_la = 0;
 static uint32_t kernel_ep = 0;
@@ -119,6 +149,11 @@ static uint32_t rootfs_align;
 static struct file_info boot_info;
 static int combined;
 static int strip_padding;
+static int ignore_size;
+static int add_jffs2_eof;
+static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
+static uint32_t fw_max_len;
+static uint32_t reserved_space;
 
 static struct file_info inspect_info;
 static int extract = 0;
@@ -152,6 +187,30 @@ static struct flash_layout layouts[] = {
                .kernel_la      = 0x80060000,
                .kernel_ep      = 0x80060000,
                .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "8Mlzma",
+               .fw_max_len     = 0x7c0000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x100000,
+       }, {
+               .id             = "16M",
+               .fw_max_len     = 0xf80000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "16Mlzma",
+               .fw_max_len     = 0xf80000,
+               .kernel_la      = 0x80060000,
+               .kernel_ep      = 0x80060000,
+               .rootfs_ofs     = 0x100000,
+       }, {
+               .id             = "16Mppc",
+               .fw_max_len     = 0xf80000,
+               .kernel_la      = 0x00000000,
+               .kernel_ep      = 0xc0000000,
+               .rootfs_ofs     = 0x2a0000,
        }, {
                /* terminating entry */
        }
@@ -159,15 +218,80 @@ static struct flash_layout layouts[] = {
 
 static struct board_info boards[] = {
        {
+               .id             = "TL-MR10Uv1",
+               .hw_id          = HWID_TL_MR10U_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
+       }, {
+               .id             = "TL-MR13Uv1",
+               .hw_id          = HWID_TL_MR13U_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
+       }, {
+               .id             = "TL-MR3020v1",
+               .hw_id          = HWID_TL_MR3020_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
+       }, {
                .id             = "TL-MR3220v1",
                .hw_id          = HWID_TL_MR3220_V1,
                .hw_rev         = 1,
                .layout_id      = "4M",
+       }, {
+               .id             = "TL-MR3220v2",
+               .hw_id          = HWID_TL_MR3220_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
        }, {
                .id             = "TL-MR3420v1",
                .hw_id          = HWID_TL_MR3420_V1,
                .hw_rev         = 1,
                .layout_id      = "4M",
+       }, {
+               .id             = "TL-MR3420v2",
+               .hw_id          = HWID_TL_MR3420_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
+       }, {
+               .id             = "TL-WA701Nv1",
+               .hw_id          = HWID_TL_WA701N_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4M",
+       }, {
+               .id             = "TL-WA701Nv2",
+               .hw_id          = HWID_TL_WA701N_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
+       }, {
+               .id             = "TL-WA7210N",
+               .hw_id          = HWID_TL_WA7210N_V2,
+               .hw_rev         = 2,
+               .layout_id      = "4Mlzma",
+       }, {
+               .id             = "TL-WA7510N",
+               .hw_id          = HWID_TL_WA7510N_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4M",
+       }, {
+               .id             = "TL-WA801NDv1",
+               .hw_id          = HWID_TL_WA801ND_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4M",
+       }, {
+               .id             = "TL-WA830REv1",
+               .hw_id          = HWID_TL_WA830RE_V1,
+               .hw_rev         = 1,
+               .layout_id      = "4M",
+       }, {
+               .id             = "TL-WA830REv2",
+               .hw_id          = HWID_TL_WA830RE_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4M",
+       }, {
+               .id             = "TL-WA801NDv2",
+               .hw_id          = HWID_TL_WA801ND_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
        }, {
                .id             = "TL-WA901NDv1",
                .hw_id          = HWID_TL_WA901ND_V1,
@@ -178,6 +302,16 @@ static struct board_info boards[] = {
                .hw_id          = HWID_TL_WA901ND_V2,
                .hw_rev         = 1,
                .layout_id      = "4M",
+       }, {
+               .id             = "TL-WDR4300v1",
+               .hw_id          = HWID_TL_WDR4300_V1_IL,
+               .hw_rev         = 1,
+               .layout_id      = "8Mlzma",
+       }, {
+               .id             = "TL-WDR4900v1",
+               .hw_id          = HWID_TL_WDR4900_V1,
+               .hw_rev         = 1,
+               .layout_id      = "16Mppc",
        }, {
                .id             = "TL-WR741NDv1",
                .hw_id          = HWID_TL_WR741ND_V1,
@@ -203,6 +337,11 @@ static struct board_info boards[] = {
                .hw_id          = HWID_TL_WR743ND_V1,
                .hw_rev         = 1,
                .layout_id      = "4M",
+       }, {
+               .id             = "TL-WR743NDv2",
+               .hw_id          = HWID_TL_WR743ND_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
        }, {
                .id             = "TL-WR841Nv1.5",
                .hw_id          = HWID_TL_WR841N_V1_5,
@@ -233,16 +372,51 @@ static struct board_info boards[] = {
                .hw_id          = HWID_TL_WR941ND_V4,
                .hw_rev         = 1,
                .layout_id      = "4M",
+       }, {
+               .id             = "TL-WR1041Nv2",
+               .hw_id          = HWID_TL_WR1041N_V2,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
        }, {
                .id             = "TL-WR1043NDv1",
                .hw_id          = HWID_TL_WR1043ND_V1,
                .hw_rev         = 1,
                .layout_id      = "8M",
+       }, {
+               .id             = "TL-WR1043NDv2",
+               .hw_id          = HWID_TL_WR1043ND_V2,
+               .hw_rev         = 1,
+               .layout_id      = "8Mlzma",
+       }, {
+               .id             = "TL-WR2543Nv1",
+               .hw_id          = HWID_TL_WR2543N_V1,
+               .hw_rev         = 1,
+               .layout_id      = "8Mlzma",
        }, {
                .id             = "TL-WR703Nv1",
                .hw_id          = HWID_TL_WR703N_V1,
                .hw_rev         = 1,
                .layout_id      = "4Mlzma",
+       }, {
+               .id             = "TL-WR720Nv3",
+               .hw_id          = HWID_TL_WR720N_V3,
+               .hw_rev         = 1,
+               .layout_id      = "4Mlzma",
+       }, {
+               .id             = "GL-INETv1",
+               .hw_id          = HWID_GL_INET_V1,
+               .hw_rev         = 1,
+               .layout_id      = "8Mlzma",
+       }, {
+               .id             = "GS-OOLITEv1",
+               .hw_id          = HWID_GS_OOLITE_V1,
+               .hw_rev         = 1,
+               .layout_id      = "16Mlzma",
+       }, {
+               .id             = "ANTMINER-S1",
+               .hw_id          = HWID_ANTMINER_S1,
+               .hw_rev         = 1,
+               .layout_id      = "8Mlzma",
        }, {
                /* terminating entry */
        }
@@ -326,6 +500,7 @@ static void usage(int status)
 "  -E <ep>         overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
 "  -L <la>         overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
 "  -H <hwid>       use hardware id specified with <hwid>\n"
+"  -W <hwrev>      use hardware revision specified with <hwrev>\n"
 "  -F <id>         use flash layout specified with <id>\n"
 "  -k <file>       read kernel image from the file <file>\n"
 "  -r <file>       read rootfs image from the file <file>\n"
@@ -333,10 +508,14 @@ static void usage(int status)
 "  -R <offset>     overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
 "  -o <file>       write output to the file <file>\n"
 "  -s              strip padding from the end of the image\n"
+"  -S              ignore firmware size limit (only for combined images)\n"
+"  -j              add jffs2 end-of-filesystem markers\n"
 "  -N <vendor>     set image vendor to <vendor>\n"
 "  -V <version>    set image version to <version>\n"
+"  -v <version>    set firmware version to <version>\n"
 "  -i <file>       inspect given firmware file <file>\n"
 "  -x              extract kernel and rootfs while inspecting (requires -i)\n"
+"  -X <size>       reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
 "  -h              show this screen\n"
        );
 
@@ -453,6 +632,13 @@ static int check_options(void)
        if (!rootfs_ofs)
                rootfs_ofs = layout->rootfs_ofs;
 
+       if (reserved_space > layout->fw_max_len) {
+               ERR("reserved space is not valid");
+               return -1;
+       }
+
+       fw_max_len = layout->fw_max_len - reserved_space;
+
        if (kernel_info.file_name == NULL) {
                ERR("no kernel image specified");
                return -1;
@@ -466,9 +652,14 @@ static int check_options(void)
 
        if (combined) {
                if (kernel_info.file_size >
-                   layout->fw_max_len - sizeof(struct fw_header)) {
-                       ERR("kernel image is too big");
-                       return -1;
+                   fw_max_len - sizeof(struct fw_header)) {
+                       if (!ignore_size) {
+                               ERR("kernel image is too big");
+                               return -1;
+                       }
+                       layout->fw_max_len = sizeof(struct fw_header) +
+                                            kernel_info.file_size +
+                                            reserved_space;
                }
        } else {
                if (rootfs_info.file_name == NULL) {
@@ -488,7 +679,7 @@ static int check_options(void)
                        DBG("kernel length aligned to %u", kernel_len);
 
                        if (kernel_len + rootfs_info.file_size >
-                           layout->fw_max_len - sizeof(struct fw_header)) {
+                           fw_max_len - sizeof(struct fw_header)) {
                                ERR("images are too big");
                                return -1;
                        }
@@ -500,7 +691,7 @@ static int check_options(void)
                        }
 
                        if (rootfs_info.file_size >
-                           (layout->fw_max_len - rootfs_ofs)) {
+                           (fw_max_len - rootfs_ofs)) {
                                ERR("rootfs image is too big");
                                return -1;
                        }
@@ -512,6 +703,12 @@ static int check_options(void)
                return -1;
        }
 
+       ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo);
+       if (ret != 3) {
+               ERR("invalid firmware version '%s'", fw_ver);
+               return -1;
+       }
+
        return 0;
 }
 
@@ -542,9 +739,47 @@ static void fill_header(char *buf, int len)
                hdr->rootfs_len = htonl(rootfs_info.file_size);
        }
 
+       hdr->ver_hi = htons(fw_ver_hi);
+       hdr->ver_mid = htons(fw_ver_mid);
+       hdr->ver_lo = htons(fw_ver_lo);
+
        get_md5(buf, len, hdr->md5sum1);
 }
 
+static int pad_jffs2(char *buf, int currlen)
+{
+       int len;
+       uint32_t pad_mask;
+
+       len = currlen;
+       pad_mask = (64 * 1024);
+       while ((len < layout->fw_max_len) && (pad_mask != 0)) {
+               uint32_t mask;
+               int i;
+
+               for (i = 10; i < 32; i++) {
+                       mask = 1 << i;
+                       if (pad_mask & mask)
+                               break;
+               }
+
+               len = ALIGN(len, mask);
+
+               for (i = 10; i < 32; i++) {
+                       mask = 1 << i;
+                       if ((len & (mask - 1)) == 0)
+                               pad_mask &= ~mask;
+               }
+
+               for (i = 0; i < sizeof(jffs2_eof_mark); i++)
+                       buf[len + i] = jffs2_eof_mark[i];
+
+               len += sizeof(jffs2_eof_mark);
+       }
+
+       return len;
+}
+
 static int write_fw(char *data, int len)
 {
        FILE *f;
@@ -606,6 +841,7 @@ static int build_fw(void)
                        p = buf + writelen;
                else
                        p = buf + rootfs_ofs;
+
                ret = read_to_buf(&rootfs_info, p);
                if (ret)
                        goto out_free_buf;
@@ -614,6 +850,9 @@ static int build_fw(void)
                        writelen += rootfs_info.file_size;
                else
                        writelen = rootfs_ofs + rootfs_info.file_size;
+
+               if (add_jffs2_eof)
+                       writelen = pad_jffs2(buf, writelen);
        }
 
        if (!strip_padding)
@@ -855,7 +1094,7 @@ int main(int argc, char *argv[])
        while ( 1 ) {
                int c;
 
-               c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhs");
+               c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsSjv:");
                if (c == -1)
                        break;
 
@@ -884,6 +1123,9 @@ int main(int argc, char *argv[])
                case 'V':
                        version = optarg;
                        break;
+               case 'v':
+                       fw_ver = optarg;
+                       break;
                case 'N':
                        vendor = optarg;
                        break;
@@ -905,15 +1147,24 @@ int main(int argc, char *argv[])
                case 's':
                        strip_padding = 1;
                        break;
+               case 'S':
+                       ignore_size = 1;
+                       break;
                case 'i':
                        inspect_info.file_name = optarg;
                        break;
+               case 'j':
+                       add_jffs2_eof = 1;
+                       break;
                case 'x':
                        extract = 1;
                        break;
                case 'h':
                        usage(EXIT_SUCCESS);
                        break;
+               case 'X':
+                       sscanf(optarg, "0x%x", &reserved_space);
+                       break;
                default:
                        usage(EXIT_FAILURE);
                        break;