Skip to content
Snippets Groups Projects
nvedit.c 30.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Wolfgang Denk's avatar
    Wolfgang Denk committed
    /*
    
     * (C) Copyright 2000-2013
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
     *
     * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
     * Andreas Heppel <aheppel@sysgo.de>
    
     *
     * Copyright 2011 Freescale Semiconductor, Inc.
     *
    
     * SPDX-License-Identifier:	GPL-2.0+
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     */
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     * Support for persistent environment data
     *
    
     * The "environment" is stored on external storage as a list of '\0'
     * terminated "name=value" strings. The end of the list is marked by
     * a double '\0'. The environment is preceeded by a 32 bit CRC over
     * the data part and, in case of redundant environment, a byte of
     * flags.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     *
    
     * This linearized representation will also be used before
     * relocation, i. e. as long as we don't have a full C runtime
     * environment. After that, we use a hash table.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     */
    
    #include <common.h>
    
    #include <cli.h>
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #include <command.h>
    
    #include <console.h>
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #include <environment.h>
    
    #include <search.h>
    #include <errno.h>
    
    Peter Tyser's avatar
    Peter Tyser committed
    #include <malloc.h>
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #include <linux/stddef.h>
    #include <asm/byteorder.h>
    
    #include <asm/io.h>
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #if	!defined(CONFIG_ENV_IS_IN_EEPROM)	&& \
    	!defined(CONFIG_ENV_IS_IN_FLASH)	&& \
    	!defined(CONFIG_ENV_IS_IN_DATAFLASH)	&& \
    	!defined(CONFIG_ENV_IS_IN_MMC)		&& \
    
    	!defined(CONFIG_ENV_IS_IN_NAND)		&& \
    	!defined(CONFIG_ENV_IS_IN_NVRAM)	&& \
    	!defined(CONFIG_ENV_IS_IN_ONENAND)	&& \
    	!defined(CONFIG_ENV_IS_IN_SPI_FLASH)	&& \
    
    	!defined(CONFIG_ENV_IS_IN_REMOTE)	&& \
    
    	!defined(CONFIG_ENV_IS_IN_UBI)		&& \
    
    	!defined(CONFIG_ENV_IS_NOWHERE)
    
    # error Define one of CONFIG_ENV_IS_IN_{EEPROM|FLASH|DATAFLASH|ONENAND|\
    
    SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #endif
    
    
    /*
     * Maximum expected input data size for import command
     */
    #define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
     * This variable is incremented on each do_env_set(), so it can
    
     * be used via get_env_id() as an indication, if the environment
     * has changed or not. So it is possible to reread an environment
     * variable only if the environment was changed ... done so for
     * example in NetInitLoop()
     */
    
    static int env_id = 1;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    int get_env_id(void)
    
    {
    	return env_id;
    }
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #ifndef CONFIG_SPL_BUILD
    
     * Command interface: print one or all environment variables
     *
     * Returns 0 in case of error, or length of printed string
    
    static int env_print(char *name, int flag)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    
    	if (name) {		/* print a single name */
    		ENTRY e, *ep;
    
    		e.key = name;
    		e.data = NULL;
    
    		hsearch_r(e, FIND, &ep, &env_htab, flag);
    
    		len = printf("%s=%s\n", ep->key, ep->data);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	if (len > 0) {
    		puts(res);
    		free(res);
    		return len;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	}
    
    
    	printf("## Error: cannot export environment\n");
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
    			char * const argv[])
    
    {
    	int i;
    	int rcode = 0;
    
    	int env_flag = H_HIDE_DOT;
    
    	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'a') {
    		argc--;
    		argv++;
    		env_flag &= ~H_HIDE_DOT;
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	if (argc == 1) {
    		/* print all env vars */
    
    		rcode = env_print(NULL, env_flag);
    
    			return 1;
    		printf("\nEnvironment size: %d/%ld bytes\n",
    			rcode, (ulong)ENV_SIZE);
    		return 0;
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	/* print selected env vars */
    
    	env_flag &= ~H_HIDE_DOT;
    
    	for (i = 1; i < argc; ++i) {
    
    		int rc = env_print(argv[i], env_flag);
    
    		if (!rc) {
    			printf("## Error: \"%s\" not defined\n", argv[i]);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		}
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	return rcode;
    }
    
    
    #ifdef CONFIG_CMD_GREPENV
    
    static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
    		       int argc, char * const argv[])
    
    	int len, grep_how, grep_what;
    
    
    	if (argc < 2)
    
    	grep_how  = H_MATCH_SUBSTR;	/* default: substring search	*/
    	grep_what = H_MATCH_BOTH;	/* default: grep names and values */
    
    	while (--argc > 0 && **++argv == '-') {
    		char *arg = *argv;
    
    		while (*++arg) {
    			switch (*arg) {
    
    #ifdef CONFIG_REGEX
    			case 'e':		/* use regex matching */
    				grep_how  = H_MATCH_REGEX;
    				break;
    #endif
    
    			case 'n':		/* grep for name */
    
    				grep_what = H_MATCH_KEY;
    
    				break;
    			case 'v':		/* grep for value */
    
    				grep_what = H_MATCH_DATA;
    
    				break;
    			case 'b':		/* grep for both */
    
    				grep_what = H_MATCH_BOTH;
    
    				break;
    			case '-':
    				goto DONE;
    			default:
    				return CMD_RET_USAGE;
    			}
    		}
    	}
    
    DONE:
    
    	len = hexport_r(&env_htab, '\n',
    
    			flag | grep_what | grep_how,
    
    			&res, 0, argc, argv);
    
    	if (len > 0) {
    		puts(res);
    		free(res);
    
    #endif /* CONFIG_SPL_BUILD */
    
    /*
     * Set a new environment variable,
     * or replace or delete an existing one.
    
    static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)
    
    {
    	int   i, len;
    	char  *name, *value, *s;
    	ENTRY e, *ep;
    
    
    	debug("Initial value for argc=%d\n", argc);
    	while (argc > 1 && **(argv + 1) == '-') {
    		char *arg = *++argv;
    
    		--argc;
    		while (*++arg) {
    			switch (*arg) {
    			case 'f':		/* force */
    				env_flag |= H_FORCE;
    				break;
    			default:
    				return CMD_RET_USAGE;
    			}
    		}
    	}
    	debug("Final value for argc=%d\n", argc);
    
    	name = argv[1];
    	value = argv[2];
    
    	if (strchr(name, '=')) {
    		printf("## Error: illegal character '='"
    		       "in variable name \"%s\"\n", name);
    		return 1;
    	}
    
    	env_id++;
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	/* Delete only ? */
    
    	if (argc < 3 || argv[2] == NULL) {
    
    		int rc = hdelete_r(name, &env_htab, env_flag);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	 */
    
    	for (i = 2, len = 0; i < argc; ++i)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		len += strlen(argv[i]) + 1;
    
    
    	value = malloc(len);
    	if (value == NULL) {
    
    		printf("## Can't malloc %d bytes\n", len);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		return 1;
    	}
    
    	for (i = 2, s = value; i < argc; ++i) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			;
    
    	e.key	= name;
    	e.data	= value;
    
    	hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
    
    	free(value);
    	if (!ep) {
    		printf("## Error inserting \"%s\" variable, errno=%d\n",
    			name, errno);
    		return 1;
    
    int setenv(const char *varname, const char *varvalue)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	const char * const argv[4] = { "setenv", varname, varvalue, NULL };
    
    
    	/* before import into hashtable */
    	if (!(gd->flags & GD_FLG_ENV_READY))
    		return 1;
    
    
    	if (varvalue == NULL || varvalue[0] == '\0')
    
    		return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC);
    
    		return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    
    /**
     * Set an environment variable to an integer value
     *
    
     * @param varname	Environment variable to set
    
     * @param value		Value to set it to
     * @return 0 if ok, 1 on error
     */
    int setenv_ulong(const char *varname, ulong value)
    {
    	/* TODO: this should be unsigned */
    	char *str = simple_itoa(value);
    
    	return setenv(varname, str);
    }
    
    /**
    
     * Set an environment variable to an value in hex
    
     * @param varname	Environment variable to set
    
     * @param value		Value to set it to
    
     * @return 0 if ok, 1 on error
     */
    
    int setenv_hex(const char *varname, ulong value)
    
    	sprintf(str, "%lx", value);
    
    	return setenv(varname, str);
    }
    
    
    ulong getenv_hex(const char *varname, ulong default_val)
    {
    	const char *s;
    	ulong value;
    	char *endp;
    
    	s = getenv(varname);
    	if (s)
    		value = simple_strtoul(s, &endp, 16);
    	if (!s || endp == s)
    		return default_val;
    
    	return value;
    }
    
    
    #ifndef CONFIG_SPL_BUILD
    
    static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	return _do_env_set(flag, argc, argv, H_INTERACTIVE);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     * Prompt for environment variable
     */
    
    #if defined(CONFIG_CMD_ASKENV)
    
    int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	char message[CONFIG_SYS_CBSIZE];
    
    	int i, len, pos, size;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	char *local_args[4];
    
    	char *endptr;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    	local_args[0] = argv[0];
    	local_args[1] = argv[1];
    	local_args[2] = NULL;
    	local_args[3] = NULL;
    
    
    	/*
    	 * Check the syntax:
    	 *
    	 * env_ask envname [message1 ...] [size]
    	 */
    	if (argc == 1)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	/*
    	 * We test the last argument if it can be converted
    	 * into a decimal number.  If yes, we assume it's
    	 * the size.  Otherwise we echo it as part of the
    	 * message.
    	 */
    	i = simple_strtoul(argv[argc - 1], &endptr, 10);
    	if (*endptr != '\0') {			/* no size */
    		size = CONFIG_SYS_CBSIZE - 1;
    	} else {				/* size given */
    		size = i;
    		--argc;
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	if (argc <= 2) {
    		sprintf(message, "Please enter '%s': ", argv[1]);
    	} else {
    		/* env_ask envname message1 ... messagen [size] */
    		for (i = 2, pos = 0; i < argc; i++) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    				message[pos++] = ' ';
    
    			strcpy(message + pos, argv[i]);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			pos += strlen(argv[i]);
    		}
    
    		message[pos++] = ' ';
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		message[pos] = '\0';
    	}
    
    
    	if (size >= CONFIG_SYS_CBSIZE)
    		size = CONFIG_SYS_CBSIZE - 1;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    	if (size <= 0)
    		return 1;
    
    	/* prompt for input */
    
    	len = cli_readline(message);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    	if (size < len)
    		console_buffer[size] = '\0';
    
    	len = 2;
    	if (console_buffer[0] != '\0') {
    		local_args[2] = console_buffer;
    		len = 3;
    	}
    
    	/* Continue calling setenv code */
    
    	return _do_env_set(flag, len, local_args, H_INTERACTIVE);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #if defined(CONFIG_CMD_ENV_CALLBACK)
    
    static int print_static_binding(const char *var_name, const char *callback_name,
    				void *priv)
    
    {
    	printf("\t%-20s %-20s\n", var_name, callback_name);
    
    	return 0;
    }
    
    static int print_active_callback(ENTRY *entry)
    {
    	struct env_clbk_tbl *clbkp;
    	int i;
    	int num_callbacks;
    
    	if (entry->callback == NULL)
    		return 0;
    
    	/* look up the callback in the linker-list */
    	num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
    	for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
    	     i < num_callbacks;
    	     i++, clbkp++) {
    #if defined(CONFIG_NEEDS_MANUAL_RELOC)
    		if (entry->callback == clbkp->callback + gd->reloc_off)
    #else
    		if (entry->callback == clbkp->callback)
    #endif
    			break;
    	}
    
    	if (i == num_callbacks)
    		/* this should probably never happen, but just in case... */
    		printf("\t%-20s %p\n", entry->key, entry->callback);
    	else
    		printf("\t%-20s %-20s\n", entry->key, clbkp->name);
    
    	return 0;
    }
    
    /*
     * Print the callbacks available and what they are bound to
     */
    int do_env_callback(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
    {
    	struct env_clbk_tbl *clbkp;
    	int i;
    	int num_callbacks;
    
    	/* Print the available callbacks */
    	puts("Available callbacks:\n");
    	puts("\tCallback Name\n");
    	puts("\t-------------\n");
    	num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
    	for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
    	     i < num_callbacks;
    	     i++, clbkp++)
    		printf("\t%s\n", clbkp->name);
    	puts("\n");
    
    	/* Print the static bindings that may exist */
    	puts("Static callback bindings:\n");
    	printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
    	printf("\t%-20s %-20s\n", "-------------", "-------------");
    
    	env_attr_walk(ENV_CALLBACK_LIST_STATIC, print_static_binding, NULL);
    
    	puts("\n");
    
    	/* walk through each variable and print the callback if it has one */
    	puts("Active callback bindings:\n");
    	printf("\t%-20s %-20s\n", "Variable Name", "Callback Name");
    	printf("\t%-20s %-20s\n", "-------------", "-------------");
    	hwalk_r(&env_htab, print_active_callback);
    	return 0;
    }
    #endif
    
    
    #if defined(CONFIG_CMD_ENV_FLAGS)
    
    static int print_static_flags(const char *var_name, const char *flags,
    			      void *priv)
    
    {
    	enum env_flags_vartype type = env_flags_parse_vartype(flags);
    
    	enum env_flags_varaccess access = env_flags_parse_varaccess(flags);
    
    	printf("\t%-20s %-20s %-20s\n", var_name,
    		env_flags_get_vartype_name(type),
    		env_flags_get_varaccess_name(access));
    
    
    	return 0;
    }
    
    static int print_active_flags(ENTRY *entry)
    {
    	enum env_flags_vartype type;
    
    	enum env_flags_varaccess access;
    
    
    	if (entry->flags == 0)
    		return 0;
    
    	type = (enum env_flags_vartype)
    		(entry->flags & ENV_FLAGS_VARTYPE_BIN_MASK);
    
    	access = env_flags_parse_varaccess_from_binflags(entry->flags);
    	printf("\t%-20s %-20s %-20s\n", entry->key,
    		env_flags_get_vartype_name(type),
    		env_flags_get_varaccess_name(access));
    
    
    	return 0;
    }
    
    /*
     * Print the flags available and what variables have flags
     */
    int do_env_flags(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
    {
    	/* Print the available variable types */
    	printf("Available variable type flags (position %d):\n",
    		ENV_FLAGS_VARTYPE_LOC);
    	puts("\tFlag\tVariable Type Name\n");
    	puts("\t----\t------------------\n");
    	env_flags_print_vartypes();
    	puts("\n");
    
    
    	/* Print the available variable access types */
    	printf("Available variable access flags (position %d):\n",
    		ENV_FLAGS_VARACCESS_LOC);
    	puts("\tFlag\tVariable Access Name\n");
    	puts("\t----\t--------------------\n");
    	env_flags_print_varaccess();
    	puts("\n");
    
    
    	/* Print the static flags that may exist */
    	puts("Static flags:\n");
    
    	printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
    		"Variable Access");
    	printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
    		"---------------");
    
    	env_attr_walk(ENV_FLAGS_LIST_STATIC, print_static_flags, NULL);
    
    	puts("\n");
    
    	/* walk through each variable and print the flags if non-default */
    	puts("Active flags:\n");
    
    	printf("\t%-20s %-20s %-20s\n", "Variable Name", "Variable Type",
    		"Variable Access");
    	printf("\t%-20s %-20s %-20s\n", "-------------", "-------------",
    		"---------------");
    
    	hwalk_r(&env_htab, print_active_flags);
    	return 0;
    }
    #endif
    
    
    Peter Tyser's avatar
    Peter Tyser committed
     * Interactively edit an environment variable
     */
    #if defined(CONFIG_CMD_EDITENV)
    
    static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
    		       char * const argv[])
    
    Peter Tyser's avatar
    Peter Tyser committed
    {
    	char buffer[CONFIG_SYS_CBSIZE];
    	char *init_val;
    
    
    Peter Tyser's avatar
    Peter Tyser committed
    
    
    	/* before import into hashtable */
    	if (!(gd->flags & GD_FLG_ENV_READY))
    		return 1;
    
    
    Peter Tyser's avatar
    Peter Tyser committed
    	/* Set read buffer to initial value or empty sting */
    	init_val = getenv(argv[1]);
    	if (init_val)
    
    		snprintf(buffer, CONFIG_SYS_CBSIZE, "%s", init_val);
    
    Peter Tyser's avatar
    Peter Tyser committed
    	else
    		buffer[0] = '\0';
    
    
    	if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
    
    Peter Tyser's avatar
    Peter Tyser committed
    
    
    	if (buffer[0] == '\0') {
    		const char * const _argv[3] = { "setenv", argv[1], NULL };
    
    		return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE);
    	} else {
    		const char * const _argv[4] = { "setenv", argv[1], buffer,
    			NULL };
    
    		return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE);
    	}
    
    Peter Tyser's avatar
    Peter Tyser committed
    }
    #endif /* CONFIG_CMD_EDITENV */
    
    #endif /* CONFIG_SPL_BUILD */
    
    Peter Tyser's avatar
    Peter Tyser committed
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     * Look up variable from environment,
     * return address of storage for that variable,
     * or NULL if not found
     */
    
    char *getenv(const char *name)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    		WATCHDOG_RESET();
    
    		e.key	= name;
    		e.data	= NULL;
    
    		hsearch_r(e, FIND, &ep, &env_htab, 0);
    
    		return ep ? ep->data : NULL;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	}
    
    
    	/* restricted capabilities before import */
    	if (getenv_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0)
    		return (char *)(gd->env_buf);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    
    /*
     * Look up variable from environment for restricted C runtime env.
     */
    
    int getenv_f(const char *name, char *buf, unsigned len)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    	int i, nxt;
    
    
    	for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		int val, n;
    
    
    		for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) {
    			if (nxt >= CONFIG_ENV_SIZE)
    				return -1;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		}
    
    
    		val = envmatch((uchar *)name, i);
    		if (val < 0)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    			continue;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    		/* found; copy out */
    
    		for (n = 0; n < len; ++n, ++buf) {
    
    			*buf = env_get_char(val++);
    			if (*buf == '\0')
    
    		printf("env_buf [%d bytes] too small for value of \"%s\"\n",
    			len, name);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	}
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    
    /**
     * Decode the integer value of an environment variable and return it.
     *
     * @param name		Name of environemnt variable
     * @param base		Number base to use (normally 10, or 16 for hex)
     * @param default_val	Default value to return if the variable is not
     *			found
     * @return the decoded value, or default_val if not found
     */
    ulong getenv_ulong(const char *name, int base, ulong default_val)
    {
    	/*
    	 * We can use getenv() here, even before relocation, since the
    	 * environment variable value is an integer and thus short.
    	 */
    	const char *str = getenv(name);
    
    	return str ? simple_strtoul(str, NULL, base) : default_val;
    }
    
    
    #ifndef CONFIG_SPL_BUILD
    
    #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
    
    static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
    		       char * const argv[])
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	printf("Saving Environment to %s...\n", env_name_spec);
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	return saveenv() ? 1 : 0;
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    U_BOOT_CMD(
    
    Peter Tyser's avatar
    Peter Tyser committed
    	"save environment variables to persistent storage",
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    #endif
    
    #endif /* CONFIG_SPL_BUILD */
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     * Match a name / name=value pair
     *
     * s1 is either a simple 'name', or a 'name=value' pair.
     * i2 is the environment index for a 'name2=value2' pair.
    
     * If the names match, return the index for the value2, else -1.
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
     */
    
    int envmatch(uchar *s1, int i2)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    {
    
    	if (s1 == NULL)
    		return -1;
    
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	while (*s1 == env_get_char(i2++))
    		if (*s1++ == '=')
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    	if (*s1 == '\0' && env_get_char(i2-1) == '=')
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    }
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    #ifndef CONFIG_SPL_BUILD
    
    static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
    
    			  int argc, char * const argv[])
    
    	int all = 0, flag = 0;
    
    	debug("Initial value for argc=%d\n", argc);
    	while (--argc > 0 && **++argv == '-') {
    		char *arg = *argv;
    
    		while (*++arg) {
    			switch (*arg) {
    			case 'a':		/* default all */
    				all = 1;
    				break;
    			case 'f':		/* force */
    				flag |= H_FORCE;
    				break;
    			default:
    				return cmd_usage(cmdtp);
    			}
    		}
    	}
    	debug("Final value for argc=%d\n", argc);
    	if (all && (argc == 0)) {
    		/* Reset the whole environment */
    		set_default_env("## Resetting to default environment\n");
    		return 0;
    	}
    	if (!all && (argc > 0)) {
    		/* Reset individual variables */
    		set_default_vars(argc, argv);
    		return 0;
    	}
    
    	return cmd_usage(cmdtp);
    
    static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
    			 int argc, char * const argv[])
    
    	int env_flag = H_INTERACTIVE;
    	int ret = 0;
    
    	debug("Initial value for argc=%d\n", argc);
    	while (argc > 1 && **(argv + 1) == '-') {
    		char *arg = *++argv;
    
    		--argc;
    		while (*++arg) {
    			switch (*arg) {
    			case 'f':		/* force */
    				env_flag |= H_FORCE;
    				break;
    			default:
    				return CMD_RET_USAGE;
    			}
    		}
    	}
    	debug("Final value for argc=%d\n", argc);
    
    	env_id++;
    
    	while (--argc > 0) {
    		char *name = *++argv;
    
    		if (!hdelete_r(name, &env_htab, env_flag))
    			ret = 1;
    	}
    
    	return ret;
    
    #ifdef CONFIG_CMD_EXPORTENV
    
     * env export [-t | -b | -c] [-s size] addr [var ...]
    
     *	-t:	export as text format; if size is given, data will be
     *		padded with '\0' bytes; if not, one terminating '\0'
     *		will be added (which is included in the "filesize"
     *		setting so you can for exmple copy this to flash and
     *		keep the termination).
     *	-b:	export as binary format (name=value pairs separated by
     *		'\0', list end marked by double "\0\0")
     *	-c:	export as checksum protected environment format as
     *		used for example by "saveenv" command
    
     *	-s size:
     *		size of output buffer
    
     *	addr:	memory address where environment gets stored
    
     *	var...	List of variable names that get included into the
     *		export. Without arguments, the whole environment gets
     *		exported.
    
     *
     * With "-c" and size is NOT given, then the export command will
     * format the data as currently used for the persistent storage,
     * i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and
     * prepend a valid CRC32 checksum and, in case of resundant
     * environment, a "current" redundancy flag. If size is given, this
     * value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32
     * checksum and redundancy flag will be inserted.
     *
     * With "-b" and "-t", always only the real data (including a
     * terminating '\0' byte) will be written; here the optional size
     * argument will be used to make sure not to overflow the user
     * provided buffer; the command will abort if the size is not
     * sufficient. Any remainign space will be '\0' padded.
     *
     * On successful return, the variable "filesize" will be set.
     * Note that filesize includes the trailing/terminating '\0' byte(s).
     *
     * Usage szenario:  create a text snapshot/backup of the current settings:
     *
     *	=> env export -t 100000
     *	=> era ${backup_addr} +${filesize}
     *	=> cp.b 100000 ${backup_addr} ${filesize}
     *
     * Re-import this snapshot, deleting all other settings:
     *
     *	=> env import -d -t ${backup_addr}
     */
    
    static int do_env_export(cmd_tbl_t *cmdtp, int flag,
    			 int argc, char * const argv[])
    
    	ulong	addr;
    	char	*ptr, *cmd, *res;
    
    	size_t	size = 0;
    
    	ssize_t	len;
    	env_t	*envp;
    	char	sep = '\n';
    	int	chk = 0;
    	int	fmt = 0;
    
    	cmd = *argv;
    
    	while (--argc > 0 && **++argv == '-') {
    		char *arg = *argv;
    		while (*++arg) {
    			switch (*arg) {
    			case 'b':		/* raw binary format */
    				if (fmt++)
    					goto sep_err;
    				sep = '\0';
    				break;
    			case 'c':		/* external checksum format */
    				if (fmt++)
    					goto sep_err;
    				sep = '\0';
    				chk = 1;
    				break;
    
    			case 's':		/* size given */
    				if (--argc <= 0)
    					return cmd_usage(cmdtp);
    				size = simple_strtoul(*++argv, NULL, 16);
    				goto NXTARG;
    
    			case 't':		/* text format */
    				if (fmt++)
    					goto sep_err;
    				sep = '\n';
    				break;
    			default:
    
    	if (argc < 1)
    
    Wolfgang Denk's avatar
    Wolfgang Denk committed
    
    
    	addr = simple_strtoul(argv[0], NULL, 16);
    	ptr = map_sysmem(addr, size);
    
    		memset(ptr, '\0', size);
    
    
    	if (sep) {		/* export as text file */
    
    		len = hexport_r(&env_htab, sep,
    				H_MATCH_KEY | H_MATCH_IDENT,
    
    				&ptr, size, argc, argv);
    
    			error("Cannot export environment: errno = %d\n", errno);
    
    		sprintf(buf, "%zX", (size_t)len);
    
    	envp = (env_t *)ptr;
    
    
    	if (chk)		/* export as checksum protected block */
    		res = (char *)envp->data;
    	else			/* export as raw binary data */
    
    	len = hexport_r(&env_htab, '\0',
    			H_MATCH_KEY | H_MATCH_IDENT,
    			&res, ENV_SIZE, argc, argv);
    
    		error("Cannot export environment: errno = %d\n", errno);
    
    		envp->crc = crc32(0, envp->data, ENV_SIZE);
    
    #ifdef CONFIG_ENV_ADDR_REDUND
    		envp->flags = ACTIVE_FLAG;
    #endif
    	}
    
    	setenv_hex("filesize", len + offsetof(env_t, data));
    
    	printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",	cmd);
    
    #ifdef CONFIG_CMD_IMPORTENV
    
     * env import [-d] [-t [-r] | -b | -c] addr [size]
    
     *	-d:	delete existing environment before importing;
     *		otherwise overwrite / append to existion definitions
     *	-t:	assume text format; either "size" must be given or the
     *		text data must be '\0' terminated
    
     *	-r:	handle CRLF like LF, that means exported variables with
     *		a content which ends with \r won't get imported. Used
     *		to import text files created with editors which are using CRLF
     *		for line endings. Only effective in addition to -t.
    
     *	-b:	assume binary format ('\0' separated, "\0\0" terminated)
     *	-c:	assume checksum protected environment format
     *	addr:	memory address to read from
     *	size:	length of input data; if missing, proper '\0'
     *		termination is mandatory
     */
    
    static int do_env_import(cmd_tbl_t *cmdtp, int flag,
    			 int argc, char * const argv[])
    
    	ulong	addr;
    	char	*cmd, *ptr;
    
    	char	sep = '\n';
    	int	chk = 0;
    	int	fmt = 0;
    	int	del = 0;
    
    	size_t	size;
    
    	cmd = *argv;
    
    	while (--argc > 0 && **++argv == '-') {
    		char *arg = *argv;
    		while (*++arg) {