blob: 6845f8d8d542945f90b005f8fcef1e261092c9c8 [file] [log] [blame]
wdenk05706fd2002-09-28 17:48:27 +00001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenk05706fd2002-09-28 17:48:27 +00003 * 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>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02007 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk05706fd2002-09-28 17:48:27 +00009 */
10
11#include <common.h>
12#include <command.h>
13#include <environment.h>
wdenk05706fd2002-09-28 17:48:27 +000014#include <linux/stddef.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020015#include <search.h>
16#include <errno.h>
wdenk05706fd2002-09-28 17:48:27 +000017#include <malloc.h>
18
Wolfgang Denk6405a152006-03-31 18:32:53 +020019DECLARE_GLOBAL_DATA_PTR;
20
wdenk05706fd2002-09-28 17:48:27 +000021/************************************************************************
22 * Default settings to be used when no valid environment is found
23 */
Joe Hershberger81321142012-10-12 08:48:51 +000024#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000025
Gerlando Falautod3925952012-08-24 00:11:39 +000026struct hsearch_data env_htab = {
Joe Hershberger71497d02012-12-11 22:16:31 -060027 .change_ok = env_flags_validate,
Gerlando Falautod3925952012-08-24 00:11:39 +000028};
Mike Frysinger92d7a992010-12-08 06:26:04 -050029
Jeroen Hofstee82d92da2014-07-12 15:22:13 +020030__weak uchar env_get_char_spec(int index)
Igor Grinberg76c02dc2011-12-26 03:33:10 +000031{
32 return *((uchar *)(gd->env_addr + index));
33}
Igor Grinberg76c02dc2011-12-26 03:33:10 +000034
Igor Grinberg3e773592011-11-07 01:14:11 +000035static uchar env_get_char_init(int index)
wdenk05706fd2002-09-28 17:48:27 +000036{
wdenk05706fd2002-09-28 17:48:27 +000037 /* if crc was bad, use the default environment */
38 if (gd->env_valid)
Igor Grinberg3e773592011-11-07 01:14:11 +000039 return env_get_char_spec(index);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020040 else
Igor Grinberg3e773592011-11-07 01:14:11 +000041 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000042}
43
Igor Grinberg3e773592011-11-07 01:14:11 +000044uchar env_get_char_memory(int index)
wdenkbb444c92002-12-07 00:20:59 +000045{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020046 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +000047}
48
Igor Grinberg3e773592011-11-07 01:14:11 +000049uchar env_get_char(int index)
Joakim Tjernlund304b3592008-07-06 12:30:09 +020050{
Joakim Tjernlund304b3592008-07-06 12:30:09 +020051 /* if relocated to RAM */
52 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg3e773592011-11-07 01:14:11 +000053 return env_get_char_memory(index);
Joakim Tjernlund304b3592008-07-06 12:30:09 +020054 else
Igor Grinberg3e773592011-11-07 01:14:11 +000055 return env_get_char_init(index);
Joakim Tjernlund304b3592008-07-06 12:30:09 +020056}
57
Igor Grinberg3e773592011-11-07 01:14:11 +000058const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +000059{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020060 if (gd->env_valid)
61 return (uchar *)(gd->env_addr + index);
62 else
63 return &default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000064}
65
Joe Hershberger864ec562012-12-11 22:16:22 -060066/*
67 * Read an environment variable as a boolean
68 * Return -1 if variable does not exist (default to true)
69 */
70int 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
Joe Hershberger6fe26c92012-12-11 22:16:34 -060080/*
81 * Look up the variable from the default environment
82 */
83char *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
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020098void set_default_env(const char *s)
Harald Welte72eb50a2008-07-07 15:40:39 +080099{
Joe Hershbergera46f7702012-12-11 22:16:19 -0600100 int flags = 0;
101
Harald Welte72eb50a2008-07-07 15:40:39 +0800102 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200103 puts("*** Error - default environment is too large\n\n");
Harald Welte72eb50a2008-07-07 15:40:39 +0800104 return;
105 }
106
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200107 if (s) {
108 if (*s == '!') {
109 printf("*** Warning - %s, "
110 "using default environment\n\n",
Igor Grinberg3e773592011-11-07 01:14:11 +0000111 s + 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200112 } else {
Joe Hershbergera46f7702012-12-11 22:16:19 -0600113 flags = H_INTERACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200114 puts(s);
115 }
116 } else {
117 puts("Using default environment\n\n");
118 }
119
Mike Frysinger92d7a992010-12-08 06:26:04 -0500120 if (himport_r(&env_htab, (char *)default_environment,
Alexander Holler3e12be72014-07-14 17:49:55 +0200121 sizeof(default_environment), '\0', flags, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600122 0, NULL) == 0)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200123 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg3e773592011-11-07 01:14:11 +0000124
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200125 gd->flags |= GD_FLG_ENV_READY;
Michal Simek32bb7c82016-05-30 16:06:54 +0200126 gd->flags |= GD_FLG_ENV_DEFAULT;
Harald Welte72eb50a2008-07-07 15:40:39 +0800127}
128
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000129
130/* [re]set individual variables to their value in the default environment */
131int set_default_vars(int nvars, char * const vars[])
132{
133 /*
134 * Special use-case: import from default environment
135 * (and use \0 as a separator)
136 */
137 return himport_r(&env_htab, (const char *)default_environment,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600138 sizeof(default_environment), '\0',
Alexander Holler3e12be72014-07-14 17:49:55 +0200139 H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars);
Gerlando Falautofe9f49d2012-08-24 00:11:41 +0000140}
141
Marek Vasut486112d2014-03-05 19:59:51 +0100142#ifdef CONFIG_ENV_AES
Stefano Babic4a243082017-04-05 18:08:00 +0200143#include <uboot_aes.h>
Marek Vasut486112d2014-03-05 19:59:51 +0100144/**
145 * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment
146 *
147 * This function shall return 16-byte array containing AES-128 key used
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400148 * to encrypt and decrypt the environment. This function must be overridden
Marek Vasut486112d2014-03-05 19:59:51 +0100149 * by the implementer as otherwise the environment encryption will not
150 * work.
151 */
152__weak uint8_t *env_aes_cbc_get_key(void)
153{
154 return NULL;
155}
156
157static int env_aes_cbc_crypt(env_t *env, const int enc)
158{
159 unsigned char *data = env->data;
160 uint8_t *key;
161 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
162 uint32_t aes_blocks;
163
164 key = env_aes_cbc_get_key();
165 if (!key)
166 return -EINVAL;
167
168 /* First we expand the key. */
169 aes_expand_key(key, key_exp);
170
171 /* Calculate the number of AES blocks to encrypt. */
172 aes_blocks = ENV_SIZE / AES_KEY_LENGTH;
173
174 if (enc)
175 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
176 else
177 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
178
179 return 0;
180}
181#else
182static inline int env_aes_cbc_crypt(env_t *env, const int enc)
183{
184 return 0;
185}
186#endif
187
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200188/*
189 * Check if CRC is valid and (if yes) import the environment.
190 * Note that "buf" may or may not be aligned.
191 */
192int env_import(const char *buf, int check)
wdenk05706fd2002-09-28 17:48:27 +0000193{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200194 env_t *ep = (env_t *)buf;
Marek Vasut486112d2014-03-05 19:59:51 +0100195 int ret;
wdenk05706fd2002-09-28 17:48:27 +0000196
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200197 if (check) {
198 uint32_t crc;
199
200 memcpy(&crc, &ep->crc, sizeof(crc));
201
202 if (crc32(0, ep->data, ENV_SIZE) != crc) {
203 set_default_env("!bad CRC");
204 return 0;
205 }
206 }
wdenk05706fd2002-09-28 17:48:27 +0000207
Marek Vasut486112d2014-03-05 19:59:51 +0100208 /* Decrypt the env if desired. */
209 ret = env_aes_cbc_crypt(ep, 0);
210 if (ret) {
211 error("Failed to decrypt env!\n");
212 set_default_env("!import failed");
213 return ret;
214 }
215
Alexander Holler3e12be72014-07-14 17:49:55 +0200216 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
Joe Hershbergera46f7702012-12-11 22:16:19 -0600217 0, NULL)) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200218 gd->flags |= GD_FLG_ENV_READY;
219 return 1;
220 }
221
222 error("Cannot import environment: errno = %d\n", errno);
223
224 set_default_env("!import failed");
225
226 return 0;
227}
228
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400229/* Export the environment and generate CRC for it. */
Marek Vasutd73c1292014-03-05 19:59:50 +0100230int env_export(env_t *env_out)
231{
232 char *res;
233 ssize_t len;
Marek Vasut486112d2014-03-05 19:59:51 +0100234 int ret;
Marek Vasutd73c1292014-03-05 19:59:50 +0100235
236 res = (char *)env_out->data;
237 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
238 if (len < 0) {
239 error("Cannot export environment: errno = %d\n", errno);
240 return 1;
241 }
Marek Vasut486112d2014-03-05 19:59:51 +0100242
243 /* Encrypt the env if desired. */
244 ret = env_aes_cbc_crypt(env_out, 1);
245 if (ret)
246 return ret;
247
Marek Vasutd73c1292014-03-05 19:59:50 +0100248 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
249
250 return 0;
251}
252
Igor Grinberg3e773592011-11-07 01:14:11 +0000253void env_relocate(void)
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200254{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200255#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200256 env_reloc();
Joe Hershberger638e6592012-12-11 22:16:21 -0600257 env_htab.change_ok += gd->reloc_off;
Heiko Schocherfaa0d2e2010-10-05 14:17:00 +0200258#endif
wdenk05706fd2002-09-28 17:48:27 +0000259 if (gd->env_valid == 0) {
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000260#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
261 /* Environment not changable */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200262 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000263#else
Simon Glass0169e6b2012-02-13 13:51:18 +0000264 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200265 set_default_env("!bad CRC");
Lei Wene0909be2010-10-10 12:36:40 +0800266#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200267 } else {
Igor Grinberg3e773592011-11-07 01:14:11 +0000268 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000269 }
wdenk05706fd2002-09-28 17:48:27 +0000270}
wdenk3902d702004-04-15 18:22:41 +0000271
Ilya Yanokf7a2c552012-09-18 00:22:50 +0000272#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
wdenk3902d702004-04-15 18:22:41 +0000273int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
274{
Mike Frysinger8d882322010-12-17 16:51:59 -0500275 ENTRY *match;
276 int found, idx;
wdenk3902d702004-04-15 18:22:41 +0000277
Mike Frysinger8d882322010-12-17 16:51:59 -0500278 idx = 0;
wdenk3902d702004-04-15 18:22:41 +0000279 found = 0;
280 cmdv[0] = NULL;
281
Mike Frysinger8d882322010-12-17 16:51:59 -0500282 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
283 int vallen = strlen(match->key) + 1;
wdenk3902d702004-04-15 18:22:41 +0000284
Mike Frysinger8d882322010-12-17 16:51:59 -0500285 if (found >= maxv - 2 || bufsz < vallen)
wdenk3902d702004-04-15 18:22:41 +0000286 break;
Mike Frysinger8d882322010-12-17 16:51:59 -0500287
wdenk3902d702004-04-15 18:22:41 +0000288 cmdv[found++] = buf;
Mike Frysinger8d882322010-12-17 16:51:59 -0500289 memcpy(buf, match->key, vallen);
290 buf += vallen;
291 bufsz -= vallen;
wdenk3902d702004-04-15 18:22:41 +0000292 }
293
Mike Frysinger8d882322010-12-17 16:51:59 -0500294 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
295
296 if (idx)
297 cmdv[found++] = "...";
Igor Grinberg3e773592011-11-07 01:14:11 +0000298
wdenk3902d702004-04-15 18:22:41 +0000299 cmdv[found] = NULL;
300 return found;
301}
302#endif