U-boot environment variables in linux

From eLinux.org
Revision as of 07:38, 31 August 2012 by Peterll (talk | contribs) (Use correct application name in comment, fix typo in comment)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

U-Boot makes use of environment variables 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.



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 built 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"


Buildroot can automatically build these tools for you. You just have to:

make menuconfig

and then select the following option:

Target filesystem options  --->
    [*] Das U-Boot Boot Monitor  --->
        [*]   fw_printenv tool in target

Tools will be installed in /usr/sbin/ of your target's rootfs.


Like Buildroot, OpenWrt can build these tools too but you will find them in a different location in the menu:

Utilities  --->
    [*] uboot-envtools

Tools will also be installed in /usr/sbin/.

All distribs

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/setenv) utility.
  # Up to two entries are valid, in this case the redundant
  # environment sector is assumed present.
  # MTD device name       Device offset   Env. size       Flash sector size
         /dev/mtd2               0x0000          0x20000         0x20000


Printing all the environment variables

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

Printing a single environment variable

  # fw_printenv stdin

Setting and verifying an environment variable

WARNING! If the fw_env.config does not point to U-Boot's 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

Care must be taken when changing env variables as a typo could easily render your system unbootable. Most problems can be fixed by connecting to the serial console and using U-Boot's command line to fix bad variable.