blob: 426d01b19bef3306b1b38f8377c4101406052d26 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
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/* #define DEBUG */
28
29#include <common.h>
30
31#if defined(CFG_ENV_IS_IN_FLASH) /* Environment is in Flash */
32
33#include <command.h>
34#include <environment.h>
35#include <cmd_nvedit.h>
36#include <linux/stddef.h>
wdenkcc1e2562003-03-06 13:39:27 +000037#include <malloc.h>
wdenkc6097192002-11-03 00:24:07 +000038
39#if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
40#define CMD_SAVEENV
41#elif defined(CFG_ENV_ADDR_REDUND)
42#error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH
43#endif
44
wdenkc6097192002-11-03 00:24:07 +000045#if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
46#error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
47#endif
48
49#ifdef CONFIG_INFERNO
50# ifdef CFG_ENV_ADDR_REDUND
51#error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
52# endif
53#endif
54
55char * env_name_spec = "Flash";
56
57#ifdef ENV_IS_EMBEDDED
58
59extern uchar environment[];
60env_t *env_ptr = (env_t *)(&environment[0]);
61
62#ifdef CMD_SAVEENV
63/* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
64static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
65#endif
66
67#else /* ! ENV_IS_EMBEDDED */
68
69env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
70#ifdef CMD_SAVEENV
71static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
72#endif
73
74#endif /* ENV_IS_EMBEDDED */
75
76#ifdef CFG_ENV_ADDR_REDUND
77static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
78
wdenkcc1e2562003-03-06 13:39:27 +000079/* CFG_ENV_ADDR is supposed to be on sector boundary */
80static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
81static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
wdenkc6097192002-11-03 00:24:07 +000082
83static uchar active_flag = 1;
84static uchar obsolete_flag = 0;
85#endif
86
87extern uchar default_environment[];
88extern int default_environment_size;
89
90
91uchar env_get_char_spec (int index)
92{
93 DECLARE_GLOBAL_DATA_PTR;
94
95 return ( *((uchar *)(gd->env_addr + index)) );
96}
97
98#ifdef CFG_ENV_ADDR_REDUND
99
100int env_init(void)
101{
102 DECLARE_GLOBAL_DATA_PTR;
103
104 int crc1_ok =
105 (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
106 int crc2_ok =
107 (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
108
109 uchar flag1 = flash_addr->flags;
110 uchar flag2 = flash_addr_new->flags;
111
112 ulong addr_default = (ulong)&default_environment[0];
113 ulong addr1 = (ulong)&(flash_addr->data);
114 ulong addr2 = (ulong)&(flash_addr_new->data);
115
116 if (crc1_ok && ! crc2_ok)
117 {
118 gd->env_addr = addr1;
119 gd->env_valid = 1;
120 }
121 else if (! crc1_ok && crc2_ok)
122 {
123 gd->env_addr = addr2;
124 gd->env_valid = 1;
125 }
126 else if (! crc1_ok && ! crc2_ok)
127 {
128 gd->env_addr = addr_default;
129 gd->env_valid = 0;
130 }
131 else if (flag1 == active_flag && flag2 == obsolete_flag)
132 {
133 gd->env_addr = addr1;
134 gd->env_valid = 1;
135 }
136 else if (flag1 == obsolete_flag && flag2 == active_flag)
137 {
138 gd->env_addr = addr2;
139 gd->env_valid = 1;
140 }
141 else if (flag1 == flag2)
142 {
143 gd->env_addr = addr1;
144 gd->env_valid = 2;
145 }
146 else if (flag1 == 0xFF)
147 {
148 gd->env_addr = addr1;
149 gd->env_valid = 2;
150 }
151 else if (flag2 == 0xFF)
152 {
153 gd->env_addr = addr2;
154 gd->env_valid = 2;
155 }
156
157 return (0);
158}
159
160#ifdef CMD_SAVEENV
161int saveenv(void)
162{
wdenkd730fbc2003-03-06 14:23:06 +0000163 char *saved_data = NULL;
wdenkc6097192002-11-03 00:24:07 +0000164 int rc = 1;
wdenkd730fbc2003-03-06 14:23:06 +0000165#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000166 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000167#endif
wdenkc6097192002-11-03 00:24:07 +0000168
169 debug ("Protect off %08lX ... %08lX\n",
170 (ulong)flash_addr, end_addr);
171
172 if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
173 goto Done;
174 }
175
176 debug ("Protect off %08lX ... %08lX\n",
177 (ulong)flash_addr_new, end_addr_new);
178
179 if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
180 goto Done;
181 }
182
wdenkcc1e2562003-03-06 13:39:27 +0000183#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
184 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
185 debug ("Data to save 0x%x\n", up_data);
186 if (up_data) {
187 if ((saved_data = malloc(up_data)) == NULL) {
188 printf("Unable to save the rest of sector (%ld)\n",
189 up_data);
190 goto Done;
191 }
192 memcpy(saved_data,
193 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
194 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
195 (long)flash_addr_new + CFG_ENV_SIZE,
196 up_data, saved_data);
197 }
198#endif
wdenkc6097192002-11-03 00:24:07 +0000199 puts ("Erasing Flash...");
200 debug (" %08lX ... %08lX ...",
201 (ulong)flash_addr_new, end_addr_new);
202
203 if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
204 goto Done;
205 }
206
207 puts ("Writing to Flash... ");
208 debug (" %08lX ... %08lX ...",
209 (ulong)&(flash_addr_new->data),
210 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
211 if (flash_write(env_ptr->data,
212 (ulong)&(flash_addr_new->data),
213 sizeof(env_ptr->data)) ||
214
215 flash_write((char *)&(env_ptr->crc),
216 (ulong)&(flash_addr_new->crc),
217 sizeof(env_ptr->crc)) ||
218
219 flash_write((char *)&obsolete_flag,
220 (ulong)&(flash_addr->flags),
221 sizeof(flash_addr->flags)) ||
222
223 flash_write((char *)&active_flag,
224 (ulong)&(flash_addr_new->flags),
225 sizeof(flash_addr_new->flags)))
226 {
227 flash_perror (rc);
228 goto Done;
229 }
230 puts ("done\n");
231
wdenkcc1e2562003-03-06 13:39:27 +0000232#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
233 if (up_data) { /* restore the rest of sector */
234 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
235 (long)flash_addr_new + CFG_ENV_SIZE, up_data);
236 if (flash_write(saved_data,
237 (long)flash_addr_new + CFG_ENV_SIZE,
238 up_data)) {
239 flash_perror(rc);
240 goto Done;
241 }
242 }
243#endif
wdenkc6097192002-11-03 00:24:07 +0000244 {
245 env_t * etmp = flash_addr;
246 ulong ltmp = end_addr;
247
248 flash_addr = flash_addr_new;
249 flash_addr_new = etmp;
250
251 end_addr = end_addr_new;
252 end_addr_new = ltmp;
253 }
254
255 rc = 0;
256Done:
257
wdenkcc1e2562003-03-06 13:39:27 +0000258 if (saved_data)
259 free (saved_data);
wdenkc6097192002-11-03 00:24:07 +0000260 /* try to re-protect */
261 (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
262 (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
263
264 return rc;
265}
266#endif /* CMD_SAVEENV */
267
268#else /* ! CFG_ENV_ADDR_REDUND */
269
270int env_init(void)
271{
272 DECLARE_GLOBAL_DATA_PTR;
273
274 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
275 gd->env_addr = (ulong)&(env_ptr->data);
276 gd->env_valid = 1;
277 } else {
278 gd->env_addr = (ulong)&default_environment[0];
279 gd->env_valid = 0;
280 }
281
282 return (0);
283}
284
285#ifdef CMD_SAVEENV
286
287int saveenv(void)
288{
289 int len, rc;
290 ulong end_addr;
291 ulong flash_sect_addr;
292#if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
293 ulong flash_offset;
294 uchar env_buffer[CFG_ENV_SECT_SIZE];
295#else
296 uchar *env_buffer = (char *)env_ptr;
297#endif /* CFG_ENV_SECT_SIZE */
298 int rcode = 0;
299
300#if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
301
302 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
303 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
304
305 debug ( "copy old content: "
306 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
307 flash_sect_addr, (ulong)flash_addr, flash_offset);
308
309 /* copy old contents to temporary buffer */
310 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
311
312 /* copy current environment to temporary buffer */
313 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
314 env_ptr,
315 CFG_ENV_SIZE);
316
317 len = CFG_ENV_SECT_SIZE;
318#else
319 flash_sect_addr = (ulong)flash_addr;
320 len = CFG_ENV_SIZE;
321#endif /* CFG_ENV_SECT_SIZE */
322
323#ifndef CONFIG_INFERNO
324 end_addr = flash_sect_addr + len - 1;
325#else
326 /* this is the last sector, and the size is hardcoded here */
327 /* otherwise we will get stack problems on loading 128 KB environment */
328 end_addr = flash_sect_addr + 0x20000 - 1;
329#endif
330
331 debug ("Protect off %08lX ... %08lX\n",
332 (ulong)flash_sect_addr, end_addr);
333
334 if (flash_sect_protect (0, flash_sect_addr, end_addr))
335 return 1;
336
337 puts ("Erasing Flash...");
338 if (flash_sect_erase (flash_sect_addr, end_addr))
339 return 1;
340
341 puts ("Writing to Flash... ");
342 rc = flash_write(env_buffer, flash_sect_addr, len);
343 if (rc != 0) {
344 flash_perror (rc);
345 rcode = 1;
346 } else {
347 puts ("done\n");
348 }
349
350 /* try to re-protect */
351 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
352 return rcode;
353}
354
355#endif /* CMD_SAVEENV */
356
357#endif /* CFG_ENV_ADDR_REDUND */
358
359void env_relocate_spec (void)
360{
361#if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
362#ifdef CFG_ENV_ADDR_REDUND
363 DECLARE_GLOBAL_DATA_PTR;
364
365 if (gd->env_addr != (ulong)&(flash_addr->data))
366 {
367 env_t * etmp = flash_addr;
368 ulong ltmp = end_addr;
369
370 flash_addr = flash_addr_new;
371 flash_addr_new = etmp;
372
373 end_addr = end_addr_new;
374 end_addr_new = ltmp;
375 }
376
377 if (flash_addr_new->flags != obsolete_flag &&
378 crc32(0, flash_addr_new->data, ENV_SIZE) ==
379 flash_addr_new->crc)
380 {
381 gd->env_valid = 2;
382 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
383 flash_write((char *)&obsolete_flag,
384 (ulong)&(flash_addr_new->flags),
385 sizeof(flash_addr_new->flags));
386 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
387 }
388
389 if (flash_addr->flags != active_flag &&
390 (flash_addr->flags & active_flag) == active_flag)
391 {
392 gd->env_valid = 2;
393 flash_sect_protect (0, (ulong)flash_addr, end_addr);
394 flash_write((char *)&active_flag,
395 (ulong)&(flash_addr->flags),
396 sizeof(flash_addr->flags));
397 flash_sect_protect (1, (ulong)flash_addr, end_addr);
398 }
399
400 if (gd->env_valid == 2)
401 puts ("*** Warning - some problems detected "
402 "reading environment; recovered successfully\n\n");
403#endif /* CFG_ENV_ADDR_REDUND */
404 memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
405#endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
406}
407
wdenk4a5c8a72003-03-06 00:02:04 +0000408#endif /* CFG_ENV_IS_IN_FLASH */