tplink-safeloader: use enum for table types
[project/firmware-utils.git] / src / tplink-safeloader.c
index 6f1ad2c4a91961b68a2e92a87789929a9443a981..79aefd973232941fbc1dfe8d89ba3fefe8c1d3ed 100644 (file)
@@ -468,6 +468,41 @@ static struct device_info boards[] = {
                .last_sysupgrade_partition = "support-list",
        },
 
+       /** Firmware layout for the CPE605V1 */
+       {
+               .id     = "CPE605V1",
+               .vendor = "CPE605(TP-LINK|UN|N150-5):1.0\r\n",
+               .support_list =
+                       "SupportList:\r\n"
+                       "CPE605(TP-LINK|UN|N150-5|00000000):1.0\r\n"
+                       "CPE605(TP-LINK|EU|N150-5|45550000):1.0\r\n"
+                       "CPE605(TP-LINK|US|N150-5|55530000):1.0\r\n",
+               .part_trail = 0x00,
+               .soft_ver = SOFT_VER_DEFAULT,
+
+               .partitions = {
+                       {"fs-uboot", 0x00000, 0x20000},
+                       {"partition-table", 0x20000, 0x02000},
+                       {"default-mac", 0x30000, 0x00020},
+                       {"serial-number", 0x30100, 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 CPE610V1 */
        {
                .id     = "CPE610V1",
@@ -1577,6 +1612,85 @@ static struct device_info boards[] = {
                .last_sysupgrade_partition = "file-system",
        },
 
+       /** Firmware layout for the Deco M4R v4 */
+       {
+               .id     = "DECO-M4R-V4",
+               .vendor = "",
+               .support_list =
+                       "SupportList:\n"
+                       "{product_name:M4R,product_ver:4.0.0,special_id:55530000}\n"
+                       "{product_name:M4R,product_ver:4.0.0,special_id:45550000}\n"
+                       "{product_name:M4R,product_ver:4.0.0,special_id:4A500000}\n"
+                       "{product_name:M4R,product_ver:4.0.0,special_id:42340000}\n"
+                       "{product_name:M4R,product_ver:4.0.0,special_id:5A470000}\n",
+               .part_trail = 0x00,
+               .soft_ver = SOFT_VER_TEXT("soft_ver:1.0.0\n"),
+
+               .partitions = {
+                       {"fs-uboot", 0x00000, 0x40000},
+                       {"firmware", 0x40000, 0xf60000},
+                       {"default-mac", 0xfa0000, 0x00300},
+                       {"device-id", 0xfa0300, 0x00100},
+                       {"product-info", 0xfa0400, 0x0fc00},
+                       {"group-info", 0xfb0000, 0x04000},
+                       {"user-config", 0xfb4000, 0x0c000},
+                       {"device-config", 0xfc0000, 0x10000},
+                       {"default-config", 0xfd0000, 0x10000},
+                       {"partition-table", 0xfe0000, 0x00800},
+                       {"soft-version", 0xfe0800, 0x00100},
+                       {"support-list", 0xfe0900, 0x00200},
+                       {"profile", 0xfe0b00, 0x03000},
+                       {"extra-para", 0xfe3b00, 0x00100},
+                       {"radio", 0xff0000, 0x10000},
+                       {NULL, 0, 0}
+               },
+               .first_sysupgrade_partition = "os-image",
+               .last_sysupgrade_partition = "file-system",
+       },
+
+       /** Firmware layout for the Deco S4 v2 */
+       {
+               .id     = "DECO-S4-V2",
+               .vendor = "",
+               .support_list =
+                       "SupportList:\n"
+                       "{product_name:S4,product_ver:1.0.0,special_id:55530000}\n"
+                       "{product_name:S4,product_ver:1.0.0,special_id:45550000}\n"
+                       "{product_name:S4,product_ver:1.0.0,special_id:43410000}\n"
+                       "{product_name:S4,product_ver:1.0.0,special_id:4A500000}\n"
+                       "{product_name:S4,product_ver:1.0.0,special_id:41550000}\n"
+                       "{product_name:S4,product_ver:1.0.0,special_id:4B520000}\n"
+                       "{product_name:S4,product_ver:2.0.0,special_id:55530000}\n"
+                       "{product_name:S4,product_ver:2.0.0,special_id:45550000}\n"
+                       "{product_name:S4,product_ver:2.0.0,special_id:43410000}\n"
+                       "{product_name:S4,product_ver:2.0.0,special_id:4A500000}\n"
+                       "{product_name:S4,product_ver:2.0.0,special_id:41550000}\n"
+                       "{product_name:S4,product_ver:2.0.0,special_id:4B520000}\n",
+               .part_trail = 0x00,
+               .soft_ver = SOFT_VER_DEFAULT,
+
+               .partitions = {
+                       {"fs-uboot", 0x00000, 0x80000},
+                       {"product-info", 0x80000, 0x05000},
+                       {"default-mac", 0x85000, 0x01000},
+                       {"device-id", 0x86000, 0x01000},
+                       {"support-list", 0x87000, 0x10000},
+                       {"user-config", 0xa7000, 0x10000},
+                       {"device-config", 0xb7000, 0x10000},
+                       {"group-info", 0xc7000, 0x10000},
+                       {"partition-table", 0xd7000, 0x02000},
+                       {"soft-version", 0xd9000, 0x10000},
+                       {"profile", 0xe9000, 0x10000},
+                       {"default-config", 0xf9000, 0x10000},
+                       {"url-sig", 0x1e0000, 0x10000},
+                       {"radio", 0x1f0000, 0x10000},
+                       {"firmware", 0x200000, 0xe00000},
+                       {NULL, 0, 0}
+               },
+               .first_sysupgrade_partition = "os-image",
+               .last_sysupgrade_partition = "file-system",
+       },
+
        /** Firmware layout for the EAP120 */
        {
                .id     = "EAP120",
@@ -1676,8 +1790,8 @@ static struct device_info boards[] = {
                        "EAP225(TP-Link|UN|AC1350-D):3.0\r\n"
                        "EAP225(TP-Link|UN|AC1350-D):3.20\r\n"
                        "EAP225(TP-Link|UN|AC1350-D):4.0 CA\r\n"
-                       "EAP225-Outdoor(TP-Link|UN|AC1350-D):1.0\r\n"
-                       "EAP225-Outdoor(TP-Link|UN|AC1350-D):3.0 CA,JP\r\n",
+                       "EAP225-Outdoor(TP-Link|UN|AC1200-D):1.0\r\n"
+                       "EAP225-Outdoor(TP-Link|UN|AC1200-D):3.0 CA,JP\r\n",
                .part_trail = PART_TRAIL_NONE,
                .soft_ver = SOFT_VER_DEFAULT,
                .soft_ver_compat_level = 1,
@@ -2916,6 +3030,46 @@ static struct device_info boards[] = {
                .last_sysupgrade_partition = "file-system"
        },
 
+       /** Firmware layout for the Mercusys MR70X */
+       {
+               .id     = "MR70X",
+               .vendor = "",
+               .support_list =
+                       "SupportList:\n"
+                       "{product_name:MR70X,product_ver:1.0.0,special_id:45550000}\n"
+                       "{product_name:MR70X,product_ver:1.0.0,special_id:4A500000}\n"
+                       "{product_name:MR70X,product_ver:1.0.0,special_id:55530000}\n",
+               .part_trail = 0x00,
+               .soft_ver = SOFT_VER_DEFAULT,
+
+               .partitions = {
+                       {"fs-uboot", 0x00000, 0x40000},
+                       {"firmware", 0x40000, 0xf60000},
+                       {"default-mac", 0xfa0000, 0x00200},
+                       {"pin", 0xfa0200, 0x00100},
+                       {"device-id", 0xfa0300, 0x00100},
+                       {"product-info", 0xfa0400, 0x0fc00},
+                       {"default-config", 0xfb0000, 0x08000},
+                       {"ap-def-config", 0xfb8000, 0x08000},
+                       {"user-config", 0xfc0000, 0x0a000},
+                       {"ag-config", 0xfca000, 0x04000},
+                       {"certificate", 0xfce000, 0x02000},
+                       {"ap-config", 0xfd0000, 0x06000},
+                       {"router-config", 0xfd6000, 0x06000},
+                       {"favicon", 0xfdc000, 0x02000},
+                       {"logo", 0xfde000, 0x02000},
+                       {"partition-table", 0xfe0000, 0x00800},
+                       {"soft-version", 0xfe0800, 0x00100},
+                       {"support-list", 0xfe0900, 0x00200},
+                       {"profile", 0xfe0b00, 0x03000},
+                       {"extra-para", 0xfe3b00, 0x00100},
+                       {"radio", 0xff0000, 0x10000},
+                       {NULL, 0, 0}
+               },
+
+               .first_sysupgrade_partition = "os-image",
+               .last_sysupgrade_partition = "file-system"
+       },
 
        {}
 };
@@ -3002,8 +3156,15 @@ static void set_partition_names(struct device_info *info)
 }
 
 /** Frees an image partition */
-static void free_image_partition(struct image_partition_entry entry) {
-       free(entry.data);
+static void free_image_partition(struct image_partition_entry *entry)
+{
+       void *data = entry->data;
+
+       entry->name = NULL;
+       entry->size = 0;
+       entry->data = NULL;
+
+       free(data);
 }
 
 static time_t source_date_epoch = -1;
@@ -3407,6 +3568,8 @@ static void build_image(const char *output,
            strcasecmp(info->id, "ARCHER-C60-V3") == 0 ||
            strcasecmp(info->id, "ARCHER-C6U-V1") == 0 ||
            strcasecmp(info->id, "ARCHER-C6-V3") == 0 ||
+           strcasecmp(info->id, "DECO-M4R-V4") == 0 ||
+           strcasecmp(info->id, "MR70X") == 0 ||
            strcasecmp(info->id, "TLWR1043NV5") == 0) {
                const uint8_t extra_para[2] = {0x01, 0x00};
                parts[5] = make_extra_para(info, extra_para,
@@ -3442,7 +3605,7 @@ static void build_image(const char *output,
        free(image);
 
        for (i = 0; parts[i].name; i++)
-               free_image_partition(parts[i]);
+               free_image_partition(&parts[i]);
 }
 
 /** Usage output */
@@ -3516,6 +3679,11 @@ static int add_flash_partition(
 }
 
 /** read the partition table into struct flash_partition_entry */
+enum PARTITION_TABLE_TYPE {
+       PARTITION_TABLE_FWUP,
+       PARTITION_TABLE_FLASH,
+};
+
 static int read_partition_table(
                FILE *file, long offset,
                struct flash_partition_entry *entries, size_t max_entries,
@@ -3530,14 +3698,14 @@ static int read_partition_table(
        /* TODO: search for the partition table */
 
        switch(type) {
-               case 0:
-                       parthdr = fwuphdr;
-                       break;
-               case 1:
-                       parthdr = flashhdr;
-                       break;
-               default:
-                       error(1, 0, "Invalid partition table");
+       case PARTITION_TABLE_FWUP:
+               parthdr = fwuphdr;
+               break;
+       case PARTITION_TABLE_FLASH:
+               parthdr = flashhdr;
+               break;
+       default:
+               error(1, 0, "Invalid partition table");
        }
 
        if (fseek(file, offset, SEEK_SET) < 0)
@@ -3694,9 +3862,8 @@ static int extract_firmware(const char *input, const char *output_directory)
 
        input_file = fopen(input, "rb");
 
-       if (read_partition_table(input_file, firmware_offset, entries, 16, 0) != 0) {
+       if (read_partition_table(input_file, firmware_offset, entries, 16, PARTITION_TABLE_FWUP) != 0)
                error(1, 0, "Error can not read the partition table (fwup-ptn)");
-       }
 
        for (size_t i = 0; i < max_entries; i++) {
                if (entries[i].name == NULL &&
@@ -3735,9 +3902,8 @@ static int firmware_info(const char *input)
 
        fp = fopen(input, "r");
 
-       if (read_partition_table(fp, 0x1014, pointers, MAX_PARTITIONS, 0)) {
+       if (read_partition_table(fp, 0x1014, pointers, MAX_PARTITIONS, PARTITION_TABLE_FWUP))
                error(1, 0, "Error can not read the partition table (fwup-ptn)");
-       }
 
        printf("Firmware image partitions:\n");
        printf("%-8s %-8s %s\n", "base", "size", "name");
@@ -3817,11 +3983,11 @@ static int firmware_info(const char *input)
 
        e = find_partition(pointers, MAX_PARTITIONS, "partition-table", NULL);
        if (e) {
+               size_t flash_table_offset = 0x1014 + e->base + 4;
                struct flash_partition_entry parts[MAX_PARTITIONS] = { };
 
-               if (read_partition_table(fp, 0x1014 + e->base + 4, parts, MAX_PARTITIONS, 1)) {
+               if (read_partition_table(fp, flash_table_offset, parts, MAX_PARTITIONS, PARTITION_TABLE_FLASH))
                        error(1, 0, "Error can not read the partition table (partition)");
-               }
 
                printf("\n[Partition table]\n");
                printf("%-8s %-8s %s\n", "base", "size", "name");
@@ -3869,6 +4035,7 @@ static void convert_firmware(const char *input, const char *output)
        struct flash_partition_entry *fwup_partition_table = NULL;
        size_t firmware_offset = 0x1014;
        FILE *input_file, *output_file;
+       size_t flash_table_offset;
 
        struct stat statbuf;
 
@@ -3897,7 +4064,8 @@ static void convert_firmware(const char *input, const char *output)
                        "partition-table", "Error can not find partition-table partition");
 
        /* the flash partition table has a 0x00000004 magic haeder */
-       if (read_partition_table(input_file, firmware_offset + fwup_partition_table->base + 4, flash, MAX_PARTITIONS, 1) != 0)
+       flash_table_offset = firmware_offset + fwup_partition_table->base + 4;
+       if (read_partition_table(input_file, flash_table_offset, flash, MAX_PARTITIONS, PARTITION_TABLE_FLASH) != 0)
                error(1, 0, "Error can not read the partition table (flash)");
 
        flash_os_image = find_partition(flash, MAX_PARTITIONS,