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> |
| 30 | #include <cmd_nvedit.h> |
| 31 | #include <linux/stddef.h> |
| 32 | #include <malloc.h> |
| 33 | |
| 34 | #ifdef CONFIG_SHOW_BOOT_PROGRESS |
| 35 | # include <status_led.h> |
| 36 | # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg) |
| 37 | #else |
| 38 | # define SHOW_BOOT_PROGRESS(arg) |
| 39 | #endif |
| 40 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_AMIGAONEG3SE |
| 42 | extern void enable_nvram(void); |
| 43 | extern void disable_nvram(void); |
| 44 | #endif |
| 45 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 46 | #undef DEBUG_ENV |
| 47 | #ifdef DEBUG_ENV |
| 48 | #define DEBUGF(fmt,args...) printf(fmt ,##args) |
| 49 | #else |
| 50 | #define DEBUGF(fmt,args...) |
| 51 | #endif |
| 52 | |
| 53 | extern env_t *env_ptr; |
| 54 | |
| 55 | extern void env_relocate_spec (void); |
| 56 | extern uchar env_get_char_spec(int); |
| 57 | |
| 58 | static uchar env_get_char_init (int index); |
| 59 | uchar (*env_get_char)(int) = env_get_char_init; |
| 60 | |
| 61 | /************************************************************************ |
| 62 | * Default settings to be used when no valid environment is found |
| 63 | */ |
| 64 | #define XMK_STR(x) #x |
| 65 | #define MK_STR(x) XMK_STR(x) |
| 66 | |
| 67 | uchar default_environment[] = { |
| 68 | #ifdef CONFIG_BOOTARGS |
| 69 | "bootargs=" CONFIG_BOOTARGS "\0" |
| 70 | #endif |
| 71 | #ifdef CONFIG_BOOTCOMMAND |
| 72 | "bootcmd=" CONFIG_BOOTCOMMAND "\0" |
| 73 | #endif |
| 74 | #ifdef CONFIG_RAMBOOTCOMMAND |
| 75 | "ramboot=" CONFIG_RAMBOOTCOMMAND "\0" |
| 76 | #endif |
| 77 | #ifdef CONFIG_NFSBOOTCOMMAND |
| 78 | "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" |
| 79 | #endif |
| 80 | #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) |
| 81 | "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0" |
| 82 | #endif |
| 83 | #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) |
| 84 | "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0" |
| 85 | #endif |
| 86 | #ifdef CONFIG_LOADS_ECHO |
| 87 | "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0" |
| 88 | #endif |
| 89 | #ifdef CONFIG_ETHADDR |
| 90 | "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0" |
| 91 | #endif |
| 92 | #ifdef CONFIG_ETH1ADDR |
| 93 | "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0" |
| 94 | #endif |
| 95 | #ifdef CONFIG_ETH2ADDR |
| 96 | "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0" |
| 97 | #endif |
| 98 | #ifdef CONFIG_IPADDR |
| 99 | "ipaddr=" MK_STR(CONFIG_IPADDR) "\0" |
| 100 | #endif |
| 101 | #ifdef CONFIG_SERVERIP |
| 102 | "serverip=" MK_STR(CONFIG_SERVERIP) "\0" |
| 103 | #endif |
| 104 | #ifdef CFG_AUTOLOAD |
| 105 | "autoload=" CFG_AUTOLOAD "\0" |
| 106 | #endif |
| 107 | #ifdef CONFIG_PREBOOT |
| 108 | "preboot=" CONFIG_PREBOOT "\0" |
| 109 | #endif |
| 110 | #ifdef CONFIG_ROOTPATH |
| 111 | "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0" |
| 112 | #endif |
| 113 | #ifdef CONFIG_GATEWAYIP |
| 114 | "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0" |
| 115 | #endif |
| 116 | #ifdef CONFIG_NETMASK |
| 117 | "netmask=" MK_STR(CONFIG_NETMASK) "\0" |
| 118 | #endif |
| 119 | #ifdef CONFIG_HOSTNAME |
| 120 | "hostname=" MK_STR(CONFIG_HOSTNAME) "\0" |
| 121 | #endif |
| 122 | #ifdef CONFIG_BOOTFILE |
| 123 | "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0" |
| 124 | #endif |
| 125 | #ifdef CONFIG_LOADADDR |
| 126 | "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0" |
| 127 | #endif |
| 128 | #ifdef CONFIG_CLOCKS_IN_MHZ |
| 129 | "clocks_in_mhz=1\0" |
| 130 | #endif |
stroese | ad10dd9 | 2003-02-14 11:21:23 +0000 | [diff] [blame^] | 131 | #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) |
| 132 | "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0" |
| 133 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 134 | #ifdef CONFIG_EXTRA_ENV_SETTINGS |
| 135 | CONFIG_EXTRA_ENV_SETTINGS |
| 136 | #endif |
| 137 | "\0" |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | void env_crc_update (void) |
| 142 | { |
| 143 | env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE); |
| 144 | } |
| 145 | |
| 146 | static uchar env_get_char_init (int index) |
| 147 | { |
| 148 | DECLARE_GLOBAL_DATA_PTR; |
| 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 | { |
| 165 | DECLARE_GLOBAL_DATA_PTR; |
| 166 | uchar retval; |
| 167 | enable_nvram(); |
| 168 | if (gd->env_valid) { |
| 169 | retval = ( *((uchar *)(gd->env_addr + index)) ); |
| 170 | } else { |
| 171 | retval = ( default_environment[index] ); |
| 172 | } |
| 173 | disable_nvram(); |
| 174 | return retval; |
| 175 | } |
| 176 | #else |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 177 | uchar env_get_char_memory (int index) |
| 178 | { |
| 179 | DECLARE_GLOBAL_DATA_PTR; |
| 180 | |
| 181 | if (gd->env_valid) { |
| 182 | return ( *((uchar *)(gd->env_addr + index)) ); |
| 183 | } else { |
| 184 | return ( default_environment[index] ); |
| 185 | } |
| 186 | } |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 187 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 188 | |
| 189 | uchar *env_get_addr (int index) |
| 190 | { |
| 191 | DECLARE_GLOBAL_DATA_PTR; |
| 192 | |
| 193 | if (gd->env_valid) { |
| 194 | return ( ((uchar *)(gd->env_addr + index)) ); |
| 195 | } else { |
| 196 | return (&default_environment[index]); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void env_relocate (void) |
| 201 | { |
| 202 | DECLARE_GLOBAL_DATA_PTR; |
| 203 | |
| 204 | DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__, |
| 205 | gd->reloc_off); |
| 206 | |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 207 | #ifdef CONFIG_AMIGAONEG3SE |
| 208 | enable_nvram(); |
| 209 | #endif |
| 210 | |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 211 | #ifdef ENV_IS_EMBEDDED |
| 212 | /* |
| 213 | * The environment buffer is embedded with the text segment, |
| 214 | * just relocate the environment pointer |
| 215 | */ |
| 216 | env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off); |
| 217 | DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr); |
| 218 | #else |
| 219 | /* |
| 220 | * We must allocate a buffer for the environment |
| 221 | */ |
| 222 | env_ptr = (env_t *)malloc (CFG_ENV_SIZE); |
| 223 | DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr); |
| 224 | #endif |
| 225 | |
| 226 | /* |
| 227 | * After relocation to RAM, we can always use the "memory" functions |
| 228 | */ |
| 229 | env_get_char = env_get_char_memory; |
| 230 | |
| 231 | if (gd->env_valid == 0) { |
| 232 | #if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */ |
| 233 | puts ("Using default environment\n\n"); |
| 234 | #else |
| 235 | puts ("*** Warning - bad CRC, using default environment\n\n"); |
| 236 | SHOW_BOOT_PROGRESS (-1); |
| 237 | #endif |
| 238 | |
| 239 | if (sizeof(default_environment) > ENV_SIZE) |
| 240 | { |
| 241 | puts ("*** Error - default environment is too large\n\n"); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | memset (env_ptr, 0, sizeof(env_t)); |
| 246 | memcpy (env_ptr->data, |
| 247 | default_environment, |
| 248 | sizeof(default_environment)); |
| 249 | #ifdef CFG_REDUNDAND_ENVIRONMENT |
| 250 | env_ptr->flags = 0xFF; |
| 251 | #endif |
| 252 | env_crc_update (); |
| 253 | gd->env_valid = 1; |
| 254 | } |
| 255 | else { |
| 256 | env_relocate_spec (); |
| 257 | } |
| 258 | gd->env_addr = (ulong)&(env_ptr->data); |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 259 | |
| 260 | #ifdef CONFIG_AMIGAONEG3SE |
| 261 | disable_nvram(); |
| 262 | #endif |
wdenk | 05706fd | 2002-09-28 17:48:27 +0000 | [diff] [blame] | 263 | } |