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