转载:http://falloutmx.blog.163.com/blog/static/39236020201211145010154/

可以通过mtd方式读取,也可以用ioremap方式。不过这些都比较麻烦,简单的方法有两种,一种是mtd_debug,另一种是fw_printenv。前者是一个软件包,需要单独下载安装。后一个就是我目前使用的方式,是Uboot带的一个工具,使用方法如下:

1、编译fw_printenv工具
在你uboot目录下用以下编译指令:
make env
成功后在tools/env下会生成fw_printenv,如果提示缺少mtd-user.h文件,从系统中拷贝过来即可:
cp /usr/include/mtd/ ./include/mtd -a

2、修改配置文件
根据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://labs.isee.biz/index.php/How_to_modify_the_uboot_environment_from_userspace
 

How to modify the uboot environment from userspace

Contents

[hide]

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:

  1. MTD device name
  2. Device Offset
  3. Environment Size
  4. Flash sector size
  5. 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)

  1. MTD device name ---> /dev/mtd2 ("u-boot environment")
  2. Device Offset ----> 80.000 + 18.000
  3. Environment Size ---> 80.000
  4. Flash sector size ---> 40.000
  5. Number of sectors (ignored in NOR)----> nothing

Pretty easy right? wrong!!! The right values are:

  1. MTD device name ----> /dev/mtd2
  2. Device Offset -----> 0x0000
  3. Environment Size -----> 0x01000
  4. Flash sector size ------> 0x020000
  5. 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!!


转载:http://www.denx.de/wiki/DULG/HowCanIAccessUBootEnvironmentVariablesInLinux

Question:
I would like to access U-Boot's environment variables from my Linux application. Is this possible?
Answer:
Yes, you can. The environment variables must be stored in flash memory, and your Linux kernel must support flash access through the MTD layer. In the U-Boot source tree you can find the environment tools in the directory tools/env, which can be built with command:
make env

For building against older versions of the MTD headers (meaning before v2.6.8-rc1) it is required to pass the argument "MTD_VERSION=old" to make:

make MTD_VERSION=old env

The resulting binary is called fw_printenv, but actually includes support for setting environment variables too. To achieve this, the binary behaves according to the name it is invoked as, so you will have to create a link called fw_setenv to fw_printenv.

These tools work exactly like the U-Boot commands printenv resp. setenv You can either build these tools with a fixed configuration selected at compile time, or you can configure the tools using the /etc/fw_env.config configuration file in your target root filesystem. Here is an example configuration file:

# Configuration file for fw_(printenv/setenv) utility.
# Up to two entries are valid, in this case the redundant
# environment sector is assumed present. #########################################################################
# For TQM8xxL modules:
#########################################################################
# MTD device name Device offset Env. size Flash sector size
/dev/mtd0 0x8000 0x4000 0x4000
/dev/mtd0 0xC000 0x4000 0x4000 #########################################################################
# For NSCU:
#########################################################################
# MTD device name Device offset Env. size Flash sector size
#/dev/mtd1 0x0000 0x8000 0x20000
#/dev/mtd2 0x0000 0x8000 0x20000 #########################################################################
# For LWMON
#########################################################################
# MTD device name Device offset Env. size Flash sector size
#/dev/mtd1 0x0000 0x2000 0x40000

转载:http://blog.csdn.net/hangbing0203/article/details/4314576

