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