From patchwork Fri Nov 18 16:49:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerlando Falauto X-Patchwork-Id: 126453 X-Patchwork-Delegate: wd@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 65B77B71E5 for ; Sat, 19 Nov 2011 03:50:51 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5F43228421; Fri, 18 Nov 2011 17:50:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rKF4RkejM6aD; Fri, 18 Nov 2011 17:50:20 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E539128B66; Fri, 18 Nov 2011 17:49:53 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 78F5028B33 for ; Fri, 18 Nov 2011 17:49:46 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id A4fIC+Z1IzrU for ; Fri, 18 Nov 2011 17:49:45 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.de.keymile.com (mail.de.keymile.com [195.8.104.1]) by theia.denx.de (Postfix) with SMTP id 0D86428981 for ; Fri, 18 Nov 2011 17:49:40 +0100 (CET) Received: from mailrelay.de.keymile.net ([10.9.1.54]) by eSafe SMTP Relay 1321520839; Fri, 18 Nov 2011 17:49:40 +0100 Received: from chber1-10555x.ch.keymile.net (chber1-10555x.ch.keymile.net [172.31.31.112]) by mailrelay.de.keymile.net (8.12.2/8.12.2) with ESMTP id pAIGn6jT017405; Fri, 18 Nov 2011 17:49:21 +0100 (MET) From: Gerlando Falauto To: u-boot@lists.denx.de Date: Fri, 18 Nov 2011 17:49:15 +0100 Message-Id: <1321634955-5561-6-git-send-email-gerlando.falauto@keymile.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1321634955-5561-1-git-send-email-gerlando.falauto@keymile.com> References: <1321634955-5561-1-git-send-email-gerlando.falauto@keymile.com> X-ESAFE-STATUS: [srvhellgate.de.keymile.net] Mail clean Cc: holger.brunck@keymile.com, Gerlando Falauto Subject: [U-Boot] [PATCH v1 5/5] env: make "env default" selective, check and apply X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Changes in the syntax (user API) for "env default": -f: override write-once variables -a: all (resetting the whole env is NOT the default behavior) Signed-off-by: Gerlando Falauto --- NOTE: Since the impact in code size is really negligible, I would rather remove this #ifdef CONFIG_CMD_DEFAULTENV_VARS conditional. README | 2 ++ common/cmd_nvedit.c | 44 +++++++++++++++++++++++++++++++++++++------- common/env_common.c | 31 ++++++++++++++++++++++++++++++- include/config_cmd_all.h | 1 + include/environment.h | 5 +++++ 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/README b/README index 07f1d11..d2b4071 100644 --- a/README +++ b/README @@ -724,6 +724,8 @@ The following options need to be configured: CONFIG_CMD_CONSOLE coninfo CONFIG_CMD_CRC32 * crc32 CONFIG_CMD_DATE * support for RTC, date/time... + CONFIG_CMD_DEFAULTENV_VARS + * Reset individual variables to default CONFIG_CMD_DHCP * DHCP support CONFIG_CMD_DIAG * Diagnostics CONFIG_CMD_DS4510 * ds4510 I2C gpio commands diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c index 128b334..5c57117 100644 --- a/common/cmd_nvedit.c +++ b/common/cmd_nvedit.c @@ -676,13 +676,40 @@ int envmatch(uchar *s1, int i2) return -1; } -static int do_env_default(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +static int do_env_default(cmd_tbl_t *cmdtp, int __flag, int argc, + char * const argv[]) { - if ((argc != 2) || (strcmp(argv[1], "-f") != 0)) - return cmd_usage(cmdtp); - - set_default_env("## Resetting to default environment\n"); - return 0; + int all = 0, flag = 0; + debug("Initial value for argc=%d\n", argc); + while (--argc > 0 && **++argv == '-') { + char *arg = *argv; + while (*++arg) { + switch (*arg) { + case 'a': /* default all */ + all = 1; + break; + case 'f': /* force */ + flag |= H_FORCE; + break; + default: + return cmd_usage(cmdtp); + } + } + } + debug("Final value for argc=%d\n", argc); + if (all && (argc == 0)) { + /* Reset the whole environment */ + set_default_env("## Resetting to default environment\n"); + return 0; + } +#ifdef CONFIG_CMD_DEFAULTENV_VARS + if (!all && (argc > 0)) { + /* Reset individual variables */ + env_default_vars(argc, argv); + return 0; + } +#endif + return cmd_usage(cmdtp); } static int do_env_delete(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) @@ -1017,7 +1044,10 @@ U_BOOT_CMD( #if defined(CONFIG_CMD_ASKENV) "ask name [message] [size] - ask for environment variable\nenv " #endif - "default -f - reset default environment\n" + "default [-f] -a - [forcibly] reset default environment\n" +#if defined(CONFIG_CMD_DEFAULTENV_VARS) + "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n" +#endif #if defined(CONFIG_CMD_EDITENV) "env edit name - edit environment variable\n" #endif diff --git a/common/env_common.c b/common/env_common.c index 9d8c59e..eca3280 100644 --- a/common/env_common.c +++ b/common/env_common.c @@ -172,6 +172,11 @@ const uchar *env_get_addr (int index) void set_default_env(const char *s) { + /* + * By default, do not apply changes as they will eventually + * be applied by someone else + */ + apply_cb apply_function = NULL; if (sizeof(default_environment) > ENV_SIZE) { puts("*** Error - default environment is too large\n\n"); return; @@ -183,6 +188,14 @@ void set_default_env(const char *s) "using default environment\n\n", s+1); } else { + /* + * This set_to_default was explicitly asked for + * by the user, as opposed to being a recovery + * mechanism. Therefore we chack every single + * variable and apply changes to the system + * right away (e.g. baudrate, console). + */ + apply_function = env_check_apply; puts(s); } } else { @@ -191,12 +204,28 @@ void set_default_env(const char *s) if (himport_r(&env_htab, (char *)default_environment, sizeof(default_environment), '\0', 0, - 0, NULL, NULL) == 0) { + 0, NULL, apply_function) == 0) { error("Environment import failed: errno = %d\n", errno); } gd->flags |= GD_FLG_ENV_READY; } +#ifdef CONFIG_CMD_DEFAULTENV_VARS + +/* [re]set individual variables to their value in the default environment */ +int env_default_vars(int nvars, char * const vars[]) +{ + /* + * Special use-case: import from default environment + * (and use \0 as a separator) + */ + return himport_r(&env_htab, (const char *)default_environment, + sizeof(default_environment), '\0', H_NOCLEAR, + nvars, vars, env_check_apply); +} + +#endif /* CONFIG_CMD_DEFAULTENV_VARS */ + /* * Check if CRC is valid and (if yes) import the environment. * Note that "buf" may or may not be aligned. diff --git a/include/config_cmd_all.h b/include/config_cmd_all.h index 9716f9c..e728eae 100644 --- a/include/config_cmd_all.h +++ b/include/config_cmd_all.h @@ -25,6 +25,7 @@ #define CONFIG_CMD_CDP /* Cisco Discovery Protocol */ #define CONFIG_CMD_CONSOLE /* coninfo */ #define CONFIG_CMD_DATE /* support for RTC, date/time...*/ +#define CONFIG_CMD_DEFAULTENV_VARS /* default individ variables */ #define CONFIG_CMD_DHCP /* DHCP Support */ #define CONFIG_CMD_DIAG /* Diagnostics */ #define CONFIG_CMD_DISPLAY /* Display support */ diff --git a/include/environment.h b/include/environment.h index 1936411..b594e67 100644 --- a/include/environment.h +++ b/include/environment.h @@ -171,6 +171,11 @@ void env_crc_update (void); /* [re]set to the default environment */ void set_default_env(const char *s); +#ifdef CONFIG_CMD_DEFAULTENV_VARS +/* [re]set individual variables to their value in the default environment */ +int env_default_vars(int nvars, char * const vars[]); +#endif + /* Import from binary representation into hash table */ int env_import(const char *buf, int check);