来自:http://falloutmx.blog.163.com/blog/static/39236020201211145010154/
http://www.cnblogs.com/cute/archive/2011/07/28/2119671.html
可以通过mtd方式读取,也可以用ioremap方式。不过这些都比较麻烦,简单的方法有两种,一种是mtd_debug,另一种是fw_printenv。前者是一个软件包,需要单独下载安装。后一个就是我目前使用的方式,是Uboot带的一个工具,使用方法如下:
在你uboot目录下用以下编译指令:
make env成功后在tools/env下会生成fw_printenv,如果提示缺少mtd-user.h文件,从系统中拷贝过来即可:
cp /usr/include/mtd/ ./include/mtd -a
根据mtd分区、UBOOT环境变量的位置、大小等内容修改tools/env下的fw_env.config文件,可参见/tools/env/README文件。这 个工具还需要一个配置文件,以获取uboot的ENV区域的位置信息。默认状态下,请将fw_env.config文件拷贝到目标机的文件系统的/etc 目录下。然后结合uboot配置中定义的ENV区和Linux下mtd分区的情况修改配置文件。具体的修改方法见fw_env.config文件中的说明 及/tools/env/README文件。
3、使用fw_printenv工具
将编译好的fw_printenv拷贝到目标机的文件系统中,并将fw_env.config文件拷贝到目标机的文件系统的/etc 目录下。执行fw_printenv即可打印Uboot环境变量信息,如果没打印出来或者打印乱码,请检查配置文件是否正确。
参考网址:
http://www.denx.de/wiki/DULG/HowCanIAccessUBootEnvironmentVariablesInLinux
http://blog.csdn.net/hangbing0203/article/details/4314576
http://labs.igep.es/index.php/How_to_modify_the_uboot_environment_from_userspace
代码分析:
README:
This is a demo implementation of a Linux command line tool to access
the U-Boot's environment variables.
For the run-time utiltity configuration uncomment the line
#define CONFIG_FILE "/etc/fw_env.config"
in fw_env.h.
这个有误,应该是用配置文件时,不要注释掉这个宏
See comments in the fw_env.config file for definitions for the
particular board.
Configuration can also be done via #defines in the fw_env.h file. The
following lines are relevant:
#define HAVE_REDUND /* For systems with 2 env sectors */
#define DEVICE1_NAME "/dev/mtd1"
#define DEVICE2_NAME "/dev/mtd2"
#define DEVICE1_OFFSET 0x0000
#define ENV1_SIZE 0x4000
#define DEVICE1_ESIZE 0x4000
#define DEVICE2_OFFSET 0x0000
#define ENV2_SIZE 0x4000
#define DEVICE2_ESIZE 0x4000
Current configuration matches the environment layout of the TRAB
board.
HAVE_REDUND是用来环境变量备份用的
Un-define HAVE_REDUND, if you want to use the utlities on a system
that does not have support for redundant environment enabled.
If HAVE_REDUND is undefined, DEVICE2_NAME is ignored,
as is ENV2_SIZE and DEVICE2_ESIZE.
The DEVICEx_NAME constants define which MTD character devices are to
be used to access the environment.
The DEVICEx_OFFSET constants define the environment offset within the
MTD character device.
ENVx_SIZE defines the size in bytes taken by the environment, which
may be less then flash sector size, if the environment takes less
then 1 sector.
DEVICEx_ESIZE defines the size of the first sector in the flash
partition where the environment resides.
DEVICEx_NAME设备名
DEVICEx_OFFSET设备中环境变量的偏移
ENVx_SIZE 环境变量大小,可能比1sector小
查看环境变量大小:
pri查看
Environment size: 1560/4092 bytes
include/configs/mv_kv.h
#define CFG_ENV_SIZE 0x1000
所以大小是0x1000
查看环境变量偏移:
#define CFG_MONITOR_LEN (512 << 10) /* Reserve 512 kB for Monitor */ uboot镜像长度 0x80000 =512k
//uboot的基址和环境变量的基址
#define CFG_MONITOR_BASE (CFG_FLASH_BASE)
#define CFG_ENV_ADDR (CFG_MONITOR_BASE + CFG_MONITOR_LEN) 环境变量开始位置
#define CFG_ENV_SIZE 0x1000 //4k
环境变量从0x80000开始 到0x80000+0x1000
根据CFG_ENV_ADDR 知道环境变量偏移是0x80000
查看erasesize
/opt/qtmarvell/mvqt/tools # cat /proc/mtd
dev: size erasesize name
mtd0: 00100000 00010000 "uboot"
mtd1: 00700000 00010000 "linux-root"
编译fw_printenv:
下载http://ftp.denx.de/pub/u-boot/u-boot-2011.03.tar.bz2
根目录下:
make env HOSTCC=arm-mv5sft-linux-gnueabi-gcc
ln -s fw_printenv fw_setenv
修改fw_env.config ,根据上面的信息:
/opt/qtmarvell/mvqt/tools # cat fw_env.config
# Configuration file for fw_(printenv/saveenv) utility.
# Up to two entries are valid, in this case the redundant
# environment sector is assumed present.
# Notice, that the "Number of sectors" is ignored on NOR.
# MTD device name Device offset Env. size Flash sector size Number of sectors
/dev/mtd0 0x80000 0x1000 0x10000