1 /* 2 * (C) Copyright 2000-2010 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> 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #include <common.h> 12 #include <command.h> 13 #include <environment.h> 14 #include <linux/stddef.h> 15 #include <search.h> 16 #include <errno.h> 17 #include <malloc.h> 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 /************************************************************************ 22 * Default settings to be used when no valid environment is found 23 */ 24 #include <env_default.h> 25 26 struct hsearch_data env_htab = { 27 .change_ok = env_flags_validate, 28 }; 29 30 __weak uchar env_get_char_spec(int index) 31 { 32 return *(uchar *)(gd->env_addr + index); 33 } 34 35 uchar env_get_char(int index) 36 { 37 /* if env is not set up, or crc was bad, use the default environment */ 38 if (!gd->env_valid) 39 return default_environment[index]; 40 else 41 return env_get_char_spec(index); 42 } 43 44 /* 45 * Read an environment variable as a boolean 46 * Return -1 if variable does not exist (default to true) 47 */ 48 int getenv_yesno(const char *var) 49 { 50 char *s = getenv(var); 51 52 if (s == NULL) 53 return -1; 54 return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? 55 1 : 0; 56 } 57 58 /* 59 * Look up the variable from the default environment 60 */ 61 char *getenv_default(const char *name) 62 { 63 char *ret_val; 64 unsigned long really_valid = gd->env_valid; 65 unsigned long real_gd_flags = gd->flags; 66 67 /* Pretend that the image is bad. */ 68 gd->flags &= ~GD_FLG_ENV_READY; 69 gd->env_valid = 0; 70 ret_val = getenv(name); 71 gd->env_valid = really_valid; 72 gd->flags = real_gd_flags; 73 return ret_val; 74 } 75 76 void set_default_env(const char *s) 77 { 78 int flags = 0; 79 80 if (sizeof(default_environment) > ENV_SIZE) { 81 puts("*** Error - default environment is too large\n\n"); 82 return; 83 } 84 85 if (s) { 86 if (*s == '!') { 87 printf("*** Warning - %s, " 88 "using default environment\n\n", 89 s + 1); 90 } else { 91 flags = H_INTERACTIVE; 92 puts(s); 93 } 94 } else { 95 puts("Using default environment\n\n"); 96 } 97 98 if (himport_r(&env_htab, (char *)default_environment, 99 sizeof(default_environment), '\0', flags, 0, 100 0, NULL) == 0) 101 error("Environment import failed: errno = %d\n", errno); 102 103 gd->flags |= GD_FLG_ENV_READY; 104 gd->flags |= GD_FLG_ENV_DEFAULT; 105 } 106 107 108 /* [re]set individual variables to their value in the default environment */ 109 int set_default_vars(int nvars, char * const vars[]) 110 { 111 /* 112 * Special use-case: import from default environment 113 * (and use \0 as a separator) 114 */ 115 return himport_r(&env_htab, (const char *)default_environment, 116 sizeof(default_environment), '\0', 117 H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars); 118 } 119 120 #ifdef CONFIG_ENV_AES 121 #include <uboot_aes.h> 122 /** 123 * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment 124 * 125 * This function shall return 16-byte array containing AES-128 key used 126 * to encrypt and decrypt the environment. This function must be overridden 127 * by the implementer as otherwise the environment encryption will not 128 * work. 129 */ 130 __weak uint8_t *env_aes_cbc_get_key(void) 131 { 132 return NULL; 133 } 134 135 static int env_aes_cbc_crypt(env_t *env, const int enc) 136 { 137 unsigned char *data = env->data; 138 uint8_t *key; 139 uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; 140 uint32_t aes_blocks; 141 142 key = env_aes_cbc_get_key(); 143 if (!key) 144 return -EINVAL; 145 146 /* First we expand the key. */ 147 aes_expand_key(key, key_exp); 148 149 /* Calculate the number of AES blocks to encrypt. */ 150 aes_blocks = ENV_SIZE / AES_KEY_LENGTH; 151 152 if (enc) 153 aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks); 154 else 155 aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks); 156 157 return 0; 158 } 159 #else 160 static inline int env_aes_cbc_crypt(env_t *env, const int enc) 161 { 162 return 0; 163 } 164 #endif 165 166 /* 167 * Check if CRC is valid and (if yes) import the environment. 168 * Note that "buf" may or may not be aligned. 169 */ 170 int env_import(const char *buf, int check) 171 { 172 env_t *ep = (env_t *)buf; 173 int ret; 174 175 if (check) { 176 uint32_t crc; 177 178 memcpy(&crc, &ep->crc, sizeof(crc)); 179 180 if (crc32(0, ep->data, ENV_SIZE) != crc) { 181 set_default_env("!bad CRC"); 182 return 0; 183 } 184 } 185 186 /* Decrypt the env if desired. */ 187 ret = env_aes_cbc_crypt(ep, 0); 188 if (ret) { 189 error("Failed to decrypt env!\n"); 190 set_default_env("!import failed"); 191 return ret; 192 } 193 194 if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, 195 0, NULL)) { 196 gd->flags |= GD_FLG_ENV_READY; 197 return 1; 198 } 199 200 error("Cannot import environment: errno = %d\n", errno); 201 202 set_default_env("!import failed"); 203 204 return 0; 205 } 206 207 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT 208 static unsigned char env_flags; 209 210 int env_import_redund(const char *buf1, const char *buf2) 211 { 212 int crc1_ok, crc2_ok; 213 env_t *ep, *tmp_env1, *tmp_env2; 214 215 tmp_env1 = (env_t *)buf1; 216 tmp_env2 = (env_t *)buf2; 217 218 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == 219 tmp_env1->crc; 220 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == 221 tmp_env2->crc; 222 223 if (!crc1_ok && !crc2_ok) { 224 set_default_env("!bad CRC"); 225 return 0; 226 } else if (crc1_ok && !crc2_ok) { 227 gd->env_valid = 1; 228 } else if (!crc1_ok && crc2_ok) { 229 gd->env_valid = 2; 230 } else { 231 /* both ok - check serial */ 232 if (tmp_env1->flags == 255 && tmp_env2->flags == 0) 233 gd->env_valid = 2; 234 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) 235 gd->env_valid = 1; 236 else if (tmp_env1->flags > tmp_env2->flags) 237 gd->env_valid = 1; 238 else if (tmp_env2->flags > tmp_env1->flags) 239 gd->env_valid = 2; 240 else /* flags are equal - almost impossible */ 241 gd->env_valid = 1; 242 } 243 244 if (gd->env_valid == 1) 245 ep = tmp_env1; 246 else 247 ep = tmp_env2; 248 249 env_flags = ep->flags; 250 return env_import((char *)ep, 0); 251 } 252 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ 253 254 /* Export the environment and generate CRC for it. */ 255 int env_export(env_t *env_out) 256 { 257 char *res; 258 ssize_t len; 259 int ret; 260 261 res = (char *)env_out->data; 262 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); 263 if (len < 0) { 264 error("Cannot export environment: errno = %d\n", errno); 265 return 1; 266 } 267 268 /* Encrypt the env if desired. */ 269 ret = env_aes_cbc_crypt(env_out, 1); 270 if (ret) 271 return ret; 272 273 env_out->crc = crc32(0, env_out->data, ENV_SIZE); 274 275 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT 276 env_out->flags = ++env_flags; /* increase the serial */ 277 #endif 278 279 return 0; 280 } 281 282 void env_relocate(void) 283 { 284 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 285 env_reloc(); 286 env_htab.change_ok += gd->reloc_off; 287 #endif 288 if (gd->env_valid == 0) { 289 #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) 290 /* Environment not changable */ 291 set_default_env(NULL); 292 #else 293 bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); 294 set_default_env("!bad CRC"); 295 #endif 296 } else { 297 env_relocate_spec(); 298 } 299 } 300 301 #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) 302 int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) 303 { 304 ENTRY *match; 305 int found, idx; 306 307 idx = 0; 308 found = 0; 309 cmdv[0] = NULL; 310 311 while ((idx = hmatch_r(var, idx, &match, &env_htab))) { 312 int vallen = strlen(match->key) + 1; 313 314 if (found >= maxv - 2 || bufsz < vallen) 315 break; 316 317 cmdv[found++] = buf; 318 memcpy(buf, match->key, vallen); 319 buf += vallen; 320 bufsz -= vallen; 321 } 322 323 qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); 324 325 if (idx) 326 cmdv[found++] = "..."; 327 328 cmdv[found] = NULL; 329 return found; 330 } 331 #endif 332