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