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