blob: d9e990dbbec92c9eb6f8b4e28194e1e6ee6d1856 [file] [log] [blame]
wdenk05706fd2002-09-28 17:48:27 +00001/*
Wolfgang Denkea882ba2010-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 Denkea882ba2010-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 Denkea882ba2010-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 Denkd87080b2006-03-31 18:32:53 +020035DECLARE_GLOBAL_DATA_PTR;
36
wdenk05706fd2002-09-28 17:48:27 +000037/************************************************************************
38 * Default settings to be used when no valid environment is found
39 */
40#define XMK_STR(x) #x
41#define MK_STR(x) XMK_STR(x)
42
Mike Frysinger147c7162011-04-23 23:43:21 +000043const uchar default_environment[] = {
wdenk05706fd2002-09-28 17:48:27 +000044#ifdef CONFIG_BOOTARGS
45 "bootargs=" CONFIG_BOOTARGS "\0"
46#endif
47#ifdef CONFIG_BOOTCOMMAND
48 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
49#endif
50#ifdef CONFIG_RAMBOOTCOMMAND
51 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
52#endif
53#ifdef CONFIG_NFSBOOTCOMMAND
54 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
55#endif
56#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
57 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
58#endif
59#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
60 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
61#endif
62#ifdef CONFIG_LOADS_ECHO
63 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
64#endif
65#ifdef CONFIG_ETHADDR
66 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
67#endif
68#ifdef CONFIG_ETH1ADDR
69 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
70#endif
71#ifdef CONFIG_ETH2ADDR
72 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
73#endif
wdenk69459792004-05-29 16:53:29 +000074#ifdef CONFIG_ETH3ADDR
75 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
76#endif
richardretanubunc68a05f2008-09-29 18:28:23 -040077#ifdef CONFIG_ETH4ADDR
78 "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
79#endif
80#ifdef CONFIG_ETH5ADDR
81 "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
82#endif
wdenk05706fd2002-09-28 17:48:27 +000083#ifdef CONFIG_IPADDR
84 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
85#endif
86#ifdef CONFIG_SERVERIP
87 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
88#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020089#ifdef CONFIG_SYS_AUTOLOAD
Igor Grinberg27aafe92011-11-07 01:14:11 +000090 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenk05706fd2002-09-28 17:48:27 +000091#endif
92#ifdef CONFIG_PREBOOT
93 "preboot=" CONFIG_PREBOOT "\0"
94#endif
95#ifdef CONFIG_ROOTPATH
Joe Hershberger8b3637c2011-10-13 13:03:47 +000096 "rootpath=" CONFIG_ROOTPATH "\0"
wdenk05706fd2002-09-28 17:48:27 +000097#endif
98#ifdef CONFIG_GATEWAYIP
99 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
100#endif
101#ifdef CONFIG_NETMASK
102 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
103#endif
104#ifdef CONFIG_HOSTNAME
105 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
106#endif
107#ifdef CONFIG_BOOTFILE
Joe Hershbergerb3f44c22011-10-13 13:03:48 +0000108 "bootfile=" CONFIG_BOOTFILE "\0"
wdenk05706fd2002-09-28 17:48:27 +0000109#endif
110#ifdef CONFIG_LOADADDR
111 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
112#endif
Igor Grinberg27aafe92011-11-07 01:14:11 +0000113#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk05706fd2002-09-28 17:48:27 +0000114 "clocks_in_mhz=1\0"
115#endif
stroesead10dd92003-02-14 11:21:23 +0000116#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
117 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
118#endif
Stephen Warren5e724ca2012-05-22 09:21:54 +0000119#ifdef CONFIG_ENV_VARS_UBOOT_CONFIG
120 "arch=" CONFIG_SYS_ARCH "\0"
121 "cpu=" CONFIG_SYS_CPU "\0"
122 "board=" CONFIG_SYS_BOARD "\0"
123#ifdef CONFIG_SYS_VENDOR
124 "vendor=" CONFIG_SYS_VENDOR "\0"
125#endif
126#ifdef CONFIG_SYS_SOC
127 "soc=" CONFIG_SYS_SOC "\0"
128#endif
129#endif
Igor Grinberg27aafe92011-11-07 01:14:11 +0000130#ifdef CONFIG_EXTRA_ENV_SETTINGS
wdenk05706fd2002-09-28 17:48:27 +0000131 CONFIG_EXTRA_ENV_SETTINGS
132#endif
133 "\0"
134};
135
Mike Frysinger2eb15732010-12-08 06:26:04 -0500136struct hsearch_data env_htab;
137
Igor Grinbergbf95df42011-12-26 03:33:10 +0000138static uchar __env_get_char_spec(int index)
139{
140 return *((uchar *)(gd->env_addr + index));
141}
142uchar env_get_char_spec(int)
143 __attribute__((weak, alias("__env_get_char_spec")));
144
Igor Grinberg27aafe92011-11-07 01:14:11 +0000145static uchar env_get_char_init(int index)
wdenk05706fd2002-09-28 17:48:27 +0000146{
wdenk05706fd2002-09-28 17:48:27 +0000147 /* if crc was bad, use the default environment */
148 if (gd->env_valid)
Igor Grinberg27aafe92011-11-07 01:14:11 +0000149 return env_get_char_spec(index);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200150 else
Igor Grinberg27aafe92011-11-07 01:14:11 +0000151 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +0000152}
153
Igor Grinberg27aafe92011-11-07 01:14:11 +0000154uchar env_get_char_memory(int index)
wdenk05706fd2002-09-28 17:48:27 +0000155{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200156 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +0000157}
158
Igor Grinberg27aafe92011-11-07 01:14:11 +0000159uchar env_get_char(int index)
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200160{
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200161 /* if relocated to RAM */
162 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg27aafe92011-11-07 01:14:11 +0000163 return env_get_char_memory(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200164 else
Igor Grinberg27aafe92011-11-07 01:14:11 +0000165 return env_get_char_init(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200166}
167
Igor Grinberg27aafe92011-11-07 01:14:11 +0000168const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +0000169{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200170 if (gd->env_valid)
171 return (uchar *)(gd->env_addr + index);
172 else
173 return &default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +0000174}
175
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200176void set_default_env(const char *s)
Harald Welte5bb12db2008-07-07 15:40:39 +0800177{
178 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200179 puts("*** Error - default environment is too large\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +0800180 return;
181 }
182
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200183 if (s) {
184 if (*s == '!') {
185 printf("*** Warning - %s, "
186 "using default environment\n\n",
Igor Grinberg27aafe92011-11-07 01:14:11 +0000187 s + 1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200188 } else {
189 puts(s);
190 }
191 } else {
192 puts("Using default environment\n\n");
193 }
194
Mike Frysinger2eb15732010-12-08 06:26:04 -0500195 if (himport_r(&env_htab, (char *)default_environment,
Igor Grinberg27aafe92011-11-07 01:14:11 +0000196 sizeof(default_environment), '\0', 0) == 0)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg27aafe92011-11-07 01:14:11 +0000198
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200199 gd->flags |= GD_FLG_ENV_READY;
200}
201
202/*
203 * Check if CRC is valid and (if yes) import the environment.
204 * Note that "buf" may or may not be aligned.
205 */
206int env_import(const char *buf, int check)
207{
208 env_t *ep = (env_t *)buf;
209
210 if (check) {
211 uint32_t crc;
212
213 memcpy(&crc, &ep->crc, sizeof(crc));
214
215 if (crc32(0, ep->data, ENV_SIZE) != crc) {
216 set_default_env("!bad CRC");
217 return 0;
218 }
219 }
220
Mike Frysinger2eb15732010-12-08 06:26:04 -0500221 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200222 gd->flags |= GD_FLG_ENV_READY;
223 return 1;
224 }
225
226 error("Cannot import environment: errno = %d\n", errno);
227
228 set_default_env("!import failed");
229
230 return 0;
Harald Welte5bb12db2008-07-07 15:40:39 +0800231}
232
Igor Grinberg27aafe92011-11-07 01:14:11 +0000233void env_relocate(void)
wdenk05706fd2002-09-28 17:48:27 +0000234{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200235#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +0200236 env_reloc();
237#endif
wdenk05706fd2002-09-28 17:48:27 +0000238 if (gd->env_valid == 0) {
Wolfgang Denk0fe247b2010-07-05 22:46:33 +0200239#if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200240 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000241#else
Simon Glass770605e2012-02-13 13:51:18 +0000242 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200243 set_default_env("!bad CRC");
Lei Wend2590792010-10-10 12:36:40 +0800244#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200245 } else {
Igor Grinberg27aafe92011-11-07 01:14:11 +0000246 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000247 }
wdenk05706fd2002-09-28 17:48:27 +0000248}
wdenk04a85b32004-04-15 18:22:41 +0000249
Mike Frysinger560d4242010-12-17 16:51:59 -0500250#ifdef CONFIG_AUTO_COMPLETE
wdenk04a85b32004-04-15 18:22:41 +0000251int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
252{
Mike Frysinger560d4242010-12-17 16:51:59 -0500253 ENTRY *match;
254 int found, idx;
wdenk04a85b32004-04-15 18:22:41 +0000255
Mike Frysinger560d4242010-12-17 16:51:59 -0500256 idx = 0;
wdenk04a85b32004-04-15 18:22:41 +0000257 found = 0;
258 cmdv[0] = NULL;
259
Mike Frysinger560d4242010-12-17 16:51:59 -0500260 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
261 int vallen = strlen(match->key) + 1;
wdenk04a85b32004-04-15 18:22:41 +0000262
Mike Frysinger560d4242010-12-17 16:51:59 -0500263 if (found >= maxv - 2 || bufsz < vallen)
wdenk04a85b32004-04-15 18:22:41 +0000264 break;
Mike Frysinger560d4242010-12-17 16:51:59 -0500265
wdenk04a85b32004-04-15 18:22:41 +0000266 cmdv[found++] = buf;
Mike Frysinger560d4242010-12-17 16:51:59 -0500267 memcpy(buf, match->key, vallen);
268 buf += vallen;
269 bufsz -= vallen;
wdenk04a85b32004-04-15 18:22:41 +0000270 }
271
Mike Frysinger560d4242010-12-17 16:51:59 -0500272 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
273
274 if (idx)
275 cmdv[found++] = "...";
Igor Grinberg27aafe92011-11-07 01:14:11 +0000276
wdenk04a85b32004-04-15 18:22:41 +0000277 cmdv[found] = NULL;
278 return found;
279}
280#endif