Re: [OpenWrt-Devel] [PATCH 3/4] firmware-utils: kernel image generator for TP-Link RE450

2016-01-19 Thread Tal Keren
On 19/01/2016 11:30, John Crispin wrote:
> can this not be merged into one of the other tplink tools ?
>
>

That is what I initially did, add a flag for this case. But then I had to 
disable about 3/4 of the original code with if/else for this case. It doesn't 
use most of the functionally that mktplinkfw provides (It doesn't search for 
and use the flash layout, it doesn't write the jffs section, it doesn't write 
most of the fields in the header, it ignores some of the flags because they are 
irrelevant for it).

So I think that creating a new and (very) stripped down version of it is 
simpler.

For reference, this is my original patch. What do you think that is better?


--- a/tools/firmware-utils/src/mktplinkfw.c
+++ b/tools/firmware-utils/src/mktplinkfw.c
@@ -154,6 +154,7 @@ static uint32_t rootfs_ofs = 0;
 static uint32_t rootfs_align;
 static struct file_info boot_info;
 static int combined;
+static int kernel_only;
 static int strip_padding;
 static int ignore_size;
 static int add_jffs2_eof;
@@ -518,6 +519,7 @@ static void usage(int status)
 "Options:\n"
 "  -B   create image for the board specified with \n"
 "  -c  use combined kernel image\n"
+"  -K  make image with only kernel\n"
 "  -E  overwrite kernel entry point with  (hexval prefixed 
with 0x)\n"
 "  -L  overwrite kernel load address with  (hexval prefixed 
with 0x)\n"
 "  -Huse hardware id specified with \n"
@@ -613,54 +615,61 @@ static int check_options(void)
 return -1;
 }
 
