firmware-utils: mktplinkfw2: use static board struct for custom values
[openwrt/openwrt.git] / tools / firmware-utils / src / mktplinkfw2.c
index df64ac3a861aadea22d40c42b2e8b5ee6e48f6c7..0b7a15c996c079242055721a8805f37620dc0f6f 100644 (file)
@@ -20,6 +20,8 @@
 #include <getopt.h>     /* for getopt() */
 #include <stdarg.h>
 #include <errno.h>
+#include <stdbool.h>
+#include <endian.h>
 #include <sys/stat.h>
 
 #include <arpa/inet.h>
 
 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
 
-#define HEADER_VERSION_V2      0x02000000
-
-#define HWID_TD_W8970_V1               0x89700001
-
 #define MD5SUM_LEN     16
 
 struct file_info {
@@ -41,32 +39,32 @@ struct file_info {
 };
 
 struct fw_header {
-       uint32_t        version;        /* 0x00: header version */
-       char            fw_version[48]; /* 0x04: fw version string */
-       uint32_t        hw_id;          /* 0x34: hardware id */
-       uint32_t        hw_rev;         /* 0x38: FIXME: hardware revision? */
-       uint32_t        unk1;           /* 0x3c: 0x00000000 */
-       uint8_t         md5sum1[MD5SUM_LEN]; /* 0x40 */
-       uint32_t        unk2;           /* 0x50: 0x00000000 */
-       uint8_t         md5sum2[MD5SUM_LEN]; /* 0x54 */
-       uint32_t        unk3;           /* 0x64: 0xffffffff */
-
-       uint32_t        kernel_la;      /* 0x68: kernel load address */
-       uint32_t        kernel_ep;      /* 0x6c: kernel entry point */
-       uint32_t        fw_length;      /* 0x70: total length of the image */
-       uint32_t        kernel_ofs;     /* 0x74: kernel data offset */
-       uint32_t        kernel_len;     /* 0x78: kernel data length */
-       uint32_t        rootfs_ofs;     /* 0x7c: rootfs data offset */
-       uint32_t        rootfs_len;     /* 0x80: rootfs data length */
-       uint32_t        boot_ofs;       /* 0x84: FIXME: seems to be unused */
-       uint32_t        boot_len;       /* 0x88: FIXME: seems to be unused */
-       uint16_t        unk4;           /* 0x8c: 0x55aa */
-       uint8_t         sver_hi;        /* 0x8e */
-       uint8_t         sver_lo;        /* 0x8f */
-       uint8_t         unk5;           /* 0x90: magic: 0xa5 */
-       uint8_t         ver_hi;         /* 0x91 */
-       uint8_t         ver_mid;        /* 0x92 */
-       uint8_t         ver_lo;         /* 0x93 */
+       uint32_t        version;                        /* 0x00: header version */
+       char            fw_version[48];                 /* 0x04: fw version string */
+       uint32_t        hw_id;                          /* 0x34: hardware id */
+       uint32_t        hw_rev;                         /* 0x38: FIXME: hardware revision? */
+       uint32_t        hw_ver_add;                     /* 0x3c: additional hardware version */
+       uint8_t         md5sum1[MD5SUM_LEN];            /* 0x40 */
+       uint32_t        unk2;                           /* 0x50: 0x00000000 */
+       uint8_t         md5sum2[MD5SUM_LEN];            /* 0x54 */
+       uint32_t        unk3;                           /* 0x64: 0xffffffff */
+
+       uint32_t        kernel_la;                      /* 0x68: kernel load address */
+       uint32_t        kernel_ep;                      /* 0x6c: kernel entry point */
+       uint32_t        fw_length;                      /* 0x70: total length of the image */
+       uint32_t        kernel_ofs;                     /* 0x74: kernel data offset */
+       uint32_t        kernel_len;                     /* 0x78: kernel data length */
+       uint32_t        rootfs_ofs;                     /* 0x7c: rootfs data offset */
+       uint32_t        rootfs_len;                     /* 0x80: rootfs data length */
+       uint32_t        boot_ofs;                       /* 0x84: bootloader offset */
+       uint32_t        boot_len;                       /* 0x88: bootloader length */
+       uint16_t        unk4;                           /* 0x8c: 0x55aa */
+       uint8_t         sver_hi;                        /* 0x8e */
+       uint8_t         sver_lo;                        /* 0x8f */
+       uint8_t         unk5;                           /* 0x90: magic: 0xa5 */
+       uint8_t         ver_hi;                         /* 0x91 */
+       uint8_t         ver_mid;                        /* 0x92 */
+       uint8_t         ver_lo;                         /* 0x93 */
        uint8_t         pad[364];
 } __attribute__ ((packed));
 
@@ -82,7 +80,10 @@ struct board_info {
        char            *id;
        uint32_t        hw_id;
        uint32_t        hw_rev;
+       uint32_t        hw_ver_add;
        char            *layout_id;
+       uint32_t        hdr_ver;
+       bool            endian_swap;
 };
 
 /*
@@ -94,15 +95,17 @@ static char *vendor = "TP-LINK Technologies";
 static char *version = "ver. 1.0";
 static char *fw_ver = "0.0.0";
 static char *sver = "1.0";
+static uint32_t hdr_ver = 2;
+
+static struct board_info custom_board;
 
 static char *board_id;
 static struct board_info *board;
 static char *layout_id;
 static struct flash_layout *layout;
 static char *opt_hw_id;
-static uint32_t hw_id;
 static char *opt_hw_rev;
-static uint32_t hw_rev;
+static char *opt_hw_ver_add;
 static int fw_ver_lo;
 static int fw_ver_mid;
 static int fw_ver_hi;
@@ -130,8 +133,8 @@ char md5salt_normal[MD5SUM_LEN] = {
 };
 
 char md5salt_boot[MD5SUM_LEN] = {
-       0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
-       0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
+       0x8c, 0xef, 0x33, 0x5f, 0xd5, 0xc5, 0xce, 0xfa,
+       0xac, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
 };
 
 static struct flash_layout layouts[] = {
@@ -141,6 +144,24 @@ static struct flash_layout layouts[] = {
                .kernel_la      = 0x80002000,
                .kernel_ep      = 0x80002000,
                .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "16Mltq",
+               .fw_max_len     = 0xf90000,
+               .kernel_la      = 0x80002000,
+               .kernel_ep      = 0x800061b0,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "8Mmtk",
+               .fw_max_len     = 0x7a0000,
+               .kernel_la      = 0x80000000,
+               .kernel_ep      = 0x80000000,
+               .rootfs_ofs     = 0x140000,
+       }, {
+               .id             = "8MLmtk",
+               .fw_max_len     = 0x7b0000,
+               .kernel_la      = 0x80000000,
+               .kernel_ep      = 0x80000000,
+               .rootfs_ofs     = 0x140000,
        }, {
                /* terminating entry */
        }
