2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <environment.h>
30 #include <linux/stddef.h>
35 DECLARE_GLOBAL_DATA_PTR;
37 /************************************************************************
38 * Default settings to be used when no valid environment is found
40 #include <env_default.h>
42 struct hsearch_data env_htab = {
43 .change_ok = env_flags_validate,
46 static uchar __env_get_char_spec(int index)
48 return *((uchar *)(gd->env_addr + index));
50 uchar env_get_char_spec(int)
51 __attribute__((weak, alias("__env_get_char_spec")));
53 static uchar env_get_char_init(int index)
55 /* if crc was bad, use the default environment */
57 return env_get_char_spec(index);
59 return default_environment[index];
62 uchar env_get_char_memory(int index)
64 return *env_get_addr(index);
67 uchar env_get_char(int index)
69 /* if relocated to RAM */
70 if (gd->flags & GD_FLG_RELOC)
71 return env_get_char_memory(index);
73 return env_get_char_init(index);
76 const uchar *env_get_addr(int index)
79 return (uchar *)(gd->env_addr + index);
81 return &default_environment[index];
85 * Read an environment variable as a boolean
86 * Return -1 if variable does not exist (default to true)
88 int getenv_yesno(const char *var)
90 char *s = getenv(var);
94 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
99 * Look up the variable from the default environment
101 char *getenv_default(const char *name)
104 unsigned long really_valid = gd->env_valid;
105 unsigned long real_gd_flags = gd->flags;
107 /* Pretend that the image is bad. */
108 gd->flags &= ~GD_FLG_ENV_READY;
110 ret_val = getenv(name);
111 gd->env_valid = really_valid;
112 gd->flags = real_gd_flags;
116 void set_default_env(const char *s)
120 if (sizeof(default_environment) > ENV_SIZE) {
121 puts("*** Error - default environment is too large\n\n");
127 printf("*** Warning - %s, "
128 "using default environment\n\n",
131 flags = H_INTERACTIVE;
135 puts("Using default environment\n\n");
138 if (himport_r(&env_htab, (char *)default_environment,
139 sizeof(default_environment), '\0', flags,
141 error("Environment import failed: errno = %d\n", errno);
143 gd->flags |= GD_FLG_ENV_READY;
147 /* [re]set individual variables to their value in the default environment */
148 int set_default_vars(int nvars, char * const vars[])
151 * Special use-case: import from default environment
152 * (and use \0 as a separator)
154 return himport_r(&env_htab, (const char *)default_environment,
155 sizeof(default_environment), '\0',
156 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
159 #ifndef CONFIG_SPL_BUILD
161 * Check if CRC is valid and (if yes) import the environment.
162 * Note that "buf" may or may not be aligned.
164 int env_import(const char *buf, int check)
166 env_t *ep = (env_t *)buf;
171 memcpy(&crc, &ep->crc, sizeof(crc));
173 if (crc32(0, ep->data, ENV_SIZE) != crc) {
174 set_default_env("!bad CRC");
179 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
181 gd->flags |= GD_FLG_ENV_READY;
185 error("Cannot import environment: errno = %d\n", errno);
187 set_default_env("!import failed");
193 void env_relocate(void)
195 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
197 env_htab.change_ok += gd->reloc_off;
199 if (gd->env_valid == 0) {
200 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
201 /* Environment not changable */
202 set_default_env(NULL);
204 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
205 set_default_env("!bad CRC");
212 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
213 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
222 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
223 int vallen = strlen(match->key) + 1;
225 if (found >= maxv - 2 || bufsz < vallen)
229 memcpy(buf, match->key, vallen);
234 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
237 cmdv[found++] = "...";