From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1379C433F5 for ; Thu, 28 Oct 2021 03:29:11 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4180B60EC0 for ; Thu, 28 Oct 2021 03:29:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 4180B60EC0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5496A834D0; Thu, 28 Oct 2021 05:28: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="L6+Yz/k9"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 373C5832E8; Thu, 28 Oct 2021 05:28:33 +0200 (CEST) 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 F163982D6F for ; Thu, 28 Oct 2021 05:28:19 +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 9540461184; Thu, 28 Oct 2021 03:28:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635391699; bh=Ib7VGslyIXTozpTzpW08RQZ/lPuBMlBRS/Kd0H/PVos=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L6+Yz/k9m3UfstP/Mnh3n8wlrHBoQK19bKwDwxfY/HnmljrL/KHgLdOlmnxj3Ptb2 Vccusez+hCNh+nlRot5JwaIeSCIgP+JIcDiA88f38kxR2dVijtLWxcrJt12YKQLYzG mS3Ka1E7Wl3agtk+1r3BGB5aWB2RI8oYAUnYUsDaqAJ0lSxg+GhuFBlFN1g2c/fu7v VejVW0TM/qeR9E/sox/G8HL4F9uvUrGs77qtl+ipmlp1XZHH8lag2nERVgccq8X6BA GvbPV9W5CYt2ZTXQT3CheGwzxE474pru533XMkeTgTMC1nu1phqbvmA48AdgY14gr9 0xM8gTJfspq4w== From: =?UTF-8?q?Marek=20Beh=C3=BAn?= To: Simon Glass , Stefan Roese , =?UTF-8?q?Pali=20Roh=C3=A1r?= Cc: u-boot@lists.denx.de, =?UTF-8?q?Marek=20Beh=C3=BAn?= Subject: [PATCH 4/5] env: Add support for board specific special default environment Date: Thu, 28 Oct 2021 05:28:09 +0200 Message-Id: <20211028032810.18146-5-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211028032810.18146-1-kabel@kernel.org> References: <20211028032810.18146-1-kabel@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 default_environment[] buffer is built at compile time, but sometimes it makes sense for some default environment variables to be determined at runtime, for example: - one board code may support different boards, and needs that fdtfile, board, board_name are set appropriately when command env default -a is executed - some boards may want to prohibit the env default -a command to remove device MAC addresses stored in ethaddr, ethNaddr. This is the case for the ESPRESSObin board code, for example, where currently the board_late_init() function rewrites the default environment array to achieve this. Add a new board specific function, const char *board_special_default_env(unsigned i, const char **name); which returns the value of i-th board special default environemnt variable, while storing it's name to *name. Add default weak implementation of this functions returning NULL. Add code to default environemnt handlers in env/common.c, which iterate these special board default environment variables and get it's values in precedence to values in the default_environment[] buffer. Signed-off-by: Marek BehĂșn --- env/common.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/env/common.c b/env/common.c index 208e2adaa0..d157ca562a 100644 --- a/env/common.c +++ b/env/common.c @@ -148,6 +148,41 @@ char *from_env(const char *envvar) return ret; } +__weak const char *board_special_default_env(unsigned i, const char **name) +{ + return NULL; +} + +static int board_special_default_env_get(const char *var, char *buf, + unsigned len) +{ + int i; + + /* + * Iterate all board special default env variables, and if one + * exists with the requested name, return it. + */ + for (i = 0; ; ++i) { + const char *name, *value; + + value = board_special_default_env(i, &name); + if (!value) + break; + + if (!strcmp(var, name)) { + unsigned res = strlen(value); + + memcpy(buf, value, min(len, res + 1)); + if (len <= res) + buf[len - 1] = '\0'; + + return res; + } + } + + return -1; +} + static int env_get_from_linear(const char *env, const char *name, char *buf, unsigned len) { @@ -157,6 +192,17 @@ static int env_get_from_linear(const char *env, const char *name, char *buf, if (name == NULL || *name == '\0') return -1; + if (env == default_environment) { + int res = board_special_default_env_get(name, buf, len); + + /* + * Board special default envs take precedence over the + * default_environment[] array. + */ + if (res >= 0) + return res; + } + name_len = strlen(name); for (p = env; *p != '\0'; p = end + 1) { @@ -248,6 +294,41 @@ char *env_get_default(const char *name) return NULL; } +static int import_board_special_default_envs(bool all, int nvars, + char * const vars[], int flags) +{ + int i; + + for (i = 0; ; ++i) { + const char *name, *value; + struct env_entry e, *ep; + + value = board_special_default_env(i, &name); + if (!value) + break; + + if (!all) { + int j; + + /* If name is not in vars, skip */ + + for (j = 0; j < nvars; ++j) + if (!strcmp(name, vars[j])) + break; + if (j == nvars) + continue; + } + + e.key = name; + e.data = (char *)value; + + if (!hsearch_r(e, ENV_ENTER, &ep, &env_htab, flags)) + return -1; + } + + return 0; +} + void env_set_default(const char *s, int flags) { if (s) { @@ -270,6 +351,12 @@ void env_set_default(const char *s, int flags) return; } + if (import_board_special_default_envs(true, 0, NULL, flags) < 0) { + pr_err("## Error: Board special default environment import failed: errno = %d\n", + errno); + return; + } + gd->flags |= GD_FLG_ENV_READY; gd->flags |= GD_FLG_ENV_DEFAULT; } @@ -278,14 +365,20 @@ void env_set_default(const char *s, int flags) /* [re]set individual variables to their value in the default environment */ int env_set_default_vars(int nvars, char * const vars[], int flags) { + int res; + /* * Special use-case: import from default environment * (and use \0 as a separator) */ flags |= H_NOCLEAR | H_DEFAULT; - return himport_r(&env_htab, default_environment, - sizeof(default_environment), '\0', - flags, 0, nvars, vars); + res = himport_r(&env_htab, default_environment, + sizeof(default_environment), '\0', flags, 0, nvars, + vars); + if (!res) + return res; + + return import_board_special_default_envs(false, nvars, vars, flags); } /* -- 2.32.0