]> Git Repo - J-u-boot.git/blame - env/common.c
env: Move callback definitions to env.h
[J-u-boot.git] / env / common.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
05706fdd 2/*
ea882baf 3 * (C) Copyright 2000-2010
05706fdd
WD
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <[email protected]>
05706fdd
WD
8 */
9
10#include <common.h>
11#include <command.h>
3f989e7b 12#include <env.h>
05706fdd 13#include <environment.h>
05706fdd 14#include <linux/stddef.h>
ea882baf
WD
15#include <search.h>
16#include <errno.h>
05706fdd
WD
17#include <malloc.h>
18
d87080b7
WD
19DECLARE_GLOBAL_DATA_PTR;
20
05706fdd
WD
21/************************************************************************
22 * Default settings to be used when no valid environment is found
23 */
ddd8418f 24#include <env_default.h>
05706fdd 25
c5983592 26struct hsearch_data env_htab = {
7d477654
PD
27#if CONFIG_IS_ENABLED(ENV_SUPPORT)
28 /* defined in flags.c, only compile with ENV_SUPPORT */
2598090b 29 .change_ok = env_flags_validate,
7d477654 30#endif
c5983592 31};
2eb1573f 32
ec8a252c
JH
33/*
34 * Read an environment variable as a boolean
35 * Return -1 if variable does not exist (default to true)
36 */
bfebc8c9 37int env_get_yesno(const char *var)
ec8a252c 38{
00caae6d 39 char *s = env_get(var);
ec8a252c
JH
40
41 if (s == NULL)
42 return -1;
43 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
44 1 : 0;
45}
46
267541f7
JH
47/*
48 * Look up the variable from the default environment
49 */
723806cc 50char *env_get_default(const char *name)
267541f7
JH
51{
52 char *ret_val;
53 unsigned long really_valid = gd->env_valid;
54 unsigned long real_gd_flags = gd->flags;
55
56 /* Pretend that the image is bad. */
57 gd->flags &= ~GD_FLG_ENV_READY;
2d7cb5b4 58 gd->env_valid = ENV_INVALID;
00caae6d 59 ret_val = env_get(name);
267541f7
JH
60 gd->env_valid = really_valid;
61 gd->flags = real_gd_flags;
62 return ret_val;
63}
64
0ac7d722 65void env_set_default(const char *s, int flags)
5bb12dbd
HW
66{
67 if (sizeof(default_environment) > ENV_SIZE) {
ea882baf 68 puts("*** Error - default environment is too large\n\n");
5bb12dbd
HW
69 return;
70 }
71
ea882baf 72 if (s) {
c5d548a9 73 if ((flags & H_INTERACTIVE) == 0) {
ea882baf 74 printf("*** Warning - %s, "
c5d548a9 75 "using default environment\n\n", s);
ea882baf
WD
76 } else {
77 puts(s);
78 }
79 } else {
58ae9990 80 debug("Using default environment\n");
ea882baf
WD
81 }
82
2eb1573f 83 if (himport_r(&env_htab, (char *)default_environment,
ecd1446f 84 sizeof(default_environment), '\0', flags, 0,
c4e0057f 85 0, NULL) == 0)
6c90f623
QS
86 pr_err("## Error: Environment import failed: errno = %d\n",
87 errno);
27aafe98 88
ea882baf 89 gd->flags |= GD_FLG_ENV_READY;
340b0e3b 90 gd->flags |= GD_FLG_ENV_DEFAULT;
5bb12dbd
HW
91}
92
b64b7c3d
GF
93
94/* [re]set individual variables to their value in the default environment */
0b9d8a05 95int env_set_default_vars(int nvars, char * const vars[], int flags)
b64b7c3d
GF
96{
97 /*
98 * Special use-case: import from default environment
99 * (and use \0 as a separator)
100 */
5a04264e 101 flags |= H_NOCLEAR;
b64b7c3d 102 return himport_r(&env_htab, (const char *)default_environment,
c4e0057f 103 sizeof(default_environment), '\0',
477f8116 104 flags, 0, nvars, vars);
b64b7c3d
GF
105}
106
ea882baf
WD
107/*
108 * Check if CRC is valid and (if yes) import the environment.
109 * Note that "buf" may or may not be aligned.
110 */
111int env_import(const char *buf, int check)
05706fdd 112{
ea882baf 113 env_t *ep = (env_t *)buf;
05706fdd 114
ea882baf
WD
115 if (check) {
116 uint32_t crc;
117
118 memcpy(&crc, &ep->crc, sizeof(crc));
119
120 if (crc32(0, ep->data, ENV_SIZE) != crc) {
0ac7d722 121 env_set_default("bad CRC", 0);
49d04c58 122 return -ENOMSG; /* needed for env_load() */
ea882baf
WD
123 }
124 }
125
ecd1446f 126 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
c4e0057f 127 0, NULL)) {
ea882baf 128 gd->flags |= GD_FLG_ENV_READY;
42a1820b 129 return 0;
ea882baf 130 }
05706fdd 131
9b643e31 132 pr_err("Cannot import environment: errno = %d\n", errno);
ea882baf 133
0ac7d722 134 env_set_default("import failed", 0);
ea882baf 135
42a1820b 136 return -EIO;
ea882baf
WD
137}
138
76768f5f
FA
139#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
140static unsigned char env_flags;
141
31f044bd
SG
142int env_import_redund(const char *buf1, int buf1_read_fail,
143 const char *buf2, int buf2_read_fail)
76768f5f
FA
144{
145 int crc1_ok, crc2_ok;
146 env_t *ep, *tmp_env1, *tmp_env2;
147
148 tmp_env1 = (env_t *)buf1;
149 tmp_env2 = (env_t *)buf2;
150
31f044bd
SG
151 if (buf1_read_fail && buf2_read_fail) {
152 puts("*** Error - No Valid Environment Area found\n");
153 } else if (buf1_read_fail || buf2_read_fail) {
154 puts("*** Warning - some problems detected ");
155 puts("reading environment; recovered successfully\n");
156 }
157
158 if (buf1_read_fail && buf2_read_fail) {
0ac7d722 159 env_set_default("bad env area", 0);
31f044bd
SG
160 return -EIO;
161 } else if (!buf1_read_fail && buf2_read_fail) {
162 gd->env_valid = ENV_VALID;
163 return env_import((char *)tmp_env1, 1);
164 } else if (buf1_read_fail && !buf2_read_fail) {
165 gd->env_valid = ENV_REDUND;
166 return env_import((char *)tmp_env2, 1);
167 }
168
76768f5f
FA
169 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
170 tmp_env1->crc;
171 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
172 tmp_env2->crc;
173
174 if (!crc1_ok && !crc2_ok) {
0ac7d722 175 env_set_default("bad CRC", 0);
49d04c58 176 return -ENOMSG; /* needed for env_load() */
76768f5f 177 } else if (crc1_ok && !crc2_ok) {
2d7cb5b4 178 gd->env_valid = ENV_VALID;
76768f5f 179 } else if (!crc1_ok && crc2_ok) {
2d7cb5b4 180 gd->env_valid = ENV_REDUND;
76768f5f
FA
181 } else {
182 /* both ok - check serial */
183 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
2d7cb5b4 184 gd->env_valid = ENV_REDUND;
76768f5f 185 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
2d7cb5b4 186 gd->env_valid = ENV_VALID;
76768f5f 187 else if (tmp_env1->flags > tmp_env2->flags)
2d7cb5b4 188 gd->env_valid = ENV_VALID;
76768f5f 189 else if (tmp_env2->flags > tmp_env1->flags)
2d7cb5b4 190 gd->env_valid = ENV_REDUND;
76768f5f 191 else /* flags are equal - almost impossible */
2d7cb5b4 192 gd->env_valid = ENV_VALID;
76768f5f
FA
193 }
194
2d7cb5b4 195 if (gd->env_valid == ENV_VALID)
76768f5f
FA
196 ep = tmp_env1;
197 else
198 ep = tmp_env2;
199
200 env_flags = ep->flags;
201 return env_import((char *)ep, 0);
202}
203#endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
204
fc0b5948 205/* Export the environment and generate CRC for it. */
7ce1526e
MV
206int env_export(env_t *env_out)
207{
208 char *res;
209 ssize_t len;
210
211 res = (char *)env_out->data;
212 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
213 if (len < 0) {
9b643e31 214 pr_err("Cannot export environment: errno = %d\n", errno);
7ce1526e
MV
215 return 1;
216 }
a4223b74 217
7ce1526e
MV
218 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
219
76768f5f
FA
220#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
221 env_out->flags = ++env_flags; /* increase the serial */
222#endif
223
7ce1526e
MV
224 return 0;
225}
226
27aafe98 227void env_relocate(void)
ea882baf 228{
2e5167cc 229#if defined(CONFIG_NEEDS_MANUAL_RELOC)
60f7da1f 230 env_reloc();
7bcdf195 231 env_fix_drivers();
7d477654
PD
232
233 if (env_htab.change_ok)
234 env_htab.change_ok += gd->reloc_off;
60f7da1f 235#endif
2d7cb5b4 236 if (gd->env_valid == ENV_INVALID) {
7ac2fe2d
IY
237#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
238 /* Environment not changable */
0ac7d722 239 env_set_default(NULL, 0);
05706fdd 240#else
770605e4 241 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
0ac7d722 242 env_set_default("bad CRC", 0);
d259079d 243#endif
ea882baf 244 } else {
310fb14b 245 env_load();
05706fdd 246 }
05706fdd 247}
04a85b3b 248
03dcf17d
BB
249#ifdef CONFIG_AUTO_COMPLETE
250int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
251 bool dollar_comp)
04a85b3b 252{
560d424b
MF
253 ENTRY *match;
254 int found, idx;
04a85b3b 255
03dcf17d
BB
256 if (dollar_comp) {
257 /*
258 * When doing $ completion, the first character should
259 * obviously be a '$'.
260 */
261 if (var[0] != '$')
262 return 0;
263
264 var++;
265
266 /*
267 * The second one, if present, should be a '{', as some
268 * configuration of the u-boot shell expand ${var} but not
269 * $var.
270 */
271 if (var[0] == '{')
272 var++;
273 else if (var[0] != '\0')
274 return 0;
275 }
276
560d424b 277 idx = 0;
04a85b3b
WD
278 found = 0;
279 cmdv[0] = NULL;
280
03dcf17d 281
560d424b
MF
282 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
283 int vallen = strlen(match->key) + 1;
04a85b3b 284
03dcf17d
BB
285 if (found >= maxv - 2 ||
286 bufsz < vallen + (dollar_comp ? 3 : 0))
04a85b3b 287 break;
560d424b 288
04a85b3b 289 cmdv[found++] = buf;
03dcf17d
BB
290
291 /* Add the '${' prefix to each var when doing $ completion. */
292 if (dollar_comp) {
293 strcpy(buf, "${");
294 buf += 2;
295 bufsz -= 3;
296 }
297
560d424b
MF
298 memcpy(buf, match->key, vallen);
299 buf += vallen;
300 bufsz -= vallen;
03dcf17d
BB
301
302 if (dollar_comp) {
303 /*
304 * This one is a bit odd: vallen already contains the
305 * '\0' character but we need to add the '}' suffix,
306 * hence the buf - 1 here. strcpy() will add the '\0'
307 * character just after '}'. buf is then incremented
308 * to account for the extra '}' we just added.
309 */
310 strcpy(buf - 1, "}");
311 buf++;
312 }
04a85b3b
WD
313 }
314
560d424b
MF
315 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
316
317 if (idx)
03dcf17d 318 cmdv[found++] = dollar_comp ? "${...}" : "...";
27aafe98 319
04a85b3b
WD
320 cmdv[found] = NULL;
321 return found;
322}
323#endif
This page took 0.563775 seconds and 4 git commands to generate.