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;
35 #ifdef CONFIG_AMIGAONEG3SE
36 extern void enable_nvram(void);
37 extern void disable_nvram(void);
42 #define DEBUGF(fmt,args...) printf(fmt ,##args)
44 #define DEBUGF(fmt,args...)
47 extern env_t *env_ptr;
49 extern void env_relocate_spec (void);
50 extern uchar env_get_char_spec(int);
52 static uchar env_get_char_init (int index);
54 /************************************************************************
55 * Default settings to be used when no valid environment is found
58 #define MK_STR(x) XMK_STR(x)
60 uchar default_environment[] = {
61 #ifdef CONFIG_BOOTARGS
62 "bootargs=" CONFIG_BOOTARGS "\0"
64 #ifdef CONFIG_BOOTCOMMAND
65 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
67 #ifdef CONFIG_RAMBOOTCOMMAND
68 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
70 #ifdef CONFIG_NFSBOOTCOMMAND
71 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
73 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
74 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
76 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
77 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
79 #ifdef CONFIG_LOADS_ECHO
80 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
83 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
85 #ifdef CONFIG_ETH1ADDR
86 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
88 #ifdef CONFIG_ETH2ADDR
89 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
91 #ifdef CONFIG_ETH3ADDR
92 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
94 #ifdef CONFIG_ETH4ADDR
95 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
97 #ifdef CONFIG_ETH5ADDR
98 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
101 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
103 #ifdef CONFIG_SERVERIP
104 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
106 #ifdef CONFIG_SYS_AUTOLOAD
107 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
109 #ifdef CONFIG_PREBOOT
110 "preboot=" CONFIG_PREBOOT "\0"
112 #ifdef CONFIG_ROOTPATH
113 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
115 #ifdef CONFIG_GATEWAYIP
116 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
118 #ifdef CONFIG_NETMASK
119 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
121 #ifdef CONFIG_HOSTNAME
122 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
124 #ifdef CONFIG_BOOTFILE
125 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
127 #ifdef CONFIG_LOADADDR
128 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
130 #ifdef CONFIG_CLOCKS_IN_MHZ
133 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
134 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
136 #ifdef CONFIG_EXTRA_ENV_SETTINGS
137 CONFIG_EXTRA_ENV_SETTINGS
142 #if defined(CONFIG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */ \
143 || defined(CONFIG_ENV_IS_IN_SPI_FLASH)
144 int default_environment_size = sizeof(default_environment);
147 void env_crc_update (void)
149 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
152 static uchar env_get_char_init (int index)
156 /* if crc was bad, use the default environment */
159 c = env_get_char_spec(index);
161 c = default_environment[index];
167 #ifdef CONFIG_AMIGAONEG3SE
168 uchar env_get_char_memory (int index)
173 retval = ( *((uchar *)(gd->env_addr + index)) );
175 retval = ( default_environment[index] );
181 uchar env_get_char_memory (int index)
184 return ( *((uchar *)(gd->env_addr + index)) );
186 return ( default_environment[index] );
191 uchar env_get_char (int index)
195 /* if relocated to RAM */
196 if (gd->flags & GD_FLG_RELOC)
197 c = env_get_char_memory(index);
199 c = env_get_char_init(index);
204 uchar *env_get_addr (int index)
207 return ( ((uchar *)(gd->env_addr + index)) );
209 return (&default_environment[index]);
213 void set_default_env(void)
215 if (sizeof(default_environment) > ENV_SIZE) {
216 puts ("*** Error - default environment is too large\n\n");
220 memset(env_ptr, 0, sizeof(env_t));
221 memcpy(env_ptr->data, default_environment,
222 sizeof(default_environment));
223 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
224 env_ptr->flags = 0xFF;
230 void env_relocate (void)
232 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
235 #ifdef CONFIG_AMIGAONEG3SE
239 #ifdef ENV_IS_EMBEDDED
241 * The environment buffer is embedded with the text segment,
242 * just relocate the environment pointer
244 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
245 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
248 * We must allocate a buffer for the environment
250 env_ptr = (env_t *)malloc (CONFIG_ENV_SIZE);
251 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
254 if (gd->env_valid == 0) {
255 #if defined(CONFIG_GTH) || defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
256 puts ("Using default environment\n\n");
258 puts ("*** Warning - bad CRC, using default environment\n\n");
259 show_boot_progress (-60);
264 env_relocate_spec ();
266 gd->env_addr = (ulong)&(env_ptr->data);
268 #ifdef CONFIG_AMIGAONEG3SE
273 #ifdef CONFIG_AUTO_COMPLETE
274 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
276 int i, nxt, len, vallen, found;
277 const char *lval, *rval;
283 /* now iterate over the variables and select those that match */
284 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
286 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
289 lval = (char *)env_get_addr(i);
290 rval = strchr(lval, '=');
292 vallen = rval - lval;
295 vallen = strlen(lval);
297 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
300 if (found >= maxv - 2 || bufsz < vallen + 1) {
301 cmdv[found++] = "...";
305 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
306 *buf++ = '\0'; bufsz--;