blob: 61c4be54d0ea28e22e0f937235d3b27483a60a67 [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 */
wdenk05706fd2002-09-28 17:48:27 +000040
Mike Frysinger147c7162011-04-23 23:43:21 +000041const uchar default_environment[] = {
wdenk05706fd2002-09-28 17:48:27 +000042#ifdef CONFIG_BOOTARGS
43 "bootargs=" CONFIG_BOOTARGS "\0"
44#endif
45#ifdef CONFIG_BOOTCOMMAND
46 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
47#endif
48#ifdef CONFIG_RAMBOOTCOMMAND
49 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
50#endif
51#ifdef CONFIG_NFSBOOTCOMMAND
52 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
53#endif
54#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
Marek Vasut5368c552012-09-23 17:41:24 +020055 "bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0"
wdenk05706fd2002-09-28 17:48:27 +000056#endif
57#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
Marek Vasut5368c552012-09-23 17:41:24 +020058 "baudrate=" __stringify(CONFIG_BAUDRATE) "\0"
wdenk05706fd2002-09-28 17:48:27 +000059#endif
60#ifdef CONFIG_LOADS_ECHO
Marek Vasut5368c552012-09-23 17:41:24 +020061 "loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0"
wdenk05706fd2002-09-28 17:48:27 +000062#endif
63#ifdef CONFIG_ETHADDR
Marek Vasut5368c552012-09-23 17:41:24 +020064 "ethaddr=" __stringify(CONFIG_ETHADDR) "\0"
wdenk05706fd2002-09-28 17:48:27 +000065#endif
66#ifdef CONFIG_ETH1ADDR
Marek Vasut5368c552012-09-23 17:41:24 +020067 "eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0"
wdenk05706fd2002-09-28 17:48:27 +000068#endif
69#ifdef CONFIG_ETH2ADDR
Marek Vasut5368c552012-09-23 17:41:24 +020070 "eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0"
wdenk05706fd2002-09-28 17:48:27 +000071#endif
wdenk69459792004-05-29 16:53:29 +000072#ifdef CONFIG_ETH3ADDR
Marek Vasut5368c552012-09-23 17:41:24 +020073 "eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0"
wdenk69459792004-05-29 16:53:29 +000074#endif
richardretanubunc68a05f2008-09-29 18:28:23 -040075#ifdef CONFIG_ETH4ADDR
Marek Vasut5368c552012-09-23 17:41:24 +020076 "eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0"
richardretanubunc68a05f2008-09-29 18:28:23 -040077#endif
78#ifdef CONFIG_ETH5ADDR
Marek Vasut5368c552012-09-23 17:41:24 +020079 "eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0"
richardretanubunc68a05f2008-09-29 18:28:23 -040080#endif
Benoît Thébaudeau84209af2012-08-10 07:45:31 +000081#ifdef CONFIG_ETHPRIME
82 "ethprime=" CONFIG_ETHPRIME "\0"
83#endif
wdenk05706fd2002-09-28 17:48:27 +000084#ifdef CONFIG_IPADDR
Marek Vasut5368c552012-09-23 17:41:24 +020085 "ipaddr=" __stringify(CONFIG_IPADDR) "\0"
wdenk05706fd2002-09-28 17:48:27 +000086#endif
87#ifdef CONFIG_SERVERIP
Marek Vasut5368c552012-09-23 17:41:24 +020088 "serverip=" __stringify(CONFIG_SERVERIP) "\0"
wdenk05706fd2002-09-28 17:48:27 +000089#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090#ifdef CONFIG_SYS_AUTOLOAD
Igor Grinberg27aafe92011-11-07 01:14:11 +000091 "autoload=" CONFIG_SYS_AUTOLOAD "\0"
wdenk05706fd2002-09-28 17:48:27 +000092#endif
93#ifdef CONFIG_PREBOOT
94 "preboot=" CONFIG_PREBOOT "\0"
95#endif
96#ifdef CONFIG_ROOTPATH
Joe Hershberger8b3637c2011-10-13 13:03:47 +000097 "rootpath=" CONFIG_ROOTPATH "\0"
wdenk05706fd2002-09-28 17:48:27 +000098#endif
99#ifdef CONFIG_GATEWAYIP
Marek Vasut5368c552012-09-23 17:41:24 +0200100 "gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0"
wdenk05706fd2002-09-28 17:48:27 +0000101#endif
102#ifdef CONFIG_NETMASK
Marek Vasut5368c552012-09-23 17:41:24 +0200103 "netmask=" __stringify(CONFIG_NETMASK) "\0"
wdenk05706fd2002-09-28 17:48:27 +0000104#endif
105#ifdef CONFIG_HOSTNAME
Marek Vasut5368c552012-09-23 17:41:24 +0200106 "hostname=" __stringify(CONFIG_HOSTNAME) "\0"
wdenk05706fd2002-09-28 17:48:27 +0000107#endif
108#ifdef CONFIG_BOOTFILE
Joe Hershbergerb3f44c22011-10-13 13:03:48 +0000109 "bootfile=" CONFIG_BOOTFILE "\0"
wdenk05706fd2002-09-28 17:48:27 +0000110#endif
111#ifdef CONFIG_LOADADDR
Marek Vasut5368c552012-09-23 17:41:24 +0200112 "loadaddr=" __stringify(CONFIG_LOADADDR) "\0"
wdenk05706fd2002-09-28 17:48:27 +0000113#endif
Igor Grinberg27aafe92011-11-07 01:14:11 +0000114#ifdef CONFIG_CLOCKS_IN_MHZ
wdenk05706fd2002-09-28 17:48:27 +0000115 "clocks_in_mhz=1\0"
116#endif
stroesead10dd92003-02-14 11:21:23 +0000117#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
Marek Vasut5368c552012-09-23 17:41:24 +0200118 "pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY)"\0"
stroesead10dd92003-02-14 11:21:23 +0000119#endif
Stephen Warren5e724ca2012-05-22 09:21:54 +0000120#ifdef CONFIG_ENV_VARS_UBOOT_CONFIG
121 "arch=" CONFIG_SYS_ARCH "\0"
122 "cpu=" CONFIG_SYS_CPU "\0"
123 "board=" CONFIG_SYS_BOARD "\0"
124#ifdef CONFIG_SYS_VENDOR
125 "vendor=" CONFIG_SYS_VENDOR "\0"
126#endif
127#ifdef CONFIG_SYS_SOC
128 "soc=" CONFIG_SYS_SOC "\0"
129#endif
130#endif
Igor Grinberg27aafe92011-11-07 01:14:11 +0000131#ifdef CONFIG_EXTRA_ENV_SETTINGS
wdenk05706fd2002-09-28 17:48:27 +0000132 CONFIG_EXTRA_ENV_SETTINGS
133#endif
134 "\0"
135};
136
Gerlando Falautoc5983592012-08-24 00:11:39 +0000137struct hsearch_data env_htab = {
138 .apply = env_check_apply,
139};
Mike Frysinger2eb15732010-12-08 06:26:04 -0500140
Igor Grinbergbf95df42011-12-26 03:33:10 +0000141static uchar __env_get_char_spec(int index)
142{
143 return *((uchar *)(gd->env_addr + index));
144}
145uchar env_get_char_spec(int)
146 __attribute__((weak, alias("__env_get_char_spec")));
147
Igor Grinberg27aafe92011-11-07 01:14:11 +0000148static uchar env_get_char_init(int index)
wdenk05706fd2002-09-28 17:48:27 +0000149{
wdenk05706fd2002-09-28 17:48:27 +0000150 /* if crc was bad, use the default environment */
151 if (gd->env_valid)
Igor Grinberg27aafe92011-11-07 01:14:11 +0000152 return env_get_char_spec(index);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 else
Igor Grinberg27aafe92011-11-07 01:14:11 +0000154 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +0000155}
156
Igor Grinberg27aafe92011-11-07 01:14:11 +0000157uchar env_get_char_memory(int index)
wdenk05706fd2002-09-28 17:48:27 +0000158{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200159 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +0000160}
161
Igor Grinberg27aafe92011-11-07 01:14:11 +0000162uchar env_get_char(int index)
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200163{
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200164 /* if relocated to RAM */
165 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg27aafe92011-11-07 01:14:11 +0000166 return env_get_char_memory(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200167 else
Igor Grinberg27aafe92011-11-07 01:14:11 +0000168 return env_get_char_init(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +0200169}
170
Igor Grinberg27aafe92011-11-07 01:14:11 +0000171const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +0000172{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200173 if (gd->env_valid)
174 return (uchar *)(gd->env_addr + index);
175 else
176 return &default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +0000177}
178
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200179void set_default_env(const char *s)
Harald Welte5bb12db2008-07-07 15:40:39 +0800180{
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000181 /*
182 * By default, do not apply changes as they will eventually
183 * be applied by someone else
184 */
185 int do_apply = 0;
Harald Welte5bb12db2008-07-07 15:40:39 +0800186 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200187 puts("*** Error - default environment is too large\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +0800188 return;
189 }
190
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200191 if (s) {
192 if (*s == '!') {
193 printf("*** Warning - %s, "
194 "using default environment\n\n",
Igor Grinberg27aafe92011-11-07 01:14:11 +0000195 s + 1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200196 } else {
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000197 /*
198 * This set_to_default was explicitly asked for
199 * by the user, as opposed to being a recovery
200 * mechanism. Therefore we check every single
201 * variable and apply changes to the system
202 * right away (e.g. baudrate, console).
203 */
204 do_apply = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200205 puts(s);
206 }
207 } else {
208 puts("Using default environment\n\n");
209 }
210
Mike Frysinger2eb15732010-12-08 06:26:04 -0500211 if (himport_r(&env_htab, (char *)default_environment,
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000212 sizeof(default_environment), '\0', 0,
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000213 0, NULL, do_apply) == 0)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200214 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg27aafe92011-11-07 01:14:11 +0000215
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200216 gd->flags |= GD_FLG_ENV_READY;
217}
218
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000219
220/* [re]set individual variables to their value in the default environment */
221int set_default_vars(int nvars, char * const vars[])
222{
223 /*
224 * Special use-case: import from default environment
225 * (and use \0 as a separator)
226 */
227 return himport_r(&env_htab, (const char *)default_environment,
228 sizeof(default_environment), '\0', H_NOCLEAR,
229 nvars, vars, 1 /* do_apply */);
230}
231
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000232#ifndef CONFIG_SPL_BUILD
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200233/*
234 * Check if CRC is valid and (if yes) import the environment.
235 * Note that "buf" may or may not be aligned.
236 */
237int env_import(const char *buf, int check)
238{
239 env_t *ep = (env_t *)buf;
240
241 if (check) {
242 uint32_t crc;
243
244 memcpy(&crc, &ep->crc, sizeof(crc));
245
246 if (crc32(0, ep->data, ENV_SIZE) != crc) {
247 set_default_env("!bad CRC");
248 return 0;
249 }
250 }
251
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000252 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
Gerlando Falautoc5983592012-08-24 00:11:39 +0000253 0, NULL, 0 /* do_apply */)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200254 gd->flags |= GD_FLG_ENV_READY;
255 return 1;
256 }
257
258 error("Cannot import environment: errno = %d\n", errno);
259
260 set_default_env("!import failed");
261
262 return 0;
Harald Welte5bb12db2008-07-07 15:40:39 +0800263}
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000264#endif
Harald Welte5bb12db2008-07-07 15:40:39 +0800265
Igor Grinberg27aafe92011-11-07 01:14:11 +0000266void env_relocate(void)
wdenk05706fd2002-09-28 17:48:27 +0000267{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200268#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +0200269 env_reloc();
270#endif
wdenk05706fd2002-09-28 17:48:27 +0000271 if (gd->env_valid == 0) {
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000272#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
273 /* Environment not changable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200274 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000275#else
Simon Glass770605e2012-02-13 13:51:18 +0000276 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200277 set_default_env("!bad CRC");
Lei Wend2590792010-10-10 12:36:40 +0800278#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200279 } else {
Igor Grinberg27aafe92011-11-07 01:14:11 +0000280 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000281 }
wdenk05706fd2002-09-28 17:48:27 +0000282}
wdenk04a85b32004-04-15 18:22:41 +0000283
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000284#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
wdenk04a85b32004-04-15 18:22:41 +0000285int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
286{
Mike Frysinger560d4242010-12-17 16:51:59 -0500287 ENTRY *match;
288 int found, idx;
wdenk04a85b32004-04-15 18:22:41 +0000289
Mike Frysinger560d4242010-12-17 16:51:59 -0500290 idx = 0;
wdenk04a85b32004-04-15 18:22:41 +0000291 found = 0;
292 cmdv[0] = NULL;
293
Mike Frysinger560d4242010-12-17 16:51:59 -0500294 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
295 int vallen = strlen(match->key) + 1;
wdenk04a85b32004-04-15 18:22:41 +0000296
Mike Frysinger560d4242010-12-17 16:51:59 -0500297 if (found >= maxv - 2 || bufsz < vallen)
wdenk04a85b32004-04-15 18:22:41 +0000298 break;
Mike Frysinger560d4242010-12-17 16:51:59 -0500299
wdenk04a85b32004-04-15 18:22:41 +0000300 cmdv[found++] = buf;
Mike Frysinger560d4242010-12-17 16:51:59 -0500301 memcpy(buf, match->key, vallen);
302 buf += vallen;
303 bufsz -= vallen;
wdenk04a85b32004-04-15 18:22:41 +0000304 }
305
Mike Frysinger560d4242010-12-17 16:51:59 -0500306 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
307
308 if (idx)
309 cmdv[found++] = "...";
Igor Grinberg27aafe92011-11-07 01:14:11 +0000310
wdenk04a85b32004-04-15 18:22:41 +0000311 cmdv[found] = NULL;
312 return found;
313}
314#endif