2.9.1、uboot的环境变量基础
2.9.1.1、环境变量的作用
2.9.1.2、环境变量的优先级
x210 # bdinfo
arch_number = 0x00000998
env_t = 0x00000000
boot_params = 0x30000100
DRAM bank = 0x00000000
-> start = 0x30000000
-> size = 0x10000000
DRAM bank = 0x00000001
-> start = 0x40000000
-> size = 0x10000000
ethaddr = 00:40:5C:26:0A:5B
ip_addr = 192.168.1.20
baudrate = 115200 bps
var foo = 'bar';
2.9.1.3、环境变量在uboot中工作方式
#define XMK_STR(x) #x
#define MK_STR(x) XMK_STR(x)
#if defined(CONFIG_S3C6410) || defined(CONFIG_S3C6430) || defined(CONFIG_S5P6440) || defined(CONFIG_S5PC100) || defined(CONFIG_S5PC110) || defined(CONFIG_S5P6442)
uchar default_environment[CFG_ENV_SIZE] = {
#else
uchar default_environment[] = {
#endif
#ifdef CONFIG_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0"
#endif
#ifdef CONFIG_BOOTCOMMAND
"bootcmd=" CONFIG_BOOTCOMMAND "\0"
2.9.2、环境变量相关命令源码解析1
2.9.2.1、printenv
x210 # print
mtdpart=80000 400000 3000000
baudrate=115200
ethaddr=00:40:5c:26:0a:5b
bootargs=console=ttySAC2,115200 root=/dev/mmcblk0p2 rw init=/linuxrc rootfstype=ext3
bootcmd=movi read kernel 30008000; bootm 30008000
filesize=37B800
fileaddr=30000000
gatewayip=192.168.1.1
netmask=255.255.255.0
ipaddr=192.168.1.20
serverip=192.168.1.141
bootdelay=10
U_BOOT_CMD(
printenv, CFG_MAXARGS, 1, do_printenv,
"printenv- print environment variables\n",
"\n - print values of all environment variables\n"
"printenv name ...\n"
" - print value of environment variable 'name'\n"
);
if (argc == 1) {
for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
;
for (k=i; k<nxt; ++k)
putc(env_get_char(k));
putc ('\n');
if (ctrlc()) {
puts ("\n ** Abort\n");
return 1;
}
}
printf("\nEnvironment size: %d/%ld bytes\n",
i, (ulong)ENV_SIZE);
return 0;
}
uchar env_get_char (int index)
{
uchar c;
if (gd->flags & GD_FLG_RELOC)
c = env_get_char_memory(index);
else
c = env_get_char_init(index);
return (c);
}
2.9.3、环境变量相关命令源码解析2
2.9.3.1、setenv
U_BOOT_CMD(
setenv, CFG_MAXARGS, 0, do_setenv,
"setenv - set environment variables\n",
"name value ...\n"
" - set environment variable 'name' to 'value ...'\n"
"setenv name\n"
" - delete environment variable 'name'\n"
);
int do_setenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
if (argc < 2) {
printf ("Usage:\n%s\n", cmdtp->usage);
return 1;
}
return _do_setenv (flag, argc, argv);
}
oldval = -1;
for (env=env_data; *env; env=nxt+1) {
for (nxt=env; *nxt; ++nxt)
;
if ((oldval = envmatch((uchar *)name, env-env_data)) >= 0)
break;
}
if (*++nxt == '\0') {
if (env > env_data) {
env--;
} else {
*env = '\0';
}
} else {
for (;;) {
*env = *nxt++;
if ((*env == '\0') && (*nxt == '\0'))
break;
++env;
}
}
*++env = '\0';
}
2.9.4、环境变量相关命令源码解析2
2.9.4.1、saveenv
#if ((defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND)) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_MOVINAND)) \
&& !defined(CFG_ENV_IS_NOWHERE)))
U_BOOT_CMD(
saveenv, 1, 0, do_saveenv,
"saveenv - save environment variables to persistent storage\n",
NULL
);
#endif
#if ((defined(CFG_ENV_IS_IN_NVRAM) || defined(CFG_ENV_IS_IN_EEPROM) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_NAND)) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_ONENAND)) \
|| (defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_MOVINAND)) \
&& !defined(CFG_ENV_IS_NOWHERE)))
int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
extern char * env_name_spec;
printf ("Saving Environment to %s...\n", env_name_spec);
return (saveenv() ? 1 : 0);
}
#endif
x210 # save
Saving Environment to SMDK bootable device...
done
char * env_name_spec = "SMDK bootable device";
#define CFG_ENV_IS_IN_AUTO
int saveenv(void)
{
#if defined(CONFIG_S5PC100) || defined(CONFIG_S5PC110) || defined(CONFIG_S5P6442)
if (INF_REG3_REG == 2)
saveenv_nand();
else if (INF_REG3_REG == 3)
saveenv_movinand();
else if (INF_REG3_REG == 1)
saveenv_onenand();
else if (INF_REG3_REG == 4)
saveenv_nor();
#elif defined(CONFIG_SMD
#define INF_REG_BASE 0xE010F000
#define INF_REG3_OFFSET 0x0c
#define INF_REG3_REG __REG(INF_REG_BASE+INF_REG3_OFFSET)
int saveenv_movinand(void)
{
#if defined(CONFIG_CMD_MOVINAND)
movi_write_env(virt_to_phys((ulong)env_ptr));
puts("done\n");
return 1;
#else
return 0;
#endif
}
void movi_write_env(ulong addr)
{
movi_write(raw_area_control.image[2].start_blk,
raw_area_control.image[2].used_blk, addr);
}
2.9.5、uboot内部获取环境变量
2.9.5.1、getenv
char *getenv (char *name)
{
int i, nxt;
WATCHDOG_RESET();
for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
int val;
for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
if (nxt >= CFG_ENV_SIZE) {
return (NULL);
}
}
if ((val=envmatch((uchar *)name, i)) < 0)
continue;
return ((char *)env_get_addr(val));
}
return (NULL);
}
2.9.5.2、getenv_r
int getenv_r (char *name, char *buf, unsigned len)
{
int i, nxt;
for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
int val, n;
for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
if (nxt >= CFG_ENV_SIZE) {
return (-1);
}
}
if ((val=envmatch((uchar *)name, i)) < 0)
continue;
n = 0;
while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
;
if (len == n)
*buf = '\0';
return (n);
}
return (-1);
}
2.9.5.3、总结