wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 3 | * 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 Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 7 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <environment.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 14 | #include <linux/stddef.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 15 | #include <search.h> |
| 16 | #include <errno.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 21 | /************************************************************************ |
| 22 | * Default settings to be used when no valid environment is found |
| 23 | */ |
Joe Hershberger | ddd8418 | 2012-10-12 08:48:51 +0000 | [diff] [blame] | 24 | #include <env_default.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 25 | |
Gerlando Falauto | c598359 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 26 | struct hsearch_data env_htab = { |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 27 | .change_ok = env_flags_validate, |
Gerlando Falauto | c598359 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 28 | }; |
Mike Frysinger | 2eb1573 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 29 | |
Igor Grinberg | bf95df4 | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 30 | static uchar __env_get_char_spec(int index) |
| 31 | { |
| 32 | return *((uchar *)(gd->env_addr + index)); |
| 33 | } |
| 34 | uchar env_get_char_spec(int) |
| 35 | __attribute__((weak, alias("__env_get_char_spec"))); |
| 36 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 37 | static uchar env_get_char_init(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 38 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 39 | /* if crc was bad, use the default environment */ |
| 40 | if (gd->env_valid) |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 41 | return env_get_char_spec(index); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 42 | else |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 43 | return default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 46 | uchar env_get_char_memory(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 47 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 48 | return *env_get_addr(index); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 51 | uchar env_get_char(int index) |
Joakim Tjernlund | b502611 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 52 | { |
Joakim Tjernlund | b502611 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 53 | /* if relocated to RAM */ |
| 54 | if (gd->flags & GD_FLG_RELOC) |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 55 | return env_get_char_memory(index); |
Joakim Tjernlund | b502611 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 56 | else |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 57 | return env_get_char_init(index); |
Joakim Tjernlund | b502611 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 60 | const uchar *env_get_addr(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 61 | { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 62 | if (gd->env_valid) |
| 63 | return (uchar *)(gd->env_addr + index); |
| 64 | else |
| 65 | return &default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Joe Hershberger | ec8a252 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 68 | /* |
| 69 | * Read an environment variable as a boolean |
| 70 | * Return -1 if variable does not exist (default to true) |
| 71 | */ |
| 72 | int getenv_yesno(const char *var) |
| 73 | { |
| 74 | char *s = getenv(var); |
| 75 | |
| 76 | if (s == NULL) |
| 77 | return -1; |
| 78 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? |
| 79 | 1 : 0; |
| 80 | } |
| 81 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 82 | /* |
| 83 | * Look up the variable from the default environment |
| 84 | */ |
| 85 | char *getenv_default(const char *name) |
| 86 | { |
| 87 | char *ret_val; |
| 88 | unsigned long really_valid = gd->env_valid; |
| 89 | unsigned long real_gd_flags = gd->flags; |
| 90 | |
| 91 | /* Pretend that the image is bad. */ |
| 92 | gd->flags &= ~GD_FLG_ENV_READY; |
| 93 | gd->env_valid = 0; |
| 94 | ret_val = getenv(name); |
| 95 | gd->env_valid = really_valid; |
| 96 | gd->flags = real_gd_flags; |
| 97 | return ret_val; |
| 98 | } |
| 99 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 100 | void set_default_env(const char *s) |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 101 | { |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 102 | int flags = 0; |
| 103 | |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 104 | if (sizeof(default_environment) > ENV_SIZE) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 105 | puts("*** Error - default environment is too large\n\n"); |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 109 | if (s) { |
| 110 | if (*s == '!') { |
| 111 | printf("*** Warning - %s, " |
| 112 | "using default environment\n\n", |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 113 | s + 1); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | } else { |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 115 | flags = H_INTERACTIVE; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 116 | puts(s); |
| 117 | } |
| 118 | } else { |
| 119 | puts("Using default environment\n\n"); |
| 120 | } |
| 121 | |
Mike Frysinger | 2eb1573 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 122 | if (himport_r(&env_htab, (char *)default_environment, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 123 | sizeof(default_environment), '\0', flags, |
| 124 | 0, NULL) == 0) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 125 | error("Environment import failed: errno = %d\n", errno); |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 126 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 127 | gd->flags |= GD_FLG_ENV_READY; |
| 128 | } |
| 129 | |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 130 | |
| 131 | /* [re]set individual variables to their value in the default environment */ |
| 132 | int set_default_vars(int nvars, char * const vars[]) |
| 133 | { |
| 134 | /* |
| 135 | * Special use-case: import from default environment |
| 136 | * (and use \0 as a separator) |
| 137 | */ |
| 138 | return himport_r(&env_htab, (const char *)default_environment, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 139 | sizeof(default_environment), '\0', |
| 140 | H_NOCLEAR | H_INTERACTIVE, nvars, vars); |
Gerlando Falauto | b64b7c3 | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 143 | #ifdef CONFIG_ENV_AES |
| 144 | #include <aes.h> |
| 145 | /** |
| 146 | * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment |
| 147 | * |
| 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 |
| 151 | * work. |
| 152 | */ |
| 153 | __weak uint8_t *env_aes_cbc_get_key(void) |
| 154 | { |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | static int env_aes_cbc_crypt(env_t *env, const int enc) |
| 159 | { |
| 160 | unsigned char *data = env->data; |
| 161 | uint8_t *key; |
| 162 | uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; |
| 163 | uint32_t aes_blocks; |
| 164 | |
| 165 | key = env_aes_cbc_get_key(); |
| 166 | if (!key) |
| 167 | return -EINVAL; |
| 168 | |
| 169 | /* First we expand the key. */ |
| 170 | aes_expand_key(key, key_exp); |
| 171 | |
| 172 | /* Calculate the number of AES blocks to encrypt. */ |
| 173 | aes_blocks = ENV_SIZE / AES_KEY_LENGTH; |
| 174 | |
| 175 | if (enc) |
| 176 | aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks); |
| 177 | else |
| 178 | aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | #else |
| 183 | static inline int env_aes_cbc_crypt(env_t *env, const int enc) |
| 184 | { |
| 185 | return 0; |
| 186 | } |
| 187 | #endif |
| 188 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 189 | /* |
| 190 | * Check if CRC is valid and (if yes) import the environment. |
| 191 | * Note that "buf" may or may not be aligned. |
| 192 | */ |
| 193 | int env_import(const char *buf, int check) |
| 194 | { |
| 195 | env_t *ep = (env_t *)buf; |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 196 | int ret; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | |
| 198 | if (check) { |
| 199 | uint32_t crc; |
| 200 | |
| 201 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 202 | |
| 203 | if (crc32(0, ep->data, ENV_SIZE) != crc) { |
| 204 | set_default_env("!bad CRC"); |
| 205 | return 0; |
| 206 | } |
| 207 | } |
| 208 | |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 209 | /* Decrypt the env if desired. */ |
| 210 | ret = env_aes_cbc_crypt(ep, 0); |
| 211 | if (ret) { |
| 212 | error("Failed to decrypt env!\n"); |
| 213 | set_default_env("!import failed"); |
| 214 | return ret; |
| 215 | } |
| 216 | |
Gerlando Falauto | 348b1f1 | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 217 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, |
Joe Hershberger | c4e0057 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 218 | 0, NULL)) { |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 219 | gd->flags |= GD_FLG_ENV_READY; |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | error("Cannot import environment: errno = %d\n", errno); |
| 224 | |
| 225 | set_default_env("!import failed"); |
| 226 | |
| 227 | return 0; |
Harald Welte | 5bb12db | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 228 | } |
| 229 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 230 | /* Emport the environment and generate CRC for it. */ |
| 231 | int env_export(env_t *env_out) |
| 232 | { |
| 233 | char *res; |
| 234 | ssize_t len; |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 235 | int ret; |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 236 | |
| 237 | res = (char *)env_out->data; |
| 238 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
| 239 | if (len < 0) { |
| 240 | error("Cannot export environment: errno = %d\n", errno); |
| 241 | return 1; |
| 242 | } |
Marek Vasut | a4223b7 | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 243 | |
| 244 | /* Encrypt the env if desired. */ |
| 245 | ret = env_aes_cbc_crypt(env_out, 1); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 249 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 254 | void env_relocate(void) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 255 | { |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 256 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | 60f7da1 | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 257 | env_reloc(); |
Joe Hershberger | 7afcf3a | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 258 | env_htab.change_ok += gd->reloc_off; |
Heiko Schocher | 60f7da1 | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 259 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 260 | if (gd->env_valid == 0) { |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 261 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
| 262 | /* Environment not changable */ |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 263 | set_default_env(NULL); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 264 | #else |
Simon Glass | 770605e | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 265 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 266 | set_default_env("!bad CRC"); |
Lei Wen | d259079 | 2010-10-10 12:36:40 +0800 | [diff] [blame] | 267 | #endif |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 268 | } else { |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 269 | env_relocate_spec(); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 270 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 271 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 272 | |
Ilya Yanok | 7ac2fe2 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 273 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 274 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
| 275 | { |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 276 | ENTRY *match; |
| 277 | int found, idx; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 278 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 279 | idx = 0; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 280 | found = 0; |
| 281 | cmdv[0] = NULL; |
| 282 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 283 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
| 284 | int vallen = strlen(match->key) + 1; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 285 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 286 | if (found >= maxv - 2 || bufsz < vallen) |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 287 | break; |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 288 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 289 | cmdv[found++] = buf; |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 290 | memcpy(buf, match->key, vallen); |
| 291 | buf += vallen; |
| 292 | bufsz -= vallen; |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Mike Frysinger | 560d424 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 295 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
| 296 | |
| 297 | if (idx) |
| 298 | cmdv[found++] = "..."; |
Igor Grinberg | 27aafe9 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 299 | |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 300 | cmdv[found] = NULL; |
| 301 | return found; |
| 302 | } |
| 303 | #endif |