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 __weak uchar env_get_char_spec(int index)
32 return *((uchar *)(gd->env_addr + index));
35 static uchar env_get_char_init(int index)
37 /* if crc was bad, use the default environment */
39 return env_get_char_spec(index);
41 return default_environment[index];
44 uchar env_get_char_memory(int index)
46 return *env_get_addr(index);
49 uchar env_get_char(int index)
51 /* if relocated to RAM */
52 if (gd->flags & GD_FLG_RELOC)
53 return env_get_char_memory(index);
55 return env_get_char_init(index);
58 const uchar *env_get_addr(int index)
61 return (uchar *)(gd->env_addr + index);
63 return &default_environment[index];
67 * Read an environment variable as a boolean
68 * Return -1 if variable does not exist (default to true)
70 int getenv_yesno(const char *var)
72 char *s = getenv(var);
76 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
81 * Look up the variable from the default environment
83 char *getenv_default(const char *name)
86 unsigned long really_valid = gd->env_valid;
87 unsigned long real_gd_flags = gd->flags;
89 /* Pretend that the image is bad. */
90 gd->flags &= ~GD_FLG_ENV_READY;
92 ret_val = getenv(name);
93 gd->env_valid = really_valid;
94 gd->flags = real_gd_flags;
98 void set_default_env(const char *s)
102 if (sizeof(default_environment) > ENV_SIZE) {
103 puts("*** Error - default environment is too large\n\n");
109 printf("*** Warning - %s, "
110 "using default environment\n\n",
113 flags = H_INTERACTIVE;
117 puts("Using default environment\n\n");
120 if (himport_r(&env_htab, (char *)default_environment,
121 sizeof(default_environment), '\0', flags, 0,
123 error("Environment import failed: errno = %d\n", errno);
125 gd->flags |= GD_FLG_ENV_READY;
126 gd->flags |= GD_FLG_ENV_DEFAULT;
130 /* [re]set individual variables to their value in the default environment */
131 int set_default_vars(int nvars, char * const vars[])
134 * Special use-case: import from default environment
135 * (and use \0 as a separator)
137 return himport_r(&env_htab, (const char *)default_environment,
138 sizeof(default_environment), '\0',
139 H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars);
142 #ifdef CONFIG_ENV_AES
145 * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment
147 * This function shall return 16-byte array containing AES-128 key used
148 * to encrypt and decrypt the environment. This function must be overriden
149 * by the implementer as otherwise the environment encryption will not
152 __weak uint8_t *env_aes_cbc_get_key(void)
157 static int env_aes_cbc_crypt(env_t *env, const int enc)
159 unsigned char *data = env->data;
161 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
164 key = env_aes_cbc_get_key();
168 /* First we expand the key. */
169 aes_expand_key(key, key_exp);
171 /* Calculate the number of AES blocks to encrypt. */
172 aes_blocks = ENV_SIZE / AES_KEY_LENGTH;
175 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
177 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
182 static inline int env_aes_cbc_crypt(env_t *env, const int enc)
189 * Check if CRC is valid and (if yes) import the environment.
190 * Note that "buf" may or may not be aligned.
192 int env_import(const char *buf, int check)
194 env_t *ep = (env_t *)buf;
200 memcpy(&crc, &ep->crc, sizeof(crc));
202 if (crc32(0, ep->data, ENV_SIZE) != crc) {
203 set_default_env("!bad CRC");
208 /* Decrypt the env if desired. */
209 ret = env_aes_cbc_crypt(ep, 0);
211 error("Failed to decrypt env!\n");
212 set_default_env("!import failed");
216 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
218 gd->flags |= GD_FLG_ENV_READY;
222 error("Cannot import environment: errno = %d\n", errno);
224 set_default_env("!import failed");
229 /* Emport the environment and generate CRC for it. */
230 int env_export(env_t *env_out)
236 res = (char *)env_out->data;
237 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
239 error("Cannot export environment: errno = %d\n", errno);
243 /* Encrypt the env if desired. */
244 ret = env_aes_cbc_crypt(env_out, 1);
248 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
253 void env_relocate(void)
255 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
257 env_htab.change_ok += gd->reloc_off;
259 if (gd->env_valid == 0) {
260 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
261 /* Environment not changable */
262 set_default_env(NULL);
264 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
265 set_default_env("!bad CRC");
272 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
273 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
282 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
283 int vallen = strlen(match->key) + 1;
285 if (found >= maxv - 2 || bufsz < vallen)
289 memcpy(buf, match->key, vallen);
294 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
297 cmdv[found++] = "...";