u-boot在线更新
需编译mtd-utils,编译fw_printenv、fw_setenv, 和修改内核 1.编译mtd-utils
将编译的flash_eraseall、flashcp拷贝到/nfsroot/cq8401/sbin中 然后用命令
flash_eraseall /dev/mtd1 刷bootloader分区
用命令
flashcp /bootloader/u-boot.bin.336new /dev/mtd1 将新的u-boot写入u-boot分区 2.编译fw_printenv和fw_setenv
进入u-boot-1.1.3/
vi tools/env/Makefile
加入
CROSS_COMPILE = mipsel-linux-
make -C tools/env all
cp tools/env/fw_printenv /nfsroot/cq8401/sbin
cd /nfsroot/cq8401/sbin
ln -sf fw_printenv fw_setenv
cp -av u-boot-1.1.3/tools/env/fw_env.config /nfsroot/cq8401/etc 问题汇总:
问题1:
fw_env.c:34:27: 错误:linux/mtd/mtd.h:没有那个文件或目录
fw_env.c: 在函数‘fw_printenv’中:
fw_env.c:244: 警告:初始化指针时目标与指针符号不一致
fw_env.c:259: 警告:传递‘fputs’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:262: 警告:传递‘puts’的参数 1 给指针时,目标与指针符号不一致
fw_env.c: 在函数‘fw_setenv’中:
fw_env.c:293: 警告:对指针赋值时目标与指针符号不一致
fw_env.c:317: 警告:传递‘strcmp’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:318: 警告:传递‘strcmp’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:350: 警告:传递‘strlen’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:364: 警告:初始化指针时目标与指针符号不一致
fw_env.c: 在函数‘flash_io’中:
fw_env.c:390: 错误:‘erase_info_t’未声明(在此函数内第一次使用)
fw_env.c:390: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
fw_env.c:390: 错误:所在的函数内也只报告一次。)
fw_env.c:390: 错误:expected ‘;’ before ‘erase’
fw_env.c:393: 警告:传递‘open’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:409: 警告:传递‘open’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:421: 错误:‘erase’未声明(在此函数内第一次使用)
fw_env.c:423: 错误:‘MEMUNLOCK’未声明(在此函数内第一次使用)
fw_env.c:462: 错误:‘MEMERASE’未声明(在此函数内第一次使用)
fw_env.c:519: 错误:‘MEMLOCK’未声明(在此函数内第一次使用)
fw_env.c: 在函数‘parse_config’中:
fw_env.c:719: 警告:传递‘stat’的参数 1 给指针时,目标与指针符号不一致
fw_env.c:726: 警告:传递‘stat’的参数 1 给指针时,目标与指针符号不一致
make: *** [fw_printenv] 错误 1 解决办法:
在tools/env/Makefile
加入CROSS_COMPILE = mipsel-linux- 问题2:
Cannot parse config file: No such file or directory
解决办法:
在/etc下创建fw_env.config文件 问题3:
Cannot parse config file: Invalid argument
解决办法:
修改文件权限
问题4:
Bus error
解决办法:
echo "/dev/mtd2 0x0000 0x20000 0x20000" > /etc/fw_env.config 问题5:
Warning: Bad CRC, using default environment
问题6:
Error: can't write fw_env to flash
5和6解决办法:
将Env. size 和 Flash sector size改为arch/mips/cq8401/com/setup.c中
所设定的值。 fw_env.config文件的内容
# 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 3.内核的修改
设置分区:
将arch/mips/cq8401/com/setup.c修改为以下,可以直接刷bootloader分区而不会将u-boot的配置空间擦除掉
static struct mtd_partition cq8401_mtd_parts[] = {
{
.name = "ROOTFS",
.size = 0xC00000,
.offset = 0,
.mask_flags = 0,
},
{
.name = "BOOTLOADER",
.size = 0x40000,
.offset = MTDPART_OFS_APPEND,
//.mask_flags = MTD_WRITEABLE, /* force read-only */
.mask_flags = 0,
},
{
.name = "uboot_env",
.size = 0x20000,
.offset = MTDPART_OFS_APPEND,
.mask_flags = 0,
}, {
.name = "KERNEL",
.size = MTDPART_SIZ_FULL,
.offset = MTDPART_OFS_APPEND,
.mask_flags = 0,
} 进入系统可用
cat /proc/mtd
查看分区设置
根据uboot的配置空间的设备节点设置/etc/fw_env.config中的第一项MTD device name,根据其偏移地址设值Device offset,根据空间大小设置Env. size ,根据闪存扇区大小设置Flash sector size 如果需要保存和恢复uboot的配置空间,用以下命令:
dd if=/dev/mtd2 of=env.bin 将配置空间的内容保存为文件env.bin
dd if=env.bin of=/dev/mtd2 将配置文件env.bin写回配置空间
如果需要读出配置空间的内容,使用以下命令:
fw_printenv
如果需要单独修改uboot的参数,使用以下命令:
fw_setenv bootdelay 5 u-boot更新脚本
#! /bin/sh
echo "##########################################"
echo "Save uboot_env space"
dd if=/dev/mtd2 of=env.bin
echo "Erasure bootloader"
flash_eraseall /dev/mtd1
echo "Erasure uboot_env space"
flash_eraseall /dev/mtd2
echo "Ersure kernel "
flash_eraseall /dev/mtd3
echo "##########################################"
echo "Write new bootloader"
flashcp /bootloader/u-boot.bin.336new /dev/mtd1
echo "Update u-boot completed"
echo "Write new kernel"
flashcp /bootloader/uImage /dev/mtd3
echo "Update kernel completed"
echo "Write uboot coonfig"
dd if=env.bin of=/dev/mtd2
echo "UPDATE completed"
echo "##########################################"

在Linux里读取UBOOT环境变量的更多相关文章

  1. MPC8313ERDB在Linux从NAND FLASH读取UBoot环境变量的代码分析

