Merge branch 'master' of git://git.denx.de/u-boot-tegra
[oweals/u-boot.git] / env / common.c
index 3317cef3552264cae2870df6ad1cedaa0096fb75..bd340fe9d52d7bdfb62d68c83ceca6954ccf27ae 100644 (file)
@@ -23,7 +23,10 @@ DECLARE_GLOBAL_DATA_PTR;
 #include <env_default.h>
 
 struct hsearch_data env_htab = {
+#if CONFIG_IS_ENABLED(ENV_SUPPORT)
+       /* defined in flags.c, only compile with ENV_SUPPORT */
        .change_ok = env_flags_validate,
+#endif
 };
 
 /*
@@ -115,7 +118,7 @@ int env_import(const char *buf, int check)
 
                if (crc32(0, ep->data, ENV_SIZE) != crc) {
                        set_default_env("bad CRC", 0);
-                       return -EIO;
+                       return -ENOMSG; /* needed for env_load() */
                }
        }
 
@@ -169,7 +172,7 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 
        if (!crc1_ok && !crc2_ok) {
                set_default_env("bad CRC", 0);
-               return -EIO;
+               return -ENOMSG; /* needed for env_load() */
        } else if (crc1_ok && !crc2_ok) {
                gd->env_valid = ENV_VALID;
        } else if (!crc1_ok && crc2_ok) {
@@ -225,7 +228,9 @@ void env_relocate(void)
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
        env_reloc();
        env_fix_drivers();
-       env_htab.change_ok += gd->reloc_off;
+
+       if (env_htab.change_ok)
+               env_htab.change_ok += gd->reloc_off;
 #endif
        if (gd->env_valid == ENV_INVALID) {
 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
@@ -240,32 +245,76 @@ void env_relocate(void)
        }
 }
 
-#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
-int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
+#ifdef CONFIG_AUTO_COMPLETE
+int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
+                bool dollar_comp)
 {
        ENTRY *match;
        int found, idx;
 
+       if (dollar_comp) {
+               /*
+                * When doing $ completion, the first character should
+                * obviously be a '$'.
+                */
+               if (var[0] != '$')
+                       return 0;
+
+               var++;
+
+               /*
+                * The second one, if present, should be a '{', as some
+                * configuration of the u-boot shell expand ${var} but not
+                * $var.
+                */
+               if (var[0] == '{')
+                       var++;
+               else if (var[0] != '\0')
+                       return 0;
+       }
+
        idx = 0;
        found = 0;
        cmdv[0] = NULL;
 
+
        while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
                int vallen = strlen(match->key) + 1;
 
-               if (found >= maxv - 2 || bufsz < vallen)
+               if (found >= maxv - 2 ||
+                   bufsz < vallen + (dollar_comp ? 3 : 0))
                        break;
 
                cmdv[found++] = buf;
+
+               /* Add the '${' prefix to each var when doing $ completion. */
+               if (dollar_comp) {
+                       strcpy(buf, "${");
+                       buf += 2;
+                       bufsz -= 3;
+               }
+
                memcpy(buf, match->key, vallen);
                buf += vallen;
                bufsz -= vallen;
+
+               if (dollar_comp) {
+                       /*
+                        * This one is a bit odd: vallen already contains the
+                        * '\0' character but we need to add the '}' suffix,
+                        * hence the buf - 1 here. strcpy() will add the '\0'
+                        * character just after '}'. buf is then incremented
+                        * to account for the extra '}' we just added.
+                        */
+                       strcpy(buf - 1, "}");
+                       buf++;
+               }
        }
 
        qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
 
        if (idx)
-               cmdv[found++] = "...";
+               cmdv[found++] = dollar_comp ? "${...}" : "...";
 
        cmdv[found] = NULL;
        return found;