[OpenWrt-Devel] [PATCH] [RFC] ath79: ag71xx: apply interface mode to MII0/1_CNTL on ar71xx/ar913x

2018-08-15 Thread Chuanhong Guo
Signed-off-by: Chuanhong Guo 
---
RFC:
Previous discussion about this patch can be found on GitHub PR#1271.
This patch applies correct interface mode to MII0/1_CNTL register at 0x1807/
0x18070004. But there is a small difference in values for these two registers:
| GMAC0| GMAC1   |
|--|-|
| 0 GMII   | 0 RGMII |
| 1 MII| 1 RMII  |
| 2 RGMII  | |
| 3 RMII   | |
I currently have 4 ways of dealing with this:
1. Use a bool value in dts indicating whether this is the second GMAC. This one
   is pretty dirty and I dropped it.
2. Split MII_CNTL into separated dt node and use different compatible for them
   like we did for ETH_CFG (gmac node) on ar933x and later SoCs. After some 
discussion
   on GitHub it turns out to be unreasonable to treat those in separated nodes.
3. Use ar7100-eth0/ar7100-eth1 as compatible string. This is what I've done in
   this patch I sent here. But I think my way of using compatible string here 
is ugly :(
   A possible cleaner implementation would be introducing 
ar7100-eth0/ar7100-eth1/
   ar9130-eth0/ar9130-eth1 to replace ar7100-eth/ar9130-eth. But I doubt whether
   introducing 4 new compatible strings for such a slight difference is worthy.
4. Somehow write all the possible values for each interface mode in device tree.
   e.g. qca,if-modes = "gmii","mii","rgmii","rmii"; for eth0 and
qca,if-modes = "rgmii","rmii"; for eth1.

Which one is better? Is there any other possible solutions for this problem?

 target/linux/ath79/dts/ar7100.dtsi|  4 +-
 target/linux/ath79/dts/ar9132.dtsi|  2 +-
 .../net/ethernet/atheros/ag71xx/ag71xx_main.c | 61 +++
 3 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/target/linux/ath79/dts/ar7100.dtsi 
b/target/linux/ath79/dts/ar7100.dtsi
index 8994a7d688..89c17bcede 100644
--- a/target/linux/ath79/dts/ar7100.dtsi
+++ b/target/linux/ath79/dts/ar7100.dtsi
@@ -171,7 +171,7 @@
 };
 
  {
-   compatible = "qca,ar7100-eth";
+   compatible = "qca,ar7100-eth0", "qca,ar7100-eth";
reg = <0x1900 0x200
0x1807 0x4>;
 
@@ -189,7 +189,7 @@
 };
 
  {
-   compatible = "qca,ar7100-eth";
+   compatible = "qca,ar7100-eth1", "qca,ar7100-eth";
reg = <0x1a00 0x200
0x18070004 0x4>;
 
diff --git a/target/linux/ath79/dts/ar9132.dtsi 
b/target/linux/ath79/dts/ar9132.dtsi
index 9d8ddcf9ba..a136b06e69 100644
--- a/target/linux/ath79/dts/ar9132.dtsi
+++ b/target/linux/ath79/dts/ar9132.dtsi
@@ -185,7 +185,7 @@
 };
 
  {
-   compatible = "qca,ar9130-eth", "syscon";
+   compatible = "qca,ar9130-eth", "qca,ar7100-eth0", "syscon";
reg = <0x1900 0x200
0x1807 0x4>;
pll-data = <0x1a00 0x13000a44 0x00441099>;
diff --git 
a/target/linux/ath79/files/drivers/net/ethernet/atheros/ag71xx/ag71xx_main.c 
b/target/linux/ath79/files/drivers/net/ethernet/atheros/ag71xx/ag71xx_main.c
index 1e0bb6937f..5ea9ef40d2 100644
--- a/target/linux/ath79/files/drivers/net/ethernet/atheros/ag71xx/ag71xx_main.c
+++ b/target/linux/ath79/files/drivers/net/ethernet/atheros/ag71xx/ag71xx_main.c
@@ -529,6 +529,60 @@ static void ath79_set_pll(struct ag71xx *ag)
udelay(100);
 }
 
