在Linux裏讀取UBOOT環境變量

轉載:http://falloutmx.blog.163.com/blog/static/39236020201211145010154/php

 

能夠經過mtd方式讀取,也能夠用ioremap方式。不過這些都比較麻煩,簡單的方法有兩種,一種是mtd_debug,另外一種是fw_printenv。前者是一個軟件包,須要單獨下載安裝。後一個就是我目前使用的方式,是Uboot帶的一個工具,使用方法以下:

linux

一、編譯fw_printenv工具
在你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文件。

三、使用fw_printenv工具app

將編譯好的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.ide

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!!post

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 :-)ui

In this file you have to specify:this

  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 spa

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 "##########################################"
相關文章
相關標籤/搜索