]>
Commit | Line | Data |
---|---|---|
13a5695b | 1 | /* |
ea882baf WD |
2 | * (C) Copyright 2000-2010 |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
4 | * | |
cc49cade SW |
5 | * (C) Copyright 2008 |
6 | * Stuart Wood, Lab X Technologies <[email protected]> | |
7 | * | |
13a5695b WD |
8 | * (C) Copyright 2004 |
9 | * Jian Zhang, Texas Instruments, [email protected]. | |
13a5695b WD |
10 | * |
11 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
12 | * Andreas Heppel <[email protected]> | |
ea882baf | 13 | * |
3765b3e7 | 14 | * SPDX-License-Identifier: GPL-2.0+ |
13a5695b WD |
15 | */ |
16 | ||
13a5695b | 17 | #include <common.h> |
13a5695b WD |
18 | #include <command.h> |
19 | #include <environment.h> | |
20 | #include <linux/stddef.h> | |
e443c944 | 21 | #include <malloc.h> |
cf92e05c | 22 | #include <memalign.h> |
addb2e16 | 23 | #include <nand.h> |
ea882baf WD |
24 | #include <search.h> |
25 | #include <errno.h> | |
13a5695b | 26 | |
4415f1d1 SG |
27 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) && \ |
28 | !defined(CONFIG_SPL_BUILD) | |
13a5695b | 29 | #define CMD_SAVEENV |
0e8d1586 | 30 | #elif defined(CONFIG_ENV_OFFSET_REDUND) |
de152b9b | 31 | #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND |
13a5695b WD |
32 | #endif |
33 | ||
de152b9b IG |
34 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
35 | (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) | |
0e8d1586 | 36 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE |
13a5695b WD |
37 | #endif |
38 | ||
0e8d1586 JCPV |
39 | #ifndef CONFIG_ENV_RANGE |
40 | #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE | |
cc49cade SW |
41 | #endif |
42 | ||
b74ab737 | 43 | #if defined(ENV_IS_EMBEDDED) |
994bc671 | 44 | env_t *env_ptr = &environment; |
b74ab737 GL |
45 | #elif defined(CONFIG_NAND_ENV_DST) |
46 | env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST; | |
13a5695b | 47 | #else /* ! ENV_IS_EMBEDDED */ |
de152b9b | 48 | env_t *env_ptr; |
13a5695b WD |
49 | #endif /* ENV_IS_EMBEDDED */ |
50 | ||
d87080b7 | 51 | DECLARE_GLOBAL_DATA_PTR; |
13a5695b | 52 | |
ea882baf WD |
53 | /* |
54 | * This is called before nand_init() so we can't read NAND to | |
55 | * validate env data. | |
56 | * | |
57 | * Mark it OK for now. env_relocate() in env_common.c will call our | |
58 | * relocate function which does the real validation. | |
d12ae808 SR |
59 | * |
60 | * When using a NAND boot image (like sequoia_nand), the environment | |
ea882baf WD |
61 | * can be embedded or attached to the U-Boot image in NAND flash. |
62 | * This way the SPL loads not only the U-Boot image from NAND but | |
63 | * also the environment. | |
13a5695b | 64 | */ |
e5bce247 | 65 | static int env_nand_init(void) |
13a5695b | 66 | { |
b74ab737 | 67 | #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST) |
d12ae808 | 68 | int crc1_ok = 0, crc2_ok = 0; |
b74ab737 GL |
69 | env_t *tmp_env1; |
70 | ||
71 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
72 | env_t *tmp_env2; | |
d12ae808 | 73 | |
0e8d1586 | 74 | tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE); |
de152b9b | 75 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc; |
b74ab737 | 76 | #endif |
b74ab737 | 77 | tmp_env1 = env_ptr; |
de152b9b | 78 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc; |
d12ae808 | 79 | |
b74ab737 | 80 | if (!crc1_ok && !crc2_ok) { |
de152b9b IG |
81 | gd->env_addr = 0; |
82 | gd->env_valid = 0; | |
b74ab737 GL |
83 | |
84 | return 0; | |
85 | } else if (crc1_ok && !crc2_ok) { | |
203e94f6 | 86 | gd->env_valid = ENV_VALID; |
b74ab737 GL |
87 | } |
88 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
89 | else if (!crc1_ok && crc2_ok) { | |
203e94f6 | 90 | gd->env_valid = ENV_REDUND; |
b74ab737 | 91 | } else { |
d12ae808 | 92 | /* both ok - check serial */ |
de152b9b | 93 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
203e94f6 | 94 | gd->env_valid = ENV_REDUND; |
de152b9b | 95 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
203e94f6 | 96 | gd->env_valid = ENV_VALID; |
de152b9b | 97 | else if (tmp_env1->flags > tmp_env2->flags) |
203e94f6 | 98 | gd->env_valid = ENV_VALID; |
de152b9b | 99 | else if (tmp_env2->flags > tmp_env1->flags) |
203e94f6 | 100 | gd->env_valid = ENV_REDUND; |
d12ae808 | 101 | else /* flags are equal - almost impossible */ |
203e94f6 | 102 | gd->env_valid = ENV_VALID; |
d12ae808 SR |
103 | } |
104 | ||
203e94f6 | 105 | if (gd->env_valid == ENV_REDUND) |
b74ab737 GL |
106 | env_ptr = tmp_env2; |
107 | else | |
108 | #endif | |
203e94f6 | 109 | if (gd->env_valid == ENV_VALID) |
d12ae808 | 110 | env_ptr = tmp_env1; |
b74ab737 GL |
111 | |
112 | gd->env_addr = (ulong)env_ptr->data; | |
113 | ||
114 | #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ | |
de152b9b | 115 | gd->env_addr = (ulong)&default_environment[0]; |
203e94f6 | 116 | gd->env_valid = ENV_VALID; |
b74ab737 | 117 | #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ |
13a5695b | 118 | |
de152b9b | 119 | return 0; |
13a5695b WD |
120 | } |
121 | ||
122 | #ifdef CMD_SAVEENV | |
addb2e16 BS |
123 | /* |
124 | * The legacy NAND code saved the environment in the first NAND device i.e., | |
125 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. | |
126 | */ | |
45f08d35 | 127 | static int writeenv(size_t offset, u_char *buf) |
cc49cade | 128 | { |
0e8d1586 | 129 | size_t end = offset + CONFIG_ENV_RANGE; |
cc49cade | 130 | size_t amount_saved = 0; |
c3db8c64 | 131 | size_t blocksize, len; |
a94a2619 | 132 | struct mtd_info *mtd; |
cc49cade SW |
133 | u_char *char_ptr; |
134 | ||
a94a2619 GS |
135 | mtd = get_nand_dev_by_index(0); |
136 | if (!mtd) | |
137 | return 1; | |
138 | ||
139 | blocksize = mtd->erasesize; | |
b4141195 | 140 | len = min(blocksize, (size_t)CONFIG_ENV_SIZE); |
cc49cade | 141 | |
0e8d1586 | 142 | while (amount_saved < CONFIG_ENV_SIZE && offset < end) { |
a94a2619 | 143 | if (nand_block_isbad(mtd, offset)) { |
cc49cade SW |
144 | offset += blocksize; |
145 | } else { | |
146 | char_ptr = &buf[amount_saved]; | |
a94a2619 | 147 | if (nand_write(mtd, offset, &len, char_ptr)) |
cc49cade | 148 | return 1; |
de152b9b | 149 | |
cc49cade | 150 | offset += blocksize; |
c3db8c64 | 151 | amount_saved += len; |
cc49cade SW |
152 | } |
153 | } | |
0e8d1586 | 154 | if (amount_saved != CONFIG_ENV_SIZE) |
cc49cade SW |
155 | return 1; |
156 | ||
157 | return 0; | |
158 | } | |
eef1d719 | 159 | |
42a8180d | 160 | struct nand_env_location { |
2b26201a PS |
161 | const char *name; |
162 | const nand_erase_options_t erase_opts; | |
163 | }; | |
eef1d719 | 164 | |
42a8180d | 165 | static int erase_and_write_env(const struct nand_env_location *location, |
2b26201a | 166 | u_char *env_new) |
13a5695b | 167 | { |
a94a2619 | 168 | struct mtd_info *mtd; |
2b26201a | 169 | int ret = 0; |
cc49cade | 170 | |
a94a2619 GS |
171 | mtd = get_nand_dev_by_index(0); |
172 | if (!mtd) | |
4cc9699b TR |
173 | return 1; |
174 | ||
2b26201a | 175 | printf("Erasing %s...\n", location->name); |
a94a2619 | 176 | if (nand_erase_opts(mtd, &location->erase_opts)) |
cc49cade | 177 | return 1; |
ea882baf | 178 | |
2b26201a PS |
179 | printf("Writing to %s... ", location->name); |
180 | ret = writeenv(location->erase_opts.offset, env_new); | |
181 | puts(ret ? "FAILED!\n" : "OK\n"); | |
ea882baf | 182 | |
e443c944 MK |
183 | return ret; |
184 | } | |
2b26201a | 185 | |
e5bce247 | 186 | static int env_nand_save(void) |
e443c944 | 187 | { |
de152b9b | 188 | int ret = 0; |
cd0f4fa1 | 189 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
2b26201a | 190 | int env_idx = 0; |
42a8180d | 191 | static const struct nand_env_location location[] = { |
2b26201a PS |
192 | { |
193 | .name = "NAND", | |
194 | .erase_opts = { | |
195 | .length = CONFIG_ENV_RANGE, | |
196 | .offset = CONFIG_ENV_OFFSET, | |
197 | }, | |
198 | }, | |
199 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
200 | { | |
201 | .name = "redundant NAND", | |
202 | .erase_opts = { | |
203 | .length = CONFIG_ENV_RANGE, | |
204 | .offset = CONFIG_ENV_OFFSET_REDUND, | |
205 | }, | |
206 | }, | |
207 | #endif | |
208 | }; | |
e093a247 | 209 | |
cc49cade | 210 | |
0e8d1586 | 211 | if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) |
cc49cade | 212 | return 1; |
ea882baf | 213 | |
7ce1526e MV |
214 | ret = env_export(env_new); |
215 | if (ret) | |
216 | return ret; | |
217 | ||
2b26201a | 218 | #ifdef CONFIG_ENV_OFFSET_REDUND |
203e94f6 | 219 | env_idx = (gd->env_valid == ENV_VALID); |
2b26201a | 220 | #endif |
13a5695b | 221 | |
2b26201a PS |
222 | ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); |
223 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
224 | if (!ret) { | |
225 | /* preset other copy for next write */ | |
203e94f6 SG |
226 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : |
227 | ENV_REDUND; | |
2b26201a | 228 | return ret; |
cc49cade | 229 | } |
13a5695b | 230 | |
2b26201a PS |
231 | env_idx = (env_idx + 1) & 1; |
232 | ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); | |
233 | if (!ret) | |
234 | printf("Warning: primary env write failed," | |
235 | " redundancy is lost!\n"); | |
236 | #endif | |
237 | ||
addb2e16 | 238 | return ret; |
13a5695b WD |
239 | } |
240 | #endif /* CMD_SAVEENV */ | |
241 | ||
9e205602 TH |
242 | #if defined(CONFIG_SPL_BUILD) |
243 | static int readenv(size_t offset, u_char *buf) | |
244 | { | |
245 | return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf); | |
246 | } | |
247 | #else | |
45f08d35 | 248 | static int readenv(size_t offset, u_char *buf) |
cc49cade | 249 | { |
0e8d1586 | 250 | size_t end = offset + CONFIG_ENV_RANGE; |
cc49cade | 251 | size_t amount_loaded = 0; |
c3db8c64 | 252 | size_t blocksize, len; |
a94a2619 | 253 | struct mtd_info *mtd; |
cc49cade SW |
254 | u_char *char_ptr; |
255 | ||
a94a2619 GS |
256 | mtd = get_nand_dev_by_index(0); |
257 | if (!mtd) | |
962ad59e | 258 | return 1; |
de152b9b | 259 | |
a94a2619 | 260 | blocksize = mtd->erasesize; |
b4141195 | 261 | len = min(blocksize, (size_t)CONFIG_ENV_SIZE); |
cc49cade | 262 | |
0e8d1586 | 263 | while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { |
a94a2619 | 264 | if (nand_block_isbad(mtd, offset)) { |
cc49cade SW |
265 | offset += blocksize; |
266 | } else { | |
267 | char_ptr = &buf[amount_loaded]; | |
a94a2619 | 268 | if (nand_read_skip_bad(mtd, offset, |
c39d6a0e | 269 | &len, NULL, |
a94a2619 | 270 | mtd->size, char_ptr)) |
cc49cade | 271 | return 1; |
de152b9b | 272 | |
cc49cade | 273 | offset += blocksize; |
c3db8c64 | 274 | amount_loaded += len; |
cc49cade SW |
275 | } |
276 | } | |
de152b9b | 277 | |
0e8d1586 | 278 | if (amount_loaded != CONFIG_ENV_SIZE) |
cc49cade SW |
279 | return 1; |
280 | ||
281 | return 0; | |
282 | } | |
9e205602 | 283 | #endif /* #if defined(CONFIG_SPL_BUILD) */ |
cc49cade | 284 | |
c9f7351b | 285 | #ifdef CONFIG_ENV_OFFSET_OOB |
151c06ec | 286 | int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result) |
c9f7351b BG |
287 | { |
288 | struct mtd_oob_ops ops; | |
de152b9b | 289 | uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)]; |
c9f7351b BG |
290 | int ret; |
291 | ||
de152b9b IG |
292 | ops.datbuf = NULL; |
293 | ops.mode = MTD_OOB_AUTO; | |
294 | ops.ooboffs = 0; | |
295 | ops.ooblen = ENV_OFFSET_SIZE; | |
296 | ops.oobbuf = (void *)oob_buf; | |
c9f7351b | 297 | |
151c06ec | 298 | ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops); |
53504a27 SW |
299 | if (ret) { |
300 | printf("error reading OOB block 0\n"); | |
301 | return ret; | |
302 | } | |
c9f7351b | 303 | |
53504a27 | 304 | if (oob_buf[0] == ENV_OOB_MARKER) { |
c5951991 | 305 | *result = ovoid ob_buf[1] * mtd->erasesize; |
53504a27 SW |
306 | } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) { |
307 | *result = oob_buf[1]; | |
c9f7351b | 308 | } else { |
53504a27 SW |
309 | printf("No dynamic environment marker in OOB block 0\n"); |
310 | return -ENOENT; | |
c9f7351b | 311 | } |
53504a27 SW |
312 | |
313 | return 0; | |
c9f7351b BG |
314 | } |
315 | #endif | |
316 | ||
0e8d1586 | 317 | #ifdef CONFIG_ENV_OFFSET_REDUND |
c5951991 | 318 | static int env_nand_load(void) |
e443c944 | 319 | { |
c5951991 SG |
320 | #if defined(ENV_IS_EMBEDDED) |
321 | return 0; | |
322 | #else | |
b76a147b | 323 | int read1_fail = 0, read2_fail = 0; |
9d364af2 | 324 | env_t *tmp_env1, *tmp_env2; |
c5951991 | 325 | int ret = 0; |
e443c944 | 326 | |
ea882baf WD |
327 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
328 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); | |
de152b9b | 329 | if (tmp_env1 == NULL || tmp_env2 == NULL) { |
15b86c3d | 330 | puts("Can't allocate buffers for environment\n"); |
ea882baf | 331 | set_default_env("!malloc() failed"); |
c5951991 | 332 | ret = -EIO; |
de152b9b | 333 | goto done; |
15b86c3d WD |
334 | } |
335 | ||
b76a147b PS |
336 | read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1); |
337 | read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2); | |
ea882baf | 338 | |
b76a147b PS |
339 | if (read1_fail && read2_fail) |
340 | puts("*** Error - No Valid Environment Area found\n"); | |
341 | else if (read1_fail || read2_fail) | |
342 | puts("*** Warning - some problems detected " | |
343 | "reading environment; recovered successfully\n"); | |
e443c944 | 344 | |
9d364af2 FA |
345 | if (read1_fail && read2_fail) { |
346 | set_default_env("!bad env area"); | |
de152b9b | 347 | goto done; |
9d364af2 | 348 | } else if (!read1_fail && read2_fail) { |
203e94f6 | 349 | gd->env_valid = ENV_VALID; |
9d364af2 FA |
350 | env_import((char *)tmp_env1, 1); |
351 | } else if (read1_fail && !read2_fail) { | |
203e94f6 | 352 | gd->env_valid = ENV_REDUND; |
9d364af2 | 353 | env_import((char *)tmp_env2, 1); |
ea882baf | 354 | } else { |
9d364af2 | 355 | env_import_redund((char *)tmp_env1, (char *)tmp_env2); |
e443c944 MK |
356 | } |
357 | ||
de152b9b | 358 | done: |
ea882baf WD |
359 | free(tmp_env1); |
360 | free(tmp_env2); | |
e443c944 | 361 | |
c5951991 | 362 | return ret; |
e443c944 MK |
363 | #endif /* ! ENV_IS_EMBEDDED */ |
364 | } | |
0e8d1586 | 365 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
addb2e16 | 366 | /* |
ea882baf WD |
367 | * The legacy NAND code saved the environment in the first NAND |
368 | * device i.e., nand_dev_desc + 0. This is also the behaviour using | |
369 | * the new NAND code. | |
addb2e16 | 370 | */ |
c5951991 | 371 | static int env_nand_load(void) |
13a5695b WD |
372 | { |
373 | #if !defined(ENV_IS_EMBEDDED) | |
d52fb7e3 | 374 | int ret; |
cd0f4fa1 | 375 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
13a5695b | 376 | |
c9f7351b | 377 | #if defined(CONFIG_ENV_OFFSET_OOB) |
a94a2619 | 378 | struct mtd_info *mtd = get_nand_dev_by_index(0); |
ea882baf WD |
379 | /* |
380 | * If unable to read environment offset from NAND OOB then fall through | |
c9f7351b BG |
381 | * to the normal environment reading code below |
382 | */ | |
a94a2619 | 383 | if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) { |
c9f7351b | 384 | printf("Found Environment offset in OOB..\n"); |
ea882baf WD |
385 | } else { |
386 | set_default_env("!no env offset in OOB"); | |
387 | return; | |
388 | } | |
c9f7351b BG |
389 | #endif |
390 | ||
cd0f4fa1 | 391 | ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf); |
ea882baf WD |
392 | if (ret) { |
393 | set_default_env("!readenv() failed"); | |
c5951991 | 394 | return -EIO; |
ea882baf | 395 | } |
13a5695b | 396 | |
cd0f4fa1 | 397 | env_import(buf, 1); |
13a5695b | 398 | #endif /* ! ENV_IS_EMBEDDED */ |
c5951991 SG |
399 | |
400 | return 0; | |
13a5695b | 401 | } |
0e8d1586 | 402 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
4415f1d1 SG |
403 | |
404 | U_BOOT_ENV_LOCATION(nand) = { | |
405 | .location = ENVL_NAND, | |
ac358beb | 406 | ENV_NAME("NAND") |
e5bce247 | 407 | .load = env_nand_load, |
4415f1d1 | 408 | #if defined(CMD_SAVEENV) |
e5bce247 | 409 | .save = env_save_ptr(env_nand_save), |
4415f1d1 | 410 | #endif |
e5bce247 | 411 | .init = env_nand_init, |
4415f1d1 | 412 | }; |