]>
Commit | Line | Data |
---|---|---|
05706fdd | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
05706fdd WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
6 | * Andreas Heppel <[email protected]> | |
ea882baf | 7 | * |
3765b3e7 | 8 | * SPDX-License-Identifier: GPL-2.0+ |
05706fdd WD |
9 | */ |
10 | ||
11 | #include <common.h> | |
12 | #include <command.h> | |
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 |
19 | DECLARE_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 | 26 | struct hsearch_data env_htab = { |
2598090b | 27 | .change_ok = env_flags_validate, |
c5983592 | 28 | }; |
2eb1573f | 29 | |
ec8a252c JH |
30 | /* |
31 | * Read an environment variable as a boolean | |
32 | * Return -1 if variable does not exist (default to true) | |
33 | */ | |
bfebc8c9 | 34 | int env_get_yesno(const char *var) |
ec8a252c | 35 | { |
00caae6d | 36 | char *s = env_get(var); |
ec8a252c JH |
37 | |
38 | if (s == NULL) | |
39 | return -1; | |
40 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? | |
41 | 1 : 0; | |
42 | } | |
43 | ||
267541f7 JH |
44 | /* |
45 | * Look up the variable from the default environment | |
46 | */ | |
723806cc | 47 | char *env_get_default(const char *name) |
267541f7 JH |
48 | { |
49 | char *ret_val; | |
50 | unsigned long really_valid = gd->env_valid; | |
51 | unsigned long real_gd_flags = gd->flags; | |
52 | ||
53 | /* Pretend that the image is bad. */ | |
54 | gd->flags &= ~GD_FLG_ENV_READY; | |
2d7cb5b4 | 55 | gd->env_valid = ENV_INVALID; |
00caae6d | 56 | ret_val = env_get(name); |
267541f7 JH |
57 | gd->env_valid = really_valid; |
58 | gd->flags = real_gd_flags; | |
59 | return ret_val; | |
60 | } | |
61 | ||
ea882baf | 62 | void set_default_env(const char *s) |
5bb12dbd | 63 | { |
c4e0057f JH |
64 | int flags = 0; |
65 | ||
5bb12dbd | 66 | if (sizeof(default_environment) > ENV_SIZE) { |
ea882baf | 67 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
68 | return; |
69 | } | |
70 | ||
ea882baf WD |
71 | if (s) { |
72 | if (*s == '!') { | |
73 | printf("*** Warning - %s, " | |
74 | "using default environment\n\n", | |
27aafe98 | 75 | s + 1); |
ea882baf | 76 | } else { |
c4e0057f | 77 | flags = H_INTERACTIVE; |
ea882baf WD |
78 | puts(s); |
79 | } | |
80 | } else { | |
58ae9990 | 81 | debug("Using default environment\n"); |
ea882baf WD |
82 | } |
83 | ||
2eb1573f | 84 | if (himport_r(&env_htab, (char *)default_environment, |
ecd1446f | 85 | sizeof(default_environment), '\0', flags, 0, |
c4e0057f | 86 | 0, NULL) == 0) |
9b643e31 | 87 | pr_err("Environment import failed: errno = %d\n", 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 */ | |
95 | int set_default_vars(int nvars, char * const vars[]) | |
96 | { | |
97 | /* | |
98 | * Special use-case: import from default environment | |
99 | * (and use \0 as a separator) | |
100 | */ | |
101 | return himport_r(&env_htab, (const char *)default_environment, | |
c4e0057f | 102 | sizeof(default_environment), '\0', |
ecd1446f | 103 | H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars); |
b64b7c3d GF |
104 | } |
105 | ||
ea882baf WD |
106 | /* |
107 | * Check if CRC is valid and (if yes) import the environment. | |
108 | * Note that "buf" may or may not be aligned. | |
109 | */ | |
110 | int env_import(const char *buf, int check) | |
05706fdd | 111 | { |
ea882baf | 112 | env_t *ep = (env_t *)buf; |
05706fdd | 113 | |
ea882baf WD |
114 | if (check) { |
115 | uint32_t crc; | |
116 | ||
117 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
118 | ||
119 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
120 | set_default_env("!bad CRC"); | |
121 | return 0; | |
122 | } | |
123 | } | |
124 | ||
ecd1446f | 125 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, |
c4e0057f | 126 | 0, NULL)) { |
ea882baf WD |
127 | gd->flags |= GD_FLG_ENV_READY; |
128 | return 1; | |
129 | } | |
05706fdd | 130 | |
9b643e31 | 131 | pr_err("Cannot import environment: errno = %d\n", errno); |
ea882baf WD |
132 | |
133 | set_default_env("!import failed"); | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
76768f5f FA |
138 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
139 | static unsigned char env_flags; | |
140 | ||
141 | int env_import_redund(const char *buf1, const char *buf2) | |
142 | { | |
143 | int crc1_ok, crc2_ok; | |
144 | env_t *ep, *tmp_env1, *tmp_env2; | |
145 | ||
146 | tmp_env1 = (env_t *)buf1; | |
147 | tmp_env2 = (env_t *)buf2; | |
148 | ||
149 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == | |
150 | tmp_env1->crc; | |
151 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == | |
152 | tmp_env2->crc; | |
153 | ||
154 | if (!crc1_ok && !crc2_ok) { | |
155 | set_default_env("!bad CRC"); | |
156 | return 0; | |
157 | } else if (crc1_ok && !crc2_ok) { | |
2d7cb5b4 | 158 | gd->env_valid = ENV_VALID; |
76768f5f | 159 | } else if (!crc1_ok && crc2_ok) { |
2d7cb5b4 | 160 | gd->env_valid = ENV_REDUND; |
76768f5f FA |
161 | } else { |
162 | /* both ok - check serial */ | |
163 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
2d7cb5b4 | 164 | gd->env_valid = ENV_REDUND; |
76768f5f | 165 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
2d7cb5b4 | 166 | gd->env_valid = ENV_VALID; |
76768f5f | 167 | else if (tmp_env1->flags > tmp_env2->flags) |
2d7cb5b4 | 168 | gd->env_valid = ENV_VALID; |
76768f5f | 169 | else if (tmp_env2->flags > tmp_env1->flags) |
2d7cb5b4 | 170 | gd->env_valid = ENV_REDUND; |
76768f5f | 171 | else /* flags are equal - almost impossible */ |
2d7cb5b4 | 172 | gd->env_valid = ENV_VALID; |
76768f5f FA |
173 | } |
174 | ||
2d7cb5b4 | 175 | if (gd->env_valid == ENV_VALID) |
76768f5f FA |
176 | ep = tmp_env1; |
177 | else | |
178 | ep = tmp_env2; | |
179 | ||
180 | env_flags = ep->flags; | |
181 | return env_import((char *)ep, 0); | |
182 | } | |
183 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
184 | ||
fc0b5948 | 185 | /* Export the environment and generate CRC for it. */ |
7ce1526e MV |
186 | int env_export(env_t *env_out) |
187 | { | |
188 | char *res; | |
189 | ssize_t len; | |
190 | ||
191 | res = (char *)env_out->data; | |
192 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); | |
193 | if (len < 0) { | |
9b643e31 | 194 | pr_err("Cannot export environment: errno = %d\n", errno); |
7ce1526e MV |
195 | return 1; |
196 | } | |
a4223b74 | 197 | |
7ce1526e MV |
198 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
199 | ||
76768f5f FA |
200 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
201 | env_out->flags = ++env_flags; /* increase the serial */ | |
202 | #endif | |
203 | ||
7ce1526e MV |
204 | return 0; |
205 | } | |
206 | ||
27aafe98 | 207 | void env_relocate(void) |
ea882baf | 208 | { |
2e5167cc | 209 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f | 210 | env_reloc(); |
7afcf3a5 | 211 | env_htab.change_ok += gd->reloc_off; |
60f7da1f | 212 | #endif |
2d7cb5b4 | 213 | if (gd->env_valid == ENV_INVALID) { |
7ac2fe2d IY |
214 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
215 | /* Environment not changable */ | |
ea882baf | 216 | set_default_env(NULL); |
05706fdd | 217 | #else |
770605e4 | 218 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
ea882baf | 219 | set_default_env("!bad CRC"); |
d259079d | 220 | #endif |
ea882baf | 221 | } else { |
310fb14b | 222 | env_load(); |
05706fdd | 223 | } |
05706fdd | 224 | } |
04a85b3b | 225 | |
7ac2fe2d | 226 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
04a85b3b WD |
227 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
228 | { | |
560d424b MF |
229 | ENTRY *match; |
230 | int found, idx; | |
04a85b3b | 231 | |
560d424b | 232 | idx = 0; |
04a85b3b WD |
233 | found = 0; |
234 | cmdv[0] = NULL; | |
235 | ||
560d424b MF |
236 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
237 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 238 | |
560d424b | 239 | if (found >= maxv - 2 || bufsz < vallen) |
04a85b3b | 240 | break; |
560d424b | 241 | |
04a85b3b | 242 | cmdv[found++] = buf; |
560d424b MF |
243 | memcpy(buf, match->key, vallen); |
244 | buf += vallen; | |
245 | bufsz -= vallen; | |
04a85b3b WD |
246 | } |
247 | ||
560d424b MF |
248 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
249 | ||
250 | if (idx) | |
251 | cmdv[found++] = "..."; | |
27aafe98 | 252 | |
04a85b3b WD |
253 | cmdv[found] = NULL; |
254 | return found; | |
255 | } | |
256 | #endif |