+static void ath79_mii_ctrl_set_if(struct ag71xx *ag, unsigned int mii_if)
+{
+   u32 t;
+
+   t = __raw_readl(ag->mii_base);
+   t &= ~(AR71XX_MII_CTRL_IF_MASK);
+   t |= (mii_if & AR71XX_MII_CTRL_IF_MASK);
+   __raw_writel(t, ag->mii_base);
+}
+
+static void ath79_mii0_ctrl_set_if(struct ag71xx *ag)
+{
+   unsigned int mii_if;
+
+   switch (ag->phy_if_mode) {
+   case PHY_INTERFACE_MODE_MII:
+   mii_if = AR71XX_MII0_CTRL_IF_MII;
+   break;
+   case PHY_INTERFACE_MODE_GMII:
+   mii_if = AR71XX_MII0_CTRL_IF_GMII;
+   break;
+   case PHY_INTERFACE_MODE_RGMII:
+   mii_if = AR71XX_MII0_CTRL_IF_RGMII;
+   break;
+   case PHY_INTERFACE_MODE_RMII:
+   mii_if = AR71XX_MII0_CTRL_IF_RMII;
+   break;
+   default:
+   WARN(1, "Impossible PHY mode defined.\n");
+   return;
+   }
+
+   ath79_mii_ctrl_set_if(ag, mii_if);
+}
+
+static void ath79_mii1_ctrl_set_if(struct ag71xx *ag)
+{
+   unsigned int mii_if;
+
+   switch (ag->phy_if_mode) {
+   case PHY_INTERFACE_MODE_RMII:
+   mii_if = AR71XX_MII1_CTRL_IF_RMII;
+   break;
+   case PHY_INTERFACE_MODE_RGMII:
+   mii_if = AR71XX_MII1_CTRL_IF_RGMII;
+   break;
+   default:
+   WARN(1, "Impossible PHY mode defined.\n");
+   return;
+   }
+
+   ath79_mii_ctrl_set_if(ag, mii_if);
+}
+
 static void ath79_mii_ctrl_set_speed(struct ag71xx *ag)
 {
unsigned int mii_speed;
@@ -1427,6 +1481,13 @@ static int ag71xx_probe(struct platform_device *pdev)
goto err_free;
}
 
+   if 

Re: [OpenWrt-Devel] [PATCH] build: add mkrasimage

2018-08-15 Thread Karl Palsson

David Bauer  wrote:
> The current make-ras.sh image generation script for the ZyXEL
> NBG6617 has portability issues with bash. Because of this,
> factory images are currently not built correctly by the OpenWRT
> buildbots.
> 
> This commit replaces the make-ras.sh by C-written mkrasimage.
> The old script is still kept but can be deleted in the future.

1) how bad are the portability issues that you felt
reimplementing in _C_ was the best path? 2) if it's that bad, why
keep it? How will future people knwo what to use. Either get rid
of it, or fix it.

Cheers,
Karl P