@@ -149,9 +170,57 @@ static struct flash_layout layouts[] = {
 static struct board_info boards[] = {
        {
                .id             = "TD-W8970v1",
-               .hw_id          = HWID_TD_W8970_V1,
+               .hw_id          = 0x89700001,
                .hw_rev         = 1,
                .layout_id      = "8Mltq",
+       }, {
+               .id             = "TD-W8980v1",
+               .hw_id          = 0x89800001,
+               .hw_rev         = 14,
+               .layout_id      = "8Mltq",
+       }, {
+               .id             = "ArcherC20i",
+               .hw_id          = 0xc2000001,
+               .hw_rev         = 58,
+               .layout_id      = "8Mmtk",
+               .hdr_ver        = 3,
+               .endian_swap    = true,
+       }, {
+               .id             = "ArcherVR200V",
+               .hw_id          = 0x73b70801,
+               .hw_rev         = 0x2f,
+               .layout_id      = "16Mltq",
+               .hdr_ver        = 2,
+       }, {
+               .id             = "ArcherC50",
+               .hw_id          = 0xc7500001,
+               .hw_rev         = 69,
+               .layout_id      = "8Mmtk",
+               .hdr_ver        = 3,
+               .endian_swap    = true,
+       }, {
+               .id             = "ArcherMR200",
+               .hw_id          = 0xd7500001,
+               .hw_rev         = 0x4a,
+               .layout_id      = "8MLmtk",
+               .hdr_ver        = 3,
+               .endian_swap    = true,
+       }, {
+               .id             = "TL-WR840NV4",
+               .hw_id          = 0x08400004,
+               .hw_rev         = 0x1,
+               .hw_ver_add     = 0x4,
+               .layout_id      = "8Mmtk",
+               .hdr_ver        = 3,
+               .endian_swap    = true,
+       }, {
+               .id             = "TL-WR841NV13",
+               .hw_id          = 0x08410013,
+               .hw_rev         = 0x268,
+               .hw_ver_add     = 0x13,
+               .layout_id      = "8Mmtk",
+               .hdr_ver        = 3,
+               .endian_swap    = true,
        }, {
                /* terminating entry */
        }
@@ -169,7 +238,7 @@ static struct board_info boards[] = {
 #define ERRS(fmt, ...) do { \
        int save = errno; \
        fflush(0); \
-       fprintf(stderr, "[%s] *** error: " fmt "\n", \
+       fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
                        progname, ## __VA_ARGS__, strerror(save)); \
 } while (0)
 
@@ -232,10 +301,12 @@ static void usage(int status)
 "Options:\n"
 "  -B <board>      create image for the board specified with <board>\n"
 "  -c              use combined kernel image\n"
+"  -e              swap endianness in kernel load address and entry point\n"
 "  -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"
+"  -w <hwveradd>   use additional hardware version specified with <hwveradd>\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"
@@ -244,6 +315,8 @@ static void usage(int status)
 "  -o <file>       write output to the file <file>\n"
 "  -s              strip padding from the end of the image\n"
 "  -j              add jffs2 end-of-filesystem markers\n"
+"  -N <vendor>     set image vendor to <vendor>\n"
+"  -T <version>    set header version to <version>\n"
 "  -V <version>    set image version to <version>\n"
 "  -v <version>    set firmware version to <version>\n"
 "  -y <version>    set secondary version to <version>\n"
@@ -337,19 +410,27 @@ static int check_options(void)
                if (layout_id == NULL)
                        layout_id = board->layout_id;
 
-               hw_id = board->hw_id;
-               hw_rev = board->hw_rev;
+               if (board->hdr_ver)
+                       hdr_ver = board->hdr_ver;
        } else {
+               board = &custom_board;
+
                if (layout_id == NULL) {
                        ERR("flash layout is not specified");
                        return -1;
                }
-               hw_id = strtoul(opt_hw_id, NULL, 0);
+
+               board->hw_id = strtoul(opt_hw_id, NULL, 0);
 
                if (opt_hw_rev)
-                       hw_rev = strtoul(opt_hw_rev, NULL, 0);
+                       board->hw_rev = strtoul(opt_hw_rev, NULL, 0);
                else
-                       hw_rev = 1;
+                       board->hw_rev = 1;
+
+               if (opt_hw_ver_add)
+                       board->hw_ver_add = strtoul(opt_hw_ver_add, NULL, 0);
+               else
+                       board->hw_ver_add = 0;
        }
 
        layout = find_layout(layout_id);
@@ -446,7 +527,7 @@ static void fill_header(char *buf, int len)
 
        memset(hdr, '\xff', sizeof(struct fw_header));
 
-       hdr->version = htonl(HEADER_VERSION_V2);
+       hdr->version = htonl(bswap_32(hdr_ver));
        ver_len = strlen(version);
        if (ver_len > (sizeof(hdr->fw_version) - 1))
                ver_len = sizeof(hdr->fw_version) - 1;
@@ -454,8 +535,9 @@ static void fill_header(char *buf, int len)
        memcpy(hdr->fw_version, version, ver_len);
        hdr->fw_version[ver_len] = 0;
 
-       hdr->hw_id = htonl(hw_id);
-       hdr->hw_rev = htonl(hw_rev);
+       hdr->hw_id = htonl(board->hw_id);
+       hdr->hw_rev = htonl(board->hw_rev);
+       hdr->hw_ver_add = htonl(board->hw_ver_add);
 
        if (boot_info.file_size == 0) {
                memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
@@ -480,7 +562,6 @@ static void fill_header(char *buf, int len)
        hdr->boot_ofs = htonl(0);
        hdr->boot_len = htonl(boot_info.file_size);
 
-       hdr->unk1 = htonl(0);
        hdr->unk2 = htonl(0);
        hdr->unk3 = htonl(0xffffffff);
        hdr->unk4 = htons(0x55aa);
@@ -493,6 +574,11 @@ static void fill_header(char *buf, int len)
        hdr->ver_mid = fw_ver_mid;
        hdr->ver_lo = fw_ver_lo;
 
+       if (board->endian_swap) {
+               hdr->kernel_la = bswap_32(hdr->kernel_la);
+               hdr->kernel_ep = bswap_32(hdr->kernel_ep);
+       }
+
        get_md5(buf, len, hdr->md5sum1);
 }
 
@@ -708,16 +794,17 @@ static int inspect_fw(void)
        inspect_fw_pstr("File name", inspect_info.file_name);
        inspect_fw_phexdec("File size", inspect_info.file_size);
 
-       if (ntohl(hdr->version) != HEADER_VERSION_V2) {
-               ERR("file does not seem to have V2 header!\n");
+       switch(bswap_32(ntohl(hdr->version))) {
+       case 2:
+       case 3:
+               break;
+       default:
+               ERR("file does not seem to have V2/V3 header!\n");
                goto out_free_buf;
        }
 
        inspect_fw_phexdec("Version 2 Header size", sizeof(struct fw_header));
 
-       if (ntohl(hdr->unk1) != 0)
-               inspect_fw_phexdec("Unknown value 1", hdr->unk1);
-
        memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
        if (ntohl(hdr->boot_len) == 0)
                memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
@@ -756,11 +843,15 @@ static int inspect_fw(void)
                                    ntohl(hdr->hw_id), board->id);
                inspect_fw_phexexp("Hardware Revision",
                                   ntohl(hdr->hw_rev), board->hw_rev);
+               inspect_fw_phexexp("Additional HW Version",
+                                  ntohl(hdr->hw_ver_add), board->hw_ver_add);
        } else {
                inspect_fw_phexpost("Hardware ID",
                                    ntohl(hdr->hw_id), "unknown");
                inspect_fw_phex("Hardware Revision",
                                ntohl(hdr->hw_rev));
+               inspect_fw_phex("Additional HW Version",
+                               ntohl(hdr->hw_ver_add));
        }
 
        printf("%-23s: %d.%d.%d-%d.%d\n", "Software version",
@@ -855,7 +946,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:xhsjv:y:");
+               c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:w:ci:k:r:R:o:xhsjv:y:T:e");
                if (c == -1)
                        break;
 
@@ -878,6 +969,9 @@ int main(int argc, char *argv[])
                case 'W':
                        opt_hw_rev = optarg;
                        break;
+               case 'w':
+                       opt_hw_ver_add = optarg;
+                       break;
                case 'L':
                        sscanf(optarg, "0x%x", &kernel_la);
                        break;
@@ -920,6 +1014,12 @@ int main(int argc, char *argv[])
                case 'x':
                        extract = 1;
                        break;
+               case 'T':
+                       hdr_ver = atoi(optarg);
+                       break;
+               case 'e':
+                       custom_board.endian_swap = true;
+                       break;
                case 'h':
                        usage(EXIT_SUCCESS);
                        break;