在Linux裡讀取UBOOT環境變數
可以通過mtd方式讀取,也可以用ioremap方式。不過這些都比較麻煩,簡單的方法有兩種,一種是mtd_debug,另一種是fw_printenv。前者是一個軟體包,需要單獨下載安裝。後一個就是我目前使用的方式,是Uboot帶的一個工具,使用方法如下:
在你uboot目錄下用以下編譯指令:2、修改配置檔案
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環境變數資訊,如果沒打印出來或者列印亂碼,請檢查配置檔案是否正確。
參考網址:
程式碼分析:
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:
根目錄下:
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