(一)、概述
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;
}