Skip to content
Snippets Groups Projects
Commit 4a9b4131 authored by Simon Glass's avatar Simon Glass Committed by Wolfgang Denk
Browse files

Add getenv_ulong() to read an integer from an environment variable


This is not an uncommon operation in U-Boot, so let's put it in a common
function.

Signed-off-by: default avatarSimon Glass <sjg@chromium.org>
Acked-by: default avatarMike Frysinger <vapier@gentoo.org>
parent 3668d8fa
No related branches found
No related tags found
No related merge requests found
...@@ -540,6 +540,26 @@ int getenv_f(const char *name, char *buf, unsigned len) ...@@ -540,6 +540,26 @@ int getenv_f(const char *name, char *buf, unsigned len)
return -1; 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) #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[]) int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
......
...@@ -297,6 +297,7 @@ void env_relocate (void); ...@@ -297,6 +297,7 @@ void env_relocate (void);
int envmatch (uchar *, int); int envmatch (uchar *, int);
char *getenv (const char *); char *getenv (const char *);
int getenv_f (const char *name, char *buf, unsigned len); int getenv_f (const char *name, char *buf, unsigned len);
ulong getenv_ulong(const char *name, int base, ulong default_val);
int saveenv (void); int saveenv (void);
#ifdef CONFIG_PPC /* ARM version to be fixed! */ #ifdef CONFIG_PPC /* ARM version to be fixed! */
int inline setenv (const char *, const char *); int inline setenv (const char *, const char *);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment