ath79: add support for TP-Link Archer C60 v3
[openwrt/openwrt.git] / tools / firmware-utils / src / tplink-safeloader.c
index f6e70e29db4c2006c9c4e5a23044a3913b2403ff..b6ad5c0869d3ce0f15e456b0a20e9c8f939de3ab 100644 (file)
@@ -231,7 +231,48 @@ static struct device_info boards[] = {
                .last_sysupgrade_partition = "support-list",
        },
 
-       /** Firmware layout for the CPE510/520 */
+       /** Firmware layout for the CPE220 V3 */
+       {
+               .id     = "CPE220V3",
+               .vendor = "CPE220(TP-LINK|UN|N300-2|00000000):3.0\r\n",
+               .support_list =
+                       "SupportList:\r\n"
+                       "CPE220(TP-LINK|EU|N300-2|00000000):3.0\r\n"
+                       "CPE220(TP-LINK|EU|N300-2|45550000):3.0\r\n"
+                       "CPE220(TP-LINK|EU|N300-2|55530000):3.0\r\n"
+                       "CPE220(TP-LINK|UN|N300-2|00000000):3.0\r\n"
+                       "CPE220(TP-LINK|UN|N300-2|45550000):3.0\r\n"
+                       "CPE220(TP-LINK|UN|N300-2|55530000):3.0\r\n"
+                       "CPE220(TP-LINK|US|N300-2|55530000):3.0\r\n"
+                       "CPE220(TP-LINK|UN|N300-2):3.0\r\n"
+                       "CPE220(TP-LINK|EU|N300-2):3.0\r\n"
+                       "CPE220(TP-LINK|US|N300-2):3.0\r\n",
+               .support_trail = '\xff',
+               .soft_ver = NULL,
+
+               .partitions = {
+                       {"fs-uboot", 0x00000, 0x20000},
+                       {"partition-table", 0x20000, 0x02000},
+                       {"default-mac", 0x30000, 0x00020},
+                       {"product-info", 0x31100, 0x00100},
+                       {"device-info", 0x31400, 0x00400},
+                       {"signature", 0x32000, 0x00400},
+                       {"device-id", 0x33000, 0x00100},
+                       {"firmware", 0x40000, 0x770000},
+                       {"soft-version", 0x7b0000, 0x00100},
+                       {"support-list", 0x7b1000, 0x01000},
+                       {"user-config", 0x7c0000, 0x10000},
+                       {"default-config", 0x7d0000, 0x10000},
+                       {"log", 0x7e0000, 0x10000},
+                       {"radio", 0x7f0000, 0x10000},
+                       {NULL, 0, 0}
+               },
+
+               .first_sysupgrade_partition = "os-image",
+               .last_sysupgrade_partition = "support-list",
+       },
+
+       /** Firmware layout for the CPE510/520 V1 */
        {
                .id     = "CPE510",
                .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
@@ -244,13 +285,45 @@ static struct device_info boards[] = {
                        "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
                        "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
                        "CPE520(TP-LINK|US|N300-5):1.1\r\n"
-                       "CPE520(TP-LINK|EU|N300-5):1.1\r\n"
+                       "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
+               .support_trail = '\xff',
+               .soft_ver = NULL,
+
+               .partitions = {
+                       {"fs-uboot", 0x00000, 0x20000},
+                       {"partition-table", 0x20000, 0x02000},
+                       {"default-mac", 0x30000, 0x00020},
+                       {"product-info", 0x31100, 0x00100},
+                       {"signature", 0x32000, 0x00400},
+                       {"os-image", 0x40000, 0x1c0000},
+                       {"file-system", 0x200000, 0x5b0000},
+                       {"soft-version", 0x7b0000, 0x00100},
+                       {"support-list", 0x7b1000, 0x00400},
+                       {"user-config", 0x7c0000, 0x10000},
+                       {"default-config", 0x7d0000, 0x10000},
+                       {"log", 0x7e0000, 0x10000},
+                       {"radio", 0x7f0000, 0x10000},
+                       {NULL, 0, 0}
+               },
+
+               .first_sysupgrade_partition = "os-image",
+               .last_sysupgrade_partition = "support-list",
+       },
+
+       /** Firmware layout for the CPE510 V2 */
+       {
+               .id     = "CPE510V2",
+               .vendor = "CPE510(TP-LINK|UN|N300-5):2.0\r\n",
+               .support_list =
+                       "SupportList:\r\n"
                        "CPE510(TP-LINK|EU|N300-5|00000000):2.0\r\n"
                        "CPE510(TP-LINK|EU|N300-5|45550000):2.0\r\n"
                        "CPE510(TP-LINK|EU|N300-5|55530000):2.0\r\n"
                        "CPE510(TP-LINK|UN|N300-5|00000000):2.0\r\n"
                        "CPE510(TP-LINK|UN|N300-5|45550000):2.0\r\n"
                        "CPE510(TP-LINK|UN|N300-5|55530000):2.0\r\n"
+                       "CPE510(TP-LINK|US|N300-5|00000000):2.0\r\n"
+                       "CPE510(TP-LINK|US|N300-5|45550000):2.0\r\n"
                        "CPE510(TP-LINK|US|N300-5|55530000):2.0\r\n"
                        "CPE510(TP-LINK|UN|N300-5):2.0\r\n"
                        "CPE510(TP-LINK|EU|N300-5):2.0\r\n"
@@ -630,7 +703,7 @@ static struct device_info boards[] = {
                        "{product_name:Archer C6,product_ver:2.0.0,special_id:52550000}\r\n"
                        "{product_name:Archer C6,product_ver:2.0.0,special_id:4A500000}\r\n",
                .support_trail = '\x00',
-               .soft_ver = "soft_ver:1.0.0\n",
+               .soft_ver = "soft_ver:1.1.1\n",
 
                .partitions = {
                        {"fs-uboot", 0x00000, 0x20000},
@@ -726,6 +799,42 @@ static struct device_info boards[] = {
                .last_sysupgrade_partition = "file-system",
        },
 
+       /** Firmware layout for the C60v3 */
+       {
+               .id     = "ARCHER-C60-V3",
+               .vendor = "",
+               .support_list =
+                       "SupportList:\r\n"
+                       "{product_name:Archer C60,product_ver:3.0.0,special_id:42520000}\r\n"
+                       "{product_name:Archer C60,product_ver:3.0.0,special_id:45550000}\r\n"
+                       "{product_name:Archer C60,product_ver:3.0.0,special_id:55530000}\r\n",
+               .support_trail = '\x00',
+               .soft_ver = "soft_ver:3.0.0\n",
+
+               .partitions = {
+                       {"factory-boot", 0x00000, 0x1fb00},
+                       {"default-mac", 0x1fb00, 0x00200},
+                       {"pin", 0x1fd00, 0x00100},
+                       {"product-info", 0x1fe00, 0x00100},
+                       {"device-id", 0x1ff00, 0x00100},
+                       {"fs-uboot", 0x20000, 0x10000},
+                       {"firmware", 0x30000, 0x7a0000},
+                       {"soft-version", 0x7d9500, 0x00100},
+                       {"support-list", 0x7d9600, 0x00100},
+                       {"extra-para", 0x7d9700, 0x00100},
+                       {"profile", 0x7d9800, 0x03000},
+                       {"default-config", 0x7dc800, 0x03000},
+                       {"partition-table", 0x7df800, 0x00800},
+                       {"user-config", 0x7e0000, 0x0c000},
+                       {"certificate", 0x7ec000, 0x04000},
+                       {"radio", 0x7f0000, 0x10000},
+                       {NULL, 0, 0}
+               },
+
+               .first_sysupgrade_partition = "os-image",
+               .last_sysupgrade_partition = "file-system",
+       },
+
        /** Firmware layout for the C5 */
        {
                .id = "ARCHER-C5-V2",
@@ -818,7 +927,8 @@ static struct device_info boards[] = {
                        "{product_name:Archer C7,product_ver:5.0.0,special_id:43410000}\n"
                        "{product_name:Archer C7,product_ver:5.0.0,special_id:4A500000}\n"
                        "{product_name:Archer C7,product_ver:5.0.0,special_id:54570000}\n"
-                       "{product_name:Archer C7,product_ver:5.0.0,special_id:52550000}\n",
+                       "{product_name:Archer C7,product_ver:5.0.0,special_id:52550000}\n"
+                       "{product_name:Archer C7,product_ver:5.0.0,special_id:4B520000}\n",
 
                .support_trail = '\x00',
                .soft_ver = "soft_ver:1.0.0\n",
@@ -1482,11 +1592,12 @@ static struct image_partition_entry read_file(const char *part_name, const char
 
        size_t len = statbuf.st_size;
 
-       if (add_jffs2_eof)
+       if (add_jffs2_eof) {
                if (file_system_partition)
                        len = ALIGN(len + file_system_partition->base, 0x10000) + sizeof(jffs2_eof_mark) - file_system_partition->base;
                else
                        len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
+       }
 
        struct image_partition_entry entry = alloc_image_partition(part_name, len);
 
@@ -1760,6 +1871,7 @@ static void build_image(const char *output,
            strcasecmp(info->id, "ARCHER-C25-V1") == 0 ||
            strcasecmp(info->id, "ARCHER-C59-V2") == 0 ||
            strcasecmp(info->id, "ARCHER-C60-V2") == 0 ||
+           strcasecmp(info->id, "ARCHER-C60-V3") == 0 ||
            strcasecmp(info->id, "TLWR1043NV5") == 0) {
                const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
                parts[5] = put_data("extra-para", mdat, 11);
@@ -1836,7 +1948,7 @@ static int add_flash_partition(
                unsigned long base,
                unsigned long size)
 {
-       int ptr;
+       size_t ptr;
        /* check if the list has a free entry */
        for (ptr = 0; ptr < max_entries; ptr++, part_list++) {
                if (part_list->name == NULL &&
@@ -1889,7 +2001,7 @@ static int read_partition_table(
        if (fseek(file, offset, SEEK_SET) < 0)
                error(1, errno, "Can not seek in the firmware");
 
-       if (fread(buf, 1, 2048, file) < 0)
+       if (fread(buf, 2048, 1, file) != 1)
                error(1, errno, "Can not read fwup-ptn from the firmware");
 
        buf[2047] = '\0';
@@ -1980,18 +2092,18 @@ static void write_partition(
        fseek(input_file, entry->base + firmware_offset, SEEK_SET);
 
        for (offset = 0; sizeof(buf) + offset <= entry->size; offset += sizeof(buf)) {
-               if (fread(buf, sizeof(buf), 1, input_file) < 0)
+               if (fread(buf, sizeof(buf), 1, input_file) != 1)
                        error(1, errno, "Can not read partition from input_file");
 
-               if (fwrite(buf, sizeof(buf), 1, output_file) < 0)
+               if (fwrite(buf, sizeof(buf), 1, output_file) != 1)
                        error(1, errno, "Can not write partition to output_file");
        }
        /* write last chunk smaller than buffer */
        if (offset < entry->size) {
                offset = entry->size - offset;
-               if (fread(buf, offset, 1, input_file) < 0)
+               if (fread(buf, offset, 1, input_file) != 1)
                        error(1, errno, "Can not read partition from input_file");
-               if (fwrite(buf, offset, 1, output_file) < 0)
+               if (fwrite(buf, offset, 1, output_file) != 1)
                        error(1, errno, "Can not write partition to output_file");
        }
 }
@@ -2044,7 +2156,7 @@ static int extract_firmware(const char *input, const char *output_directory)
                error(1, 0, "Error can not read the partition table (fwup-ptn)");
        }
 
-       for (int i = 0; i < max_entries; i++) {
+       for (size_t i = 0; i < max_entries; i++) {
                if (entries[i].name == NULL &&
                                entries[i].base == 0 &&
                                entries[i].size == 0)
@@ -2060,7 +2172,7 @@ static struct flash_partition_entry *find_partition(
                struct flash_partition_entry *entries, size_t max_entries,
                const char *name, const char *error_msg)
 {
-       for (int i = 0; i < max_entries; i++, entries++) {
+       for (size_t i = 0; i < max_entries; i++, entries++) {
                if (strcmp(entries->name, name) == 0)
                        return entries;
        }
@@ -2072,19 +2184,19 @@ static struct flash_partition_entry *find_partition(
 static void write_ff(FILE *output_file, size_t size)
 {
        char buf[4096];
-       int offset;
+       size_t offset;
 
        memset(buf, 0xff, sizeof(buf));
 
        for (offset = 0; offset + sizeof(buf) < size ; offset += sizeof(buf)) {
-               if (fwrite(buf, sizeof(buf), 1, output_file) < 0)
+               if (fwrite(buf, sizeof(buf), 1, output_file) != 1)
                        error(1, errno, "Can not write 0xff to output_file");
        }
 
        /* write last chunk smaller than buffer */
        if (offset < size) {
                offset = size - offset;
-               if (fwrite(buf, offset, 1, output_file) < 0)
+               if (fwrite(buf, offset, 1, output_file) != 1)
                        error(1, errno, "Can not write partition to output_file");
        }
 }