]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_nvedit.c
Update from 2013.01 to 2013.07
[karo-tx-uboot.git] / common / cmd_nvedit.c
index 7633f0c44a7e3ecb4c8828fbb62af4ede1d9fa21..778dca5af9fbb6c026514332f22ec7f3377479ea 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (C) Copyright 2000-2010
+ * (C) Copyright 2000-2013
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
@@ -7,23 +7,7 @@
  *
  * Copyright 2011 Freescale Semiconductor, Inc.
  *
- * 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
+ * SPDX-License-Identifier:    GPL-2.0+ 
  */
 
 /*
@@ -62,9 +46,10 @@ DECLARE_GLOBAL_DATA_PTR;
        !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} or CONFIG_ENV_IS_NOWHERE
+SPI_FLASH|NVRAM|MMC|FAT|REMOTE|UBI} or CONFIG_ENV_IS_NOWHERE
 #endif
 
 /*
@@ -95,7 +80,7 @@ int get_env_id(void)
 static int env_print(char *name, int flag)
 {
        char *res = NULL;
-       size_t len;
+       ssize_t len;
 
        if (name) {             /* print a single name */
                ENTRY e, *ep;
@@ -119,6 +104,7 @@ static int env_print(char *name, int flag)
        }
 
        /* should never happen */
+       printf("## Error: cannot export environment\n");
        return 0;
 }
 
@@ -162,31 +148,57 @@ static int do_env_print(cmd_tbl_t *cmdtp, int flag, int argc,
 static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
                       int argc, char * const argv[])
 {
-       ENTRY *match;
-       unsigned char matched[env_htab.size / 8];
-       int rcode = 1, arg = 1, idx;
+       char *res = NULL;
+       int len, grep_how, grep_what;
 
        if (argc < 2)
                return CMD_RET_USAGE;
 
-       memset(matched, 0, env_htab.size / 8);
+       grep_how  = H_MATCH_SUBSTR;     /* default: substring search    */
+       grep_what = H_MATCH_BOTH;       /* default: grep names and values */
+
+       while (argc > 1 && **(argv + 1) == '-') {
+               char *arg = *++argv;
 
-       while (arg <= argc) {
-               idx = 0;
-               while ((idx = hstrstr_r(argv[arg], idx, &match, &env_htab))) {
-                       if (!(matched[idx / 8] & (1 << (idx & 7)))) {
-                               puts(match->key);
-                               puts("=");
-                               puts(match->data);
-                               puts("\n");
+               --argc;
+               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;
                        }
-                       matched[idx / 8] |= 1 << (idx & 7);
-                       rcode = 0;
                }
-               arg++;
        }
 
-       return rcode;
+DONE:
+       len = hexport_r(&env_htab, '\n',
+                       flag | grep_what | grep_how,
+                       &res, 0, argc, argv);
+
+       if (len > 0) {
+               puts(res);
+               free(res);
+       }
+
+       if (len < 2)
+               return 1;
+
+       return 0;
 }
 #endif
 #endif /* CONFIG_SPL_BUILD */
@@ -273,6 +285,10 @@ int setenv(const char *varname, const char *varvalue)
 {
        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);
        else
@@ -282,7 +298,7 @@ int setenv(const char *varname, const char *varvalue)
 /**
  * Set an environment variable to an integer value
  *
- * @param varname      Environmet variable to set
+ * @param varname      Environment variable to set
  * @param value                Value to set it to
  * @return 0 if ok, 1 on error
  */
@@ -295,20 +311,35 @@ int setenv_ulong(const char *varname, ulong value)
 }
 
 /**
- * Set an environment variable to an address in hex
+ * Set an environment variable to an value in hex
  *
- * @param varname      Environmet variable to set
- * @param addr         Value to set it to
+ * @param varname      Environment variable to set
+ * @param value                Value to set it to
  * @return 0 if ok, 1 on error
  */
