]>
Commit | Line | Data |
---|---|---|
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 |
35 | DECLARE_GLOBAL_DATA_PTR; |
36 | ||
05706fdd WD |
37 | /************************************************************************ |
38 | * Default settings to be used when no valid environment is found | |
39 | */ | |
40 | #define XMK_STR(x) #x | |
41 | #define MK_STR(x) XMK_STR(x) | |
42 | ||
147c7169 | 43 | const uchar default_environment[] = { |
05706fdd WD |
44 | #ifdef CONFIG_BOOTARGS |
45 | "bootargs=" CONFIG_BOOTARGS "\0" | |
46 | #endif | |
47 | #ifdef CONFIG_BOOTCOMMAND | |
48 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" | |
49 | #endif | |
50 | #ifdef CONFIG_RAMBOOTCOMMAND | |
51 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" | |
52 | #endif | |
53 | #ifdef CONFIG_NFSBOOTCOMMAND | |
54 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" | |
55 | #endif | |
56 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) | |
57 | "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0" | |
58 | #endif | |
59 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) | |
60 | "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0" | |
61 | #endif | |
62 | #ifdef CONFIG_LOADS_ECHO | |
63 | "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0" | |
64 | #endif | |
65 | #ifdef CONFIG_ETHADDR | |
66 | "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0" | |
67 | #endif | |
68 | #ifdef CONFIG_ETH1ADDR | |
69 | "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0" | |
70 | #endif | |
71 | #ifdef CONFIG_ETH2ADDR | |
72 | "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0" | |
73 | #endif | |
69459791 WD |
74 | #ifdef CONFIG_ETH3ADDR |
75 | "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0" | |
76 | #endif | |
c68a05fe | 77 | #ifdef CONFIG_ETH4ADDR |
78 | "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0" | |
79 | #endif | |
80 | #ifdef CONFIG_ETH5ADDR | |
81 | "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0" | |
82 | #endif | |
84209afb BT |
83 | #ifdef CONFIG_ETHPRIME |
84 | "ethprime=" CONFIG_ETHPRIME "\0" | |
85 | #endif | |
05706fdd WD |
86 | #ifdef CONFIG_IPADDR |
87 | "ipaddr=" MK_STR(CONFIG_IPADDR) "\0" | |
88 | #endif | |
89 | #ifdef CONFIG_SERVERIP | |
90 | "serverip=" MK_STR(CONFIG_SERVERIP) "\0" | |
91 | #endif | |
6d0f6bcf | 92 | #ifdef CONFIG_SYS_AUTOLOAD |
27aafe98 | 93 | "autoload=" CONFIG_SYS_AUTOLOAD "\0" |
05706fdd WD |
94 | #endif |
95 | #ifdef CONFIG_PREBOOT | |
96 | "preboot=" CONFIG_PREBOOT "\0" | |
97 | #endif | |
98 | #ifdef CONFIG_ROOTPATH | |
8b3637c6 | 99 | "rootpath=" CONFIG_ROOTPATH "\0" |
05706fdd WD |
100 | #endif |
101 | #ifdef CONFIG_GATEWAYIP | |
102 | "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0" | |
103 | #endif | |
104 | #ifdef CONFIG_NETMASK | |
105 | "netmask=" MK_STR(CONFIG_NETMASK) "\0" | |
106 | #endif | |
107 | #ifdef CONFIG_HOSTNAME | |
108 | "hostname=" MK_STR(CONFIG_HOSTNAME) "\0" | |
109 | #endif | |
110 | #ifdef CONFIG_BOOTFILE | |
b3f44c21 | 111 | "bootfile=" CONFIG_BOOTFILE "\0" |
05706fdd WD |
112 | #endif |
113 | #ifdef CONFIG_LOADADDR | |
114 | "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0" | |
115 | #endif | |
27aafe98 | 116 | #ifdef CONFIG_CLOCKS_IN_MHZ |
05706fdd WD |
117 | "clocks_in_mhz=1\0" |
118 | #endif | |
ad10dd9a SR |
119 | #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) |
120 | "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0" | |
121 | #endif | |
5e724ca2 SW |
122 | #ifdef CONFIG_ENV_VARS_UBOOT_CONFIG |
123 | "arch=" CONFIG_SYS_ARCH "\0" | |
124 | "cpu=" CONFIG_SYS_CPU "\0" | |
125 | "board=" CONFIG_SYS_BOARD "\0" | |
126 | #ifdef CONFIG_SYS_VENDOR | |
127 | "vendor=" CONFIG_SYS_VENDOR "\0" | |
128 | #endif | |
129 | #ifdef CONFIG_SYS_SOC | |
130 | "soc=" CONFIG_SYS_SOC "\0" | |
131 | #endif | |
132 | #endif | |
27aafe98 | 133 | #ifdef CONFIG_EXTRA_ENV_SETTINGS |
05706fdd WD |
134 | CONFIG_EXTRA_ENV_SETTINGS |
135 | #endif | |
136 | "\0" | |
137 | }; | |
138 | ||
c5983592 GF |
139 | struct hsearch_data env_htab = { |
140 | .apply = env_check_apply, | |
141 | }; | |
2eb1573f | 142 | |
bf95df44 IG |
143 | static uchar __env_get_char_spec(int index) |
144 | { | |
145 | return *((uchar *)(gd->env_addr + index)); | |
146 | } | |
147 | uchar env_get_char_spec(int) | |
148 | __attribute__((weak, alias("__env_get_char_spec"))); | |
149 | ||
27aafe98 | 150 | static uchar env_get_char_init(int index) |
05706fdd | 151 | { |
05706fdd WD |
152 | /* if crc was bad, use the default environment */ |
153 | if (gd->env_valid) | |
27aafe98 | 154 | return env_get_char_spec(index); |
ea882baf | 155 | else |
27aafe98 | 156 | return default_environment[index]; |
05706fdd WD |
157 | } |
158 | ||
27aafe98 | 159 | uchar env_get_char_memory(int index) |
05706fdd | 160 | { |
ea882baf | 161 | return *env_get_addr(index); |
05706fdd WD |
162 | } |
163 | ||
27aafe98 | 164 | uchar env_get_char(int index) |
b502611b | 165 | { |
b502611b JT |
166 | /* if relocated to RAM */ |
167 | if (gd->flags & GD_FLG_RELOC) | |
27aafe98 | 168 | return env_get_char_memory(index); |
b502611b | 169 | else |
27aafe98 | 170 | return env_get_char_init(index); |
b502611b JT |
171 | } |
172 | ||
27aafe98 | 173 | const uchar *env_get_addr(int index) |
05706fdd | 174 | { |
ea882baf WD |
175 | if (gd->env_valid) |
176 | return (uchar *)(gd->env_addr + index); | |
177 | else | |
178 | return &default_environment[index]; | |
05706fdd WD |
179 | } |
180 | ||
ea882baf | 181 | void set_default_env(const char *s) |
5bb12dbd | 182 | { |
b64b7c3d GF |
183 | /* |
184 | * By default, do not apply changes as they will eventually | |
185 | * be applied by someone else | |
186 | */ | |
187 | int do_apply = 0; | |
5bb12dbd | 188 | if (sizeof(default_environment) > ENV_SIZE) { |
ea882baf | 189 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
190 | return; |
191 | } | |
192 | ||
ea882baf WD |
193 | if (s) { |
194 | if (*s == '!') { | |
195 | printf("*** Warning - %s, " | |
196 | "using default environment\n\n", | |
27aafe98 | 197 | s + 1); |
ea882baf | 198 | } else { |
b64b7c3d GF |
199 | /* |
200 | * This set_to_default was explicitly asked for | |
201 | * by the user, as opposed to being a recovery | |
202 | * mechanism. Therefore we check every single | |
203 | * variable and apply changes to the system | |
204 | * right away (e.g. baudrate, console). | |
205 | */ | |
206 | do_apply = 1; | |
ea882baf WD |
207 | puts(s); |
208 | } | |
209 | } else { | |
210 | puts("Using default environment\n\n"); | |
211 | } | |
212 | ||
2eb1573f | 213 | if (himport_r(&env_htab, (char *)default_environment, |
348b1f1c | 214 | sizeof(default_environment), '\0', 0, |
b64b7c3d | 215 | 0, NULL, do_apply) == 0) |
ea882baf | 216 | error("Environment import failed: errno = %d\n", errno); |
27aafe98 | 217 | |
ea882baf | 218 | gd->flags |= GD_FLG_ENV_READY; |
5bb12dbd HW |
219 | } |
220 | ||
b64b7c3d GF |
221 | |
222 | /* [re]set individual variables to their value in the default environment */ | |
223 | int set_default_vars(int nvars, char * const vars[]) | |
224 | { | |
225 | /* | |
226 | * Special use-case: import from default environment | |
227 | * (and use \0 as a separator) | |
228 | */ | |
229 | return himport_r(&env_htab, (const char *)default_environment, | |
230 | sizeof(default_environment), '\0', H_NOCLEAR, | |
231 | nvars, vars, 1 /* do_apply */); | |
232 | } | |
233 | ||
ea882baf WD |
234 | /* |
235 | * Check if CRC is valid and (if yes) import the environment. | |
236 | * Note that "buf" may or may not be aligned. | |
237 | */ | |
238 | int env_import(const char *buf, int check) | |
05706fdd | 239 | { |
ea882baf | 240 | env_t *ep = (env_t *)buf; |
05706fdd | 241 | |
ea882baf WD |
242 | if (check) { |
243 | uint32_t crc; | |
244 | ||
245 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
246 | ||
247 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
248 | set_default_env("!bad CRC"); | |
249 | return 0; | |
250 | } | |
251 | } | |
252 | ||
348b1f1c | 253 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, |
c5983592 | 254 | 0, NULL, 0 /* do_apply */)) { |
ea882baf WD |
255 | gd->flags |= GD_FLG_ENV_READY; |
256 | return 1; | |
257 | } | |
05706fdd | 258 | |
ea882baf WD |
259 | error("Cannot import environment: errno = %d\n", errno); |
260 | ||
261 | set_default_env("!import failed"); | |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
27aafe98 | 266 | void env_relocate(void) |
ea882baf | 267 | { |
2e5167cc | 268 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f HS |
269 | env_reloc(); |
270 | #endif | |
05706fdd | 271 | if (gd->env_valid == 0) { |
0fe247b9 | 272 | #if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */ |
ea882baf | 273 | set_default_env(NULL); |
05706fdd | 274 | #else |
770605e4 | 275 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
ea882baf | 276 | set_default_env("!bad CRC"); |
d259079d | 277 | #endif |
ea882baf | 278 | } else { |
27aafe98 | 279 | env_relocate_spec(); |
05706fdd | 280 | } |
05706fdd | 281 | } |
04a85b3b | 282 | |
560d424b | 283 | #ifdef CONFIG_AUTO_COMPLETE |
04a85b3b WD |
284 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
285 | { | |
560d424b MF |
286 | ENTRY *match; |
287 | int found, idx; | |
04a85b3b | 288 | |
560d424b | 289 | idx = 0; |
04a85b3b WD |
290 | found = 0; |
291 | cmdv[0] = NULL; | |
292 | ||
560d424b MF |
293 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
294 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 295 | |
560d424b | 296 | if (found >= maxv - 2 || bufsz < vallen) |
04a85b3b | 297 | break; |
560d424b | 298 | |
04a85b3b | 299 | cmdv[found++] = buf; |
560d424b MF |
300 | memcpy(buf, match->key, vallen); |
301 | buf += vallen; | |
302 | bufsz -= vallen; | |
04a85b3b WD |
303 | } |
304 | ||
560d424b MF |
305 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
306 | ||
307 | if (idx) | |
308 | cmdv[found++] = "..."; | |
27aafe98 | 309 | |
04a85b3b WD |
310 | cmdv[found] = NULL; |
311 | return found; | |
312 | } | |
313 | #endif |