From patchwork Wed Nov 3 23:23: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: 1550638 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=BWUIcxF3; 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 4Hl2v12ZXSz9sRN for ; Thu, 4 Nov 2021 10:25:32 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0D48B83678; Thu, 4 Nov 2021 00:24:31 +0100 (CET) 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="BWUIcxF3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B9274835B9; Thu, 4 Nov 2021 00:23:57 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.8 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 1F5A983659 for ; Thu, 4 Nov 2021 00:23:49 +0100 (CET) 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 D826F611C5; Wed, 3 Nov 2021 23:23:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635981828; bh=nl6ByzKOg8HZORBoYSHFz8N8eCVQUjMCHi8xozkRg40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BWUIcxF3LB6/gVVO2pfgqzKABdlKhT1TFk/xTESwSmuyJTh714InbQO+efvyyH9sl ZY5pRSeZhib21/KndeWCBrDu9d4+n9N+uBfairBsp/2DWFXRo5fEf0Rtl3mEHYMTZ8 2LAg58+1uJkALYbcmH8XCrqgFGpXgqE/sAEAh4TYES60IG+zUj3uPCs71YcCFpYjpT SjirLGNJ+XVDrRo+iFL3Qxp3+mFH90IEtg7DXmLc7mSmdn2wDW/G3lin6AAjiQXIqX DqSaqK23xlql0zm0EZy5OH4U8snOcKv4G/t4bnwm1+RQExVn1eFDz8hXEOQ5wU9OXD JoZakRt7tYX/Q== From: =?utf-8?q?Marek_Beh=C3=BAn?= To: Simon Glass , =?utf-8?q?Pali_Roh=C3=A1r?= Cc: u-boot@lists.denx.de, =?utf-8?q?Marek_Beh=C3=BAn?= Subject: [PATCH v2 10/12] env: Add support for overwriting default environment via sysinfo Date: Thu, 4 Nov 2021 00:23:30 +0100 Message-Id: <20211103232332.2737-11-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211103232332.2737-1-kabel@kernel.org> References: <20211103232332.2737-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 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 new sysinfo IDs SYSINFO_ID_DEF_ENV_NAMES SYSINFO_ID_DEF_ENV_VALUES corresponding to sysinfo string list of names and values of default environment variables that are to take precedence over the default_environment[] buffer. Add code to default environemnt handlers in env/common.c, which iterate these sysinfo string lists correspondingly. Signed-off-by: Marek BehĂșn --- env/common.c | 107 +++++++++++++++++++++++++++++++++++++++++++++- include/sysinfo.h | 4 ++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/env/common.c b/env/common.c index cefe58561b..8ac287beae 100644 --- a/env/common.c +++ b/env/common.c @@ -22,6 +22,7 @@ #include #include #include +#include #include DECLARE_GLOBAL_DATA_PTR; @@ -148,6 +149,31 @@ char *from_env(const char *envvar) return ret; } +static int sysinfo_default_env_get(const char *var, char *buf, unsigned len) +{ + struct udevice *dev; + unsigned idx; + char *name; + void *iter; + + if (sysinfo_get(&dev) < 0 || sysinfo_detect(dev) < 0) + return -ENODEV; + + idx = 0; + for_each_sysinfo_str_list(dev, SYSINFO_ID_DEF_ENV_NAMES, name, iter) { + if (strcmp(var, name)) { + ++idx; + continue; + } + + free(iter); + return sysinfo_get_str_list(dev, SYSINFO_ID_DEF_ENV_VALUES, idx, + len, buf); + } + + return -ENOENT; +} + static int env_get_from_linear(const char *env, const char *name, char *buf, unsigned len) { @@ -157,6 +183,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 = sysinfo_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,8 +285,69 @@ char *env_get_default(const char *name) return NULL; } +static int sysinfo_import_default_envs(bool all, int nvars, char * const vars[], + int flags) +{ + struct udevice *dev; + char *name, *value; + unsigned idx; + void *iter; + int len; + + if (sysinfo_get(&dev) < 0 || sysinfo_detect(dev) < 0) + return 0; + + len = sysinfo_get_str_list_max_len(dev, SYSINFO_ID_DEF_ENV_VALUES); + if (len == -ENOSYS || len == -ENOENT || len == -ERANGE) + return 0; + else if (len < 0) + return len; + + value = malloc(len + 1); + if (!value) + return -ENOMEM; + + idx = 0; + for_each_sysinfo_str_list(dev, SYSINFO_ID_DEF_ENV_NAMES, name, iter) { + struct env_entry e, *ep; + + 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) { + idx++; + continue; + } + } + + len = sysinfo_get_str_list(dev, SYSINFO_ID_DEF_ENV_VALUES, + idx++, ENV_SIZE, value); + if (len < 0) + continue; + + e.key = name; + e.data = value; + if (!hsearch_r(e, ENV_ENTER, &ep, &env_htab, flags)) { + int res = -errno; + free(iter); + free(value); + return res; + } + } + + free(value); + + return 0; +} + void env_set_default(const char *s, int flags) { + int res; + if (s) { if ((flags & H_INTERACTIVE) == 0) { printf("*** Warning - %s, " @@ -270,6 +368,13 @@ void env_set_default(const char *s, int flags) return; } + res = sysinfo_import_default_envs(true, 0, NULL, flags); + if (res < 0) { + pr_err("## Error: Board special default environment import failed: %d\n", + res); + return; + } + gd->flags |= GD_FLG_ENV_READY; gd->flags |= GD_FLG_ENV_DEFAULT; } @@ -288,7 +393,7 @@ int env_set_default_vars(int nvars, char * const vars[], int flags) vars)) return -errno; - return 0; + return sysinfo_import_default_envs(false, nvars, vars, flags); } /* diff --git a/include/sysinfo.h b/include/sysinfo.h index d32bf3e808..587ad0dc75 100644 --- a/include/sysinfo.h +++ b/include/sysinfo.h @@ -47,6 +47,10 @@ enum sysinfo_id { /* For show_board_info() */ SYSINFO_ID_BOARD_MODEL, + /* For overwriting default environment variables */ + SYSINFO_ID_DEF_ENV_NAMES, + SYSINFO_ID_DEF_ENV_VALUES, + /* First value available for downstream/board used */ SYSINFO_ID_USER = 0x1000, };