From fdac4c085daccd774b4eba6b8c3fca2739cbb8dd Mon Sep 17 00:00:00 2001
From: =?utf8?q?Thibaut=20VAR=C3=88NE?= <hacks@slashdirt.org>
Date: Wed, 18 Mar 2020 10:35:26 +0100
Subject: [PATCH] ath79: improve mikrotik-caldata.sh
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

Reduce unnecessary flash wear and be tidy:
- Run the extraction only if necessary
- Extract temporary file to /tmp
- cleanup after execution

Tested-by: Roger Pueyo Centelles <roger.pueyo@guifi.net>
Signed-off-by: Thibaut VARÈNE <hacks@slashdirt.org>
---
 .../base-files/lib/functions/mikrotik-caldata.sh         | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/target/linux/ath79/mikrotik/base-files/lib/functions/mikrotik-caldata.sh b/target/linux/ath79/mikrotik/base-files/lib/functions/mikrotik-caldata.sh
index 9c4016ee5d..71a1bf02f3 100644
--- a/target/linux/ath79/mikrotik/base-files/lib/functions/mikrotik-caldata.sh
+++ b/target/linux/ath79/mikrotik/base-files/lib/functions/mikrotik-caldata.sh
@@ -9,13 +9,18 @@ mikrotik_caldata_extract() {
 	local offset=$(($2))
 	local count=$(($3))
 	local mtd
-	local erdfile="/lib/firmware/erd.bin"
+	local erdfile="/tmp/erd.bin"
+	local fwfile="/lib/firmware/${FIRMWARE}"
+
+	[ -e $fwfile ] && exit 0
 
 	mtd=$(find_mtd_chardev $part)
 	[ -n "$mtd" ] || caldata_die "no mtd device found for partition $part"
 
 	rbextract -e $mtd $erdfile
 
-	dd if=$erdfile of=/lib/firmware/$FIRMWARE iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
+	dd if=$erdfile of=$fwfile iflag=skip_bytes bs=$count skip=$offset count=1 2>/dev/null || \
 		caldata_die "failed to extract calibration data from $mtd"
+
+	rm -f $erdfile
 }
-- 
2.30.2