From: Matthias Schiffer <mschiffer@universe-factory.net>
Date: Sat, 20 Jul 2019 13:12:58 +0200
Subject: uboot-fritz4040: add fixes for bootloader image generation

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>

diff --git a/package/boot/uboot-fritz4040/patches/200-fritzcreator-replace-obscure-padding-generation-with.patch b/package/boot/uboot-fritz4040/patches/200-fritzcreator-replace-obscure-padding-generation-with.patch
new file mode 100644
index 0000000000000000000000000000000000000000..44062b88048f483b618992c51b1dffdfe7ca511a
--- /dev/null
+++ b/package/boot/uboot-fritz4040/patches/200-fritzcreator-replace-obscure-padding-generation-with.patch
@@ -0,0 +1,63 @@
+From 9af24ea29494a279eba01d2b284c9c94d4fdb267 Mon Sep 17 00:00:00 2001
+Message-Id: <9af24ea29494a279eba01d2b284c9c94d4fdb267.1563619497.git.mschiffer@universe-factory.net>
+From: Matthias Schiffer <mschiffer@universe-factory.net>
+Date: Sat, 20 Jul 2019 12:35:01 +0200
+Subject: [PATCH 1/2] fritzcreator: replace obscure padding generation with
+ something more compatible
+
+It as reported that OpenWrt/Gluon would sometimes generate invalid
+bootloader images with insufficient for the FritzBox 4040, presumably
+depending on the distro version used on the build host.
+
+This commit tries to fix this by replacing the printf-based padding
+generation (that in one case relies on 65536 arguments being passed to the
+printf command) with a more compatible read from /dev/zero.
+
+[1] https://github.com/freifunk-gluon/gluon/issues/1766
+---
+ fritz/fritzcreator.sh | 8 ++++----
+ 1 file changed, 4 insertions(+), 4 deletions(-)
+
+diff --git a/fritz/fritzcreator.sh b/fritz/fritzcreator.sh
+index 8ed0c08cc5ed..8ea412e5aca5 100755
+--- a/fritz/fritzcreator.sh
++++ b/fritz/fritzcreator.sh
+@@ -55,7 +55,7 @@ cat "$FRITZ_DTB" "$FRITZ_DTB" "$FRITZ_DTB" "$FRITZ_DTB" >> $UBOOT_FRITZ4040
+ rm -f "$FRITZ_DTB"
+ 
+ # Add 512 bytes of pad area
+-printf "%0.s\0" {1..512} >> $UBOOT_FRITZ4040
++dd if=/dev/zero bs=512 count=1 >> $UBOOT_FRITZ4040
+ 
+ # This table links to the individual DTBs for every HWSubRevision.
+ # A table entry consists of two 32-bit words.
+@@ -84,7 +84,7 @@ mv "$UBOOT_FRITZ4040.new" "$UBOOT_FRITZ4040"
+ 
+ 
+ # Add 64k bytes of pad area
+-printf "%0.s\0" {1..65536} >> $UBOOT_FRITZ4040
++dd if=/dev/zero bs=1024 count=64 >> $UBOOT_FRITZ4040
+ 
+ # Pack it with lzma
+ fritz/lzma e "$UBOOT_FRITZ4040" -lc1 -lp2 -pb2 "$UBOOT_FRITZ4040.new"
+@@ -99,7 +99,7 @@ fritz/lzma2eva $UBOOT_LOADADDR $UBOOT_LOADADDR "$UBOOT_FRITZ4040.new" "$UBOOT_FR
+ # The next bit. The hshqs partition should be aligned to 0x100
+ let size=$(stat -c%s "$UBOOT_FRITZ4040")
+ let "pad = 256 - ( $size % 256) % 256"
+-( printf "%0.s\377" {1..256} | dd conv=sync bs=$pad count=1 ) > $UBOOT_FRITZ4040.pad
++dd if=/dev/zero bs=$pad count=1 | tr '\0' '\377' > $UBOOT_FRITZ4040.pad
+ 
+ cat "$UBOOT_FRITZ4040" "$UBOOT_FRITZ4040.pad" > $UBOOT_FRITZ4040.new
+ 
+@@ -109,7 +109,7 @@ rm -f "$UBOOT_FRITZ4040.pad"
+ 
+ # Apparently, EVA checks for the SquashFS filesystem MAGIC too. Likely for the rootfs
+ # entry.
+-(cat "$UBOOT_FRITZ4040"; echo "hsqs"; printf "%0.s\0" {1..124} ) > $UBOOT_FRITZ4040.new
++(cat "$UBOOT_FRITZ4040"; echo "hsqs"; dd if=/dev/zero bs=124 count=1 ) > $UBOOT_FRITZ4040.new
+ 
+ # Make it so that this fits into 512k (Note: we have to add 8 Bytes for the final checksum
+ # so 524280 is 512k - 8.
+-- 
+2.22.0
+
diff --git a/package/boot/uboot-fritz4040/patches/201-fritzcreator-actually-add-checksum-spacer.patch b/package/boot/uboot-fritz4040/patches/201-fritzcreator-actually-add-checksum-spacer.patch
new file mode 100644
index 0000000000000000000000000000000000000000..f92060a23312a4961ddc75716fd06b9d6a106a1d
--- /dev/null
+++ b/package/boot/uboot-fritz4040/patches/201-fritzcreator-actually-add-checksum-spacer.patch
@@ -0,0 +1,30 @@
+From dd05d1bb619b372e4717a74da560450dd93d4c3b Mon Sep 17 00:00:00 2001
+Message-Id: <dd05d1bb619b372e4717a74da560450dd93d4c3b.1563619497.git.mschiffer@universe-factory.net>
+In-Reply-To: <9af24ea29494a279eba01d2b284c9c94d4fdb267.1563619497.git.mschiffer@universe-factory.net>
+References: <9af24ea29494a279eba01d2b284c9c94d4fdb267.1563619497.git.mschiffer@universe-factory.net>
+From: Matthias Schiffer <mschiffer@universe-factory.net>
+Date: Sat, 20 Jul 2019 12:43:48 +0200
+Subject: [PATCH 2/2] fritzcreator: actually add checksum spacer
+
+The spacer was written to $UBOOT_FRITZ4040.new, but this file was not used
+at all before it was overwritten again.
+---
+ fritz/fritzcreator.sh | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/fritz/fritzcreator.sh b/fritz/fritzcreator.sh
+index 8ea412e5aca5..ea5a50df1f87 100755
+--- a/fritz/fritzcreator.sh
++++ b/fritz/fritzcreator.sh
+@@ -94,7 +94,7 @@ fritz/lzma2eva $UBOOT_LOADADDR $UBOOT_LOADADDR "$UBOOT_FRITZ4040.new" "$UBOOT_FR
+ 
+ # The bootloader seems to add a TI checksum signature (8 Bytes) as part of the
+ # "check mtd1" command in the FTP prompt. To make this easier we add spacer here.
+-(cat "$UBOOT_FRITZ4040"; printf "\xff\xff\xff\xff\xff\xff\xff\xff" ) > $UBOOT_FRITZ4040.new
++printf "\xff\xff\xff\xff\xff\xff\xff\xff" >> $UBOOT_FRITZ4040
+ 
+ # The next bit. The hshqs partition should be aligned to 0x100
+ let size=$(stat -c%s "$UBOOT_FRITZ4040")
+-- 
+2.22.0
+
