From 5126a6049b304726cd7b51cda40f7234094c4b0d Mon Sep 17 00:00:00 2001 From: Hector Palacios Date: Thu, 8 Jun 2017 13:56:33 +0200 Subject: [PATCH] u-boot-fw-utils: dynamically create fw_env.config for NAND-based platforms A mechanism was introduced in U-Boot to dynamically locate the environment copies within a NAND partition, skipping bad blocks and making full use of the partition blocks. To trigger this mechanism in Linux tools (fw_printenv/fw_setenv) the configuration file /etc/fw_env.config must have both copies entries pointing to the same offset. This commit substitutes the hardcoded configuration file for CC6UL with a stub and generates one on an RPM post-script basing on: - Supplied DEY variables (with default values per platform): - UBOOT_ENV_OFFSET: starting offset of the environment within the partition. - UBOOT_ENV_SIZE: size of each copy of the environment. - UBOOT_ENV_RANGE: size in the partition that the environment copies may occupy, starting from UBOOT_ENV_OFFSET. If undefined, the whole partition is used. - Calculated values: - NAND Erase block size - Number of blocks available for the environment copies Signed-off-by: Hector Palacios https://jira.digi.com/browse/DEL-4256 --- meta-digi-arm/conf/machine/ccimx6ulsbc.conf | 9 ++++ .../conf/machine/ccimx6ulstarter.conf | 9 ++++ .../u-boot-fw-utils/ccimx6ul/fw_env.config | 8 ++-- .../u-boot/u-boot-fw-utils_%.bbappend | 43 ++++++++++++++++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/meta-digi-arm/conf/machine/ccimx6ulsbc.conf b/meta-digi-arm/conf/machine/ccimx6ulsbc.conf index 224813415..33664cb87 100644 --- a/meta-digi-arm/conf/machine/ccimx6ulsbc.conf +++ b/meta-digi-arm/conf/machine/ccimx6ulsbc.conf @@ -11,6 +11,15 @@ UBOOT_CONFIG ??= "ccimx6ulsbc1GB ccimx6ulsbc" UBOOT_CONFIG[ccimx6ulsbc1GB] = "ccimx6ulsbc1GB_defconfig" UBOOT_CONFIG[ccimx6ulsbc] = "ccimx6ulsbc_defconfig" +# U-Boot environment offset (within partition) +UBOOT_ENV_OFFSET ?= "0x0" +# U-Boot environment size +UBOOT_ENV_SIZE ?= "0x20000" +# U-Boot environment range: size (in hex) in the environment partition that +# the U-Boot environment can take up (if undefined, it will take up all the +# available space in the environment partition) +UBOOT_ENV_RANGE ?= "" + KERNEL_DEVICETREE ?= " \ imx6ul-ccimx6ulsbc.dtb \ imx6ul-ccimx6ulsbc-wb.dtb \ diff --git a/meta-digi-arm/conf/machine/ccimx6ulstarter.conf b/meta-digi-arm/conf/machine/ccimx6ulstarter.conf index 317b38ee5..f7b95048b 100644 --- a/meta-digi-arm/conf/machine/ccimx6ulstarter.conf +++ b/meta-digi-arm/conf/machine/ccimx6ulstarter.conf @@ -11,6 +11,15 @@ UBOOT_CONFIG ??= "ccimx6ulstarter1GB ccimx6ulstarter" UBOOT_CONFIG[ccimx6ulstarter1GB] = "ccimx6ulstarter1GB_defconfig" UBOOT_CONFIG[ccimx6ulstarter] = "ccimx6ulstarter_defconfig" +# U-Boot environment offset (within partition) +UBOOT_ENV_OFFSET ?= "0x0" +# U-Boot environment size +UBOOT_ENV_SIZE ?= "0x20000" +# U-Boot environment range: size (in hex) in the environment partition that +# the U-Boot environment can take up (if undefined, it will take up all the +# available space in the environment partition) +UBOOT_ENV_RANGE ?= "" + KERNEL_DEVICETREE ?= " \ imx6ul-ccimx6ulstarter.dtb \ imx6ul-ccimx6ulstarter-wb.dtb \ diff --git a/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils/ccimx6ul/fw_env.config b/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils/ccimx6ul/fw_env.config index be29acc2f..e31ceca4d 100644 --- a/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils/ccimx6ul/fw_env.config +++ b/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils/ccimx6ul/fw_env.config @@ -1,7 +1,9 @@ # Configuration file for fw_(printenv/setenv) utility. # Up to two entries are valid, in this case the redundant # environment sector is assumed present. +# If both copies are set to the same offset, an automatic mechanism will +# determine the first good sectors where each copy lives, skipping bad blocks. -# Device name Offset Size -/dev/mtd1 0x0 0x20000 -/dev/mtd1 0x20000 0x20000 +# Device name Offset Size Erase-size No.Blocks +/dev/##MTDINDEX## ##ENV_OFFSET## ##ENV_SIZE## ##ERASEBLOCK## ##NBLOCKS## +/dev/##MTDINDEX## ##ENV_REDUND_OFFSET## ##ENV_SIZE## ##ERASEBLOCK## ##NBLOCKS## diff --git a/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils_%.bbappend b/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils_%.bbappend index 0f5236f84..547138192 100644 --- a/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils_%.bbappend +++ b/meta-digi-arm/recipes-bsp/u-boot/u-boot-fw-utils_%.bbappend @@ -41,9 +41,50 @@ pkg_postinst_${PN}() { if [ x"$D" != "x" ]; then exit 1 fi + CONFIG_FILE="/etc/fw_env.config" MMCDEV="$(sed -ne 's,.*root=/dev/mmcblk\([0-9]\)p.*,\1,g;T;p' /proc/cmdline)" if [ -n "${MMCDEV}" ]; then - sed -i -e "s,^/dev/mmcblk[^[:blank:]]\+,/dev/mmcblk${MMCDEV},g" /etc/fw_env.config + sed -i -e "s,^/dev/mmcblk[^[:blank:]]\+,/dev/mmcblk${MMCDEV},g" ${CONFIG_FILE} + fi + + PARTTABLE="/proc/mtd" + MTDINDEX="$(sed -ne "s/\(^mtd[0-9]\+\):.*\.*/\1/g;T;p" ${PARTTABLE} 2>/dev/null)" + if [ -n "${MTDINDEX}" ]; then + # Initialize variables for fixed offset values + # (backwards compatible with old U-Boot) + ENV_OFFSET="${UBOOT_ENV_OFFSET}" + ENV_REDUND_OFFSET="${UBOOT_ENV_SIZE}" + ENV_SIZE="${UBOOT_ENV_SIZE}" + ERASEBLOCK="" + NBLOCKS="" + + if [ -f "/proc/device-tree/digi,uboot,dynamic-env" ]; then + # Update variables for dynamic environment + # - Both copies starting at the same offset + ENV_REDUND_OFFSET="${UBOOT_ENV_OFFSET}" + # - Calculated erase block size + ERASEBLOCK="$(grep "^${MTDINDEX}" ${PARTTABLE} | awk '{printf("0x%d",$3)}')" + # - Calculated number of blocks + MTDSIZE="$(grep "^${MTDINDEX}" ${PARTTABLE} | awk '{printf("0x%d",$2)}')" + NBLOCKS="$(((MTDSIZE - UBOOT_ENV_OFFSET) / ERASEBLOCK))" + # If a range was provided, calculate the number of + # blocks in the range and use that number, unless they + # exceed the total number of blocks available in the + # whole partition. + if [ -n "${UBOOT_ENV_RANGE}" ]; then + RANGE_BLOCKS="$((UBOOT_ENV_RANGE / ERASEBLOCK))" + [ "${RANGE_BLOCKS}" -lt "${NBLOCKS}" ] && NBLOCKS="${RANGE_BLOCKS}" + fi + fi + + # Substitute stub with configuration and calculated values + sed -i -e "s/##MTDINDEX##/${MTDINDEX}/g" \ + -e "s/##ENV_OFFSET##/${ENV_OFFSET}/g" \ + -e "s/##ENV_REDUND_OFFSET##/${ENV_REDUND_OFFSET}/g" \ + -e "s/##ENV_SIZE##/${ENV_SIZE}/g" \ + -e "s/##ERASEBLOCK##/${ERASEBLOCK}/g" \ + -e "s/##NBLOCKS##/${NBLOCKS}/g" \ + ${CONFIG_FILE} fi }