How to modify the uboot environment from userspace
From IGEP - ISEE Wiki
Contents
Overview
This How-To is meant to be a starting point for people to learn how access to the uboot environment from userspace.
This is my first wiki page so please be benevolent with the formatting
Tools needed
In order to modify the uboot environment variables from userspace we will need a program called fw_setenv, we can compile this program ourselves from the u-boot sources, follow the instructions in this wiki page and then proceed to compile with (don't modify the .h file as it says in the tools/env/README file):
make env
This will give us fw_printenv, in order to get fw_setenv we just have to change the name of the file. Magic!!
cp fw_printenv fw_setenv
The next thing we will need is the fw_env.config file with the appropiate settings for our board. Here comes the confusion
In this file you have to specify:
- MTD device name
- Device Offset
- Environment Size
- Flash sector size
- Number of sectors (ignored in NOR)
Finding out the correct values for fw_env.config
Ok, so in order to get all this information the easyest thing is to read it from dmesg or from /proc/mtd
cat /proc/mtd
All my initial confusion comes from using an incorrect kernel, my first tries where with 2.6.28.10 and the driver used to read mtd was NAND, in the kernel configuration file I only had :
CONFIG_MTD_NAND=y
being the output of /proc/mtd:
dev: size erasesize name mtd0: 00080000 00040000 "X-Loader" mtd1: 001e0000 00040000 "U-Boot" mtd2: 00020000 00040000 "U-Boot Env" mtd3: 00500000 00040000 "Kernel" mtd4: 1f880000 00040000 "File System"
through "dmesg" I could see this line indicating something was wrong:
[ 1174.517120] Creating 5 MTD partitions on "omap2-onenand": [ 1174.522552] 0x00000000-0x00080000 : "X-Loader" [ 1174.528381] 0x00080000-0x00260000 : "U-Boot" [ 1174.533508] 0x00260000-0x00280000 : "U-Boot Env" [ 1174.538177] mtd: partition "U-Boot Env" doesn't start on an erase block boundary -- force read-only [ 1174.548126] 0x00280000-0x00780000 : "Kernel" [ 1174.553192] 0x00780000-0x20000000 : "File System"
But if we use the aproppiate kernel, in my case 2.6.33.7-0 with the onenand driver instead of the nand driver:
CONFIG_MTD_ONENAND=y
The output of /proc/mtd is different:
dev: size erasesize name mtd0: 00080000 00040000 "X-Loader" mtd1: 00180000 00040000 "U-Boot" mtd2: 00080000 00040000 "Environment" mtd3: 00300000 00040000 "Kernel" mtd4: 1fa80000 00040000 "File System"
Now we can fill in all the values we needed (that's what I thought)
- MTD device name ---> /dev/mtd2 ("u-boot environment")
- Device Offset ----> 80.000 + 18.000
- Environment Size ---> 80.000
- Flash sector size ---> 40.000
- Number of sectors (ignored in NOR)----> nothing
Pretty easy right? wrong!!! The right values are:
- MTD device name ----> /dev/mtd2
- Device Offset -----> 0x0000
- Environment Size -----> 0x01000
- Flash sector size ------> 0x020000
- Number of sectors (ignored in NOR) -----> nothing
I figured these out by reading different posts and by trial and error, but I still don't understand the environment size value or the flash sector size, I can imagine the device offset is 0 if you specify /dev/mtd2 but I'd be great if someone could edit this page and point out why are this the correct values.
Giving it a try
In order to give it a try is better to poke with fw_printenv than with fw_setenv, because the first one just tries to read, so go ahead and try it:
fw_printenv
this should output something like:
ethact=smc911x-0 distro=poky project=poky-image-sato ipaddr=192.168.254.254 netmask=255.255.255.0 gatewayip=192.168.254.10 serverip=192.168.254.10 addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}::eth0: mmc-boot=mmc init 0; if fatload mmc 0 80000000 boot.ini; then source; else if fatload mmc 0 80000000 uImage; then run mmc-bootargs; bootm; fi; fi onenand-bootargs=setenv bootargs ${bootargs-base} root=/dev/mtdblock4 rootfstype=jffs2 ; run addip onenand-boot=run onenand-bootargs; onenand read 80000000 280000 400000 ; bootm 80000000 nfs-bootargs=setenv bootargs ${bootargs-base} root=/dev/nfs nfsroot=${serverip}:/srv/nfs/${distro}/${project}/${machine} ; run addip nfs-boot=if ping ${serverip}; then run nfs-bootargs; tftp 0x80000000 ${distro}/${project}/${machine}/uImage; bootm; fi; machine=igep0020 mmc-bootargs=setenv bootargs ${bootargs-base} root=/dev/mmcblk0p2 rw rootwait dieid=${dieid#} dieid#=638a00040000000004036abc07016018 mpurate=110 stdin=serial stdout=serial stderr=serial bootargs-base=mem=512M console=ttyS2,115200n8 console=tty0 omapfb.mode=dvi:1024x768MR-16@60 mpurate=110 bootcmd=run onenand-boot; run mmc-boot;
if you get something like:
Warning: Bad CRC, using default environment bootcmd=bootp; setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; bootm bootdelay=5 baudrate=115200
it means the values in /etc/fw_env.config are not correct, so don't even try fw_setenv
Using fw_setenv
Now that we found out the correct values for /etc/fw_env.config you can just use fw_setenv with the same syntax as if you were in u-boot command line, to assing a value don't use "=" but a space.
./fw_setenv test 1234
then fw_printenv should output
test=1234
notice we didn't need to use saveenv as we would do in u-boot command line.
That's it!!
Feedback and Contributing
Creating articles in the wiki is a collaborative process, at any point, if you see a mistake you can contribute to this article.
Please, use the discussion tab for user comments. This is useful to separate page content and the discussion thereof and also, if you don't want to give normal users the right to edit the page but still want user contributed notes.
Editing permissions are restricted to registered users. Register in the main IGEP site and you will have single sign-on.
Consult the User's Guide for information on using the wiki software.
There is a set of Wiki contribution guidelines.