From 760e4b8400fde296a7b6139157125b1feb5be910 Mon Sep 17 00:00:00 2001 From: INAGAKI Hiroshi Date: Sat, 31 Oct 2020 21:45:05 +0900 Subject: [PATCH] firmware-utils: fix mistake and improve logic in nec-enc this patch fixes/improves follows: - PATTERN_LEN is defined as a macro but unused - redundant logic in count-up for "ptn" Signed-off-by: INAGAKI Hiroshi --- tools/firmware-utils/Makefile | 2 +- tools/firmware-utils/src/nec-enc.c | 8 +- tools/firmware-utils/src/tplink-safeloader.c | 327 +++++++++++++++++-- 3 files changed, 303 insertions(+), 34 deletions(-) diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile index 4fcda7e439..81c62d977a 100644 --- a/tools/firmware-utils/Makefile +++ b/tools/firmware-utils/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk PKG_NAME := firmware-utils -PKG_RELEASE := 2 +PKG_RELEASE := 5 include $(INCLUDE_DIR)/host-build.mk include $(INCLUDE_DIR)/kernel.mk diff --git a/tools/firmware-utils/src/nec-enc.c b/tools/firmware-utils/src/nec-enc.c index 3c4e38721e..a2be378586 100644 --- a/tools/firmware-utils/src/nec-enc.c +++ b/tools/firmware-utils/src/nec-enc.c @@ -47,7 +47,7 @@ static unsigned char buf_pattern[4096], buf[4096]; int main(int argc, char **argv) { - int k_off = 0, ptn = 0, c, ret = EXIT_SUCCESS; + int k_off = 0, ptn = 1, c, ret = EXIT_SUCCESS; char *ifn = NULL, *ofn = NULL, *key = NULL; size_t n, k_len; FILE *out, *in; @@ -99,11 +99,11 @@ int main(int argc, char **argv) while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { for (int i = 0; i < n; i++) { - buf_pattern[i] = ptn + 1; + buf_pattern[i] = ptn; ptn++; - if (ptn > 250) - ptn = 0; + if (ptn > PATTERN_LEN) + ptn = 1; } k_off = xor_pattern(buf_pattern, n, key, k_len, k_off); diff --git a/tools/firmware-utils/src/tplink-safeloader.c b/tools/firmware-utils/src/tplink-safeloader.c index 24ba8b555e..22427a67e5 100644 --- a/tools/firmware-utils/src/tplink-safeloader.c +++ b/tools/firmware-utils/src/tplink-safeloader.c @@ -77,6 +77,7 @@ struct device_info { const char *support_list; char support_trail; const char *soft_ver; + uint32_t soft_ver_compat_level; struct flash_partition_entry partitions[MAX_PARTITIONS+1]; const char *first_sysupgrade_partition; const char *last_sysupgrade_partition; @@ -84,7 +85,7 @@ struct device_info { /** The content of the soft-version structure */ struct __attribute__((__packed__)) soft_version { - uint32_t magic; + uint32_t data_len; uint32_t zero; uint8_t pad1; uint8_t version_major; @@ -95,7 +96,6 @@ struct __attribute__((__packed__)) soft_version { uint8_t month; uint8_t day; uint32_t rev; - uint8_t pad2; }; @@ -140,8 +140,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -263,8 +263,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -342,8 +342,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -384,8 +384,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -426,8 +426,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -466,8 +466,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -506,8 +506,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -538,8 +538,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -570,8 +570,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -603,8 +603,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -636,8 +636,8 @@ static struct device_info boards[] = { {"default-mac", 0x30000, 0x00020}, {"product-info", 0x31100, 0x00100}, {"signature", 0x32000, 0x00400}, - {"os-image", 0x40000, 0x200000}, - {"file-system", 0x240000, 0x570000}, + {"os-image", 0x40000, 0x300000}, + {"file-system", 0x340000, 0x470000}, {"soft-version", 0x7b0000, 0x00100}, {"support-list", 0x7b1000, 0x00400}, {"user-config", 0x7c0000, 0x10000}, @@ -1298,6 +1298,67 @@ static struct device_info boards[] = { .last_sysupgrade_partition = "file-system" }, + /** Firmware layout for the EAP225-Wall v2 */ + { + .id = "EAP225-WALL-V2", + .support_list = + "SupportList:\r\n" + "EAP225-Wall(TP-Link|UN|AC1200-D):2.0\r\n", + .support_trail = '\xff', + .soft_ver = NULL, + .soft_ver_compat_level = 1, + + .partitions = { + {"fs-uboot", 0x00000, 0x20000}, + {"partition-table", 0x20000, 0x02000}, + {"default-mac", 0x30000, 0x01000}, + {"support-list", 0x31000, 0x00100}, + {"product-info", 0x31100, 0x00400}, + {"soft-version", 0x32000, 0x00100}, + {"firmware", 0x40000, 0xd80000}, + {"user-config", 0xdc0000, 0x30000}, + {"mutil-log", 0xf30000, 0x80000}, + {"oops", 0xfb0000, 0x40000}, + {"radio", 0xff0000, 0x10000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + + /** Firmware layout for the EAP245 v3 */ + { + .id = "EAP245-V3", + .support_list = + "SupportList:\r\n" + "EAP245(TP-Link|UN|AC1750-D):3.0\r\n", + .support_trail = '\xff', + .soft_ver = NULL, + .soft_ver_compat_level = 1, + + /** Firmware partition with dynamic kernel/rootfs split */ + .partitions = { + {"factroy-boot", 0x00000, 0x40000}, + {"fs-uboot", 0x40000, 0x40000}, + {"partition-table", 0x80000, 0x10000}, + {"default-mac", 0x90000, 0x01000}, + {"support-list", 0x91000, 0x00100}, + {"product-info", 0x91100, 0x00400}, + {"soft-version", 0x92000, 0x00100}, + {"radio", 0xa0000, 0x10000}, + {"extra-para", 0xb0000, 0x10000}, + {"firmware", 0xc0000, 0xe40000}, + {"config", 0xf00000, 0x30000}, + {"mutil-log", 0xf30000, 0x80000}, + {"oops", 0xfb0000, 0x40000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + /** Firmware layout for the TL-WA850RE v2 */ { .id = "TLWA850REV2", @@ -1380,6 +1441,113 @@ static struct device_info boards[] = { .last_sysupgrade_partition = "file-system" }, + /** Firmware layout for the TL-WPA8630P v2 (EU)*/ + { + .id = "TL-WPA8630P-V2.0-EU", + .vendor = "", + .support_list = + "SupportList:\n" + "{product_name:TL-WPA8630P,product_ver:2.0.0,special_id:45550000}\n", + .support_trail = '\x00', + .soft_ver = NULL, + + .partitions = { + {"factory-uboot", 0x00000, 0x20000}, + {"fs-uboot", 0x20000, 0x20000}, + {"firmware", 0x40000, 0x5e0000}, + {"partition-table", 0x620000, 0x02000}, + {"default-mac", 0x630000, 0x00020}, + {"pin", 0x630100, 0x00020}, + {"device-id", 0x630200, 0x00030}, + {"product-info", 0x631100, 0x01000}, + {"extra-para", 0x632100, 0x01000}, + {"soft-version", 0x640000, 0x01000}, + {"support-list", 0x641000, 0x01000}, + {"profile", 0x642000, 0x08000}, + {"user-config", 0x650000, 0x10000}, + {"default-config", 0x660000, 0x10000}, + {"default-nvm", 0x670000, 0xc0000}, + {"default-pib", 0x730000, 0x40000}, + {"radio", 0x7f0000, 0x10000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + + /** Firmware layout for the TL-WPA8630P v2 (INT)*/ + { + .id = "TL-WPA8630P-V2-INT", + .vendor = "", + .support_list = + "SupportList:\n" + "{product_name:TL-WPA8630P,product_ver:2.0.0,special_id:41550000}\n" + "{product_name:TL-WPA8630P,product_ver:2.0.0,special_id:44450000}\n" + "{product_name:TL-WPA8630P,product_ver:2.1.0,special_id:41550000}\n", + .support_trail = '\x00', + .soft_ver = NULL, + + .partitions = { + {"factory-uboot", 0x00000, 0x20000}, + {"fs-uboot", 0x20000, 0x20000}, + {"firmware", 0x40000, 0x5e0000}, + {"partition-table", 0x620000, 0x02000}, + {"extra-para", 0x632100, 0x01000}, + {"soft-version", 0x640000, 0x01000}, + {"support-list", 0x641000, 0x01000}, + {"profile", 0x642000, 0x08000}, + {"user-config", 0x650000, 0x10000}, + {"default-config", 0x660000, 0x10000}, + {"default-nvm", 0x670000, 0xc0000}, + {"default-pib", 0x730000, 0x40000}, + {"default-mac", 0x7e0000, 0x00020}, + {"pin", 0x7e0100, 0x00020}, + {"device-id", 0x7e0200, 0x00030}, + {"product-info", 0x7e1100, 0x01000}, + {"radio", 0x7f0000, 0x10000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + + /** Firmware layout for the TL-WPA8630P v2.1 (EU)*/ + { + .id = "TL-WPA8630P-V2.1-EU", + .vendor = "", + .support_list = + "SupportList:\n" + "{product_name:TL-WPA8630P,product_ver:2.1.0,special_id:45550000}\n", + .support_trail = '\x00', + .soft_ver = NULL, + + .partitions = { + {"factory-uboot", 0x00000, 0x20000}, + {"fs-uboot", 0x20000, 0x20000}, + {"firmware", 0x40000, 0x5e0000}, + {"extra-para", 0x680000, 0x01000}, + {"product-info", 0x690000, 0x01000}, + {"partition-table", 0x6a0000, 0x02000}, + {"soft-version", 0x6b0000, 0x01000}, + {"support-list", 0x6b1000, 0x01000}, + {"profile", 0x6b2000, 0x08000}, + {"user-config", 0x6c0000, 0x10000}, + {"default-config", 0x6d0000, 0x10000}, + {"default-nvm", 0x6e0000, 0xc0000}, + {"default-pib", 0x7a0000, 0x40000}, + {"default-mac", 0x7e0000, 0x00020}, + {"pin", 0x7e0100, 0x00020}, + {"device-id", 0x7e0200, 0x00030}, + {"radio", 0x7f0000, 0x10000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + /** Firmware layout for the TL-WR1043 v5 */ { .id = "TLWR1043NV5", @@ -1562,6 +1730,94 @@ static struct device_info boards[] = { .last_sysupgrade_partition = "file-system" }, + /** Firmware layout for the RE200 v3 */ + { + .id = "RE200-V3", + .vendor = "", + .support_list = + "SupportList:\n" + "{product_name:RE200,product_ver:3.0.0,special_id:00000000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:41520000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:41550000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:42520000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:43410000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:45470000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:45530000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:45550000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:49440000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:4A500000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:4B520000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:52550000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:54570000}\n" + "{product_name:RE200,product_ver:3.0.0,special_id:55530000}\n", + .support_trail = '\x00', + .soft_ver = NULL, + + .partitions = { + {"fs-uboot", 0x00000, 0x20000}, + {"firmware", 0x20000, 0x7a0000}, + {"partition-table", 0x7c0000, 0x02000}, + {"default-mac", 0x7c2000, 0x00020}, + {"pin", 0x7c2100, 0x00020}, + {"product-info", 0x7c3100, 0x01000}, + {"soft-version", 0x7c4200, 0x01000}, + {"support-list", 0x7c5200, 0x01000}, + {"profile", 0x7c6200, 0x08000}, + {"config-info", 0x7ce200, 0x00400}, + {"user-config", 0x7d0000, 0x10000}, + {"default-config", 0x7e0000, 0x10000}, + {"radio", 0x7f0000, 0x10000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + + /** Firmware layout for the RE200 v4 */ + { + .id = "RE200-V4", + .vendor = "", + .support_list = + "SupportList:\n" + "{product_name:RE200,product_ver:4.0.0,special_id:00000000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:45550000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:4A500000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:4B520000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:43410000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:41550000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:42520000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:55530000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:41520000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:52550000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:54570000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:45530000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:49440000}\n" + "{product_name:RE200,product_ver:4.0.0,special_id:45470000}\n", + .support_trail = '\x00', + .soft_ver = "soft_ver:1.1.0\n", + + .partitions = { + {"fs-uboot", 0x00000, 0x20000}, + {"firmware", 0x20000, 0x7a0000}, + {"partition-table", 0x7c0000, 0x02000}, + {"default-mac", 0x7c2000, 0x00020}, + {"pin", 0x7c2100, 0x00020}, + {"product-info", 0x7c3100, 0x01000}, + {"soft-version", 0x7c4200, 0x01000}, + {"support-list", 0x7c5200, 0x01000}, + {"profile", 0x7c6200, 0x08000}, + {"config-info", 0x7ce200, 0x00400}, + {"user-config", 0x7d0000, 0x10000}, + {"default-config", 0x7e0000, 0x10000}, + {"radio", 0x7f0000, 0x10000}, + {NULL, 0, 0} + }, + + .first_sysupgrade_partition = "os-image", + .last_sysupgrade_partition = "file-system" + }, + /** Firmware layout for the RE220 v2 */ { .id = "RE220-V2", @@ -2024,8 +2280,13 @@ static inline uint8_t bcd(uint8_t v) { /** Generates the soft-version partition */ -static struct image_partition_entry make_soft_version(uint32_t rev) { - struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version)); +static struct image_partition_entry make_soft_version(struct device_info *info, uint32_t rev) { + size_t part_len = sizeof(struct soft_version); + if (info->soft_ver_compat_level > 0) + part_len += sizeof(uint32_t); + + struct image_partition_entry entry = + alloc_image_partition("soft-version", part_len+1); struct soft_version *s = (struct soft_version *)entry.data; time_t t; @@ -2037,7 +2298,8 @@ static struct image_partition_entry make_soft_version(uint32_t rev) { struct tm *tm = gmtime(&t); - s->magic = htonl(0x0000000c); + /* Partition contents size, minus 8 byte header and trailing byte */ + s->data_len = htonl(entry.size-9); s->zero = 0; s->pad1 = 0xff; @@ -2051,7 +2313,11 @@ static struct image_partition_entry make_soft_version(uint32_t rev) { s->day = bcd(tm->tm_mday); s->rev = htonl(rev); - s->pad2 = 0xff; + if (info->soft_ver_compat_level > 0) + *(uint32_t *)(entry.data + sizeof(struct soft_version)) = + htonl(info->soft_ver_compat_level); + + entry.data[entry.size-1] = 0xff; return entry; } @@ -2363,7 +2629,7 @@ static void build_image(const char *output, if (info->soft_ver) parts[1] = make_soft_version_from_string(info->soft_ver); else - parts[1] = make_soft_version(rev); + parts[1] = make_soft_version(info, rev); parts[2] = make_support_list(info); parts[3] = read_file("os-image", kernel_image, false, NULL); @@ -2387,6 +2653,9 @@ static void build_image(const char *output, } else if (strcasecmp(info->id, "ARCHER-C6-V2-US") == 0) { const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00}; parts[5] = put_data("extra-para", mdat, 11); + } else if (strcasecmp(info->id, "EAP245-V3") == 0) { + const char mdat[10] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}; + parts[5] = put_data("extra-para", mdat, 10); } size_t len;