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,
123 error("Environment import failed: errno = %d\n", errno);
125 gd->flags |= GD_FLG_ENV_READY;
129 /* [re]set individual variables to their value in the default environment */
130 int set_default_vars(int nvars, char * const vars[])
133 * Special use-case: import from default environment
134 * (and use \0 as a separator)
136 return himport_r(&env_htab, (const char *)default_environment,
137 sizeof(default_environment), '\0',
138 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
141 #ifdef CONFIG_ENV_AES
144 * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment
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
151 __weak uint8_t *env_aes_cbc_get_key(void)
156 static int env_aes_cbc_crypt(env_t *env, const int enc)
158 unsigned char *data = env->data;
160 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
163 key = env_aes_cbc_get_key();
167 /* First we expand the key. */
168 aes_expand_key(key, key_exp);
170 /* Calculate the number of AES blocks to encrypt. */
171 aes_blocks = ENV_SIZE / AES_KEY_LENGTH;
174 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
176 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
181 static inline int env_aes_cbc_crypt(env_t *env, const int enc)
188 * Check if CRC is valid and (if yes) import the environment.
189 * Note that "buf" may or may not be aligned.
191 int env_import(const char *buf, int check)
193 env_t *ep = (env_t *)buf;
199 memcpy(&crc, &ep->crc, sizeof(crc));
201 if (crc32(0, ep->data, ENV_SIZE) != crc) {
202 set_default_env("!bad CRC");
207 /* Decrypt the env if desired. */
208 ret = env_aes_cbc_crypt(ep, 0);
210 error("Failed to decrypt env!\n");
211 set_default_env("!import failed");
215 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
217 gd->flags |= GD_FLG_ENV_READY;
221 error("Cannot import environment: errno = %d\n", errno);
223 set_default_env("!import failed");
228 /* Emport the environment and generate CRC for it. */
229 int env_export(env_t *env_out)
235 res = (char *)env_out->data;
236 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
238 error("Cannot export environment: errno = %d\n", errno);
242 /* Encrypt the env if desired. */
243 ret = env_aes_cbc_crypt(env_out, 1);
247 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
252 void env_relocate(void)
254 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
256 env_htab.change_ok += gd->reloc_off;
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);
263 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
264 set_default_env("!bad CRC");
271 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
272 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
281 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
282 int vallen = strlen(match->key) + 1;
284 if (found >= maxv - 2 || bufsz < vallen)
288 memcpy(buf, match->key, vallen);
293 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
296 cmdv[found++] = "...";