]>
Commit | Line | Data |
---|---|---|
13a5695b | 1 | /* |
ea882baf WD |
2 | * (C) Copyright 2000-2010 |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
cc49cade SW |
5 | * (C) Copyright 2008 |
6 | * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com> | |
7 | * | |
13a5695b WD |
8 | * (C) Copyright 2004 |
9 | * Jian Zhang, Texas Instruments, jzhang@ti.com. | |
13a5695b WD |
10 | * |
11 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
12 | * Andreas Heppel <aheppel@sysgo.de> | |
ea882baf | 13 | * |
13a5695b WD |
14 | * See file CREDITS for list of people who contributed to this |
15 | * project. | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or | |
18 | * modify it under the terms of the GNU General Public License as | |
19 | * published by the Free Software Foundation; either version 2 of | |
20 | * the License, or (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU General Public License | |
28 | * along with this program; if not, write to the Free Software | |
29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
30 | * MA 02111-1307 USA | |
31 | */ | |
32 | ||
ea882baf | 33 | #define DEBUG |
13a5695b WD |
34 | |
35 | #include <common.h> | |
13a5695b WD |
36 | #include <command.h> |
37 | #include <environment.h> | |
38 | #include <linux/stddef.h> | |
e443c944 | 39 | #include <malloc.h> |
addb2e16 | 40 | #include <nand.h> |
ea882baf WD |
41 | #include <search.h> |
42 | #include <errno.h> | |
13a5695b | 43 | |
bdab39d3 | 44 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) |
13a5695b | 45 | #define CMD_SAVEENV |
0e8d1586 | 46 | #elif defined(CONFIG_ENV_OFFSET_REDUND) |
bdab39d3 | 47 | #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND |
13a5695b WD |
48 | #endif |
49 | ||
0e8d1586 JCPV |
50 | #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) |
51 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE | |
13a5695b WD |
52 | #endif |
53 | ||
0e8d1586 JCPV |
54 | #ifndef CONFIG_ENV_RANGE |
55 | #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE | |
cc49cade SW |
56 | #endif |
57 | ||
ea882baf | 58 | char *env_name_spec = "NAND"; |
13a5695b WD |
59 | |
60 | ||
b74ab737 | 61 | #if defined(ENV_IS_EMBEDDED) |
994bc671 | 62 | env_t *env_ptr = &environment; |
b74ab737 GL |
63 | #elif defined(CONFIG_NAND_ENV_DST) |
64 | env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST; | |
13a5695b | 65 | #else /* ! ENV_IS_EMBEDDED */ |
49822e23 | 66 | env_t *env_ptr = 0; |
13a5695b WD |
67 | #endif /* ENV_IS_EMBEDDED */ |
68 | ||
d87080b7 | 69 | DECLARE_GLOBAL_DATA_PTR; |
13a5695b WD |
70 | |
71 | uchar env_get_char_spec (int index) | |
72 | { | |
13a5695b WD |
73 | return ( *((uchar *)(gd->env_addr + index)) ); |
74 | } | |
75 | ||
ea882baf WD |
76 | /* |
77 | * This is called before nand_init() so we can't read NAND to | |
78 | * validate env data. | |
79 | * | |
80 | * Mark it OK for now. env_relocate() in env_common.c will call our | |
81 | * relocate function which does the real validation. | |
d12ae808 SR |
82 | * |
83 | * When using a NAND boot image (like sequoia_nand), the environment | |
ea882baf WD |
84 | * can be embedded or attached to the U-Boot image in NAND flash. |
85 | * This way the SPL loads not only the U-Boot image from NAND but | |
86 | * also the environment. | |
13a5695b WD |
87 | */ |
88 | int env_init(void) | |
89 | { | |
b74ab737 | 90 | #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST) |
d12ae808 | 91 | int crc1_ok = 0, crc2_ok = 0; |
b74ab737 GL |
92 | env_t *tmp_env1; |
93 | ||
94 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
95 | env_t *tmp_env2; | |
d12ae808 | 96 | |
0e8d1586 | 97 | tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE); |
b74ab737 GL |
98 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); |
99 | #endif | |
100 | ||
101 | tmp_env1 = env_ptr; | |
d12ae808 SR |
102 | |
103 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); | |
d12ae808 | 104 | |
b74ab737 GL |
105 | if (!crc1_ok && !crc2_ok) { |
106 | gd->env_addr = 0; | |
d12ae808 | 107 | gd->env_valid = 0; |
b74ab737 GL |
108 | |
109 | return 0; | |
110 | } else if (crc1_ok && !crc2_ok) { | |
d12ae808 | 111 | gd->env_valid = 1; |
b74ab737 GL |
112 | } |
113 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
114 | else if (!crc1_ok && crc2_ok) { | |
d12ae808 | 115 | gd->env_valid = 2; |
b74ab737 | 116 | } else { |
d12ae808 SR |
117 | /* both ok - check serial */ |
118 | if(tmp_env1->flags == 255 && tmp_env2->flags == 0) | |
119 | gd->env_valid = 2; | |
120 | else if(tmp_env2->flags == 255 && tmp_env1->flags == 0) | |
121 | gd->env_valid = 1; | |
122 | else if(tmp_env1->flags > tmp_env2->flags) | |
123 | gd->env_valid = 1; | |
124 | else if(tmp_env2->flags > tmp_env1->flags) | |
125 | gd->env_valid = 2; | |
126 | else /* flags are equal - almost impossible */ | |
127 | gd->env_valid = 1; | |
128 | } | |
129 | ||
b74ab737 GL |
130 | if (gd->env_valid == 2) |
131 | env_ptr = tmp_env2; | |
132 | else | |
133 | #endif | |
d12ae808 SR |
134 | if (gd->env_valid == 1) |
135 | env_ptr = tmp_env1; | |
b74ab737 GL |
136 | |
137 | gd->env_addr = (ulong)env_ptr->data; | |
138 | ||
139 | #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ | |
e443c944 | 140 | gd->env_addr = (ulong)&default_environment[0]; |
13a5695b | 141 | gd->env_valid = 1; |
b74ab737 | 142 | #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ |
13a5695b WD |
143 | |
144 | return (0); | |
145 | } | |
146 | ||
147 | #ifdef CMD_SAVEENV | |
addb2e16 BS |
148 | /* |
149 | * The legacy NAND code saved the environment in the first NAND device i.e., | |
150 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. | |
151 | */ | |
cc49cade SW |
152 | int writeenv(size_t offset, u_char *buf) |
153 | { | |
0e8d1586 | 154 | size_t end = offset + CONFIG_ENV_RANGE; |
cc49cade | 155 | size_t amount_saved = 0; |
c3db8c64 | 156 | size_t blocksize, len; |
cc49cade SW |
157 | |
158 | u_char *char_ptr; | |
159 | ||
160 | blocksize = nand_info[0].erasesize; | |
0e8d1586 | 161 | len = min(blocksize, CONFIG_ENV_SIZE); |
cc49cade | 162 | |
0e8d1586 | 163 | while (amount_saved < CONFIG_ENV_SIZE && offset < end) { |
cc49cade SW |
164 | if (nand_block_isbad(&nand_info[0], offset)) { |
165 | offset += blocksize; | |
166 | } else { | |
167 | char_ptr = &buf[amount_saved]; | |
c3db8c64 | 168 | if (nand_write(&nand_info[0], offset, &len, |
cc49cade SW |
169 | char_ptr)) |
170 | return 1; | |
171 | offset += blocksize; | |
c3db8c64 | 172 | amount_saved += len; |
cc49cade SW |
173 | } |
174 | } | |
0e8d1586 | 175 | if (amount_saved != CONFIG_ENV_SIZE) |
cc49cade SW |
176 | return 1; |
177 | ||
178 | return 0; | |
179 | } | |
eef1d719 | 180 | |
0e8d1586 | 181 | #ifdef CONFIG_ENV_OFFSET_REDUND |
eef1d719 SW |
182 | static unsigned char env_flags; |
183 | ||
13a5695b WD |
184 | int saveenv(void) |
185 | { | |
ea882baf WD |
186 | env_t env_new; |
187 | ssize_t len; | |
188 | char *res; | |
189 | int ret = 0; | |
cc49cade | 190 | nand_erase_options_t nand_erase_options; |
e443c944 | 191 | |
3b250ffb | 192 | memset(&nand_erase_options, 0, sizeof(nand_erase_options)); |
0e8d1586 | 193 | nand_erase_options.length = CONFIG_ENV_RANGE; |
cc49cade | 194 | |
0e8d1586 | 195 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
cc49cade | 196 | return 1; |
ea882baf WD |
197 | |
198 | res = (char *)&env_new.data; | |
37f2fe74 | 199 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
200 | if (len < 0) { |
201 | error("Cannot export environment: errno = %d\n", errno); | |
202 | return 1; | |
203 | } | |
204 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
eef1d719 | 205 | env_new.flags = ++env_flags; /* increase the serial */ |
ea882baf | 206 | |
e443c944 | 207 | if(gd->env_valid == 1) { |
ea882baf | 208 | puts("Erasing redundant NAND...\n"); |
0e8d1586 | 209 | nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND; |
cc49cade | 210 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
e443c944 | 211 | return 1; |
cc49cade | 212 | |
ea882baf WD |
213 | puts("Writing to redundant NAND... "); |
214 | ret = writeenv(CONFIG_ENV_OFFSET_REDUND, | |
215 | (u_char *)&env_new); | |
e443c944 | 216 | } else { |
ea882baf | 217 | puts("Erasing NAND...\n"); |
0e8d1586 | 218 | nand_erase_options.offset = CONFIG_ENV_OFFSET; |
cc49cade | 219 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
e443c944 MK |
220 | return 1; |
221 | ||
ea882baf WD |
222 | puts("Writing to NAND... "); |
223 | ret = writeenv(CONFIG_ENV_OFFSET, | |
224 | (u_char *)&env_new); | |
e443c944 | 225 | } |
cc49cade SW |
226 | if (ret) { |
227 | puts("FAILED!\n"); | |
e443c944 | 228 | return 1; |
cc49cade | 229 | } |
e443c944 | 230 | |
ea882baf WD |
231 | puts("done\n"); |
232 | ||
e443c944 | 233 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
ea882baf | 234 | |
e443c944 MK |
235 | return ret; |
236 | } | |
0e8d1586 | 237 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
e443c944 MK |
238 | int saveenv(void) |
239 | { | |
d52fb7e3 | 240 | int ret = 0; |
ea882baf WD |
241 | env_t env_new; |
242 | ssize_t len; | |
243 | char *res; | |
9e4006bc | 244 | nand_erase_options_t nand_erase_options; |
e093a247 | 245 | |
3b250ffb | 246 | memset(&nand_erase_options, 0, sizeof(nand_erase_options)); |
0e8d1586 | 247 | nand_erase_options.length = CONFIG_ENV_RANGE; |
0e8d1586 | 248 | nand_erase_options.offset = CONFIG_ENV_OFFSET; |
cc49cade | 249 | |
0e8d1586 | 250 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
cc49cade | 251 | return 1; |
ea882baf WD |
252 | |
253 | res = (char *)&env_new.data; | |
37f2fe74 | 254 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL); |
ea882baf WD |
255 | if (len < 0) { |
256 | error("Cannot export environment: errno = %d\n", errno); | |
257 | return 1; | |
258 | } | |
259 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
260 | ||
261 | puts("Erasing Nand...\n"); | |
cc49cade | 262 | if (nand_erase_opts(&nand_info[0], &nand_erase_options)) |
addb2e16 | 263 | return 1; |
13a5695b | 264 | |
ea882baf WD |
265 | puts("Writing to Nand... "); |
266 | if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) { | |
cc49cade | 267 | puts("FAILED!\n"); |
13a5695b | 268 | return 1; |
cc49cade | 269 | } |
13a5695b | 270 | |
ea882baf | 271 | puts("done\n"); |
addb2e16 | 272 | return ret; |
13a5695b | 273 | } |
0e8d1586 | 274 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
13a5695b WD |
275 | #endif /* CMD_SAVEENV */ |
276 | ||
ea882baf | 277 | int readenv(size_t offset, u_char * buf) |
cc49cade | 278 | { |
0e8d1586 | 279 | size_t end = offset + CONFIG_ENV_RANGE; |
cc49cade | 280 | size_t amount_loaded = 0; |
c3db8c64 | 281 | size_t blocksize, len; |
cc49cade SW |
282 | |
283 | u_char *char_ptr; | |
284 | ||
285 | blocksize = nand_info[0].erasesize; | |
962ad59e MF |
286 | if (!blocksize) |
287 | return 1; | |
0e8d1586 | 288 | len = min(blocksize, CONFIG_ENV_SIZE); |
cc49cade | 289 | |
0e8d1586 | 290 | while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { |
cc49cade SW |
291 | if (nand_block_isbad(&nand_info[0], offset)) { |
292 | offset += blocksize; | |
293 | } else { | |
294 | char_ptr = &buf[amount_loaded]; | |
068a208b | 295 | if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr)) |
cc49cade SW |
296 | return 1; |
297 | offset += blocksize; | |
c3db8c64 | 298 | amount_loaded += len; |
cc49cade SW |
299 | } |
300 | } | |
0e8d1586 | 301 | if (amount_loaded != CONFIG_ENV_SIZE) |
cc49cade SW |
302 | return 1; |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
c9f7351b BG |
307 | #ifdef CONFIG_ENV_OFFSET_OOB |
308 | int get_nand_env_oob(nand_info_t *nand, unsigned long *result) | |
309 | { | |
310 | struct mtd_oob_ops ops; | |
311 | uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)]; | |
312 | int ret; | |
313 | ||
314 | ops.datbuf = NULL; | |
315 | ops.mode = MTD_OOB_AUTO; | |
316 | ops.ooboffs = 0; | |
317 | ops.ooblen = ENV_OFFSET_SIZE; | |
318 | ops.oobbuf = (void *) oob_buf; | |
319 | ||
320 | ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops); | |
53504a27 SW |
321 | if (ret) { |
322 | printf("error reading OOB block 0\n"); | |
323 | return ret; | |
324 | } | |
c9f7351b | 325 | |
53504a27 SW |
326 | if (oob_buf[0] == ENV_OOB_MARKER) { |
327 | *result = oob_buf[1] * nand->erasesize; | |
328 | } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) { | |
329 | *result = oob_buf[1]; | |
c9f7351b | 330 | } else { |
53504a27 SW |
331 | printf("No dynamic environment marker in OOB block 0\n"); |
332 | return -ENOENT; | |
c9f7351b | 333 | } |
53504a27 SW |
334 | |
335 | return 0; | |
c9f7351b BG |
336 | } |
337 | #endif | |
338 | ||
0e8d1586 | 339 | #ifdef CONFIG_ENV_OFFSET_REDUND |
ea882baf | 340 | void env_relocate_spec(void) |
e443c944 MK |
341 | { |
342 | #if !defined(ENV_IS_EMBEDDED) | |
2770bcb2 | 343 | int crc1_ok = 0, crc2_ok = 0; |
ea882baf | 344 | env_t *ep, *tmp_env1, *tmp_env2; |
e443c944 | 345 | |
ea882baf WD |
346 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
347 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); | |
e443c944 | 348 | |
15b86c3d WD |
349 | if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) { |
350 | puts("Can't allocate buffers for environment\n"); | |
ea882baf WD |
351 | free(tmp_env1); |
352 | free(tmp_env2); | |
353 | set_default_env("!malloc() failed"); | |
354 | return; | |
15b86c3d WD |
355 | } |
356 | ||
0e8d1586 | 357 | if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1)) |
ea882baf WD |
358 | puts("No Valid Environment Area found\n"); |
359 | ||
0e8d1586 | 360 | if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2)) |
ea882baf | 361 | puts("No Valid Redundant Environment Area found\n"); |
e443c944 MK |
362 | |
363 | crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); | |
364 | crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); | |
365 | ||
ea882baf | 366 | if (!crc1_ok && !crc2_ok) { |
5a9427dc | 367 | free(tmp_env1); |
368 | free(tmp_env2); | |
ea882baf WD |
369 | set_default_env("!bad CRC"); |
370 | return; | |
371 | } else if (crc1_ok && !crc2_ok) { | |
e443c944 | 372 | gd->env_valid = 1; |
ea882baf | 373 | } else if (!crc1_ok && crc2_ok) { |
e443c944 | 374 | gd->env_valid = 2; |
ea882baf | 375 | } else { |
e443c944 | 376 | /* both ok - check serial */ |
ea882baf | 377 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
e443c944 | 378 | gd->env_valid = 2; |
ea882baf | 379 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
e443c944 | 380 | gd->env_valid = 1; |
ea882baf | 381 | else if (tmp_env1->flags > tmp_env2->flags) |
e443c944 | 382 | gd->env_valid = 1; |
ea882baf | 383 | else if (tmp_env2->flags > tmp_env1->flags) |
e443c944 MK |
384 | gd->env_valid = 2; |
385 | else /* flags are equal - almost impossible */ | |
386 | gd->env_valid = 1; | |
13a5695b | 387 | |
e443c944 MK |
388 | } |
389 | ||
390 | free(env_ptr); | |
ea882baf WD |
391 | |
392 | if (gd->env_valid == 1) | |
393 | ep = tmp_env1; | |
394 | else | |
395 | ep = tmp_env2; | |
396 | ||
eef1d719 | 397 | env_flags = ep->flags; |
ea882baf WD |
398 | env_import((char *)ep, 0); |
399 | ||
400 | free(tmp_env1); | |
401 | free(tmp_env2); | |
e443c944 MK |
402 | |
403 | #endif /* ! ENV_IS_EMBEDDED */ | |
404 | } | |
0e8d1586 | 405 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
addb2e16 | 406 | /* |
ea882baf WD |
407 | * The legacy NAND code saved the environment in the first NAND |
408 | * device i.e., nand_dev_desc + 0. This is also the behaviour using | |
409 | * the new NAND code. | |
addb2e16 | 410 | */ |
13a5695b WD |
411 | void env_relocate_spec (void) |
412 | { | |
413 | #if !defined(ENV_IS_EMBEDDED) | |
d52fb7e3 | 414 | int ret; |
ea882baf | 415 | char buf[CONFIG_ENV_SIZE]; |
13a5695b | 416 | |
c9f7351b BG |
417 | #if defined(CONFIG_ENV_OFFSET_OOB) |
418 | ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset); | |
ea882baf WD |
419 | /* |
420 | * If unable to read environment offset from NAND OOB then fall through | |
c9f7351b BG |
421 | * to the normal environment reading code below |
422 | */ | |
ea882baf | 423 | if (!ret) { |
c9f7351b | 424 | printf("Found Environment offset in OOB..\n"); |
ea882baf WD |
425 | } else { |
426 | set_default_env("!no env offset in OOB"); | |
427 | return; | |
428 | } | |
c9f7351b BG |
429 | #endif |
430 | ||
ea882baf WD |
431 | ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf); |
432 | if (ret) { | |
433 | set_default_env("!readenv() failed"); | |
434 | return; | |
435 | } | |
13a5695b | 436 | |
ea882baf | 437 | env_import(buf, 1); |
13a5695b | 438 | #endif /* ! ENV_IS_EMBEDDED */ |
13a5695b | 439 | } |
0e8d1586 | 440 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |