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