#include <cli.h>
#include <command.h>
#include <console.h>
-#include <environment.h>
+#include <env.h>
+#include <env_internal.h>
#include <search.h>
#include <errno.h>
#include <malloc.h>
#include <mapmem.h>
+#include <u-boot/crc.h>
#include <watchdog.h>
#include <linux/stddef.h>
#include <asm/byteorder.h>
/*
* This variable is incremented on each do_env_set(), so it can
- * be used via get_env_id() as an indication, if the environment
+ * be used via env_get_id() as an indication, if the environment
* has changed or not. So it is possible to reread an environment
* variable only if the environment was changed ... done so for
* example in NetInitLoop()
*/
static int env_id = 1;
-int get_env_id(void)
+int env_get_id(void)
{
return env_id;
}
ssize_t len;
if (name) { /* print a single name */
- ENTRY e, *ep;
+ struct env_entry e, *ep;
e.key = name;
e.data = NULL;
- hsearch_r(e, FIND, &ep, &env_htab, flag);
+ hsearch_r(e, ENV_FIND, &ep, &env_htab, flag);
if (ep == NULL)
return 0;
len = printf("%s=%s\n", ep->key, ep->data);
{
int i, len;
char *name, *value, *s;
- ENTRY e, *ep;
+ struct env_entry e, *ep;
debug("Initial value for argc=%d\n", argc);
e.key = name;
e.data = value;
- hsearch_r(e, ENTER, &ep, &env_htab, env_flag);
+ hsearch_r(e, ENV_ENTER, &ep, &env_htab, env_flag);
free(value);
if (!ep) {
printf("## Error inserting \"%s\" variable, errno=%d\n",
return value;
}
-void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr)
-{
- char *end;
- int i;
-
- for (i = 0; i < 6; ++i) {
- enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
- if (addr)
- addr = (*end) ? end + 1 : end;
- }
-}
-
int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr)
{
- eth_parse_enetaddr(env_get(name), enetaddr);
+ string_to_enetaddr(env_get(name), enetaddr);
return is_valid_ethaddr(enetaddr);
}
return 0;
}
-static int print_active_callback(ENTRY *entry)
+static int print_active_callback(struct env_entry *entry)
{
struct env_clbk_tbl *clbkp;
int i;
return 0;
}
-static int print_active_flags(ENTRY *entry)
+static int print_active_flags(struct env_entry *entry)
{
enum env_flags_vartype type;
enum env_flags_varaccess access;
char *env_get(const char *name)
{
if (gd->flags & GD_FLG_ENV_READY) { /* after import into hashtable */
- ENTRY e, *ep;
+ struct env_entry e, *ep;
WATCHDOG_RESET();
e.key = name;
e.data = NULL;
- hsearch_r(e, FIND, &ep, &env_htab, 0);
+ hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
return ep ? ep->data : NULL;
}
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.
*/
return -1;
}
- val = envmatch((uchar *)name, i);
+ val = env_match((uchar *)name, i);
if (val < 0)
continue;
"save environment variables to persistent storage",
""
);
+
+#if defined(CONFIG_CMD_ERASEENV)
+static int do_env_erase(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
+{
+ return env_erase() ? 1 : 0;
+}
+
+U_BOOT_CMD(
+ eraseenv, 1, 0, do_env_erase,
+ "erase environment variables from persistent storage",
+ ""
+);
+#endif
#endif
#endif /* CONFIG_SPL_BUILD */
-
-/*
- * Match a name / name=value pair
- *
- * s1 is either a simple 'name', or a 'name=value' pair.
- * i2 is the environment index for a 'name2=value2' pair.
- * If the names match, return the index for the value2, else -1.
- */
-int envmatch(uchar *s1, int i2)
+int env_match(uchar *s1, int i2)
{
if (s1 == NULL)
return -1;
debug("Final value for argc=%d\n", argc);
if (all && (argc == 0)) {
/* Reset the whole environment */
- set_default_env("## Resetting to default environment\n",
+ env_set_default("## Resetting to default environment\n",
env_flag);
return 0;
}
if (!all && (argc > 0)) {
/* Reset individual variables */
- set_default_vars(argc, argv, env_flag);
+ env_set_default_vars(argc, argv, env_flag);
return 0;
}
envp->crc = crc32(0, envp->data,
size ? size - offsetof(env_t, data) : ENV_SIZE);
#ifdef CONFIG_ENV_ADDR_REDUND
- envp->flags = ACTIVE_FLAG;
+ envp->flags = ENV_REDUND_ACTIVE;
#endif
}
env_set_hex("filesize", len + offsetof(env_t, data));
}
#endif
+#if defined(CONFIG_CMD_NVEDIT_INFO)
+/*
+ * print_env_info - print environment information
+ */
+static int print_env_info(void)
+{
+ const char *value;
+
+ /* print environment validity value */
+ switch (gd->env_valid) {
+ case ENV_INVALID:
+ value = "invalid";
+ break;
+ case ENV_VALID:
+ value = "valid";
+ break;
+ case ENV_REDUND:
+ value = "redundant";
+ break;
+ default:
+ value = "unknown";
+ break;
+ }
+ printf("env_valid = %s\n", value);
+
+ /* print environment ready flag */
+ value = gd->flags & GD_FLG_ENV_READY ? "true" : "false";
+ printf("env_ready = %s\n", value);
+
+ /* print environment using default flag */
+ value = gd->flags & GD_FLG_ENV_DEFAULT ? "true" : "false";
+ printf("env_use_default = %s\n", value);
+
+ return CMD_RET_SUCCESS;
+}
+
+#define ENV_INFO_IS_DEFAULT BIT(0) /* default environment bit mask */
+#define ENV_INFO_IS_PERSISTED BIT(1) /* environment persistence bit mask */
+
+/*
+ * env info - display environment information
+ * env info [-d] - evaluate whether default environment is used
+ * env info [-p] - evaluate whether environment can be persisted
+ */
+static int do_env_info(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
+{
+ int eval_flags = 0;
+ int eval_results = 0;
+
+ /* display environment information */
+ if (argc <= 1)
+ return print_env_info();
+
+ /* process options */
+ while (--argc > 0 && **++argv == '-') {
+ char *arg = *argv;
+
+ while (*++arg) {
+ switch (*arg) {
+ case 'd':
+ eval_flags |= ENV_INFO_IS_DEFAULT;
+ break;
+ case 'p':
+ eval_flags |= ENV_INFO_IS_PERSISTED;
+ break;
+ default:
+ return CMD_RET_USAGE;
+ }
+ }
+ }
+
+ /* evaluate whether default environment is used */
+ if (eval_flags & ENV_INFO_IS_DEFAULT) {
+ if (gd->flags & GD_FLG_ENV_DEFAULT) {
+ printf("Default environment is used\n");
+ eval_results |= ENV_INFO_IS_DEFAULT;
+ } else {
+ printf("Environment was loaded from persistent storage\n");
+ }
+ }
+
+ /* evaluate whether environment can be persisted */
+ if (eval_flags & ENV_INFO_IS_PERSISTED) {
+#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
+ printf("Environment can be persisted\n");
+ eval_results |= ENV_INFO_IS_PERSISTED;
+#else
+ printf("Environment cannot be persisted\n");
+#endif
+ }
+
+ /* The result of evaluations is combined with AND */
+ if (eval_flags != eval_results)
+ return CMD_RET_FAILURE;
+
+ return CMD_RET_SUCCESS;
+}
+#endif
+
#if defined(CONFIG_CMD_ENV_EXISTS)
static int do_env_exists(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
- ENTRY e, *ep;
+ struct env_entry e, *ep;
if (argc < 2)
return CMD_RET_USAGE;
e.key = argv[1];
e.data = NULL;
- hsearch_r(e, FIND, &ep, &env_htab, 0);
+ hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
return (ep == NULL) ? 1 : 0;
}
#endif
#if defined(CONFIG_CMD_IMPORTENV)
U_BOOT_CMD_MKENT(import, 5, 0, do_env_import, "", ""),
+#endif
+#if defined(CONFIG_CMD_NVEDIT_INFO)
+ U_BOOT_CMD_MKENT(info, 2, 0, do_env_info, "", ""),
#endif
U_BOOT_CMD_MKENT(print, CONFIG_SYS_MAXARGS, 1, do_env_print, "", ""),
#if defined(CONFIG_CMD_RUN)
#endif
#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
U_BOOT_CMD_MKENT(save, 1, 0, do_env_save, "", ""),
+#if defined(CONFIG_CMD_ERASEENV)
+ U_BOOT_CMD_MKENT(erase, 1, 0, do_env_erase, "", ""),
+#endif
#endif
U_BOOT_CMD_MKENT(set, CONFIG_SYS_MAXARGS, 0, do_env_set, "", ""),
#if defined(CONFIG_CMD_ENV_EXISTS)
#endif
#if defined(CONFIG_CMD_IMPORTENV)
"env import [-d] [-t [-r] | -b | -c] addr [size] [var ...] - import environment\n"
+#endif
+#if defined(CONFIG_CMD_NVEDIT_INFO)
+ "env info - display environment information\n"
+ "env info [-d] - whether default environment is used\n"
+ "env info [-p] - whether environment can be persisted\n"
#endif
"env print [-a | name ...] - print environment\n"
#if defined(CONFIG_CMD_NVEDIT_EFI)
- "env print -e [name ...] - print UEFI environment\n"
+ "env print -e [-guid guid|-all][-n] [name ...] - print UEFI environment\n"
#endif
#if defined(CONFIG_CMD_RUN)
"env run var [...] - run commands in an environment variable\n"
#endif
#if defined(CONFIG_CMD_SAVEENV) && defined(ENV_IS_IN_DEVICE)
"env save - save environment\n"
+#if defined(CONFIG_CMD_ERASEENV)
+ "env erase - erase environment\n"
+#endif
#endif
#if defined(CONFIG_CMD_NVEDIT_EFI)
- "env set -e name [arg ...] - set UEFI variable; unset if 'arg' not specified\n"
+ "env set -e [-nv][-bs][-rt][-a][-i addr,size][-v] name [arg ...]\n"
+ " - set UEFI variable; unset if '-i' or 'arg' not specified\n"
#endif
"env set [-f] name [arg ...]\n";
#endif
"print environment variables",
"[-a]\n - print [all] values of all environment variables\n"
#if defined(CONFIG_CMD_NVEDIT_EFI)
- "printenv -e [name ...]\n"
+ "printenv -e [-guid guid|-all][-n] [name ...]\n"
" - print UEFI variable 'name' or all the variables\n"
+ " \"-n\": suppress dumping variable's value\n"
#endif
"printenv name ...\n"
" - print value of environment variable 'name'",
setenv, CONFIG_SYS_MAXARGS, 0, do_env_set,
"set environment variables",
#if defined(CONFIG_CMD_NVEDIT_EFI)
- "-e [-nv] name [value ...]\n"
+ "-e [-guid guid][-nv][-bs][-rt][-a][-v]\n"
+ " [-i addr,size name], or [name [value ...]]\n"
" - set UEFI variable 'name' to 'value' ...'\n"
- " 'nv' option makes the variable non-volatile\n"
+ " \"-guid\": set vendor guid\n"
+ " \"-nv\": set non-volatile attribute\n"
+ " \"-bs\": set boot-service attribute\n"
+ " \"-rt\": set runtime attribute\n"
+ " \"-a\": append-write\n"
+ " \"-i addr,size\": use <addr,size> as variable's value\n"
+ " \"-v\": verbose message\n"
" - delete UEFI variable 'name' if 'value' not specified\n"
#endif
"setenv [-f] name value ...\n"