From patchwork Sat Jan 5 01:51:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [U-Boot, RFC, 19/44] Add getenv_hex() to return an environment variable as hex Date: Fri, 04 Jan 2013 15:51:48 -0000 From: Simon Glass X-Patchwork-Id: 209621 Message-Id: <1357350734-13737-20-git-send-email-sjg@chromium.org> To: U-Boot Mailing List Cc: Joel A Fernandes , Tom Rini , Vadim Bendebury , =?UTF-8?q?Andreas=20B=C3=A4ck?= 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 --- common/cmd_nvedit.c | 15 +++++++++++++++ include/common.h | 13 +++++++++++++ 2 files changed, 28 insertions(+), 0 deletions(-) diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c index d646d90..d6d5eea 100644 --- a/common/cmd_nvedit.c +++ b/common/cmd_nvedit.c @@ -309,6 +309,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[]) { diff --git a/include/common.h b/include/common.h index 8ecaf56..cc6bf5e 100644 --- a/include/common.h +++ b/include/common.h @@ -341,6 +341,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)