blob: d6257d07bd097db647a8e7b8d503c00e660a68e4 [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>
wdenkc6097192002-11-03 00:24:07 +000035#include <linux/stddef.h>
wdenkcc1e2562003-03-06 13:39:27 +000036#include <malloc.h>
wdenkc6097192002-11-03 00:24:07 +000037
38#if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_FLASH)) == (CFG_CMD_ENV|CFG_CMD_FLASH))
39#define CMD_SAVEENV
40#elif defined(CFG_ENV_ADDR_REDUND)
41#error Cannot use CFG_ENV_ADDR_REDUND without CFG_CMD_ENV & CFG_CMD_FLASH
42#endif
43
wdenkc6097192002-11-03 00:24:07 +000044#if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
45#error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
46#endif
47
48#ifdef CONFIG_INFERNO
49# ifdef CFG_ENV_ADDR_REDUND
50#error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
51# endif
52#endif
53
54char * env_name_spec = "Flash";
55
56#ifdef ENV_IS_EMBEDDED
57
58extern uchar environment[];
59env_t *env_ptr = (env_t *)(&environment[0]);
60
61#ifdef CMD_SAVEENV
62/* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
63static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
64#endif
65
66#else /* ! ENV_IS_EMBEDDED */
67
68env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
69#ifdef CMD_SAVEENV
70static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
71#endif
72
73#endif /* ENV_IS_EMBEDDED */
74
75#ifdef CFG_ENV_ADDR_REDUND
76static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
77
wdenkcc1e2562003-03-06 13:39:27 +000078/* CFG_ENV_ADDR is supposed to be on sector boundary */
79static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
80static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
wdenkc6097192002-11-03 00:24:07 +000081
wdenkacd9b102004-03-14 00:59:59 +000082#define ACTIVE_FLAG 1
83#define OBSOLETE_FLAG 0
84#endif /* CFG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000085
86extern uchar default_environment[];
87extern int default_environment_size;
88
89
90uchar env_get_char_spec (int index)
91{
92 DECLARE_GLOBAL_DATA_PTR;
93
94 return ( *((uchar *)(gd->env_addr + index)) );
95}
96
97#ifdef CFG_ENV_ADDR_REDUND
98
99int env_init(void)
100{
101 DECLARE_GLOBAL_DATA_PTR;
wdenkf8062712005-01-09 23:16:25 +0000102 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +0000103
104 uchar flag1 = flash_addr->flags;
105 uchar flag2 = flash_addr_new->flags;
106
107 ulong addr_default = (ulong)&default_environment[0];
108 ulong addr1 = (ulong)&(flash_addr->data);
109 ulong addr2 = (ulong)&(flash_addr_new->data);
110
wdenkf8062712005-01-09 23:16:25 +0000111#ifdef CONFIG_OMAP2420H4
112 int flash_probe(void);
113
114 if(flash_probe() == 0)
115 goto bad_flash;
116#endif
117
118 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
119 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
120
wdenkacd9b102004-03-14 00:59:59 +0000121 if (crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000122 gd->env_addr = addr1;
123 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000124 } else if (! crc1_ok && crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000125 gd->env_addr = addr2;
126 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000127 } else if (! crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000128 gd->env_addr = addr_default;
129 gd->env_valid = 0;
wdenkacd9b102004-03-14 00:59:59 +0000130 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000131 gd->env_addr = addr1;
132 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000133 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000134 gd->env_addr = addr2;
135 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000136 } else if (flag1 == flag2) {
wdenkc6097192002-11-03 00:24:07 +0000137 gd->env_addr = addr1;
138 gd->env_valid = 2;
wdenkacd9b102004-03-14 00:59:59 +0000139 } else if (flag1 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000140 gd->env_addr = addr1;
141 gd->env_valid = 2;
wdenkacd9b102004-03-14 00:59:59 +0000142 } else if (flag2 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000143 gd->env_addr = addr2;
144 gd->env_valid = 2;
145 }
146
wdenkf8062712005-01-09 23:16:25 +0000147#ifdef CONFIG_OMAP2420H4
148bad_flash:
149#endif
wdenkc6097192002-11-03 00:24:07 +0000150 return (0);
151}
152
153#ifdef CMD_SAVEENV
154int saveenv(void)
155{
wdenkd730fbc2003-03-06 14:23:06 +0000156 char *saved_data = NULL;
wdenkc6097192002-11-03 00:24:07 +0000157 int rc = 1;
wdenkacd9b102004-03-14 00:59:59 +0000158 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
wdenkd730fbc2003-03-06 14:23:06 +0000159#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000160 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000161#endif
wdenkc6097192002-11-03 00:24:07 +0000162
163 debug ("Protect off %08lX ... %08lX\n",
164 (ulong)flash_addr, end_addr);
165
166 if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
167 goto Done;
168 }
169
170 debug ("Protect off %08lX ... %08lX\n",
171 (ulong)flash_addr_new, end_addr_new);
172
173 if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
174 goto Done;
175 }
176
wdenkcc1e2562003-03-06 13:39:27 +0000177#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
178 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
179 debug ("Data to save 0x%x\n", up_data);
180 if (up_data) {
181 if ((saved_data = malloc(up_data)) == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000182 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000183 up_data);
184 goto Done;
185 }
wdenk57b2d802003-06-27 21:31:46 +0000186 memcpy(saved_data,
wdenkcc1e2562003-03-06 13:39:27 +0000187 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
wdenk57b2d802003-06-27 21:31:46 +0000188 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
189 (long)flash_addr_new + CFG_ENV_SIZE,
wdenkcc1e2562003-03-06 13:39:27 +0000190 up_data, saved_data);
191 }
192#endif
wdenkc6097192002-11-03 00:24:07 +0000193 puts ("Erasing Flash...");
194 debug (" %08lX ... %08lX ...",
195 (ulong)flash_addr_new, end_addr_new);
196
197 if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
198 goto Done;
199 }
200
201 puts ("Writing to Flash... ");
202 debug (" %08lX ... %08lX ...",
203 (ulong)&(flash_addr_new->data),
204 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
wdenkf602aa02004-03-13 23:29:43 +0000205 if ((rc = flash_write(env_ptr->data,
wdenk57b2d802003-06-27 21:31:46 +0000206 (ulong)&(flash_addr_new->data),
wdenkf602aa02004-03-13 23:29:43 +0000207 sizeof(env_ptr->data))) ||
wdenkf602aa02004-03-13 23:29:43 +0000208 (rc = flash_write((char *)&(env_ptr->crc),
wdenk57b2d802003-06-27 21:31:46 +0000209 (ulong)&(flash_addr_new->crc),
wdenkf602aa02004-03-13 23:29:43 +0000210 sizeof(env_ptr->crc))) ||
wdenkacd9b102004-03-14 00:59:59 +0000211 (rc = flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000212 (ulong)&(flash_addr->flags),
wdenkf602aa02004-03-13 23:29:43 +0000213 sizeof(flash_addr->flags))) ||
wdenkacd9b102004-03-14 00:59:59 +0000214 (rc = flash_write(&new_flag,
wdenk57b2d802003-06-27 21:31:46 +0000215 (ulong)&(flash_addr_new->flags),
wdenkf602aa02004-03-13 23:29:43 +0000216 sizeof(flash_addr_new->flags))))
wdenkc6097192002-11-03 00:24:07 +0000217 {
218 flash_perror (rc);
219 goto Done;
220 }
221 puts ("done\n");
222
wdenkcc1e2562003-03-06 13:39:27 +0000223#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
224 if (up_data) { /* restore the rest of sector */
225 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
226 (long)flash_addr_new + CFG_ENV_SIZE, up_data);
wdenk57b2d802003-06-27 21:31:46 +0000227 if (flash_write(saved_data,
228 (long)flash_addr_new + CFG_ENV_SIZE,
wdenkcc1e2562003-03-06 13:39:27 +0000229 up_data)) {
230 flash_perror(rc);
231 goto Done;
232 }
233 }
234#endif
wdenkc6097192002-11-03 00:24:07 +0000235 {
236 env_t * etmp = flash_addr;
237 ulong ltmp = end_addr;
238
239 flash_addr = flash_addr_new;
240 flash_addr_new = etmp;
241
242 end_addr = end_addr_new;
243 end_addr_new = ltmp;
244 }
245
246 rc = 0;
247Done:
248
wdenkcc1e2562003-03-06 13:39:27 +0000249 if (saved_data)
250 free (saved_data);
wdenkc6097192002-11-03 00:24:07 +0000251 /* try to re-protect */
252 (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
253 (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
254
255 return rc;
256}
257#endif /* CMD_SAVEENV */
258
259#else /* ! CFG_ENV_ADDR_REDUND */
260
261int env_init(void)
262{
263 DECLARE_GLOBAL_DATA_PTR;
wdenkf8062712005-01-09 23:16:25 +0000264#ifdef CONFIG_OMAP2420H4
265 int flash_probe(void);
wdenkc6097192002-11-03 00:24:07 +0000266
wdenkf8062712005-01-09 23:16:25 +0000267 if(flash_probe() == 0)
268 goto bad_flash;
269#endif
wdenkc6097192002-11-03 00:24:07 +0000270 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
271 gd->env_addr = (ulong)&(env_ptr->data);
272 gd->env_valid = 1;
wdenkf8062712005-01-09 23:16:25 +0000273 return(0);
wdenkc6097192002-11-03 00:24:07 +0000274 }
wdenkf8062712005-01-09 23:16:25 +0000275#ifdef CONFIG_OMAP2420H4
276bad_flash:
277#endif
278 gd->env_addr = (ulong)&default_environment[0];
279 gd->env_valid = 0;
wdenkc6097192002-11-03 00:24:07 +0000280 return (0);
281}
282
283#ifdef CMD_SAVEENV
284
285int saveenv(void)
286{
287 int len, rc;
288 ulong end_addr;
289 ulong flash_sect_addr;
290#if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
291 ulong flash_offset;
292 uchar env_buffer[CFG_ENV_SECT_SIZE];
293#else
294 uchar *env_buffer = (char *)env_ptr;
295#endif /* CFG_ENV_SECT_SIZE */
296 int rcode = 0;
297
298#if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
299
300 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
301 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
302
303 debug ( "copy old content: "
304 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
305 flash_sect_addr, (ulong)flash_addr, flash_offset);
306
307 /* copy old contents to temporary buffer */
308 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
309
310 /* copy current environment to temporary buffer */
311 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
312 env_ptr,
313 CFG_ENV_SIZE);
314
315 len = CFG_ENV_SECT_SIZE;
316#else
317 flash_sect_addr = (ulong)flash_addr;
318 len = CFG_ENV_SIZE;
319#endif /* CFG_ENV_SECT_SIZE */
320
321#ifndef CONFIG_INFERNO
322 end_addr = flash_sect_addr + len - 1;
323#else
324 /* this is the last sector, and the size is hardcoded here */
325 /* otherwise we will get stack problems on loading 128 KB environment */
326 end_addr = flash_sect_addr + 0x20000 - 1;
327#endif
328
329 debug ("Protect off %08lX ... %08lX\n",
330 (ulong)flash_sect_addr, end_addr);
331
332 if (flash_sect_protect (0, flash_sect_addr, end_addr))
333 return 1;
334
335 puts ("Erasing Flash...");
336 if (flash_sect_erase (flash_sect_addr, end_addr))
337 return 1;
338
339 puts ("Writing to Flash... ");
340 rc = flash_write(env_buffer, flash_sect_addr, len);
341 if (rc != 0) {
342 flash_perror (rc);
343 rcode = 1;
344 } else {
345 puts ("done\n");
346 }
347
348 /* try to re-protect */
349 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
350 return rcode;
351}
352
353#endif /* CMD_SAVEENV */
354
355#endif /* CFG_ENV_ADDR_REDUND */
356
357void env_relocate_spec (void)
358{
359#if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
360#ifdef CFG_ENV_ADDR_REDUND
361 DECLARE_GLOBAL_DATA_PTR;
362
wdenkacd9b102004-03-14 00:59:59 +0000363 if (gd->env_addr != (ulong)&(flash_addr->data)) {
wdenkc6097192002-11-03 00:24:07 +0000364 env_t * etmp = flash_addr;
365 ulong ltmp = end_addr;
366
367 flash_addr = flash_addr_new;
368 flash_addr_new = etmp;
369
370 end_addr = end_addr_new;
371 end_addr_new = ltmp;
372 }
373
wdenkacd9b102004-03-14 00:59:59 +0000374 if (flash_addr_new->flags != OBSOLETE_FLAG &&
wdenkc6097192002-11-03 00:24:07 +0000375 crc32(0, flash_addr_new->data, ENV_SIZE) ==
wdenkacd9b102004-03-14 00:59:59 +0000376 flash_addr_new->crc) {
377 char flag = OBSOLETE_FLAG;
378
wdenkc6097192002-11-03 00:24:07 +0000379 gd->env_valid = 2;
380 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000381 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000382 (ulong)&(flash_addr_new->flags),
383 sizeof(flash_addr_new->flags));
wdenkc6097192002-11-03 00:24:07 +0000384 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
385 }
386
wdenkacd9b102004-03-14 00:59:59 +0000387 if (flash_addr->flags != ACTIVE_FLAG &&
388 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
389 char flag = ACTIVE_FLAG;
390
wdenkc6097192002-11-03 00:24:07 +0000391 gd->env_valid = 2;
392 flash_sect_protect (0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000393 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000394 (ulong)&(flash_addr->flags),
395 sizeof(flash_addr->flags));
wdenkc6097192002-11-03 00:24:07 +0000396 flash_sect_protect (1, (ulong)flash_addr, end_addr);
397 }
398
399 if (gd->env_valid == 2)
400 puts ("*** Warning - some problems detected "
401 "reading environment; recovered successfully\n\n");
402#endif /* CFG_ENV_ADDR_REDUND */
403 memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
404#endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
405}
406
wdenk4a5c8a72003-03-06 00:02:04 +0000407#endif /* CFG_ENV_IS_IN_FLASH */