wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 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 | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 7 | * |
Wolfgang Denk | bd8ec7e | 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 | 460a9ff | 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 | 6405a15 | 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 | 8132114 | 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 | d392595 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 26 | struct hsearch_data env_htab = { |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 27 | .change_ok = env_flags_validate, |
Gerlando Falauto | d392595 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 28 | }; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 29 | |
Jeroen Hofstee | 82d92da | 2014-07-12 15:22:13 +0200 | [diff] [blame] | 30 | __weak uchar env_get_char_spec(int index) |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 31 | { |
| 32 | return *((uchar *)(gd->env_addr + index)); |
| 33 | } |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 34 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 35 | static uchar env_get_char_init(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 36 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 37 | /* if crc was bad, use the default environment */ |
| 38 | if (gd->env_valid) |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 39 | return env_get_char_spec(index); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 40 | else |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 41 | return default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 44 | uchar env_get_char_memory(int index) |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 45 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 46 | return *env_get_addr(index); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 49 | uchar env_get_char(int index) |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 50 | { |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 51 | /* if relocated to RAM */ |
| 52 | if (gd->flags & GD_FLG_RELOC) |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 53 | return env_get_char_memory(index); |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 54 | else |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 55 | return env_get_char_init(index); |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 58 | const uchar *env_get_addr(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 59 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 60 | if (gd->env_valid) |
| 61 | return (uchar *)(gd->env_addr + index); |
| 62 | else |
| 63 | return &default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Joe Hershberger | 864ec56 | 2012-12-11 22:16:22 -0600 | [diff] [blame] | 66 | /* |
| 67 | * Read an environment variable as a boolean |
| 68 | * Return -1 if variable does not exist (default to true) |
| 69 | */ |
| 70 | int 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 Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 80 | /* |
| 81 | * Look up the variable from the default environment |
| 82 | */ |
| 83 | char *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 Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 98 | void set_default_env(const char *s) |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 99 | { |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 100 | int flags = 0; |
| 101 | |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 102 | if (sizeof(default_environment) > ENV_SIZE) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | puts("*** Error - default environment is too large\n\n"); |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 107 | if (s) { |
| 108 | if (*s == '!') { |
| 109 | printf("*** Warning - %s, " |
| 110 | "using default environment\n\n", |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 111 | s + 1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 112 | } else { |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 113 | flags = H_INTERACTIVE; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 114 | puts(s); |
| 115 | } |
| 116 | } else { |
| 117 | puts("Using default environment\n\n"); |
| 118 | } |
| 119 | |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 120 | if (himport_r(&env_htab, (char *)default_environment, |
Alexander Holler | 3e12be7 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 121 | sizeof(default_environment), '\0', flags, 0, |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 122 | 0, NULL) == 0) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 123 | error("Environment import failed: errno = %d\n", errno); |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 124 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 125 | gd->flags |= GD_FLG_ENV_READY; |
Michal Simek | 32bb7c8 | 2016-05-30 16:06:54 +0200 | [diff] [blame] | 126 | gd->flags |= GD_FLG_ENV_DEFAULT; |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 129 | |
| 130 | /* [re]set individual variables to their value in the default environment */ |
| 131 | int 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 Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 138 | sizeof(default_environment), '\0', |
Alexander Holler | 3e12be7 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 139 | H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars); |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Marek Vasut | 486112d | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 142 | #ifdef CONFIG_ENV_AES |
| 143 | #include <aes.h> |
| 144 | /** |
| 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 |
| 148 | * to encrypt and decrypt the environment. This function must be overriden |
| 149 | * 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 | |
| 157 | static 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 |
| 182 | static inline int env_aes_cbc_crypt(env_t *env, const int enc) |
| 183 | { |
| 184 | return 0; |
| 185 | } |
| 186 | #endif |
| 187 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 188 | /* |
| 189 | * Check if CRC is valid and (if yes) import the environment. |
| 190 | * Note that "buf" may or may not be aligned. |
| 191 | */ |
| 192 | int env_import(const char *buf, int check) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 193 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 194 | env_t *ep = (env_t *)buf; |
Marek Vasut | 486112d | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 195 | int ret; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 196 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | 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 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 207 | |
Marek Vasut | 486112d | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 208 | /* 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 Holler | 3e12be7 | 2014-07-14 17:49:55 +0200 | [diff] [blame] | 216 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 217 | 0, NULL)) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 218 | 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 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 229 | /* Emport the environment and generate CRC for it. */ |
| 230 | int env_export(env_t *env_out) |
| 231 | { |
| 232 | char *res; |
| 233 | ssize_t len; |
Marek Vasut | 486112d | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 234 | int ret; |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 235 | |
| 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 Vasut | 486112d | 2014-03-05 19:59:51 +0100 | [diff] [blame] | 242 | |
| 243 | /* Encrypt the env if desired. */ |
| 244 | ret = env_aes_cbc_crypt(env_out, 1); |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 248 | env_out->crc = crc32(0, env_out->data, ENV_SIZE); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 253 | void env_relocate(void) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 254 | { |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 255 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | faa0d2e | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 256 | env_reloc(); |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 257 | env_htab.change_ok += gd->reloc_off; |
Heiko Schocher | faa0d2e | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 258 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 259 | if (gd->env_valid == 0) { |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 260 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
| 261 | /* Environment not changable */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 262 | set_default_env(NULL); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 263 | #else |
Simon Glass | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 264 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 265 | set_default_env("!bad CRC"); |
Lei Wen | e0909be | 2010-10-10 12:36:40 +0800 | [diff] [blame] | 266 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 267 | } else { |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 268 | env_relocate_spec(); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 269 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 270 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 271 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 272 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 273 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
| 274 | { |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 275 | ENTRY *match; |
| 276 | int found, idx; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 277 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 278 | idx = 0; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 279 | found = 0; |
| 280 | cmdv[0] = NULL; |
| 281 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 282 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
| 283 | int vallen = strlen(match->key) + 1; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 284 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 285 | if (found >= maxv - 2 || bufsz < vallen) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 286 | break; |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 287 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 288 | cmdv[found++] = buf; |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 289 | memcpy(buf, match->key, vallen); |
| 290 | buf += vallen; |
| 291 | bufsz -= vallen; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 294 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
| 295 | |
| 296 | if (idx) |
| 297 | cmdv[found++] = "..."; |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 298 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 299 | cmdv[found] = NULL; |
| 300 | return found; |
| 301 | } |
| 302 | #endif |