blob: eb33422af4b9c664da8928c1e2d61d986067d338 [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
Wolfgang Denkd87080b2006-03-31 18:32:53 +020040DECLARE_GLOBAL_DATA_PTR;
41
wdenkc7de8292002-11-19 11:04:11 +000042#ifdef CONFIG_AMIGAONEG3SE
43 extern void enable_nvram(void);
44 extern void disable_nvram(void);
45#endif
46
wdenk05706fd2002-09-28 17:48:27 +000047#undef DEBUG_ENV
48#ifdef DEBUG_ENV
49#define DEBUGF(fmt,args...) printf(fmt ,##args)
50#else
51#define DEBUGF(fmt,args...)
52#endif
53
54extern env_t *env_ptr;
55
56extern void env_relocate_spec (void);
57extern uchar env_get_char_spec(int);
58
59static uchar env_get_char_init (int index);
60uchar (*env_get_char)(int) = env_get_char_init;
61
62/************************************************************************
63 * Default settings to be used when no valid environment is found
64 */
65#define XMK_STR(x) #x
66#define MK_STR(x) XMK_STR(x)
67
68uchar default_environment[] = {
69#ifdef CONFIG_BOOTARGS
70 "bootargs=" CONFIG_BOOTARGS "\0"
71#endif
72#ifdef CONFIG_BOOTCOMMAND
73 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
74#endif
75#ifdef CONFIG_RAMBOOTCOMMAND
76 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
77#endif
78#ifdef CONFIG_NFSBOOTCOMMAND
79 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
80#endif
81#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
82 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
83#endif
84#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
85 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
86#endif
87#ifdef CONFIG_LOADS_ECHO
88 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
89#endif
90#ifdef CONFIG_ETHADDR
91 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
92#endif
93#ifdef CONFIG_ETH1ADDR
94 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
95#endif
96#ifdef CONFIG_ETH2ADDR
97 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
98#endif
wdenk69459792004-05-29 16:53:29 +000099#ifdef CONFIG_ETH3ADDR
100 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
101#endif
wdenk05706fd2002-09-28 17:48:27 +0000102#ifdef CONFIG_IPADDR
103 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
104#endif
105#ifdef CONFIG_SERVERIP
106 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
107#endif
108#ifdef CFG_AUTOLOAD
109 "autoload=" CFG_AUTOLOAD "\0"
110#endif
111#ifdef CONFIG_PREBOOT
112 "preboot=" CONFIG_PREBOOT "\0"
113#endif
114#ifdef CONFIG_ROOTPATH
115 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
116#endif
117#ifdef CONFIG_GATEWAYIP
118 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
119#endif
120#ifdef CONFIG_NETMASK
121 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
122#endif
123#ifdef CONFIG_HOSTNAME
124 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
125#endif
126#ifdef CONFIG_BOOTFILE
127 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
128#endif
129#ifdef CONFIG_LOADADDR
130 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
131#endif
132#ifdef CONFIG_CLOCKS_IN_MHZ
133 "clocks_in_mhz=1\0"
134#endif
stroesead10dd92003-02-14 11:21:23 +0000135#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
136 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
137#endif
wdenk05706fd2002-09-28 17:48:27 +0000138#ifdef CONFIG_EXTRA_ENV_SETTINGS
139 CONFIG_EXTRA_ENV_SETTINGS
140#endif
141 "\0"
142};
143
wdenk13a56952004-06-09 14:58:14 +0000144#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
145int default_environment_size = sizeof(default_environment);
146#endif
wdenk05706fd2002-09-28 17:48:27 +0000147
148void env_crc_update (void)
149{
150 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
151}
152
153static uchar env_get_char_init (int index)
154{
wdenk05706fd2002-09-28 17:48:27 +0000155 uchar c;
156
157 /* if crc was bad, use the default environment */
158 if (gd->env_valid)
159 {
160 c = env_get_char_spec(index);
161 } else {
162 c = default_environment[index];
163 }
164
165 return (c);
166}
167
wdenk7c7a23b2002-12-07 00:20:59 +0000168#ifdef CONFIG_AMIGAONEG3SE
169uchar env_get_char_memory (int index)
170{
wdenk7c7a23b2002-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
wdenk05706fd2002-09-28 17:48:27 +0000182uchar env_get_char_memory (int index)
183{
wdenk05706fd2002-09-28 17:48:27 +0000184 if (gd->env_valid) {
185 return ( *((uchar *)(gd->env_addr + index)) );
186 } else {
187 return ( default_environment[index] );
188 }
189}
wdenk7c7a23b2002-12-07 00:20:59 +0000190#endif
wdenk05706fd2002-09-28 17:48:27 +0000191
192uchar *env_get_addr (int index)
193{
wdenk05706fd2002-09-28 17:48:27 +0000194 if (gd->env_valid) {
195 return ( ((uchar *)(gd->env_addr + index)) );
196 } else {
197 return (&default_environment[index]);
198 }
199}
200
201void env_relocate (void)
202{
wdenk05706fd2002-09-28 17:48:27 +0000203 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
204 gd->reloc_off);
205
wdenkc7de8292002-11-19 11:04:11 +0000206#ifdef CONFIG_AMIGAONEG3SE
207 enable_nvram();
208#endif
209
wdenk05706fd2002-09-28 17:48:27 +0000210#ifdef ENV_IS_EMBEDDED
211 /*
212 * The environment buffer is embedded with the text segment,
213 * just relocate the environment pointer
214 */
215 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
216 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
217#else
218 /*
219 * We must allocate a buffer for the environment
220 */
221 env_ptr = (env_t *)malloc (CFG_ENV_SIZE);
222 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
223#endif
224
225 /*
226 * After relocation to RAM, we can always use the "memory" functions
227 */
228 env_get_char = env_get_char_memory;
229
230 if (gd->env_valid == 0) {
231#if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */
232 puts ("Using default environment\n\n");
233#else
234 puts ("*** Warning - bad CRC, using default environment\n\n");
235 SHOW_BOOT_PROGRESS (-1);
236#endif
237
238 if (sizeof(default_environment) > ENV_SIZE)
239 {
240 puts ("*** Error - default environment is too large\n\n");
241 return;
242 }
243
244 memset (env_ptr, 0, sizeof(env_t));
245 memcpy (env_ptr->data,
246 default_environment,
247 sizeof(default_environment));
248#ifdef CFG_REDUNDAND_ENVIRONMENT
249 env_ptr->flags = 0xFF;
250#endif
251 env_crc_update ();
252 gd->env_valid = 1;
253 }
254 else {
255 env_relocate_spec ();
256 }
257 gd->env_addr = (ulong)&(env_ptr->data);
wdenkc7de8292002-11-19 11:04:11 +0000258
259#ifdef CONFIG_AMIGAONEG3SE
260 disable_nvram();
261#endif
wdenk05706fd2002-09-28 17:48:27 +0000262}
wdenk04a85b32004-04-15 18:22:41 +0000263
264#ifdef CONFIG_AUTO_COMPLETE
265int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
266{
267 int i, nxt, len, vallen, found;
268 const char *lval, *rval;
269
270 found = 0;
271 cmdv[0] = NULL;
272
273 len = strlen(var);
274 /* now iterate over the variables and select those that match */
275 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
276
277 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
278 ;
279
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200280 lval = (char *)env_get_addr(i);
wdenk04a85b32004-04-15 18:22:41 +0000281 rval = strchr(lval, '=');
282 if (rval != NULL) {
283 vallen = rval - lval;
284 rval++;
285 } else
286 vallen = strlen(lval);
287
288 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
289 continue;
290
291 if (found >= maxv - 2 || bufsz < vallen + 1) {
292 cmdv[found++] = "...";
293 break;
294 }
295 cmdv[found++] = buf;
296 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
297 *buf++ = '\0'; bufsz--;
298 }
299
300 cmdv[found] = NULL;
301 return found;
302}
303#endif