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