u-boot environment variables in linux

U-boot makes use of environment variable which can be read and set from the u-boot command line with printenv and setenv. It can be helpful to read and set these variables from linux as well. The u-boot distribution has sources for these commands, named fw_printenv and fw_setenv, but the Angstrom OE distribution does not build the tools automatically. I’m a novice at OE and bitbake, so here are the instructions for building and installing the tools manually. Theses instructions assume that you have already buillt the angstrom distribution for the beagle board. Substitute directories and compiler locations to match your environment.

  1. cd to the source for u-boot in the OE tree. For me, it is at “…/oe/tmp/work/beagleboard-angstrom-linux-gnueabi/u-boot-git-r18.1/git”

  2. manually build fw_printenv with make: “make env ARCH=arm CROSS_COMPILE= /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnuabi-”

  3. if the compile succeeded, you should have fw_printenv in the u-boot directory tools/env. Copy fw_printenv to /sbin on the root filesystem of your beagle board.

  4. the fw_printenv binary also contains the code for fw_setenv. the functions of fw_setenv are run when the program is invoked with the name “fw_setenv”. this is most easily accomplished by creating a symbolic link from fw_printenv to fw_setenv, as in “ln -sf /sbin/fw_printenv /sbin/fw_setenv”

  5. the last and most important step is creating the config file for the utilities. the file name is fw_env.config and it lives in /etc.
    to create this file, you need to know information about where u-boot stores its env variables. Specifically, you need the mtd device name, the device offset, the env size and the flash sector size. I found most of what I needed by looking in the file /proc/mtd. here is my fw_env.config file:

Configuration file for fw_(printenv/saveenv) utility.

Up to two entries are valid, in this case the redundand

environment sector is assumed present.

MTD device name Device offset Env. size Flash sector size

/dev/mtd2 0x0000 0x20000 0x20000

  1. usage
    a) printing all the environment variables


bootcmd=nand read 80200000 280000 400000;bootm 80200000
bootargs=console=ttyS2,115200n8 console=tty0 root=/dev/mtdblock4 rw rootfstyp
ffs2 nohz=off

b) printing a single environment variable

fw_printenv stdin


c) setting and verifying an environment variable - WARNING! If the fw_env.config does not point to u-boots env section in flash, running fw_setenv could corrupt your flash. Make sure that fw_printenv works properly and does not return a message about “bad CRC” before running fw_setenv.

fw_setenv mytestvariable abcdefg

fw_printenv mytestvariable


I use these utilities most often to change the boot characteristic of my board (ie boot from nand, boot from mmc, boot from nfs). Again, care must be taken when changing env variables as a typo could easily render your boot unbootable. most problems can be fixed by connecting to the serial console and using u-boots command line to fix bad variable.

frank agius




Thanks for posting so quickly. It looks good.