Setting up fw printenv to modify u-boot environment variables

From RidgeRun Developer Wiki

The fw_printenv and fw_setenv commands can be used to read and set U-Boot environment variables from Linux. This guide uses the RidgeRun SDK to enable these commands in your target board and configure the /etc/fw_env.config file accordingly.

Setup

Building the U-Boot env tools

Edit the $DEVDIR/bootloader/u-boot-*/Makefile and add a buildenv rule with the following commands.

buildenv:
  $(V) $(MAKE) -C src HOSTCC=$(TOOLCHAIN_PREFIX)-gcc env $(QOUT) $(ERRQOUT)
  $(V) cp src/tools/env/fw_printenv $(FSROOT)/usr/bin
  $(V) ( cd $(FSROOT)/usr/bin ; rm -f fw_setenv ; ln -s fw_printenv fw_setenv )

You may find interesting that we're setting HOSTCC to our cross-compiler. That's misleading indeed, but that's how maintainers thought of this (interesting reading).

After editing your file, you'll be able to build the tools, installing fw_printenv and fw_setenv in $(FSROOT)/usr/bin/.

cd $DEVDIR/bootloader/u-boot-*/
make buildenv

In order to enable the fw_printenv and fw_setenv commands, you will have to activate the option, into the config system, by toggling it in the following location:

Prompt: Bootloader user space environment tools
  Location:
    -> Bootloader configuration

Preparing the fw_env.config file

For the fw_env.config file you need to know the MTD device name, the device offset, the environment size, flash sector size, and the number of sectors (ignored in the NOR case).

  • MTD device name: the Linux command "cat /proc/mtd" can be helpful to determine the MTD device where u-boot is living in your board.
  • Device offset: in the u-boot source folder, look inside include/configs for the header file related to your board; for the DM36x leopard board, the file is davinci_dm368leopard.h. Search for CONFIG_ENV_OFFSET.
  • Environment size: same as above, but search for CONFIG_ENV_SIZE.
  • Flash sector size: in u-boot's prompt, the command "nand info" will display the sector size; example output: NAND 256MiB 3,3V 8-bit, sector size 128 KiB.
  • Number of sectors: this setting will be ignored in the NOR case, and possibly not necessary to set in other cases. Anyway, it is the division of the environment size, i.e. 0x40000, and the sector size, i.e. 0x20000 for 128Kb, resulting in this example that the environment takes 2 sectors (0x40000/0x20000).

fw_env.config file for known boards

Using RidgeRun's SDK, these /etc/fw_env.config files work out of the box. For simplicity, all of these files assume uboot lives in mtd0.

DM36x Leopard

# MTD device name   Device offset   Env. size   Flash sector size  Number of sectors
/dev/mtd0           0x3C0000        0x40000     0x20000            2

DM816x EVM

# MTD device name   Device offset   Env. size   Flash sector size  Number of sectors
/dev/mtd4           0x260000        0x2000      0x20000            1

DM816x Z3

# MTD device name   Device offset   Env. size   Flash sector size  Number of sectors
/dev/mtd0           0x260000        0x2000      0x20000            1

Using the env tools

The Linux command fw_printenv is the homologous of U-boot's printenv command.

# / fw_printenv
autoload=no
autostart=yes
baudrate=115200
...

To print a single variable/

# / fw_printenv autoload
autoload=no

Then fw_setenv is the homologous of U-boot's setenv command.

# / fw_setenv autoload yes
# / fw_printenv autoload
autoload=yes

References

U-Boot Environment variables in Linux