Patchwork [U-Boot,v2,01/10] Add getenv_ulong() to read an integer from an environment variable

login
register
mail settings
Submitter Simon Glass
Date Oct. 14, 2011, 4:48 p.m.
Message ID <1318610916-6975-2-git-send-email-sjg@chromium.org>
Download mbox | patch
Permalink /patch/119856/
State New, archived
Headers show

Comments

Simon Glass - Oct. 14, 2011, 4:48 p.m.
This is not an uncommon operation in U-Boot, so let's put it in a common
function.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
Changes in v2:
- Fix commit title from getenv_int() to getenv_ulong()

 common/cmd_nvedit.c |    7 +++++++
 include/common.h    |   12 ++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
Mike Frysinger - Oct. 14, 2011, 5:09 p.m.
Acked-by: Mike Frysinger <vapier@gentoo.org>
-mike
Wolfgang Denk - Oct. 14, 2011, 7:36 p.m.
Dear Simon Glass,

In message <1318610916-6975-2-git-send-email-sjg@chromium.org> you wrote:
> This is not an uncommon operation in U-Boot, so let's put it in a common
> function.

Please make sure to use this function ONLY in places where getenv()
use is OK.  Do NOT use it where getenv_f() is needed instead.

Best regards,

Wolfgang Denk

Patch

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 101bc49..337ae9a 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -540,6 +540,13 @@  int getenv_f(const char *name, char *buf, unsigned len)
 	return -1;
 }
 
+ulong getenv_ulong(const char *name, int base, ulong default_val)
+{
+	const char *str = getenv(name);
+
+	return str ? simple_strtoul(str, NULL, base) : default_val;
+}
+
 #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[])
diff --git a/include/common.h b/include/common.h
index eb19a44..c2796e2 100644
--- a/include/common.h
+++ b/include/common.h
@@ -288,6 +288,18 @@  void	env_relocate (void);
 int	envmatch     (uchar *, int);
 char	*getenv	     (const char *);
 int	getenv_f     (const char *name, char *buf, unsigned len);
+
+/**
+ * Decode the 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
+ */
+ulong getenv_ulong(const char *name, int base, ulong default_val);
+
 int	saveenv	     (void);
 #ifdef CONFIG_PPC		/* ARM version to be fixed! */
 int inline setenv    (const char *, const char *);