2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <environment.h>
30 #include <upg_scripts.h>
31 #include <linux/stddef.h>
35 DECLARE_GLOBAL_DATA_PTR;
39 #if defined(DEBUG_ENV)
40 #define DEBUGF(fmt,args...) printf(fmt ,##args)
42 #define DEBUGF(fmt,args...)
45 extern env_t *env_ptr;
47 extern void env_relocate_spec(void);
48 extern uchar env_get_char_spec(int);
50 static uchar env_get_char_init(int index);
51 uchar (*env_get_char)(int) = env_get_char_init;
53 /************************************************************************
54 * Default settings to be used when no valid environment is found
57 #define MK_STR(x) XMK_STR(x)
59 uchar default_environment[] = {
60 #if defined(CONFIG_BOOTARGS)
61 "bootargs=" CONFIG_BOOTARGS "\0"
63 #if defined(CONFIG_BOOTCOMMAND)
64 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
66 #if defined(CONFIG_RAMBOOTCOMMAND)
67 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
69 #if defined(CONFIG_NFSBOOTCOMMAND)
70 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
72 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
73 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
75 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
76 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
78 #if defined(CONFIG_LOADS_ECHO)
79 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
81 #if defined(CONFIG_ETHADDR)
82 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
84 #if defined(CONFIG_ETH1ADDR)
85 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
87 #if defined(CONFIG_ETH2ADDR)
88 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
90 #if defined(CONFIG_ETH3ADDR)
91 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
93 #if defined(CONFIG_IPADDR)
94 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
96 #if defined(CONFIG_SERVERIP)
97 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
99 #if defined(CFG_AUTOLOAD)
100 "autoload=" CFG_AUTOLOAD "\0"
102 #if defined(CONFIG_ROOTPATH)
103 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
105 #if defined(CONFIG_GATEWAYIP)
106 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
108 #if defined(CONFIG_NETMASK)
109 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
111 #if defined(CONFIG_HOSTNAME)
112 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
114 #if defined(CONFIG_BOOTFILE)
115 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
117 #if defined(CONFIG_LOADADDR)
118 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
120 #if defined(CONFIG_CLOCKS_IN_MHZ)
123 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
124 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
126 #if defined(CONFIG_NETCONSOLE_PORT)
127 "ncport=" MK_STR(CONFIG_NETCONSOLE_PORT) "\0"
129 #if defined(CONFIG_EXTRA_ENV_SETTINGS)
130 CONFIG_EXTRA_ENV_SETTINGS
132 #if defined(CONFIG_ENV_UPG_SCRIPTS_UBOOT)
133 CONFIG_ENV_UPG_SCRIPTS_UBOOT
135 #if defined(CONFIG_ENV_UPG_SCRIPTS_FW)
136 CONFIG_ENV_UPG_SCRIPTS_FW
141 /* Environment is in Nand Flash */
142 #if defined(CFG_ENV_IS_IN_NAND)
143 int default_environment_size = sizeof(default_environment);
146 void env_crc_update(void)
148 env_ptr->crc = tinf_crc32(env_ptr->data, ENV_SIZE);
151 static uchar env_get_char_init(int index)
155 /* if crc was bad, use the default environment */
157 c = env_get_char_spec(index);
159 c = default_environment[index];
164 uchar env_get_char_memory(int index)
167 return *((uchar *)(gd->env_addr + index));
169 return default_environment[index];
172 uchar *env_get_addr(int index)
175 return ((uchar *)(gd->env_addr + index));
177 return &default_environment[index];
180 void env_relocate(void)
182 DEBUGF("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__, gd->reloc_off);
184 #if defined(ENV_IS_EMBEDDED)
186 * The environment buffer is embedded with the text segment,
187 * just relocate the environment pointer
189 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
190 DEBUGF("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
192 /* We must allocate a buffer for the environment */
193 env_ptr = (env_t *)malloc(CFG_ENV_SIZE);
194 DEBUGF("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
197 /* After relocation to RAM, we can always use the "memory" functions */
198 env_get_char = env_get_char_memory;
200 if (gd->env_valid == 0) {
201 #if !defined(CFG_ENV_IS_NOWHERE)
202 puts("** Warning: bad env CRC, using default,\n"
203 " use 'saveenv' to save it in FLASH\n\n");
206 if (sizeof(default_environment) > ENV_SIZE) {
207 puts("## Error: default env is too big!\n");
211 memset(env_ptr, 0, sizeof(env_t));
212 memcpy(env_ptr->data, default_environment,
213 sizeof(default_environment));
215 #if defined(CFG_REDUNDAND_ENVIRONMENT)
216 env_ptr->flags = 0xFF;
225 gd->env_addr = (ulong)&(env_ptr->data);