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 | extern env_t *env_ptr; |
| 38 | |
| 39 | extern void env_relocate_spec (void); |
| 40 | extern uchar env_get_char_spec(int); |
| 41 | |
| 42 | static uchar env_get_char_init (int index); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 43 | |
| 44 | /************************************************************************ |
| 45 | * Default settings to be used when no valid environment is found |
| 46 | */ |
| 47 | #define XMK_STR(x) #x |
| 48 | #define MK_STR(x) XMK_STR(x) |
| 49 | |
Mike Frysinger | 018f0c8 | 2011-04-23 23:43:21 +0000 | [diff] [blame] | 50 | const uchar default_environment[] = { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_BOOTARGS |
| 52 | "bootargs=" CONFIG_BOOTARGS "\0" |
| 53 | #endif |
| 54 | #ifdef CONFIG_BOOTCOMMAND |
| 55 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" |
| 56 | #endif |
| 57 | #ifdef CONFIG_RAMBOOTCOMMAND |
| 58 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" |
| 59 | #endif |
| 60 | #ifdef CONFIG_NFSBOOTCOMMAND |
| 61 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" |
| 62 | #endif |
| 63 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 64 | "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0" |
| 65 | #endif |
| 66 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) |
| 67 | "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0" |
| 68 | #endif |
| 69 | #ifdef CONFIG_LOADS_ECHO |
| 70 | "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0" |
| 71 | #endif |
| 72 | #ifdef CONFIG_ETHADDR |
| 73 | "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0" |
| 74 | #endif |
| 75 | #ifdef CONFIG_ETH1ADDR |
| 76 | "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0" |
| 77 | #endif |
| 78 | #ifdef CONFIG_ETH2ADDR |
| 79 | "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0" |
| 80 | #endif |
wdenk | cd91445 | 2004-05-29 16:53:29 +0000 | [diff] [blame] | 81 | #ifdef CONFIG_ETH3ADDR |
| 82 | "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0" |
| 83 | #endif |
richardretanubun | e5167f1 | 2008-09-29 18:28:23 -0400 | [diff] [blame] | 84 | #ifdef CONFIG_ETH4ADDR |
| 85 | "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0" |
| 86 | #endif |
| 87 | #ifdef CONFIG_ETH5ADDR |
| 88 | "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0" |
| 89 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_IPADDR |
| 91 | "ipaddr=" MK_STR(CONFIG_IPADDR) "\0" |
| 92 | #endif |
| 93 | #ifdef CONFIG_SERVERIP |
| 94 | "serverip=" MK_STR(CONFIG_SERVERIP) "\0" |
| 95 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #ifdef CONFIG_SYS_AUTOLOAD |
| 97 | "autoload=" CONFIG_SYS_AUTOLOAD "\0" |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 98 | #endif |
| 99 | #ifdef CONFIG_PREBOOT |
| 100 | "preboot=" CONFIG_PREBOOT "\0" |
| 101 | #endif |
| 102 | #ifdef CONFIG_ROOTPATH |
Joe Hershberger | 257ff78 | 2011-10-13 13:03:47 +0000 | [diff] [blame] | 103 | "rootpath=" CONFIG_ROOTPATH "\0" |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef CONFIG_GATEWAYIP |
| 106 | "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0" |
| 107 | #endif |
| 108 | #ifdef CONFIG_NETMASK |
| 109 | "netmask=" MK_STR(CONFIG_NETMASK) "\0" |
| 110 | #endif |
| 111 | #ifdef CONFIG_HOSTNAME |
| 112 | "hostname=" MK_STR(CONFIG_HOSTNAME) "\0" |
| 113 | #endif |
| 114 | #ifdef CONFIG_BOOTFILE |
Joe Hershberger | e4da248 | 2011-10-13 13:03:48 +0000 | [diff] [blame] | 115 | "bootfile=" CONFIG_BOOTFILE "\0" |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 116 | #endif |
| 117 | #ifdef CONFIG_LOADADDR |
| 118 | "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0" |
| 119 | #endif |
| 120 | #ifdef CONFIG_CLOCKS_IN_MHZ |
| 121 | "clocks_in_mhz=1\0" |
| 122 | #endif |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 123 | #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) |
| 124 | "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0" |
| 125 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 126 | #ifdef CONFIG_EXTRA_ENV_SETTINGS |
| 127 | CONFIG_EXTRA_ENV_SETTINGS |
| 128 | #endif |
| 129 | "\0" |
| 130 | }; |
| 131 | |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 132 | struct hsearch_data env_htab; |
| 133 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 134 | static uchar env_get_char_init (int index) |
| 135 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 136 | uchar c; |
| 137 | |
| 138 | /* if crc was bad, use the default environment */ |
| 139 | if (gd->env_valid) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 140 | c = env_get_char_spec(index); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 141 | else |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 142 | c = default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 143 | |
| 144 | return (c); |
| 145 | } |
| 146 | |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 147 | uchar env_get_char_memory (int index) |
| 148 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 149 | return *env_get_addr(index); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 152 | uchar env_get_char (int index) |
| 153 | { |
| 154 | uchar c; |
| 155 | |
| 156 | /* if relocated to RAM */ |
| 157 | if (gd->flags & GD_FLG_RELOC) |
| 158 | c = env_get_char_memory(index); |
| 159 | else |
| 160 | c = env_get_char_init(index); |
| 161 | |
| 162 | return (c); |
| 163 | } |
| 164 | |
Mike Frysinger | 018f0c8 | 2011-04-23 23:43:21 +0000 | [diff] [blame] | 165 | const uchar *env_get_addr (int index) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 166 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 167 | if (gd->env_valid) |
| 168 | return (uchar *)(gd->env_addr + index); |
| 169 | else |
| 170 | return &default_environment[index]; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 173 | void set_default_env(const char *s) |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 174 | { |
| 175 | if (sizeof(default_environment) > ENV_SIZE) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 176 | puts("*** Error - default environment is too large\n\n"); |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 180 | if (s) { |
| 181 | if (*s == '!') { |
| 182 | printf("*** Warning - %s, " |
| 183 | "using default environment\n\n", |
| 184 | s+1); |
| 185 | } else { |
| 186 | puts(s); |
| 187 | } |
| 188 | } else { |
| 189 | puts("Using default environment\n\n"); |
| 190 | } |
| 191 | |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 192 | if (himport_r(&env_htab, (char *)default_environment, |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 193 | sizeof(default_environment), '\0', 0) == 0) { |
| 194 | error("Environment import failed: errno = %d\n", errno); |
| 195 | } |
| 196 | gd->flags |= GD_FLG_ENV_READY; |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 197 | } |
| 198 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 199 | /* |
| 200 | * Check if CRC is valid and (if yes) import the environment. |
| 201 | * Note that "buf" may or may not be aligned. |
| 202 | */ |
| 203 | int env_import(const char *buf, int check) |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 204 | { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 205 | env_t *ep = (env_t *)buf; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 206 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 207 | if (check) { |
| 208 | uint32_t crc; |
| 209 | |
| 210 | memcpy(&crc, &ep->crc, sizeof(crc)); |
| 211 | |
| 212 | if (crc32(0, ep->data, ENV_SIZE) != crc) { |
| 213 | set_default_env("!bad CRC"); |
| 214 | return 0; |
| 215 | } |
| 216 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 217 | |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 218 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0)) { |
Wolfgang Denk | 460a9ff | 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; |
| 228 | } |
| 229 | |
| 230 | void env_relocate (void) |
| 231 | { |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 232 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
Heiko Schocher | faa0d2e | 2010-10-05 14:17:00 +0200 | [diff] [blame] | 233 | extern void env_reloc(void); |
| 234 | |
| 235 | env_reloc(); |
| 236 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 237 | if (gd->env_valid == 0) { |
Wolfgang Denk | 30c3add | 2010-07-05 22:46:33 +0200 | [diff] [blame] | 238 | #if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 239 | set_default_env(NULL); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 240 | #else |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 241 | show_boot_progress (-60); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 242 | set_default_env("!bad CRC"); |
Lei Wen | e0909be | 2010-10-10 12:36:40 +0800 | [diff] [blame] | 243 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 244 | } else { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 245 | env_relocate_spec (); |
| 246 | } |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 247 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 248 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 249 | #ifdef CONFIG_AUTO_COMPLETE |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 250 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
| 251 | { |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 252 | ENTRY *match; |
| 253 | int found, idx; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 254 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 255 | idx = 0; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 256 | found = 0; |
| 257 | cmdv[0] = NULL; |
| 258 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 259 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
| 260 | int vallen = strlen(match->key) + 1; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 261 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 262 | if (found >= maxv - 2 || bufsz < vallen) |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 263 | break; |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 264 | |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 265 | cmdv[found++] = buf; |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 266 | memcpy(buf, match->key, vallen); |
| 267 | buf += vallen; |
| 268 | bufsz -= vallen; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 271 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
| 272 | |
| 273 | if (idx) |
| 274 | cmdv[found++] = "..."; |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 275 | cmdv[found] = NULL; |
| 276 | return found; |
| 277 | } |
| 278 | #endif |