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 #define MK_STR(x) XMK_STR(x)
43 const uchar default_environment[] = {
44 #ifdef CONFIG_BOOTARGS
45 "bootargs=" CONFIG_BOOTARGS "\0"
47 #ifdef CONFIG_BOOTCOMMAND
48 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
50 #ifdef CONFIG_RAMBOOTCOMMAND
51 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
53 #ifdef CONFIG_NFSBOOTCOMMAND
54 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
56 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
57 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
59 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
60 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
62 #ifdef CONFIG_LOADS_ECHO
63 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
66 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
68 #ifdef CONFIG_ETH1ADDR
69 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
71 #ifdef CONFIG_ETH2ADDR
72 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
74 #ifdef CONFIG_ETH3ADDR
75 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
77 #ifdef CONFIG_ETH4ADDR
78 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
80 #ifdef CONFIG_ETH5ADDR
81 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
83 #ifdef CONFIG_ETHPRIME
84 "ethprime=" CONFIG_ETHPRIME "\0"
87 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
89 #ifdef CONFIG_SERVERIP
90 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
92 #ifdef CONFIG_SYS_AUTOLOAD
93 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
96 "preboot=" CONFIG_PREBOOT "\0"
98 #ifdef CONFIG_ROOTPATH
99 "rootpath=" CONFIG_ROOTPATH "\0"
101 #ifdef CONFIG_GATEWAYIP
102 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
104 #ifdef CONFIG_NETMASK
105 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
107 #ifdef CONFIG_HOSTNAME
108 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
110 #ifdef CONFIG_BOOTFILE
111 "bootfile=" CONFIG_BOOTFILE "\0"
113 #ifdef CONFIG_LOADADDR
114 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
116 #ifdef CONFIG_CLOCKS_IN_MHZ
119 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
120 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
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"
129 #ifdef CONFIG_SYS_SOC
130 "soc=" CONFIG_SYS_SOC "\0"
133 #ifdef CONFIG_EXTRA_ENV_SETTINGS
134 CONFIG_EXTRA_ENV_SETTINGS
139 struct hsearch_data env_htab = {
140 .apply = env_check_apply,
143 static uchar __env_get_char_spec(int index)
145 return *((uchar *)(gd->env_addr + index));
147 uchar env_get_char_spec(int)
148 __attribute__((weak, alias("__env_get_char_spec")));
150 static uchar env_get_char_init(int index)
152 /* if crc was bad, use the default environment */
154 return env_get_char_spec(index);
156 return default_environment[index];
159 uchar env_get_char_memory(int index)
161 return *env_get_addr(index);
164 uchar env_get_char(int index)
166 /* if relocated to RAM */
167 if (gd->flags & GD_FLG_RELOC)
168 return env_get_char_memory(index);
170 return env_get_char_init(index);
173 const uchar *env_get_addr(int index)
176 return (uchar *)(gd->env_addr + index);
178 return &default_environment[index];
181 void set_default_env(const char *s)
184 * By default, do not apply changes as they will eventually
185 * be applied by someone else
188 if (sizeof(default_environment) > ENV_SIZE) {
189 puts("*** Error - default environment is too large\n\n");
195 printf("*** Warning - %s, "
196 "using default environment\n\n",
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).
210 puts("Using default environment\n\n");
213 if (himport_r(&env_htab, (char *)default_environment,
214 sizeof(default_environment), '\0', 0,
215 0, NULL, do_apply) == 0)
216 error("Environment import failed: errno = %d\n", errno);
218 gd->flags |= GD_FLG_ENV_READY;
222 /* [re]set individual variables to their value in the default environment */
223 int set_default_vars(int nvars, char * const vars[])
226 * Special use-case: import from default environment
227 * (and use \0 as a separator)
229 return himport_r(&env_htab, (const char *)default_environment,
230 sizeof(default_environment), '\0', H_NOCLEAR,
231 nvars, vars, 1 /* do_apply */);
235 * Check if CRC is valid and (if yes) import the environment.
236 * Note that "buf" may or may not be aligned.
238 int env_import(const char *buf, int check)
240 env_t *ep = (env_t *)buf;
245 memcpy(&crc, &ep->crc, sizeof(crc));
247 if (crc32(0, ep->data, ENV_SIZE) != crc) {
248 set_default_env("!bad CRC");
253 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
254 0, NULL, 0 /* do_apply */)) {
255 gd->flags |= GD_FLG_ENV_READY;
259 error("Cannot import environment: errno = %d\n", errno);
261 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) /* Environment not changable */
273 set_default_env(NULL);
275 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
276 set_default_env("!bad CRC");
283 #ifdef CONFIG_AUTO_COMPLETE
284 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
293 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
294 int vallen = strlen(match->key) + 1;
296 if (found >= maxv - 2 || bufsz < vallen)
300 memcpy(buf, match->key, vallen);
305 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
308 cmdv[found++] = "...";