Tuesday 23 April 2013

How to access and change the u-Boot env variables in linux


Have you ever thought to change the U-boot settings once, after you got the Linux console? Well, you might be thinking to entering into the U-boot setting prompt to do this. But there is a beautiful method already exist to achieve this operation!

Well, here I'm not giving anything from the sky or vanishing anything like a magician, but trying to show a Utility tool (you can't say exactly it's a tool, better it can be consider as one option ;) ) which already available :0 .

All you have to do is, to try some geeky way (though very simple) only at once and do the magic always.
The key tool which you have to enable is "fw_printenv" from the U-boot.

Follow the below steps to make the wonders in U-boot from your own lovely Linux console ;)
  1. Step 1: Generate the "fw_printenv" utility tool in U-boot

    1. give 'env' (optional parameter) in u-boot and compile it. Be patient for the compilation ;) 
    2. The above step generate the 'fw_printenv' in the U-Boot directory tools/env 
    3. Copy the 'fw_env.config' file too.
  2. Step 2: Well, we copied, but where to paste it ! Follow the steps ;)

    1. Copy fw_printenv to /sbin on the root file system of your own board. 
    2. The interesting thing here is that 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"                                                                                       
  3. Step 3: Create config file for the utility, in order to take place the action(I have not used config file in my example given here).

    1. Remember, you copied 'fw_env.config' from 'u-boot/tools/env/' path? But you didn't play with  it yet right? ;) Well, this is the right time for that! All you have to do is, copy the fw_env.config  into /etc path. Finished? Relaxed? No...remember, this is a geeky professional way dude ;) 
    2. Open the 'fw_env.config' and make required changes related to env size, flash sector size and number of sectors according to flash which you are using. You have to change the fw_env.config with respect to your system modification rather than simply putting it into /etc. you can easily check this from /proc/mtd.
Once you configure the appropriate values in fw_env.config, then rest of the stuffs are pretty easy.

From the Linux console, you can print the u-boot setting by giving the fw_printenv where as with fw_setenv , you can edit the u-boot settings!!! Easy right? 

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

Example wrt  processor architecture.
changes did with respect to my LPC1788 board(changes highleted with yellow collour).
  1. u-boot/tools/env/Makefile changed:
#
# (C) Copyright 2002-2006
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
include $(TOPDIR)/config.mk
SRCS := $(obj)crc32.c fw_env.c fw_env_main.c
HEADERS := fw_env.h
CPPFLAGS := -Wall -DUSE_HOSTCC -I$(SRCTREE)/include
CFLAGS := -Os -mcpu=cortex-m3 -mthumb -I$(INSTALL_ROOT)/linux/inlude/linux
LDFLAGS := -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/linux/lib
ifeq ($(MTD_VERSION),old)
CPPFLAGS += -DMTD_OLD
endif
all: $(obj)fw_printenv
$(obj)fw_printenv: $(SRCS) $(HEADERS)
$(CROSS_COMPILE)gcc $(CPPFLAGS) $(LDFLAGS) $(SRCS) -o $(obj)fw_printenv
clean:
rm -f $(obj)fw_printenv $(obj)crc32.c
$(obj)crc32.c:
ln -s $(src)../../lib_generic/crc32.c $(obj)crc32.c
#########################################################################

include $(TOPDIR)/rules.mk

sinclude $(obj).depend

#########################################################################


Note: copied from application folder /app/Makefile(This two lines says that we need to compile wrt  processor architecture.)

CFLAGS := -Os -mcpu=cortex-m3 -mthumb -I$(INSTALL_ROOT)/linux/inlude/linux
LDFLAGS := -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/linux/lib

2. fw_env.h
----------------------------------------------------------------------------------------------------------------------------------
/*
* (C) Copyright 2002-2008
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/*
* To build the utility with the run-time configuration
* uncomment the next line.
* See included "fw_env.config" sample file (TRAB board)
* for notes on configuration.
*/
/*#define CONFIG_FILE "/etc/fw_env.config"*/
/*#define HAVE_REDUND For systems with 2 env sectors */
#define DEVICE1_NAME "/dev/mtd0"
/*#define DEVICE2_NAME "/dev/mtd2"*/
#define DEVICE1_OFFSET 0x00000
#define ENV1_SIZE (4 * 1024) /* taken it with comparig to the file u-boot/include/configs/lpc-lnx-evb.h*/
#define DEVICE1_ESIZE 0x20000 /*taken from proc/mtd erasesector*/
#define DEVICE1_ENVSECTORS 1
/*#define DEVICE2_OFFSET 0x0000
#define ENV2_SIZE 0x4000
#define DEVICE2_ESIZE 0x4000*/
#define CONFIG_BAUDRATE 115200
#define CONFIG_BOOTDELAY 5 /* autoboot after 5 seconds */
#define CONFIG_BOOTCOMMAND \
"bootp; " \
"setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} " \
"ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; " \
"bootm"
extern int fw_printenv(int argc, char *argv[]);
extern char *fw_getenv (char *name);
extern int fw_setenv (int argc, char *argv[]);
extern unsigned long crc32 (unsigned long, const unsigned char *, unsigned);
----------------------------------------------------------------------------------------------------------------------------------

Well, I would like to share three good web links also for your better understanding..

No comments:

Post a Comment