Restore and enable by default upgrade scripts
[oweals/u-boot_mod.git] / u-boot / common / env_common.c
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 <upg_scripts.h>
31 #include <linux/stddef.h>
32 #include <malloc.h>
33 #include <tinf.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 #undef DEBUG_ENV
38
39 #if defined(DEBUG_ENV)
40         #define DEBUGF(fmt,args...) printf(fmt ,##args)
41 #else
42         #define DEBUGF(fmt,args...)
43 #endif
44
45 extern env_t *env_ptr;
46
47 extern void env_relocate_spec(void);
48 extern uchar env_get_char_spec(int);
49
50 static uchar env_get_char_init(int index);
51 uchar (*env_get_char)(int) = env_get_char_init;
52
53 /************************************************************************
54  * Default settings to be used when no valid environment is found
55  */
56 #define XMK_STR(x)      #x
57 #define MK_STR(x)       XMK_STR(x)
58
59 uchar default_environment[] = {
60 #if defined(CONFIG_BOOTARGS)
61         "bootargs=" CONFIG_BOOTARGS "\0"
62 #endif
63 #if defined(CONFIG_BOOTCOMMAND)
64         "bootcmd=" CONFIG_BOOTCOMMAND "\0"
65 #endif
66 #if defined(CONFIG_RAMBOOTCOMMAND)
67         "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
68 #endif
69 #if defined(CONFIG_NFSBOOTCOMMAND)
70         "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
71 #endif
72 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
73         "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
74 #endif
75 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
76         "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
77 #endif
78 #if defined(CONFIG_LOADS_ECHO)
79         "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
80 #endif
81 #if defined(CONFIG_ETHADDR)
82         "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
83 #endif
84 #if defined(CONFIG_ETH1ADDR)
85         "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
86 #endif
87 #if defined(CONFIG_ETH2ADDR)
88         "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
89 #endif
90 #if defined(CONFIG_ETH3ADDR)
91         "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
92 #endif
93 #if defined(CONFIG_IPADDR)
94         "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
95 #endif
96 #if defined(CONFIG_SERVERIP)
97         "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
98 #endif
99 #if defined(CFG_AUTOLOAD)
100         "autoload=" CFG_AUTOLOAD "\0"
101 #endif
102 #if defined(CONFIG_ROOTPATH)
103         "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
104 #endif
105 #if defined(CONFIG_GATEWAYIP)
106         "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
107 #endif
108 #if defined(CONFIG_NETMASK)
109         "netmask=" MK_STR(CONFIG_NETMASK) "\0"
110 #endif
111 #if defined(CONFIG_HOSTNAME)
112         "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
113 #endif
114 #if defined(CONFIG_BOOTFILE)
115         "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
116 #endif
117 #if defined(CONFIG_LOADADDR)
118         "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
119 #endif
120 #if defined(CONFIG_CLOCKS_IN_MHZ)
121         "clocks_in_mhz=1\0"
122 #endif
123 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
124         "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
125 #endif
126 #if defined(CONFIG_NETCONSOLE_PORT)
127         "ncport=" MK_STR(CONFIG_NETCONSOLE_PORT) "\0"
128 #endif
129 #if defined(CONFIG_EXTRA_ENV_SETTINGS)
130         CONFIG_EXTRA_ENV_SETTINGS
131 #endif
132 #if defined(CONFIG_ENV_UPG_SCRIPTS_UBOOT)
133                 CONFIG_ENV_UPG_SCRIPTS_UBOOT
134 #endif
135 #if defined(CONFIG_ENV_UPG_SCRIPTS_FW)
136                 CONFIG_ENV_UPG_SCRIPTS_FW
137 #endif
138         "\0"
139 };
140
141 /* Environment is in Nand Flash */
142 #if defined(CFG_ENV_IS_IN_NAND)
143 int default_environment_size = sizeof(default_environment);
144 #endif
145
146 void env_crc_update(void)
147 {
148         env_ptr->crc = tinf_crc32(env_ptr->data, ENV_SIZE);
149 }
150
151 static uchar env_get_char_init(int index)
152 {
153         uchar c;
154
155         /* if crc was bad, use the default environment */
156         if (gd->env_valid)
157                 c = env_get_char_spec(index);
158         else
159                 c = default_environment[index];
160
161         return c;
162 }
163
164 uchar env_get_char_memory(int index)
165 {
166         if (gd->env_valid)
167                 return *((uchar *)(gd->env_addr + index));
168         else
169                 return default_environment[index];
170 }
171
172 uchar *env_get_addr(int index)
173 {
174         if (gd->env_valid)
175                 return ((uchar *)(gd->env_addr + index));
176         else
177                 return &default_environment[index];
178 }
179
180 void env_relocate(void)
181 {
182         DEBUGF("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__, gd->reloc_off);
183
184 #if defined(ENV_IS_EMBEDDED)
185         /*
186          * The environment buffer is embedded with the text segment,
187          * just relocate the environment pointer
188          */
189         env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
190         DEBUGF("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
191 #else
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);
195 #endif
196
197         /* After relocation to RAM, we can always use the "memory" functions */
198         env_get_char = env_get_char_memory;
199
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");
204 #endif
205
206                 if (sizeof(default_environment) > ENV_SIZE) {
207                         puts("## Error: default env is too big!\n");
208                         return;
209                 }
210
211                 memset(env_ptr, 0, sizeof(env_t));
212                 memcpy(env_ptr->data, default_environment,
213                         sizeof(default_environment));
214
215 #if defined(CFG_REDUNDAND_ENVIRONMENT)
216                 env_ptr->flags = 0xFF;
217 #endif
218
219                 env_crc_update();
220                 gd->env_valid = 1;
221         } else {
222                 env_relocate_spec();
223         }
224
225         gd->env_addr = (ulong)&(env_ptr->data);
226 }