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