diff options
Diffstat (limited to 'tools/env')
| -rw-r--r-- | tools/env/Makefile | 5 | ||||
| -rw-r--r-- | tools/env/README | 4 | ||||
| -rw-r--r-- | tools/env/fw_env.c | 104 | ||||
| -rw-r--r-- | tools/env/fw_env.h | 19 | ||||
| -rw-r--r-- | tools/env/fw_env_main.c | 59 | 
5 files changed, 109 insertions, 82 deletions
| diff --git a/tools/env/Makefile b/tools/env/Makefile index 07634bcff..ab73c8c74 100644 --- a/tools/env/Makefile +++ b/tools/env/Makefile @@ -24,13 +24,14 @@  include $(TOPDIR)/config.mk  HOSTSRCS := $(SRCTREE)/lib/crc32.c  fw_env.c  fw_env_main.c -HEADERS	:= fw_env.h +HEADERS	:= fw_env.h $(OBJTREE)/include/config.h  # Compile for a hosted environment on the target  HOSTCPPFLAGS  = -idirafter $(SRCTREE)/include \  		-idirafter $(OBJTREE)/include2 \  		-idirafter $(OBJTREE)/include \ -		-DUSE_HOSTCC +		-DUSE_HOSTCC \ +		-DTEXT_BASE=$(TEXT_BASE)  ifeq ($(MTD_VERSION),old)  HOSTCPPFLAGS += -DMTD_OLD diff --git a/tools/env/README b/tools/env/README index 3f0d77e2a..df020e4af 100644 --- a/tools/env/README +++ b/tools/env/README @@ -55,3 +55,7 @@ partition where the environment resides.  DEVICEx_ENVSECTORS defines the number of sectors that may be used for  this environment instance. On NAND this is used to limit the range  within which bad blocks are skipped, on NOR it is not used. + +To prevent losing changes to the environment and to prevent confusing the MTD +drivers, a lock file at /var/lock/fw_printenv.lock is used to serialize access +to the environment. diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 1a2c22756..ab8c15d30 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -26,6 +26,7 @@  #include <errno.h>  #include <fcntl.h> +#include <linux/stringify.h>  #include <stdio.h>  #include <stdlib.h>  #include <stddef.h> @@ -45,8 +46,6 @@  #include "fw_env.h" -#include <config.h> -  #define WHITESPACE(c) ((c == '\t') || (c == ' '))  #define min(x, y) ({				\ @@ -81,7 +80,7 @@ static int dev_current;  #define ENVSECTORS(i) envdevices[(i)].env_sectors  #define DEVTYPE(i)    envdevices[(i)].mtd_type -#define CONFIG_ENV_SIZE ENVSIZE(dev_current) +#define CUR_ENVSIZE ENVSIZE(dev_current)  #define ENV_SIZE      getenvsize() @@ -121,9 +120,6 @@ static unsigned char active_flag = 1;  static unsigned char obsolete_flag = 0; -#define XMK_STR(x)	#x -#define MK_STR(x)	XMK_STR(x) -  static char default_environment[] = {  #if defined(CONFIG_BOOTARGS)  	"bootargs=" CONFIG_BOOTARGS "\0" @@ -138,40 +134,40 @@ static char default_environment[] = {  	"nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"  #endif  #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) -	"bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0" +	"bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0"  #endif  #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) -	"baudrate=" MK_STR (CONFIG_BAUDRATE) "\0" +	"baudrate=" __stringify(CONFIG_BAUDRATE) "\0"  #endif  #ifdef	CONFIG_LOADS_ECHO -	"loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0" +	"loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0"  #endif  #ifdef	CONFIG_ETHADDR -	"ethaddr=" MK_STR (CONFIG_ETHADDR) "\0" +	"ethaddr=" __stringify(CONFIG_ETHADDR) "\0"  #endif  #ifdef	CONFIG_ETH1ADDR -	"eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0" +	"eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0"  #endif  #ifdef	CONFIG_ETH2ADDR -	"eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0" +	"eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0"  #endif  #ifdef	CONFIG_ETH3ADDR -	"eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0" +	"eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0"  #endif  #ifdef	CONFIG_ETH4ADDR -	"eth4addr=" MK_STR (CONFIG_ETH4ADDR) "\0" +	"eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0"  #endif  #ifdef	CONFIG_ETH5ADDR -	"eth5addr=" MK_STR (CONFIG_ETH5ADDR) "\0" +	"eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0"  #endif  #ifdef	CONFIG_ETHPRIME  	"ethprime=" CONFIG_ETHPRIME "\0"  #endif  #ifdef	CONFIG_IPADDR -	"ipaddr=" MK_STR (CONFIG_IPADDR) "\0" +	"ipaddr=" __stringify(CONFIG_IPADDR) "\0"  #endif  #ifdef	CONFIG_SERVERIP -	"serverip=" MK_STR (CONFIG_SERVERIP) "\0" +	"serverip=" __stringify(CONFIG_SERVERIP) "\0"  #endif  #ifdef	CONFIG_SYS_AUTOLOAD  	"autoload=" CONFIG_SYS_AUTOLOAD "\0" @@ -180,19 +176,19 @@ static char default_environment[] = {  	"rootpath=" CONFIG_ROOTPATH "\0"  #endif  #ifdef	CONFIG_GATEWAYIP -	"gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0" +	"gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0"  #endif  #ifdef	CONFIG_NETMASK -	"netmask=" MK_STR (CONFIG_NETMASK) "\0" +	"netmask=" __stringify(CONFIG_NETMASK) "\0"  #endif  #ifdef	CONFIG_HOSTNAME -	"hostname=" MK_STR (CONFIG_HOSTNAME) "\0" +	"hostname=" __stringify(CONFIG_HOSTNAME) "\0"  #endif  #ifdef	CONFIG_BOOTFILE  	"bootfile=" CONFIG_BOOTFILE "\0"  #endif  #ifdef	CONFIG_LOADADDR -	"loadaddr=" MK_STR (CONFIG_LOADADDR) "\0" +	"loadaddr=" __stringify(CONFIG_LOADADDR) "\0"  #endif  #ifdef	CONFIG_PREBOOT  	"preboot=" CONFIG_PREBOOT "\0" @@ -201,7 +197,7 @@ static char default_environment[] = {  	"clocks_in_mhz=" "1" "\0"  #endif  #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) -	"pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0" +	"pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY) "\0"  #endif  #ifdef	CONFIG_ENV_VARS_UBOOT_CONFIG  	"arch=" CONFIG_SYS_ARCH "\0" @@ -229,7 +225,7 @@ static int get_config (char *);  #endif  static inline ulong getenvsize (void)  { -	ulong rc = CONFIG_ENV_SIZE - sizeof (long); +	ulong rc = CUR_ENVSIZE - sizeof(long);  	if (HaveRedundEnv)  		rc -= sizeof (char); @@ -260,9 +256,6 @@ char *fw_getenv (char *name)  {  	char *env, *nxt; -	if (fw_env_open()) -		return NULL; -  	for (env = environment.data; *env; env = nxt + 1) {  		char *val; @@ -411,7 +404,7 @@ int fw_env_write(char *name, char *value)  		    (strcmp(name, "serial#") == 0) ||  		    ((strcmp(name, "ethaddr") == 0)  #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) -		    && (strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0) +		    && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0)  #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */  		   ) ) {  			fprintf (stderr, "Can't overwrite \"%s\"\n", name); @@ -445,7 +438,7 @@ int fw_env_write(char *name, char *value)  		++env;  	/*  	 * Overflow when: -	 * "name" + "=" + "val" +"\0\0"  > CONFIG_ENV_SIZE - (env-environment) +	 * "name" + "=" + "val" +"\0\0"  > CUR_ENVSIZE - (env-environment)  	 */  	len = strlen (name) + 2;  	/* add '=' for first arg, ' ' for all others */ @@ -483,7 +476,6 @@ int fw_setenv(int argc, char *argv[])  	int i, len;  	char *name;  	char *value = NULL; -	char *tmpval = NULL;  	if (argc < 2) {  		errno = EINVAL; @@ -497,34 +489,28 @@ int fw_setenv(int argc, char *argv[])  	name = argv[1]; -	len = strlen(name) + 2; -	for (i = 2; i < argc; ++i) -		len += strlen(argv[i]) + 1; - -	/* Allocate enough place to the data string */ +	len = 0;  	for (i = 2; i < argc; ++i) {  		char *val = argv[i]; +		size_t val_len = strlen(val); + +		value = realloc(value, len + val_len + 1);  		if (!value) { -			value = (char *)malloc(len - strlen(name)); -			if (!value) { -				fprintf(stderr, +			fprintf(stderr,  				"Cannot malloc %zu bytes: %s\n", -				len - strlen(name), strerror(errno)); -				return -1; -			} -			memset(value, 0, len - strlen(name)); -			tmpval = value; +				len, strerror(errno)); +			return -1;  		} -		if (i != 2) -			*tmpval++ = ' '; -		while (*val != '\0') -			*tmpval++ = *val++; + +		memcpy(value + len, val, val_len); +		len += val_len; +		value[len++] = ' ';  	} +	value[len - 1] = '\0';  	fw_env_write(name, value); -	if (value) -		free(value); +	free(value);  	return fw_env_close();  } @@ -960,8 +946,8 @@ static int flash_write (int fd_current, int fd_target, int dev_target)  	printf ("Writing new environment at 0x%lx on %s\n",  		DEVOFFSET (dev_target), DEVNAME (dev_target));  #endif -	rc = flash_write_buf (dev_target, fd_target, environment.image, -			      CONFIG_ENV_SIZE, DEVOFFSET (dev_target), +	rc = flash_write_buf(dev_target, fd_target, environment.image, +			      CUR_ENVSIZE, DEVOFFSET(dev_target),  			      DEVTYPE(dev_target));  	if (rc < 0)  		return rc; @@ -1000,10 +986,10 @@ static int flash_read (int fd)  	DEVTYPE(dev_current) = mtdinfo.type; -	rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE, +	rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,  			     DEVOFFSET (dev_current), mtdinfo.type); -	return (rc != CONFIG_ENV_SIZE) ? -1 : 0; +	return (rc != CUR_ENVSIZE) ? -1 : 0;  }  static int flash_io (int mode) @@ -1072,6 +1058,8 @@ exit:  static char *envmatch (char * s1, char * s2)  { +	if (s1 == NULL || s2 == NULL) +		return NULL;  	while (*s1 == *s2++)  		if (*s1++ == '=') @@ -1100,11 +1088,11 @@ int fw_env_open(void)  	if (parse_config ())		/* should fill envdevices */  		return -1; -	addr0 = calloc (1, CONFIG_ENV_SIZE); +	addr0 = calloc(1, CUR_ENVSIZE);  	if (addr0 == NULL) { -		fprintf (stderr, +		fprintf(stderr,  			"Not enough memory for environment (%ld bytes)\n", -			CONFIG_ENV_SIZE); +			CUR_ENVSIZE);  		return -1;  	} @@ -1139,11 +1127,11 @@ int fw_env_open(void)  		flag0 = *environment.flags;  		dev_current = 1; -		addr1 = calloc (1, CONFIG_ENV_SIZE); +		addr1 = calloc(1, CUR_ENVSIZE);  		if (addr1 == NULL) { -			fprintf (stderr, +			fprintf(stderr,  				"Not enough memory for environment (%ld bytes)\n", -				CONFIG_ENV_SIZE); +				CUR_ENVSIZE);  			return -1;  		}  		redundant = addr1; diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h index ad3244664..a1a680744 100644 --- a/tools/env/fw_env.h +++ b/tools/env/fw_env.h @@ -21,6 +21,15 @@   * MA 02111-1307 USA   */ +/* Pull in the current config to define the default environment */ +#ifndef __ASSEMBLY__ +#define __ASSEMBLY__ /* get only #defines from config.h */ +#include <config.h> +#undef	__ASSEMBLY__ +#else +#include <config.h> +#endif +  /*   * To build the utility with the static configuration   * comment out the next line. @@ -29,6 +38,7 @@   */  #define CONFIG_FILE     "/etc/fw_env.config" +#ifndef CONFIG_FILE  #define HAVE_REDUND /* For systems with 2 env sectors */  #define DEVICE1_NAME      "/dev/mtd1"  #define DEVICE2_NAME      "/dev/mtd2" @@ -40,14 +50,23 @@  #define ENV2_SIZE         0x4000  #define DEVICE2_ESIZE     0x4000  #define DEVICE2_ENVSECTORS     2 +#endif +#ifndef CONFIG_BAUDRATE  #define CONFIG_BAUDRATE		115200 +#endif + +#ifndef CONFIG_BOOTDELAY  #define CONFIG_BOOTDELAY	5	/* autoboot after 5 seconds	*/ +#endif + +#ifndef CONFIG_BOOTCOMMAND  #define CONFIG_BOOTCOMMAND							\  	"bootp; "								\  	"setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} "	\  	"ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; "	\  	"bootm" +#endif  extern int   fw_printenv(int argc, char *argv[]);  extern char *fw_getenv  (char *name); diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c index c65405731..c855f4c17 100644 --- a/tools/env/fw_env_main.c +++ b/tools/env/fw_env_main.c @@ -39,10 +39,13 @@   *		  variable "name"   */ +#include <fcntl.h> +#include <getopt.h>  #include <stdio.h>  #include <string.h>  #include <stdlib.h> -#include <getopt.h> +#include <sys/file.h> +#include <unistd.h>  #include "fw_env.h"  #define	CMD_PRINTENV	"fw_printenv" @@ -81,13 +84,27 @@ void usage(void)  	);  } -int -main(int argc, char *argv[]) +int main(int argc, char *argv[])  {  	char *p;  	char *cmdname = *argv;  	char *script_file = NULL;  	int c; +	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; +	int lockfd = -1; +	int retval = EXIT_SUCCESS; + +	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC); +	if (-1 == lockfd) { +		fprintf(stderr, "Error opening lock file %s\n", lockname); +		return EXIT_FAILURE; +	} + +	if (-1 == flock(lockfd, LOCK_EX)) { +		fprintf(stderr, "Error locking file %s\n", lockname); +		close(lockfd); +		return EXIT_FAILURE; +	}  	if ((p = strrchr (cmdname, '/')) != NULL) {  		cmdname = p + 1; @@ -104,38 +121,36 @@ main(int argc, char *argv[])  			break;  		case 'h':  			usage(); -			return EXIT_SUCCESS; +			goto exit;  		default: /* '?' */  			fprintf(stderr, "Try `%s --help' for more information."  				"\n", cmdname); -			return EXIT_FAILURE; +			retval = EXIT_FAILURE; +			goto exit;  		}  	} -  	if (strcmp(cmdname, CMD_PRINTENV) == 0) { - -		if (fw_printenv (argc, argv) != 0) -			return EXIT_FAILURE; - -		return EXIT_SUCCESS; - +		if (fw_printenv(argc, argv) != 0) +			retval = EXIT_FAILURE;  	} else if (strcmp(cmdname, CMD_SETENV) == 0) {  		if (!script_file) {  			if (fw_setenv(argc, argv) != 0) -				return EXIT_FAILURE; +				retval = EXIT_FAILURE;  		} else {  			if (fw_parse_script(script_file) != 0) -				return EXIT_FAILURE; +				retval = EXIT_FAILURE;  		} - -		return EXIT_SUCCESS; - +	} else { +		fprintf(stderr, +			"Identity crisis - may be called as `" CMD_PRINTENV +			"' or as `" CMD_SETENV "' but not as `%s'\n", +			cmdname); +		retval = EXIT_FAILURE;  	} -	fprintf (stderr, -		"Identity crisis - may be called as `" CMD_PRINTENV -		"' or as `" CMD_SETENV "' but not as `%s'\n", -		cmdname); -	return EXIT_FAILURE; +exit: +	flock(lockfd, LOCK_UN); +	close(lockfd); +	return retval;  } |