blob: eccfb62a3beb9fe7a24713830ab0d6a06cf0aecc [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
Wolfgang Denk6405a152006-03-31 18:32:53 +020038DECLARE_GLOBAL_DATA_PTR;
39
Jon Loeliger052fc842007-07-08 18:10:08 -050040#if defined(CONFIG_CMD_ENV) && defined(CONFIG_CMD_FLASH)
wdenkc6097192002-11-03 00:24:07 +000041#define CMD_SAVEENV
42#elif defined(CFG_ENV_ADDR_REDUND)
Jon Loeligerd704d912007-07-10 11:02:44 -050043#error Cannot use CFG_ENV_ADDR_REDUND without CONFIG_CMD_ENV & CONFIG_CMD_FLASH
wdenkc6097192002-11-03 00:24:07 +000044#endif
45
wdenkc6097192002-11-03 00:24:07 +000046#if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND < CFG_ENV_SIZE)
47#error CFG_ENV_SIZE_REDUND should not be less then CFG_ENV_SIZE
48#endif
49
50#ifdef CONFIG_INFERNO
51# ifdef CFG_ENV_ADDR_REDUND
52#error CFG_ENV_ADDR_REDUND is not implemented for CONFIG_INFERNO
53# endif
54#endif
55
56char * env_name_spec = "Flash";
57
58#ifdef ENV_IS_EMBEDDED
59
60extern uchar environment[];
61env_t *env_ptr = (env_t *)(&environment[0]);
62
63#ifdef CMD_SAVEENV
64/* static env_t *flash_addr = (env_t *)(&environment[0]);-broken on ARM-wd-*/
65static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
66#endif
67
68#else /* ! ENV_IS_EMBEDDED */
69
70env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
71#ifdef CMD_SAVEENV
72static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
73#endif
74
75#endif /* ENV_IS_EMBEDDED */
76
77#ifdef CFG_ENV_ADDR_REDUND
78static env_t *flash_addr_new = (env_t *)CFG_ENV_ADDR_REDUND;
79
wdenkcc1e2562003-03-06 13:39:27 +000080/* CFG_ENV_ADDR is supposed to be on sector boundary */
81static ulong end_addr = CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1;
82static ulong end_addr_new = CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1;
wdenkc6097192002-11-03 00:24:07 +000083
wdenkacd9b102004-03-14 00:59:59 +000084#define ACTIVE_FLAG 1
85#define OBSOLETE_FLAG 0
86#endif /* CFG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000087
88extern uchar default_environment[];
89extern int default_environment_size;
90
91
92uchar env_get_char_spec (int index)
93{
wdenkc6097192002-11-03 00:24:07 +000094 return ( *((uchar *)(gd->env_addr + index)) );
95}
96
97#ifdef CFG_ENV_ADDR_REDUND
98
99int env_init(void)
100{
wdenkf8062712005-01-09 23:16:25 +0000101 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +0000102
103 uchar flag1 = flash_addr->flags;
104 uchar flag2 = flash_addr_new->flags;
105
106 ulong addr_default = (ulong)&default_environment[0];
107 ulong addr1 = (ulong)&(flash_addr->data);
108 ulong addr2 = (ulong)&(flash_addr_new->data);
109
wdenkf8062712005-01-09 23:16:25 +0000110 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
111 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
112
wdenkacd9b102004-03-14 00:59:59 +0000113 if (crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000114 gd->env_addr = addr1;
115 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000116 } else if (! crc1_ok && crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000117 gd->env_addr = addr2;
118 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000119 } else if (! crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000120 gd->env_addr = addr_default;
121 gd->env_valid = 0;
wdenkacd9b102004-03-14 00:59:59 +0000122 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000123 gd->env_addr = addr1;
124 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000125 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000126 gd->env_addr = addr2;
127 gd->env_valid = 1;
wdenkacd9b102004-03-14 00:59:59 +0000128 } else if (flag1 == flag2) {
wdenkc6097192002-11-03 00:24:07 +0000129 gd->env_addr = addr1;
130 gd->env_valid = 2;
wdenkacd9b102004-03-14 00:59:59 +0000131 } else if (flag1 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000132 gd->env_addr = addr1;
133 gd->env_valid = 2;
wdenkacd9b102004-03-14 00:59:59 +0000134 } else if (flag2 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000135 gd->env_addr = addr2;
136 gd->env_valid = 2;
137 }
138
139 return (0);
140}
141
142#ifdef CMD_SAVEENV
143int saveenv(void)
144{
wdenkd730fbc2003-03-06 14:23:06 +0000145 char *saved_data = NULL;
wdenkc6097192002-11-03 00:24:07 +0000146 int rc = 1;
wdenkacd9b102004-03-14 00:59:59 +0000147 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
wdenkd730fbc2003-03-06 14:23:06 +0000148#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
wdenkcc1e2562003-03-06 13:39:27 +0000149 ulong up_data = 0;
wdenkd730fbc2003-03-06 14:23:06 +0000150#endif
wdenkc6097192002-11-03 00:24:07 +0000151
152 debug ("Protect off %08lX ... %08lX\n",
153 (ulong)flash_addr, end_addr);
154
155 if (flash_sect_protect (0, (ulong)flash_addr, end_addr)) {
156 goto Done;
157 }
158
159 debug ("Protect off %08lX ... %08lX\n",
160 (ulong)flash_addr_new, end_addr_new);
161
162 if (flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new)) {
163 goto Done;
164 }
165
wdenkcc1e2562003-03-06 13:39:27 +0000166#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
167 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CFG_ENV_SIZE));
168 debug ("Data to save 0x%x\n", up_data);
169 if (up_data) {
170 if ((saved_data = malloc(up_data)) == NULL) {
wdenk57b2d802003-06-27 21:31:46 +0000171 printf("Unable to save the rest of sector (%ld)\n",
wdenkcc1e2562003-03-06 13:39:27 +0000172 up_data);
173 goto Done;
174 }
wdenk57b2d802003-06-27 21:31:46 +0000175 memcpy(saved_data,
wdenkcc1e2562003-03-06 13:39:27 +0000176 (void *)((long)flash_addr_new + CFG_ENV_SIZE), up_data);
wdenk57b2d802003-06-27 21:31:46 +0000177 debug ("Data (start 0x%x, len 0x%x) saved at 0x%x\n",
178 (long)flash_addr_new + CFG_ENV_SIZE,
wdenkcc1e2562003-03-06 13:39:27 +0000179 up_data, saved_data);
180 }
181#endif
wdenkc6097192002-11-03 00:24:07 +0000182 puts ("Erasing Flash...");
183 debug (" %08lX ... %08lX ...",
184 (ulong)flash_addr_new, end_addr_new);
185
186 if (flash_sect_erase ((ulong)flash_addr_new, end_addr_new)) {
187 goto Done;
188 }
189
190 puts ("Writing to Flash... ");
191 debug (" %08lX ... %08lX ...",
192 (ulong)&(flash_addr_new->data),
193 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200194 if ((rc = flash_write((char *)env_ptr->data,
wdenk57b2d802003-06-27 21:31:46 +0000195 (ulong)&(flash_addr_new->data),
wdenkf602aa02004-03-13 23:29:43 +0000196 sizeof(env_ptr->data))) ||
wdenkf602aa02004-03-13 23:29:43 +0000197 (rc = flash_write((char *)&(env_ptr->crc),
wdenk57b2d802003-06-27 21:31:46 +0000198 (ulong)&(flash_addr_new->crc),
wdenkf602aa02004-03-13 23:29:43 +0000199 sizeof(env_ptr->crc))) ||
wdenkacd9b102004-03-14 00:59:59 +0000200 (rc = flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000201 (ulong)&(flash_addr->flags),
wdenkf602aa02004-03-13 23:29:43 +0000202 sizeof(flash_addr->flags))) ||
wdenkacd9b102004-03-14 00:59:59 +0000203 (rc = flash_write(&new_flag,
wdenk57b2d802003-06-27 21:31:46 +0000204 (ulong)&(flash_addr_new->flags),
wdenkf602aa02004-03-13 23:29:43 +0000205 sizeof(flash_addr_new->flags))))
wdenkc6097192002-11-03 00:24:07 +0000206 {
207 flash_perror (rc);
208 goto Done;
209 }
210 puts ("done\n");
211
wdenkcc1e2562003-03-06 13:39:27 +0000212#if CFG_ENV_SECT_SIZE > CFG_ENV_SIZE
213 if (up_data) { /* restore the rest of sector */
214 debug ("Restoring the rest of data to 0x%x len 0x%x\n",
215 (long)flash_addr_new + CFG_ENV_SIZE, up_data);
wdenk57b2d802003-06-27 21:31:46 +0000216 if (flash_write(saved_data,
217 (long)flash_addr_new + CFG_ENV_SIZE,
wdenkcc1e2562003-03-06 13:39:27 +0000218 up_data)) {
219 flash_perror(rc);
220 goto Done;
221 }
222 }
223#endif
wdenkc6097192002-11-03 00:24:07 +0000224 {
225 env_t * etmp = flash_addr;
226 ulong ltmp = end_addr;
227
228 flash_addr = flash_addr_new;
229 flash_addr_new = etmp;
230
231 end_addr = end_addr_new;
232 end_addr_new = ltmp;
233 }
234
235 rc = 0;
236Done:
237
wdenkcc1e2562003-03-06 13:39:27 +0000238 if (saved_data)
239 free (saved_data);
wdenkc6097192002-11-03 00:24:07 +0000240 /* try to re-protect */
241 (void) flash_sect_protect (1, (ulong)flash_addr, end_addr);
242 (void) flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
243
244 return rc;
245}
246#endif /* CMD_SAVEENV */
247
248#else /* ! CFG_ENV_ADDR_REDUND */
249
250int env_init(void)
251{
wdenkc6097192002-11-03 00:24:07 +0000252 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
253 gd->env_addr = (ulong)&(env_ptr->data);
254 gd->env_valid = 1;
wdenkf8062712005-01-09 23:16:25 +0000255 return(0);
wdenkc6097192002-11-03 00:24:07 +0000256 }
Dirk Behmeafa1dd52007-08-20 07:09:05 +0200257
wdenkf8062712005-01-09 23:16:25 +0000258 gd->env_addr = (ulong)&default_environment[0];
259 gd->env_valid = 0;
wdenkc6097192002-11-03 00:24:07 +0000260 return (0);
261}
262
263#ifdef CMD_SAVEENV
264
265int saveenv(void)
266{
267 int len, rc;
268 ulong end_addr;
269 ulong flash_sect_addr;
270#if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
271 ulong flash_offset;
272 uchar env_buffer[CFG_ENV_SECT_SIZE];
273#else
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200274 uchar *env_buffer = (uchar *)env_ptr;
wdenkc6097192002-11-03 00:24:07 +0000275#endif /* CFG_ENV_SECT_SIZE */
276 int rcode = 0;
277
278#if defined(CFG_ENV_SECT_SIZE) && (CFG_ENV_SECT_SIZE > CFG_ENV_SIZE)
279
280 flash_offset = ((ulong)flash_addr) & (CFG_ENV_SECT_SIZE-1);
281 flash_sect_addr = ((ulong)flash_addr) & ~(CFG_ENV_SECT_SIZE-1);
282
283 debug ( "copy old content: "
284 "sect_addr: %08lX env_addr: %08lX offset: %08lX\n",
285 flash_sect_addr, (ulong)flash_addr, flash_offset);
286
287 /* copy old contents to temporary buffer */
288 memcpy (env_buffer, (void *)flash_sect_addr, CFG_ENV_SECT_SIZE);
289
290 /* copy current environment to temporary buffer */
291 memcpy ((uchar *)((unsigned long)env_buffer + flash_offset),
292 env_ptr,
293 CFG_ENV_SIZE);
294
295 len = CFG_ENV_SECT_SIZE;
296#else
297 flash_sect_addr = (ulong)flash_addr;
298 len = CFG_ENV_SIZE;
299#endif /* CFG_ENV_SECT_SIZE */
300
301#ifndef CONFIG_INFERNO
302 end_addr = flash_sect_addr + len - 1;
303#else
304 /* this is the last sector, and the size is hardcoded here */
305 /* otherwise we will get stack problems on loading 128 KB environment */
306 end_addr = flash_sect_addr + 0x20000 - 1;
307#endif
308
309 debug ("Protect off %08lX ... %08lX\n",
310 (ulong)flash_sect_addr, end_addr);
311
312 if (flash_sect_protect (0, flash_sect_addr, end_addr))
313 return 1;
314
315 puts ("Erasing Flash...");
316 if (flash_sect_erase (flash_sect_addr, end_addr))
317 return 1;
318
319 puts ("Writing to Flash... ");
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200320 rc = flash_write((char *)env_buffer, flash_sect_addr, len);
wdenkc6097192002-11-03 00:24:07 +0000321 if (rc != 0) {
322 flash_perror (rc);
323 rcode = 1;
324 } else {
325 puts ("done\n");
326 }
327
328 /* try to re-protect */
329 (void) flash_sect_protect (1, flash_sect_addr, end_addr);
330 return rcode;
331}
332
333#endif /* CMD_SAVEENV */
334
335#endif /* CFG_ENV_ADDR_REDUND */
336
337void env_relocate_spec (void)
338{
339#if !defined(ENV_IS_EMBEDDED) || defined(CFG_ENV_ADDR_REDUND)
340#ifdef CFG_ENV_ADDR_REDUND
wdenkacd9b102004-03-14 00:59:59 +0000341 if (gd->env_addr != (ulong)&(flash_addr->data)) {
wdenkc6097192002-11-03 00:24:07 +0000342 env_t * etmp = flash_addr;
343 ulong ltmp = end_addr;
344
345 flash_addr = flash_addr_new;
346 flash_addr_new = etmp;
347
348 end_addr = end_addr_new;
349 end_addr_new = ltmp;
350 }
351
wdenkacd9b102004-03-14 00:59:59 +0000352 if (flash_addr_new->flags != OBSOLETE_FLAG &&
wdenkc6097192002-11-03 00:24:07 +0000353 crc32(0, flash_addr_new->data, ENV_SIZE) ==
wdenkacd9b102004-03-14 00:59:59 +0000354 flash_addr_new->crc) {
355 char flag = OBSOLETE_FLAG;
356
wdenkc6097192002-11-03 00:24:07 +0000357 gd->env_valid = 2;
358 flash_sect_protect (0, (ulong)flash_addr_new, end_addr_new);
wdenkacd9b102004-03-14 00:59:59 +0000359 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000360 (ulong)&(flash_addr_new->flags),
361 sizeof(flash_addr_new->flags));
wdenkc6097192002-11-03 00:24:07 +0000362 flash_sect_protect (1, (ulong)flash_addr_new, end_addr_new);
363 }
364
wdenkacd9b102004-03-14 00:59:59 +0000365 if (flash_addr->flags != ACTIVE_FLAG &&
366 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
367 char flag = ACTIVE_FLAG;
368
wdenkc6097192002-11-03 00:24:07 +0000369 gd->env_valid = 2;
370 flash_sect_protect (0, (ulong)flash_addr, end_addr);
wdenkacd9b102004-03-14 00:59:59 +0000371 flash_write(&flag,
wdenk57b2d802003-06-27 21:31:46 +0000372 (ulong)&(flash_addr->flags),
373 sizeof(flash_addr->flags));
wdenkc6097192002-11-03 00:24:07 +0000374 flash_sect_protect (1, (ulong)flash_addr, end_addr);
375 }
376
377 if (gd->env_valid == 2)
378 puts ("*** Warning - some problems detected "
379 "reading environment; recovered successfully\n\n");
380#endif /* CFG_ENV_ADDR_REDUND */
381 memcpy (env_ptr, (void*)flash_addr, CFG_ENV_SIZE);
382#endif /* ! ENV_IS_EMBEDDED || CFG_ENV_ADDR_REDUND */
383}
384
wdenk4a5c8a72003-03-06 00:02:04 +0000385#endif /* CFG_ENV_IS_IN_FLASH */