blob: 1ac337730c3ec0d1f467e951497cfa1c5be16c6b [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk05706fd2002-09-28 17:48:27 +00009 */
10
11#include <common.h>
12#include <command.h>
13#include <environment.h>
wdenk05706fd2002-09-28 17:48:27 +000014#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020015#include <search.h>
16#include <errno.h>
wdenk05706fd2002-09-28 17:48:27 +000017#include <malloc.h>
18
Wolfgang Denk6405a152006-03-31 18:32:53 +020019DECLARE_GLOBAL_DATA_PTR;
20
wdenk05706fd2002-09-28 17:48:27 +000021/************************************************************************
22 * Default settings to be used when no valid environment is found
23 */
Joe Hershberger81321142012-10-12 08:48:51 +000024#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000025
Gerlando Falautod3925952012-08-24 00:11:39 +000026struct hsearch_data env_htab = {
Joe Hershberger71497d02012-12-11 22:16:31 -060027 .change_ok = env_flags_validate,
Gerlando Falautod3925952012-08-24 00:11:39 +000028};
Mike Frysinger92d7a992010-12-08 06:26:04 -050029
Igor Grinberg76c02dc2011-12-26 03:33:10 +000030static uchar __env_get_char_spec(int index)
31{
32 return *((uchar *)(gd->env_addr + index));
33}
34uchar env_get_char_spec(int)
35 __attribute__((weak, alias("__env_get_char_spec")));
36
Igor Grinberg3e773592011-11-07 01:14:11 +000037static uchar env_get_char_init(int index)
wdenk05706fd2002-09-28 17:48:27 +000038{
wdenk05706fd2002-09-28 17:48:27 +000039 /* if crc was bad, use the default environment */
40 if (gd->env_valid)
Igor Grinberg3e773592011-11-07 01:14:11 +000041 return env_get_char_spec(index);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020042 else
Igor Grinberg3e773592011-11-07 01:14:11 +000043 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000044}
45
Igor Grinberg3e773592011-11-07 01:14:11 +000046uchar env_get_char_memory(int index)
wdenkbb444c92002-12-07 00:20:59 +000047{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020048 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +000049}
50
Igor Grinberg3e773592011-11-07 01:14:11 +000051uchar env_get_char(int index)
Joakim Tjernlund304b3592008-07-06 12:30:09 +020052{
Joakim Tjernlund304b3592008-07-06 12:30:09 +020053 /* if relocated to RAM */
54 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg3e773592011-11-07 01:14:11 +000055 return env_get_char_memory(index);
Joakim Tjernlund304b3592008-07-06 12:30:09 +020056 else
Igor Grinberg3e773592011-11-07 01:14:11 +000057 return env_get_char_init(index);
Joakim Tjernlund304b3592008-07-06 12:30:09 +020058}
59
Igor Grinberg3e773592011-11-07 01:14:11 +000060const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +000061{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020062 if (gd->env_valid)
63 return (uchar *)(gd->env_addr + index);
64 else
65 return &default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000066}
67
Joe Hershberger864ec562012-12-11 22:16:22 -060068/*
69 * Read an environment variable as a boolean
70 * Return -1 if variable does not exist (default to true)
71 */
72int getenv_yesno(const char *var)
73{
74 char *s = getenv(var);
75
76 if (s == NULL)
77 return -1;
78 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
79 1 : 0;
80}
81
Joe Hershberger6fe26c92012-12-11 22:16:34 -060082/*
83 * Look up the variable from the default environment
84 */
85char *getenv_default(const char *name)
86{
87 char *ret_val;
88 unsigned long really_valid = gd->env_valid;
89 unsigned long real_gd_flags = gd->flags;
90
91 /* Pretend that the image is bad. */
92 gd->flags &= ~GD_FLG_ENV_READY;
93 gd->env_valid = 0;
94 ret_val = getenv(name);
95 gd->env_valid = really_valid;
96 gd->flags = real_gd_flags;
97 return ret_val;
98}
99
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200100void set_default_env(const char *s)
Harald Welte72eb50a2008-07-07 15:40:39 +0800101{
Joe Hershbergera46f7702012-12-11 22:16:19 -0600102 int flags = 0;
103
Harald Welte72eb50a2008-07-07 15:40:39 +0800104 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200105 puts("*** Error - default environment is too large\n\n");
Harald Welte72eb50a2008-07-07 15:40:39 +0800106 return;
107 }
108
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200109 if (s) {
110 if (*s == '!') {
111 printf("*** Warning - %s, "
112 "using default environment\n\n",
Igor Grinberg3e773592011-11-07 01:14:11 +0000113 s + 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200114 } else {
Joe Hershbergera46f7702012-12-11 22:16:19 -0600115 flags = H_INTERACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 puts(s);
117 }
118 } else {
119 puts("Using default environment\n\n");
120 }
121
Mike Frysinger92d7a992010-12-08 06:26:04 -0500122 if (himport_r(&env_htab, (char *)default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600123 sizeof(default_environment), '\0', flags,
124 0, NULL) == 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200125 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg3e773592011-11-07 01:14:11 +0000126
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200127 gd->flags |= GD_FLG_ENV_READY;
Harald Welte72eb50a2008-07-07 15:40:39 +0800128}
129
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000130
131/* [re]set individual variables to their value in the default environment */
132int set_default_vars(int nvars, char * const vars[])
133{
134 /*
135 * Special use-case: import from default environment
136 * (and use \0 as a separator)
137 */
138 return himport_r(&env_htab, (const char *)default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600139 sizeof(default_environment), '\0',
140 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000141}
142
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200143/*
144 * Check if CRC is valid and (if yes) import the environment.
145 * Note that "buf" may or may not be aligned.
146 */
147int env_import(const char *buf, int check)
wdenk05706fd2002-09-28 17:48:27 +0000148{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200149 env_t *ep = (env_t *)buf;
wdenk05706fd2002-09-28 17:48:27 +0000150
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200151 if (check) {
152 uint32_t crc;
153
154 memcpy(&crc, &ep->crc, sizeof(crc));
155
156 if (crc32(0, ep->data, ENV_SIZE) != crc) {
157 set_default_env("!bad CRC");
158 return 0;
159 }
160 }
wdenk05706fd2002-09-28 17:48:27 +0000161
Gerlando Falauto9b0e1ff2012-08-24 00:11:38 +0000162 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600163 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200164 gd->flags |= GD_FLG_ENV_READY;
165 return 1;
166 }
167
168 error("Cannot import environment: errno = %d\n", errno);
169
170 set_default_env("!import failed");
171
172 return 0;
173}
174
Igor Grinberg3e773592011-11-07 01:14:11 +0000175void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200176{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200177#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200178 env_reloc();
Joe Hershberger638e6592012-12-11 22:16:21 -0600179 env_htab.change_ok += gd->reloc_off;
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200180#endif
wdenk05706fd2002-09-28 17:48:27 +0000181 if (gd->env_valid == 0) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000182#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
183 /* Environment not changable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200184 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000185#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000186 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200187 set_default_env("!bad CRC");
Lei Wene0909be2010-10-10 12:36:40 +0800188#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200189 } else {
Igor Grinberg3e773592011-11-07 01:14:11 +0000190 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000191 }
wdenk05706fd2002-09-28 17:48:27 +0000192}
wdenk3902d702004-04-15 18:22:41 +0000193
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000194#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
wdenk3902d702004-04-15 18:22:41 +0000195int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
196{
Mike Frysinger8d882322010-12-17 16:51:59 -0500197 ENTRY *match;
198 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000199
Mike Frysinger8d882322010-12-17 16:51:59 -0500200 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000201 found = 0;
202 cmdv[0] = NULL;
203
Mike Frysinger8d882322010-12-17 16:51:59 -0500204 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
205 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000206
Mike Frysinger8d882322010-12-17 16:51:59 -0500207 if (found >= maxv - 2 || bufsz < vallen)
wdenk3902d702004-04-15 18:22:41 +0000208 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500209
wdenk3902d702004-04-15 18:22:41 +0000210 cmdv[found++] = buf;
Mike Frysinger8d882322010-12-17 16:51:59 -0500211 memcpy(buf, match->key, vallen);
212 buf += vallen;
213 bufsz -= vallen;
wdenk3902d702004-04-15 18:22:41 +0000214 }
215
Mike Frysinger8d882322010-12-17 16:51:59 -0500216 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
217
218 if (idx)
219 cmdv[found++] = "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000220
wdenk3902d702004-04-15 18:22:41 +0000221 cmdv[found] = NULL;
222 return found;
223}
224#endif