> 
> Signed-off-by: David Bauer 
> ---
>  include/image-commands.mk |  13 +
>  target/linux/ipq40xx/image/Makefile   |   2 +-
>  tools/firmware-utils/Makefile |   1 +
>  tools/firmware-utils/src/mkrasimage.c | 374 ++
>  4 files changed, 389 insertions(+), 1 deletion(-)
>  create mode 100644 tools/firmware-utils/src/mkrasimage.c
> 
> diff --git a/include/image-commands.mk
> b/include/image-commands.mk index 3cc5dc21e1..bb5fe46e1a 100644
> --- a/include/image-commands.mk
> +++ b/include/image-commands.mk
> @@ -62,6 +62,19 @@ define Build/make-ras
>   @mv $@.new $@
>  endef
>  
> +define Build/zyxel-ras-image
> + let \
> + newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
> + $(STAGING_DIR_HOST)/bin/mkrasimage \
> + -b $(RAS_BOARD) \
> + -v $(RAS_VERSION) \
> + -k $(call 
> param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
> + -r $@ \
> + -s $$newsize \
> + -o $@.new
> + @mv $@.new $@
> +endef
> +
>  define Build/mkbuffaloimg
>   $(STAGING_DIR_HOST)/bin/mkbuffaloimg -B $(BOARDNAME) \
>   -R $$(($(subst k, * 1024,$(ROOTFS_SIZE \
> diff --git a/target/linux/ipq40xx/image/Makefile
> b/target/linux/ipq40xx/image/Makefile index
> d1ee1004fd..6e4125db0b 100644
> --- a/target/linux/ipq40xx/image/Makefile
> +++ b/target/linux/ipq40xx/image/Makefile
> @@ -221,7 +221,7 @@ define Device/zyxel_nbg6617
>  #at least as large as the one of the initial firmware image (not the 
> current
>  #one on the device). This only applies to the Web-UI, the bootlaoder 
> ignores
>  #this minimum-size. However, the larger image can be flashed both ways.
> - IMAGE/factory.bin := append-rootfs | pad-rootfs | check-size 
> (ROOTFS_SIZE) | make-ras
> + IMAGE/factory.bin := append-rootfs | pad-rootfs | check-size 
> (ROOTFS_SIZE) | zyxel-ras-image
>   IMAGE/sysupgrade.bin/squashfs := append-rootfs | pad-rootfs | 
> check-size (ROOTFS_SIZE) | sysupgrade-tar rootfs=@ | append-metadata
>   DEVICE_PACKAGES := ipq-wifi-zyxel_nbg6617 uboot-envtools
>  endef
> diff --git a/tools/firmware-utils/Makefile
> b/tools/firmware-utils/Makefile index 436a43794c..00917c3417
> 100644
> --- a/tools/firmware-utils/Makefile
> +++ b/tools/firmware-utils/Makefile
> @@ -70,6 +70,7 @@ define Host/Compile
>   $(call cc,fix-u-media-header cyg_crc32,-Wall)
>   $(call cc,hcsmakeimage bcmalgo)
>   $(call cc,mkporayfw, -Wall)
> + $(call cc,mkrasimage, --std=gnu99)
>   $(call cc,mkhilinkfw, -lcrypto)
>   $(call cc,mkdcs932, -Wall)
>   $(call cc,mkheader_gemtek,-lz)
> diff --git a/tools/firmware-utils/src/mkrasimage.c
> b/tools/firmware-utils/src/mkrasimage.c new file mode 100644
> index 00..1cac7b5da9
> --- /dev/null
> +++ b/tools/firmware-utils/src/mkrasimage.c
> @@ -0,0 +1,374 @@
> +/*
> + * --- ZyXEL header format ---
> + * Original Version by Benjamin Berg 
> + * C implementation based on generation-script by Christian Lamparter 
> 
> + *
> + * The firmware image prefixed with a header (which is written into the MTD 
> device).
> + * The header is one erase block (~64KiB) in size, but the checksum only 
> convers the
> + * first 2KiB. Padding is 0xff. All integers are in big-endian.
> + *
> + * The checksum is always a 16-Bit System V checksum (sum -s) stored in a 
> 32-Bit integer.
> + *
> + *   4 bytes:  checksum of the rootfs image
> + *   4 bytes:  length of the contained rootfs image file (big endian)
> + *  32 bytes:  Firmware Version string (NUL terminated, 0xff padded)
> + *   4 bytes:  checksum over the header partition (big endian - see below)
> + *  64 bytes:  Model (e.g. "NBG6617", NUL termiated, 0xff padded)
> + *   4 bytes:  checksum of the kernel partition
> + *   4 bytes:  length of the contained kernel image file (big endian)
> + *  rest:  0xff padding (To erase block size)
> + *
> + * The checksums are calculated by adding up all bytes and if a 16bit
> + * overflow occurs, one is added and the sum is masked to 16 bit:
> + *   csum = csum + databyte; if (csum > 0x) { csum += 1; csum &= 0x 
> };
> + * Should the file have an odd number of bytes then the byte len-0x800 is
> + * used additionally.
> + *
> + * The checksum for the header is calculated over the first 2048 bytes with
> + 

[OpenWrt-Devel] [PATCH v2] ath79: add support for Fritz!Box 4020

2018-08-15 Thread David Bauer
This commit adds support for the AVM Fritz!Box 4020 WiFi-router.

SoC:   Qualcomm Atheros QCA9561 (Dragonfly) 750MHz
RAM:   Winbond W971GG6KB-25
FLASH: Macronix MX25L12835F
WiFi:  QCA9561 b/g/n 3x3 450Mbit/s
USB:   1x USB 2.0
IN:WPS button, WiFi button
OUT:   Power LED green, Internet LED green, WLAN LED green,
   LAN LED green, INFO LED green, INFO LED red
UART:  Header Next to Black metal shield
   Pinout is 3.3V - RX - TX - GND (Square Pad is 3.3V)
   The Serial setting is 115200-8-N-1.

Tested and working:
 - Ethernet (LAN + WAN)
 - WiFi (correct MAC)
 - Installation via EVA bootloader
 - OpenWRT sysupgrade
 - Buttons
 - LEDs

Not working:
 - USB port

Installation via EVA:
In the first seconds after Power is connected, the bootloader will
listen for FTP connections on 169.254.157.1 (Might also be 192.168.178.1).
Firmware can be uploaded like following:

  ftp> quote USER adam2
  ftp> quote PASS adam2
  ftp> binary
  ftp> debug
  ftp> passive
  ftp> quote MEDIA FLSH
  ftp> put openwrt-sysupgrade.bin mtd1

Note that this procedure might take up to two minutes. After transfer is
complete you need to powercycle the device to boot OpenWRT.

Signed-off-by: David Bauer 
---

v2 changes:
 - add annotation for initial output-line-state setting
 - rebase onto current master

 .../ath79/base-files/etc/board.d/01_leds  |   4 +
 .../ath79/base-files/etc/board.d/02_network   |   5 +
 .../etc/hotplug.d/firmware/10-ath9k-eeprom|   3 +
 .../linux/ath79/dts/qca9561_avm_fritz4020.dts | 172 ++
 target/linux/ath79/image/generic.mk   |  14 ++
 .../005-gpio-74x164-add-initvals.patch|  15 ++
 6 files changed, 213 insertions(+)
 create mode 100644 target/linux/ath79/dts/qca9561_avm_fritz4020.dts
 create mode 100644 
target/linux/ath79/patches-4.14/005-gpio-74x164-add-initvals.patch

diff --git a/target/linux/ath79/base-files/etc/board.d/01_leds 
b/target/linux/ath79/base-files/etc/board.d/01_leds
index c3dc1bbe56..f058bf599e 100755
--- a/target/linux/ath79/base-files/etc/board.d/01_leds
+++ b/target/linux/ath79/base-files/etc/board.d/01_leds
@@ -17,6 +17,10 @@ avm,fritz300e)
ucidef_set_led_rssi "rssimediumhigh" "RSSIMEDIUMHIGH" 
"$boardname:green:rssi3" "wlan0" "60" "100"
ucidef_set_led_rssi "rssihigh" "RSSIHIGH" "$boardname:green:rssi4" 
"wlan0" "80" "100"
;;
+avm,fritz4020)
+   ucidef_set_led_netdev "wan" "WAN" "$boardname:green:wan" "eth0"
+   ucidef_set_led_switch "lan" "LAN" "$boardname:green:lan" "switch0" 
"0x1E"
+   ;;
 etactica,eg200)
ucidef_set_led_netdev "lan" "LAN" "$boardname:red:eth0" "eth0"
ucidef_set_led_oneshot "modbus" "Modbus" "$boardname:red:modbus" "100" 
"33"
diff --git a/target/linux/ath79/base-files/etc/board.d/02_network 
b/target/linux/ath79/base-files/etc/board.d/02_network
index a29a295c56..6cd1686e23 100755
--- a/target/linux/ath79/base-files/etc/board.d/02_network
+++ b/target/linux/ath79/base-files/etc/board.d/02_network
@@ -43,6 +43,7 @@ ath79_setup_interfaces()
etactica,eg200)
ucidef_set_interface_lan "eth0" "dhcp"
;;
+   avm,fritz4020|\
glinet,ar150|\
glinet,ar300m)
ucidef_set_interfaces_lan_wan "eth1" "eth0"
@@ -155,6 +156,10 @@ ath79_setup_macs()
avm,fritz300e)
lan_mac=$(fritz_tffs -n maca -i $(find_mtd_part "tffs (1)"))
;;
+   avm,fritz4020)
+   lan_mac=$(fritz_tffs -n maca -i $(find_mtd_part "tffs (1)"))
+   wan_mac=$(fritz_tffs -n macb -i $(find_mtd_part "tffs (1)"))
+   ;;
dlink,dir-825-b1)
lan_mac=$(mtd_get_mac_text "caldata" 65440)
wan_mac=$(mtd_get_mac_text "caldata" 65460)
diff --git 
a/target/linux/ath79/base-files/etc/hotplug.d/firmware/10-ath9k-eeprom 
b/target/linux/ath79/base-files/etc/hotplug.d/firmware/10-ath9k-eeprom
index f71139c55c..c36b566a35 100644
--- a/target/linux/ath79/base-files/etc/hotplug.d/firmware/10-ath9k-eeprom
+++ b/target/linux/ath79/base-files/etc/hotplug.d/firmware/10-ath9k-eeprom
@@ -97,6 +97,9 @@ board=$(board_name)
 case "$FIRMWARE" in
 "ath9k-eeprom-ahb-1810.wmac.bin")
case $board in
+   avm,fritz4020)
+   ath9k_eeprom_extract_reverse "urlader" 5441 1088
+   ;;
iodata,wn-ac1600dgr2)
ath9k_eeprom_extract "art" 4096 1088
ath9k_patch_fw_mac $(mtd_get_mac_ascii u-boot-env ethaddr) 2
diff --git a/target/linux/ath79/dts/qca9561_avm_fritz4020.dts 
b/target/linux/ath79/dts/qca9561_avm_fritz4020.dts
new file mode 100644
index 00..a4e059efd4
--- /dev/null
+++ b/target/linux/ath79/dts/qca9561_avm_fritz4020.dts
@@ -0,0 +1,172 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
+/dts-v1/;
+
+#include 
+#include 
+
+#include "qca956x.dtsi"
+
+/ {
+   compatible = "avm,fritz4020", "qca,qca9560";
+   model = "AVM FRITZ!Box 4020";
+
+   aliases {
+   

Re: [OpenWrt-Devel] [PATCH] build: add mkrasimage

2018-08-15 Thread Michael Heimpold
Hi David,

a few code-styling nitpicks, see comments below:

Am Mittwoch, 15. August 2018, 16:44:03 CEST schrieb David Bauer:
> The current make-ras.sh image generation script for the ZyXEL NBG6617
> has portability issues with bash. Because of this, factory images are
> currently not built correctly by the OpenWRT buildbots.
> 
> This commit replaces the make-ras.sh by C-written mkrasimage. The old
> script is still kept but can be deleted in the future.
> 
> Signed-off-by: David Bauer 
> ---
>  include/image-commands.mk |  13 +
>  target/linux/ipq40xx/image/Makefile   |   2 +-
>  tools/firmware-utils/Makefile |   1 +
>  tools/firmware-utils/src/mkrasimage.c | 374 ++
>  4 files changed, 389 insertions(+), 1 deletion(-)
>  create mode 100644 tools/firmware-utils/src/mkrasimage.c
> 
> diff --git a/include/image-commands.mk b/include/image-commands.mk
> index 3cc5dc21e1..bb5fe46e1a 100644
> --- a/include/image-commands.mk
> +++ b/include/image-commands.mk
> @@ -62,6 +62,19 @@ define Build/make-ras
>   @mv $@.new $@
>  endef
> 
> +define Build/zyxel-ras-image
> + let \
> + newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
> + $(STAGING_DIR_HOST)/bin/mkrasimage \
> + -b $(RAS_BOARD) \
> + -v $(RAS_VERSION) \
> + -k $(call 
> param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
> + -r $@ \
> + -s $$newsize \
> + -o $@.new
> + @mv $@.new $@
> +endef
> +
>  define Build/mkbuffaloimg
>   $(STAGING_DIR_HOST)/bin/mkbuffaloimg -B $(BOARDNAME) \
>   -R $$(($(subst k, * 1024,$(ROOTFS_SIZE \
> diff --git a/target/linux/ipq40xx/image/Makefile
> b/target/linux/ipq40xx/image/Makefile index d1ee1004fd..6e4125db0b 100644
> --- a/target/linux/ipq40xx/image/Makefile
> +++ b/target/linux/ipq40xx/image/Makefile
> @@ -221,7 +221,7 @@ define Device/zyxel_nbg6617
>  #at least as large as the one of the initial firmware image (not the
> current # one on the device). This only applies to the Web-UI, the
> bootlaoder ignores #  this minimum-size. However, the larger image can be
> flashed both ways. -  IMAGE/factory.bin := append-rootfs | pad-rootfs |
> check-size (ROOTFS_SIZE) | make-ras + IMAGE/factory.bin :=
> append-rootfs | pad-rootfs | check-size (ROOTFS_SIZE) | zyxel-ras-image
> IMAGE/sysupgrade.bin/squashfs := append-rootfs | pad-rootfs | check-size
> (ROOTFS_SIZE) | sysupgrade-tar rootfs=@ | append-metadata
> DEVICE_PACKAGES := ipq-wifi-zyxel_nbg6617 uboot-envtools
>  endef
> diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile
> index 436a43794c..00917c3417 100644
> --- a/tools/firmware-utils/Makefile
> +++ b/tools/firmware-utils/Makefile
> @@ -70,6 +70,7 @@ define Host/Compile
>   $(call cc,fix-u-media-header cyg_crc32,-Wall)
>   $(call cc,hcsmakeimage bcmalgo)
>   $(call cc,mkporayfw, -Wall)
> + $(call cc,mkrasimage, --std=gnu99)
>   $(call cc,mkhilinkfw, -lcrypto)
>   $(call cc,mkdcs932, -Wall)
>   $(call cc,mkheader_gemtek,-lz)
> diff --git a/tools/firmware-utils/src/mkrasimage.c
> b/tools/firmware-utils/src/mkrasimage.c new file mode 100644
> index 00..1cac7b5da9
> --- /dev/null
> +++ b/tools/firmware-utils/src/mkrasimage.c
> @@ -0,0 +1,374 @@
> +/*
> + * --- ZyXEL header format ---
> + * Original Version by Benjamin Berg 
> + * C implementation based on generation-script by Christian Lamparter
>  + *
> + * The firmware image prefixed with a header (which is written into the MTD
> device). + * The header is one erase block (~64KiB) in size, but the
> checksum only convers the + * first 2KiB. Padding is 0xff. All integers are
> in big-endian.
> + *
> + * The checksum is always a 16-Bit System V checksum (sum -s) stored in a
> 32-Bit integer. + *
> + *   4 bytes:  checksum of the rootfs image
> + *   4 bytes:  length of the contained rootfs image file (big endian)
> + *  32 bytes:  Firmware Version string (NUL terminated, 0xff padded)
> + *   4 bytes:  checksum over the header partition (big endian - see below)
> + *  64 bytes:  Model (e.g. "NBG6617", NUL termiated, 0xff padded)
> + *   4 bytes:  checksum of the kernel partition
> + *   4 bytes:  length of the contained kernel image file (big endian)
> + *  rest:  0xff padding (To erase block size)
> + *
> + * The checksums are calculated by adding up all bytes and if a 16bit
> + * overflow occurs, one is added and the sum is masked to 16 bit:
> + *   csum = csum + databyte; if (csum > 0x) { csum += 1; csum &= 0x
> }; + * Should the file have an odd number of bytes then the byte len-0x800
> is + * used additionally.
> + *
> + * The checksum for the header is calculated over the first 2048 bytes with
> + * the rootfs image checksum as the placeholder during calculation. + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under 

Re: [OpenWrt-Devel] RFT: ar71xx/mac80211 update

2018-08-15 Thread Sven Schönhoff
Hi John,

I've tested your tree on my Archer C7 v4 but it bricked the device.
Only the power led is shining and I don't get a dhcp lease.

I've added the new "libubox: set RPATH for host build" commit to skip
the base-files error during build, added luci in make menuconfig and
unchecked "keep settings" for the flash.

Here is the serial log: https://pastebin.com/tcM2zTcm


Thank you and best regards
Sven

___
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


[OpenWrt-Devel] [PATCH] build: add mkrasimage

2018-08-15 Thread David Bauer
The current make-ras.sh image generation script for the ZyXEL NBG6617
has portability issues with bash. Because of this, factory images are
currently not built correctly by the OpenWRT buildbots.

This commit replaces the make-ras.sh by C-written mkrasimage. The old
script is still kept but can be deleted in the future.

Signed-off-by: David Bauer 
---
 include/image-commands.mk |  13 +
 target/linux/ipq40xx/image/Makefile   |   2 +-
 tools/firmware-utils/Makefile |   1 +
 tools/firmware-utils/src/mkrasimage.c | 374 ++
 4 files changed, 389 insertions(+), 1 deletion(-)
 create mode 100644 tools/firmware-utils/src/mkrasimage.c

diff --git a/include/image-commands.mk b/include/image-commands.mk
index 3cc5dc21e1..bb5fe46e1a 100644
--- a/include/image-commands.mk
+++ b/include/image-commands.mk
@@ -62,6 +62,19 @@ define Build/make-ras
@mv $@.new $@
 endef
 
+define Build/zyxel-ras-image
+   let \
+   newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
+   $(STAGING_DIR_HOST)/bin/mkrasimage \
+   -b $(RAS_BOARD) \
+   -v $(RAS_VERSION) \
+   -k $(call 
param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
+   -r $@ \
+   -s $$newsize \
+   -o $@.new
+   @mv $@.new $@
+endef
+
 define Build/mkbuffaloimg
$(STAGING_DIR_HOST)/bin/mkbuffaloimg -B $(BOARDNAME) \
-R $$(($(subst k, * 1024,$(ROOTFS_SIZE \
diff --git a/target/linux/ipq40xx/image/Makefile 
b/target/linux/ipq40xx/image/Makefile
index d1ee1004fd..6e4125db0b 100644
--- a/target/linux/ipq40xx/image/Makefile
+++ b/target/linux/ipq40xx/image/Makefile
@@ -221,7 +221,7 @@ define Device/zyxel_nbg6617
 #  at least as large as the one of the initial firmware image (not the 
current
 #  one on the device). This only applies to the Web-UI, the bootlaoder 
ignores
 #  this minimum-size. However, the larger image can be flashed both ways.
-   IMAGE/factory.bin := append-rootfs | pad-rootfs | check-size 
(ROOTFS_SIZE) | make-ras
+   IMAGE/factory.bin := append-rootfs | pad-rootfs | check-size 
(ROOTFS_SIZE) | zyxel-ras-image
IMAGE/sysupgrade.bin/squashfs := append-rootfs | pad-rootfs | 
check-size (ROOTFS_SIZE) | sysupgrade-tar rootfs=@ | append-metadata
DEVICE_PACKAGES := ipq-wifi-zyxel_nbg6617 uboot-envtools
 endef
diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile
index 436a43794c..00917c3417 100644
--- a/tools/firmware-utils/Makefile
+++ b/tools/firmware-utils/Makefile
@@ -70,6 +70,7 @@ define Host/Compile
$(call cc,fix-u-media-header cyg_crc32,-Wall)
$(call cc,hcsmakeimage bcmalgo)
$(call cc,mkporayfw, -Wall)
+   $(call cc,mkrasimage, --std=gnu99)
$(call cc,mkhilinkfw, -lcrypto)
$(call cc,mkdcs932, -Wall)
$(call cc,mkheader_gemtek,-lz)
diff --git a/tools/firmware-utils/src/mkrasimage.c 
b/tools/firmware-utils/src/mkrasimage.c
new file mode 100644
index 00..1cac7b5da9
--- /dev/null
+++ b/tools/firmware-utils/src/mkrasimage.c
@@ -0,0 +1,374 @@
+/*
+ * --- ZyXEL header format ---
+ * Original Version by Benjamin Berg 
+ * C implementation based on generation-script by Christian Lamparter 

+ *
+ * The firmware image prefixed with a header (which is written into the MTD 
device).
+ * The header is one erase block (~64KiB) in size, but the checksum only 
convers the
+ * first 2KiB. Padding is 0xff. All integers are in big-endian.
+ *
+ * The checksum is always a 16-Bit System V checksum (sum -s) stored in a 
32-Bit integer.
+ *
+ *   4 bytes:  checksum of the rootfs image
+ *   4 bytes:  length of the contained rootfs image file (big endian)
+ *  32 bytes:  Firmware Version string (NUL terminated, 0xff padded)
+ *   4 bytes:  checksum over the header partition (big endian - see below)
+ *  64 bytes:  Model (e.g. "NBG6617", NUL termiated, 0xff padded)
+ *   4 bytes:  checksum of the kernel partition
+ *   4 bytes:  length of the contained kernel image file (big endian)
+ *  rest:  0xff padding (To erase block size)
+ *
+ * The checksums are calculated by adding up all bytes and if a 16bit
+ * overflow occurs, one is added and the sum is masked to 16 bit:
+ *   csum = csum + databyte; if (csum > 0x) { csum += 1; csum &= 0x };
+ * Should the file have an odd number of bytes then the byte len-0x800 is
+ * used additionally.
+ *
+ * The checksum for the header is calculated over the first 2048 bytes with
+ * the rootfs image checksum as the placeholder during calculation.
+ *
+ * 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 
+#include 
+
+#define VERSION_STRING_LEN 31
+#define ROOTFS_HEADER_LEN 40
+
+#define