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