]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_common.c
env: Add environment variable flags
[karo-tx-uboot.git] / common / env_common.c
1 /*
2  * (C) Copyright 2000-2010
3  * 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>
7  *
8  * 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>
30 #include <linux/stddef.h>
31 #include <search.h>
32 #include <errno.h>
33 #include <malloc.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 /************************************************************************
38  * Default settings to be used when no valid environment is found
39  */
40 #include <env_default.h>
41
42 struct hsearch_data env_htab = {
43         .change_ok = env_flags_validate,
44 };
45
46 static uchar __env_get_char_spec(int index)
47 {
48         return *((uchar *)(gd->env_addr + index));
49 }
50 uchar env_get_char_spec(int)
51         __attribute__((weak, alias("__env_get_char_spec")));
52
53 static uchar env_get_char_init(int index)
54 {
55         /* if crc was bad, use the default environment */
56         if (gd->env_valid)
57                 return env_get_char_spec(index);
58         else
59                 return default_environment[index];
60 }
61
62 uchar env_get_char_memory(int index)
63 {
64         return *env_get_addr(index);
65 }
66
67 uchar env_get_char(int index)
68 {
69         /* if relocated to RAM */
70         if (gd->flags & GD_FLG_RELOC)
71                 return env_get_char_memory(index);
72         else
73                 return env_get_char_init(index);
74 }
75
76 const uchar *env_get_addr(int index)
77 {
78         if (gd->env_valid)
79                 return (uchar *)(gd->env_addr + index);
80         else
81                 return &default_environment[index];
82 }
83
84 /*
85  * Read an environment variable as a boolean
86  * Return -1 if variable does not exist (default to true)
87  */
88 int 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
98 void set_default_env(const char *s)
99 {
100         int flags = 0;
101
102         if (sizeof(default_environment) > ENV_SIZE) {
103                 puts("*** Error - default environment is too large\n\n");
104                 return;
105         }
106
107         if (s) {
108                 if (*s == '!') {
109                         printf("*** Warning - %s, "
110                                 "using default environment\n\n",
111                                 s + 1);
112                 } else {
113                         flags = H_INTERACTIVE;
114                         puts(s);
115                 }
116         } else {
117                 puts("Using default environment\n\n");
118         }
119
120         if (himport_r(&env_htab, (char *)default_environment,
121                         sizeof(default_environment), '\0', flags,
122                         0, NULL) == 0)
123                 error("Environment import failed: errno = %d\n", errno);
124
125         gd->flags |= GD_FLG_ENV_READY;
126 }
127
128
129 /* [re]set individual variables to their value in the default environment */
130 int set_default_vars(int nvars, char * const vars[])
131 {
132         /*
133          * Special use-case: import from default environment
134          * (and use \0 as a separator)
135          */
136         return himport_r(&env_htab, (const char *)default_environment,
137                                 sizeof(default_environment), '\0',
138                                 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
139 }
140
141 #ifndef CONFIG_SPL_BUILD
142 /*
143  * Check if CRC is valid and (if yes) import the environment.
144  * Note that "buf" may or may not be aligned.
145  */
146 int env_import(const char *buf, int check)
147 {
148         env_t *ep = (env_t *)buf;
149
150         if (check) {
151                 uint32_t crc;
152
153                 memcpy(&crc, &ep->crc, sizeof(crc));
154
155                 if (crc32(0, ep->data, ENV_SIZE) != crc) {
156                         set_default_env("!bad CRC");
157                         return 0;
158                 }
159         }
160
161         if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
162                         0, NULL)) {
163                 gd->flags |= GD_FLG_ENV_READY;
164                 return 1;
165         }
166
167         error("Cannot import environment: errno = %d\n", errno);
168
169         set_default_env("!import failed");
170
171         return 0;
172 }
173 #endif
174
175 void env_relocate(void)
176 {
177 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
178         env_reloc();
179         env_htab.change_ok += gd->reloc_off;
180 #endif
181         if (gd->env_valid == 0) {
182 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
183                 /* Environment not changable */
184                 set_default_env(NULL);
185 #else
186                 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
187                 set_default_env("!bad CRC");
188 #endif
189         } else {
190                 env_relocate_spec();
191         }
192 }
193
194 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
195 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
196 {
197         ENTRY *match;
198         int found, idx;
199
200         idx = 0;
201         found = 0;
202         cmdv[0] = NULL;
203
204         while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
205                 int vallen = strlen(match->key) + 1;
206
207                 if (found >= maxv - 2 || bufsz < vallen)
208                         break;
209
210                 cmdv[found++] = buf;
211                 memcpy(buf, match->key, vallen);
212                 buf += vallen;
213                 bufsz -= vallen;
214         }
215
216         qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
217
218         if (idx)
219                 cmdv[found++] = "...";
220
221         cmdv[found] = NULL;
222         return found;
223 }
224 #endif