[U-Boot] [PATCH 1/2] cmd: Add getenv_long to support reading signed integers from the uboot env

Vince Bridgers
2015-02-09T14:44:32Z

This patch adds a function to read a signed integer from the uboot environment
for the Micrel ksz9031 Phy driver to read postive and negatice skew values
from the uboot environment.

Signed-off-by: Vince Bridgers <vbridger@opensource.altera.com>
---
common/cmd_nvedit.c | 22 +++++++++++++++++++++-
include/common.h | 1 +
2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 855808c..ec23696 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -665,7 +665,27 @@ int getenv_f(const char *name, char *buf, unsigned len)
}

/**
- * Decode the integer value of an environment variable and return it.
+ * Decode the signed integer value of an environment variable and return it.
+ *
+ * @param name Name of environemnt variable
+ * @param base Number base to use (normally 10, or 16 for hex)
+ * @param default_val Default value to return if the variable is not
+ * found
+ * @return the decoded value, or default_val if not found
+ */
+long getenv_long(const char *name, int base, long default_val)
+{
+ /*
+ * We can use getenv() here, even before relocation, since the
+ * environment variable value is an integer and thus short.
+ */
+ const char *str = getenv(name);
+
+ return str ? simple_strtol(str, NULL, base) : default_val;
+}
+
+/**
+ * Decode the unsigned integer value of an environment variable and return it.
*
* @param name Name of environemnt variable
* @param base Number base to use (normally 10, or 16 for hex)
diff --git a/include/common.h b/include/common.h
index 97c8f79..55a8cb3 100644
--- a/include/common.h
+++ b/include/common.h
@@ -304,6 +304,7 @@ 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);
+long getenv_long(const char *name, int base, long default_val);

/**
* getenv_hex() - Return an environment variable as a hex value
--
1.9.1