u-boot环境 变量可以理解成全局变量,在不改变源码、不用重新编译的情况下,可以使我们通过设置这些环境变量的值来改变u-boot的一些设置,入bootdelay、baudrate等等。在u-boot2008中,通用的环境变量在env_common.c文件中,包含如下:
uchar default_environment[] = {
#ifdef CONFIG_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0" //boot引导参数
#endif
#ifdef CONFIG_BOOTCOMMAND
"bootcmd=" CONFIG_BOOTCOMMAND "\0" //很重要,引导kernel的命令
#endif
#ifdef CONFIG_RAMBOOTCOMMAND
"ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
#endif
#ifdef CONFIG_NFSBOOTCOMMAND
"nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
#endif
#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
"bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
#endif
#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
"baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
#endif
#ifdef CONFIG_LOADS_ECHO
"loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
#endif
#ifdef CONFIG_ETHADDR
"ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
#endif
#ifdef CONFIG_ETH1ADDR
"eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
#endif
#ifdef CONFIG_ETH2ADDR
"eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
#endif
#ifdef CONFIG_ETH3ADDR
"eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
#endif
#ifdef CONFIG_ETH4ADDR
"eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
#endif
#ifdef CONFIG_ETH5ADDR
"eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
#endif
#ifdef CONFIG_IPADDR
"ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
#endif
#ifdef CONFIG_SERVERIP
"serverip=" MK_STR(CONFIG_SERVERIP) "\0"
#endif
#ifdef CONFIG_SYS_AUTOLOAD
"autoload=" CONFIG_SYS_AUTOLOAD "\0"
#endif
#ifdef CONFIG_PREBOOT
"preboot=" CONFIG_PREBOOT "\0"
#endif
#ifdef CONFIG_ROOTPATH
"rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
#endif
#ifdef CONFIG_GATEWAYIP
"gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
#endif
#ifdef CONFIG_NETMASK
"netmask=" MK_STR(CONFIG_NETMASK) "\0"
#endif
#ifdef CONFIG_HOSTNAME
"hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
#endif
#ifdef CONFIG_BOOTFILE
"bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
#endif
#ifdef CONFIG_LOADADDR
"loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
#endif
#ifdef CONFIG_CLOCKS_IN_MHZ
"clocks_in_mhz=1\0"
#endif
#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
"pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
#endif
#ifdef CONFIG_EXTRA_ENV_SETTINGS
CONFIG_EXTRA_ENV_SETTINGS
#endif
"\0"
};
在u-boot的start_armboot函数中,功能初始化序列数组init_sequence中,包含如下:
init_fnc_t *init_sequence[] = {
#if defined(CONFIG_BOARD_EARLY_INIT_F)
board_early_init_f, /* Call board-specific init code early.*/
#endif
env_init,
serial_init,
console_init_f,
display_options,
checkcpu,
checkboard,
NULL, /* Terminate this list */
};
其中env_init就是环境变量初始化函数,进一步分析如下:
int env_init(void)
{
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 0;
return (0);
}
该函数中实际执行的是把default_environment的地址赋值给全局变量gd中的env_addr和env_valid两个值。
上面的环境变量,是u-boot默认的, 有时候不太够, 或者说需要一些定制环境变量。default_environment 中的最后一项中的
CONFIG_EXTRA_ENV_SETTINGS 就是为用户自定义的环境变量,用户的环境变量一般在移值的对应板子头文件里,比如致远开发板中,就在mx28_evk.h中定义了该环境变量,如下:
/*
1 Extra Environments
*/
#define CONFIG_EXTRA_ENV_SETTINGS \
"kernel=uImage\0" \
"kernelsize=0x400000\0" \
"rootfs=rootfs.ubifs\0" \
"showbitmap=0\0" \
"kerneladdr=" "0x00200000\0" \
"kerneladdr2=" "0x00780000\0" \
"gatewayip=" "192.168.1.1\0" \
"ds2460_mac=00:04:00:00:00:00\0" \
"nfsroot=/nfsroot/rootfs\0" \
"bootargs=gpmi=g console=ttyAM0,115200n8 ubi.mtd=5 root=ubi0:rootfs rootfstype=ubifs ro\0" \
"bootargs_nfs=setenv bootargs gpmi=g root=/dev/nfs rw console=ttyAM0,115200n8 nfsroot=$(serverip):$(nfsroot) ip=$(ipaddr):$(serverip):$(gatewayip):$(netmask):epc.zlgmcu.com:eth0:down\0" \
"bootcmd_net=run bootargs_nfs;\0" \
"bootcmd_mmc=run bootargs_mmc; " \
"mmc read 0 ${loadaddr} 100 3000; bootm\0" \
"bootargs_nand=setenv bootargs gpmi=g console=ttyAM0,115200n8 ubi.mtd=5 root=ubi0:rootfs rootfstype=ubifs ro\0" \
"bootargs_mmc=gpmi=g console=ttyAM0,115200n8 console=tty0 root=/dev/mmcblk0p3 rw\0" \
"upuboot=tftp $(loadaddr) $(serverip):imx28_ivt_uboot.sb;nand erase 0x0 0x100000; nand write $(loadaddr) 0x0 0x100000\0" \
"upkernel=" "tftp $(loadaddr) $(serverip):$(kernel);" \
"nand erase $(kerneladdr) $(kernelsize);" \
"nand write $(loadaddr) $(kerneladdr) $(filesize);\0" \
"upkernel2=" "nand erase $(kerneladdr2) $(kernelsize);" \
"nand write $(loadaddr) $(kerneladdr2) $(filesize)\0" \
"uprootfs=" "mtdparts default;" \
"nand erase rootfs;" \
"ubi part rootfs;" \
"ubi create rootfs;" \
"tftp $(loadaddr) $(rootfs);" \
"ubi write $(loadaddr) rootfs $(filesize)\0" \
"sd_upkernel=" "mmc read 0 0x42000000 1 1; fatload mmc 0:1 42000000 uImage;" \
"nand erase $(kerneladdr) $(kernelsize);" \
"nand write $(loadaddr) $(kerneladdr) $(filesize);\0" \
"sd_upkernel2=" "nand erase $(kerneladdr2) $(kernelsize);" \
"nand write $(loadaddr) $(kerneladdr2) $(filesize)\0" \
"sd_uprootfs=" "mtdparts default;" \
"nand erase rootfs;" \
"ubi part rootfs;" \
"ubi create rootfs;" \
"mmc read 0 0x42000000 1 1; fatload mmc 0:1 42000000 rootfs.ubifs;" \
"ubi write $(loadaddr) rootfs $(filesize)\0" \
"tftp_boot=tftp $(loadaddr) $(serverip):uImage; bootm;\0" \
"nand_boot=nand read.jffs2 $(loadaddr) $(kerneladdr) $(kernelsize);" \
"bootm $(loadaddr);" \
"nand read.jffs2 $(loadaddr) $(kerneladdr2) $(kernelsize);" \
"bootm $(loadaddr)\0" \
"setnandboot=setenv bootcmd 'run nand_boot';saveenv \0" \
"settftpboot=setenv bootcmd 'run tftp_boot';saveenv \0" \
"upsystem=run upkernel;run upkernel2;run uprootfs;reset \0" \
"sd_upsystem=run sd_upkernel;run sd_upkernel2;run sd_uprootfs;reset \0"