    MPC8313ERDB在Linux从NAND FLASH读取UBoot环境变量的代码分析 Yao.GUET@2014-05-19 一.故事起因 由于文件系统的增大,已经大大的超出了8MB的NOR FL ...

  2. Linux系统——访问U-BOOT环境变量

    Linux系统下访问U-BOOT环境变量 移植过U-BOOT的人,都知道:在U-BOOT中存有ENV.但U-BOOT在引导内核启动之后,U-BOOT的生命周期就结束了.那么启动LINUX内核之后,U- ...

  3. (转)在Linux里设置用户环境变量的方法

    背景:在linux下,root用户安装完jdk后,非root用户不能使用java环境变量. 问题排查: 通过在root和非root用户下分别执行echo $PATH 可以观察环境变量的异同.然后将需要 ...

  4. I.MX6 Linux U-boot 环境变量解析

    /********************************************************************************** * I.MX6 Linux U- ...

  5. 嵌入式 Linux下永久生效环境变量bashrc

    嵌入式 Linux下永久生效环境变量bashrc 1) .bashrc文件 在linux系统普通用户目录(cd /home/xxx)或root用户目录(cd /root)下,用指令ls -al可以看到 ...

  6. u-boot 环境变量参数设置

    今天本来是烧写内核,结果一不小心把uboot也整不能用了,无奈之下只好重新烧个uboot,等都弄好以后,发现系统还是启动不了,原来是启动参数设置不对,于是找到了这篇文章,//是我添加的内容. 原文地址 ...

  7. uboot环境变量

    一. uboot运行时环境变量分布 1.1. 环境变量有2份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中 ...

  8. uboot环境变量实现分析

    u-boot的环境变量用来存储一些经常使用的参数变量,uboot希望将环境变量存储在静态存储器中(如nand nor eeprom mmc). 其中有一些也是大家经常使用,有一些是使用人员自己定义的, ...

  9. Linux下各种常见环境变量的配置

      Linux系统下各种环境变量都通过修改/etc/profile文件来实现.由于是系统文件,修改此文件需要root权限.因此实现以下功能都需要用户拥有root权限. 另:不要轻易修改profile文 ...

随机推荐

  1. <转>详解DNS的常用记录(上):DNS系列之二

    详解DNS的常用记录(上) 在上篇博文中,我们介绍了DNS服务器的体系结构,从中我们了解到如果我们希望注册一个域名,那么必须经过顶级域名服务器或其下级的域名服务器为我们申请的域名进行委派,把解析权委派 ...

  2. (一)使用Blender导出GameMaker支持的模型脚本

    源于YOYO论坛帖子:http://gmc.yoyogames.com/index.php?showtopic=603723 既然想做3D,那就先从模型的导入开始,具体的源文件,可以在“(二)使用等高 ...

  3. MVC & MVVM

    什么是MVC,什么是MVVM? 面向过程 --> 面向对象 --> MVC --> MV* 面向过程: 开发人员按照需求逻辑顺序开发代码逻辑,主要思维模式在于如何实现.先细节,后整体 ...

  4. 多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率

    // 多校5 1001 HDU5781 ATM Mechine // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi ...

  5. MFC使用ShowWindow(SW_MAXIMIZE)任务栏消失的处理

    ShowWindow(SW_SHOWMAXIMIZED);//窗口最大化 问题:在写程序时,如果包含了标题栏,但是没有包含最大化按钮或者最小话按钮. 那么人工用ShowWindow(SW_MAXIMI ...

  6. Tkinter教程之Button篇(2)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811300 # Tkinter教程之Button篇(2)'''5.指定Button的宽度与高度 ...

  7. tensorflow-cnn

    需要安装 python,numpy,tensorflow,运行代码即可. tensorflow很好装,用pip安装即可. 可以参照http://wiki.jikexueyuan.com/project ...

  8. 产品经理如何赢得开发人员的尊重和支持?-摘自infoq

    对于产品经理来说,赢得开发人员的尊重和支持,从某种意义上讲,是产品迈向成功的坚实一步.最近,知乎社区上的开发人员和管理者在前.后两个帖子中对此展开了激烈的讨论,其中不乏真知灼见. 林志霖Cray认为产 ...

  9. phonegap WebApp

    打开网页浏览器,进入Android SDK网站(http://developer.android.com/sdk/index.html). 我们可以看到,Google官方提供了包括Windows平台在 ...

  10. 【现代程序设计】【homework-07】

    C++11 中值得关注的几大变化 1.Lambda 表达式 Lambda表达式来源于函数式编程,说白就了就是在使用的地方定义函数,有的语言叫“闭包”,如果 lambda 函数没有传回值(例如 void ...