From patchwork Fri Oct 14 21:04:44 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 119918 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 77AD7B6FD1 for ; Sat, 15 Oct 2011 08:05:09 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 2A3E928732; Fri, 14 Oct 2011 23:05:07 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3LkVNKxRra5N; Fri, 14 Oct 2011 23:05:06 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5656328690; Fri, 14 Oct 2011 23:05:05 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BD78228691 for ; Fri, 14 Oct 2011 23:05:02 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 3aXbXo0RSREY for ; Fri, 14 Oct 2011 23:05:01 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from smtp-out.google.com (smtp-out.google.com [74.125.121.67]) by theia.denx.de (Postfix) with ESMTPS id 871F228690 for ; Fri, 14 Oct 2011 23:05:00 +0200 (CEST) Received: from hpaq7.eem.corp.google.com (hpaq7.eem.corp.google.com [172.25.149.7]) by smtp-out.google.com with ESMTP id p9EL4uEl009731; Fri, 14 Oct 2011 14:04:56 -0700 Received: from sglass.mtv.corp.google.com (sglass.mtv.corp.google.com [172.22.72.144]) by hpaq7.eem.corp.google.com with ESMTP id p9EL4pTH024870; Fri, 14 Oct 2011 14:04:51 -0700 Received: by sglass.mtv.corp.google.com (Postfix, from userid 121222) id E4858140843; Fri, 14 Oct 2011 14:04:50 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Date: Fri, 14 Oct 2011 14:04:44 -0700 Message-Id: <1318626284-11161-1-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.7.3.1 In-Reply-To: <1318610916-6975-2-git-send-email-sjg@chromium.org> References: <1318610916-6975-2-git-send-email-sjg@chromium.org> X-System-Of-Record: true Subject: [U-Boot] [PATCH v3 01/10] Add getenv_ulong() to read an integer from an environment variable X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This is not an uncommon operation in U-Boot, so let's put it in a common function. Signed-off-by: Simon Glass --- Changes in v2: - Fix commit title from getenv_int() to getenv_ulong() Changes in v3: - Move getenv_ulong() function comment into C file - Add special code for early environment access common/cmd_nvedit.c | 25 +++++++++++++++++++++++++ include/common.h | 1 + 2 files changed, 26 insertions(+), 0 deletions(-) diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c index 101bc49..1456b99 100644 --- a/common/cmd_nvedit.c +++ b/common/cmd_nvedit.c @@ -540,6 +540,31 @@ int getenv_f(const char *name, char *buf, unsigned len) return -1; } +/** + * 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) +{ + char buff[20]; + const char *str = NULL; + + /* + * Prior to the import of the environment into the hashtable we + * should not call getenv() + */ + if (gd->flags & GD_FLG_ENV_READY) + str = getenv(name); + else if (getenv_f(name, buff, sizeof(buff)) > 0) + str = buff; + 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..27f5e98 100644 --- a/include/common.h +++ b/include/common.h @@ -288,6 +288,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 *);