📄 cmd_nvedit.c
字号:
/*
* (C) Copyright 2000
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/*
* Support for persistent environment data
*/
#include <ppcboot.h>
#include <command.h>
#include <cmd_nvedit.h>
#ifdef CONFIG_4xx
#include <malloc.h>
#endif
/*
* The environment storages is simply a list of '\0'-terminated
* "name=value" strings, the end of the list marked by a double '\0'.
* New entries are always addrd at the end. Deleting an entry shifts
* the remaining entries to the front. Replacing an entry is a
* combination of deleting the old and adding the new value.
*/
#ifdef CONFIG_NVRAM_ENV
uchar *environment = (uchar *)CFG_NVRAM_VAR_ADDR;
ulong env_size = CFG_NVRAM_ENV_SIZE;
#else
#if defined(CFG_FLASH_ENV_ADDR)
static uchar environment[CFG_FLASH_ENV_SIZE];
static ulong env_size = CFG_FLASH_ENV_SIZE;
/* need both ENV and flash */
#if ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
static uchar *flash_addr = (uchar *)CFG_FLASH_ENV_ADDR;
#endif /* ENV, FLASH */
/*
* the contents of the rom_addr pointer will not change when the monitor
* is relocated into RAM and so will contain the old address of the
* environment[] array in flash, but the environment[] array itself *will*
* be relocated. Hence, we can tell whether the monitor has been relocated
* by comparing the value of rom_addr with environment.
*/
static uchar *rom_addr = environment;
#if defined(CONFIG_COGENT) && !defined(CONFIG_CMA302)
#define ISVALID(p) 0 /* can't get motherboard flash working yet */
#else
#define ISVALID(p) (crc32(0, (char *)(p), env_size - sizeof (ulong)) == \
*(ulong *)((char *)(p) + env_size - sizeof (ulong)))
#endif
#define MAKEVALID(p, s) (*(ulong *)((char *)(p) + (s) - sizeof (ulong)) = \
crc32(0, (char *)(p), (s) - sizeof (ulong)))
#else /* CFG_FLASH_ENV_ADDR */
extern uchar environment[];
extern ulong env_size;
/* need both ENV and flash */
#if ((CONFIG_COMMANDS & (CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
static uchar *flash_addr = environment;
#endif /* ENV, FLASH */
#endif /* CFG_FLASH_ENV_ADDR */
#endif /* CONFIG_NVRAM_ENV */
static uchar *envmatch (uchar *, uchar *);
#if defined(CFG_FLASH_ENV_ADDR)
static uchar *env_init(void);
#else
static void env_init(void);
#endif /* CFG_FLASH_ENV_ADDR */
#define XMK_STR(x) #x
#define MK_STR(x) XMK_STR(x)
static uchar default_environment[] = {
#ifdef CONFIG_BOOTARGS
"bootargs=" CONFIG_BOOTARGS "\0"
#endif
#ifdef CONFIG_BOOTCOMMAND
"bootcmd=" CONFIG_BOOTCOMMAND "\0"
#endif
#if (CONFIG_BOOTDELAY >= 0)
"bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
#endif
#if (CONFIG_BAUDRATE >= 0)
"baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
#endif
#ifdef CONFIG_ETHADDR
"ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
#endif
#ifdef CONFIG_IPADDR
"ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
#endif
#ifdef CONFIG_SERVERIP
"serverip=" MK_STR(CONFIG_SERVERIP) "\0"
#endif
"\0"
};
char *getenv (uchar *name)
{
uchar *env, *nxt;
#if defined(CFG_FLASH_ENV_ADDR)
uchar *environment = env_init();
#else
env_init();
#endif /* CFG_FLASH_ENV_ADDR */
for (env=environment; *env; env=nxt+1) {
char *val;
for (nxt=env; *nxt; ++nxt)
;
val=envmatch(name, env);
if (!val)
continue;
return (val);
}
return (NULL);
}
void do_printenv (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
{
uchar *env, *nxt;
int i;
#if defined(CFG_FLASH_ENV_ADDR)
uchar *environment = env_init();
#else
env_init();
#endif /* CFG_FLASH_ENV_ADDR */
if (argc == 1) { /* Print all env variables */
uchar *start = environment;
for (env=environment; *env; env=nxt+1) {
for (nxt=env; *nxt; ++nxt)
;
puts (env);
putc ('\n');
if (tstc()) {
getc ();
printf ("\n ** Abort\n");
return;
}
}
printf("\nEnvironment size: %d bytes\n", env-start);
return;
}
for (i=1; i<argc; ++i) { /* print single env variables */
char *name = argv[i];
char *val = NULL;
for (env=environment; *env; env=nxt+1) {
for (nxt=env; *nxt; ++nxt)
;
val=envmatch(name, env);
if (val) {
puts (name);
putc ('=');
puts (val);
putc ('\n');
break;
}
}
if (!val)
printf ("## Error: \"%s\" not defined\n", name);
}
}
void _do_setenv (bd_t *bd, int flag, int argc, char *argv[])
{
int i, len;
int console = -1;
uchar *env, *nxt;
uchar *oldval = NULL;
uchar *name;
#if defined(CFG_FLASH_ENV_ADDR)
uchar *environment = env_init();
ulong real_env_size = env_size;
ulong env_size = real_env_size - sizeof (ulong);
#else
env_init();
#endif /* CFG_FLASH_ENV_ADDR */
name = argv[1];
/*
* search if variable with this name already exists
*/
for (env=environment; *env; env=nxt+1) {
for (nxt=env; *nxt; ++nxt)
;
if ((oldval=envmatch(name, env)) != NULL)
break;
}
/*
* Delete any existing definition
*/
if (oldval) {
#ifndef CONFIG_ENV_OVERWRITE
/*
* Ethernet Address and serial# can be set only once
*/
if ((strcmp (name, "ethaddr") == 0) ||
(strcmp (name, "serial#") == 0) ) {
printf ("Can't overwrite \"%s\"\n", name);
return;
}
#endif
/* Check for console redirection */
if (strcmp(name,"stdin") == 0) {
console = stdin;
} else if (strcmp(name,"stdout") == 0) {
console = stdout;
} else if (strcmp(name,"stderr") == 0) {
console = stderr;
}
if (console != -1) {
if (argc < 3) /* Cannot delete it! */
return;
/* Try assigning specified device */
if (console_assign (console, argv[2]) < 0)
return;
}
if (*++nxt == '\0') {
*env = '\0';
} else {
for (;;) {
*env = *nxt++;
if ((*env == '\0') && (*nxt == '\0'))
break;
++env;
}
}
*++env = '\0';
}
#if defined(CFG_FLASH_ENV_ADDR)
MAKEVALID(environment, real_env_size);
#endif /* CFG_FLASH_ENV_ADDR */
/* Delete only ? */
if (argc < 3)
return;
/*
* Append new definition at the end
*/
for (env=environment; *env || *(env+1); ++env)
;
if (env > environment)
++env;
/*
* Overflow when:
* "name" + "=" + "val" +"\0\0" > env_size - (env-environment)
*/
len = strlen(name) + 2;
/* add '=' for first arg, ' ' for all others */
for (i=2; i<argc; ++i) {
len += strlen(argv[i]) + 1;
}
if (len > (&environment[env_size]-env)) {
printf ("## Error: environment overflow, \"%s\" deleted\n", name);
return;
}
while ((*env = *name++) != '\0')
env++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -