brcm63xx: add support for Huawei HG622
[librecmc/librecmc.git] / tools / firmware-utils / src / mktplinkfw.c
index 12420ad99552c9b93de34042df871017dab3cb2b..085eb4c39d7ca6438029a8f0c1e6d541fa126931 100644 (file)
@@ -30,8 +30,9 @@
 #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_ANTMINER_S3       0x04440003
+#define HEADER_VERSION_V2      0x02000000
+#define HWID_ANTMINER_S1       0x04440101
+#define HWID_ANTMINER_S3       0x04440301
 #define HWID_GL_INET_V1                0x08000001
 #define HWID_GS_OOLITE_V1      0x3C000101
 #define HWID_ONION_OMEGA       0x04700001
@@ -130,6 +131,7 @@ static char *progname;
 static char *vendor = "TP-LINK Technologies";
 static char *version = "ver. 1.0";
 static char *fw_ver = "0.0.0";
+static uint32_t hdr_ver = HEADER_VERSION_V1;
 
 static char *board_id;
 static struct board_info *board;
@@ -139,6 +141,7 @@ static char *opt_hw_id;
 static uint32_t hw_id;
 static char *opt_hw_rev;
 static uint32_t hw_rev;
+static uint32_t opt_hdr_ver = 1;
 static int fw_ver_lo;
 static int fw_ver_mid;
 static int fw_ver_hi;
@@ -430,6 +433,11 @@ static struct board_info boards[] = {
                .hw_id          = HWID_ANTMINER_S1,
                .hw_rev         = 1,
                .layout_id      = "8Mlzma",
+       }, {
+               .id             = "ANTMINER-S3",
+               .hw_id          = HWID_ANTMINER_S3,
+               .hw_rev         = 1,
+               .layout_id      = "8Mlzma",
        }, {
                /* terminating entry */
        }
@@ -447,7 +455,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)
 
@@ -526,6 +534,7 @@ static void usage(int status)
 "  -N <vendor>     set image vendor to <vendor>\n"
 "  -V <version>    set image version to <version>\n"
 "  -v <version>    set firmware version to <version>\n"
+"  -m <version>    set header 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"
@@ -700,13 +709,13 @@ static int check_options(void)
                } else {
                        exceed_bytes = kernel_info.file_size - (rootfs_ofs - sizeof(struct fw_header));
                        if (exceed_bytes > 0) {
-                               ERR("kernel image is too big");
+                               ERR("kernel image is too big by %i bytes", exceed_bytes);
                                return -1;
                        }
 
                        exceed_bytes = rootfs_info.file_size - (fw_max_len - rootfs_ofs);
                        if (exceed_bytes > 0) {
-                               ERR("rootfs image is too big");
+                               ERR("rootfs image is too big by %i bytes", exceed_bytes);
                                return -1;
                        }
                }
@@ -723,6 +732,15 @@ static int check_options(void)
                return -1;
        }
 
+       if (opt_hdr_ver == 1) {
+               hdr_ver = HEADER_VERSION_V1;
+       } else if (opt_hdr_ver == 2) {
+               hdr_ver = HEADER_VERSION_V2;
+       } else {
+               ERR("invalid header version '%u'", opt_hdr_ver);
+               return -1;
+       }
+
        return 0;
 }
 
@@ -732,7 +750,7 @@ static void fill_header(char *buf, int len)
 
        memset(hdr, 0, sizeof(struct fw_header));
 
-       hdr->version = htonl(HEADER_VERSION_V1);
+       hdr->version = htonl(hdr_ver);
        strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
        strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
        hdr->hw_id = htonl(hw_id);
@@ -972,8 +990,9 @@ 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_V1) {
-               ERR("file does not seem to have V1 header!\n");
+       if ((ntohl(hdr->version) != HEADER_VERSION_V1) &&
+           (ntohl(hdr->version) != HEADER_VERSION_V2)) {
+               ERR("file does not seem to have V1/V2 header!\n");
                goto out_free_buf;
        }
 
@@ -1108,7 +1127,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:xX:hsSjv:");
+               c = getopt(argc, argv, "a:B:H:E:F:L:m:V:N:W:ci:k:r:R:o:xX:hsSjv:");
                if (c == -1)
                        break;
 
@@ -1134,6 +1153,9 @@ int main(int argc, char *argv[])
                case 'L':
                        sscanf(optarg, "0x%x", &kernel_la);
                        break;
+               case 'm':
+                       sscanf(optarg, "%u", &opt_hdr_ver);
+                       break;
                case 'V':
                        version = optarg;
                        break;