(一)、概述
Linux應用程序可以通過uboot/tools/env目錄下的fw_printenv程序,查看,修改,刪除Uboot的環境變量。
如:system("/usr/sbin/fw_setenv ipaddr 192.168.17.100");
或者在命令行 # /usr/sbin/fw_setenv ipaddr 192.168.17.100
(二)、編譯
1.確保Fw_env.h文件中存在以下定義:
#define CONFIG_FILE "/etc/fw_env.config"
上面的定義使得fw_setenv程序會從/etc/fw_env.config中獲取配置參數。具體參見
Fw_env.c中parse_config ()源碼。
static int parse_config ()
{
struct stat st;
#if defined(CONFIG_FILE)
/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
if (get_config (CONFIG_FILE)) {
fprintf (stderr,
"Cannot parse config file: %s\n", strerror (errno));
return -1;
}
#else
strcpy (DEVNAME (0), DEVICE1_NAME);
DEVOFFSET (0) = DEVICE1_OFFSET;
ENVSIZE (0) = ENV1_SIZE;
DEVESIZE (0) = DEVICE1_ESIZE;
ENVSECTORS (0) = DEVICE1_ENVSECTORS;
.....
#endif
.....
}
2.在uboot根目錄執行
Make env
3.將uboot/tools/env/fw_printenv 拷貝到目標機的/ust/sbin目錄下,並通過“ln -s fw_printenv fw_setenv”創建一個fw_setenv到fw_printenv的軟連接。
4.將uboot/tools/env/fw_env.config拷貝到目標機的/etc/目錄下,並修改爲合適值。
(三)、修改配置
1、fw_env.config默認配置如下:
[root@localhost env]# vi 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/mtd1 0x0000 0x4000 0x4000
/dev/mtd2 0x0000 0x4000 0x4000
# NAND example
#/dev/mtd0 0x4000 0x4000 0x20000 2
2、我使用的是NAND FLASH
先在目標機執行cat /proc/mtd,獲得配置參數MTD device name和 Flash sector size
# cat /proc/mtd
dev: size erasesize name
mtd0: 00040000 00020000 "u-boot"
mtd1: 00040000 00020000 "u-bootenv"
mtd2: 00280000 00020000 "kernel"
mtd3: 00200000 00020000 "data"
mtd4: 07b00000 00020000 "rootfs"
簡單解釋下配置文件中一個字符串的含義
MTD device name : "u-bootenv"對應“mtd1”
Device offset : 環境變量在mtd1中的偏移地址
Env. size : 環境變量大小
Flash sector size :NAND flash 塊大小 擦除大小 0x20000
3.一般NAND flash 會保存兩次環境變量(在兩個地方),也就是RedundEnv。
在uboot命令行執行saveenv 也會發現執行了兩次write nand 操作:
執行兩次的原因是定義了CONFIG_ENV_OFFSET_REDUND。
firetux # saveenv
Saving Environment to NAND...
Erasing redundant Nand...
Erasing at 0x60000 -- 100% complete.
Writing to redundant Nand... Erasing Nand...
Erasing at 0x40000 -- 100% complete.
Writing to Nand... done
firetux #
4.查看uboot/include/autoconfig.mk文件,獲取配置參數Device offset, Env. size 。
CONFIG_ENV_OFFSET="0x40000"
CONFIG_ENV_OFFSET_REDUND="0x60000"
CONFIG_ENV_SIZE="0x20000"
可見,環境變量存放的位置有兩個。
第一個:0x40000開始,大小是0x20000 .
第二個:0x60000開始,大小是0x20000 .
0x40000,0x0x60000都是相對有mtd0(0x00000)而言,相對於mtd1而言就是0x0000和0x20000
所以配置文件應該修改爲
# 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/mtd1 0x0000 0x20000 0x20000 1
/dev/mtd1 0x20000 0x20000 0x20000 1
(四)、備註
Fw_setenv 不能修改或者刪除"ethaddr", "serial#"這兩個環境變量。
當然可以通過修改fw_env.c中的fw_env_write函數,來越過此限制。
屏蔽下面代碼
if ((strcmp (name, "ethaddr") == 0) ||
(strcmp (name, "serial#") == 0)) {
fprintf (stderr, "Can't overwrite \"%s\"\n", name);
errno = EROFS;
return -1;
}