From patchwork Sun Oct 17 15:36:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542240 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=k3WldKpG; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPJP2mD8z9sP7 for ; Mon, 18 Oct 2021 02:37:07 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 398B883173; Sun, 17 Oct 2021 17:36:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="k3WldKpG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A3AEF832EA; Sun, 17 Oct 2021 17:36:48 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4E87683173 for ; Sun, 17 Oct 2021 17:36:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 2D5CD60E74; Sun, 17 Oct 2021 15:36:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485004; bh=WpId9gemHVAF1/H6ZG5zX/SzkZfGGSjJYu0xhavqxyY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k3WldKpGRhBrZA4mGKX9v3A5P83C4uV/OveQ3gnI4UskEeoCu19hs7/2kY/TvShjv 2JPHJpHqOsGPLn8lYt92YUf7uJf3pLWdeuZdaL7uHnLqYNDRYWSlTWqamgOO7SZ/Fo 2fvI8bhzehfw/5LWHvZb5xNG/8LNjDTpGdTXmTT9sdPk1KiscThebqJ9+Zv7HlQYhy HIllYDTVVp70XtnE2rVTXNAed9JCfFT5IOeHNoe3OU9grXfwU2lzyzWMnbD5fFNq+u 7iYhBVqXrS9f/4CbSz9E/CMKSNlQ+p3Yxq/Ja4Ak8zTrkfvh5PLtzrIS3S00/BtI+u ZSV3PXc0lLbMw== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 01/13] env: Fix documentation for env_get_f() Date: Sun, 17 Oct 2021 17:36:26 +0200 Message-Id: <20211017153638.29870-2-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún This function actually returns: - the number of bytes written into @buf excluding the terminating NULL-byte, if there was enough space in @buf - the number of bytes written into @buf including the terminating NULL-byte, if there wasn't enough space in @buf - -1 if the variable is not found Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- include/env.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/env.h b/include/env.h index d5e2bcb530..b1a4003681 100644 --- a/include/env.h +++ b/include/env.h @@ -131,7 +131,10 @@ char *from_env(const char *envvar); * support reading the value (slowly) and some will not. * * @varname: Variable to look up - * @return value of variable, or NULL if not found + * @return number of bytes written into @buf, excluding the terminating + * NULL-byte if there was enough space in @buf, and including the + * terminating NULL-byte if there wasn't enough space, or -1 if the + * variable is not found */ int env_get_f(const char *name, char *buf, unsigned int len); From patchwork Sun Oct 17 15:36:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542244 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=qkbjVV/w; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPKF3fkTz9sP7 for ; Mon, 18 Oct 2021 02:37:53 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C061F83405; Sun, 17 Oct 2021 17:37:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="qkbjVV/w"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 626FB83457; Sun, 17 Oct 2021 17:36:56 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B903483221 for ; Sun, 17 Oct 2021 17:36:46 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 670A761151; Sun, 17 Oct 2021 15:36:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485005; bh=tythmMizsc977IubW0TTI9ynPnt/R/0yGtQMoeEbluc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qkbjVV/wHWW2fY4gjJ8PbgedXvmmHNfzWh1zt1e7lh9Oni5pi2ab92Lqw+h4h4ZpO 3n3byKXdQh1L2yaezqs6m8AWPOUbIMZ4Trxqk/WTozmAaFou25BtKosBFw2H9JLMPx sNP4/qIJVAsyoc8P+/nQn8UjSnHEOBt57nFZ8qE1hss7Sb6bwfZYFl7bh7U17Y1ASb CjjvEgBa97OWOq5qVKWGwad9yeLI/pWdvR5hCEjj5xIWCMRjI+a2vzBnAcNlSeLQGw cOKokQhPV3+migS6SbEH9b8HVjwCTJ5tmH5/fhPbNPnPh03l/dtGfi8OyB4nlANoZT SU9jl84sgQ9ew== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 02/13] env: Drop env_get_char_spec() and old, unused .get_char() implementations Date: Sun, 17 Oct 2021 17:36:27 +0200 Message-Id: <20211017153638.29870-3-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Commit b2cdef4861be ("env: restore old env_get_char() behaviour") dropped the .get_char() method from struct env_driver, but left the two existing implementations (eeprom and nvram) in case someone would use them by overwriting weak function env_get_char_spec(). Since this was never done in the 3.5 years, let's drop these methods and simplify the code. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- env/eeprom.c | 18 ------------------ env/env.c | 7 +------ env/nvram.c | 14 -------------- 3 files changed, 1 insertion(+), 38 deletions(-) diff --git a/env/eeprom.c b/env/eeprom.c index 253bdf1428..f8556a4721 100644 --- a/env/eeprom.c +++ b/env/eeprom.c @@ -64,24 +64,6 @@ static int eeprom_bus_write(unsigned dev_addr, unsigned offset, return rcode; } -/** Call this function from overridden env_get_char_spec() if you need - * this functionality. - */ -int env_eeprom_get_char(int index) -{ - uchar c; - unsigned int off = CONFIG_ENV_OFFSET; - -#ifdef CONFIG_ENV_OFFSET_REDUND - if (gd->env_valid == ENV_REDUND) - off = CONFIG_ENV_OFFSET_REDUND; -#endif - eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, - off + index + offsetof(env_t, data), &c, 1); - - return c; -} - static int env_eeprom_load(void) { char buf_env[CONFIG_ENV_SIZE]; diff --git a/env/env.c b/env/env.c index e534008006..91d220c3dd 100644 --- a/env/env.c +++ b/env/env.c @@ -166,17 +166,12 @@ static struct env_driver *env_driver_lookup(enum env_operation op, int prio) return drv; } -__weak int env_get_char_spec(int index) -{ - return *(uchar *)(gd->env_addr + index); -} - int env_get_char(int index) { if (gd->env_valid == ENV_INVALID) return default_environment[index]; else - return env_get_char_spec(index); + return *(uchar *)(gd->env_addr + index); } int env_load(void) diff --git a/env/nvram.c b/env/nvram.c index f4126858b5..261b31edfb 100644 --- a/env/nvram.c +++ b/env/nvram.c @@ -42,20 +42,6 @@ extern void nvram_write(long dest, const void *src, size_t count); static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; #endif -#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE -/** Call this function from overridden env_get_char_spec() if you need - * this functionality. - */ -int env_nvram_get_char(int index) -{ - uchar c; - - nvram_read(&c, CONFIG_ENV_ADDR + index, 1); - - return c; -} -#endif - static int env_nvram_load(void) { char buf[CONFIG_ENV_SIZE]; From patchwork Sun Oct 17 15:36:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542242 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=IzP6a3VP; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPJl2YgXz9sP7 for ; Mon, 18 Oct 2021 02:37:27 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1CB06832EA; Sun, 17 Oct 2021 17:37:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="IzP6a3VP"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 70E8683404; Sun, 17 Oct 2021 17:36:57 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D3D5C83303 for ; Sun, 17 Oct 2021 17:36:47 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id A0325610E5; Sun, 17 Oct 2021 15:36:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485006; bh=VKCD/A+IwNOwyDYxUaAaLsGE+qtyOyoN1yIDhZ2I4gw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IzP6a3VP29wvumkdxIIxB7pAukmn6v0OFi1aVo51yB23YjmplOdWABs6T470QNOcx JPuPZwUXn4YGVEH89Zj2xfo7lK7KeU1e72dKc2JjfvN/GPNV4vEShp2Y+LsyfRpwvO cT1B4ixxUb+j2rCNa5Cj1DTyI8Pkil2XOuviN3LQG+/86J9Gf6UtSvo6pJdCH3pvjL bcWU3pTmPdbr+VB5PyELwnoXOuNuidLc57RrqwEuzZo0WGRkvPc9tlkrCVSBVw4kX9 3bQIyVPN+fibc3ms4Dyr+mizXt5pFpfpqaRf1ZExO7Kl5bTrKBMJZk5NW9TC/kdJuW SrtVgywyDoSrw== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 03/13] examples: api: glue: Remove comment that does not apply anymore Date: Sun, 17 Oct 2021 17:36:28 +0200 Message-Id: <20211017153638.29870-4-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún This comment is not true since commit 6215bd4c1fd6 ("api: Use hashtable function for API_env_enum"). Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- examples/api/glue.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/api/glue.c b/examples/api/glue.c index 91d13157a1..075d307ae2 100644 --- a/examples/api/glue.c +++ b/examples/api/glue.c @@ -365,11 +365,6 @@ const char * ub_env_enum(const char *last) env = NULL; - /* - * It's OK to pass only the name piece as last (and not the whole - * 'name=val' string), since the API_ENUM_ENV call uses env_match() - * internally, which handles such case - */ if (!syscall(API_ENV_ENUM, NULL, last, &env)) return NULL; From patchwork Sun Oct 17 15:36:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542243 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=a5TdK9+E; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPK101WDz9sP7 for ; Mon, 18 Oct 2021 02:37:40 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0EC7083303; Sun, 17 Oct 2021 17:37:24 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="a5TdK9+E"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 08F2083405; Sun, 17 Oct 2021 17:36:55 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1806783318 for ; Sun, 17 Oct 2021 17:36:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id D91BB611AF; Sun, 17 Oct 2021 15:36:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485007; bh=gCiht31CBnruh7iDlmZjVWWUSp9yF0Y68RDAPfJUJXU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a5TdK9+ELnR03kCout+RW6/1QmuwgTTEl71rTN+4dY45JLB9QziqaeffWsiBHxlKj ryPhWG/xklBb9TP04ba/EO0ibaRHqKjrTSBKc9looD7WZ/3D5/TebGTJ5h2rh4Y7Dh 3NF+JZqncHgUpVBoZEXBmmownWUKsPfuoLgAEThDyv7fifA9icyv9MZFpkRxxY1uHC udznr8qOHVa8wDFahYRsyKxGe1Gf5aU8plvZjCgODvxNeQaaKPB49haL7YnSga39Mb 703DNjTuXY1FeJzElatpfO/jMOoDpO0gIM4CnGtiQ7OHpz9t0x6CPF1DtxA9sn38js GtkJrHGtChGLw== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 04/13] env: Change env_match() to static and remove from header Date: Sun, 17 Oct 2021 17:36:29 +0200 Message-Id: <20211017153638.29870-5-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún This function was used by other parts of U-Boot in the past when environment was read from underlying device one character at a time. This is not the case anymore. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 30 +++++++++++++++--------------- include/env.h | 11 ----------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index ddc715b4f9..742e0924af 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -706,6 +706,21 @@ char *from_env(const char *envvar) return ret; } +static int env_match(uchar *s1, int i2) +{ + if (s1 == NULL) + return -1; + + while (*s1 == env_get_char(i2++)) + if (*s1++ == '=') + return i2; + + if (*s1 == '\0' && env_get_char(i2-1) == '=') + return i2; + + return -1; +} + /* * Look up variable from environment for restricted C runtime env. */ @@ -816,21 +831,6 @@ static int do_env_select(struct cmd_tbl *cmdtp, int flag, int argc, #endif /* CONFIG_SPL_BUILD */ -int env_match(uchar *s1, int i2) -{ - if (s1 == NULL) - return -1; - - while (*s1 == env_get_char(i2++)) - if (*s1++ == '=') - return i2; - - if (*s1 == '\0' && env_get_char(i2-1) == '=') - return i2; - - return -1; -} - #ifndef CONFIG_SPL_BUILD static int do_env_default(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) diff --git a/include/env.h b/include/env.h index b1a4003681..a9b2a4c8b2 100644 --- a/include/env.h +++ b/include/env.h @@ -90,17 +90,6 @@ int env_init(void); */ void env_relocate(void); -/** - * env_match() - Match a name / name=value pair - * - * This is used prior to relocation for finding envrionment variables - * - * @name: A simple 'name', or a 'name=value' pair. - * @index: The environment index for a 'name2=value2' pair. - * @return index for the value if the names match, else -1. - */ -int env_match(unsigned char *name, int index); - /** * env_get() - Look up the value of an environment variable * From patchwork Sun Oct 17 15:36:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542247 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=kTOZxE1f; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPKy2Sclz9sP7 for ; Mon, 18 Oct 2021 02:38:30 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 036FA834A3; Sun, 17 Oct 2021 17:37:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="kTOZxE1f"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0497A8331C; Sun, 17 Oct 2021 17:37:00 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C8E8F802A1 for ; Sun, 17 Oct 2021 17:36:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 1E2A260E74; Sun, 17 Oct 2021 15:36:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485009; bh=3JM+6NS+mLCGpZzLp2i5x6snviLTsdscdw5IhpUJHHM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kTOZxE1f5ASCo3P/xE3Xbss2IrPx4mMJDJOjVmqfKj/b5XEfk77rQQ6IqR1hbNR0n plDjEQXR3Wq3qTXSeHxZFsYsdyuKAI2+pthxbsAgkN0kj5iDTa/ykTCaidjjr1pxyK 32ra4gD+Dq12QmnMMR4Bm8tTdLOaSMxz+IFoXXHd14WRwYIi5Rp2CbmMQgUG8pJTGY fNrV0meqtlG9+vtzP4emdRYYeBX2c+uwjSqXR/7Qo80wU16fExMCdAhPVjhHpLxb4v 9J4lMroXIXK182pwlIL1Z9Fa1cQ33Lp/Qaf5hCI78srjeyyl2ogYBn54uDiw+jjZG0 sOmK0zDTYlnKQ== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 05/13] env: Inline env_get_char() into its only user Date: Sun, 17 Oct 2021 17:36:30 +0200 Message-Id: <20211017153638.29870-6-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún This function is a relic from the past when environment was read from underlying device one character at a time. It is used only in the case when getting an environemnt variable prior relocation, and the function is simple enough to be inlined there. Since env_get_char() is being changed to simple access to an array, we can drop the failing cases and simplify the code (this could have been done before, since env_get_char() did not fail even before). Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 28 ++++++++++++++-------------- env/env.c | 8 -------- env/nowhere.c | 5 ++--- include/env.h | 10 ---------- 4 files changed, 16 insertions(+), 35 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 742e0924af..3952deda1b 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -706,16 +706,16 @@ char *from_env(const char *envvar) return ret; } -static int env_match(uchar *s1, int i2) +static int env_match(const char *env, const char *s1, int i2) { if (s1 == NULL) return -1; - while (*s1 == env_get_char(i2++)) + while (*s1 == env[i2++]) if (*s1++ == '=') return i2; - if (*s1 == '\0' && env_get_char(i2-1) == '=') + if (*s1 == '\0' && env[i2-1] == '=') return i2; return -1; @@ -726,28 +726,28 @@ static int env_match(uchar *s1, int i2) */ int env_get_f(const char *name, char *buf, unsigned len) { - int i, nxt, c; + const char *env; + int i, nxt; - for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { + if (gd->env_valid == ENV_INVALID) + env = (const char *)default_environment; + else + env = (const char *)gd->env_addr; + + for (i = 0; env[i] != '\0'; i = nxt + 1) { int val, n; - for (nxt = i; (c = env_get_char(nxt)) != '\0'; ++nxt) { - if (c < 0) - return c; + for (nxt = i; env[nxt] != '\0'; ++nxt) if (nxt >= CONFIG_ENV_SIZE) return -1; - } - val = env_match((uchar *)name, i); + val = env_match(env, name, i); if (val < 0) continue; /* found; copy out */ for (n = 0; n < len; ++n, ++buf) { - c = env_get_char(val++); - if (c < 0) - return c; - *buf = c; + *buf = env[val++]; if (*buf == '\0') return n; } diff --git a/env/env.c b/env/env.c index 91d220c3dd..e4dfb92e15 100644 --- a/env/env.c +++ b/env/env.c @@ -166,14 +166,6 @@ static struct env_driver *env_driver_lookup(enum env_operation op, int prio) return drv; } -int env_get_char(int index) -{ - if (gd->env_valid == ENV_INVALID) - return default_environment[index]; - else - return *(uchar *)(gd->env_addr + index); -} - int env_load(void) { struct env_driver *drv; diff --git a/env/nowhere.c b/env/nowhere.c index 41557f5ce4..1fcf503453 100644 --- a/env/nowhere.c +++ b/env/nowhere.c @@ -31,9 +31,8 @@ static int env_nowhere_init(void) static int env_nowhere_load(void) { /* - * for SPL, set env_valid = ENV_INVALID is enough as env_get_char() - * return the default env if env_get is used - * and SPL don't used env_import to reduce its size + * For SPL, setting env_valid = ENV_INVALID is enough, as env_get() + * searches default_environment array in that case. * For U-Boot proper, import the default environment to allow reload. */ if (!IS_ENABLED(CONFIG_SPL_BUILD)) diff --git a/include/env.h b/include/env.h index a9b2a4c8b2..220ab979d9 100644 --- a/include/env.h +++ b/include/env.h @@ -351,16 +351,6 @@ char *env_get_default(const char *name); /* [re]set to the default environment */ void env_set_default(const char *s, int flags); -/** - * env_get_char() - Get a character from the early environment - * - * This reads from the pre-relocation environment - * - * @index: Index of character to read (0 = first) - * @return character read, or -ve on error - */ -int env_get_char(int index); - /** * env_reloc() - Relocate the 'env' sub-commands * From patchwork Sun Oct 17 15:36:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542245 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=qdocIsg9; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPKV4HJ7z9sP7 for ; Mon, 18 Oct 2021 02:38:06 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 651BF833D4; Sun, 17 Oct 2021 17:37:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="qdocIsg9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E14008343C; Sun, 17 Oct 2021 17:36:58 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 793B8831CD for ; Sun, 17 Oct 2021 17:36:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 577BC61151; Sun, 17 Oct 2021 15:36:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485010; bh=M0q/ClvGToohAsN4ObCVblPxoeZGpGFSynega6EycMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qdocIsg9g0acjYGyoll6zsdIxzBpi5+VwN5/FkrALZchatNJFSaPUlC9fCv0u9NKa eLk7gAggXuzf/hzEc011aVRwyYrXT0QXSvMbFaksLjZXPJFimKWCVo4D43hVUGZebG 4EeQaw28DrLwpAamGexliPwyYnRPhAtkM+7fiD4647eHDAM+wxTXGdM8Z8fdnoVwEK CyoxzeEIEEMCM1xVjUKFGlp8RRX8ZJrb3o67Mf6gO9FlFHrXsPmKN9ajXwCl49A5iK BnT85nK26s1UYkwnwTu0FOq+Cv5QlCqg2/R6ZMp8vajuqg5TmPpfoI7jBUY/sNR2Dw N6xZecpJ9w+FA== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 06/13] env: Use string pointer instead of indexes in env_get_f() Date: Sun, 17 Oct 2021 17:36:31 +0200 Message-Id: <20211017153638.29870-7-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Since we no longer use env_get_char() to access n-th character of linearized environment data, but rather access the arrays themselves, we can convert the iteration to use string pointers instead of position indexes. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 3952deda1b..f395d2893a 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -706,19 +706,19 @@ char *from_env(const char *envvar) return ret; } -static int env_match(const char *env, const char *s1, int i2) +static const char *env_match(const char *p, const char *s1) { if (s1 == NULL) - return -1; + return NULL; - while (*s1 == env[i2++]) + while (*s1 == *p++) if (*s1++ == '=') - return i2; + return p; - if (*s1 == '\0' && env[i2-1] == '=') - return i2; + if (*s1 == '\0' && p[-1] == '=') + return p; - return -1; + return NULL; } /* @@ -726,28 +726,28 @@ static int env_match(const char *env, const char *s1, int i2) */ int env_get_f(const char *name, char *buf, unsigned len) { - const char *env; - int i, nxt; + const char *env, *p, *nxt; if (gd->env_valid == ENV_INVALID) env = (const char *)default_environment; else env = (const char *)gd->env_addr; - for (i = 0; env[i] != '\0'; i = nxt + 1) { - int val, n; + for (p = env; *p != '\0'; p = nxt + 1) { + const char *value; + int n; - for (nxt = i; env[nxt] != '\0'; ++nxt) - if (nxt >= CONFIG_ENV_SIZE) + for (nxt = p; *nxt != '\0'; ++nxt) + if (nxt - env >= CONFIG_ENV_SIZE) return -1; - val = env_match(env, name, i); - if (val < 0) + value = env_match(p, name); + if (value == NULL) continue; /* found; copy out */ for (n = 0; n < len; ++n, ++buf) { - *buf = env[val++]; + *buf = *value++; if (*buf == '\0') return n; } From patchwork Sun Oct 17 15:36:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542249 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=fQyWtqaV; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPLN1JY8z9sP7 for ; Mon, 18 Oct 2021 02:38:52 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9D264834F1; Sun, 17 Oct 2021 17:37:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="fQyWtqaV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EA6AF833D8; Sun, 17 Oct 2021 17:37:05 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BA26C83406 for ; Sun, 17 Oct 2021 17:36:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 90BED610E5; Sun, 17 Oct 2021 15:36:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485011; bh=Z9Sg/nb328JfJqnpbzDK/2P6xuzipFBjmR/Zk9Ihq7U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fQyWtqaV6NmiqxrWEsLXP9cQIsCRyWOYQxRd96dvnj4YyURUF5Hnis4Hea8bqPQ92 7f8sA0sCj9pGMCZuguhy2ExNAap7gRHeo4SBq5HtbRpIvoJ3eJJ94Kmwb60Fra7CYn SyvzPAthO91zcjijilU99nG8GQRyeELow/51tJFowK4Moxo4Wmw7qrZgKKeG7N4geG fbzWPClYJcM/s20c+laUIMC24uycEjKrMaKqn1XuvnSvBg8X6Teg0I0gR6AE07AKZu 0rA3QSoGBB3AE/2PzKhCtGuE2wAa+xP4O6rLcKEkJBJEiMgwj1krtD/uJxq0yejNdg Vw6HWJJQeSYiw== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 07/13] env: Use better name for variable in env_get_f() Date: Sun, 17 Oct 2021 17:36:32 +0200 Message-Id: <20211017153638.29870-8-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún The `nxt` variable actually points to the terminating null-byte of the current env var, and the next env var is at `nxt + 1`, not `nxt`. So a better name for this variable is `end`. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index f395d2893a..5b1d4c2448 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -726,19 +726,19 @@ static const char *env_match(const char *p, const char *s1) */ int env_get_f(const char *name, char *buf, unsigned len) { - const char *env, *p, *nxt; + const char *env, *p, *end; if (gd->env_valid == ENV_INVALID) env = (const char *)default_environment; else env = (const char *)gd->env_addr; - for (p = env; *p != '\0'; p = nxt + 1) { + for (p = env; *p != '\0'; p = end + 1) { const char *value; int n; - for (nxt = p; *nxt != '\0'; ++nxt) - if (nxt - env >= CONFIG_ENV_SIZE) + for (end = p; *end != '\0'; ++end) + if (end - env >= CONFIG_ENV_SIZE) return -1; value = env_match(p, name); From patchwork Sun Oct 17 15:36:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542246 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=Th3zgfML; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPKk59PBz9sP7 for ; Mon, 18 Oct 2021 02:38:18 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 62673807D5; Sun, 17 Oct 2021 17:37:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="Th3zgfML"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 48F3881197; Sun, 17 Oct 2021 17:37:06 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 00217832EA for ; Sun, 17 Oct 2021 17:36:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id CA1DB611AF; Sun, 17 Oct 2021 15:36:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485012; bh=kB1itiglQUcIeRCsnaAI/olUkHRfjS0jYemsr4wb/IM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Th3zgfMLIXPbc5VsYpIEAb/AQyRiD/n/xuiTUjXJ7z5oHNvlH5oEEWmXDe0efRyNi 0j11PHZFDnBZ/OJqCOEeScgyCQUhMCmjWB57XC3TRtWgErzps1SrqwgHlNhLna2wQy /Cy0oUg6lgXIWF8IzpKR57QFMmSOHYyBdm7X/KG+fZbXdXOdHDzIiMBH9LLnWEstdN uxQKlo2q0HzWRm4a6pDFt5pyd4r0adI6EAkMWU2UBHxCHMOmIU1A7tA8/BOGZaotaM HMuBdK1f5Ll6yEsv6IEQbYbMUhEkmp/IpU1gLC0v/RV9NuSkFke4x7IwH9i9dsgJZY Yq/UqTxVxZCqw== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 08/13] env: Don't match empty variable name in env_match() Date: Sun, 17 Oct 2021 17:36:33 +0200 Message-Id: <20211017153638.29870-9-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Do we really allow zero-length variable name? I guess not. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 5b1d4c2448..8d53579d92 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -708,7 +708,7 @@ char *from_env(const char *envvar) static const char *env_match(const char *p, const char *s1) { - if (s1 == NULL) + if (s1 == NULL || *s1 == '\0') return NULL; while (*s1 == *p++) From patchwork Sun Oct 17 15:36:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542248 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=ccEoV/BO; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPL944YKz9sP7 for ; Mon, 18 Oct 2021 02:38:41 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4640183318; Sun, 17 Oct 2021 17:37:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="ccEoV/BO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2CF9B8331E; Sun, 17 Oct 2021 17:37:08 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 70A7B8342C for ; Sun, 17 Oct 2021 17:36:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 1863D60E74; Sun, 17 Oct 2021 15:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485013; bh=3KtbTd8SNvV3K6d5VL6qEhQGKaiHM6mYhfoa2W4jaL4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ccEoV/BOyL8WdpmLj+OBGfmFU7mjngE1aDuZGeu8UsKeyn27W8Xb6YgkRubzE+cq8 ferpgNYhxcrXJ0XPT39igzhshQoyYevbS1Rn4OAjhbIdK1TP13Sq75noLsUUjXVbfH GyNXmdAEDsj3h6IgnRVT+ccGmJn7j4YCU/lxL9mVlOH0YO6+Et3v528S1pRl+RWlPD EMX28ONP50ODWERXvJ4JIvP6aNrkQqYlnAjxlOrqEyUoSDHc07a9NxuLoLP5JZfdJt HbTa9zWx/+pTsyJeDTtlpzV84SWhy3ngmpdwMyOT+zi+DmOt7UXY/8UX/P/s1E9gTS EvR6PTf3MnsEQ== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 09/13] env: Early return from env_get_f() on NULL name Date: Sun, 17 Oct 2021 17:36:34 +0200 Message-Id: <20211017153638.29870-10-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Test non-NULL name immediately, not in env_match(). Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 8d53579d92..063cc76282 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -708,9 +708,6 @@ char *from_env(const char *envvar) static const char *env_match(const char *p, const char *s1) { - if (s1 == NULL || *s1 == '\0') - return NULL; - while (*s1 == *p++) if (*s1++ == '=') return p; @@ -728,6 +725,9 @@ int env_get_f(const char *name, char *buf, unsigned len) { const char *env, *p, *end; + if (name == NULL || *name == '\0') + return -1; + if (gd->env_valid == ENV_INVALID) env = (const char *)default_environment; else From patchwork Sun Oct 17 15:36:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542251 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=qeCG85p+; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPLq5vfgz9sP7 for ; Mon, 18 Oct 2021 02:39:15 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 88758834B9; Sun, 17 Oct 2021 17:38:04 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="qeCG85p+"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 15FCC81197; Sun, 17 Oct 2021 17:37:12 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 96AA783221 for ; Sun, 17 Oct 2021 17:36:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 5182660FE3; Sun, 17 Oct 2021 15:36:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485015; bh=JGPBSrFPMYngOvpKfyPIgfzJfd8nI+vYvMvGwiB870M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qeCG85p+KEOejnyGIIS3dyQgUI4WheUHwfjJ8/yP4AyFRK3FnA1Rhr7OSM8fhF1ws nFpggntAxGvN77UuE+Om4A3hDL/hSrkGF9gw00HBgwdVSszmTqpmji1XX/u18yigP0 JYZTOO9wiUj2iq0vIyK8ds1MNWlxqbvRney0U8/KwVE4y31hcBPqXAOxgiwutQj5C9 hNoIygjBCncdS0ERtNUwZiXGPQH27XvKP0IdAhMjtQUiZsKon40q4BYQ+4kYfLjBsq STlRC65ARQMefAmDXoRNjB4CppexbmwsGaWgSYyj/1F0gyKdGkEYjgnJ7U8Wk3LAMM yYOS3MoQm70QQ== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 10/13] env: Make return value of env_get_f() behave like sprintf() on success Date: Sun, 17 Oct 2021 17:36:35 +0200 Message-Id: <20211017153638.29870-11-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Currently the env_get_f() function's return value behaves weirdly: it returns the number of bytes written into `buf`, but whether this is excluding the terminating NULL-byte or including it depends on whether there was enough space in `buf`. Change the function to always return the actual length of the value of the environment variable (excluding the terminating NULL-byte) on success. This makes it behave like sprintf(). All users of this function in U-Boot are compatible with this change. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 8 +++++--- include/env.h | 6 ++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 063cc76282..527b522e9e 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -735,7 +735,7 @@ int env_get_f(const char *name, char *buf, unsigned len) for (p = env; *p != '\0'; p = end + 1) { const char *value; - int n; + int n, res; for (end = p; *end != '\0'; ++end) if (end - env >= CONFIG_ENV_SIZE) @@ -745,11 +745,13 @@ int env_get_f(const char *name, char *buf, unsigned len) if (value == NULL) continue; + res = end - value; + /* found; copy out */ for (n = 0; n < len; ++n, ++buf) { *buf = *value++; if (*buf == '\0') - return n; + return res; } if (n) @@ -758,7 +760,7 @@ int env_get_f(const char *name, char *buf, unsigned len) printf("env_buf [%u bytes] too small for value of \"%s\"\n", len, name); - return n; + return res; } return -1; diff --git a/include/env.h b/include/env.h index 220ab979d9..ee5e30d036 100644 --- a/include/env.h +++ b/include/env.h @@ -120,10 +120,8 @@ char *from_env(const char *envvar); * support reading the value (slowly) and some will not. * * @varname: Variable to look up - * @return number of bytes written into @buf, excluding the terminating - * NULL-byte if there was enough space in @buf, and including the - * terminating NULL-byte if there wasn't enough space, or -1 if the - * variable is not found + * @return actual length of the variable value excluding the terminating + * NULL-byte, or -1 if the variable is not found */ int env_get_f(const char *name, char *buf, unsigned int len); From patchwork Sun Oct 17 15:36:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542250 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=BccnE81O; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPLc2Hf4z9sP7 for ; Mon, 18 Oct 2021 02:39:04 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 96DCE8356E; Sun, 17 Oct 2021 17:38:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="BccnE81O"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4AA03832EA; Sun, 17 Oct 2021 17:37:07 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id AF2C683310 for ; Sun, 17 Oct 2021 17:36:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 8A5D6610E5; Sun, 17 Oct 2021 15:36:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485016; bh=ymxZMJWScq/f8imIjvPfB3rW4eoTBMh7FGjZXSbKS/s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BccnE81O+/6xCy9+bYfjxrnyo0B2wKdBAvOCmcsVeScwvxGmZ/aqAYZDnZCmtOsNm VLOpha49tkkHi+VTn5PHNgsv6/wXupYUg4u3G9CMJv0pNetH2u1gIuWHqcNCVtE0pv vO4CLz7Lg0sxdmHd+ySKkocO2vAHFMyM1Vs0RJFuSqWxBpVTbaHmra9KvP78sBrFX2 ZScz1u+eSYZvja60b+rqjFvQfSZ4SjY0yZHOwpLg8D1eF8FNJ0MpZHzHoj2G5LOOxU yBmTDdmCJZfoZfvSd7dUfo2Rf/Wm0GGPZO4/aTOqXX7Nk9ekBtZUs9FGwxNUUvBmyl 6J3qlnFLm4yjg== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 11/13] env: Use memcpy() instead of ad-hoc code to copy variable value Date: Sun, 17 Oct 2021 17:36:36 +0200 Message-Id: <20211017153638.29870-12-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Copy the value of the found variable into given buffer with memcpy() instead of ad-hoc code. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 527b522e9e..ffcfb55f10 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -735,7 +735,7 @@ int env_get_f(const char *name, char *buf, unsigned len) for (p = env; *p != '\0'; p = end + 1) { const char *value; - int n, res; + unsigned res; for (end = p; *end != '\0'; ++end) if (end - env >= CONFIG_ENV_SIZE) @@ -746,20 +746,14 @@ int env_get_f(const char *name, char *buf, unsigned len) continue; res = end - value; + memcpy(buf, value, min(len, res + 1)); - /* found; copy out */ - for (n = 0; n < len; ++n, ++buf) { - *buf = *value++; - if (*buf == '\0') - return res; + if (len <= res) { + buf[len - 1] = '\0'; + printf("env_buf [%u bytes] too small for value of \"%s\"\n", + len, name); } - if (n) - *--buf = '\0'; - - printf("env_buf [%u bytes] too small for value of \"%s\"\n", - len, name); - return res; } From patchwork Sun Oct 17 15:36:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542252 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=PyxSu2Q5; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPM20tRsz9sP7 for ; Mon, 18 Oct 2021 02:39:26 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A0B718349C; Sun, 17 Oct 2021 17:38:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="PyxSu2Q5"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7ADE983221; Sun, 17 Oct 2021 17:37:13 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id F3C83831CD for ; Sun, 17 Oct 2021 17:36:58 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id C366C61151; Sun, 17 Oct 2021 15:36:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485017; bh=rEY9u4VmzR/u9TVBztHw00WOO/sZmK9yWL9aGTUAShY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PyxSu2Q53ihcMw4TG9MjUYe+ySkSliJbLXJ1YG5rDE1JPezLWQJ6Sc4XSiAQ+Q2Hq 12y3QpIjLJkEak1wbr+hwOp02PZ63nP3XsOmV0w7HyJ0VcJhgjEbIFHSLABuZ6e+sm yD2tWozmoUQETlUR+rEqw9KKM6hxyyhOwJdKebcOy98VP4V4+vZUhTSYjZin+7K3h8 u6ho4iGn2ov6/HyEalqjS0fwTTlU/Oc444+sRGZ+LQDTJ5Kw7CSQZyn5sgCDxj8cBo w41lSIgETeLNOWFlTQRZUB5JfNVYgsDJjYHmoISiU3iSQ/RlTs2QL6gdn1S0OV54hs MbfjPEFUvOAlg== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 12/13] env: Simplify env_match() and inline into env_get_f() Date: Sun, 17 Oct 2021 17:36:37 +0200 Message-Id: <20211017153638.29870-13-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún In the past the env_match() function was used to match envs with - name, i.e. string "name" - variable assignment, i.e. string "name=other_value" The latter is not the case anymore, since the env_match() function is now used only in env_get_f(), and so we can simplify the function into a simple strncmp() with an additional comparison to '='. Let's do this, and since the resulting function is quite simple, let's also inline its code into env_get_f(). Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index ffcfb55f10..272d0c7718 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -706,28 +706,19 @@ char *from_env(const char *envvar) return ret; } -static const char *env_match(const char *p, const char *s1) -{ - while (*s1 == *p++) - if (*s1++ == '=') - return p; - - if (*s1 == '\0' && p[-1] == '=') - return p; - - return NULL; -} - /* * Look up variable from environment for restricted C runtime env. */ int env_get_f(const char *name, char *buf, unsigned len) { const char *env, *p, *end; + size_t name_len; if (name == NULL || *name == '\0') return -1; + name_len = strlen(name); + if (gd->env_valid == ENV_INVALID) env = (const char *)default_environment; else @@ -741,9 +732,9 @@ int env_get_f(const char *name, char *buf, unsigned len) if (end - env >= CONFIG_ENV_SIZE) return -1; - value = env_match(p, name); - if (value == NULL) + if (strncmp(name, p, name_len) || p[name_len] != '=') continue; + value = &p[name_len + 1]; res = end - value; memcpy(buf, value, min(len, res + 1)); From patchwork Sun Oct 17 15:36:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marek_Beh=C3=BAn?= X-Patchwork-Id: 1542253 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=nLRicIfy; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HXPMD17dMz9sP7 for ; Mon, 18 Oct 2021 02:39:36 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 65CE48362B; Sun, 17 Oct 2021 17:38:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="nLRicIfy"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E5D6083303; Sun, 17 Oct 2021 17:37:19 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3C51F802A1 for ; Sun, 17 Oct 2021 17:37:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id 08E1C60FE3; Sun, 17 Oct 2021 15:36:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634485018; bh=PrDITsM8nsXQgOv1Dja2XLiTQHjG3DJ/DeVDgyiyRBM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nLRicIfyxLi3IwBA9UkPuTXxWcCXmwZttKMBkMrQ46WAkBfof6rClGU0QJX17sOSe ibetzjK8rgiARbSNvUkkfG1j8ySr3qVa5elxjMGRn1nKsUcDPuD1Lh65oygQmMFXam XYc9KxYeDJ3joaTZQDG40xBie6akPXh/1rXAF5PuLaGoxv1E4mzHJLYp1h2tLU+jp2 WrlFpDFnqk2KZj3rwFYcGc4njgdSNyoG157Z0vIQ0DnLfBq5G+rUJnY2LBqqyGOHu/ Og0F4FyZTnxok93ac7kbAi55dwTu4VNhK7Tdmok2LRCCPI9GM0ojJZvHDXbJ+oKmok +VKCr4iFQoeVA== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , Tom Rini Cc: U-Boot Mailing List , =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v3 13/13] env: Move non-cli env functions to env/common.c Date: Sun, 17 Oct 2021 17:36:38 +0200 Message-Id: <20211017153638.29870-14-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211017153638.29870-1-kabel@kernel.org> References: <20211017153638.29870-1-kabel@kernel.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Marek Behún Move the following functions from cmd/nvedit.c to env/common.c: env_set_ulong() env_set_hex() env_get_hex() eth_env_get_enetaddr() eth_env_set_enetaddr() env_get() from_env() env_get_f() env_get_ulong() since these functions are not specific for U-Boot's CLI. We leave env_set() in cmd/nvedit.c, since it calls _do_env_set(), which is a static function in that file. Signed-off-by: Marek Behún Reviewed-by: Simon Glass Signed-off-by: Marek Behún Reviewed-by: Simon Glass --- cmd/nvedit.c | 175 ------------------------------------------------- env/common.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 175 deletions(-) diff --git a/cmd/nvedit.c b/cmd/nvedit.c index 272d0c7718..3bb6e764c0 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -38,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -320,69 +318,6 @@ int env_set(const char *varname, const char *varvalue) return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); } -/** - * Set an environment variable to an integer value - * - * @param varname Environment variable to set - * @param value Value to set it to - * @return 0 if ok, 1 on error - */ -int env_set_ulong(const char *varname, ulong value) -{ - /* TODO: this should be unsigned */ - char *str = simple_itoa(value); - - return env_set(varname, str); -} - -/** - * Set an environment variable to an value in hex - * - * @param varname Environment variable to set - * @param value Value to set it to - * @return 0 if ok, 1 on error - */ -int env_set_hex(const char *varname, ulong value) -{ - char str[17]; - - sprintf(str, "%lx", value); - return env_set(varname, str); -} - -ulong env_get_hex(const char *varname, ulong default_val) -{ - const char *s; - ulong value; - char *endp; - - s = env_get(varname); - if (s) - value = hextoul(s, &endp); - if (!s || endp == s) - return default_val; - - return value; -} - -int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) -{ - string_to_enetaddr(env_get(name), enetaddr); - return is_valid_ethaddr(enetaddr); -} - -int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) -{ - char buf[ARP_HLEN_ASCII + 1]; - - if (eth_env_get_enetaddr(name, (uint8_t *)buf)) - return -EEXIST; - - sprintf(buf, "%pM", enetaddr); - - return env_set(name, buf); -} - #ifndef CONFIG_SPL_BUILD static int do_env_set(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) @@ -661,117 +596,7 @@ static int do_env_edit(struct cmd_tbl *cmdtp, int flag, int argc, } } #endif /* CONFIG_CMD_EDITENV */ -#endif /* CONFIG_SPL_BUILD */ -/* - * Look up variable from environment, - * return address of storage for that variable, - * or NULL if not found - */ -char *env_get(const char *name) -{ - if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ - struct env_entry e, *ep; - - WATCHDOG_RESET(); - - e.key = name; - e.data = NULL; - hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); - - return ep ? ep->data : NULL; - } - - /* restricted capabilities before import */ - if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) - return (char *)(gd->env_buf); - - return NULL; -} - -/* - * Like env_get, but prints an error if envvar isn't defined in the - * environment. It always returns what env_get does, so it can be used in - * place of env_get without changing error handling otherwise. - */ -char *from_env(const char *envvar) -{ - char *ret; - - ret = env_get(envvar); - - if (!ret) - printf("missing environment variable: %s\n", envvar); - - return ret; -} - -/* - * Look up variable from environment for restricted C runtime env. - */ -int env_get_f(const char *name, char *buf, unsigned len) -{ - const char *env, *p, *end; - size_t name_len; - - if (name == NULL || *name == '\0') - return -1; - - name_len = strlen(name); - - if (gd->env_valid == ENV_INVALID) - env = (const char *)default_environment; - else - env = (const char *)gd->env_addr; - - for (p = env; *p != '\0'; p = end + 1) { - const char *value; - unsigned res; - - for (end = p; *end != '\0'; ++end) - if (end - env >= CONFIG_ENV_SIZE) - return -1; - - if (strncmp(name, p, name_len) || p[name_len] != '=') - continue; - value = &p[name_len + 1]; - - res = end - value; - memcpy(buf, value, min(len, res + 1)); - - if (len <= res) { - buf[len - 1] = '\0'; - printf("env_buf [%u bytes] too small for value of \"%s\"\n", - len, name); - } - - return res; - } - - return -1; -} - -/** - * Decode the integer value of an environment variable and return it. - * - * @param name Name of environment 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 env_get_ulong(const char *name, int base, ulong default_val) -{ - /* - * We can use env_get() here, even before relocation, since the - * environment variable value is an integer and thus short. - */ - const char *str = env_get(name); - - return str ? simple_strtoul(str, NULL, base) : default_val; -} - -#ifndef CONFIG_SPL_BUILD #if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE) static int do_env_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) diff --git a/env/common.c b/env/common.c index 81e9e0b2aa..db213b7748 100644 --- a/env/common.c +++ b/env/common.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include DECLARE_GLOBAL_DATA_PTR; @@ -33,6 +35,184 @@ struct hsearch_data env_htab = { .change_ok = env_flags_validate, }; +/* + * This env_set() function is defined in cmd/nvedit.c, since it calls + * _do_env_set(), whis is a static function in that file. + * + * int env_set(const char *varname, const char *varvalue); + */ + +/** + * Set an environment variable to an integer value + * + * @param varname Environment variable to set + * @param value Value to set it to + * @return 0 if ok, 1 on error + */ +int env_set_ulong(const char *varname, ulong value) +{ + /* TODO: this should be unsigned */ + char *str = simple_itoa(value); + + return env_set(varname, str); +} + +/** + * Set an environment variable to an value in hex + * + * @param varname Environment variable to set + * @param value Value to set it to + * @return 0 if ok, 1 on error + */ +int env_set_hex(const char *varname, ulong value) +{ + char str[17]; + + sprintf(str, "%lx", value); + return env_set(varname, str); +} + +ulong env_get_hex(const char *varname, ulong default_val) +{ + const char *s; + ulong value; + char *endp; + + s = env_get(varname); + if (s) + value = hextoul(s, &endp); + if (!s || endp == s) + return default_val; + + return value; +} + +int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr) +{ + string_to_enetaddr(env_get(name), enetaddr); + return is_valid_ethaddr(enetaddr); +} + +int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr) +{ + char buf[ARP_HLEN_ASCII + 1]; + + if (eth_env_get_enetaddr(name, (uint8_t *)buf)) + return -EEXIST; + + sprintf(buf, "%pM", enetaddr); + + return env_set(name, buf); +} + +/* + * Look up variable from environment, + * return address of storage for that variable, + * or NULL if not found + */ +char *env_get(const char *name) +{ + if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */ + struct env_entry e, *ep; + + WATCHDOG_RESET(); + + e.key = name; + e.data = NULL; + hsearch_r(e, ENV_FIND, &ep, &env_htab, 0); + + return ep ? ep->data : NULL; + } + + /* restricted capabilities before import */ + if (env_get_f(name, (char *)(gd->env_buf), sizeof(gd->env_buf)) > 0) + return (char *)(gd->env_buf); + + return NULL; +} + +/* + * Like env_get, but prints an error if envvar isn't defined in the + * environment. It always returns what env_get does, so it can be used in + * place of env_get without changing error handling otherwise. + */ +char *from_env(const char *envvar) +{ + char *ret; + + ret = env_get(envvar); + + if (!ret) + printf("missing environment variable: %s\n", envvar); + + return ret; +} + +/* + * Look up variable from environment for restricted C runtime env. + */ +int env_get_f(const char *name, char *buf, unsigned len) +{ + const char *env, *p, *end; + size_t name_len; + + if (name == NULL || *name == '\0') + return -1; + + name_len = strlen(name); + + if (gd->env_valid == ENV_INVALID) + env = (const char *)default_environment; + else + env = (const char *)gd->env_addr; + + for (p = env; *p != '\0'; p = end + 1) { + const char *value; + unsigned res; + + for (end = p; *end != '\0'; ++end) + if (end - env >= CONFIG_ENV_SIZE) + return -1; + + if (strncmp(name, p, name_len) || p[name_len] != '=') + continue; + value = &p[name_len + 1]; + + res = end - value; + memcpy(buf, value, min(len, res + 1)); + + if (len <= res) { + buf[len - 1] = '\0'; + printf("env_buf [%u bytes] too small for value of \"%s\"\n", + len, name); + } + + return res; + } + + return -1; +} + +/** + * Decode the integer value of an environment variable and return it. + * + * @param name Name of environment 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 env_get_ulong(const char *name, int base, ulong default_val) +{ + /* + * We can use env_get() here, even before relocation, since the + * environment variable value is an integer and thus short. + */ + const char *str = env_get(name); + + return str ? simple_strtoul(str, NULL, base) : default_val; +} + /* * Read an environment variable as a boolean * Return -1 if variable does not exist (default to true)