blob: 1da78b7102e4c13b3589abb9a18e04d71bd4a927 [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
Wolfgang Denkea882ba2010-06-20 23:33:59 +020085#undef debug
86#define debug printf
87
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020088#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc6097192002-11-03 00:24:07 +000089
90int env_init(void)
91{
wdenk8ed96042005-01-09 23:16:25 +000092 int crc1_ok = 0, crc2_ok = 0;
wdenkc6097192002-11-03 00:24:07 +000093
94 uchar flag1 = flash_addr->flags;
95 uchar flag2 = flash_addr_new->flags;
96
97 ulong addr_default = (ulong)&default_environment[0];
98 ulong addr1 = (ulong)&(flash_addr->data);
99 ulong addr2 = (ulong)&(flash_addr_new->data);
100
wdenk8ed96042005-01-09 23:16:25 +0000101 crc1_ok = (crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc);
102 crc2_ok = (crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc);
103
wdenkc3f9d492004-03-14 00:59:59 +0000104 if (crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000105 gd->env_addr = addr1;
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 = addr2;
109 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000110 } else if (! crc1_ok && ! crc2_ok) {
wdenkc6097192002-11-03 00:24:07 +0000111 gd->env_addr = addr_default;
112 gd->env_valid = 0;
wdenkc3f9d492004-03-14 00:59:59 +0000113 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000114 gd->env_addr = addr1;
115 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000116 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
wdenkc6097192002-11-03 00:24:07 +0000117 gd->env_addr = addr2;
118 gd->env_valid = 1;
wdenkc3f9d492004-03-14 00:59:59 +0000119 } else if (flag1 == flag2) {
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 (flag1 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000123 gd->env_addr = addr1;
124 gd->env_valid = 2;
wdenkc3f9d492004-03-14 00:59:59 +0000125 } else if (flag2 == 0xFF) {
wdenkc6097192002-11-03 00:24:07 +0000126 gd->env_addr = addr2;
127 gd->env_valid = 2;
128 }
129
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200130 return 0;
wdenkc6097192002-11-03 00:24:07 +0000131}
132
133#ifdef CMD_SAVEENV
134int saveenv(void)
135{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200136 env_t env_new;
137 ssize_t len;
138 char *saved_data = NULL;
139 char *res;
140 int rc = 1;
141 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200142#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200143 ulong up_data = 0;
wdenk500545c2003-03-06 14:23:06 +0000144#endif
wdenkc6097192002-11-03 00:24:07 +0000145
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200146 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000147 (ulong)flash_addr, end_addr);
148
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200149 if (flash_sect_protect(0, (ulong)flash_addr, end_addr)) {
150 goto done;
wdenkc6097192002-11-03 00:24:07 +0000151 }
152
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 debug("Protect off %08lX ... %08lX\n",
wdenkc6097192002-11-03 00:24:07 +0000154 (ulong)flash_addr_new, end_addr_new);
155
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200156 if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new)) {
157 goto done;
wdenkc6097192002-11-03 00:24:07 +0000158 }
159
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200160 res = (char *)&env_new.data;
161 len = hexport('\0', &res, ENV_SIZE);
162 if (len < 0) {
163 error("Cannot export environment: errno = %d\n", errno);
164 goto done;
165 }
166 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
167 env_new.flags = new_flag;
168
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200169#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
170 up_data = (end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200171 debug("Data to save 0x%lX\n", up_data);
wdenk47cd00f2003-03-06 13:39:27 +0000172 if (up_data) {
173 if ((saved_data = malloc(up_data)) == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +0000174 printf("Unable to save the rest of sector (%ld)\n",
wdenk47cd00f2003-03-06 13:39:27 +0000175 up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200176 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000177 }
wdenk8bde7f72003-06-27 21:31:46 +0000178 memcpy(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200179 (void *)((long)flash_addr_new + CONFIG_ENV_SIZE), up_data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200180 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
181 (long)flash_addr_new + CONFIG_ENV_SIZE,
182 up_data, saved_data);
wdenk47cd00f2003-03-06 13:39:27 +0000183 }
184#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185 puts("Erasing Flash...");
186 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000187 (ulong)flash_addr_new, end_addr_new);
188
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200189 if (flash_sect_erase((ulong)flash_addr_new, end_addr_new)) {
190 goto done;
wdenkc6097192002-11-03 00:24:07 +0000191 }
192
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200193 puts("Writing to Flash... ");
194 debug(" %08lX ... %08lX ...",
wdenkc6097192002-11-03 00:24:07 +0000195 (ulong)&(flash_addr_new->data),
196 sizeof(env_ptr->data)+(ulong)&(flash_addr_new->data));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197 if ((rc = flash_write((char *)&env_new,
198 (ulong)flash_addr_new,
199 sizeof(env_new))) ||
wdenkc3f9d492004-03-14 00:59:59 +0000200 (rc = flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000201 (ulong)&(flash_addr->flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200202 sizeof(flash_addr->flags))) ) {
203 flash_perror(rc);
204 goto done;
wdenkc6097192002-11-03 00:24:07 +0000205 }
wdenkc6097192002-11-03 00:24:07 +0000206
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200207#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
wdenk47cd00f2003-03-06 13:39:27 +0000208 if (up_data) { /* restore the rest of sector */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200209 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
210 (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
wdenk8bde7f72003-06-27 21:31:46 +0000211 if (flash_write(saved_data,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200212 (long)flash_addr_new + CONFIG_ENV_SIZE,
wdenk47cd00f2003-03-06 13:39:27 +0000213 up_data)) {
214 flash_perror(rc);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200215 goto done;
wdenk47cd00f2003-03-06 13:39:27 +0000216 }
217 }
218#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200219 puts("done\n");
220
wdenkc6097192002-11-03 00:24:07 +0000221 {
222 env_t * etmp = flash_addr;
223 ulong ltmp = end_addr;
224
225 flash_addr = flash_addr_new;
226 flash_addr_new = etmp;
227
228 end_addr = end_addr_new;
229 end_addr_new = ltmp;
230 }
231
232 rc = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200233done:
wdenk47cd00f2003-03-06 13:39:27 +0000234 if (saved_data)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200235 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000236 /* try to re-protect */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200237 (void) flash_sect_protect(1, (ulong)flash_addr, end_addr);
238 (void) flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000239
240 return rc;
241}
242#endif /* CMD_SAVEENV */
243
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200244#else /* ! CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000245
246int env_init(void)
247{
wdenkc6097192002-11-03 00:24:07 +0000248 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
249 gd->env_addr = (ulong)&(env_ptr->data);
250 gd->env_valid = 1;
wdenk8ed96042005-01-09 23:16:25 +0000251 return(0);
wdenkc6097192002-11-03 00:24:07 +0000252 }
Dirk Behme9bb8b202007-08-20 07:09:05 +0200253
wdenk8ed96042005-01-09 23:16:25 +0000254 gd->env_addr = (ulong)&default_environment[0];
255 gd->env_valid = 0;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256 return 0;
wdenkc6097192002-11-03 00:24:07 +0000257}
258
259#ifdef CMD_SAVEENV
260
261int saveenv(void)
262{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200263 env_t env_new;
264 ssize_t len;
265 int rc = 1;
266 char *res;
267 char *saved_data = NULL;
268#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
269 ulong up_data = 0;
wdenkc6097192002-11-03 00:24:07 +0000270
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200271 up_data = (end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE));
272 debug("Data to save 0x%lx\n", up_data);
273 if (up_data) {
274 if ((saved_data = malloc(up_data)) == NULL) {
275 printf("Unable to save the rest of sector (%ld)\n",
276 up_data);
277 goto done;
278 }
279 memcpy(saved_data,
280 (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
281 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
282 (ulong)flash_addr + CONFIG_ENV_SIZE,
283 up_data,
284 (ulong)saved_data);
wdenkc6097192002-11-03 00:24:07 +0000285 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200286#endif /* CONFIG_ENV_SECT_SIZE */
wdenkc6097192002-11-03 00:24:07 +0000287
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200288 debug("Protect off %08lX ... %08lX\n",
289 (ulong)flash_addr, end_addr);
290
291 if (flash_sect_protect(0, (long)flash_addr, end_addr))
292 goto done;
293
294 res = (char *)&env_new.data;
295 len = hexport('\0', &res, ENV_SIZE);
296 if (len < 0) {
297 error("Cannot export environment: errno = %d\n", errno);
298 goto done;
299 }
300 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
301
302 puts("Erasing Flash...");
303 if (flash_sect_erase((long)flash_addr, end_addr))
304 goto done;
305
306 puts("Writing to Flash... ");
307 rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
308 if (rc != 0) {
309 flash_perror(rc);
310 goto done;
311 }
312#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
313 if (up_data) { /* restore the rest of sector */
314 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
315 (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
316 if (flash_write(saved_data,
317 (long)flash_addr + CONFIG_ENV_SIZE,
318 up_data)) {
319 flash_perror(rc);
320 goto done;
321 }
322 }
323#endif
324 puts("done\n");
325 rc = 0;
326done:
327 if (saved_data)
328 free(saved_data);
wdenkc6097192002-11-03 00:24:07 +0000329 /* try to re-protect */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200330 (void) flash_sect_protect(1, (long)flash_addr, end_addr);
331 return rc;
wdenkc6097192002-11-03 00:24:07 +0000332}
333
334#endif /* CMD_SAVEENV */
335
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200336#endif /* CONFIG_ENV_ADDR_REDUND */
wdenkc6097192002-11-03 00:24:07 +0000337
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200338void env_relocate_spec(void)
wdenkc6097192002-11-03 00:24:07 +0000339{
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200340#ifdef CONFIG_ENV_ADDR_REDUND
wdenkc3f9d492004-03-14 00:59:59 +0000341 if (gd->env_addr != (ulong)&(flash_addr->data)) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200342 env_t *etmp = flash_addr;
wdenkc6097192002-11-03 00:24:07 +0000343 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
wdenkc3f9d492004-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) ==
wdenkc3f9d492004-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;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200358 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
wdenkc3f9d492004-03-14 00:59:59 +0000359 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000360 (ulong)&(flash_addr_new->flags),
361 sizeof(flash_addr_new->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200362 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
wdenkc6097192002-11-03 00:24:07 +0000363 }
364
wdenkc3f9d492004-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;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200370 flash_sect_protect(0, (ulong)flash_addr, end_addr);
wdenkc3f9d492004-03-14 00:59:59 +0000371 flash_write(&flag,
wdenk8bde7f72003-06-27 21:31:46 +0000372 (ulong)&(flash_addr->flags),
373 sizeof(flash_addr->flags));
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200374 flash_sect_protect(1, (ulong)flash_addr, end_addr);
wdenkc6097192002-11-03 00:24:07 +0000375 }
376
377 if (gd->env_valid == 2)
378 puts ("*** Warning - some problems detected "
379 "reading environment; recovered successfully\n\n");
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200380#endif /* CONFIG_ENV_ADDR_REDUND */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200381
382 env_import((char *)flash_addr, 1);
wdenkc6097192002-11-03 00:24:07 +0000383}