cmd_nvedit.c

来自「U-boot源码 ARM7启动代码」· C语言 代码 · 共 649 行 · 第 1/2 页

C
649
字号
/* * (C) Copyright 2000-2002 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> * Andreas Heppel <aheppel@sysgo.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 * * The "environment" is stored as a list of '\0' terminated * "name=value" strings. The end of the list is marked by a double * '\0'. New entries are always added at the end. Deleting an entry * shifts the remaining entries to the front. Replacing an entry is a * combination of deleting the old value and adding the new one. * * The environment is preceeded by a 32 bit CRC over the data part. * ************************************************************************** */#include <common.h>#include <command.h>#include <environment.h>#include <watchdog.h>#include <serial.h>#include <linux/stddef.h>#include <asm/byteorder.h>#if defined(CONFIG_CMD_NET)#include <net.h>#endifDECLARE_GLOBAL_DATA_PTR;#if !defined(CFG_ENV_IS_IN_NVRAM)	&& \    !defined(CFG_ENV_IS_IN_EEPROM)	&& \    !defined(CFG_ENV_IS_IN_FLASH)	&& \    !defined(CFG_ENV_IS_IN_DATAFLASH)	&& \    !defined(CFG_ENV_IS_IN_NAND)	&& \    !defined(CFG_ENV_IS_IN_ONENAND)	&& \    !defined(CFG_ENV_IS_NOWHERE)# error Define one of CFG_ENV_IS_IN_{NVRAM|EEPROM|FLASH|DATAFLASH|ONENAND|NOWHERE}#endif#define XMK_STR(x)	#x#define MK_STR(x)	XMK_STR(x)/************************************************************************************************************************************************//* Function that returns a character from the environment */extern uchar (*env_get_char)(int);/* Function that returns a pointer to a value from the environment *//* (Only memory version supported / needed). */extern uchar *env_get_addr(int);/* Function that updates CRC of the enironment */extern void env_crc_update (void);/************************************************************************************************************************************************/static int envmatch (uchar *, int);/* * Table with supported baudrates (defined in config_xyz.h) */static const unsigned long baudrate_table[] = CFG_BAUDRATE_TABLE;#define	N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))/************************************************************************ * Command interface: print one or all environment variables */int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){	int i, j, k, nxt;	int rcode = 0;	if (argc == 1) {		/* Print all env variables	*/		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/%d bytes\n", i, ENV_SIZE);		return 0;	}	for (i=1; i<argc; ++i) {	/* print single env variables	*/		char *name = argv[i];		k = -1;		for (j=0; env_get_char(j) != '\0'; j=nxt+1) {			for (nxt=j; env_get_char(nxt) != '\0'; ++nxt)				;			k = envmatch((uchar *)name, j);			if (k < 0) {				continue;			}			puts (name);			putc ('=');			while (k < nxt)				putc(env_get_char(k++));			putc ('\n');			break;		}		if (k < 0) {			printf ("## Error: \"%s\" not defined\n", name);			rcode ++;		}	}	return rcode;}/************************************************************************ * Set a new environment variable, * or replace or delete an existing one. * * This function will ONLY work with a in-RAM copy of the environment */int _do_setenv (int flag, int argc, char *argv[]){	int   i, len, oldval;	int   console = -1;	uchar *env, *nxt = NULL;	char *name;	bd_t *bd = gd->bd;	uchar *env_data = env_get_addr(0);	if (!env_data)	/* need copy in RAM */		return 1;	name = argv[1];	if (strchr(name, '=')) {		printf ("## Error: illegal character '=' in variable name \"%s\"\n", name);		return 1;	}	/*	 * search if variable with this name already exists	 */	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;	}	/*	 * Delete any existing definition	 */	if (oldval >= 0) {#ifndef CONFIG_ENV_OVERWRITE		/*		 * Ethernet Address and serial# can be set only once,		 * ver is readonly.		 */#ifdef CONFIG_HAS_UID		/* Allow serial# forced overwrite with 0xdeaf4add flag */		if ( ((strcmp (name, "serial#") == 0) && (flag != 0xdeaf4add)) ||#else		if ( (strcmp (name, "serial#") == 0) ||#endif		    ((strcmp (name, "ethaddr") == 0)#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)		     && (strcmp ((char *)env_get_addr(oldval),MK_STR(CONFIG_ETHADDR)) != 0)#endif	/* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */		    ) ) {			printf ("Can't overwrite \"%s\"\n", name);			return 1;		}#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! */				printf("Can't delete \"%s\"\n", name);				return 1;			}			/* Try assigning specified device */			if (console_assign (console, argv[2]) < 0)				return 1;#ifdef CONFIG_SERIAL_MULTI			if (serial_assign (argv[2]) < 0)				return 1;#endif		}		/*		 * Switch to new baudrate if new baudrate is supported		 */		if (strcmp(argv[1],"baudrate") == 0) {			int baudrate = simple_strtoul(argv[2], NULL, 10);			int i;			for (i=0; i<N_BAUDRATES; ++i) {				if (baudrate == baudrate_table[i])					break;			}			if (i == N_BAUDRATES) {				printf ("## Baudrate %d bps not supported\n",					baudrate);				return 1;			}			printf ("## Switch baudrate to %d bps and press ENTER ...\n",				baudrate);			udelay(50000);			gd->baudrate = baudrate;#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)			gd->bd->bi_baudrate = baudrate;#endif			serial_setbrg ();			udelay(50000);			for (;;) {				if (getc() == '\r')				      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';	}#ifdef CONFIG_NET_MULTI	if (strncmp(name, "eth", 3) == 0) {		char *end;		int   num = simple_strtoul(name+3, &end, 10);		if (strcmp(end, "addr") == 0) {			eth_set_enetaddr(num, argv[2]);		}	}#endif	/* Delete only ? */	if ((argc < 3) || argv[2] == NULL) {		env_crc_update ();		return 0;	}	/*	 * Append new definition at the end	 */	for (env=env_data; *env || *(env+1); ++env)		;	if (env > env_data)		++env;	/*	 * Overflow when:	 * "name" + "=" + "val" +"\0\0"  > ENV_SIZE - (env-env_data)	 */	len = strlen(name) + 2;	/* add '=' for first arg, ' ' for all others */	for (i=2; i<argc; ++i) {		len += strlen(argv[i]) + 1;	}	if (len > (&env_data[ENV_SIZE]-env)) {		printf ("## Error: environment overflow, \"%s\" deleted\n", name);		return 1;	}	while ((*env = *name++) != '\0')		env++;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?