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 | * |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <environment.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 30 | #include <linux/stddef.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 31 | #include <search.h> |
| 32 | #include <errno.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 33 | #include <malloc.h> |
| 34 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 37 | /************************************************************************ |
| 38 | * Default settings to be used when no valid environment is found |
| 39 | */ |
Joe Hershberger | 8132114 | 2012-10-12 08:48:51 +0000 | [diff] [blame] | 40 | #include <env_default.h> |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 41 | |
Gerlando Falauto | d392595 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 42 | struct hsearch_data env_htab = { |
| 43 | .apply = env_check_apply, |
| 44 | }; |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 45 | |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 46 | static uchar __env_get_char_spec(int index) |
| 47 | { |
| 48 | return *((uchar *)(gd->env_addr + index)); |
| 49 | } |
| 50 | uchar env_get_char_spec(int) |
| 51 | __attribute__((weak, alias("__env_get_char_spec"))); |
| 52 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 53 | static uchar env_get_char_init(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 54 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 55 | /* if crc was bad, use the default environment */ |
| 56 | if (gd->env_valid) |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 57 | return env_get_char_spec(index); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 58 | else |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 59 | return default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 62 | uchar env_get_char_memory(int index) |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 63 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 64 | return *env_get_addr(index); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 67 | uchar env_get_char(int index) |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 68 | { |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 69 | /* if relocated to RAM */ |
| 70 | if (gd->flags & GD_FLG_RELOC) |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 71 | return env_get_char_memory(index); |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 72 | else |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 73 | return env_get_char_init(index); |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 76 | const uchar *env_get_addr(int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 77 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 78 | if (gd->env_valid) |
| 79 | return (uchar *)(gd->env_addr + index); |
| 80 | else |
| 81 | return &default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 84 | void set_default_env(const char *s) |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 85 | { |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 86 | /* |
| 87 | * By default, do not apply changes as they will eventually |
| 88 | * be applied by someone else |
| 89 | */ |
| 90 | int do_apply = 0; |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 91 | if (sizeof(default_environment) > ENV_SIZE) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 92 | puts("*** Error - default environment is too large\n\n"); |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 96 | if (s) { |
| 97 | if (*s == '!') { |
| 98 | printf("*** Warning - %s, " |
| 99 | "using default environment\n\n", |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 100 | s + 1); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 101 | } else { |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 102 | /* |
| 103 | * This set_to_default was explicitly asked for |
| 104 | * by the user, as opposed to being a recovery |
| 105 | * mechanism. Therefore we check every single |
| 106 | * variable and apply changes to the system |
| 107 | * right away (e.g. baudrate, console). |
| 108 | */ |
| 109 | do_apply = 1; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 110 | puts(s); |
| 111 | } |
| 112 | } else { |
| 113 | puts("Using default environment\n\n"); |
| 114 | } |
| 115 | |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 116 | if (himport_r(&env_htab, (char *)default_environment, |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 117 | sizeof(default_environment), '\0', 0, |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 118 | 0, NULL, do_apply) == 0) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 119 | error("Environment import failed: errno = %d\n", errno); |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 120 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 121 | gd->flags |= GD_FLG_ENV_READY; |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 122 | } |
| 123 | |
Gerlando Falauto | fe9f49d | 2012-08-24 00:11:41 +0000 | [diff] [blame] | 124 | |
| 125 | /* [re]set individual variables to their value in the default environment */ |
| 126 | int set_default_vars(int nvars, char * const vars[]) |
| 127 | { |
| 128 | /* |
| 129 | * Special use-case: import from default environment |
| 130 | * (and use \0 as a separator) |
| 131 | */ |
| 132 | return himport_r(&env_htab, (const char *)default_environment, |
| 133 | sizeof(default_environment), '\0', H_NOCLEAR, |
| 134 | nvars, vars, 1 /* do_apply */); |
| 135 | } |
| 136 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 137 | #ifndef CONFIG_SPL_BUILD |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Check if CRC is valid and (if yes) import the environment. |
| 140 | * Note that "buf" may or may not be aligned. |
| 141 | */ |
| 142 | int env_import(const char *buf, int check) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 143 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 144 | env_t *ep = (env_t *)buf; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 145 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 146 | if (check) { |
| 147 | uint32_t crc; |
| 148 | |
| 149 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 150 | |
| 151 | if (crc32(0, ep->data, ENV_SIZE) != crc) { |
| 152 | set_default_env("!bad CRC"); |
| 153 | return 0; |
| 154 | } |
| 155 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 156 | |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 157 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, |
Gerlando Falauto | d392595 | 2012-08-24 00:11:39 +0000 | [diff] [blame] | 158 | 0, NULL, 0 /* do_apply */)) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 159 | gd->flags |= GD_FLG_ENV_READY; |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | error("Cannot import environment: errno = %d\n", errno); |
| 164 | |
| 165 | set_default_env("!import failed"); |
| 166 | |
| 167 | return 0; |
| 168 | } |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 169 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 170 | |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 171 | void env_relocate(void) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 172 | { |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 173 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | faa0d2e | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 174 | env_reloc(); |
| 175 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 176 | if (gd->env_valid == 0) { |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 177 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
| 178 | /* Environment not changable */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 179 | set_default_env(NULL); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 180 | #else |
Simon Glass | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 181 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 182 | set_default_env("!bad CRC"); |
Lei Wen | e0909be | 2010-10-10 12:36:40 +0800 | [diff] [blame] | 183 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 184 | } else { |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 185 | env_relocate_spec(); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 186 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 187 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 188 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 189 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 190 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
| 191 | { |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 192 | ENTRY *match; |
| 193 | int found, idx; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 194 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 195 | idx = 0; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 196 | found = 0; |
| 197 | cmdv[0] = NULL; |
| 198 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 199 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
| 200 | int vallen = strlen(match->key) + 1; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 201 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 202 | if (found >= maxv - 2 || bufsz < vallen) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 203 | break; |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 204 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 205 | cmdv[found++] = buf; |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 206 | memcpy(buf, match->key, vallen); |
| 207 | buf += vallen; |
| 208 | bufsz -= vallen; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 211 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
| 212 | |
| 213 | if (idx) |
| 214 | cmdv[found++] = "..."; |
Igor Grinberg | 3e77359 | 2011-11-07 01:14:11 +0000 | [diff] [blame] | 215 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 216 | cmdv[found] = NULL; |
| 217 | return found; |
| 218 | } |
| 219 | #endif |