blob: 906b41fccada86dc41d58d23ad42f74e7d5476b2 [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 +000037/************************************************************************
38 * Default settings to be used when no valid environment is found
39 */
Joe Hershberger81321142012-10-12 08:48:51 +000040#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000041
Gerlando Falautod3925952012-08-24 00:11:39 +000042struct hsearch_data env_htab = {
Joe Hershberger71497d02012-12-11 22:16:31 -060043 .change_ok = env_flags_validate,
Gerlando Falautod3925952012-08-24 00:11:39 +000044};
Mike Frysinger92d7a992010-12-08 06:26:04 -050045
Igor Grinberg76c02dc2011-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 Grinberg3e773592011-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 Grinberg3e773592011-11-07 01:14:11 +000057 return env_get_char_spec(index);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020058 else
Igor Grinberg3e773592011-11-07 01:14:11 +000059 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000060}
61
Igor Grinberg3e773592011-11-07 01:14:11 +000062uchar env_get_char_memory(int index)
wdenkbb444c92002-12-07 00:20:59 +000063{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020064 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +000065}
66
Igor Grinberg3e773592011-11-07 01:14:11 +000067uchar env_get_char(int index)
Joakim Tjernlund304b3592008-07-06 12:30:09 +020068{
Joakim Tjernlund304b3592008-07-06 12:30:09 +020069 /* if relocated to RAM */
70 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg3e773592011-11-07 01:14:11 +000071 return env_get_char_memory(index);
Joakim Tjernlund304b3592008-07-06 12:30:09 +020072 else
Igor Grinberg3e773592011-11-07 01:14:11 +000073 return env_get_char_init(index);
Joakim Tjernlund304b3592008-07-06 12:30:09 +020074}
75
Igor Grinberg3e773592011-11-07 01:14:11 +000076const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +000077{
Wolfgang Denk460a9ff2010-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
Joe Hershberger864ec562012-12-11 22:16:22 -060084/*
85 * Read an environment variable as a boolean
86 * Return -1 if variable does not exist (default to true)
87 */
88int getenv_yesno(const char *var)
89{
90 char *s = getenv(var);
91
92 if (s == NULL)
93 return -1;
94 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
95 1 : 0;
96}
97
Joe Hershberger6fe26c92012-12-11 22:16:34 -060098/*
99 * Look up the variable from the default environment
100 */
101char *getenv_default(const char *name)
102{
103 char *ret_val;
104 unsigned long really_valid = gd->env_valid;
105 unsigned long real_gd_flags = gd->flags;
106
107 /* Pretend that the image is bad. */
108 gd->flags &= ~GD_FLG_ENV_READY;
109 gd->env_valid = 0;
110 ret_val = getenv(name);
111 gd->env_valid = really_valid;
112 gd->flags = real_gd_flags;
113 return ret_val;
114}
115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116void set_default_env(const char *s)
Harald Welte72eb50a2008-07-07 15:40:39 +0800117{
Joe Hershbergera46f7702012-12-11 22:16:19 -0600118 int flags = 0;
119
Harald Welte72eb50a2008-07-07 15:40:39 +0800120 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200121 puts("*** Error - default environment is too large\n\n");
Harald Welte72eb50a2008-07-07 15:40:39 +0800122 return;
123 }
124
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200125 if (s) {
126 if (*s == '!') {
127 printf("*** Warning - %s, "
128 "using default environment\n\n",
Igor Grinberg3e773592011-11-07 01:14:11 +0000129 s + 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200130 } else {
Joe Hershbergera46f7702012-12-11 22:16:19 -0600131 flags = H_INTERACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200132 puts(s);
133 }
134 } else {
135 puts("Using default environment\n\n");
136 }
137
Mike Frysinger92d7a992010-12-08 06:26:04 -0500138 if (himport_r(&env_htab, (char *)default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600139 sizeof(default_environment), '\0', flags,
140 0, NULL) == 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200141 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg3e773592011-11-07 01:14:11 +0000142
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200143 gd->flags |= GD_FLG_ENV_READY;
Harald Welte72eb50a2008-07-07 15:40:39 +0800144}
145
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000146
147/* [re]set individual variables to their value in the default environment */
148int set_default_vars(int nvars, char * const vars[])
149{
150 /*
151 * Special use-case: import from default environment
152 * (and use \0 as a separator)
153 */
154 return himport_r(&env_htab, (const char *)default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600155 sizeof(default_environment), '\0',
156 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000157}
158
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000159#ifndef CONFIG_SPL_BUILD
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200160/*
161 * Check if CRC is valid and (if yes) import the environment.
162 * Note that "buf" may or may not be aligned.
163 */
164int env_import(const char *buf, int check)
wdenk05706fd2002-09-28 17:48:27 +0000165{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200166 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000167
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200168 if (check) {
169 uint32_t crc;
170
171 memcpy(&crc, &ep->crc, sizeof(crc));
172
173 if (crc32(0, ep->data, ENV_SIZE) != crc) {
174 set_default_env("!bad CRC");
175 return 0;
176 }
177 }
wdenk05706fd2002-09-28 17:48:27 +0000178
Gerlando Falauto9b0e1ff2012-08-24 00:11:38 +0000179 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600180 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200181 gd->flags |= GD_FLG_ENV_READY;
182 return 1;
183 }
184
185 error("Cannot import environment: errno = %d\n", errno);
186
187 set_default_env("!import failed");
188
189 return 0;
190}
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000191#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200192
Igor Grinberg3e773592011-11-07 01:14:11 +0000193void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200194{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200195#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200196 env_reloc();
Joe Hershberger638e6592012-12-11 22:16:21 -0600197 env_htab.change_ok += gd->reloc_off;
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200198#endif
wdenk05706fd2002-09-28 17:48:27 +0000199 if (gd->env_valid == 0) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000200#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
201 /* Environment not changable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200202 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000203#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000204 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200205 set_default_env("!bad CRC");
Lei Wene0909be2010-10-10 12:36:40 +0800206#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200207 } else {
Igor Grinberg3e773592011-11-07 01:14:11 +0000208 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000209 }
wdenk05706fd2002-09-28 17:48:27 +0000210}
wdenk3902d702004-04-15 18:22:41 +0000211
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000212#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
wdenk3902d702004-04-15 18:22:41 +0000213int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
214{
Mike Frysinger8d882322010-12-17 16:51:59 -0500215 ENTRY *match;
216 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000217
Mike Frysinger8d882322010-12-17 16:51:59 -0500218 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000219 found = 0;
220 cmdv[0] = NULL;
221
Mike Frysinger8d882322010-12-17 16:51:59 -0500222 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
223 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000224
Mike Frysinger8d882322010-12-17 16:51:59 -0500225 if (found >= maxv - 2 || bufsz < vallen)
wdenk3902d702004-04-15 18:22:41 +0000226 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500227
wdenk3902d702004-04-15 18:22:41 +0000228 cmdv[found++] = buf;
Mike Frysinger8d882322010-12-17 16:51:59 -0500229 memcpy(buf, match->key, vallen);
230 buf += vallen;
231 bufsz -= vallen;
wdenk3902d702004-04-15 18:22:41 +0000232 }
233
Mike Frysinger8d882322010-12-17 16:51:59 -0500234 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
235
236 if (idx)
237 cmdv[found++] = "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000238
wdenk3902d702004-04-15 18:22:41 +0000239 cmdv[found] = NULL;
240 return found;
241}
242#endif