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