Managing U-Boot Environment from Linux Print

 

U-Boot makes use of the so-called environment variables to define various aspects of the target functionality. The U-Boot environment is stored in Flash memory and is persistent across power or reset cycles. Parameters defined by the U-boot environment variables include: target IP address, target MAC address, location in RAM where a Linux bootable image will be loaded, and many others.

In various use case scenarios, it may be needed to read or update U-Boot environment variables from Linux. Probably, the most obvious scenario would be when a remote target is accessible only through an Ethernet or WiFi link and no access to the serial console is available. In that scenario, it is impossible to get access to the U-Boot serial line command monitor, however the target still can be connected to over a telnet or ssh link providing access to the Linux shell interface. This application note explains how to read and update U-Boot environment variables from Linux.

The Linux utilities are called fw_printenv (read U-Boot environment) and fw_setenv (modify U-Boot environment). The two utitlies are in fact implemented as a single utility, and the name of the executable file is used to distinguish between the fw_printenv and fw_setenv use cases on the target.

When you boot the newly installed Linux image on the target, you will be able to read and update the U-Boot environment. Here are some examples.

The following command reads the whole U-Boot environment:

/ # fw_printenv
bootargs=stm32_platform=stm32f4x9-som console=ttyS0,115200 panic=10
....
bootdelay=1
/ #

Here is how you can read a specific environment variable:

/ # fw_printenv bootdelay
bootdelay=1
/ #

Here is how you change the value of an environment variable:

/ # fw_setenv bootdelay 5
/ #

Having updated bootdelay as shown above, on the next reset U-Boot will wait for 5 seconds for you to press a keyboard and enter the U-Boot command monitor before proceeding to boot Linux.

The source files of the fw_printenv utility reside in the U-boot source tree, which is located in the u-boot/ sub-directory in the Emcraft software distribution.

fw_setenv and fw_printenv are included in the default rootfs project. Here is how the utilities are built:

The U-Boot source tree is configured for the STM32F4 System-On-Module:

-bash-4.2$ . ./ACTIVATE.sh
-bash-4.2$ cd u-boot
-bash-4.2$ make distclean
-bash-4.2$ make stm-som_config
Configuring for stm-som board...
-bash-4.2$

The fw_printenv utility is built:

-bash-4.2$ make -s env CPPFLAGS="-Wall -DUSE_HOSTCC -I`pwd`/include \
-mcpu=cortex-m4 -mthumb"

-bash-4.2$ ls -lt tools/env/fw_printenv
-rwxr--r-- 1 vlad vlad 43796 2015-04-23 14:52 tools/env/fw_printenv

When run on the target, the utility makes use of a configuration file that defines the geomery of the Flash device and where the U-Boot environment is located in the Flash. Here is what the configuration file looks like for the STM32F4 System-On-Module:

-bash-4.2$ cd ../projects/rootfs
-bash-4.2$ cat etc/fw_env.config
# MTD device name Device offset Env. size Flash sector size Number of sectors
/dev/mtd0 0x0000 0x1000 0x20000

In the rootfs project, the fw_setenv and the configuration file are added to rootfs.initramfs:

...
file /bin/fw_printenv ${INSTALL_ROOT}/u-boot/tools/env/fw_printenv 755 0 0
slink /bin/fw_setenv fw_printenv 755 0 0
file /etc/fw_env.config ${INSTALL_ROOT}/u-boot/tools/env/fw_env.config 644 0 0
...