2 * (C) Copyright 2000-2002
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
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 #ifdef CONFIG_SHOW_BOOT_PROGRESS
34 # include <status_led.h>
35 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
37 # define SHOW_BOOT_PROGRESS(arg)
40 #ifdef CONFIG_AMIGAONEG3SE
41 extern void enable_nvram(void);
42 extern void disable_nvram(void);
47 #define DEBUGF(fmt,args...) printf(fmt ,##args)
49 #define DEBUGF(fmt,args...)
52 extern env_t *env_ptr;
54 extern void env_relocate_spec (void);
55 extern uchar env_get_char_spec(int);
57 static uchar env_get_char_init (int index);
58 uchar (*env_get_char)(int) = env_get_char_init;
60 /************************************************************************
61 * Default settings to be used when no valid environment is found
64 #define MK_STR(x) XMK_STR(x)
66 uchar default_environment[] = {
67 #ifdef CONFIG_BOOTARGS
68 "bootargs=" CONFIG_BOOTARGS "\0"
70 #ifdef CONFIG_BOOTCOMMAND
71 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
73 #ifdef CONFIG_RAMBOOTCOMMAND
74 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
76 #ifdef CONFIG_NFSBOOTCOMMAND
77 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
79 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
80 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
82 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
83 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
85 #ifdef CONFIG_LOADS_ECHO
86 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
89 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
91 #ifdef CONFIG_ETH1ADDR
92 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
94 #ifdef CONFIG_ETH2ADDR
95 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
97 #ifdef CONFIG_ETH3ADDR
98 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
101 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
103 #ifdef CONFIG_SERVERIP
104 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
107 "autoload=" CFG_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
143 void env_crc_update (void)
145 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
148 static uchar env_get_char_init (int index)
150 DECLARE_GLOBAL_DATA_PTR;
153 /* if crc was bad, use the default environment */
156 c = env_get_char_spec(index);
158 c = default_environment[index];
164 #ifdef CONFIG_AMIGAONEG3SE
165 uchar env_get_char_memory (int index)
167 DECLARE_GLOBAL_DATA_PTR;
171 retval = ( *((uchar *)(gd->env_addr + index)) );
173 retval = ( default_environment[index] );
179 uchar env_get_char_memory (int index)
181 DECLARE_GLOBAL_DATA_PTR;
184 return ( *((uchar *)(gd->env_addr + index)) );
186 return ( default_environment[index] );
191 uchar *env_get_addr (int index)
193 DECLARE_GLOBAL_DATA_PTR;
196 return ( ((uchar *)(gd->env_addr + index)) );
198 return (&default_environment[index]);
202 void env_relocate (void)
204 DECLARE_GLOBAL_DATA_PTR;
206 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
209 #ifdef CONFIG_AMIGAONEG3SE
213 #ifdef ENV_IS_EMBEDDED
215 * The environment buffer is embedded with the text segment,
216 * just relocate the environment pointer
218 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
219 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
222 * We must allocate a buffer for the environment
224 env_ptr = (env_t *)malloc (CFG_ENV_SIZE);
225 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
229 * After relocation to RAM, we can always use the "memory" functions
231 env_get_char = env_get_char_memory;
233 if (gd->env_valid == 0) {
234 #if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */
235 puts ("Using default environment\n\n");
237 puts ("*** Warning - bad CRC, using default environment\n\n");
238 SHOW_BOOT_PROGRESS (-1);
241 if (sizeof(default_environment) > ENV_SIZE)
243 puts ("*** Error - default environment is too large\n\n");
247 memset (env_ptr, 0, sizeof(env_t));
248 memcpy (env_ptr->data,
250 sizeof(default_environment));
251 #ifdef CFG_REDUNDAND_ENVIRONMENT
252 env_ptr->flags = 0xFF;
258 env_relocate_spec ();
260 gd->env_addr = (ulong)&(env_ptr->data);
262 #ifdef CONFIG_AMIGAONEG3SE
267 #ifdef CONFIG_AUTO_COMPLETE
268 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
270 int i, nxt, len, vallen, found;
271 const char *lval, *rval;
277 /* now iterate over the variables and select those that match */
278 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
280 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
283 lval = env_get_addr(i);
284 rval = strchr(lval, '=');
286 vallen = rval - lval;
289 vallen = strlen(lval);
291 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
294 if (found >= maxv - 2 || bufsz < vallen + 1) {
295 cmdv[found++] = "...";
299 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
300 *buf++ = '\0'; bufsz--;