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.

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:
 * 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.

# 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

Printing all the environment variables
# fw_printenv bootdelay=1 baudrate=115200 ipaddr=192.168.0.2 serverip=192.168.0.1 netmask=255.255.255.0 bootfile="uImage" filesize=B81A24 bootcmd=nand read 80200000 280000 400000;bootm 80200000 bootargs=console=ttyS2,115200n8 console=tty0 root=/dev/mtdblock4 rw rootfstyp=jffs2 nohz=off stdin=serial stdout=serial stderr=serial

Printing a single environment variable
# fw_printenv stdin stdin=serial

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 mytestenv=abcdefg

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.