blob: 82e4936ced54182c3f97cb8cf06d8d96284ab5ba [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 Denk6405a152006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenk05706fd2002-09-28 17:48:27 +000035#undef DEBUG_ENV
36#ifdef DEBUG_ENV
37#define DEBUGF(fmt,args...) printf(fmt ,##args)
38#else
39#define DEBUGF(fmt,args...)
40#endif
41
42extern env_t *env_ptr;
43
44extern void env_relocate_spec (void);
45extern uchar env_get_char_spec(int);
46
47static uchar env_get_char_init (int index);
wdenk05706fd2002-09-28 17:48:27 +000048
49/************************************************************************
50 * Default settings to be used when no valid environment is found
51 */
52#define XMK_STR(x) #x
53#define MK_STR(x) XMK_STR(x)
54
55uchar default_environment[] = {
56#ifdef CONFIG_BOOTARGS
57 "bootargs=" CONFIG_BOOTARGS "\0"
58#endif
59#ifdef CONFIG_BOOTCOMMAND
60 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
61#endif
62#ifdef CONFIG_RAMBOOTCOMMAND
63 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
64#endif
65#ifdef CONFIG_NFSBOOTCOMMAND
66 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
67#endif
68#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
69 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
70#endif
71#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
72 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
73#endif
74#ifdef CONFIG_LOADS_ECHO
75 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
76#endif
77#ifdef CONFIG_ETHADDR
78 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
79#endif
80#ifdef CONFIG_ETH1ADDR
81 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
82#endif
83#ifdef CONFIG_ETH2ADDR
84 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
85#endif
wdenkcd914452004-05-29 16:53:29 +000086#ifdef CONFIG_ETH3ADDR
87 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
88#endif
richardretanubune5167f12008-09-29 18:28:23 -040089#ifdef CONFIG_ETH4ADDR
90 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
91#endif
92#ifdef CONFIG_ETH5ADDR
93 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
94#endif
wdenk05706fd2002-09-28 17:48:27 +000095#ifdef CONFIG_IPADDR
96 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
97#endif
98#ifdef CONFIG_SERVERIP
99 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
100#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200101#ifdef CONFIG_SYS_AUTOLOAD
102 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenk05706fd2002-09-28 17:48:27 +0000103#endif
104#ifdef CONFIG_PREBOOT
105 "preboot=" CONFIG_PREBOOT "\0"
106#endif
107#ifdef CONFIG_ROOTPATH
108 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
109#endif
110#ifdef CONFIG_GATEWAYIP
111 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
112#endif
113#ifdef CONFIG_NETMASK
114 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
115#endif
116#ifdef CONFIG_HOSTNAME
117 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
118#endif
119#ifdef CONFIG_BOOTFILE
120 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
121#endif
122#ifdef CONFIG_LOADADDR
123 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
124#endif
125#ifdef CONFIG_CLOCKS_IN_MHZ
126 "clocks_in_mhz=1\0"
127#endif
stroesef5dd4102003-02-14 11:21:23 +0000128#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
129 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
130#endif
wdenk05706fd2002-09-28 17:48:27 +0000131#ifdef CONFIG_EXTRA_ENV_SETTINGS
132 CONFIG_EXTRA_ENV_SETTINGS
133#endif
134 "\0"
135};
136
wdenk05706fd2002-09-28 17:48:27 +0000137void env_crc_update (void)
138{
139 env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
140}
141
142static uchar env_get_char_init (int index)
143{
wdenk05706fd2002-09-28 17:48:27 +0000144 uchar c;
145
146 /* if crc was bad, use the default environment */
147 if (gd->env_valid)
148 {
149 c = env_get_char_spec(index);
150 } else {
151 c = default_environment[index];
152 }
153
154 return (c);
155}
156
wdenkbb444c92002-12-07 00:20:59 +0000157uchar env_get_char_memory (int index)
158{
wdenk05706fd2002-09-28 17:48:27 +0000159 if (gd->env_valid) {
160 return ( *((uchar *)(gd->env_addr + index)) );
161 } else {
162 return ( default_environment[index] );
163 }
164}
165
Joakim Tjernlund304b3592008-07-06 12:30:09 +0200166uchar env_get_char (int index)
167{
168 uchar c;
169
170 /* if relocated to RAM */
171 if (gd->flags & GD_FLG_RELOC)
172 c = env_get_char_memory(index);
173 else
174 c = env_get_char_init(index);
175
176 return (c);
177}
178
wdenk05706fd2002-09-28 17:48:27 +0000179uchar *env_get_addr (int index)
180{
wdenk05706fd2002-09-28 17:48:27 +0000181 if (gd->env_valid) {
182 return ( ((uchar *)(gd->env_addr + index)) );
183 } else {
184 return (&default_environment[index]);
185 }
186}
187
Harald Welte72eb50a2008-07-07 15:40:39 +0800188void set_default_env(void)
189{
190 if (sizeof(default_environment) > ENV_SIZE) {
191 puts ("*** Error - default environment is too large\n\n");
192 return;
193 }
194
195 memset(env_ptr, 0, sizeof(env_t));
196 memcpy(env_ptr->data, default_environment,
197 sizeof(default_environment));
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200198#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
Harald Welte72eb50a2008-07-07 15:40:39 +0800199 env_ptr->flags = 0xFF;
200#endif
201 env_crc_update ();
202 gd->env_valid = 1;
203}
204
wdenk05706fd2002-09-28 17:48:27 +0000205void env_relocate (void)
206{
Peter Tyser9057cbf2009-09-21 11:20:36 -0500207#ifndef CONFIG_RELOC_FIXUP_WORKS
wdenk05706fd2002-09-28 17:48:27 +0000208 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
209 gd->reloc_off);
Peter Tyser9057cbf2009-09-21 11:20:36 -0500210#endif
wdenk05706fd2002-09-28 17:48:27 +0000211
212#ifdef ENV_IS_EMBEDDED
213 /*
214 * The environment buffer is embedded with the text segment,
215 * just relocate the environment pointer
216 */
Peter Tyser9057cbf2009-09-21 11:20:36 -0500217#ifndef CONFIG_RELOC_FIXUP_WORKS
wdenk05706fd2002-09-28 17:48:27 +0000218 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
Peter Tyser9057cbf2009-09-21 11:20:36 -0500219#endif
wdenk05706fd2002-09-28 17:48:27 +0000220 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
221#else
222 /*
223 * We must allocate a buffer for the environment
224 */
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200225 env_ptr = (env_t *)malloc (CONFIG_ENV_SIZE);
wdenk05706fd2002-09-28 17:48:27 +0000226 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
227#endif
228
wdenk05706fd2002-09-28 17:48:27 +0000229 if (gd->env_valid == 0) {
Jean-Christophe PLAGNIOL-VILLARD68a87562008-09-10 22:48:00 +0200230#if defined(CONFIG_GTH) || defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
wdenk05706fd2002-09-28 17:48:27 +0000231 puts ("Using default environment\n\n");
232#else
233 puts ("*** Warning - bad CRC, using default environment\n\n");
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200234 show_boot_progress (-60);
wdenk05706fd2002-09-28 17:48:27 +0000235#endif
Harald Welte72eb50a2008-07-07 15:40:39 +0800236 set_default_env();
wdenk05706fd2002-09-28 17:48:27 +0000237 }
238 else {
239 env_relocate_spec ();
240 }
241 gd->env_addr = (ulong)&(env_ptr->data);
wdenk05706fd2002-09-28 17:48:27 +0000242}
wdenk3902d702004-04-15 18:22:41 +0000243
244#ifdef CONFIG_AUTO_COMPLETE
245int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
246{
247 int i, nxt, len, vallen, found;
248 const char *lval, *rval;
249
250 found = 0;
251 cmdv[0] = NULL;
252
253 len = strlen(var);
254 /* now iterate over the variables and select those that match */
255 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
256
257 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
258 ;
259
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200260 lval = (char *)env_get_addr(i);
wdenk3902d702004-04-15 18:22:41 +0000261 rval = strchr(lval, '=');
262 if (rval != NULL) {
263 vallen = rval - lval;
264 rval++;
265 } else
266 vallen = strlen(lval);
267
268 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
269 continue;
270
271 if (found >= maxv - 2 || bufsz < vallen + 1) {
272 cmdv[found++] = "...";
273 break;
274 }
275 cmdv[found++] = buf;
276 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
277 *buf++ = '\0'; bufsz--;
278 }
279
280 cmdv[found] = NULL;
281 return found;
282}
283#endif