blob: 54c0bfec7624192276886dbfa0c886fc13052a02 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenkc6097192002-11-03 00:24:07 +00003 * 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>
wdenkc6097192002-11-03 00:24:07 +000030#include <command.h>
31#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000032#include <linux/stddef.h>
wdenk47cd00f2003-03-06 13:39:27 +000033#include <malloc.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020034#include <search.h>
35#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000036
Wolfgang Denkd87080b2006-03-31 18:32:53 +020037DECLARE_GLOBAL_DATA_PTR;
38
Mike Frysingerbdab39d2009-01-28 19:08:14 -050039#if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
wdenkc6097192002-11-03 00:24:07 +000040#define CMD_SAVEENV
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020041#elif defined(CONFIG_ENV_ADDR_REDUND)
Mike Frysingerbdab39d2009-01-28 19:08:14 -050042#error Cannot use CONFIG_ENV_ADDR_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
wdenkc6097192002-11-03 00:24:07 +000043#endif
44
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020045#if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND < CONFIG_ENV_SIZE)
46#error CONFIG_ENV_SIZE_REDUND should not be less then CONFIG_ENV_SIZE
wdenkc6097192002-11-03 00:24:07 +000047#endif
48
wdenkc6097192002-11-03 00:24:07 +000049char * env_name_spec = "Flash";
50
51#ifdef ENV_IS_EMBEDDED
52
53extern uchar environment[];
54env_t *env_ptr = (env_t *)(&environment[0]);
55
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020056static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000057
58#else /* ! ENV_IS_EMBEDDED */
59
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020060env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020061static env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000062
63#endif /* ENV_IS_EMBEDDED */
64
Wolfgang Denkea882ba2010-06-20 23:33:59 +020065#if defined(CMD_SAVEENV) || defined(CONFIG_ENV_ADDR_REDUND)
66/* CONFIG_ENV_ADDR is supposed to be on sector boundary */
67static ulong end_addr = CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
68#endif
69
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020070#ifdef CONFIG_ENV_ADDR_REDUND
71static env_t *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
wdenkc6097192002-11-03 00:24:07 +000072
Wolfgang Denkea882ba2010-06-20 23:33:59 +020073/* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020074static ulong end_addr_new = CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020075#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +000076
77extern uchar default_environment[];
wdenkc6097192002-11-03 00:24:07 +000078
79
Wolfgang Denkea882ba2010-06-20 23:33:59 +020080uchar env_get_char_spec(int index)
wdenkc6097192002-11-03 00:24:07 +000081{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020082 return (*((uchar *)(gd->env_addr + index)));
wdenkc6097192002-11-03 00:24:07 +000083}
84
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020085#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc6097192002-11-03 00:24:07 +000086
87int env_init(void)
88{
wdenk8ed96042005-01-09 23:16:25 +000089 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000090
91 uchar flag1 = flash_addr->flags;
92 uchar flag2 = flash_addr_new->flags;
93
94 ulong addr_default = (ulong)&default_environment[0];
95 ulong addr1 = (ulong)&(flash_addr->data);
96 ulong addr2 = (ulong)&(flash_addr_new->data);
97
wdenk8ed96042005-01-09 23:16:25 +000098 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
99 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
100
wdenkc3f9d492004-03-14 00:59:59 +0000101 if (crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000102 gd->env_addr = addr1;
103 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000104 } else if (! crc1_ok && crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000105 gd->env_addr = addr2;
106 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000107 } else if (! crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000108 gd->env_addr = addr_default;
109 gd->env_valid = 0;
wdenkc3f9d492004-03-14 00:59:59 +0000110 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000111 gd->env_addr = addr1;
112 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000113 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000114 gd->env_addr = addr2;
115 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000116 } else if (flag1 == flag2) {
wdenkc6097192002-11-03 00:24:07 +0000117 gd->env_addr = addr1;
118 gd->env_valid = 2;
wdenkc3f9d492004-03-14 00:59:59 +0000119 } else if (flag1 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000120 gd->env_addr = addr1;
121 gd->env_valid = 2;
wdenkc3f9d492004-03-14 00:59:59 +0000122 } else if (flag2 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000123 gd->env_addr = addr2;
124 gd->env_valid = 2;
125 }
126
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200127 return 0;
wdenkc6097192002-11-03 00:24:07 +0000128}
129
130#ifdef CMD_SAVEENV
131int saveenv(void)
132{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200133 env_t env_new;
134 ssize_t len;
135 char *saved_data = NULL;
136 char *res;
137 int rc = 1;
138 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200139#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200140 ulong up_data = 0;
wdenk500545c2003-03-06 14:23:06 +0000141#endif
wdenkc6097192002-11-03 00:24:07 +0000142
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200143 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000144 (ulong)flash_addr, end_addr);
145
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200146 if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
147 goto done;
wdenkc6097192002-11-03 00:24:07 +0000148 }
149
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200150 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000151 (ulong)flash_addr_new, end_addr_new);
152
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
154 goto done;
wdenkc6097192002-11-03 00:24:07 +0000155 }
156
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200157 res = (char *)&env_new.data;
158 len = hexport('\0', &res, ENV_SIZE);
159 if (len < 0) {
160 error("Cannot export environment: errno = %d\n", errno);
161 goto done;
162 }
163 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
164 env_new.flags = new_flag;
165
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200166#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
167 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200168 debug("Data to save 0x%lX\n", up_data);
wdenk47cd00f2003-03-06 13:39:27 +0000169 if (up_data) {
170 if ((saved_data = malloc(up_data)) == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000171 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000172 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200173 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000174 }
wdenk8bde7f72003-06-27 21:31:46 +0000175 memcpy(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200176 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200177 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
178 (long)flash_addr_new + CONFIG_ENV_SIZE,
179 up_data, saved_data);
wdenk47cd00f2003-03-06 13:39:27 +0000180 }
181#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200182 puts("Erasing Flash...");
183 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000184 (ulong)flash_addr_new, end_addr_new);
185
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200186 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
187 goto done;
wdenkc6097192002-11-03 00:24:07 +0000188 }
189
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200190 puts("Writing to Flash... ");
191 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000192 (ulong)&(flash_addr_new->data),
193 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200194 if ((rc = flash_write((char *)&env_new,
195 (ulong)flash_addr_new,
196 sizeof(env_new))) ||
wdenkc3f9d492004-03-14 00:59:59 +0000197 (rc = flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000198 (ulong)&(flash_addr->flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200199 sizeof(flash_addr->flags))) ) {
200 flash_perror(rc);
201 goto done;
wdenkc6097192002-11-03 00:24:07 +0000202 }
wdenkc6097192002-11-03 00:24:07 +0000203
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200204#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenk47cd00f2003-03-06 13:39:27 +0000205 if (up_data) { /* restore the rest of sector */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200206 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
207 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk8bde7f72003-06-27 21:31:46 +0000208 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200209 (long)flash_addr_new + CONFIG_ENV_SIZE,
wdenk47cd00f2003-03-06 13:39:27 +0000210 up_data)) {
211 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200212 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000213 }
214 }
215#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200216 puts("done\n");
217
wdenkc6097192002-11-03 00:24:07 +0000218 {
219 env_t * etmp = flash_addr;
220 ulong ltmp = end_addr;
221
222 flash_addr = flash_addr_new;
223 flash_addr_new = etmp;
224
225 end_addr = end_addr_new;
226 end_addr_new = ltmp;
227 }
228
229 rc = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200230done:
wdenk47cd00f2003-03-06 13:39:27 +0000231 if (saved_data)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200232 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000233 /* try to re-protect */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200234 (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
235 (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000236
237 return rc;
238}
239#endif /* CMD_SAVEENV */
240
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200241#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000242
243int env_init(void)
244{
wdenkc6097192002-11-03 00:24:07 +0000245 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
246 gd->env_addr = (ulong)&(env_ptr->data);
247 gd->env_valid = 1;
wdenk8ed96042005-01-09 23:16:25 +0000248 return(0);
wdenkc6097192002-11-03 00:24:07 +0000249 }
Dirk Behme9bb8b202007-08-20 07:09:05 +0200250
wdenk8ed96042005-01-09 23:16:25 +0000251 gd->env_addr = (ulong)&default_environment[0];
252 gd->env_valid = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200253 return 0;
wdenkc6097192002-11-03 00:24:07 +0000254}
255
256#ifdef CMD_SAVEENV
257
258int saveenv(void)
259{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200260 env_t env_new;
261 ssize_t len;
262 int rc = 1;
263 char *res;
264 char *saved_data = NULL;
265#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
266 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000267
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200268 up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
269 debug("Data to save 0x%lx\n", up_data);
270 if (up_data) {
271 if ((saved_data = malloc(up_data)) == NULL) {
272 printf("Unable to save the rest of sector (%ld)\n",
273 up_data);
274 goto done;
275 }
276 memcpy(saved_data,
277 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
278 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
279 (ulong)flash_addr + CONFIG_ENV_SIZE,
280 up_data,
281 (ulong)saved_data);
wdenkc6097192002-11-03 00:24:07 +0000282 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200283#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000284
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200285 debug("Protect off %08lX ... %08lX\n",
286 (ulong)flash_addr, end_addr);
287
288 if (flash_sect_protect(0, (long)flash_addr, end_addr))
289 goto done;
290
291 res = (char *)&env_new.data;
292 len = hexport('\0', &res, ENV_SIZE);
293 if (len < 0) {
294 error("Cannot export environment: errno = %d\n", errno);
295 goto done;
296 }
297 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
298
299 puts("Erasing Flash...");
300 if (flash_sect_erase((long)flash_addr, end_addr))
301 goto done;
302
303 puts("Writing to Flash... ");
304 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
305 if (rc != 0) {
306 flash_perror(rc);
307 goto done;
308 }
309#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
310 if (up_data) { /* restore the rest of sector */
311 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
312 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
313 if (flash_write(saved_data,
314 (long)flash_addr + CONFIG_ENV_SIZE,
315 up_data)) {
316 flash_perror(rc);
317 goto done;
318 }
319 }
320#endif
321 puts("done\n");
322 rc = 0;
323done:
324 if (saved_data)
325 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000326 /* try to re-protect */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200327 (void) flash_sect_protect(1, (long)flash_addr, end_addr);
328 return rc;
wdenkc6097192002-11-03 00:24:07 +0000329}
330
331#endif /* CMD_SAVEENV */
332
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200333#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000334
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200335void env_relocate_spec(void)
wdenkc6097192002-11-03 00:24:07 +0000336{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200337#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc3f9d492004-03-14 00:59:59 +0000338 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200339 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000340 ulong ltmp = end_addr;
341
342 flash_addr = flash_addr_new;
343 flash_addr_new = etmp;
344
345 end_addr = end_addr_new;
346 end_addr_new = ltmp;
347 }
348
wdenkc3f9d492004-03-14 00:59:59 +0000349 if (flash_addr_new->flags != OBSOLETE_FLAG &&
wdenkc6097192002-11-03 00:24:07 +0000350 crc32(0, flash_addr_new->data, ENV_SIZE) ==
wdenkc3f9d492004-03-14 00:59:59 +0000351 flash_addr_new->crc) {
352 char flag = OBSOLETE_FLAG;
353
wdenkc6097192002-11-03 00:24:07 +0000354 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200355 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkc3f9d492004-03-14 00:59:59 +0000356 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000357 (ulong)&(flash_addr_new->flags),
358 sizeof(flash_addr_new->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200359 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000360 }
361
wdenkc3f9d492004-03-14 00:59:59 +0000362 if (flash_addr->flags != ACTIVE_FLAG &&
363 (flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
364 char flag = ACTIVE_FLAG;
365
wdenkc6097192002-11-03 00:24:07 +0000366 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200367 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkc3f9d492004-03-14 00:59:59 +0000368 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000369 (ulong)&(flash_addr->flags),
370 sizeof(flash_addr->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200371 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000372 }
373
374 if (gd->env_valid == 2)
375 puts ("*** Warning - some problems detected "
376 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200377#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200378
379 env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000380}