]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
Add getenv_hex() to return an environment variable as hex
authorSimon Glass <sjg@chromium.org>
Sat, 20 Apr 2013 08:42:43 +0000 (08:42 +0000)
committerTom Rini <trini@ti.com>
Wed, 1 May 2013 15:17:21 +0000 (11:17 -0400)
This conversion is required in a number of places in U-Boot. Add a
standard function to provide this feature, so we avoid all the different
variations in the way it is coded.

Signed-off-by: Simon Glass <sjg@chromium.org>
common/cmd_nvedit.c
include/common.h

index 68b0f4f6d809b36e8e8b91a87ad07e4c21473c09..d893aa1854624899cef4eb3e274cc7d78c08253a 100644 (file)
@@ -315,6 +315,21 @@ int setenv_hex(const char *varname, ulong 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[])
 {
index 76c79ae58e4eb9512a33aa413fc65d698f03ca74..28aa4b9d6033b92229c2cdbf14f35ea71337714d 100644 (file)
@@ -352,6 +352,19 @@ int        envmatch     (uchar *, int);
 char   *getenv      (const char *);
 int    getenv_f     (const char *name, char *buf, unsigned len);
 ulong getenv_ulong(const char *name, int base, ulong default_val);
+
+/**
+ * getenv_hex() - Return an environment variable as a hex value
+ *
+ * Decode an environment as a hex number (it may or may not have a 0x
+ * prefix). If the environment variable cannot be found, or does not start
+ * with hex digits, the default value is returned.
+ *
+ * @varname:           Variable to decode
+ * @default_val:       Value to return on error
+ */
+ulong getenv_hex(const char *varname, ulong default_val);
+
 /*
  * Read an environment variable as a boolean
  * Return -1 if variable does not exist (default to true)