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