From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) by mx.groups.io with SMTP id smtpd.web11.3097.1713980690551311366 for ; Wed, 24 Apr 2024 10:44:50 -0700 Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-69b514d3cf4so1905126d6.0 for ; Wed, 24 Apr 2024 10:44:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713980689; x=1714585489; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=xaw4c4w7JTl0nd/sJoVJWkfsPwTMlK//ygisi52eTRY=; b=B93Zgqtt5HdZ3jybaOlKAyuiPeKI36nMHi1FEebcBD98/Gae2DGuFTo4GGr0IWCbII px0Oz5ugYXBX3ZkuMWGSjTYfkq/R1zGuPzYbHsECS4BN+XParxkSvAZ2FEU+VWk4sRxm f5iHkPYC3mOZ+yiaHlVAbmhF9pcJrWy4I1IoPVcDQTqYS06FPgDIuBqfiMsOFzYWILuS 5A5vxcg8GSfXm3ki3Ap7M6wNiPCVAohEuk5tjZauwTfU36K2dn04YunBuX0JnNONd/0u ovDjWk7d21F0Yex6vdvmzKS/w466qWQ2UYO0zYpnen5v8Z9Kvrn2FnfN+aRBQDAGTSqP OhNg== Return-Path: Received: from localhost.localdomain (pppoe-209-91-167-254.vianet.ca. [209.91.167.254]) by smtp.gmail.com with ESMTPSA id p11-20020a0c9a0b000000b0069b32845235sm6323093qvd.85.2024.04.24.10.44.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 10:44:48 -0700 (PDT) From: "Trevor Woerner" Subject: [meta-rockchip][PATCH v3] enable stored U-Boot environment Date: Wed, 24 Apr 2024 13:44:44 -0400 Message-ID: <20240424174444.37244-1-twoerner@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: yocto-patches@lists.yoctoproject.org List-ID: U-Boot has the ability to store its environment variables to a permanent storage device. Whether or not it does so for any one specific device depends on whatever settings are enabled in that specific device's defconfig. In order to definitively configure U-Boot to be able to store its environment into the device from which it boots, for any device supported in this BSP, simply add the following to MACHINE_FEATURES: rk-u-boot-env If enabled, there is now a second choice to make: should the build also include the U-Boot environment in the image or not? The default environment, as generated by U-Boot, can be included in the generated wic image. If it is included, then flashing the image will also flash the default U-Boot environment variables and settings, wiping out anything that might have been there already. If it is not included then your device will either continue using whatever environment happens to be there (if valid), or will not use any stored environment if the stored environment has not been set or is invalid. The variable which governs this behaviour is: RK_IMAGE_INCLUDES_UBOOT_ENV By default this is set to "0", meaning that by default the image does not contain the U-Boot environment. To enable this behaviour, enable this variable. This variable only takes effect if rk-u-boot-env is listed in MACHINE_FEATURES, and has no effect otherwise. The script: scripts/dump-uboot-env-from-yocto-image.sh can be used on a rockchip wic image to see the contents of the U-Boot environment partition at build time. Tested by booting the same image on both eMMC and SDcard with the following devices, verifying the ability to read and write the U-Boot environment in both U-Boot and Linux user-space, and that changes made in one are seen in the other: rock-3a rock-5a rock-5b rock-pi-4b rock-pi-e rock64 Signed-off-by: Trevor Woerner --- v3 changes: - switch from using a bbclass so the `rk-u-boot-env` override can be used directly - add SPDX header to script - drop `inherit deploy` in U-Boot bbappend v2 changes: - re-word the commit message and README for clarity - use bash's built-in math handling instead of depending on `bc` - in anticipation of the upcoming feature in U-Boot whereby rockchip devices can automatically select the environment storage device to be the same as the boot device, use a more recent SRCREV for builds that do environment handling, instead of hardcoding the environment storage device by SoC family - handle RK_IMAGE_INCLUDES_UBOOT_ENV as a boolean --- README | 20 +++++++++++++ conf/machine/include/rockchip-wic.inc | 11 +++++++ .../rockchip-enable-environment-mmc.cfg | 6 ++++ recipes-bsp/u-boot/u-boot_%.bbappend | 19 ++++++++++++ scripts/dump-uboot-env-from-yocto-image.sh | 29 +++++++++++++++++++ wic/rockchip.wks | 2 +- 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 recipes-bsp/u-boot/files/rk-u-boot-env/rockchip-enable-environment-mmc.cfg create mode 100755 scripts/dump-uboot-env-from-yocto-image.sh diff --git a/README b/README index c76fc9131276..605773d4ecd3 100644 --- a/README +++ b/README @@ -67,6 +67,26 @@ Notes: in the configuration (e.g. conf/local.conf). +U-Boot Environment: +------------------ + In order to configure U-Boot to be able to store its environment into the + device from which it was booted, for any device supported in this BSP, + simply add the following to MACHINE_FEATURES: + + rk-u-boot-env + + If enabled, to additionally have the U-Boot environment generated and + stored in the image, also enable the following variable (default: off): + + RK_IMAGE_INCLUDES_UBOOT_ENV + + The script: + + scripts/dump-uboot-env-from-yocto-image.sh + + can be used on a rockchip wic image to see the contents of the U-Boot + environment partition at build time. + Maintenance: ----------- Please send pull requests, patches, comments, or questions to the diff --git a/conf/machine/include/rockchip-wic.inc b/conf/machine/include/rockchip-wic.inc index 147a36685d7d..18b75a2de945 100644 --- a/conf/machine/include/rockchip-wic.inc +++ b/conf/machine/include/rockchip-wic.inc @@ -2,6 +2,11 @@ require conf/machine/include/rockchip-extlinux.inc +# 'rk-u-boot-env' indicates the user wants to be able to save their U-Boot +# environment back to the drive from which the device was booted +MACHINEOVERRIDES .= "${@bb.utils.contains('MACHINE_FEATURES', 'rk-u-boot-env', ':rk-u-boot-env', '', d)}" +IMAGE_INSTALL:append:rk-u-boot-env = " u-boot-fw-utils u-boot-env" + SPL_BINARY ?= "idbloader.img" IMAGE_FSTYPES += "wic wic.bmap" @@ -11,7 +16,13 @@ WKS_FILE_DEPENDS ?= " \ virtual/bootloader \ " +RK_IMAGE_INCLUDES_UBOOT_ENV ?= "no" +RK_UBOOT_ENV = " " +RK_UBOOT_ENV:rk-u-boot-env = "${@ '--source rawcopy --sourceparams=file=u-boot.env' if \ + bb.utils.to_boolean(d.getVar('RK_IMAGE_INCLUDES_UBOOT_ENV'), False) else ' '}" + WICVARS:append = " \ SPL_BINARY \ UBOOT_SUFFIX \ + RK_UBOOT_ENV \ " diff --git a/recipes-bsp/u-boot/files/rk-u-boot-env/rockchip-enable-environment-mmc.cfg b/recipes-bsp/u-boot/files/rk-u-boot-env/rockchip-enable-environment-mmc.cfg new file mode 100644 index 000000000000..778772d27767 --- /dev/null +++ b/recipes-bsp/u-boot/files/rk-u-boot-env/rockchip-enable-environment-mmc.cfg @@ -0,0 +1,6 @@ +CONFIG_ENV_SIZE=0x8000 +CONFIG_ENV_OFFSET=0x3f8000 +# CONFIG_ENV_IS_NOWHERE is not set +CONFIG_ENV_IS_IN_MMC=y +CONFIG_DM_SEQ_ALIAS=y +CONFIG_SPL_DM_SEQ_ALIAS=y diff --git a/recipes-bsp/u-boot/u-boot_%.bbappend b/recipes-bsp/u-boot/u-boot_%.bbappend index a83179a9f007..d63042b9be41 100644 --- a/recipes-bsp/u-boot/u-boot_%.bbappend +++ b/recipes-bsp/u-boot/u-boot_%.bbappend @@ -1,7 +1,13 @@ +FILESEXTRAPATHS:prepend := "${THISDIR}/files:" + +SRC_URI:append:rk-u-boot-env = " file://rockchip-enable-environment-mmc.cfg" +SRCREV:rk-u-boot-env = "cdfcc37428e06f4730ab9a17cc084eeb7676ea1a" + # various machines require the pyelftools library for parsing dtb files DEPENDS:append = " python3-pyelftools-native" DEPENDS:append:rk3308 = " u-boot-tools-native" DEPENDS:append:rock-pi-4 = " gnutls-native" +DEPENDS:append:rk-u-boot-env = " u-boot-mkenvimage-native" EXTRA_OEMAKE:append:px30 = " BL31=${DEPLOY_DIR_IMAGE}/bl31-px30.elf" EXTRA_OEMAKE:append:rk3308 = " \ @@ -34,3 +40,16 @@ do_compile:append:rock2-square () { cp ${B}/spl/${SPL_BINARY} ${B} fi } + +do_compile:append:rk-u-boot-env() { + UBOOT_ENV_SIZE="$(cat ${B}/.config | grep "^CONFIG_ENV_SIZE=" | cut -d'=' -f2)" + echo "/dev/disk/by-partlabel/uboot_env 0x0000 ${UBOOT_ENV_SIZE}" > ${WORKDIR}/fw_env.config +} + +do_deploy:append:rk-u-boot-env() { + UBOOT_ENV_SIZE="$(cat ${B}/.config | grep "^CONFIG_ENV_SIZE=" | cut -d'=' -f2)" + mkenvimage -s ${UBOOT_ENV_SIZE} ${B}/u-boot-initial-env -o ${WORKDIR}/u-boot.env + + install -d ${DEPLOYDIR} + install -m 0644 ${WORKDIR}/u-boot.env ${DEPLOYDIR} +} diff --git a/scripts/dump-uboot-env-from-yocto-image.sh b/scripts/dump-uboot-env-from-yocto-image.sh new file mode 100755 index 000000000000..9386c1d29213 --- /dev/null +++ b/scripts/dump-uboot-env-from-yocto-image.sh @@ -0,0 +1,29 @@ +#/bin/bash +# SPDX-License-Identifier: OSL-3.0 +# +# a program that can take a wic file and dump out the contents +# of the U-Boot environment in canonical hex+ascii format +# (assuming the "rockchip" layout specified in this layer's wic file) + +# check for programs +check_pgm() { + $1 --help > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "required program \"$1\" not found" + exit 1 + fi +} +check_pgm dd +check_pgm hexdump + +if [ $# -ne 1 ]; then + echo "required param missing: yocto wic image" + exit 1 +fi +if [ ! -e "$1" ]; then + echo "specified file \"$1\" not found" + exit 1 +fi + +SKIP=$(( 8128 * 512 )) +dd if="$1" ibs=1 skip=$SKIP count=32k 2> /dev/null | hexdump -C diff --git a/wic/rockchip.wks b/wic/rockchip.wks index 9ba3352b51bb..bc65f73b0cf3 100644 --- a/wic/rockchip.wks +++ b/wic/rockchip.wks @@ -22,7 +22,7 @@ part loader1 --offset 64s --fixed-size 3552K --fstype=none --part-name load part v_storage --offset 7168s --fixed-size 256K --fstype=none --part-name v_storage part reserved --offset 7680s --fixed-size 192K --fstype=none --part-name reserved part reserved1 --offset 8064s --fixed-size 32K --fstype=none --part-name reserved1 -part uboot_env --offset 8128s --fixed-size 32K --fstype=none --part-name uboot_env +part uboot_env --offset 8128s --fixed-size 32K --fstype=none --part-name uboot_env ${RK_UBOOT_ENV} part reserved2 --offset 8192s --fixed-size 4096K --fstype=none --part-name reserved2 part loader2 --offset 16384s --fixed-size 4096K --fstype=none --part-name loader2 --source rawcopy --sourceparams="file=u-boot.${UBOOT_SUFFIX}" part atf --offset 24576s --fixed-size 4096K --fstype=none --part-name atf -- 2.44.0.478.g7774cfed6261