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 | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
wdenk | c7de829 | 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); |
Wolfgang Denk | c9dca3c | 2008-05-12 00:40:58 +0200 | [diff] [blame] | 53 | uchar (*env_get_char)(int) = env_get_char_init; |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 54 | |
| 55 | /************************************************************************ |
| 56 | * Default settings to be used when no valid environment is found |
| 57 | */ |
| 58 | #define XMK_STR(x) #x |
| 59 | #define MK_STR(x) XMK_STR(x) |
| 60 | |
| 61 | uchar default_environment[] = { |
| 62 | #ifdef CONFIG_BOOTARGS |
| 63 | "bootargs=" CONFIG_BOOTARGS "\0" |
| 64 | #endif |
| 65 | #ifdef CONFIG_BOOTCOMMAND |
| 66 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" |
| 67 | #endif |
| 68 | #ifdef CONFIG_RAMBOOTCOMMAND |
| 69 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" |
| 70 | #endif |
| 71 | #ifdef CONFIG_NFSBOOTCOMMAND |
| 72 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" |
| 73 | #endif |
| 74 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 75 | "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0" |
| 76 | #endif |
| 77 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) |
| 78 | "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0" |
| 79 | #endif |
| 80 | #ifdef CONFIG_LOADS_ECHO |
| 81 | "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0" |
| 82 | #endif |
| 83 | #ifdef CONFIG_ETHADDR |
| 84 | "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0" |
| 85 | #endif |
| 86 | #ifdef CONFIG_ETH1ADDR |
| 87 | "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0" |
| 88 | #endif |
| 89 | #ifdef CONFIG_ETH2ADDR |
| 90 | "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0" |
| 91 | #endif |
wdenk | 6945979 | 2004-05-29 16:53:29 +0000 | [diff] [blame] | 92 | #ifdef CONFIG_ETH3ADDR |
| 93 | "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\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 |
| 101 | #ifdef CFG_AUTOLOAD |
| 102 | "autoload=" CFG_AUTOLOAD "\0" |
| 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 | ad10dd9 | 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 | |
Haavard Skinnemoen | 8c66497 | 2008-05-16 11:10:35 +0200 | [diff] [blame] | 137 | #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */ \ |
| 138 | || defined(CFG_ENV_IS_IN_SPI_FLASH) |
wdenk | 13a5695 | 2004-06-09 14:58:14 +0000 | [diff] [blame] | 139 | int default_environment_size = sizeof(default_environment); |
| 140 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 141 | |
| 142 | void env_crc_update (void) |
| 143 | { |
| 144 | env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE); |
| 145 | } |
| 146 | |
| 147 | static uchar env_get_char_init (int index) |
| 148 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 149 | uchar c; |
| 150 | |
| 151 | /* if crc was bad, use the default environment */ |
| 152 | if (gd->env_valid) |
| 153 | { |
| 154 | c = env_get_char_spec(index); |
| 155 | } else { |
| 156 | c = default_environment[index]; |
| 157 | } |
| 158 | |
| 159 | return (c); |
| 160 | } |
| 161 | |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 162 | #ifdef CONFIG_AMIGAONEG3SE |
| 163 | uchar env_get_char_memory (int index) |
| 164 | { |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 165 | uchar retval; |
| 166 | enable_nvram(); |
| 167 | if (gd->env_valid) { |
| 168 | retval = ( *((uchar *)(gd->env_addr + index)) ); |
| 169 | } else { |
| 170 | retval = ( default_environment[index] ); |
| 171 | } |
| 172 | disable_nvram(); |
| 173 | return retval; |
| 174 | } |
| 175 | #else |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 176 | uchar env_get_char_memory (int index) |
| 177 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 178 | if (gd->env_valid) { |
| 179 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 180 | } else { |
| 181 | return ( default_environment[index] ); |
| 182 | } |
| 183 | } |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 184 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 185 | |
| 186 | uchar *env_get_addr (int index) |
| 187 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 188 | if (gd->env_valid) { |
| 189 | return ( ((uchar *)(gd->env_addr + index)) ); |
| 190 | } else { |
| 191 | return (&default_environment[index]); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void env_relocate (void) |
| 196 | { |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 197 | DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__, |
| 198 | gd->reloc_off); |
| 199 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 200 | #ifdef CONFIG_AMIGAONEG3SE |
| 201 | enable_nvram(); |
| 202 | #endif |
| 203 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 204 | #ifdef ENV_IS_EMBEDDED |
| 205 | /* |
| 206 | * The environment buffer is embedded with the text segment, |
| 207 | * just relocate the environment pointer |
| 208 | */ |
| 209 | env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off); |
| 210 | DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr); |
| 211 | #else |
| 212 | /* |
| 213 | * We must allocate a buffer for the environment |
| 214 | */ |
| 215 | env_ptr = (env_t *)malloc (CFG_ENV_SIZE); |
| 216 | DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr); |
| 217 | #endif |
| 218 | |
Wolfgang Denk | c9dca3c | 2008-05-12 00:40:58 +0200 | [diff] [blame] | 219 | /* |
| 220 | * After relocation to RAM, we can always use the "memory" functions |
| 221 | */ |
| 222 | env_get_char = env_get_char_memory; |
| 223 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 224 | if (gd->env_valid == 0) { |
| 225 | #if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */ |
| 226 | puts ("Using default environment\n\n"); |
| 227 | #else |
| 228 | puts ("*** Warning - bad CRC, using default environment\n\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 229 | show_boot_progress (-60); |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 230 | #endif |
| 231 | |
| 232 | if (sizeof(default_environment) > ENV_SIZE) |
| 233 | { |
| 234 | puts ("*** Error - default environment is too large\n\n"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | memset (env_ptr, 0, sizeof(env_t)); |
| 239 | memcpy (env_ptr->data, |
| 240 | default_environment, |
| 241 | sizeof(default_environment)); |
| 242 | #ifdef CFG_REDUNDAND_ENVIRONMENT |
| 243 | env_ptr->flags = 0xFF; |
| 244 | #endif |
| 245 | env_crc_update (); |
| 246 | gd->env_valid = 1; |
| 247 | } |
| 248 | else { |
| 249 | env_relocate_spec (); |
| 250 | } |
| 251 | gd->env_addr = (ulong)&(env_ptr->data); |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 252 | |
| 253 | #ifdef CONFIG_AMIGAONEG3SE |
| 254 | disable_nvram(); |
| 255 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 256 | } |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 257 | |
| 258 | #ifdef CONFIG_AUTO_COMPLETE |
| 259 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
| 260 | { |
| 261 | int i, nxt, len, vallen, found; |
| 262 | const char *lval, *rval; |
| 263 | |
| 264 | found = 0; |
| 265 | cmdv[0] = NULL; |
| 266 | |
| 267 | len = strlen(var); |
| 268 | /* now iterate over the variables and select those that match */ |
| 269 | for (i=0; env_get_char(i) != '\0'; i=nxt+1) { |
| 270 | |
| 271 | for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) |
| 272 | ; |
| 273 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 274 | lval = (char *)env_get_addr(i); |
wdenk | 04a85b3 | 2004-04-15 18:22:41 +0000 | [diff] [blame] | 275 | rval = strchr(lval, '='); |
| 276 | if (rval != NULL) { |
| 277 | vallen = rval - lval; |
| 278 | rval++; |
| 279 | } else |
| 280 | vallen = strlen(lval); |
| 281 | |
| 282 | if (len > 0 && (vallen < len || memcmp(lval, var, len) != 0)) |
| 283 | continue; |
| 284 | |
| 285 | if (found >= maxv - 2 || bufsz < vallen + 1) { |
| 286 | cmdv[found++] = "..."; |
| 287 | break; |
| 288 | } |
| 289 | cmdv[found++] = buf; |
| 290 | memcpy(buf, lval, vallen); buf += vallen; bufsz -= vallen; |
| 291 | *buf++ = '\0'; bufsz--; |
| 292 | } |
| 293 | |
| 294 | cmdv[found] = NULL; |
| 295 | return found; |
| 296 | } |
| 297 | #endif |