我正在尝试添加两个新的 u-boot 环境变量
...
当我启动设备并进入 U-Boot 时,我运行 printenv 并且我的两个新变量不存在。我错过了什么小步骤?
配置选项(例如 CONFIG_XXX)和环境变量之间的关系并不像您想象的那样相关。仅使用一组已知的配置选项(在将它们转换为预处理器宏之后)来组成默认环境。 (由于配置选项(在.config)和宏(在包含/生成/autoconf.h)看起来相同,一些开发人员忘记或不知道有一个转换过程。)因此您根本无法通过新的配置选项(或宏)创建自己的环境变量。
要查看使用哪些配置选项来组成默认环境,请参阅包含/env_default.h https://elixir.bootlin.com/u-boot/v2021.04/source/include/env_default.h.
U-Boot 实际上有几组环境变量,具体来说是默认值、活动值和保存值。
-
默认环境是一组环境变量
在构建时定义,并保留在二进制映像中。
-
保存的环境是一组环境变量
保留在持久存储中。它们必须使用saveenv
命令,并使用 CRC32 校验字进行验证。
-
活动环境是一组环境变量
U-Boot 执行时保存在 RAM 中。
启动时,U-Boot 尝试使用已保存的环境填充(空)活动环境,但前提是 CRC32 校验字确认已保存环境的完整性。
验证成功后,持久存储中的环境变量(静默)将成为活动环境。
当验证失败时,会显示一条通知“警告:错误的 CRC,使用默认环境”,并且默认环境中的环境变量将变为活动环境。
The printenv
命令(仅)显示活动环境变量集。
构建 U-Boot 时,定义了一组默认环境变量。也可以选择创建一组已保存的环境变量(例如,预先构建的u-boot.env可以与新版本一起安装的文件u-boot.bin可执行文件)使用mkenv图像 https://elixir.bootlin.com/u-boot/latest/source/tools/mkenvimage.c tool.
将新环境变量添加到默认环境的一种方法是使用 CONFIG_EXTRA_ENV_SETTINGS。来自READMEU-Boot 源文件中:
CONFIG_EXTRA_ENV_SETTINGS
Define this to contain any number of null terminated
strings (variable = value pairs) that will be part of
the default environment compiled into the boot image.
For example, place something like this in your
board's config file:
#define CONFIG_EXTRA_ENV_SETTINGS \
"myvar1=value1\0" \
"myvar2=value2\0"
Warning: This method is based on knowledge about the
internal format how the environment is stored by the
U-Boot code. This is NOT an official, exported
interface! Although it is unlikely that this format
will change soon, there is no guarantee either.
You better know what you are doing here.
Note: overly (ab)use of the default environment is
discouraged. Make sure to check other ways to preset
the environment like the "source" command or the
boot command first.
请注意,CONFIG_EXTRA_ENV_SETTINGS 通常仅定义为宏,而不用作配置选项。例如参考包括/配置/mx6ullevk.h https://elixir.bootlin.com/u-boot/v2021.04/source/include/configs/mx6ullevk.h#L46.
将新环境变量添加到默认环境的另一种方法是使用 CONFIG_USE_DEFAULT_ENV_FILE (这是一个配置选项)。其Kconfig描述为:
CONFIG_USE_DEFAULT_ENV_FILE:
Normally, the default environment is automatically generated
based on the settings of various CONFIG_* options, as well
as the CONFIG_EXTRA_ENV_SETTINGS. By selecting this option,
you can instead define the entire default environment in an
external file.