⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmd_nvedit.c

📁 source code of armboot for s3c4510
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * (C) Copyright 2000 * 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 */#include "armboot.h"#include "command.h"#include "malloc.h"#include "cmd_nvedit.h"#if (CONFIG_COMMANDS & CFG_CMD_NET)#include "net.h"#endif/* * 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]))/* * Default settings to be used when no valid environment is found */#define XMK_STR(x)	#x#define MK_STR(x)	XMK_STR(x)uchar default_environment[] = {#ifdef	CONFIG_BOOTARGS	"bootargs="	CONFIG_BOOTARGS			"\0"#endif#ifdef	CONFIG_BOOTCOMMAND	"bootcmd="	CONFIG_BOOTCOMMAND		"\0"#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/* choish 20020828 */#if defined(CONFIG_S3C2500) || defined(CONFIG_S3C2510) #ifdef	CONFIG_ICACHE	"icache="	MK_STR(CONFIG_ICACHE)	"\0" #endif #ifdef	CONFIG_DCACHE	"dcache="	MK_STR(CONFIG_DCACHE)	"\0" #endif #ifdef	CONFIG_WBUFFER 	"wbuffer="	MK_STR(CONFIG_WBUFFER)	"\0" #endif#endif/* choish end */#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_ETH2ADDR	"eth2addr="	MK_STR(CONFIG_ETH2ADDR)		"\0"#endif#ifdef	CONFIG_ETH3ADDR	"eth3addr="	MK_STR(CONFIG_ETH3ADDR)		"\0"#endif#ifdef	CFG_AUTOLOAD	"autoload="	CFG_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/* choish 20020829   for kernel */#if defined(CONFIG_S3C2500) || defined(CONFIG_S3C2510)	 #ifdef	CONFIG_HWADDR0	"HWADDR0="  MK_STR(CONFIG_HWADDR0)		"\0" #endif #ifdef	CONFIG_HWADDR1	"HWADDR1="  MK_STR(CONFIG_HWADDR1)		"\0" #endif#endif#ifdef	CONFIG_IPADDR	"ipaddr="	MK_STR(CONFIG_IPADDR)		"\0"#endif#ifdef	CONFIG_SERVERIP	"serverip="	MK_STR(CONFIG_SERVERIP)		"\0"#endif /* choish end */	"\0"};static int envmatch (bd_t *, uchar *, int);/* * return one character from env */static uchar get_env_char(bd_t *bd, int index){    uchar c;       /* use RAM copy, if possible */    if (bd->bi_env)    {		if (index < sizeof(bd->bi_env_data))	  		c = bd->bi_env_data[index];		else	  		panic("bad size for get_env_char! %s[%d]\n", __FILE__, __LINE__);    }    else    {		/* try a board specific lookup */		if (board_env_getchar(bd, index, &c) < 0)		{			if (index < sizeof(default_environment))			  c = default_environment[index];			else 			  panic("bad size for get_env_char %s[%d]!\n", __FILE__, __LINE__);		}    }    return c;}/* * return address into environment */static uchar *get_env_addr(bd_t *bd, int index){    uchar *p = 0;       /* use RAM copy, if possible */    if (bd->bi_env)    {		if (index < sizeof(bd->bi_env_data))		  p = &bd->bi_env_data[index];		else		  panic("bad size for get_env_char! %s[%d]\n", __FILE__, __LINE__);    }    else    {		/* try a board specific lookup */		if ((p = board_env_getaddr(bd, index)) == 0)		{			if (index < sizeof(default_environment))			  p = &default_environment[index];			else 			  panic("bad size for get_env_char! %s[%d]\n", __FILE__, __LINE__);		}    }    return p;}/************************************************************************ * Command interface: print one or all environment variables */int do_printenv (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[]){    int i, j, k, nxt;        if (argc == 1) 	{		/* Print all env variables	*/		for (i=0; get_env_char(bd, i) != '\0'; i=nxt+1) 		{			for (nxt=i; get_env_char(bd, nxt) != '\0'; ++nxt)			  ;			for (k=i; k<nxt; ++k)			  putc(get_env_char(bd, k));			putc  ('\n');						if (ctrlc()) {			printf ("\n ** Abort\n");			return 1;		}	}		printf("\nEnvironment size: %d/%ld bytes\n", i, sizeof(bd->bi_env_data));		return 0;    }        for (i=1; i<argc; ++i) {	/* print single env variables	*/	char *name = argv[i];		k = -1;		for (j=0; get_env_char(bd, j) != '\0'; j=nxt+1) {	    	    for (nxt=j; get_env_char(bd, nxt) != '\0'; ++nxt)	      ;	    k = envmatch(bd, name, j);	    if (k < 0) {		continue;	    }	    puts (name);	    putc ('=');	    while (k < nxt)	      putc(get_env_char(bd, k++));	    putc ('\n');	    break;	}	if (k < 0)	{	  printf ("## Error: \"%s\" not defined\n", name);	  return 1;	}    }    return 0;}int _do_setenv (bd_t *bd, int flag, int argc, char *argv[]){    int   i, len, oldval;    uchar *env, *nxt = 0;    uchar *name;        /* need writable copy in RAM */    if (!bd->bi_env_data)      return 1;    name = argv[1];        /*     * search if variable with this name already exists     */    oldval = -1;    for (env = bd->bi_env_data; *env; env = nxt+1) 	{		for (nxt = env; *nxt; ++nxt);		if ((oldval = envmatch(bd, name, (ulong)env - (ulong)bd->bi_env_data)) >= 0)	  		break;    }        /*     * Delete any existing definition     */    if (oldval >= 0) 	{#ifndef CONFIG_ENV_OVERWRITE		/*	 * Ethernet Address and serial# can be set only once	 */		if ( (strcmp (name, "serial#") == 0) ||	    	((strcmp (name, "ethaddr") == 0)#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)	     	&& (strcmp (get_env_addr(bd, oldval),MK_STR(CONFIG_ETHADDR)) != 0)#endif	/* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */	     	) ) 		{	    	printf ("Can't overwrite \"%s\"\n", name);	    	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);	    	serial_setbrg (bd, baudrate);	    	udelay(50000);	    	for (;;) 			{				if (getc() == '\r')		  			break;	    	}	    	bd->bi_baudrate = baudrate;		}			if (*++nxt == '\0') 		{	    	if ((ulong)env > (ulong)bd->bi_env_data) 			{				env--;	    	}			else 			{				*env = '\0';	    	}		} 		else		{	    	for (;;) 			{				*env = *nxt++;				if ((*env == '\0') && (*nxt == '\0'))		  			break;				++env;	    	}		}			*++env = '\0';    }    /* Delete only ? */    if ((argc < 3) || argv[2] == NULL) 	{    		/* Update CRC */		bd->bi_env_crc = crc32(0, bd->bi_env_data, sizeof(bd->bi_env_data));		return 0;    }    /*     * Append new definition at the end     */    for (env = bd->bi_env_data; *env || *(env+1); ++env);	    if ((ulong)env > (ulong)bd->bi_env_data)      ++env;    /*     * Overflow when:     * "name" + "=" + "val" +"\0\0"  >      *      sizeof(bd->bi_env_data) - (env-bd->bi_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 > sizeof(bd->bi_env_data)) 	{

⌨️ 快捷键说明

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