2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <environment.h>
14 #include <linux/stddef.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /************************************************************************
22 * Default settings to be used when no valid environment is found
24 #include <env_default.h>
26 struct hsearch_data env_htab = {
27 .change_ok = env_flags_validate,
30 static uchar __env_get_char_spec(int index)
32 return *((uchar *)(gd->env_addr + index));
34 uchar env_get_char_spec(int)
35 __attribute__((weak, alias("__env_get_char_spec")));
37 static uchar env_get_char_init(int index)
39 /* if crc was bad, use the default environment */
41 return env_get_char_spec(index);
43 return default_environment[index];
46 uchar env_get_char_memory(int index)
48 return *env_get_addr(index);
51 uchar env_get_char(int index)
53 /* if relocated to RAM */
54 if (gd->flags & GD_FLG_RELOC)
55 return env_get_char_memory(index);
57 return env_get_char_init(index);
60 const uchar *env_get_addr(int index)
63 return (uchar *)(gd->env_addr + index);
65 return &default_environment[index];
69 * Read an environment variable as a boolean
70 * Return -1 if variable does not exist (default to true)
72 int getenv_yesno(const char *var)
74 char *s = getenv(var);
78 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
83 * Look up the variable from the default environment
85 char *getenv_default(const char *name)
88 unsigned long really_valid = gd->env_valid;
89 unsigned long real_gd_flags = gd->flags;
91 /* Pretend that the image is bad. */
92 gd->flags &= ~GD_FLG_ENV_READY;
94 ret_val = getenv(name);
95 gd->env_valid = really_valid;
96 gd->flags = real_gd_flags;
100 void set_default_env(const char *s)
104 if (sizeof(default_environment) > ENV_SIZE) {
105 puts("*** Error - default environment is too large\n\n");
111 printf("*** Warning - %s, "
112 "using default environment\n\n",
115 flags = H_INTERACTIVE;
119 puts("Using default environment\n\n");
122 if (himport_r(&env_htab, (char *)default_environment,
123 sizeof(default_environment), '\0', flags,
125 error("Environment import failed: errno = %d\n", errno);
127 gd->flags |= GD_FLG_ENV_READY;
131 /* [re]set individual variables to their value in the default environment */
132 int set_default_vars(int nvars, char * const vars[])
135 * Special use-case: import from default environment
136 * (and use \0 as a separator)
138 return himport_r(&env_htab, (const char *)default_environment,
139 sizeof(default_environment), '\0',
140 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
143 #ifdef CONFIG_ENV_AES
146 * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment
148 * This function shall return 16-byte array containing AES-128 key used
149 * to encrypt and decrypt the environment. This function must be overriden
150 * by the implementer as otherwise the environment encryption will not
153 __weak uint8_t *env_aes_cbc_get_key(void)
158 static int env_aes_cbc_crypt(env_t *env, const int enc)
160 unsigned char *data = env->data;
162 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
165 key = env_aes_cbc_get_key();
169 /* First we expand the key. */
170 aes_expand_key(key, key_exp);
172 /* Calculate the number of AES blocks to encrypt. */
173 aes_blocks = ENV_SIZE / AES_KEY_LENGTH;
176 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
178 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
183 static inline int env_aes_cbc_crypt(env_t *env, const int enc)
190 * Check if CRC is valid and (if yes) import the environment.
191 * Note that "buf" may or may not be aligned.
193 int env_import(const char *buf, int check)
195 env_t *ep = (env_t *)buf;
201 memcpy(&crc, &ep->crc, sizeof(crc));
203 if (crc32(0, ep->data, ENV_SIZE) != crc) {
204 set_default_env("!bad CRC");
209 /* Decrypt the env if desired. */
210 ret = env_aes_cbc_crypt(ep, 0);
212 error("Failed to decrypt env!\n");
213 set_default_env("!import failed");
217 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
219 gd->flags |= GD_FLG_ENV_READY;
223 error("Cannot import environment: errno = %d\n", errno);
225 set_default_env("!import failed");
230 /* Emport the environment and generate CRC for it. */
231 int env_export(env_t *env_out)
237 res = (char *)env_out->data;
238 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
240 error("Cannot export environment: errno = %d\n", errno);
244 /* Encrypt the env if desired. */
245 ret = env_aes_cbc_crypt(env_out, 1);
249 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
254 void env_relocate(void)
256 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
258 env_htab.change_ok += gd->reloc_off;
260 if (gd->env_valid == 0) {
261 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
262 /* Environment not changable */
263 set_default_env(NULL);
265 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
266 set_default_env("!bad CRC");
273 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
274 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
283 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
284 int vallen = strlen(match->key) + 1;
286 if (found >= maxv - 2 || bufsz < vallen)
290 memcpy(buf, match->key, vallen);
295 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
298 cmdv[found++] = "...";