]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_nvedit.c
env: Add a console env handler
[karo-tx-uboot.git] / common / cmd_nvedit.c
index d655ab9684eef9389c798cbc199a451a484e7189..cb191cd0637c14332b609b8e8519d38afec388e1 100644 (file)
 #include <errno.h>
 #include <malloc.h>
 #include <watchdog.h>
-#include <serial.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
-#if defined(CONFIG_CMD_NET)
-#include <net.h>
-#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -76,16 +72,6 @@ SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
  */
 #define        MAX_ENV_SIZE    (1 << 20)       /* 1 MiB */
 
-ulong load_addr = CONFIG_SYS_LOAD_ADDR;        /* Default Load Address */
-ulong save_addr;                       /* Default Save Address */
-ulong save_size;                       /* Default Save Size (in bytes) */
-
-/*
- * Table with supported baudrates (defined in config_xyz.h)
- */
-static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
-#define        N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
-
 /*
  * This variable is incremented on each do_env_set(), so it can
  * be used via get_env_id() as an indication, if the environment
@@ -106,7 +92,7 @@ int get_env_id(void)
  *
  * Returns 0 in case of error, or length of printed string
  */
-static int env_print(char *name)
+static int env_print(char *name, int flag)
 {
        char *res = NULL;
        size_t len;
@@ -116,7 +102,7 @@ static int env_print(char *name)
 
                e.key = name;
                e.data = NULL;
-               hsearch_r(e, FIND, &ep, &env_htab);
+               hsearch_r(e, FIND, &ep, &env_htab, flag);
                if (ep == NULL)
                        return 0;
                len = printf("%s=%s\n", ep->key, ep->data);
@@ -124,7 +110,7 @@ static int env_print(char *name)
        }
 
        /* print whole list */
-       len = hexport_r(&env_htab, '\n', &res, 0, 0, NULL);
+       len = hexport_r(&env_htab, '\n', flag, &res, 0, 0, NULL);
 
        if (len > 0) {
                puts(res);
@@ -136,14 +122,22 @@ static int env_print(char *name)
        return 0;
 }
 
