]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_common.c
Merge branch 'master' of git://www.denx.de/git/u-boot-mmc
[people/ms/u-boot.git] / common / env_common.c
CommitLineData
05706fdd 1/*
ea882baf 2 * (C) Copyright 2000-2010
05706fdd
WD
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
ea882baf 7 *
05706fdd
WD
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <command.h>
29#include <environment.h>
05706fdd 30#include <linux/stddef.h>
ea882baf
WD
31#include <search.h>
32#include <errno.h>
05706fdd
WD
33#include <malloc.h>
34
d87080b7
WD
35DECLARE_GLOBAL_DATA_PTR;
36
05706fdd
WD
37/************************************************************************
38 * Default settings to be used when no valid environment is found
39 */
ddd8418f 40#include <env_default.h>
05706fdd 41
c5983592 42struct hsearch_data env_htab = {
2598090b 43 .change_ok = env_flags_validate,
c5983592 44};
2eb1573f 45
bf95df44
IG
46static uchar __env_get_char_spec(int index)
47{
48 return *((uchar *)(gd->env_addr + index));
49}
50uchar env_get_char_spec(int)
51 __attribute__((weak, alias("__env_get_char_spec")));
52
27aafe98 53static uchar env_get_char_init(int index)
05706fdd 54{
05706fdd
WD
55 /* if crc was bad, use the default environment */
56 if (gd->env_valid)
27aafe98 57 return env_get_char_spec(index);
ea882baf 58 else
27aafe98 59 return default_environment[index];
05706fdd
WD
60}
61
27aafe98 62uchar env_get_char_memory(int index)
05706fdd 63{
ea882baf 64 return *env_get_addr(index);
05706fdd
WD
65}
66
27aafe98 67uchar env_get_char(int index)
b502611b 68{
b502611b
JT
69 /* if relocated to RAM */
70 if (gd->flags & GD_FLG_RELOC)
27aafe98 71 return env_get_char_memory(index);
b502611b 72 else
27aafe98 73 return env_get_char_init(index);
b502611b
JT
74}
75
27aafe98 76const uchar *env_get_addr(int index)
05706fdd 77{
ea882baf
WD
78 if (gd->env_valid)
79 return (uchar *)(gd->env_addr + index);
80 else
81 return &default_environment[index];
05706fdd
WD
82}
83
ec8a252c
JH
84/*
85 * Read an environment variable as a boolean
86 * Return -1 if variable does not exist (default to true)
87 */
88int getenv_yesno(const char *var)
89{
90 char *s = getenv(var);
91
92 if (s == NULL)
93 return -1;
94 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
95 1 : 0;
96}
97
267541f7
JH
98/*
99 * Look up the variable from the default environment
100 */
101char *getenv_default(const char *name)
102{
103 char *ret_val;
104 unsigned long really_valid = gd->env_valid;
105 unsigned long real_gd_flags = gd->flags;
106
107 /* Pretend that the image is bad. */
108 gd->flags &= ~GD_FLG_ENV_READY;
109 gd->env_valid = 0;
110 ret_val = getenv(name);
111 gd->env_valid = really_valid;
112 gd->flags = real_gd_flags;
113 return ret_val;
114}
115
ea882baf 116void set_default_env(const char *s)
5bb12dbd 117{
c4e0057f
JH
118 int flags = 0;
119
5bb12dbd 120 if (sizeof(default_environment) > ENV_SIZE) {
ea882baf 121 puts("*** Error - default environment is too large\n\n");
5bb12dbd
HW
122 return;
123 }
124
ea882baf
WD
125 if (s) {
126 if (*s == '!') {
127 printf("*** Warning - %s, "
128 "using default environment\n\n",
27aafe98 129 s + 1);
ea882baf 130 } else {
c4e0057f 131 flags = H_INTERACTIVE;
ea882baf
WD
132 puts(s);
133 }
134 } else {
135 puts("Using default environment\n\n");
136 }
137
2eb1573f 138 if (himport_r(&env_htab, (char *)default_environment,
c4e0057f
JH
139 sizeof(default_environment), '\0', flags,
140 0, NULL) == 0)
ea882baf 141 error("Environment import failed: errno = %d\n", errno);
27aafe98 142
ea882baf 143 gd->flags |= GD_FLG_ENV_READY;
5bb12dbd
HW
144}
145
b64b7c3d
GF
146
147/* [re]set individual variables to their value in the default environment */
148int set_default_vars(int nvars, char * const vars[])
149{
150 /*
151 * Special use-case: import from default environment
152 * (and use \0 as a separator)
153 */
154 return himport_r(&env_htab, (const char *)default_environment,
c4e0057f
JH
155 sizeof(default_environment), '\0',
156 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
b64b7c3d
GF
157}
158
7ac2fe2d 159#ifndef CONFIG_SPL_BUILD
ea882baf
WD
160/*
161 * Check if CRC is valid and (if yes) import the environment.
162 * Note that "buf" may or may not be aligned.
163 */
164int env_import(const char *buf, int check)
05706fdd 165{
ea882baf 166 env_t *ep = (env_t *)buf;
05706fdd 167
ea882baf
WD
168 if (check) {
169 uint32_t crc;
170
171 memcpy(&crc, &ep->crc, sizeof(crc));
172
173 if (crc32(0, ep->data, ENV_SIZE) != crc) {
174 set_default_env("!bad CRC");
175 return 0;
176 }
177 }
178
348b1f1c 179 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
c4e0057f 180 0, NULL)) {
ea882baf
WD
181 gd->flags |= GD_FLG_ENV_READY;
182 return 1;
183 }
05706fdd 184
ea882baf
WD
185 error("Cannot import environment: errno = %d\n", errno);
186
187 set_default_env("!import failed");
188
189 return 0;
190}
7ac2fe2d 191#endif
ea882baf 192
27aafe98 193void env_relocate(void)
ea882baf 194{
2e5167cc 195#if defined(CONFIG_NEEDS_MANUAL_RELOC)
60f7da1f 196 env_reloc();
7afcf3a5 197 env_htab.change_ok += gd->reloc_off;
60f7da1f 198#endif
05706fdd 199 if (gd->env_valid == 0) {
7ac2fe2d
IY
200#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
201 /* Environment not changable */
ea882baf 202 set_default_env(NULL);
05706fdd 203#else
770605e4 204 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
ea882baf 205 set_default_env("!bad CRC");
d259079d 206#endif
ea882baf 207 } else {
27aafe98 208 env_relocate_spec();
05706fdd 209 }
05706fdd 210}
04a85b3b 211
7ac2fe2d 212#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
04a85b3b
WD
213int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
214{
560d424b
MF
215 ENTRY *match;
216 int found, idx;
04a85b3b 217
560d424b 218 idx = 0;
04a85b3b
WD
219 found = 0;
220 cmdv[0] = NULL;
221
560d424b
MF
222 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
223 int vallen = strlen(match->key) + 1;
04a85b3b 224
560d424b 225 if (found >= maxv - 2 || bufsz < vallen)
04a85b3b 226 break;
560d424b 227
04a85b3b 228 cmdv[found++] = buf;
560d424b
MF
229 memcpy(buf, match->key, vallen);
230 buf += vallen;
231 bufsz -= vallen;
04a85b3b
WD
232 }
233
560d424b
MF
234 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
235
236 if (idx)
237 cmdv[found++] = "...";
27aafe98 238
04a85b3b
WD
239 cmdv[found] = NULL;
240 return found;
241}
242#endif