在Linux里读取UBOOT环境变量
转载:http://falloutmx.blog.163.com/blog/static/39236020201211145010154/
可以通过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工具
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:
- 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!!
转载: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环境变量的更多相关文章
- MPC8313ERDB在Linux从NAND FLASH读取UBoot环境变量的代码分析
MPC8313ERDB在Linux从NAND FLASH读取UBoot环境变量的代码分析 Yao.GUET@2014-05-19 一.故事起因 由于文件系统的增大,已经大大的超出了8MB的NOR FL ...
- Linux系统——访问U-BOOT环境变量
Linux系统下访问U-BOOT环境变量 移植过U-BOOT的人,都知道:在U-BOOT中存有ENV.但U-BOOT在引导内核启动之后,U-BOOT的生命周期就结束了.那么启动LINUX内核之后,U- ...
- (转)在Linux里设置用户环境变量的方法
背景:在linux下,root用户安装完jdk后,非root用户不能使用java环境变量. 问题排查: 通过在root和非root用户下分别执行echo $PATH 可以观察环境变量的异同.然后将需要 ...
- I.MX6 Linux U-boot 环境变量解析
/********************************************************************************** * I.MX6 Linux U- ...
- 嵌入式 Linux下永久生效环境变量bashrc
嵌入式 Linux下永久生效环境变量bashrc 1) .bashrc文件 在linux系统普通用户目录(cd /home/xxx)或root用户目录(cd /root)下,用指令ls -al可以看到 ...
- u-boot 环境变量参数设置
今天本来是烧写内核,结果一不小心把uboot也整不能用了,无奈之下只好重新烧个uboot,等都弄好以后,发现系统还是启动不了,原来是启动参数设置不对,于是找到了这篇文章,//是我添加的内容. 原文地址 ...
- uboot环境变量
一. uboot运行时环境变量分布 1.1. 环境变量有2份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中 ...
- uboot环境变量实现分析
u-boot的环境变量用来存储一些经常使用的参数变量,uboot希望将环境变量存储在静态存储器中(如nand nor eeprom mmc). 其中有一些也是大家经常使用,有一些是使用人员自己定义的, ...
- Linux下各种常见环境变量的配置
Linux系统下各种环境变量都通过修改/etc/profile文件来实现.由于是系统文件,修改此文件需要root权限.因此实现以下功能都需要用户拥有root权限. 另:不要轻易修改profile文 ...
随机推荐
- [GRYZ2015]INCR
题目描述 数列 A1,A2,...,AN,修改最少的数字,使得数列严格单调递增. 输入格式 第 1 行,1 个整数 N 第 2 行,N 个整数 A1,A2,...,AN 输出格式 1 个整数,表示最少 ...
- hadoop2.7【单节点】单机、伪分布、分布式安装指导
问题导读 1.从本文部署实际部署,总结本地模式.伪分布.分布式的区别是什么?2.单机是否是伪分布?3.本地模式是否可以运行mapreduce? 来源:about云 http://www.aboutyu ...
- Merge Cells for DataGrid 合并单元格
只适合不分页的固定行列的表格 <script type="text/javascript"> function onLoadSuccess(data){ var mer ...
- Struts2零碎点整理
1. 关于 Struts2 请求的扩展名问题 1). org.apache.struts2 包下的 default.properties 中配置了 Struts2 应用的一些常量 2). struts ...
- ActiveX控件是什么?
一.ActiveX的由来 ActiveX最初只不过是一个商标名称而已,它所涵盖的技术并不是各自孤立的,其中多数都与Internet和Web有一定的关联.更重要的是,ActiveX的整体技术是由Micr ...
- freemaker自定义分页控件实现
<link href="${res}/css/pages-jhdb.css" rel="stylesheet" type="text/css&q ...
- The Trip PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- Oracle DataGuard 物理Standby 搭建(上)
物理standby database 环境搭建 Arch asysnc Oracle Dataguard host IP Oracle_sid DB_unique_name FAL_server FA ...
- 一个无聊的实验:验证网站是否通过web容器还是微服务部署
一般来说一台web服务器会部署多个实例(且共享80端口),举个栗子例如nginx通常部署多个站点,每个站点都有自己的端口 例如 8091,8092之类的. 通过nginx进行代理.(前提微服务直接使用 ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...