wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 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 | * 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> |
| 31 | #include <malloc.h> |
| 32 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 35 | #undef DEBUG_ENV |
| 36 | #ifdef DEBUG_ENV |
| 37 | #define DEBUGF(fmt,args...) printf(fmt ,##args) |
| 38 | #else |
| 39 | #define DEBUGF(fmt,args...) |
| 40 | #endif |
| 41 | |
| 42 | extern env_t *env_ptr; |
| 43 | |
| 44 | extern void env_relocate_spec (void); |
| 45 | extern uchar env_get_char_spec(int); |
| 46 | |
| 47 | static uchar env_get_char_init (int index); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 48 | |
| 49 | /************************************************************************ |
| 50 | * Default settings to be used when no valid environment is found |
| 51 | */ |
| 52 | #define XMK_STR(x) #x |
| 53 | #define MK_STR(x) XMK_STR(x) |
| 54 | |
| 55 | uchar default_environment[] = { |
| 56 | #ifdef CONFIG_BOOTARGS |
| 57 | "bootargs=" CONFIG_BOOTARGS "\0" |
| 58 | #endif |
| 59 | #ifdef CONFIG_BOOTCOMMAND |
| 60 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" |
| 61 | #endif |
| 62 | #ifdef CONFIG_RAMBOOTCOMMAND |
| 63 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" |
| 64 | #endif |
| 65 | #ifdef CONFIG_NFSBOOTCOMMAND |
| 66 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" |
| 67 | #endif |
| 68 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 69 | "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0" |
| 70 | #endif |
| 71 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) |
| 72 | "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0" |
| 73 | #endif |
| 74 | #ifdef CONFIG_LOADS_ECHO |
| 75 | "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0" |
| 76 | #endif |
| 77 | #ifdef CONFIG_ETHADDR |
| 78 | "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0" |
| 79 | #endif |
| 80 | #ifdef CONFIG_ETH1ADDR |
| 81 | "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0" |
| 82 | #endif |
| 83 | #ifdef CONFIG_ETH2ADDR |
| 84 | "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0" |
| 85 | #endif |
wdenk | cd91445 | 2004-05-29 16:53:29 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_ETH3ADDR |
| 87 | "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0" |
| 88 | #endif |
richardretanubun | e5167f1 | 2008-09-29 18:28:23 -0400 | [diff] [blame] | 89 | #ifdef CONFIG_ETH4ADDR |
| 90 | "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0" |
| 91 | #endif |
| 92 | #ifdef CONFIG_ETH5ADDR |
| 93 | "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0" |
| 94 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 95 | #ifdef CONFIG_IPADDR |
| 96 | "ipaddr=" MK_STR(CONFIG_IPADDR) "\0" |
| 97 | #endif |
| 98 | #ifdef CONFIG_SERVERIP |
| 99 | "serverip=" MK_STR(CONFIG_SERVERIP) "\0" |
| 100 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 101 | #ifdef CONFIG_SYS_AUTOLOAD |
| 102 | "autoload=" CONFIG_SYS_AUTOLOAD "\0" |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef CONFIG_PREBOOT |
| 105 | "preboot=" CONFIG_PREBOOT "\0" |
| 106 | #endif |
| 107 | #ifdef CONFIG_ROOTPATH |
| 108 | "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0" |
| 109 | #endif |
| 110 | #ifdef CONFIG_GATEWAYIP |
| 111 | "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0" |
| 112 | #endif |
| 113 | #ifdef CONFIG_NETMASK |
| 114 | "netmask=" MK_STR(CONFIG_NETMASK) "\0" |
| 115 | #endif |
| 116 | #ifdef CONFIG_HOSTNAME |
| 117 | "hostname=" MK_STR(CONFIG_HOSTNAME) "\0" |
| 118 | #endif |
| 119 | #ifdef CONFIG_BOOTFILE |
| 120 | "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0" |
| 121 | #endif |
| 122 | #ifdef CONFIG_LOADADDR |
| 123 | "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0" |
| 124 | #endif |
| 125 | #ifdef CONFIG_CLOCKS_IN_MHZ |
| 126 | "clocks_in_mhz=1\0" |
| 127 | #endif |
stroese | f5dd410 | 2003-02-14 11:21:23 +0000 | [diff] [blame] | 128 | #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) |
| 129 | "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0" |
| 130 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 131 | #ifdef CONFIG_EXTRA_ENV_SETTINGS |
| 132 | CONFIG_EXTRA_ENV_SETTINGS |
| 133 | #endif |
| 134 | "\0" |
| 135 | }; |
| 136 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 137 | void env_crc_update (void) |
| 138 | { |
| 139 | env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE); |
| 140 | } |
| 141 | |
| 142 | static uchar env_get_char_init (int index) |
| 143 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 144 | uchar c; |
| 145 | |
| 146 | /* if crc was bad, use the default environment */ |
| 147 | if (gd->env_valid) |
| 148 | { |
| 149 | c = env_get_char_spec(index); |
| 150 | } else { |
| 151 | c = default_environment[index]; |
| 152 | } |
| 153 | |
| 154 | return (c); |
| 155 | } |
| 156 | |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 157 | uchar env_get_char_memory (int index) |
| 158 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 159 | if (gd->env_valid) { |
| 160 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 161 | } else { |
| 162 | return ( default_environment[index] ); |
| 163 | } |
| 164 | } |
| 165 | |
Joakim Tjernlund | 304b359 | 2008-07-06 12:30:09 +0200 | [diff] [blame] | 166 | uchar env_get_char (int index) |
| 167 | { |
| 168 | uchar c; |
| 169 | |
| 170 | /* if relocated to RAM */ |
| 171 | if (gd->flags & GD_FLG_RELOC) |
| 172 | c = env_get_char_memory(index); |
| 173 | else |
| 174 | c = env_get_char_init(index); |
| 175 | |
| 176 | return (c); |
| 177 | } |
| 178 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 179 | uchar *env_get_addr (int index) |
| 180 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 181 | if (gd->env_valid) { |
| 182 | return ( ((uchar *)(gd->env_addr + index)) ); |
| 183 | } else { |
| 184 | return (&default_environment[index]); |
| 185 | } |
| 186 | } |
| 187 | |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 188 | void set_default_env(void) |
| 189 | { |
| 190 | if (sizeof(default_environment) > ENV_SIZE) { |
| 191 | puts ("*** Error - default environment is too large\n\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | memset(env_ptr, 0, sizeof(env_t)); |
| 196 | memcpy(env_ptr->data, default_environment, |
| 197 | sizeof(default_environment)); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 198 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 199 | env_ptr->flags = 0xFF; |
| 200 | #endif |
| 201 | env_crc_update (); |
| 202 | gd->env_valid = 1; |
| 203 | } |
| 204 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 205 | void env_relocate (void) |
| 206 | { |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 207 | #ifndef CONFIG_RELOC_FIXUP_WORKS |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 208 | DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__, |
| 209 | gd->reloc_off); |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 210 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 211 | |
| 212 | #ifdef ENV_IS_EMBEDDED |
| 213 | /* |
| 214 | * The environment buffer is embedded with the text segment, |
| 215 | * just relocate the environment pointer |
| 216 | */ |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 217 | #ifndef CONFIG_RELOC_FIXUP_WORKS |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 218 | env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off); |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 219 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 220 | DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr); |
| 221 | #else |
| 222 | /* |
| 223 | * We must allocate a buffer for the environment |
| 224 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 225 | env_ptr = (env_t *)malloc (CONFIG_ENV_SIZE); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 226 | DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr); |
| 227 | #endif |
| 228 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 229 | if (gd->env_valid == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 68a8756 | 2008-09-10 22:48:00 +0200 | [diff] [blame] | 230 | #if defined(CONFIG_GTH) || defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */ |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 231 | puts ("Using default environment\n\n"); |
| 232 | #else |
| 233 | puts ("*** Warning - bad CRC, using default environment\n\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 234 | show_boot_progress (-60); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 235 | #endif |
Harald Welte | 72eb50a | 2008-07-07 15:40:39 +0800 | [diff] [blame] | 236 | set_default_env(); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 237 | } |
| 238 | else { |
| 239 | env_relocate_spec (); |
| 240 | } |
| 241 | gd->env_addr = (ulong)&(env_ptr->data); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 242 | } |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 243 | |
| 244 | #ifdef CONFIG_AUTO_COMPLETE |
| 245 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
| 246 | { |
| 247 | int i, nxt, len, vallen, found; |
| 248 | const char *lval, *rval; |
| 249 | |
| 250 | found = 0; |
| 251 | cmdv[0] = NULL; |
| 252 | |
| 253 | len = strlen(var); |
| 254 | /* now iterate over the variables and select those that match */ |
| 255 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 256 | |
| 257 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) |
| 258 | ; |
| 259 | |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 260 | lval = (char *)env_get_addr(i); |
wdenk | 3902d70 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 261 | rval = strchr(lval, '='); |
| 262 | if (rval != NULL) { |
| 263 | vallen = rval - lval; |
| 264 | rval++; |
| 265 | } else |
| 266 | vallen = strlen(lval); |
| 267 | |
| 268 | if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0)) |
| 269 | continue; |
| 270 | |
| 271 | if (found >= maxv - 2 || bufsz < vallen + 1) { |
| 272 | cmdv[found++] = "..."; |
| 273 | break; |
| 274 | } |
| 275 | cmdv[found++] = buf; |
| 276 | memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen; |
| 277 | *buf++ = '\0'; bufsz--; |
| 278 | } |
| 279 | |
| 280 | cmdv[found] = NULL; |
| 281 | return found; |
| 282 | } |
| 283 | #endif |