blob: 5acda4d49ed66225889aa7e9e409652c9ad03cce [file] [log] [blame]
wdenk05706fd2002-09-28 17:48:27 +00001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenk05706fd2002-09-28 17:48:27 +00003 * 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>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02007 *
wdenk05706fd2002-09-28 17:48:27 +00008 * 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>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020031#include <search.h>
32#include <errno.h>
wdenk05706fd2002-09-28 17:48:27 +000033#include <malloc.h>
34
Wolfgang Denk6405a152006-03-31 18:32:53 +020035DECLARE_GLOBAL_DATA_PTR;
36
wdenk05706fd2002-09-28 17:48:27 +000037extern env_t *env_ptr;
38
39extern void env_relocate_spec (void);
40extern uchar env_get_char_spec(int);
41
42static uchar env_get_char_init (int index);
wdenk05706fd2002-09-28 17:48:27 +000043
44/************************************************************************
45 * Default settings to be used when no valid environment is found
46 */
47#define XMK_STR(x) #x
48#define MK_STR(x) XMK_STR(x)
49
50uchar default_environment[] = {
51#ifdef CONFIG_BOOTARGS
52 "bootargs=" CONFIG_BOOTARGS "\0"
53#endif
54#ifdef CONFIG_BOOTCOMMAND
55 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
56#endif
57#ifdef CONFIG_RAMBOOTCOMMAND
58 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
59#endif
60#ifdef CONFIG_NFSBOOTCOMMAND
61 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
62#endif
63#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
64 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
65#endif
66#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
67 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
68#endif
69#ifdef CONFIG_LOADS_ECHO
70 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
71#endif
72#ifdef CONFIG_ETHADDR
73 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
74#endif
75#ifdef CONFIG_ETH1ADDR
76 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
77#endif
78#ifdef CONFIG_ETH2ADDR
79 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
80#endif
wdenkcd914452004-05-29 16:53:29 +000081#ifdef CONFIG_ETH3ADDR
82 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
83#endif
richardretanubune5167f12008-09-29 18:28:23 -040084#ifdef CONFIG_ETH4ADDR
85 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
86#endif
87#ifdef CONFIG_ETH5ADDR
88 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
89#endif
wdenk05706fd2002-09-28 17:48:27 +000090#ifdef CONFIG_IPADDR
91 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
92#endif
93#ifdef CONFIG_SERVERIP
94 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
95#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020096#ifdef CONFIG_SYS_AUTOLOAD
97 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenk05706fd2002-09-28 17:48:27 +000098#endif
99#ifdef CONFIG_PREBOOT
100 "preboot=" CONFIG_PREBOOT "\0"
101#endif
102#ifdef CONFIG_ROOTPATH
103 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
104#endif
105#ifdef CONFIG_GATEWAYIP
106 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
107#endif
108#ifdef CONFIG_NETMASK
109 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
110#endif
111#ifdef CONFIG_HOSTNAME
112 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
113#endif
114#ifdef CONFIG_BOOTFILE
115 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
116#endif
117#ifdef CONFIG_LOADADDR
118 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
119#endif
120#ifdef CONFIG_CLOCKS_IN_MHZ
121 "clocks_in_mhz=1\0"
122#endif
stroesef5dd4102003-02-14 11:21:23 +0000123#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
124 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
125#endif
wdenk05706fd2002-09-28 17:48:27 +0000126#ifdef CONFIG_EXTRA_ENV_SETTINGS
127 CONFIG_EXTRA_ENV_SETTINGS
128#endif
129 "\0"
130};
131
wdenk05706fd2002-09-28 17:48:27 +0000132static uchar env_get_char_init (int index)
133{
wdenk05706fd2002-09-28 17:48:27 +0000134 uchar c;
135
136 /* if crc was bad, use the default environment */
137 if (gd->env_valid)
wdenk05706fd2002-09-28 17:48:27 +0000138 c = env_get_char_spec(index);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200139 else
wdenk05706fd2002-09-28 17:48:27 +0000140 c = default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +0000141
142 return (c);
143}
144
wdenkbb444c92002-12-07 00:20:59 +0000145uchar env_get_char_memory (int index)
146{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200147 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +0000148}
149
Joakim Tjernlund304b3592008-07-06 12:30:09 +0200150uchar env_get_char (int index)
151{
152 uchar c;
153
154 /* if relocated to RAM */
155 if (gd->flags & GD_FLG_RELOC)
156 c = env_get_char_memory(index);
157 else
158 c = env_get_char_init(index);
159
160 return (c);
161}
162
wdenk05706fd2002-09-28 17:48:27 +0000163uchar *env_get_addr (int index)
164{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200165 if (gd->env_valid)
166 return (uchar *)(gd->env_addr + index);
167 else
168 return &default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +0000169}
170
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200171void set_default_env(const char *s)
Harald Welte72eb50a2008-07-07 15:40:39 +0800172{
173 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200174 puts("*** Error - default environment is too large\n\n");
Harald Welte72eb50a2008-07-07 15:40:39 +0800175 return;
176 }
177
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200178 if (s) {
179 if (*s == '!') {
180 printf("*** Warning - %s, "
181 "using default environment\n\n",
182 s+1);
183 } else {
184 puts(s);
185 }
186 } else {
187 puts("Using default environment\n\n");
188 }
189
190 if (himport((char *)default_environment,
191 sizeof(default_environment), '\0', 0) == 0) {
192 error("Environment import failed: errno = %d\n", errno);
193 }
194 gd->flags |= GD_FLG_ENV_READY;
Harald Welte72eb50a2008-07-07 15:40:39 +0800195}
196
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200197/*
198 * Check if CRC is valid and (if yes) import the environment.
199 * Note that "buf" may or may not be aligned.
200 */
201int env_import(const char *buf, int check)
wdenk05706fd2002-09-28 17:48:27 +0000202{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200203 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000204
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200205 if (check) {
206 uint32_t crc;
207
208 memcpy(&crc, &ep->crc, sizeof(crc));
209
210 if (crc32(0, ep->data, ENV_SIZE) != crc) {
211 set_default_env("!bad CRC");
212 return 0;
213 }
214 }
wdenk05706fd2002-09-28 17:48:27 +0000215
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200216 if (himport((char *)ep->data, ENV_SIZE, '\0', 0)) {
217 gd->flags |= GD_FLG_ENV_READY;
218 return 1;
219 }
220
221 error("Cannot import environment: errno = %d\n", errno);
222
223 set_default_env("!import failed");
224
225 return 0;
226}
227
228void env_relocate (void)
229{
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200230#if !defined(CONFIG_RELOC_FIXUP_WORKS)
231 extern void env_reloc(void);
232
233 env_reloc();
234#endif
wdenk05706fd2002-09-28 17:48:27 +0000235 if (gd->env_valid == 0) {
Wolfgang Denk30c3add2010-07-05 22:46:33 +0200236#if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200237 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000238#else
Heiko Schocher8a8ec532007-07-13 09:54:17 +0200239 show_boot_progress (-60);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200240 set_default_env("!bad CRC");
Lei Wene0909be2010-10-10 12:36:40 +0800241#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242 } else {
wdenk05706fd2002-09-28 17:48:27 +0000243 env_relocate_spec ();
244 }
wdenk05706fd2002-09-28 17:48:27 +0000245}
wdenk3902d702004-04-15 18:22:41 +0000246
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200247#if 0 /* need to reimplement - def CONFIG_AUTO_COMPLETE */
wdenk3902d702004-04-15 18:22:41 +0000248int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
249{
250 int i, nxt, len, vallen, found;
251 const char *lval, *rval;
252
253 found = 0;
254 cmdv[0] = NULL;
255
256 len = strlen(var);
257 /* now iterate over the variables and select those that match */
258 for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
259
260 for (nxt=i; env_get_char(nxt) != '\0'; ++nxt)
261 ;
262
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200263 lval = (char *)env_get_addr(i);
wdenk3902d702004-04-15 18:22:41 +0000264 rval = strchr(lval, '=');
265 if (rval != NULL) {
266 vallen = rval - lval;
267 rval++;
268 } else
269 vallen = strlen(lval);
270
271 if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0))
272 continue;
273
274 if (found >= maxv - 2 || bufsz < vallen + 1) {
275 cmdv[found++] = "...";
276 break;
277 }
278 cmdv[found++] = buf;
279 memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen;
280 *buf++ = '\0'; bufsz--;
281 }
282
283 cmdv[found] = NULL;
284 return found;
285}
286#endif