]>
Commit | Line | Data |
---|---|---|
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 | * SPDX-License-Identifier: GPL-2.0+ | |
9 | */ | |
10 | ||
11 | #include <common.h> | |
12 | #include <command.h> | |
13 | #include <environment.h> | |
14 | #include <linux/stddef.h> | |
15 | #include <search.h> | |
16 | #include <errno.h> | |
17 | #include <malloc.h> | |
18 | ||
19 | DECLARE_GLOBAL_DATA_PTR; | |
20 | ||
21 | /************************************************************************ | |
22 | * Default settings to be used when no valid environment is found | |
23 | */ | |
24 | #include <env_default.h> | |
25 | ||
26 | struct hsearch_data env_htab = { | |
27 | .change_ok = env_flags_validate, | |
28 | }; | |
29 | ||
30 | __weak uchar env_get_char_spec(int index) | |
31 | { | |
32 | return *((uchar *)(gd->env_addr + index)); | |
33 | } | |
34 | ||
35 | static uchar env_get_char_init(int index) | |
36 | { | |
37 | /* if crc was bad, use the default environment */ | |
38 | if (gd->env_valid) | |
39 | return env_get_char_spec(index); | |
40 | else | |
41 | return default_environment[index]; | |
42 | } | |
43 | ||
44 | uchar env_get_char_memory(int index) | |
45 | { | |
46 | return *env_get_addr(index); | |
47 | } | |
48 | ||
49 | uchar env_get_char(int index) | |
50 | { | |
51 | /* if relocated to RAM */ | |
52 | if (gd->flags & GD_FLG_RELOC) | |
53 | return env_get_char_memory(index); | |
54 | else | |
55 | return env_get_char_init(index); | |
56 | } | |
57 | ||
58 | const uchar *env_get_addr(int index) | |
59 | { | |
60 | if (gd->env_valid) | |
61 | return (uchar *)(gd->env_addr + index); | |
62 | else | |
63 | return &default_environment[index]; | |
64 | } | |
65 | ||
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 | ||
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 | ||
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, 0, | |
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, 0, nvars, vars); | |
139 | } | |
140 | ||
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 | ||
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) | |
192 | { | |
193 | env_t *ep = (env_t *)buf; | |
194 | int ret; | |
195 | ||
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 | ||
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 | ||
215 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, | |
216 | 0, NULL)) { | |
217 | gd->flags |= GD_FLG_ENV_READY; | |
218 | return 1; | |
219 | } | |
220 | ||
221 | error("Cannot import environment: errno = %d\n", errno); | |
222 | ||
223 | set_default_env("!import failed"); | |
224 | ||
225 | return 0; | |
226 | } | |
227 | ||
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; | |
233 | int ret; | |
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 | } | |
241 | ||
242 | /* Encrypt the env if desired. */ | |
243 | ret = env_aes_cbc_crypt(env_out, 1); | |
244 | if (ret) | |
245 | return ret; | |
246 | ||
247 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | void env_relocate(void) | |
253 | { | |
254 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) | |
255 | env_reloc(); | |
256 | env_htab.change_ok += gd->reloc_off; | |
257 | #endif | |
258 | if (gd->env_valid == 0) { | |
259 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) | |
260 | /* Environment not changable */ | |
261 | set_default_env(NULL); | |
262 | #else | |
263 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); | |
264 | set_default_env("!bad CRC"); | |
265 | #endif | |
266 | } else { | |
267 | env_relocate_spec(); | |
268 | } | |
269 | } | |
270 | ||
271 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) | |
272 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) | |
273 | { | |
274 | ENTRY *match; | |
275 | int found, idx; | |
276 | ||
277 | idx = 0; | |
278 | found = 0; | |
279 | cmdv[0] = NULL; | |
280 | ||
281 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { | |
282 | int vallen = strlen(match->key) + 1; | |
283 | ||
284 | if (found >= maxv - 2 || bufsz < vallen) | |
285 | break; | |
286 | ||
287 | cmdv[found++] = buf; | |
288 | memcpy(buf, match->key, vallen); | |
289 | buf += vallen; | |
290 | bufsz -= vallen; | |
291 | } | |
292 | ||
293 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); | |
294 | ||
295 | if (idx) | |
296 | cmdv[found++] = "..."; | |
297 | ||
298 | cmdv[found] = NULL; | |
299 | return found; | |
300 | } | |
301 | #endif |