From b16e14a220fcab19e3145328057e7188f00b6a17 Mon Sep 17 00:00:00 2001
From: Tony Ambardar <itugrok@yahoo.com>
Date: Sat, 11 Nov 2023 04:29:26 -0800
Subject: [PATCH] image: use helper function for size units

Add the make function 'exp_units' for helping evaluate k/m/g size units in
expressions, and use this to consistently replace many ad hoc substitutions
like '$(subst k,* 1024,$(subst m, * 1024k,$(IMAGE_SIZE)))' in makefiles.

Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
---
 include/image-commands.mk             | 8 ++++----
 include/image.mk                      | 2 ++
 target/linux/ath79/image/generic.mk   | 2 +-
 target/linux/ath79/image/nand.mk      | 2 +-
 target/linux/ipq40xx/image/generic.mk | 2 +-
 target/linux/mediatek/image/mt7622.mk | 2 +-
 target/linux/ramips/image/Makefile    | 4 ++--
 7 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/include/image-commands.mk b/include/image-commands.mk
index d07c8827619..0f292d15ee3 100644
--- a/include/image-commands.mk
+++ b/include/image-commands.mk
@@ -215,7 +215,7 @@ endef
 
 define Build/check-size
 	@imagesize="$$(stat -c%s $@)"; \
-	limitsize="$$(($(subst k,* 1024,$(subst m, * 1024k,$(if $(1),$(1),$(IMAGE_SIZE))))))"; \
+	limitsize="$$(($(call exp_units,$(if $(1),$(1),$(IMAGE_SIZE)))))"; \
 	[ $$limitsize -ge $$imagesize ] || { \
 		$(call ERROR_MESSAGE,    WARNING: Image file $@ is too big: $$imagesize > $$limitsize); \
 		rm -f $@; \
@@ -466,8 +466,8 @@ endef
 define Build/pad-offset
 	let \
 		size="$$(stat -c%s $@)" \
-		pad="$(subst k,* 1024,$(word 1, $(1)))" \
-		offset="$(subst k,* 1024,$(word 2, $(1)))" \
+		pad="$(call exp_units,$(word 1, $(1)))" \
+		offset="$(call exp_units,$(word 2, $(1)))" \
 		pad="(pad - ((size + offset) % pad)) % pad" \
 		newsize='size + pad'; \
 		dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
@@ -629,7 +629,7 @@ endef
 
 define Build/zyxel-ras-image
 	let \
-		newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
+		newsize="$(call exp_units,$(RAS_ROOTFS_SIZE))"; \
 		$(STAGING_DIR_HOST)/bin/mkrasimage \
 			-b $(RAS_BOARD) \
 			-v $(RAS_VERSION) \
diff --git a/include/image.mk b/include/image.mk
index 4ebff2e9ae0..ef52337deea 100644
--- a/include/image.mk
+++ b/include/image.mk
@@ -20,6 +20,8 @@ include $(INCLUDE_DIR)/rootfs.mk
 override MAKE:=$(_SINGLE)$(SUBMAKE)
 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
 
+exp_units = $(subst k, * 1024,$(subst m, * 1024k,$(subst g, * 1024m,$(1))))
+
 target_params = $(subst +,$(space),$*)
 param_get = $(patsubst $(1)=%,%,$(filter $(1)=%,$(2)))
 param_get_default = $(firstword $(call param_get,$(1),$(2)) $(3))
diff --git a/target/linux/ath79/image/generic.mk b/target/linux/ath79/image/generic.mk
index e435806c7bb..68c41055615 100644
--- a/target/linux/ath79/image/generic.mk
+++ b/target/linux/ath79/image/generic.mk
@@ -85,7 +85,7 @@ define Build/mkmylofw_16m
 
 	let \
 		size="$$(stat -c%s $@)" \
-		pad="$(subst k,* 1024,$(BLOCKSIZE))" \
+		pad="$(call exp_units,$(BLOCKSIZE))" \
 		pad="(pad - (size % pad)) % pad" \
 		newsize='size + pad' ; \
 		[ $$newsize -lt $$((0x660000)) ] && newsize=0x660000 ; \
diff --git a/target/linux/ath79/image/nand.mk b/target/linux/ath79/image/nand.mk
index 5c9e190e193..bf2b1a1f8ab 100644
--- a/target/linux/ath79/image/nand.mk
+++ b/target/linux/ath79/image/nand.mk
@@ -32,7 +32,7 @@ endef
 
 define Build/zyxel-factory
 	let \
-		maxsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
+		maxsize="$(call exp_units,$(RAS_ROOTFS_SIZE))"; \
 		let size="$$(stat -c%s $@)"; \
 		if [ $$size -lt $$maxsize ]; then \
 			$(STAGING_DIR_HOST)/bin/mkrasimage \
diff --git a/target/linux/ipq40xx/image/generic.mk b/target/linux/ipq40xx/image/generic.mk
index 1757684a7c1..b4719033c0a 100644
--- a/target/linux/ipq40xx/image/generic.mk
+++ b/target/linux/ipq40xx/image/generic.mk
@@ -68,7 +68,7 @@ define Build/mkmylofw_32m
 
 	let \
 		size="$$(stat -c%s $@)" \
-		pad="$(subst k,* 1024,$(BLOCKSIZE))" \
+		pad="$(call exp_units,$(BLOCKSIZE))" \
 		pad="(pad - (size % pad)) % pad" \
 		newsize='size + pad'; \
 		$(STAGING_DIR_HOST)/bin/mkmylofw \
diff --git a/target/linux/mediatek/image/mt7622.mk b/target/linux/mediatek/image/mt7622.mk
index e362e7428bd..d0f5280dcf7 100644
--- a/target/linux/mediatek/image/mt7622.mk
+++ b/target/linux/mediatek/image/mt7622.mk
@@ -21,7 +21,7 @@ define Build/buffalo-trx
 		-f $(kern_bin) \
 		$(if $(rtfs_bin),\
 			-a 0x20000 \
-			-b $$(( $(subst k, * 1024,$(kern_size)) )) \
+			-b $$(( $(call exp_units,$(kern_size)) )) \
 			-f $(rtfs_bin),) \
 		$(if $(apnd_bin),\
 			-A $(apnd_bin) \
diff --git a/target/linux/ramips/image/Makefile b/target/linux/ramips/image/Makefile
index 265e9fb5e0c..fdc15aa1efc 100644
--- a/target/linux/ramips/image/Makefile
+++ b/target/linux/ramips/image/Makefile
@@ -31,7 +31,7 @@ KERNEL_DTB = kernel-bin | append-dtb | lzma
 
 define Build/jcg-header
 	$(STAGING_DIR_HOST)/bin/jcgimage -v $(1) \
-		$(if $(JCG_MAXSIZE), -m $$(($(subst k, * 1024,$(JCG_MAXSIZE)))),) \
+		$(if $(JCG_MAXSIZE), -m $$(($(call exp_units,$(JCG_MAXSIZE)))),) \
 		-u $@ -o $@.new
 	mv $@.new $@
 endef
@@ -142,7 +142,7 @@ endef
 define Build/trx
 	$(STAGING_DIR_HOST)/bin/trx $(1) \
 		-o $@ \
-		-m $$(($(subst k, * 1024,$(IMAGE_SIZE)))) \
+		-m $$(($(call exp_units,$(IMAGE_SIZE)))) \
 		-f $(IMAGE_KERNEL) \
 		-a 4 -f $(IMAGE_ROOTFS)
 endef