]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
Add getenv_ulong() to read an integer from an environment variable
authorSimon Glass <sjg@chromium.org>
Fri, 14 Oct 2011 13:25:18 +0000 (13:25 +0000)
committerWolfgang Denk <wd@denx.de>
Sun, 23 Oct 2011 21:32:45 +0000 (23:32 +0200)
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>
Acked-by: Mike Frysinger <vapier@gentoo.org>
common/cmd_nvedit.c
include/common.h

index 101bc4906c0cf8df6bba51e94e7b9fd4f30b07a8..fa99c44b010628137d0693a2f95cff602503a5a8 100644 (file)
@@ -540,6 +540,26 @@ int getenv_f(const char *name, char *buf, unsigned len)
        return -1;
 }
 
+/**
+ * Decode the 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
+ */
+ulong getenv_ulong(const char *name, int base, ulong 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_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[])
index db1c7d0f51356f6d388560adbfdff47db2ce5d02..a1683a20aa76ab5f5a853f3f84c1ad4f3f0dc1cf 100644 (file)
@@ -297,6 +297,7 @@ void        env_relocate (void);
 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);
 int    saveenv      (void);
 #ifdef CONFIG_PPC              /* ARM version to be fixed! */
 int inline setenv    (const char *, const char *);