blob: 3b979bcb9869996d14937aeba5c2856a7147ae3a [file] [log] [blame]
wdenk05706fd2002-09-28 17:48:27 +00001/*
Wolfgang Denkea882ba2010-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 Denkea882ba2010-06-20 23:33:59 +02007 *
Wolfgang Denk3765b3e2013-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 Denkea882ba2010-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 Denkd87080b2006-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 Hershbergerddd84182012-10-12 08:48:51 +000024#include <env_default.h>
wdenk05706fd2002-09-28 17:48:27 +000025
Gerlando Falautoc5983592012-08-24 00:11:39 +000026struct hsearch_data env_htab = {
Joe Hershberger25980902012-12-11 22:16:31 -060027 .change_ok = env_flags_validate,
Gerlando Falautoc5983592012-08-24 00:11:39 +000028};
Mike Frysinger2eb15732010-12-08 06:26:04 -050029
Jeroen Hofstee0b7df652014-07-12 15:22:13 +020030__weak uchar env_get_char_spec(int index)
Igor Grinbergbf95df42011-12-26 03:33:10 +000031{
32 return *((uchar *)(gd->env_addr + index));
33}
Igor Grinbergbf95df42011-12-26 03:33:10 +000034
Igor Grinberg27aafe92011-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 Grinberg27aafe92011-11-07 01:14:11 +000039 return env_get_char_spec(index);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020040 else
Igor Grinberg27aafe92011-11-07 01:14:11 +000041 return default_environment[index];
wdenk05706fd2002-09-28 17:48:27 +000042}
43
Igor Grinberg27aafe92011-11-07 01:14:11 +000044uchar env_get_char_memory(int index)
wdenk05706fd2002-09-28 17:48:27 +000045{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020046 return *env_get_addr(index);
wdenk05706fd2002-09-28 17:48:27 +000047}
48
Igor Grinberg27aafe92011-11-07 01:14:11 +000049uchar env_get_char(int index)
Joakim Tjernlundb5026112008-07-06 12:30:09 +020050{
Joakim Tjernlundb5026112008-07-06 12:30:09 +020051 /* if relocated to RAM */
52 if (gd->flags & GD_FLG_RELOC)
Igor Grinberg27aafe92011-11-07 01:14:11 +000053 return env_get_char_memory(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +020054 else
Igor Grinberg27aafe92011-11-07 01:14:11 +000055 return env_get_char_init(index);
Joakim Tjernlundb5026112008-07-06 12:30:09 +020056}
57
Igor Grinberg27aafe92011-11-07 01:14:11 +000058const uchar *env_get_addr(int index)
wdenk05706fd2002-09-28 17:48:27 +000059{
Wolfgang Denkea882ba2010-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 Hershbergerec8a2522012-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 Hershberger267541f2012-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 Denkea882ba2010-06-20 23:33:59 +020098void set_default_env(const char *s)
Harald Welte5bb12db2008-07-07 15:40:39 +080099{
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600100 int flags = 0;
101
Harald Welte5bb12db2008-07-07 15:40:39 +0800102 if (sizeof(default_environment) > ENV_SIZE) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200103 puts("*** Error - default environment is too large\n\n");
Harald Welte5bb12db2008-07-07 15:40:39 +0800104 return;
105 }
106
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200107 if (s) {
108 if (*s == '!') {
109 printf("*** Warning - %s, "
110 "using default environment\n\n",
Igor Grinberg27aafe92011-11-07 01:14:11 +0000111 s + 1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200112 } else {
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600113 flags = H_INTERACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200114 puts(s);
115 }
116 } else {
117 puts("Using default environment\n\n");
118 }
119
Mike Frysinger2eb15732010-12-08 06:26:04 -0500120 if (himport_r(&env_htab, (char *)default_environment,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600121 sizeof(default_environment), '\0', flags,
122 0, NULL) == 0)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200123 error("Environment import failed: errno = %d\n", errno);
Igor Grinberg27aafe92011-11-07 01:14:11 +0000124
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200125 gd->flags |= GD_FLG_ENV_READY;
126}
127
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000128
129/* [re]set individual variables to their value in the default environment */
130int 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,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600137 sizeof(default_environment), '\0',
138 H_NOCLEAR | H_INTERACTIVE, nvars, vars);
Gerlando Falautob64b7c32012-08-24 00:11:41 +0000139}
140
Marek Vasuta4223b72014-03-05 19:59:51 +0100141#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
156static 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
181static inline int env_aes_cbc_crypt(env_t *env, const int enc)
182{
183 return 0;
184}
185#endif
186
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200187/*
188 * Check if CRC is valid and (if yes) import the environment.
189 * Note that "buf" may or may not be aligned.
190 */
191int env_import(const char *buf, int check)
192{
193 env_t *ep = (env_t *)buf;
Marek Vasuta4223b72014-03-05 19:59:51 +0100194 int ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200195
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
Marek Vasuta4223b72014-03-05 19:59:51 +0100207 /* 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
Gerlando Falauto348b1f12012-08-24 00:11:38 +0000215 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
Joe Hershbergerc4e00572012-12-11 22:16:19 -0600216 0, NULL)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200217 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;
Harald Welte5bb12db2008-07-07 15:40:39 +0800226}
227
Marek Vasut7ce15262014-03-05 19:59:50 +0100228/* Emport the environment and generate CRC for it. */
229int env_export(env_t *env_out)
230{
231 char *res;
232 ssize_t len;
Marek Vasuta4223b72014-03-05 19:59:51 +0100233 int ret;
Marek Vasut7ce15262014-03-05 19:59:50 +0100234
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 }
Marek Vasuta4223b72014-03-05 19:59:51 +0100241
242 /* Encrypt the env if desired. */
243 ret = env_aes_cbc_crypt(env_out, 1);
244 if (ret)
245 return ret;
246
Marek Vasut7ce15262014-03-05 19:59:50 +0100247 env_out->crc = crc32(0, env_out->data, ENV_SIZE);
248
249 return 0;
250}
251
Igor Grinberg27aafe92011-11-07 01:14:11 +0000252void env_relocate(void)
wdenk05706fd2002-09-28 17:48:27 +0000253{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200254#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Heiko Schocher60f7da12010-10-05 14:17:00 +0200255 env_reloc();
Joe Hershberger7afcf3a2012-12-11 22:16:21 -0600256 env_htab.change_ok += gd->reloc_off;
Heiko Schocher60f7da12010-10-05 14:17:00 +0200257#endif
wdenk05706fd2002-09-28 17:48:27 +0000258 if (gd->env_valid == 0) {
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000259#if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
260 /* Environment not changable */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200261 set_default_env(NULL);
wdenk05706fd2002-09-28 17:48:27 +0000262#else
Simon Glass770605e2012-02-13 13:51:18 +0000263 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 set_default_env("!bad CRC");
Lei Wend2590792010-10-10 12:36:40 +0800265#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200266 } else {
Igor Grinberg27aafe92011-11-07 01:14:11 +0000267 env_relocate_spec();
wdenk05706fd2002-09-28 17:48:27 +0000268 }
wdenk05706fd2002-09-28 17:48:27 +0000269}
wdenk04a85b32004-04-15 18:22:41 +0000270
Ilya Yanok7ac2fe22012-09-18 00:22:50 +0000271#if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
wdenk04a85b32004-04-15 18:22:41 +0000272int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
273{
Mike Frysinger560d4242010-12-17 16:51:59 -0500274 ENTRY *match;
275 int found, idx;
wdenk04a85b32004-04-15 18:22:41 +0000276
Mike Frysinger560d4242010-12-17 16:51:59 -0500277 idx = 0;
wdenk04a85b32004-04-15 18:22:41 +0000278 found = 0;
279 cmdv[0] = NULL;
280
Mike Frysinger560d4242010-12-17 16:51:59 -0500281 while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
282 int vallen = strlen(match->key) + 1;
wdenk04a85b32004-04-15 18:22:41 +0000283
Mike Frysinger560d4242010-12-17 16:51:59 -0500284 if (found >= maxv - 2 || bufsz < vallen)
wdenk04a85b32004-04-15 18:22:41 +0000285 break;
Mike Frysinger560d4242010-12-17 16:51:59 -0500286
wdenk04a85b32004-04-15 18:22:41 +0000287 cmdv[found++] = buf;
Mike Frysinger560d4242010-12-17 16:51:59 -0500288 memcpy(buf, match->key, vallen);
289 buf += vallen;
290 bufsz -= vallen;
wdenk04a85b32004-04-15 18:22:41 +0000291 }
292
Mike Frysinger560d4242010-12-17 16:51:59 -0500293 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
294
295 if (idx)
296 cmdv[found++] = "...";
Igor Grinberg27aafe92011-11-07 01:14:11 +0000297
wdenk04a85b32004-04-15 18:22:41 +0000298 cmdv[found] = NULL;
299 return found;
300}
301#endif