blob: 3201135ea2eddbe7b46c427c7f1deef21d9d7d32 [file] [log] [blame]
wdenk05706fd2002-09-28 17:48:27 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <command.h>
29#include <environment.h>
wdenk05706fd2002-09-28 17:48:27 +000030#include <linux/stddef.h>
31#include <malloc.h>
32
33#ifdef CONFIG_SHOW_BOOT_PROGRESS
34# include <status_led.h>
35# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
36#else
37# define SHOW_BOOT_PROGRESS(arg)
38#endif
39
wdenk452cfd62002-11-19 11:04:11 +000040#ifdef CONFIG_AMIGAONEG3SE
41 extern void enable_nvram(void);
42 extern void disable_nvram(void);
43#endif
44
wdenk05706fd2002-09-28 17:48:27 +000045#undef DEBUG_ENV
46#ifdef DEBUG_ENV
47#define DEBUGF(fmt,args...) printf(fmt ,##args)
48#else
49#define DEBUGF(fmt,args...)
50#endif
51
52extern env_t *env_ptr;
53
54extern void env_relocate_spec (void);
55extern uchar env_get_char_spec(int);
56
57static uchar env_get_char_init (int index);
58uchar (*env_get_char)(int) = env_get_char_init;
59
60/************************************************************************
61 * Default settings to be used when no valid environment is found
62 */
63#define XMK_STR(x) #x
64#define MK_STR(x) XMK_STR(x)
65
66uchar default_environment[] = {
67#ifdef CONFIG_BOOTARGS
68 "bootargs=" CONFIG_BOOTARGS "\0"
69#endif
70#ifdef CONFIG_BOOTCOMMAND
71 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
72#endif
73#ifdef CONFIG_RAMBOOTCOMMAND
74 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
75#endif
76#ifdef CONFIG_NFSBOOTCOMMAND
77 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
78#endif
79#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
80 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
81#endif
82#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
83 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
84#endif
85#ifdef CONFIG_LOADS_ECHO
86 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
87#endif
88#ifdef CONFIG_ETHADDR
89 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
90#endif
91#ifdef CONFIG_ETH1ADDR
92 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
93#endif
94#ifdef CONFIG_ETH2ADDR
95 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
96#endif
wdenkcd914452004-05-29 16:53:29 +000097#ifdef CONFIG_ETH3ADDR
98 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
99#endif
wdenk05706fd2002-09-28 17:48:27 +0000100#ifdef CONFIG_IPADDR
101 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
102#endif
103#ifdef CONFIG_SERVERIP
104 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
105#endif
106#ifdef CFG_AUTOLOAD
107 "autoload=" CFG_AUTOLOAD "\0"
108#endif
109#ifdef CONFIG_PREBOOT
110 "preboot=" CONFIG_PREBOOT "\0"
111#endif
112#ifdef CONFIG_ROOTPATH
113 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
114#endif
115#ifdef CONFIG_GATEWAYIP
116 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
117#endif
118#ifdef CONFIG_NETMASK
119 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
120#endif
121#ifdef CONFIG_HOSTNAME
122 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
123#endif
124#ifdef CONFIG_BOOTFILE
125 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
126#endif
127#ifdef CONFIG_LOADADDR
128 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
129#endif
130#ifdef CONFIG_CLOCKS_IN_MHZ
131 "clocks_in_mhz=1\0"
132#endif
stroesef5dd4102003-02-14 11:21:23 +0000133#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
134 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
135#endif
wdenk05706fd2002-09-28 17:48:27 +0000136#ifdef CONFIG_EXTRA_ENV_SETTINGS
137 CONFIG_EXTRA_ENV_SETTINGS
138#endif
139 "\0"
140};
141
wdenk79b59372004-06-09 14:58:14 +0000142#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
143int default_environment_size = sizeof(default_environment);
144#endif
wdenk05706fd2002-09-28 17:48:27 +0000145
146void env_crc_update (void)
147{
148 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
149}
150
151static uchar env_get_char_init (int index)
152{
153 DECLARE_GLOBAL_DATA_PTR;
154 uchar c;
155
156 /* if crc was bad, use the default environment */
157 if (gd->env_valid)
158 {
159 c = env_get_char_spec(index);
160 } else {
161 c = default_environment[index];
162 }
163
164 return (c);
165}
166
wdenkbb444c92002-12-07 00:20:59 +0000167#ifdef CONFIG_AMIGAONEG3SE
wdenk05706fd2002-09-28 17:48:27 +0000168uchar env_get_char_memory (int index)
169{
170 DECLARE_GLOBAL_DATA_PTR;
wdenkbb444c92002-12-07 00:20:59 +0000171 uchar retval;
172 enable_nvram();
173 if (gd->env_valid) {
174 retval = ( *((uchar *)(gd->env_addr + index)) );
175 } else {
176 retval = ( default_environment[index] );
177 }
178 disable_nvram();
179 return retval;
180}
181#else
182uchar env_get_char_memory (int index)
183{
184 DECLARE_GLOBAL_DATA_PTR;
wdenk05706fd2002-09-28 17:48:27 +0000185
186 if (gd->env_valid) {
187 return ( *((uchar *)(gd->env_addr + index)) );
188 } else {
189 return ( default_environment[index] );
190 }
191}
wdenkbb444c92002-12-07 00:20:59 +0000192#endif
wdenk05706fd2002-09-28 17:48:27 +0000193
194uchar *env_get_addr (int index)
195{
196 DECLARE_GLOBAL_DATA_PTR;
197
198 if (gd->env_valid) {
199 return ( ((uchar *)(gd->env_addr + index)) );
200 } else {
201 return (&default_environment[index]);
202 }
203}
204
205void env_relocate (void)
206{
207 DECLARE_GLOBAL_DATA_PTR;
208
209 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
210 gd->reloc_off);
211
wdenk452cfd62002-11-19 11:04:11 +0000212#ifdef CONFIG_AMIGAONEG3SE
213 enable_nvram();
214#endif
215
wdenk05706fd2002-09-28 17:48:27 +0000216#ifdef ENV_IS_EMBEDDED
217 /*
218 * The environment buffer is embedded with the text segment,
219 * just relocate the environment pointer
220 */
221 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
222 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
223#else
224 /*
225 * We must allocate a buffer for the environment
226 */
227 env_ptr = (env_t *)malloc (CFG_ENV_SIZE);
228 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
229#endif
230
231 /*
232 * After relocation to RAM, we can always use the "memory" functions
233 */
234 env_get_char = env_get_char_memory;
235
236 if (gd->env_valid == 0) {
237#if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */
238 puts ("Using default environment\n\n");
239#else
240 puts ("*** Warning - bad CRC, using default environment\n\n");
241 SHOW_BOOT_PROGRESS (-1);
242#endif
243
244 if (sizeof(default_environment) > ENV_SIZE)
245 {
246 puts ("*** Error - default environment is too large\n\n");
247 return;
248 }
249
250 memset (env_ptr, 0, sizeof(env_t));
251 memcpy (env_ptr->data,
252 default_environment,
253 sizeof(default_environment));
254#ifdef CFG_REDUNDAND_ENVIRONMENT
255 env_ptr->flags = 0xFF;
256#endif
257 env_crc_update ();
258 gd->env_valid = 1;
259 }
260 else {
261 env_relocate_spec ();
262 }
263 gd->env_addr = (ulong)&(env_ptr->data);
wdenk452cfd62002-11-19 11:04:11 +0000264
265#ifdef CONFIG_AMIGAONEG3SE
266 disable_nvram();
267#endif
wdenk05706fd2002-09-28 17:48:27 +0000268}
wdenk3902d702004-04-15 18:22:41 +0000269
270#ifdef CONFIG_AUTO_COMPLETE
271int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
272{
273 int i, nxt, len, vallen, found;
274 const char *lval, *rval;
275
276 found = 0;
277 cmdv[0] = NULL;
278
279 len = strlen(var);
280 /* now iterate over the variables and select those that match */
281 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
282
283 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
284 ;
285
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200286 lval = (char *)env_get_addr(i);
wdenk3902d702004-04-15 18:22:41 +0000287 rval = strchr(lval, '=');
288 if (rval != NULL) {
289 vallen = rval - lval;
290 rval++;
291 } else
292 vallen = strlen(lval);
293
294 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
295 continue;
296
297 if (found >= maxv - 2 || bufsz < vallen + 1) {
298 cmdv[found++] = "...";
299 break;
300 }
301 cmdv[found++] = buf;
302 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
303 *buf++ = '\0'; bufsz--;
304 }
305
306 cmdv[found] = NULL;
307 return found;
308}
309#endif