image: improve UBI image sizing on NAND devices
authorTony Ambardar <itugrok@yahoo.com>
Sat, 11 Nov 2023 12:46:14 +0000 (04:46 -0800)
committerHauke Mehrtens <hauke@hauke-m.de>
Sun, 26 Nov 2023 17:37:20 +0000 (18:37 +0100)
Many NAND devices use a build recipe with "append-ubi | check-size" to
ensure factory images don't exceed the target flash partition size.
However, UBI reserves space for bad block handling and other operational
overhead, and thus 'check-size' can overestimate the space available by
several MB. In practice, this means a failed check is definitely a failure,
while a passing check is only probably a pass.

Improve the situation by teaching 'Build/append-ubi' to check image sizes
while accounting for UBI reserved blocks. Add new device variable NAND_SIZE
and use with existing IMAGE_SIZE to derate the available space. Each UBI
device reserves 20 PEBs per 1024 PEBs of the entire NAND device for bad
blocks, plus an additional 4 PEBs overhead.

Many devices can transparently enable this check by setting NAND_SIZE based
on their flash storage, and may then remove any unneeded 'check-size'.

Link: http://www.linux-mtd.infradead.org/doc/ubi.html#L_overhead
Suggested-by: Shiji Yang <yangshiji66@qq.com>
Suggested-by: Robert Marko <robimarko@gmail.com>
Signed-off-by: Tony Ambardar <itugrok@yahoo.com>
include/image-commands.mk
include/image.mk

index 7d9f93fed741053d72574cc02124d849ab65c4b3..41a5e1198a95a098aef1ff6931f37ee917d721dc 100644 (file)
@@ -133,6 +133,8 @@ define Build/append-md5sum-ascii-salted
        rm $@.salted
 endef
 
+UBI_NAND_SIZE_LIMIT = $(IMAGE_SIZE) - ($(NAND_SIZE)*20/1024 + 4*$(BLOCKSIZE))
+
 define Build/append-ubi
        sh $(TOPDIR)/scripts/ubinize-image.sh \
                $(if $(UBOOTENV_IN_UBI),--uboot-env) \
@@ -146,6 +148,8 @@ define Build/append-ubi
                $(UBINIZE_OPTS)
        cat $@.tmp >> $@
        rm $@.tmp
+       $(if $(and $(IMAGE_SIZE),$(NAND_SIZE)),\
+               $(call Build/check-size,$(UBI_NAND_SIZE_LIMIT)))
 endef
 
 define Build/ubinize-kernel
index ef52337deea3369e5e898b689994c2c9ec7180f2..096ccb5f1858c2921fac563261fee133e8c6f38b 100644 (file)
@@ -397,6 +397,7 @@ define Device/Init
   DEVICE_IMG_NAME = $$(DEVICE_IMG_PREFIX)-$$(1)-$$(2)
   FACTORY_IMG_NAME :=
   IMAGE_SIZE :=
+  NAND_SIZE :=
   KERNEL_PREFIX = $$(DEVICE_IMG_PREFIX)
   KERNEL_SUFFIX := -kernel.bin
   KERNEL_INITRAMFS_SUFFIX = $$(KERNEL_SUFFIX)
@@ -457,7 +458,7 @@ DEFAULT_DEVICE_VARS := \
   DEVICE_DTS_DIR DEVICE_DTS_OVERLAY DEVICE_DTS_LOADADDR \
   DEVICE_FDT_NUM DEVICE_IMG_PREFIX SOC BOARD_NAME UIMAGE_MAGIC UIMAGE_NAME \
   UIMAGE_TIME SUPPORTED_DEVICES IMAGE_METADATA KERNEL_ENTRY KERNEL_LOADADDR \
-  UBOOT_PATH IMAGE_SIZE \
+  UBOOT_PATH IMAGE_SIZE NAND_SIZE \
   FACTORY_IMG_NAME FACTORY_SIZE \
   DEVICE_PACKAGES DEVICE_COMPAT_VERSION DEVICE_COMPAT_MESSAGE \
   DEVICE_VENDOR DEVICE_MODEL DEVICE_VARIANT \