blob: f22f5b968efaeb525948e8d316f3a93c8b90b585 [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 */
Joe Hershbergerddd84182012-10-12 08:48:51 +000040#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000041
Gerlando Falautoc5983592012-08-24 00:11:39 +000042struct hsearch_data env_htab = {
43 .apply = env_check_apply,
44};
Mike Frysinger2eb15732010-12-08 06:26:04 -050045
Igor Grinbergbf95df42011-12-26 03:33:10 +000046static uchar __env_get_char_spec(int index)
47{
48 return *((uchar *)(gd->env_addr + index));
49}
50uchar env_get_char_spec(int)
51 __attribute__((weak, alias("__env_get_char_spec")));
52
Igor Grinberg27aafe92011-11-07 01:14:11 +000053static uchar env_get_char_init(int index)
wdenk05706fd2002-09-28 17:48:27 +000054{
wdenk05706fd2002-09-28 17:48:27 +000055 /* if crc was bad, use the default environment */
56 if (gd->env_valid)
Igor Grinberg27aafe92011-11-07 01:14:11 +000057 return env_get_char_spec(index);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058 else
Igor Grinberg27aafe92011-11-07 01:14:11 +000059 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000060}
61
Igor Grinberg27aafe92011-11-07 01:14:11 +000062uchar env_get_char_memory(int index)
wdenk05706fd2002-09-28 17:48:27 +000063{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020064 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +000065}
66
Igor Grinberg27aafe92011-11-07 01:14:11 +000067uchar env_get_char(int index)
Joakim Tjernlundb5026112008-07-06 12:30:09 +020068{
Joakim Tjernlundb5026112008-07-06 12:30:09 +020069 /* if relocated to RAM */
70 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg27aafe92011-11-07 01:14:11 +000071 return env_get_char_memory(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +020072 else
Igor Grinberg27aafe92011-11-07 01:14:11 +000073 return env_get_char_init(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +020074}
75
Igor Grinberg27aafe92011-11-07 01:14:11 +000076const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +000077{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020078 if (gd->env_valid)
79 return (uchar *)(gd->env_addr + index);
80 else
81 return &default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000082}
83
Wolfgang Denkea882ba2010-06-20 23:33:59 +020084void set_default_env(const char *s)
Harald Welte5bb12db2008-07-07 15:40:39 +080085{
Joe Hershbergerc4e00572012-12-11 22:16:19 -060086 int flags = 0;
87
Harald Welte5bb12db2008-07-07 15:40:39 +080088 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +020089 puts("*** Error - default environment is too large\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +080090 return;
91 }
92
Wolfgang Denkea882ba2010-06-20 23:33:59 +020093 if (s) {
94 if (*s == '!') {
95 printf("*** Warning - %s, "
96 "using default environment\n\n",
Igor Grinberg27aafe92011-11-07 01:14:11 +000097 s + 1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020098 } else {
Joe Hershbergerc4e00572012-12-11 22:16:19 -060099 flags = H_INTERACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200100 puts(s);
101 }
102 } else {
103 puts("Using default environment\n\n");
104 }
105
Mike Frysinger2eb15732010-12-08 06:26:04 -0500106 if (himport_r(&env_htab, (char *)default_environment,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600107 sizeof(default_environment), '\0', flags,
108 0, NULL) == 0)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200109 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg27aafe92011-11-07 01:14:11 +0000110
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200111 gd->flags |= GD_FLG_ENV_READY;
112}
113
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000114
115/* [re]set individual variables to their value in the default environment */
116int set_default_vars(int nvars, char * const vars[])
117{
118 /*
119 * Special use-case: import from default environment
120 * (and use \0 as a separator)
121 */
122 return himport_r(&env_htab, (const char *)default_environment,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600123 sizeof(default_environment), '\0',
124 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000125}
126
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000127#ifndef CONFIG_SPL_BUILD
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200128/*
129 * Check if CRC is valid and (if yes) import the environment.
130 * Note that "buf" may or may not be aligned.
131 */
132int env_import(const char *buf, int check)
133{
134 env_t *ep = (env_t *)buf;
135
136 if (check) {
137 uint32_t crc;
138
139 memcpy(&crc, &ep->crc, sizeof(crc));
140
141 if (crc32(0, ep->data, ENV_SIZE) != crc) {
142 set_default_env("!bad CRC");
143 return 0;
144 }
145 }
146
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000147 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600148 0, NULL)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200149 gd->flags |= GD_FLG_ENV_READY;
150 return 1;
151 }
152
153 error("Cannot import environment: errno = %d\n", errno);
154
155 set_default_env("!import failed");
156
157 return 0;
Harald Welte5bb12db2008-07-07 15:40:39 +0800158}
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000159#endif
Harald Welte5bb12db2008-07-07 15:40:39 +0800160
Igor Grinberg27aafe92011-11-07 01:14:11 +0000161void env_relocate(void)
wdenk05706fd2002-09-28 17:48:27 +0000162{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200163#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +0200164 env_reloc();
165#endif
wdenk05706fd2002-09-28 17:48:27 +0000166 if (gd->env_valid == 0) {
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000167#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
168 /* Environment not changable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200169 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000170#else
Simon Glass770605e2012-02-13 13:51:18 +0000171 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200172 set_default_env("!bad CRC");
Lei Wend2590792010-10-10 12:36:40 +0800173#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200174 } else {
Igor Grinberg27aafe92011-11-07 01:14:11 +0000175 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000176 }
wdenk05706fd2002-09-28 17:48:27 +0000177}
wdenk04a85b32004-04-15 18:22:41 +0000178
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000179#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
wdenk04a85b32004-04-15 18:22:41 +0000180int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
181{
Mike Frysinger560d4242010-12-17 16:51:59 -0500182 ENTRY *match;
183 int found, idx;
wdenk04a85b32004-04-15 18:22:41 +0000184
Mike Frysinger560d4242010-12-17 16:51:59 -0500185 idx = 0;
wdenk04a85b32004-04-15 18:22:41 +0000186 found = 0;
187 cmdv[0] = NULL;
188
Mike Frysinger560d4242010-12-17 16:51:59 -0500189 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
190 int vallen = strlen(match->key) + 1;
wdenk04a85b32004-04-15 18:22:41 +0000191
Mike Frysinger560d4242010-12-17 16:51:59 -0500192 if (found >= maxv - 2 || bufsz < vallen)
wdenk04a85b32004-04-15 18:22:41 +0000193 break;
Mike Frysinger560d4242010-12-17 16:51:59 -0500194
wdenk04a85b32004-04-15 18:22:41 +0000195 cmdv[found++] = buf;
Mike Frysinger560d4242010-12-17 16:51:59 -0500196 memcpy(buf, match->key, vallen);
197 buf += vallen;
198 bufsz -= vallen;
wdenk04a85b32004-04-15 18:22:41 +0000199 }
200
Mike Frysinger560d4242010-12-17 16:51:59 -0500201 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
202
203 if (idx)
204 cmdv[found++] = "...";
Igor Grinberg27aafe92011-11-07 01:14:11 +0000205
wdenk04a85b32004-04-15 18:22:41 +0000206 cmdv[found] = NULL;
207 return found;
208}
209#endif