blob: d51c2114de2e29b17944ed8911ae315c8f5d2281 [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
Wolfgang Denkd87080b2006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenkc7de8292002-11-19 11:04:11 +000035#ifdef CONFIG_AMIGAONEG3SE
36 extern void enable_nvram(void);
37 extern void disable_nvram(void);
38#endif
39
wdenk05706fd2002-09-28 17:48:27 +000040#undef DEBUG_ENV
41#ifdef DEBUG_ENV
42#define DEBUGF(fmt,args...) printf(fmt ,##args)
43#else
44#define DEBUGF(fmt,args...)
45#endif
46
47extern env_t *env_ptr;
48
49extern void env_relocate_spec (void);
50extern uchar env_get_char_spec(int);
51
52static uchar env_get_char_init (int index);
wdenk05706fd2002-09-28 17:48:27 +000053
54/************************************************************************
55 * Default settings to be used when no valid environment is found
56 */
57#define XMK_STR(x) #x
58#define MK_STR(x) XMK_STR(x)
59
60uchar default_environment[] = {
61#ifdef CONFIG_BOOTARGS
62 "bootargs=" CONFIG_BOOTARGS "\0"
63#endif
64#ifdef CONFIG_BOOTCOMMAND
65 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
66#endif
67#ifdef CONFIG_RAMBOOTCOMMAND
68 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
69#endif
70#ifdef CONFIG_NFSBOOTCOMMAND
71 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
72#endif
73#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
74 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
75#endif
76#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
77 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
78#endif
79#ifdef CONFIG_LOADS_ECHO
80 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
81#endif
82#ifdef CONFIG_ETHADDR
83 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
84#endif
85#ifdef CONFIG_ETH1ADDR
86 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
87#endif
88#ifdef CONFIG_ETH2ADDR
89 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
90#endif
wdenk69459792004-05-29 16:53:29 +000091#ifdef CONFIG_ETH3ADDR
92 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
93#endif
wdenk05706fd2002-09-28 17:48:27 +000094#ifdef CONFIG_IPADDR
95 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
96#endif
97#ifdef CONFIG_SERVERIP
98 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
99#endif
100#ifdef CFG_AUTOLOAD
101 "autoload=" CFG_AUTOLOAD "\0"
102#endif
103#ifdef CONFIG_PREBOOT
104 "preboot=" CONFIG_PREBOOT "\0"
105#endif
106#ifdef CONFIG_ROOTPATH
107 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
108#endif
109#ifdef CONFIG_GATEWAYIP
110 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
111#endif
112#ifdef CONFIG_NETMASK
113 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
114#endif
115#ifdef CONFIG_HOSTNAME
116 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
117#endif
118#ifdef CONFIG_BOOTFILE
119 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
120#endif
121#ifdef CONFIG_LOADADDR
122 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
123#endif
124#ifdef CONFIG_CLOCKS_IN_MHZ
125 "clocks_in_mhz=1\0"
126#endif
stroesead10dd92003-02-14 11:21:23 +0000127#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
128 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
129#endif
wdenk05706fd2002-09-28 17:48:27 +0000130#ifdef CONFIG_EXTRA_ENV_SETTINGS
131 CONFIG_EXTRA_ENV_SETTINGS
132#endif
133 "\0"
134};
135
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200136#if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */ \
137 || defined(CFG_ENV_IS_IN_SPI_FLASH)
wdenk13a56952004-06-09 14:58:14 +0000138int default_environment_size = sizeof(default_environment);
139#endif
wdenk05706fd2002-09-28 17:48:27 +0000140
141void env_crc_update (void)
142{
143 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
144}
145
146static uchar env_get_char_init (int index)
147{
wdenk05706fd2002-09-28 17:48:27 +0000148 uchar c;
149
150 /* if crc was bad, use the default environment */
151 if (gd->env_valid)
152 {
153 c = env_get_char_spec(index);
154 } else {
155 c = default_environment[index];
156 }
157
158 return (c);
159}
160
wdenk7c7a23b2002-12-07 00:20:59 +0000161#ifdef CONFIG_AMIGAONEG3SE
162uchar env_get_char_memory (int index)
163{
wdenk7c7a23b2002-12-07 00:20:59 +0000164 uchar retval;
165 enable_nvram();
166 if (gd->env_valid) {
167 retval = ( *((uchar *)(gd->env_addr + index)) );
168 } else {
169 retval = ( default_environment[index] );
170 }
171 disable_nvram();
172 return retval;
173}
174#else
wdenk05706fd2002-09-28 17:48:27 +0000175uchar env_get_char_memory (int index)
176{
wdenk05706fd2002-09-28 17:48:27 +0000177 if (gd->env_valid) {
178 return ( *((uchar *)(gd->env_addr + index)) );
179 } else {
180 return ( default_environment[index] );
181 }
182}
wdenk7c7a23b2002-12-07 00:20:59 +0000183#endif
wdenk05706fd2002-09-28 17:48:27 +0000184
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200185uchar env_get_char (int index)
186{
187 uchar c;
188
189 /* if relocated to RAM */
190 if (gd->flags & GD_FLG_RELOC)
191 c = env_get_char_memory(index);
192 else
193 c = env_get_char_init(index);
194
195 return (c);
196}
197
wdenk05706fd2002-09-28 17:48:27 +0000198uchar *env_get_addr (int index)
199{
wdenk05706fd2002-09-28 17:48:27 +0000200 if (gd->env_valid) {
201 return ( ((uchar *)(gd->env_addr + index)) );
202 } else {
203 return (&default_environment[index]);
204 }
205}
206
Harald Welte5bb12db2008-07-07 15:40:39 +0800207void set_default_env(void)
208{
209 if (sizeof(default_environment) > ENV_SIZE) {
210 puts ("*** Error - default environment is too large\n\n");
211 return;
212 }
213
214 memset(env_ptr, 0, sizeof(env_t));
215 memcpy(env_ptr->data, default_environment,
216 sizeof(default_environment));
217#ifdef CFG_REDUNDAND_ENVIRONMENT
218 env_ptr->flags = 0xFF;
219#endif
220 env_crc_update ();
221 gd->env_valid = 1;
222}
223
wdenk05706fd2002-09-28 17:48:27 +0000224void env_relocate (void)
225{
wdenk05706fd2002-09-28 17:48:27 +0000226 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
227 gd->reloc_off);
228
wdenkc7de8292002-11-19 11:04:11 +0000229#ifdef CONFIG_AMIGAONEG3SE
230 enable_nvram();
231#endif
232
wdenk05706fd2002-09-28 17:48:27 +0000233#ifdef ENV_IS_EMBEDDED
234 /*
235 * The environment buffer is embedded with the text segment,
236 * just relocate the environment pointer
237 */
238 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
239 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
240#else
241 /*
242 * We must allocate a buffer for the environment
243 */
244 env_ptr = (env_t *)malloc (CFG_ENV_SIZE);
245 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
246#endif
247
wdenk05706fd2002-09-28 17:48:27 +0000248 if (gd->env_valid == 0) {
249#if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */
250 puts ("Using default environment\n\n");
251#else
252 puts ("*** Warning - bad CRC, using default environment\n\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200253 show_boot_progress (-60);
wdenk05706fd2002-09-28 17:48:27 +0000254#endif
Harald Welte5bb12db2008-07-07 15:40:39 +0800255 set_default_env();
wdenk05706fd2002-09-28 17:48:27 +0000256 }
257 else {
258 env_relocate_spec ();
259 }
260 gd->env_addr = (ulong)&(env_ptr->data);
wdenkc7de8292002-11-19 11:04:11 +0000261
262#ifdef CONFIG_AMIGAONEG3SE
263 disable_nvram();
264#endif
wdenk05706fd2002-09-28 17:48:27 +0000265}
wdenk04a85b32004-04-15 18:22:41 +0000266
267#ifdef CONFIG_AUTO_COMPLETE
268int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
269{
270 int i, nxt, len, vallen, found;
271 const char *lval, *rval;
272
273 found = 0;
274 cmdv[0] = NULL;
275
276 len = strlen(var);
277 /* now iterate over the variables and select those that match */
278 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
279
280 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
281 ;
282
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200283 lval = (char *)env_get_addr(i);
wdenk04a85b32004-04-15 18:22:41 +0000284 rval = strchr(lval, '=');
285 if (rval != NULL) {
286 vallen = rval - lval;
287 rval++;
288 } else
289 vallen = strlen(lval);
290
291 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
292 continue;
293
294 if (found >= maxv - 2 || bufsz < vallen + 1) {
295 cmdv[found++] = "...";
296 break;
297 }
298 cmdv[found++] = buf;
299 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
300 *buf++ = '\0'; bufsz--;
301 }
302
303 cmdv[found] = NULL;
304 return found;
305}
306#endif