2 * (C) Copyright 2000-2002
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>
33 DECLARE_GLOBAL_DATA_PTR;
37 #define DEBUGF(fmt,args...) printf(fmt ,##args)
39 #define DEBUGF(fmt,args...)
42 extern env_t *env_ptr;
44 extern void env_relocate_spec (void);
45 extern uchar env_get_char_spec(int);
47 static uchar env_get_char_init (int index);
49 /************************************************************************
50 * Default settings to be used when no valid environment is found
53 #define MK_STR(x) XMK_STR(x)
55 uchar default_environment[] = {
56 #ifdef CONFIG_BOOTARGS
57 "bootargs=" CONFIG_BOOTARGS "\0"
59 #ifdef CONFIG_BOOTCOMMAND
60 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
62 #ifdef CONFIG_RAMBOOTCOMMAND
63 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
65 #ifdef CONFIG_NFSBOOTCOMMAND
66 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
68 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
69 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
71 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
72 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
74 #ifdef CONFIG_LOADS_ECHO
75 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
78 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
80 #ifdef CONFIG_ETH1ADDR
81 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
83 #ifdef CONFIG_ETH2ADDR
84 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
86 #ifdef CONFIG_ETH3ADDR
87 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
89 #ifdef CONFIG_ETH4ADDR
90 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
92 #ifdef CONFIG_ETH5ADDR
93 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
96 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
98 #ifdef CONFIG_SERVERIP
99 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
101 #ifdef CONFIG_SYS_AUTOLOAD
102 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
104 #ifdef CONFIG_PREBOOT
105 "preboot=" CONFIG_PREBOOT "\0"
107 #ifdef CONFIG_ROOTPATH
108 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
110 #ifdef CONFIG_GATEWAYIP
111 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
113 #ifdef CONFIG_NETMASK
114 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
116 #ifdef CONFIG_HOSTNAME
117 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
119 #ifdef CONFIG_BOOTFILE
120 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
122 #ifdef CONFIG_LOADADDR
123 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
125 #ifdef CONFIG_CLOCKS_IN_MHZ
128 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
129 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
131 #ifdef CONFIG_EXTRA_ENV_SETTINGS
132 CONFIG_EXTRA_ENV_SETTINGS
137 void env_crc_update (void)
139 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
142 static uchar env_get_char_init (int index)
146 /* if crc was bad, use the default environment */
149 c = env_get_char_spec(index);
151 c = default_environment[index];
157 uchar env_get_char_memory (int index)
160 return ( *((uchar *)(gd->env_addr + index)) );
162 return ( default_environment[index] );
166 uchar env_get_char (int index)
170 /* if relocated to RAM */
171 if (gd->flags & GD_FLG_RELOC)
172 c = env_get_char_memory(index);
174 c = env_get_char_init(index);
179 uchar *env_get_addr (int index)
182 return ( ((uchar *)(gd->env_addr + index)) );
184 return (&default_environment[index]);
188 void set_default_env(void)
190 if (sizeof(default_environment) > ENV_SIZE) {
191 puts ("*** Error - default environment is too large\n\n");
195 memset(env_ptr, 0, sizeof(env_t));
196 memcpy(env_ptr->data, default_environment,
197 sizeof(default_environment));
198 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
199 env_ptr->flags = 0xFF;
205 void env_relocate (void)
207 #ifndef CONFIG_RELOC_FIXUP_WORKS
208 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
212 #ifdef ENV_IS_EMBEDDED
214 * The environment buffer is embedded with the text segment,
215 * just relocate the environment pointer
217 #ifndef CONFIG_RELOC_FIXUP_WORKS
218 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
220 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
223 * We must allocate a buffer for the environment
225 env_ptr = (env_t *)malloc (CONFIG_ENV_SIZE);
226 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
229 if (gd->env_valid == 0) {
230 #if defined(CONFIG_GTH) || defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
231 puts ("Using default environment\n\n");
233 puts ("*** Warning - bad CRC, using default environment\n\n");
234 show_boot_progress (-60);
239 env_relocate_spec ();
241 gd->env_addr = (ulong)&(env_ptr->data);
244 #ifdef CONFIG_AUTO_COMPLETE
245 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
247 int i, nxt, len, vallen, found;
248 const char *lval, *rval;
254 /* now iterate over the variables and select those that match */
255 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
257 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
260 lval = (char *)env_get_addr(i);
261 rval = strchr(lval, '=');
263 vallen = rval - lval;
266 vallen = strlen(lval);
268 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
271 if (found >= maxv - 2 || bufsz < vallen + 1) {
272 cmdv[found++] = "...";
276 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
277 *buf++ = '\0'; bufsz--;