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 <hector.palacios@digi.com>

https://jira.digi.com/browse/DEL-4256
This commit is contained in:
Hector Palacios 2017-06-08 13:56:33 +02:00
parent 3d4f6dffd0
commit 5126a6049b
4 changed files with 65 additions and 4 deletions

View File

@ -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 \

View File

@ -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 \

View File

@ -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##

View File

@ -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]\+\):.*\<environment\>.*/\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
}