]>
Commit | Line | Data |
---|---|---|
05706fdd | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
05706fdd WD |
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> | |
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 | |
0b7df656 | 30 | __weak uchar env_get_char_spec(int index) |
bcdfb8d5 | 31 | { |
5fc52540 | 32 | return *(uchar *)(gd->env_addr + index); |
05706fdd WD |
33 | } |
34 | ||
27aafe98 | 35 | uchar env_get_char(int index) |
b502611b | 36 | { |
5fc52540 SG |
37 | /* if env is not set up, or crc was bad, use the default environment */ |
38 | if (!gd->env_valid) | |
39 | return default_environment[index]; | |
b502611b | 40 | else |
81a4dea2 | 41 | return env_get_char_spec(index); |
b502611b JT |
42 | } |
43 | ||
ec8a252c JH |
44 | /* |
45 | * Read an environment variable as a boolean | |
46 | * Return -1 if variable does not exist (default to true) | |
47 | */ | |
48 | int getenv_yesno(const char *var) | |
49 | { | |
50 | char *s = getenv(var); | |
51 | ||
52 | if (s == NULL) | |
53 | return -1; | |
54 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? | |
55 | 1 : 0; | |
56 | } | |
57 | ||
267541f7 JH |
58 | /* |
59 | * Look up the variable from the default environment | |
60 | */ | |
61 | char *getenv_default(const char *name) | |
62 | { | |
63 | char *ret_val; | |
64 | unsigned long really_valid = gd->env_valid; | |
65 | unsigned long real_gd_flags = gd->flags; | |
66 | ||
67 | /* Pretend that the image is bad. */ | |
68 | gd->flags &= ~GD_FLG_ENV_READY; | |
69 | gd->env_valid = 0; | |
70 | ret_val = getenv(name); | |
71 | gd->env_valid = really_valid; | |
72 | gd->flags = real_gd_flags; | |
73 | return ret_val; | |
74 | } | |
75 | ||
ea882baf | 76 | void set_default_env(const char *s) |
5bb12dbd | 77 | { |
c4e0057f JH |
78 | int flags = 0; |
79 | ||
5bb12dbd | 80 | if (sizeof(default_environment) > ENV_SIZE) { |
ea882baf | 81 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
82 | return; |
83 | } | |
84 | ||
ea882baf WD |
85 | if (s) { |
86 | if (*s == '!') { | |
87 | printf("*** Warning - %s, " | |
88 | "using default environment\n\n", | |
27aafe98 | 89 | s + 1); |
ea882baf | 90 | } else { |
c4e0057f | 91 | flags = H_INTERACTIVE; |
ea882baf WD |
92 | puts(s); |
93 | } | |
94 | } else { | |
95 | puts("Using default environment\n\n"); | |
96 | } | |
97 | ||
2eb1573f | 98 | if (himport_r(&env_htab, (char *)default_environment, |
ecd1446f | 99 | sizeof(default_environment), '\0', flags, 0, |
c4e0057f | 100 | 0, NULL) == 0) |
ea882baf | 101 | error("Environment import failed: errno = %d\n", errno); |
27aafe98 | 102 | |
ea882baf | 103 | gd->flags |= GD_FLG_ENV_READY; |
340b0e3b | 104 | gd->flags |= GD_FLG_ENV_DEFAULT; |
5bb12dbd HW |
105 | } |
106 | ||
b64b7c3d GF |
107 | |
108 | /* [re]set individual variables to their value in the default environment */ | |
109 | int set_default_vars(int nvars, char * const vars[]) | |
110 | { | |
111 | /* | |
112 | * Special use-case: import from default environment | |
113 | * (and use \0 as a separator) | |
114 | */ | |
115 | return himport_r(&env_htab, (const char *)default_environment, | |
c4e0057f | 116 | sizeof(default_environment), '\0', |
ecd1446f | 117 | H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars); |
b64b7c3d GF |
118 | } |
119 | ||
a4223b74 | 120 | #ifdef CONFIG_ENV_AES |
b80c0b99 | 121 | #include <uboot_aes.h> |
a4223b74 MV |
122 | /** |
123 | * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment | |
124 | * | |
125 | * This function shall return 16-byte array containing AES-128 key used | |
62a3b7dd | 126 | * to encrypt and decrypt the environment. This function must be overridden |
a4223b74 MV |
127 | * by the implementer as otherwise the environment encryption will not |
128 | * work. | |
129 | */ | |
130 | __weak uint8_t *env_aes_cbc_get_key(void) | |
131 | { | |
132 | return NULL; | |
133 | } | |
134 | ||
135 | static int env_aes_cbc_crypt(env_t *env, const int enc) | |
136 | { | |
137 | unsigned char *data = env->data; | |
138 | uint8_t *key; | |
139 | uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; | |
140 | uint32_t aes_blocks; | |
141 | ||
142 | key = env_aes_cbc_get_key(); | |
143 | if (!key) | |
144 | return -EINVAL; | |
145 | ||
146 | /* First we expand the key. */ | |
147 | aes_expand_key(key, key_exp); | |
148 | ||
149 | /* Calculate the number of AES blocks to encrypt. */ | |
150 | aes_blocks = ENV_SIZE / AES_KEY_LENGTH; | |
151 | ||
152 | if (enc) | |
153 | aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks); | |
154 | else | |
155 | aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | #else | |
160 | static inline int env_aes_cbc_crypt(env_t *env, const int enc) | |
161 | { | |
162 | return 0; | |
163 | } | |
164 | #endif | |
165 | ||
ea882baf WD |
166 | /* |
167 | * Check if CRC is valid and (if yes) import the environment. | |
168 | * Note that "buf" may or may not be aligned. | |
169 | */ | |
170 | int env_import(const char *buf, int check) | |
05706fdd | 171 | { |
ea882baf | 172 | env_t *ep = (env_t *)buf; |
a4223b74 | 173 | int ret; |
05706fdd | 174 | |
ea882baf WD |
175 | if (check) { |
176 | uint32_t crc; | |
177 | ||
178 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
179 | ||
180 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
181 | set_default_env("!bad CRC"); | |
182 | return 0; | |
183 | } | |
184 | } | |
185 | ||
a4223b74 MV |
186 | /* Decrypt the env if desired. */ |
187 | ret = env_aes_cbc_crypt(ep, 0); | |
188 | if (ret) { | |
189 | error("Failed to decrypt env!\n"); | |
190 | set_default_env("!import failed"); | |
191 | return ret; | |
192 | } | |
193 | ||
ecd1446f | 194 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, |
c4e0057f | 195 | 0, NULL)) { |
ea882baf WD |
196 | gd->flags |= GD_FLG_ENV_READY; |
197 | return 1; | |
198 | } | |
05706fdd | 199 | |
ea882baf WD |
200 | error("Cannot import environment: errno = %d\n", errno); |
201 | ||
202 | set_default_env("!import failed"); | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
76768f5f FA |
207 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
208 | static unsigned char env_flags; | |
209 | ||
210 | int env_import_redund(const char *buf1, const char *buf2) | |
211 | { | |
212 | int crc1_ok, crc2_ok; | |
213 | env_t *ep, *tmp_env1, *tmp_env2; | |
214 | ||
215 | tmp_env1 = (env_t *)buf1; | |
216 | tmp_env2 = (env_t *)buf2; | |
217 | ||
218 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == | |
219 | tmp_env1->crc; | |
220 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == | |
221 | tmp_env2->crc; | |
222 | ||
223 | if (!crc1_ok && !crc2_ok) { | |
224 | set_default_env("!bad CRC"); | |
225 | return 0; | |
226 | } else if (crc1_ok && !crc2_ok) { | |
227 | gd->env_valid = 1; | |
228 | } else if (!crc1_ok && crc2_ok) { | |
229 | gd->env_valid = 2; | |
230 | } else { | |
231 | /* both ok - check serial */ | |
232 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
233 | gd->env_valid = 2; | |
234 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) | |
235 | gd->env_valid = 1; | |
236 | else if (tmp_env1->flags > tmp_env2->flags) | |
237 | gd->env_valid = 1; | |
238 | else if (tmp_env2->flags > tmp_env1->flags) | |
239 | gd->env_valid = 2; | |
240 | else /* flags are equal - almost impossible */ | |
241 | gd->env_valid = 1; | |
242 | } | |
243 | ||
244 | if (gd->env_valid == 1) | |
245 | ep = tmp_env1; | |
246 | else | |
247 | ep = tmp_env2; | |
248 | ||
249 | env_flags = ep->flags; | |
250 | return env_import((char *)ep, 0); | |
251 | } | |
252 | #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ | |
253 | ||
fc0b5948 | 254 | /* Export the environment and generate CRC for it. */ |
7ce1526e MV |
255 | int env_export(env_t *env_out) |
256 | { | |
257 | char *res; | |
258 | ssize_t len; | |
a4223b74 | 259 | int ret; |
7ce1526e MV |
260 | |
261 | res = (char *)env_out->data; | |
262 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); | |
263 | if (len < 0) { | |
264 | error("Cannot export environment: errno = %d\n", errno); | |
265 | return 1; | |
266 | } | |
a4223b74 MV |
267 | |
268 | /* Encrypt the env if desired. */ | |
269 | ret = env_aes_cbc_crypt(env_out, 1); | |
270 | if (ret) | |
271 | return ret; | |
272 | ||
7ce1526e MV |
273 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
274 | ||
76768f5f FA |
275 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
276 | env_out->flags = ++env_flags; /* increase the serial */ | |
277 | #endif | |
278 | ||
7ce1526e MV |
279 | return 0; |
280 | } | |
281 | ||
27aafe98 | 282 | void env_relocate(void) |
ea882baf | 283 | { |
2e5167cc | 284 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f | 285 | env_reloc(); |
7afcf3a5 | 286 | env_htab.change_ok += gd->reloc_off; |
60f7da1f | 287 | #endif |
05706fdd | 288 | if (gd->env_valid == 0) { |
7ac2fe2d IY |
289 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
290 | /* Environment not changable */ | |
ea882baf | 291 | set_default_env(NULL); |
05706fdd | 292 | #else |
770605e4 | 293 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
ea882baf | 294 | set_default_env("!bad CRC"); |
d259079d | 295 | #endif |
ea882baf | 296 | } else { |
27aafe98 | 297 | env_relocate_spec(); |
05706fdd | 298 | } |
05706fdd | 299 | } |
04a85b3b | 300 | |
7ac2fe2d | 301 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
04a85b3b WD |
302 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
303 | { | |
560d424b MF |
304 | ENTRY *match; |
305 | int found, idx; | |
04a85b3b | 306 | |
560d424b | 307 | idx = 0; |
04a85b3b WD |
308 | found = 0; |
309 | cmdv[0] = NULL; | |
310 | ||
560d424b MF |
311 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
312 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 313 | |
560d424b | 314 | if (found >= maxv - 2 || bufsz < vallen) |
04a85b3b | 315 | break; |
560d424b | 316 | |
04a85b3b | 317 | cmdv[found++] = buf; |
560d424b MF |
318 | memcpy(buf, match->key, vallen); |
319 | buf += vallen; | |
320 | bufsz -= vallen; | |
04a85b3b WD |
321 | } |
322 | ||
560d424b MF |
323 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
324 | ||
325 | if (idx) | |
326 | cmdv[found++] = "..."; | |
27aafe98 | 327 | |
04a85b3b WD |
328 | cmdv[found] = NULL; |
329 | return found; | |
330 | } | |
331 | #endif |