-int setenv_addr(const char *varname, const void *addr)
+int setenv_hex(const char *varname, ulong value)
 {
        char str[17];
 
-       sprintf(str, "%lx", (uintptr_t)addr);
+       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[])
 {
@@ -325,41 +356,50 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        char message[CONFIG_SYS_CBSIZE];
-       int size = CONFIG_SYS_CBSIZE - 1;
-       int i, len, pos;
+       int i, len, pos, size;
        char *local_args[4];
+       char *endptr;
 
        local_args[0] = argv[0];
        local_args[1] = argv[1];
        local_args[2] = NULL;
        local_args[3] = NULL;
 
-       /* Check the syntax */
-       switch (argc) {
-       case 1:
+       /*
+        * Check the syntax:
+        *
+        * env_ask envname [message1 ...] [size]
+        */
+       if (argc == 1)
                return CMD_RET_USAGE;
 
-       case 2:         /* env_ask envname */
-               sprintf(message, "Please enter '%s':", argv[1]);
-               break;
-
-       case 3:         /* env_ask envname size */
-               sprintf(message, "Please enter '%s':", argv[1]);
-               size = simple_strtoul(argv[2], NULL, 10);
-               break;
+       /*
+        * 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;
+       }
 
-       default:        /* env_ask envname message1 ... messagen size */
-               for (i = 2, pos = 0; i < argc - 1; i++) {
+       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++) {
                        if (pos)
                                message[pos++] = ' ';
 
                        strcpy(message + pos, argv[i]);
                        pos += strlen(argv[i]);
                }
-
+               message[pos++] = ' ';
                message[pos] = '\0';
-               size = simple_strtoul(argv[argc - 1], NULL, 10);
-               break;
        }
 
        if (size >= CONFIG_SYS_CBSIZE)
@@ -552,7 +592,8 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
        else
                buffer[0] = '\0';
 
-       readline_into_buffer("edit: ", buffer, 0);
+       if (readline_into_buffer("edit: ", buffer, 0) < 0)
+               return 1;
 
        return setenv(argv[1], buffer);
 }
@@ -861,7 +902,9 @@ NXTARG:             ;
        argv++;
 
        if (sep) {              /* export as text file */
-               len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
+               len = hexport_r(&env_htab, sep,
+                               H_MATCH_KEY | H_MATCH_IDENT,
+                               &addr, size, argc, argv);
                if (len < 0) {
                        error("Cannot export environment: errno = %d\n", errno);
                        return 1;
@@ -879,7 +922,9 @@ NXTARG:             ;
        else                    /* export as raw binary data */
                res = addr;
 
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
+       len = hexport_r(&env_htab, '\0',
+                       H_MATCH_KEY | H_MATCH_IDENT,
+                       &res, ENV_SIZE, argc, argv);
        if (len < 0) {
                error("Cannot export environment: errno = %d\n", errno);
                return 1;
@@ -891,8 +936,7 @@ NXTARG:             ;
                envp->flags = ACTIVE_FLAG;
 #endif
        }
-       sprintf(buf, "%zX", (size_t)(len + offsetof(env_t, data)));
-       setenv("filesize", buf);
+       setenv_hex("filesize", len + offsetof(env_t, data));
 
        return 0;
 
@@ -1099,7 +1143,11 @@ static char env_help_text[] =
        "env flags - print variables that have non-default flags\n"
 #endif
 #if defined(CONFIG_CMD_GREPENV)
-       "env grep string [...] - search environment\n"
+#ifdef CONFIG_REGEX
+       "env grep [-e] [-n | -v | -b] string [...] - search environment\n"
+#else
+       "env grep [-n | -v | -b] string [...] - search environment\n"
+#endif
 #endif
 #if defined(CONFIG_CMD_IMPORTENV)
        "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
@@ -1146,8 +1194,17 @@ U_BOOT_CMD_COMPLETE(
 U_BOOT_CMD_COMPLETE(
        grepenv, CONFIG_SYS_MAXARGS, 0,  do_env_grep,
        "search environment variables",
-       "string ...\n"
-       "    - list environment name=value pairs matching 'string'",
+#ifdef CONFIG_REGEX
+       "[-e] [-n | -v | -b] string ...\n"
+#else
+       "[-n | -v | -b] string ...\n"
+#endif
+       "    - list environment name=value pairs matching 'string'\n"
+#ifdef CONFIG_REGEX
+       "      \"-e\": enable regular expressions;\n"
+#endif
+       "      \"-n\": search variable names; \"-v\": search values;\n"
+       "      \"-b\": search both names and values (default)",
        var_complete
 );
 #endif
@@ -1168,14 +1225,7 @@ U_BOOT_CMD(
        askenv, CONFIG_SYS_MAXARGS,     1,      do_env_ask,
        "get environment variables from stdin",
        "name [message] [size]\n"
-       "    - get environment variable 'name' from stdin (max 'size' chars)\n"
-       "askenv name\n"
-       "    - get environment variable 'name' from stdin\n"
-       "askenv name size\n"
-       "    - get environment variable 'name' from stdin (max 'size' chars)\n"
-       "askenv name [message] size\n"
-       "    - display 'message' string and get environment variable 'name'"
-       "from stdin (max 'size' chars)"
+       "    - get environment variable 'name' from stdin (max 'size' chars)"
 );
 #endif