]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_nvedit.c
karo: tx28: update Kconfig
[karo-tx-uboot.git] / common / cmd_nvedit.c
index 2478c95b73604b9628b0df1678734dd70b776920..855808c3e4a4d58bc3437da32725a3b725face01 100644 (file)
@@ -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+
  */
 
 /*
@@ -41,6 +25,7 @@
  */
 
 #include <common.h>
+#include <cli.h>
 #include <command.h>
 #include <environment.h>
 #include <search.h>
@@ -49,6 +34,7 @@
 #include <watchdog.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
+#include <asm/io.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -173,10 +159,8 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
        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;
-
-               --argc;
+       while (--argc > 0 && **++argv == '-') {
+               char *arg = *argv;
                while (*++arg) {
                        switch (*arg) {
 #ifdef CONFIG_REGEX
@@ -425,7 +409,7 @@ int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                return 1;
 
        /* prompt for input */
-       len = readline(message);
+       len = cli_readline(message);
 
        if (size < len)
                console_buffer[size] = '\0';
@@ -608,7 +592,7 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
        else
                buffer[0] = '\0';
 
-       if (readline_into_buffer("edit: ", buffer, 0) < 0)
+       if (cli_readline_into_buffer("edit: ", buffer, 0) < 0)
                return 1;
 
        return setenv(argv[1], buffer);
@@ -864,7 +848,8 @@ static int do_env_export(cmd_tbl_t *cmdtp, int flag,
                         int argc, char * const argv[])
 {
        char    buf[32];
-       char    *addr, *cmd, *res;
+       ulong   addr;
+       char    *ptr, *cmd, *res;
        size_t  size = 0;
        ssize_t len;
        env_t   *envp;
@@ -909,10 +894,11 @@ NXTARG:           ;
        if (argc < 1)
                return CMD_RET_USAGE;
 
-       addr = (char *)simple_strtoul(argv[0], NULL, 16);
+       addr = simple_strtoul(argv[0], NULL, 16);
+       ptr = map_sysmem(addr, size);
 
        if (size)
-               memset(addr, '\0', size);
+               memset(ptr, '\0', size);
 
        argc--;
        argv++;
@@ -920,7 +906,7 @@ NXTARG:             ;
        if (sep) {              /* export as text file */
                len = hexport_r(&env_htab, sep,
                                H_MATCH_KEY | H_MATCH_IDENT,
-                               &addr, size, argc, argv);
+                               &ptr, size, argc, argv);
                if (len < 0) {
                        error("Cannot export environment: errno = %d\n", errno);
                        return 1;
@@ -931,12 +917,12 @@ NXTARG:           ;
                return 0;
        }
 
-       envp = (env_t *)addr;
+       envp = (env_t *)ptr;
 
        if (chk)                /* export as checksum protected block */
                res = (char *)envp->data;
        else                    /* export as raw binary data */
-               res = addr;
+               res = ptr;
 
        len = hexport_r(&env_htab, '\0',
                        H_MATCH_KEY | H_MATCH_IDENT,
@@ -964,11 +950,15 @@ sep_err:
 
 #ifdef CONFIG_CMD_IMPORTENV
 /*
- * env import [-d] [-t | -b | -c] addr [size]
+ * 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
@@ -978,11 +968,13 @@ sep_err:
 static int do_env_import(cmd_tbl_t *cmdtp, int flag,
                         int argc, char * const argv[])
 {
-       char    *cmd, *addr;
+       ulong   addr;
+       char    *cmd, *ptr;
        char    sep = '\n';
        int     chk = 0;
        int     fmt = 0;
        int     del = 0;
+       int     crlf_is_lf = 0;
        size_t  size;
 
        cmd = *argv;
@@ -1007,6 +999,9 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
                                        goto sep_err;
                                sep = '\n';
                                break;
+                       case 'r':               /* handle CRLF like LF */
+                               crlf_is_lf = 1;
+                               break;
                        case 'd':
                                del = 1;
                                break;
@@ -1022,12 +1017,19 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
        if (!fmt)
                printf("## Warning: defaulting to text format\n");
 
-       addr = (char *)simple_strtoul(argv[0], NULL, 16);
+       if (sep != '\n' && crlf_is_lf )
+               crlf_is_lf = 0;
+
+       addr = simple_strtoul(argv[0], NULL, 16);
+       ptr = map_sysmem(addr, 0);
 
        if (argc == 2) {
                size = simple_strtoul(argv[1], NULL, 16);
+       } else if (argc == 1 && chk) {
+               puts("## Error: external checksum format must pass size\n");
+               return CMD_RET_FAILURE;
        } else {
-               char *s = addr;
+               char *s = ptr;
 
                size = 0;
 
@@ -1047,7 +1049,7 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
 
        if (chk) {
                uint32_t crc;
-               env_t *ep = (env_t *)addr;
+               env_t *ep = (env_t *)ptr;
 
                size -= offsetof(env_t, data);
                memcpy(&crc, &ep->crc, sizeof(crc));
@@ -1056,11 +1058,11 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
                        puts("## Error: bad CRC, import failed\n");
                        return 1;
                }
-               addr = (char *)ep->data;
+               ptr = (char *)ep->data;
        }
 
-       if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
-                       0, NULL) == 0) {
+       if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR,
+                       crlf_is_lf, 0, NULL) == 0) {
                error("Environment import failed: errno = %d\n", errno);
                return 1;
        }
@@ -1075,6 +1077,23 @@ sep_err:
 }
 #endif
 
+#if defined(CONFIG_CMD_ENV_EXISTS)
+static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
+                      char * const argv[])
+{
+       ENTRY e, *ep;
+
+       if (argc < 2)
+               return CMD_RET_USAGE;
+
+       e.key = argv[1];
+       e.data = NULL;
+       hsearch_r(e, FIND, &ep, &env_htab, 0);
+
+       return (ep == NULL) ? 1 : 0;
+}
+#endif
+
 /*
  * New command line interface: "env" command with subcommands
  */
@@ -1110,6 +1129,9 @@ static cmd_tbl_t cmd_env_sub[] = {
        U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
 #endif
        U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
+#if defined(CONFIG_CMD_ENV_EXISTS)
+       U_BOOT_CMD_MKENT(exists, 2, 0, do_env_exists, "", ""),
+#endif
 };
 
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
@@ -1152,6 +1174,9 @@ static char env_help_text[] =
 #if defined(CONFIG_CMD_EDITENV)
        "env edit name - edit environment variable\n"
 #endif
+#if defined(CONFIG_CMD_ENV_EXISTS)
+       "env exists name - tests for existence of variable\n"
+#endif
 #if defined(CONFIG_CMD_EXPORTENV)
        "env export [-t | -b | -c] [-s size] addr [var ...] - export environment\n"
 #endif
@@ -1166,7 +1191,7 @@ static char env_help_text[] =
 #endif
 #endif
 #if defined(CONFIG_CMD_IMPORTENV)
-       "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
+       "env import [-d] [-t [-r] | -b | -c] addr [size] - import environment\n"
 #endif
        "env print [-a | name ...] - print environment\n"
 #if defined(CONFIG_CMD_RUN)