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