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
41 const uchar default_environment[] = {
42 #ifdef CONFIG_BOOTARGS
43 "bootargs=" CONFIG_BOOTARGS "\0"
45 #ifdef CONFIG_BOOTCOMMAND
46 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
48 #ifdef CONFIG_RAMBOOTCOMMAND
49 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
51 #ifdef CONFIG_NFSBOOTCOMMAND
52 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
54 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
55 "bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0"
57 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
58 "baudrate=" __stringify(CONFIG_BAUDRATE) "\0"
60 #ifdef CONFIG_LOADS_ECHO
61 "loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0"
64 "ethaddr=" __stringify(CONFIG_ETHADDR) "\0"
66 #ifdef CONFIG_ETH1ADDR
67 "eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0"
69 #ifdef CONFIG_ETH2ADDR
70 "eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0"
72 #ifdef CONFIG_ETH3ADDR
73 "eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0"
75 #ifdef CONFIG_ETH4ADDR
76 "eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0"
78 #ifdef CONFIG_ETH5ADDR
79 "eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0"
81 #ifdef CONFIG_ETHPRIME
82 "ethprime=" CONFIG_ETHPRIME "\0"
85 "ipaddr=" __stringify(CONFIG_IPADDR) "\0"
87 #ifdef CONFIG_SERVERIP
88 "serverip=" __stringify(CONFIG_SERVERIP) "\0"
90 #ifdef CONFIG_SYS_AUTOLOAD
91 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
94 "preboot=" CONFIG_PREBOOT "\0"
96 #ifdef CONFIG_ROOTPATH
97 "rootpath=" CONFIG_ROOTPATH "\0"
99 #ifdef CONFIG_GATEWAYIP
100 "gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0"
102 #ifdef CONFIG_NETMASK
103 "netmask=" __stringify(CONFIG_NETMASK) "\0"
105 #ifdef CONFIG_HOSTNAME
106 "hostname=" __stringify(CONFIG_HOSTNAME) "\0"
108 #ifdef CONFIG_BOOTFILE
109 "bootfile=" CONFIG_BOOTFILE "\0"
111 #ifdef CONFIG_LOADADDR
112 "loadaddr=" __stringify(CONFIG_LOADADDR) "\0"
114 #ifdef CONFIG_CLOCKS_IN_MHZ
117 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
118 "pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY)"\0"
120 #ifdef CONFIG_ENV_VARS_UBOOT_CONFIG
121 "arch=" CONFIG_SYS_ARCH "\0"
122 "cpu=" CONFIG_SYS_CPU "\0"
123 "board=" CONFIG_SYS_BOARD "\0"
124 #ifdef CONFIG_SYS_VENDOR
125 "vendor=" CONFIG_SYS_VENDOR "\0"
127 #ifdef CONFIG_SYS_SOC
128 "soc=" CONFIG_SYS_SOC "\0"
131 #ifdef CONFIG_EXTRA_ENV_SETTINGS
132 CONFIG_EXTRA_ENV_SETTINGS
137 struct hsearch_data env_htab = {
138 .apply = env_check_apply,
141 static uchar __env_get_char_spec(int index)
143 return *((uchar *)(gd->env_addr + index));
145 uchar env_get_char_spec(int)
146 __attribute__((weak, alias("__env_get_char_spec")));
148 static uchar env_get_char_init(int index)
150 /* if crc was bad, use the default environment */
152 return env_get_char_spec(index);
154 return default_environment[index];
157 uchar env_get_char_memory(int index)
159 return *env_get_addr(index);
162 uchar env_get_char(int index)
164 /* if relocated to RAM */
165 if (gd->flags & GD_FLG_RELOC)
166 return env_get_char_memory(index);
168 return env_get_char_init(index);
171 const uchar *env_get_addr(int index)
174 return (uchar *)(gd->env_addr + index);
176 return &default_environment[index];
179 void set_default_env(const char *s)
182 * By default, do not apply changes as they will eventually
183 * be applied by someone else
186 if (sizeof(default_environment) > ENV_SIZE) {
187 puts("*** Error - default environment is too large\n\n");
193 printf("*** Warning - %s, "
194 "using default environment\n\n",
198 * This set_to_default was explicitly asked for
199 * by the user, as opposed to being a recovery
200 * mechanism. Therefore we check every single
201 * variable and apply changes to the system
202 * right away (e.g. baudrate, console).
208 puts("Using default environment\n\n");
211 if (himport_r(&env_htab, (char *)default_environment,
212 sizeof(default_environment), '\0', 0,
213 0, NULL, do_apply) == 0)
214 error("Environment import failed: errno = %d\n", errno);
216 gd->flags |= GD_FLG_ENV_READY;
220 /* [re]set individual variables to their value in the default environment */
221 int set_default_vars(int nvars, char * const vars[])
224 * Special use-case: import from default environment
225 * (and use \0 as a separator)
227 return himport_r(&env_htab, (const char *)default_environment,
228 sizeof(default_environment), '\0', H_NOCLEAR,
229 nvars, vars, 1 /* do_apply */);
232 #ifndef CONFIG_SPL_BUILD
234 * Check if CRC is valid and (if yes) import the environment.
235 * Note that "buf" may or may not be aligned.
237 int env_import(const char *buf, int check)
239 env_t *ep = (env_t *)buf;
244 memcpy(&crc, &ep->crc, sizeof(crc));
246 if (crc32(0, ep->data, ENV_SIZE) != crc) {
247 set_default_env("!bad CRC");
252 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
253 0, NULL, 0 /* do_apply */)) {
254 gd->flags |= GD_FLG_ENV_READY;
258 error("Cannot import environment: errno = %d\n", errno);
260 set_default_env("!import failed");
266 void env_relocate(void)
268 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
271 if (gd->env_valid == 0) {
272 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
273 /* Environment not changable */
274 set_default_env(NULL);
276 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
277 set_default_env("!bad CRC");
284 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
285 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
294 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
295 int vallen = strlen(match->key) + 1;
297 if (found >= maxv - 2 || bufsz < vallen)
301 memcpy(buf, match->key, vallen);
306 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
309 cmdv[found++] = "...";