-int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+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;
+       }
 
        if (argc == 1) {
                /* print all env vars */
-               rcode = env_print(NULL);
+               rcode = env_print(NULL, env_flag);
                if (!rcode)
                        return 1;
                printf("\nEnvironment size: %d/%ld bytes\n",
@@ -152,8 +146,9 @@ int do_env_print (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        }
 
        /* print selected env vars */
+       env_flag &= ~H_HIDE_DOT;
        for (i = 1; i < argc; ++i) {
-               int rc = env_print(argv[i]);
+               int rc = env_print(argv[i], env_flag);
                if (!rc) {
                        printf("## Error: \"%s\" not defined\n", argv[i]);
                        ++rcode;
@@ -207,49 +202,27 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
  * overwriting of write-once variables.
  */
 
-int env_check_apply(const char *name, const char *oldval,
-                       const char *newval, int flag)
+int env_change_ok(const ENTRY *item, const char *newval, enum env_op op,
+       int flag)
 {
-       int   console = -1;
-
-       /* Default value for NULL to protect string-manipulating functions */
-       newval = newval ? : "";
-
-       /* Check for console redirection */
-       if (strcmp(name, "stdin") == 0)
-               console = stdin;
-       else if (strcmp(name, "stdout") == 0)
-               console = stdout;
-       else if (strcmp(name, "stderr") == 0)
-               console = stderr;
-
-       if (console != -1) {
-               if ((newval == NULL) || (*newval == '\0')) {
-                       /* We cannot delete stdin/stdout/stderr */
-                       if ((flag & H_FORCE) == 0)
-                               printf("Can't delete \"%s\"\n", name);
-                       return 1;
-               }
+#ifndef CONFIG_ENV_OVERWRITE
+       const char *name;
+#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
+       const char *oldval = NULL;
 
-#ifdef CONFIG_CONSOLE_MUX
-               if (iomux_doenv(console, newval))
-                       return 1;
-#else
-               /* Try assigning specified device */
-               if (console_assign(console, newval) < 0)
-                       return 1;
+       if (op != env_op_create)
+               oldval = item->data;
+#endif
 
-               if (serial_assign(newval) < 0)
-                       return 1;
-#endif /* CONFIG_CONSOLE_MUX */
-       }
+       name = item->key;
+#endif
 
+#ifndef CONFIG_ENV_OVERWRITE
        /*
         * Some variables like "ethaddr" and "serial#" can be set only once and
         * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
         */
-#ifndef CONFIG_ENV_OVERWRITE
-       if (oldval != NULL &&                   /* variable exists */
+       if (op != env_op_create &&              /* variable exists */
                (flag & H_FORCE) == 0) {        /* and we are not forced */
                if (strcmp(name, "serial#") == 0 ||
                    (strcmp(name, "ethaddr") == 0
@@ -262,62 +235,7 @@ int env_check_apply(const char *name, const char *oldval,
                }
        }
 #endif
-       /*
-        * When we change baudrate, or we are doing an env default -a
-        * (which will erase all variables prior to calling this),
-        * we want the baudrate to actually change - for real.
-        */
-       if (oldval != NULL ||                   /* variable exists */
-               (flag & H_NOCLEAR) == 0) {      /* or env is clear */
-               /*
-                * Switch to new baudrate if new baudrate is supported
-                */
-               if (strcmp(name, "baudrate") == 0) {
-                       int baudrate = simple_strtoul(newval, NULL, 10);
-                       int i;
-                       for (i = 0; i < N_BAUDRATES; ++i) {
-                               if (baudrate == baudrate_table[i])
-                                       break;
-                       }
-                       if (i == N_BAUDRATES) {
-                               if ((flag & H_FORCE) == 0)
-                                       printf("## Baudrate %d bps not "
-                                               "supported\n", baudrate);
-                               return 1;
-                       }
-                       if (gd->baudrate == baudrate) {
-                               /* If unchanged, we just say it's OK */
-                               return 0;
-                       }
-                       printf("## Switch baudrate to %d bps and"
-                               "press ENTER ...\n", baudrate);
-                       udelay(50000);
-                       gd->baudrate = baudrate;
-#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
-                       gd->bd->bi_baudrate = baudrate;
-#endif
 
-                       serial_setbrg();
-                       udelay(50000);
-                       while (getc() != '\r')
-                               ;
-               }
-       }
-
-       /*
-        * Some variables should be updated when the corresponding
-        * entry in the environment is changed
-        */
-       if (strcmp(name, "loadaddr") == 0) {
-               load_addr = simple_strtoul(newval, NULL, 16);
-               return 0;
-       }
-#if defined(CONFIG_CMD_NET)
-       else if (strcmp(name, "bootfile") == 0) {
-               copy_filename(BootFile, newval, sizeof(BootFile));
-               return 0;
-       }
-#endif
        return 0;
 }
 
@@ -325,7 +243,7 @@ int env_check_apply(const char *name, const char *oldval,
  * Set a new environment variable,
  * or replace or delete an existing one.
 */
-int _do_env_set(int flag, int argc, char * const argv[])
+static int _do_env_set(int flag, int argc, char * const argv[])
 {
        int   i, len;
        char  *name, *value, *s;
@@ -341,25 +259,10 @@ int _do_env_set(int flag, int argc, char * const argv[])
        }
 
        env_id++;
-       /*
-        * search if variable with this name already exists
-        */
-       e.key = name;
-       e.data = NULL;
-       hsearch_r(e, FIND, &ep, &env_htab);
-
-       /*
-        * Perform requested checks. Notice how since we are overwriting
-        * a single variable, we need to set H_NOCLEAR
-        */
-       if (env_check_apply(name, ep ? ep->data : NULL, value, H_NOCLEAR)) {
-               debug("check function did not approve, refusing\n");
-               return 1;
-       }
 
        /* Delete only ? */
        if (argc < 3 || argv[2] == NULL) {
-               int rc = hdelete_r(name, &env_htab, 0);
+               int rc = hdelete_r(name, &env_htab, H_INTERACTIVE);
                return !rc;
        }
 
@@ -386,7 +289,7 @@ int _do_env_set(int flag, int argc, char * const argv[])
 
        e.key   = name;
        e.data  = value;
-       hsearch_r(e, ENTER, &ep, &env_htab);
+       hsearch_r(e, ENTER, &ep, &env_htab, H_INTERACTIVE);
        free(value);
        if (!ep) {
                printf("## Error inserting \"%s\" variable, errno=%d\n",
@@ -438,7 +341,7 @@ int setenv_addr(const char *varname, const void *addr)
 }
 
 #ifndef CONFIG_SPL_BUILD
-int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        if (argc < 2)
                return CMD_RET_USAGE;
@@ -513,11 +416,87 @@ int do_env_ask(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 }
 #endif
 
+#if defined(CONFIG_CMD_ENV_CALLBACK)
+static int print_static_binding(const char *var_name, const char *callback_name)
+{
+       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);
+       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
+
 /*
  * Interactively edit an environment variable
  */
 #if defined(CONFIG_CMD_EDITENV)
-int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,
+                      char * const argv[])
 {
        char buffer[CONFIG_SYS_CBSIZE];
        char *init_val;
@@ -553,7 +532,7 @@ char *getenv(const char *name)
 
                e.key   = name;
                e.data  = NULL;
-               hsearch_r(e, FIND, &ep, &env_htab);
+               hsearch_r(e, FIND, &ep, &env_htab, 0);
 
                return ep ? ep->data : NULL;
        }
@@ -625,7 +604,8 @@ ulong getenv_ulong(const char *name, int base, ulong default_val)
 
 #ifndef CONFIG_SPL_BUILD
 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
-int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc,
+                      char * const argv[])
 {
        printf("Saving Environment to %s...\n", env_name_spec);
 
@@ -650,6 +630,9 @@ U_BOOT_CMD(
  */
 int envmatch(uchar *s1, int i2)
 {
+       if (s1 == NULL)
+               return -1;
+
        while (*s1 == env_get_char(i2++))
                if (*s1++ == '=')
                        return i2;
@@ -809,7 +792,7 @@ NXTARG:             ;
        argv++;
 
        if (sep) {              /* export as text file */
-               len = hexport_r(&env_htab, sep, &addr, size, argc, argv);
+               len = hexport_r(&env_htab, sep, 0, &addr, size, argc, argv);
                if (len < 0) {
                        error("Cannot export environment: errno = %d\n", errno);
                        return 1;
@@ -827,7 +810,7 @@ NXTARG:             ;
        else                    /* export as raw binary data */
                res = addr;
 
-       len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, argc, argv);
+       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, argc, argv);
        if (len < 0) {
                error("Cannot export environment: errno = %d\n", errno);
                return 1;
@@ -948,7 +931,7 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,
        }
 
        if (himport_r(&env_htab, addr, size, sep, del ? 0 : H_NOCLEAR,
-                       0, NULL, 0 /* do_apply */) == 0) {
+                       0, NULL) == 0) {
                error("Environment import failed: errno = %d\n", errno);
                return 1;
        }
@@ -975,6 +958,9 @@ static cmd_tbl_t cmd_env_sub[] = {
 #if defined(CONFIG_CMD_EDITENV)
        U_BOOT_CMD_MKENT(edit, 2, 0, do_env_edit, "", ""),
 #endif
+#if defined(CONFIG_CMD_ENV_CALLBACK)
+       U_BOOT_CMD_MKENT(callbacks, 1, 0, do_env_callback, "", ""),
+#endif
 #if defined(CONFIG_CMD_EXPORTENV)
        U_BOOT_CMD_MKENT(export, 4, 0, do_env_export, "", ""),
 #endif
@@ -1020,11 +1006,13 @@ static int do_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        return CMD_RET_USAGE;
 }
 
-U_BOOT_CMD(
-       env, CONFIG_SYS_MAXARGS, 1, do_env,
-       "environment handling commands",
+#ifdef CONFIG_SYS_LONGHELP
+static char env_help_text[] =
 #if defined(CONFIG_CMD_ASKENV)
        "ask name [message] [size] - ask for environment variable\nenv "
+#endif
+#if defined(CONFIG_CMD_ENV_CALLBACK)
+       "callbacks - print callbacks and their associated variables\nenv "
 #endif
        "default [-f] -a - [forcibly] reset default environment\n"
        "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
@@ -1040,14 +1028,19 @@ U_BOOT_CMD(
 #if defined(CONFIG_CMD_IMPORTENV)
        "env import [-d] [-t | -b | -c] addr [size] - import environment\n"
 #endif
-       "env print [name ...] - print environment\n"
+       "env print [-a | name ...] - print environment\n"
 #if defined(CONFIG_CMD_RUN)
        "env run var [...] - run commands in an environment variable\n"
 #endif
 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
        "env save - save environment\n"
 #endif
-       "env set [-f] name [arg ...]\n"
+       "env set [-f] name [arg ...]\n";
+#endif
+
+U_BOOT_CMD(
+       env, CONFIG_SYS_MAXARGS, 1, do_env,
+       "environment handling commands", env_help_text
 );
 
 /*
@@ -1067,7 +1060,7 @@ U_BOOT_CMD_COMPLETE(
 U_BOOT_CMD_COMPLETE(
        printenv, CONFIG_SYS_MAXARGS, 1,        do_env_print,
        "print environment variables",
-       "\n    - print values of all environment variables\n"
+       "[-a]\n    - print [all] values of all environment variables\n"
        "printenv name ...\n"
        "    - print value of environment variable 'name'",
        var_complete