blob: a2ea9c41406842d7acbc404aef25538f19d75df7 [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>
wdenk47cd00f2003-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
wdenk47cd00f2003-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
wdenkc3f9d492004-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;
wdenk8ed96042005-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
wdenk8ed96042005-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
wdenkc3f9d492004-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;
wdenkc3f9d492004-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;
wdenkc3f9d492004-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;
wdenkc3f9d492004-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;
wdenkc3f9d492004-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;
wdenkc3f9d492004-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;
wdenkc3f9d492004-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;
wdenkc3f9d492004-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
wdenk8ed96042005-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{
wdenk47cd00f2003-03-06 13:39:27 +0000156 char *saved_data = NULL;
wdenk500545c2003-03-06 14:23:06 +0000157 int rc = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000158 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
wdenk500545c2003-03-06 14:23:06 +0000159#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
160 ulong up_data = 0;
161#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
wdenk47cd00f2003-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) {
wdenk8bde7f72003-06-27 21:31:46 +0000182 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000183 up_data);
184 goto Done;
185 }
wdenk8bde7f72003-06-27 21:31:46 +0000186 memcpy(saved_data,
wdenk47cd00f2003-03-06 13:39:27 +0000187 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
wdenk8bde7f72003-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,
wdenk47cd00f2003-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));
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200205 if ((rc = flash_write((char *)env_ptr->data,
wdenk8bde7f72003-06-27 21:31:46 +0000206 (ulong)&(flash_addr_new->data),
wdenkc40b2952004-03-13 23:29:43 +0000207 sizeof(env_ptr->data))) ||
wdenkc40b2952004-03-13 23:29:43 +0000208 (rc = flash_write((char *)&(env_ptr->crc),
wdenk8bde7f72003-06-27 21:31:46 +0000209 (ulong)&(flash_addr_new->crc),
wdenkc40b2952004-03-13 23:29:43 +0000210 sizeof(env_ptr->crc))) ||
wdenkc3f9d492004-03-14 00:59:59 +0000211 (rc = flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000212 (ulong)&(flash_addr->flags),
wdenkc40b2952004-03-13 23:29:43 +0000213 sizeof(flash_addr->flags))) ||
wdenkc3f9d492004-03-14 00:59:59 +0000214 (rc = flash_write(&new_flag,
wdenk8bde7f72003-06-27 21:31:46 +0000215 (ulong)&(flash_addr_new->flags),
wdenkc40b2952004-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
wdenk47cd00f2003-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);
wdenk8bde7f72003-06-27 21:31:46 +0000227 if (flash_write(saved_data,
228 (long)flash_addr_new + CFG_ENV_SIZE,
wdenk47cd00f2003-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
wdenk47cd00f2003-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;
wdenk8ed96042005-01-09 23:16:25 +0000264#ifdef CONFIG_OMAP2420H4
265 int flash_probe(void);
wdenkc6097192002-11-03 00:24:07 +0000266
wdenk8ed96042005-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;
wdenk8ed96042005-01-09 23:16:25 +0000273 return(0);
wdenkc6097192002-11-03 00:24:07 +0000274 }
wdenk8ed96042005-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
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200294 uchar *env_buffer = (uchar *)env_ptr;
wdenkc6097192002-11-03 00:24:07 +0000295#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... ");
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200340 rc = flash_write((char *)env_buffer, flash_sect_addr, len);
wdenkc6097192002-11-03 00:24:07 +0000341 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
wdenkc3f9d492004-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
wdenkc3f9d492004-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) ==
wdenkc3f9d492004-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);
wdenkc3f9d492004-03-14 00:59:59 +0000381 flash_write(&flag,
wdenk8bde7f72003-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
wdenkc3f9d492004-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);
wdenkc3f9d492004-03-14 00:59:59 +0000393 flash_write(&flag,
wdenk8bde7f72003-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
wdenk43d96162003-03-06 00:02:04 +0000407#endif /* CFG_ENV_IS_IN_FLASH */