-if (board_id == NULL && opt_hw_id == NULL) {
-ERR("either board or hardware id must be specified");
-return -1;
-}
-
-if (board_id) {
-board = find_board(board_id);
-if (board == NULL) {
-ERR("unknown/unsupported board id \"%s\"", board_id);
+if (kernel_only) {
+if (!kernel_la || !kernel_ep) {
+ERR("kernel loading address and entry point must be specified");
 return -1;
 }
-if (layout_id == NULL)
-layout_id = board->layout_id;
-
-hw_id = board->hw_id;
-hw_rev = board->hw_rev;
 } else {
-if (layout_id == NULL) {
-ERR("flash layout is not specified");
+if (board_id == NULL && opt_hw_id == NULL) {
+ERR("either board or hardware id must be specified");
 return -1;
 }
-hw_id = strtoul(opt_hw_id, NULL, 0);
 
-if (opt_hw_rev)
-hw_rev = strtoul(opt_hw_rev, NULL, 0);
-else
-hw_rev = 1;
-}
+if (board_id) {
+board = find_board(board_id);
+if (board == NULL) {
+ERR("unknown/unsupported board id \"%s\"", board_id);
+return -1;
+}
+if (layout_id == NULL)
+layout_id = board->layout_id;
 
-layout = find_layout(layout_id);
-if (layout == NULL) {
-ERR("unknown flash layout \"%s\"", layout_id);
-return -1;
-}
+hw_id = board->hw_id;
+hw_rev = board->hw_rev;
+} else {
+if (layout_id == NULL) {
+ERR("flash layout is not specified");
+return -1;
+}
+hw_id = strtoul(opt_hw_id, NULL, 0);
 
-if (!kernel_la)
-kernel_la = layout->kernel_la;
-if (!kernel_ep)
-kernel_ep = layout->kernel_ep;
-if (!rootfs_ofs)
-rootfs_ofs = layout->rootfs_ofs;
+if (opt_hw_rev)
+hw_rev = strtoul(opt_hw_rev, NULL, 0);
+else
+hw_rev = 1;
+}
 
-if (reserved_space > layout->fw_max_len) {
-ERR("reserved space is not valid");
-return -1;
-}
+layout = find_layout(layout_id);
+if (layout == NULL) {
+ERR("unknown flash layout \"%s\"", layout_id);
+return -1;
+}
 
-fw_max_len = layout->fw_max_len - reserved_space;
+if (!kernel_la)
+kernel_la = layout->kernel_la;
+if (!kernel_ep)
+kernel_ep = layout->kernel_ep;
+if (!rootfs_ofs)
+rootfs_ofs = layout->rootfs_ofs;
+
+if (reserved_space > layout->fw_max_len) {
+ERR("reserved space is not valid");
+return -1;
+}
+
+fw_max_len = layout->fw_max_len - reserved_space;
+}
 
 if (kernel_info.file_name == NULL) {
 ERR("no kernel image specified");
@@ -673,50 +682,52 @@ static int check_options(void)
 
 kernel_len = kernel_info.file_size;
 
-if (combined) {
-exceed_bytes = kernel_info.file_size - (fw_max_len - sizeof(struct 
fw_header));
-if (exceed_bytes > 0) {
-if (!ignore_size) {
-ERR("kernel image is too big by %i bytes", exceed_bytes);
-return -1;
-}
-layout->fw_max_len = sizeof(struct fw_header) +
- kernel_info.file_size +
-   

[OpenWrt-Devel] [PATCH 1/4] base-files: add option to specify netdev led mode in configuration generation

2016-01-06 Thread Tal Keren
This is necessary for controlling leds of RJ45 port, when one indicate the link
status and the other indicate data transfer.

Signed-off-by: Tal Keren 
---
 package/base-files/files/bin/config_generate   | 7 ---
 package/base-files/files/lib/functions/uci-defaults.sh | 2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/package/base-files/files/bin/config_generate 
b/package/base-files/files/bin/config_generate
index 9218788..4f257e4 100755
--- a/package/base-files/files/bin/config_generate
+++ b/package/base-files/files/bin/config_generate
@@ -257,11 +257,12 @@ generate_led() {
;;
 
netdev)
-   local device
-   json_get_vars device
+   local device mode
+   json_get_vars device mode
+   [ -n "$mode" ] || mode='link tx rx'
uci -q batch <<-EOF
set system.$cfg.trigger='netdev'
-   set system.$cfg.mode='link tx rx'
+   set system.$cfg.mode='$mode'
set system.$cfg.dev='$device'
EOF
;;
diff --git a/package/base-files/files/lib/functions/uci-defaults.sh 
b/package/base-files/files/lib/functions/uci-defaults.sh
index de3f180..c0ff98a 100755
--- a/package/base-files/files/lib/functions/uci-defaults.sh
+++ b/package/base-files/files/lib/functions/uci-defaults.sh
@@ -355,6 +355,7 @@ ucidef_set_led_netdev() {
local name="$2"
local sysfs="$3"
local dev="$4"
+   local mode="$5"
 
json_select_object led
 
@@ -363,6 +364,7 @@ ucidef_set_led_netdev() {
json_add_string type netdev
json_add_string sysfs "$sysfs"
json_add_string device "$dev"
+   [ -n "$mode" ] && json_add_string mode "$mode"
json_select ..
 
json_select ..
-- 
2.6.4
___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel


[OpenWrt-Devel] [PATCH 3/4] firmware-utils: kernel image generator for TP-Link RE450

2016-01-06 Thread Tal Keren
The firmware image that is used in TP-Link RE450 (and some more devices from
the RE series) is tplink-safeloader.
In the kernel partition, the kernel is compressed in a regular tp-link
firmware that is just used for booting. Since it is only used for compressing
and booting, only four fields are filled in the header:
Vendor, version, kernel load address and kernel entry point.
mktplinkfw-kernel is a simpler version of mktpolinkfw that generate such
images. It also specifies the hardware id (as it is in the product info
section), so when doing a sysupgrade - the existing code will check for
hardware compatibility.

Signed-off-by: Tal Keren 
---
 tools/firmware-utils/Makefile|   1 +
 tools/firmware-utils/src/mktplinkfw-kernel.c | 352 +++
 2 files changed, 353 insertions(+)
 create mode 100755 tools/firmware-utils/src/mktplinkfw-kernel.c

diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile
index dc922b0..db1c953 100644
--- a/tools/firmware-utils/Makefile
+++ b/tools/firmware-utils/Makefile
@@ -42,6 +42,7 @@ define Host/Compile
$(call cc,mkplanexfw sha1)
$(call cc,mktplinkfw md5)
$(call cc,mktplinkfw2 md5)
+   $(call cc,mktplinkfw-kernel)
$(call cc,tplink-safeloader md5, -Wall)
$(call cc,pc1crypt)
$(call cc,osbridge-crc)
diff --git a/tools/firmware-utils/src/mktplinkfw-kernel.c 
b/tools/firmware-utils/src/mktplinkfw-kernel.c
new file mode 100755
index 000..1565e73
--- /dev/null
+++ b/tools/firmware-utils/src/mktplinkfw-kernel.c
@@ -0,0 +1,352 @@
+/*
+ * Copyright (C) 2009 Gabor Juhos 
+ * Copyright (C) 2016 Tal Keren 
+ *
+ * Stripped down version of the regular tplink firmware that is only used
+ * for compressing and booting the kernel.
+ *
+ * This tool was based on:
+ *   TP-Link WR941 V2 firmware checksum fixing tool.
+ *   Copyright (C) 2008,2009 Wang Jian 
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include  /* for unlink() */
+#include 
+#include  /* for getopt() */
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
+
+#define HEADER_VERSION_V1  0x0100
+
+#define MD5SUM_LEN 16
+
+struct file_info {
+   char*file_name; /* name of the file */
+   uint32_tfile_size;  /* length of the file */
+};
+
+struct fw_header {
+   uint32_tversion;/* header version */
+   charvendor_name[24];
+   charfw_version[36];
+   uint32_thw_id;  /* hardware id */
+   uint32_thw_rev; /* hardware revision */
+   uint32_tunk1;
+   uint8_t md5sum1[MD5SUM_LEN];
+   uint32_tunk2;
+   uint8_t md5sum2[MD5SUM_LEN];
+   uint32_tunk3;
+   uint32_tkernel_la;  /* kernel load address */
+   uint32_tkernel_ep;  /* kernel entry point */
+   uint32_tfw_length;  /* total length of the firmware */
+   uint32_tkernel_ofs; /* kernel data offset */
+   uint32_tkernel_len; /* kernel data length */
+   uint32_trootfs_ofs; /* rootfs data offset */
+   uint32_trootfs_len; /* rootfs data length */
+   uint32_tboot_ofs;   /* bootloader data offset */
+   uint32_tboot_len;   /* bootloader data length */
+   uint16_tver_hi;
+   uint16_tver_mid;
+   uint16_tver_lo;
+   uint8_t pad[354];
+} __attribute__ ((packed));
+
+
+/*
+ * Globals
+ */
+static char *ofname;
+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 *opt_hw_id;
+static uint32_t hw_id = 0;
+static struct file_info kernel_info;
+static uint32_t kernel_la = 0;
+static uint32_t kernel_ep = 0;
+static uint32_t kernel_len = 0;
+
+/*
+ * Message macros
+ */
+#define ERR(fmt, ...) do { \
+   fflush(0); \
+   fprintf(stderr, "[%s] *** error: " fmt "\n", \
+   progname, ## __VA_ARGS__ ); \
+} while (0)
+
+#define ERRS(fmt, ...) do { \
+   int save = errno; \
+   fflush(0); \
+   fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
+   progname, ## __VA_ARGS__, strerror(save)); \
+} while (0)
+
+#define DBG(fmt, ...) do { \
+   fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
+} while (0)
+
+static void usage(int status)
+{
+   FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
+ 

[OpenWrt-Devel] [PATCH 2/4] firmware-utils: add support for TP-Link RE450 for tplink-safeload

2016-01-06 Thread Tal Keren
Change tplink-safeloader to make it simpler to support new devices.
This design seems to fit for all the devices seen so far (CPE510/RE450/Archer
C2600 and similiar devices).
Add check for partition size when creating factory image.

Add the firmware layout of RE450. The firmware layout is different from the
stock layout, since the stock layout didn't have enough space for the kernel.
512KB were moved from the file system to the kerenl partition.
This will work with the OEM firmware since it uses the partition table
specified in the new firmware when flashing it.

Signed-off-by: Tal Keren 
---
 tools/firmware-utils/src/tplink-safeloader.c | 290 ++-
 1 file changed, 195 insertions(+), 95 deletions(-)

diff --git a/tools/firmware-utils/src/tplink-safeloader.c 
b/tools/firmware-utils/src/tplink-safeloader.c
index 77a894b..fb7401f 100644
--- a/tools/firmware-utils/src/tplink-safeloader.c
+++ b/tools/firmware-utils/src/tplink-safeloader.c
@@ -27,8 +27,7 @@
 /*
tplink-safeloader
 
-   Image generation tool for the TP-LINK SafeLoader as seen on
-   TP-LINK Pharos devices (CPE210/220/510/520)
+   Image generation tool for the TP-LINK SafeLoader
 */
 
 
@@ -53,6 +52,8 @@
 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
 
 
+#define MAX_PARTITIONS 32
+
 /** An image partition table entry */
 struct image_partition_entry {
const char *name;
@@ -67,6 +68,16 @@ struct flash_partition_entry {
uint32_t size;
 };
 
+/** Information requried to build the firmware */
+struct firmware_info {
+   const char *id;
+   const struct flash_partition_entry flash_partitions[MAX_PARTITIONS+1];
+   const char *vendor;
+   const char *support_list;
+   const char *first_sysupgrade_partition;
+   const char *last_sysupgrade_partition;
+};
+
 
 /** The content of the soft-version structure */
 struct __attribute__((__packed__)) soft_version {
@@ -102,45 +113,91 @@ static const uint8_t md5_salt[16] = {
 };
 
 
-/** Vendor information for CPE210/220/510/520 */
-static const char cpe510_vendor[] = "CPE510(TP-LINK|UN|N300-5):1.0\r\n";
-
-
-/**
-The flash partition table for CPE210/220/510/520;
-it is the same as the one used by the stock images.
-*/
-static const struct flash_partition_entry cpe510_partitions[] = {
-   {"fs-uboot", 0x0, 0x2},
-   {"partition-table", 0x2, 0x02000},
-   {"default-mac", 0x3, 0x00020},
-   {"product-info", 0x31100, 0x00100},
-   {"signature", 0x32000, 0x00400},
-   {"os-image", 0x4, 0x17},
-   {"soft-version", 0x1b, 0x00100},
-   {"support-list", 0x1b1000, 0x00400},
-   {"file-system", 0x1c, 0x60},
-   {"user-config", 0x7c, 0x1},
-   {"default-config", 0x7d, 0x1},
-   {"log", 0x7e, 0x1},
-   {"radio", 0x7f, 0x1},
-   {NULL, 0, 0}
+static struct firmware_info boards[] = {
+   {
+   .id = "CPE510",
+
+   /**
+  The flash partition table for CPE210/220/510/520;
+  it is the same as the one used by the stock images.
+   */
+   .flash_partitions = {
+   {"fs-uboot", 0x0, 0x2},
+   {"partition-table", 0x2, 0x02000},
+   {"default-mac", 0x3, 0x00020},
+   {"product-info", 0x31100, 0x00100},
+   {"signature", 0x32000, 0x00400},
+   {"os-image", 0x4, 0x17},
+   {"soft-version", 0x1b, 0x00100},
+   {"support-list", 0x1b1000, 0x00400},
+   {"file-system", 0x1c, 0x60},
+   {"user-config", 0x7c, 0x1},
+   {"default-config", 0x7d, 0x1},
+   {"log", 0x7e, 0x1},
+   {"radio", 0x7f, 0x1},
+   {NULL, 0, 0}
+   },
+
+   /** Vendor information for CPE210/220/510/520 */
+   .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
+
+   /**
+  The support list for RE450
+   */
+   .support_list =
+   "SupportList:\r\n"
+   "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
+   "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
+   "CPE520(TP-LINK|UN|N300-5):1.0\r\n"
+   "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
+   "CPE210(TP-LINK|UN|N300-2):1.0

[OpenWrt-Devel] [PATCH 4/4] a71xx: add support for TP-Link RE450

2016-01-06 Thread Tal Keren
Add full support for TP-Link RE450.
The wireless SoC is similiar to Archer C7: QCA9558 + QCA9880 (pci).
The ethernet interface is AR8035, but the mdio is connected to the gpio and
the chipset builtin mdio bus isn't used, which is unique (and weird), so the
kernel gpio mdio module is used.
The two ethernet leds are connected to the GPIO, so they are both configured,
one to indicate link status and the other to indicate data transfer.

The generation of the image was added to the image Makefile.
The return value of tplink-safeloader is not ignored anymore (to fail on error)

The result factory image is flashable from the stock web ui.

Signed-off-by: Tal Keren 
---
 target/linux/ar71xx/base-files/etc/board.d/01_leds |   9 +
 .../linux/ar71xx/base-files/etc/board.d/02_network |   1 +
 target/linux/ar71xx/base-files/etc/diag.sh |   3 +
 .../etc/hotplug.d/firmware/11-ath10k-caldata   |   4 +
 target/linux/ar71xx/base-files/lib/ar71xx.sh   |   3 +
 .../ar71xx/base-files/lib/upgrade/platform.sh  |   1 +
 target/linux/ar71xx/config-4.1 |   3 +
 .../ar71xx/files/arch/mips/ath79/Kconfig.openwrt   |  10 ++
 target/linux/ar71xx/files/arch/mips/ath79/Makefile |   1 +
 .../ar71xx/files/arch/mips/ath79/mach-re450.c  | 189 +
 .../linux/ar71xx/files/arch/mips/ath79/machtypes.h |   1 +
 target/linux/ar71xx/generic/profiles/tp-link.mk|  11 ++
 target/linux/ar71xx/image/Makefile |  46 -
 target/linux/ar71xx/mikrotik/config-default|   1 +
 target/linux/ar71xx/nand/config-default|   1 +
 15 files changed, 279 insertions(+), 5 deletions(-)
 create mode 100755 target/linux/ar71xx/files/arch/mips/ath79/mach-re450.c

diff --git a/target/linux/ar71xx/base-files/etc/board.d/01_leds 
b/target/linux/ar71xx/base-files/etc/board.d/01_leds
index cfb42a5..88d1a9c 100755
--- a/target/linux/ar71xx/base-files/etc/board.d/01_leds
+++ b/target/linux/ar71xx/base-files/etc/board.d/01_leds
@@ -398,6 +398,15 @@ qihoo-c301)
ucidef_set_led_wlan "wlan2g" "WLAN2G" "qihoo:red:status" "phy1tpt"
;;
 
+re450)
+   ucidef_set_led_netdev "lan_data" "LAN Data" "tp-link:green:lan_data" 
"eth0" "tx rx"
+   ucidef_set_led_netdev "lan_link" "LAN Link" "tp-link:green:lan_link" 
"eth0" "link"
+   ucidef_set_led_default "wps" "WPS" "tp-link:blue:wps" "0"
+   ucidef_set_led_default "wps_error" "WPS Error" "tp-link:red:wps" "0"
+   ucidef_set_led_wlan "wlan2g" "WLAN 2.4 GHz" "tp-link:blue:wlan2g" 
"phy1tpt"
+   ucidef_set_led_wlan "wlan5g" "WLAN 5 GHz" "tp-link:blue:wlan5g" 
"phy0tpt"
+   ;;
+
 smart-300)
ucidef_set_led_netdev "wan" "WAN" "nc-link:green:wan" "eth0"
ucidef_set_led_switch "lan1" "LAN1" "nc-link:green:lan1" "switch0" 
"0x04"
diff --git a/target/linux/ar71xx/base-files/etc/board.d/02_network 
b/target/linux/ar71xx/base-files/etc/board.d/02_network
index 6b6c6a4..8c16c53 100755
--- a/target/linux/ar71xx/base-files/etc/board.d/02_network
+++ b/target/linux/ar71xx/base-files/etc/board.d/02_network
@@ -340,6 +340,7 @@ rb-912uag-2hpnd |\
 rb-912uag-5hpnd |\
 rb-sxt2n |\
 rb-sxt5n |\
+re450 |\
 rocket-m-xw |\
 tl-mr10u |\
 tl-mr11u |\
diff --git a/target/linux/ar71xx/base-files/etc/diag.sh 
b/target/linux/ar71xx/base-files/etc/diag.sh
index 13a0a98..077a6a9 100644
--- a/target/linux/ar71xx/base-files/etc/diag.sh
+++ b/target/linux/ar71xx/base-files/etc/diag.sh
@@ -245,6 +245,9 @@ get_status_led() {
rb-sxt5n)
status_led="rb:green:power"
;;
+   re450)
+   status_led="tp-link:blue:system"
+   ;;
routerstation | routerstation-pro)
status_led="ubnt:green:rf"
;;
diff --git 
a/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata 
b/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata
index 74c3417..88eb10a 100644
--- a/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata
+++ b/target/linux/ar71xx/base-files/etc/hotplug.d/firmware/11-ath10k-caldata
@@ -75,6 +75,10 @@ case "$FIRMWARE" in
ath10kcal_extract "ART" 20480 2116
ath10kcal_patch_mac $(macaddr_add $(cat 
/sys/class/net/eth0/address) +16)
;;
+   re450)
+   ath10kcal_extract "art" 20480 2116
+   ath10kcal_patch_mac $(macaddr_add $(cat 
/sys/class/net/eth0/address) -2)
+   ;;
esac
;;
 "ath10k/cal-pci-:01:00.0.bin&