章節概述:
uboot下可以通過命令訪問和修改環境變量,但是如果需要在arm-Linux系統下訪問這些數據該怎么辦呢?其實uboot早就幫我們想好了。
步驟
uboot版本:一般在2011年以后的都有(見過2011年版本的uboot教程)
編譯fw_printenv
在你使用的uboot代碼中用以下編譯指令:
make env ARCH=xx CROSS_COMPLIE=xx-
這樣就可以編譯tools/env下的代碼,編譯出的fw_printenv工具有讀寫uboot環境變量區的能力。
安裝fw_printenv
在tools/env
目錄中,將編譯好的fw_printenv拷貝到目標機的文件系統中,並通過ln -s fw_printenv fw_setenv
,創建一個fw_setenv到fw_printenv的軟鏈。
配置
這個工具還需要一個配置文件,以獲取uboot的ENV區域的位置信息。
默認狀態下,請將fw_env.config文件拷貝到目標機的文件系統的/etc目錄下。
然后結合uboot配置中定義的ENV區和Linux下mtd分區的情況修改配置文件。
具體的修改方法見fw_env.config文件中的說明及/tools/env/README文件。
配置一定要和系統的配置相同。
跟據以上三個定義修改fw_env.config
,以emmc為例:
# Configuration file for fw_(printenv/setenv) utility.
# Up to two entries are valid, in this case the redundant
# environment sector is assumed present.
# Notice, that the "Number of sectors" is not required on NOR and SPI-dataflash.
# Futhermore, if the Flash sector size is ommitted, this value is assumed to
# be the same as the Environment size, which is valid for NOR and SPI-dataflash
# NOR example
# MTD device name Device offset Env. size Flash sector size Number of sectors
#/dev/mtd1 0x0000 0x4000 0x4000
#/dev/mtd2 0x0000 0x4000 0x4000
# MTD SPI-dataflash example
# MTD device name Device offset Env. size Flash sector size Number of sectors
#/dev/mtd5 0x4200 0x4200
#/dev/mtd6 0x4200 0x4200
# NAND example
#/dev/mtd0 0x4000 0x4000 0x20000 2
# Block device example
# device name env_offset Env.bin_size Env.bin_size Env_partition_sectors
/dev/mmcblk0 0x3000000 0x20000 0x20000 0x8000
參數解析:
/dev/mtd0
是專門給環境變量分配的分區。Device offset 是只環境變量在此分區上的偏移,不是指在整個nand上的偏移。
環境變量的燒寫地址是 0x80000,大小0x10000,block大小是0x20000。這里因為mtd0分區設定了起始地址是0x80000,所以環境變量在此分區上的偏移地址為 0了
使用 fw_printenv
其實fw_printenv使用起來和uboot下的printenv和setenv指令是一模一樣的。
獲取uboot環境變量
fw_printenv [[ -n name ] | [ name ... ]]
如果不指定name,fw_printenv會打印出ENV區中的所有環境變量
設置uboot環境變量
fw_setenv name [ value ... ]
如果不指定value,表示要刪除這個name的環境變量。