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"
84 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
86 #ifdef CONFIG_SERVERIP
87 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
89 #ifdef CONFIG_SYS_AUTOLOAD
90 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
93 "preboot=" CONFIG_PREBOOT "\0"
95 #ifdef CONFIG_ROOTPATH
96 "rootpath=" CONFIG_ROOTPATH "\0"
98 #ifdef CONFIG_GATEWAYIP
99 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
101 #ifdef CONFIG_NETMASK
102 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
104 #ifdef CONFIG_HOSTNAME
105 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
107 #ifdef CONFIG_BOOTFILE
108 "bootfile=" CONFIG_BOOTFILE "\0"
110 #ifdef CONFIG_LOADADDR
111 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
113 #ifdef CONFIG_CLOCKS_IN_MHZ
116 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
117 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
119 #ifdef CONFIG_EXTRA_ENV_SETTINGS
120 CONFIG_EXTRA_ENV_SETTINGS
125 struct hsearch_data env_htab;
127 static uchar __env_get_char_spec(int index)
129 return *((uchar *)(gd->env_addr + index));
131 uchar env_get_char_spec(int)
132 __attribute__((weak, alias("__env_get_char_spec")));
134 static uchar env_get_char_init(int index)
136 /* if crc was bad, use the default environment */
138 return env_get_char_spec(index);
140 return default_environment[index];
143 uchar env_get_char_memory(int index)
145 return *env_get_addr(index);
148 uchar env_get_char(int index)
150 /* if relocated to RAM */
151 if (gd->flags & GD_FLG_RELOC)
152 return env_get_char_memory(index);
154 return env_get_char_init(index);
157 const uchar *env_get_addr(int index)
160 return (uchar *)(gd->env_addr + index);
162 return &default_environment[index];
165 void set_default_env(const char *s)
167 if (sizeof(default_environment) > ENV_SIZE) {
168 puts("*** Error - default environment is too large\n\n");
174 printf("*** Warning - %s, "
175 "using default environment\n\n",
181 puts("Using default environment\n\n");
184 if (himport_r(&env_htab, (char *)default_environment,
185 sizeof(default_environment), '\0', 0) == 0)
186 error("Environment import failed: errno = %d\n", errno);
188 gd->flags |= GD_FLG_ENV_READY;
192 * Check if CRC is valid and (if yes) import the environment.
193 * Note that "buf" may or may not be aligned.
195 int env_import(const char *buf, int check)
197 env_t *ep = (env_t *)buf;
202 memcpy(&crc, &ep->crc, sizeof(crc));
204 if (crc32(0, ep->data, ENV_SIZE) != crc) {
205 set_default_env("!bad CRC");
210 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0)) {
211 gd->flags |= GD_FLG_ENV_READY;
215 error("Cannot import environment: errno = %d\n", errno);
217 set_default_env("!import failed");
222 void env_relocate(void)
224 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
227 if (gd->env_valid == 0) {
228 #if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
229 set_default_env(NULL);
231 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
232 set_default_env("!bad CRC");
239 #ifdef CONFIG_AUTO_COMPLETE
240 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
249 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
250 int vallen = strlen(match->key) + 1;
252 if (found >= maxv - 2 || bufsz < vallen)
256 memcpy(buf, match->key, vallen);
261 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
264 cmdv[found++] = "...";