2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
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>
35 DECLARE_GLOBAL_DATA_PTR;
37 extern env_t *env_ptr;
39 extern void env_relocate_spec (void);
40 extern uchar env_get_char_spec(int);
42 static uchar env_get_char_init (int index);
44 /************************************************************************
45 * Default settings to be used when no valid environment is found
48 #define MK_STR(x) XMK_STR(x)
50 uchar default_environment[] = {
51 #ifdef CONFIG_BOOTARGS
52 "bootargs=" CONFIG_BOOTARGS "\0"
54 #ifdef CONFIG_BOOTCOMMAND
55 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
57 #ifdef CONFIG_RAMBOOTCOMMAND
58 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
60 #ifdef CONFIG_NFSBOOTCOMMAND
61 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
63 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
64 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
66 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
67 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
69 #ifdef CONFIG_LOADS_ECHO
70 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
73 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
75 #ifdef CONFIG_ETH1ADDR
76 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
78 #ifdef CONFIG_ETH2ADDR
79 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
81 #ifdef CONFIG_ETH3ADDR
82 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
84 #ifdef CONFIG_ETH4ADDR
85 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
87 #ifdef CONFIG_ETH5ADDR
88 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
91 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
93 #ifdef CONFIG_SERVERIP
94 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
96 #ifdef CONFIG_SYS_AUTOLOAD
97 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
100 "preboot=" CONFIG_PREBOOT "\0"
102 #ifdef CONFIG_ROOTPATH
103 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
105 #ifdef CONFIG_GATEWAYIP
106 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
108 #ifdef CONFIG_NETMASK
109 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
111 #ifdef CONFIG_HOSTNAME
112 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
114 #ifdef CONFIG_BOOTFILE
115 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
117 #ifdef CONFIG_LOADADDR
118 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
120 #ifdef CONFIG_CLOCKS_IN_MHZ
123 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
124 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
126 #ifdef CONFIG_EXTRA_ENV_SETTINGS
127 CONFIG_EXTRA_ENV_SETTINGS
132 static uchar env_get_char_init (int index)
136 /* if crc was bad, use the default environment */
138 c = env_get_char_spec(index);
140 c = default_environment[index];
145 uchar env_get_char_memory (int index)
147 return *env_get_addr(index);
150 uchar env_get_char (int index)
154 /* if relocated to RAM */
155 if (gd->flags & GD_FLG_RELOC)
156 c = env_get_char_memory(index);
158 c = env_get_char_init(index);
163 uchar *env_get_addr (int index)
166 return (uchar *)(gd->env_addr + index);
168 return &default_environment[index];
171 void set_default_env(const char *s)
173 if (sizeof(default_environment) > ENV_SIZE) {
174 puts("*** Error - default environment is too large\n\n");
180 printf("*** Warning - %s, "
181 "using default environment\n\n",
187 puts("Using default environment\n\n");
190 if (himport((char *)default_environment,
191 sizeof(default_environment), '\0', 0) == 0) {
192 error("Environment import failed: errno = %d\n", errno);
194 gd->flags |= GD_FLG_ENV_READY;
198 * Check if CRC is valid and (if yes) import the environment.
199 * Note that "buf" may or may not be aligned.
201 int env_import(const char *buf, int check)
203 env_t *ep = (env_t *)buf;
208 memcpy(&crc, &ep->crc, sizeof(crc));
210 if (crc32(0, ep->data, ENV_SIZE) != crc) {
211 set_default_env("!bad CRC");
216 if (himport((char *)ep->data, ENV_SIZE, '\0', 0)) {
217 gd->flags |= GD_FLG_ENV_READY;
221 error("Cannot import environment: errno = %d\n", errno);
223 set_default_env("!import failed");
228 void env_relocate (void)
230 #if !defined(CONFIG_RELOC_FIXUP_WORKS)
231 extern void env_reloc(void);
235 if (gd->env_valid == 0) {
236 #if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
237 set_default_env(NULL);
239 show_boot_progress (-60);
241 set_default_env("!bad CRC");
243 env_relocate_spec ();
247 #if 0 /* need to reimplement - def CONFIG_AUTO_COMPLETE */
248 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
250 int i, nxt, len, vallen, found;
251 const char *lval, *rval;
257 /* now iterate over the variables and select those that match */
258 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
260 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
263 lval = (char *)env_get_addr(i);
264 rval = strchr(lval, '=');
266 vallen = rval - lval;
269 vallen = strlen(lval);
271 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
274 if (found >= maxv - 2 || bufsz < vallen + 1) {
275 cmdv[found++] = "...";
279 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
280 *buf++ = '\0'; bufsz--;