Skip to content
Snippets Groups Projects
Commit b64b7c3d authored by Gerlando Falauto's avatar Gerlando Falauto Committed by Tom Rini
Browse files

env: make "env default" selective, check and apply


Change the syntax (user API) for "env default":
  -f: override write-once variables
  var... : accept individual variable(s)
  -a: all (resetting the whole env is NOT the default behavior)

Enable variable checking and make changes effective by
enabling do_apply argument to himport_r().

Signed-off-by: default avatarGerlando Falauto <gerlando.falauto@keymile.com>
parent 152874b6
Branches
Tags
No related merge requests found
...@@ -656,15 +656,42 @@ int envmatch(uchar *s1, int i2) ...@@ -656,15 +656,42 @@ int envmatch(uchar *s1, int i2)
return -1; return -1;
} }
static int do_env_default(cmd_tbl_t *cmdtp, int flag, static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
int argc, char * const argv[]) int argc, char * const argv[])
{ {
if (argc != 2 || strcmp(argv[1], "-f") != 0) int all = 0, flag = 0;
return CMD_RET_USAGE;
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"); set_default_env("## Resetting to default environment\n");
return 0; return 0;
} }
if (!all && (argc > 0)) {
/* Reset individual variables */
set_default_vars(argc, argv);
return 0;
}
return cmd_usage(cmdtp);
}
static int do_env_delete(cmd_tbl_t *cmdtp, int flag, static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
int argc, char * const argv[]) int argc, char * const argv[])
...@@ -994,7 +1021,8 @@ U_BOOT_CMD( ...@@ -994,7 +1021,8 @@ U_BOOT_CMD(
#if defined(CONFIG_CMD_ASKENV) #if defined(CONFIG_CMD_ASKENV)
"ask name [message] [size] - ask for environment variable\nenv " "ask name [message] [size] - ask for environment variable\nenv "
#endif #endif
"default -f - reset default environment\n" "default [-f] -a - [forcibly] reset default environment\n"
"env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
#if defined(CONFIG_CMD_EDITENV) #if defined(CONFIG_CMD_EDITENV)
"env edit name - edit environment variable\n" "env edit name - edit environment variable\n"
#endif #endif
......
...@@ -180,6 +180,11 @@ const uchar *env_get_addr(int index) ...@@ -180,6 +180,11 @@ const uchar *env_get_addr(int index)
void set_default_env(const char *s) void set_default_env(const char *s)
{ {
/*
* By default, do not apply changes as they will eventually
* be applied by someone else
*/
int do_apply = 0;
if (sizeof(default_environment) > ENV_SIZE) { if (sizeof(default_environment) > ENV_SIZE) {
puts("*** Error - default environment is too large\n\n"); puts("*** Error - default environment is too large\n\n");
return; return;
...@@ -191,6 +196,14 @@ void set_default_env(const char *s) ...@@ -191,6 +196,14 @@ void set_default_env(const char *s)
"using default environment\n\n", "using default environment\n\n",
s + 1); s + 1);
} else { } else {
/*
* This set_to_default was explicitly asked for
* by the user, as opposed to being a recovery
* mechanism. Therefore we check every single
* variable and apply changes to the system
* right away (e.g. baudrate, console).
*/
do_apply = 1;
puts(s); puts(s);
} }
} else { } else {
...@@ -199,12 +212,25 @@ void set_default_env(const char *s) ...@@ -199,12 +212,25 @@ void set_default_env(const char *s)
if (himport_r(&env_htab, (char *)default_environment, if (himport_r(&env_htab, (char *)default_environment,
sizeof(default_environment), '\0', 0, sizeof(default_environment), '\0', 0,
0, NULL, 0 /* do_apply */) == 0) 0, NULL, do_apply) == 0)
error("Environment import failed: errno = %d\n", errno); error("Environment import failed: errno = %d\n", errno);
gd->flags |= GD_FLG_ENV_READY; gd->flags |= GD_FLG_ENV_READY;
} }
/* [re]set individual variables to their value in the default environment */
int set_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, 1 /* do_apply */);
}
/* /*
* Check if CRC is valid and (if yes) import the environment. * Check if CRC is valid and (if yes) import the environment.
* Note that "buf" may or may not be aligned. * Note that "buf" may or may not be aligned.
......
...@@ -181,6 +181,9 @@ void env_crc_update(void); ...@@ -181,6 +181,9 @@ void env_crc_update(void);
/* [re]set to the default environment */ /* [re]set to the default environment */
void set_default_env(const char *s); void set_default_env(const char *s);
/* [re]set individual variables to their value in the default environment */
int set_default_vars(int nvars, char * const vars[]);
/* Import from binary representation into hash table */ /* Import from binary representation into hash table */
int env_import(const char *buf, int check); int env_import(const char *buf, int check);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment