]>
Commit | Line | Data |
---|---|---|
13a5695b | 1 | /* |
ea882baf WD |
2 | * (C) Copyright 2000-2010 |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
4 | * | |
cc49cade SW |
5 | * (C) Copyright 2008 |
6 | * Stuart Wood, Lab X Technologies <[email protected]> | |
7 | * | |
13a5695b WD |
8 | * (C) Copyright 2004 |
9 | * Jian Zhang, Texas Instruments, [email protected]. | |
13a5695b WD |
10 | * |
11 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
12 | * Andreas Heppel <[email protected]> | |
ea882baf | 13 | * |
3765b3e7 | 14 | * SPDX-License-Identifier: GPL-2.0+ |
13a5695b WD |
15 | */ |
16 | ||
13a5695b | 17 | #include <common.h> |
13a5695b WD |
18 | #include <command.h> |
19 | #include <environment.h> | |
20 | #include <linux/stddef.h> | |
e443c944 | 21 | #include <malloc.h> |
addb2e16 | 22 | #include <nand.h> |
ea882baf WD |
23 | #include <search.h> |
24 | #include <errno.h> | |
13a5695b | 25 | |
bdab39d3 | 26 | #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) |
13a5695b | 27 | #define CMD_SAVEENV |
0e8d1586 | 28 | #elif defined(CONFIG_ENV_OFFSET_REDUND) |
de152b9b | 29 | #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND |
13a5695b WD |
30 | #endif |
31 | ||
de152b9b IG |
32 | #if defined(CONFIG_ENV_SIZE_REDUND) && \ |
33 | (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE) | |
0e8d1586 | 34 | #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE |
13a5695b WD |
35 | #endif |
36 | ||
0e8d1586 JCPV |
37 | #ifndef CONFIG_ENV_RANGE |
38 | #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE | |
cc49cade SW |
39 | #endif |
40 | ||
ea882baf | 41 | char *env_name_spec = "NAND"; |
13a5695b | 42 | |
b74ab737 | 43 | #if defined(ENV_IS_EMBEDDED) |
994bc671 | 44 | env_t *env_ptr = &environment; |
b74ab737 GL |
45 | #elif defined(CONFIG_NAND_ENV_DST) |
46 | env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST; | |
13a5695b | 47 | #else /* ! ENV_IS_EMBEDDED */ |
de152b9b | 48 | env_t *env_ptr; |
13a5695b WD |
49 | #endif /* ENV_IS_EMBEDDED */ |
50 | ||
d87080b7 | 51 | DECLARE_GLOBAL_DATA_PTR; |
13a5695b | 52 | |
ea882baf WD |
53 | /* |
54 | * This is called before nand_init() so we can't read NAND to | |
55 | * validate env data. | |
56 | * | |
57 | * Mark it OK for now. env_relocate() in env_common.c will call our | |
58 | * relocate function which does the real validation. | |
d12ae808 SR |
59 | * |
60 | * When using a NAND boot image (like sequoia_nand), the environment | |
ea882baf WD |
61 | * can be embedded or attached to the U-Boot image in NAND flash. |
62 | * This way the SPL loads not only the U-Boot image from NAND but | |
63 | * also the environment. | |
13a5695b WD |
64 | */ |
65 | int env_init(void) | |
66 | { | |
b74ab737 | 67 | #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST) |
d12ae808 | 68 | int crc1_ok = 0, crc2_ok = 0; |
b74ab737 GL |
69 | env_t *tmp_env1; |
70 | ||
71 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
72 | env_t *tmp_env2; | |
d12ae808 | 73 | |
0e8d1586 | 74 | tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE); |
de152b9b | 75 | crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc; |
b74ab737 | 76 | #endif |
b74ab737 | 77 | tmp_env1 = env_ptr; |
de152b9b | 78 | crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc; |
d12ae808 | 79 | |
b74ab737 | 80 | if (!crc1_ok && !crc2_ok) { |
de152b9b IG |
81 | gd->env_addr = 0; |
82 | gd->env_valid = 0; | |
b74ab737 GL |
83 | |
84 | return 0; | |
85 | } else if (crc1_ok && !crc2_ok) { | |
d12ae808 | 86 | gd->env_valid = 1; |
b74ab737 GL |
87 | } |
88 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
89 | else if (!crc1_ok && crc2_ok) { | |
d12ae808 | 90 | gd->env_valid = 2; |
b74ab737 | 91 | } else { |
d12ae808 | 92 | /* both ok - check serial */ |
de152b9b | 93 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
d12ae808 | 94 | gd->env_valid = 2; |
de152b9b | 95 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
d12ae808 | 96 | gd->env_valid = 1; |
de152b9b | 97 | else if (tmp_env1->flags > tmp_env2->flags) |
d12ae808 | 98 | gd->env_valid = 1; |
de152b9b | 99 | else if (tmp_env2->flags > tmp_env1->flags) |
d12ae808 SR |
100 | gd->env_valid = 2; |
101 | else /* flags are equal - almost impossible */ | |
102 | gd->env_valid = 1; | |
103 | } | |
104 | ||
b74ab737 GL |
105 | if (gd->env_valid == 2) |
106 | env_ptr = tmp_env2; | |
107 | else | |
108 | #endif | |
d12ae808 SR |
109 | if (gd->env_valid == 1) |
110 | env_ptr = tmp_env1; | |
b74ab737 GL |
111 | |
112 | gd->env_addr = (ulong)env_ptr->data; | |
113 | ||
114 | #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ | |
de152b9b IG |
115 | gd->env_addr = (ulong)&default_environment[0]; |
116 | gd->env_valid = 1; | |
b74ab737 | 117 | #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */ |
13a5695b | 118 | |
de152b9b | 119 | return 0; |
13a5695b WD |
120 | } |
121 | ||
122 | #ifdef CMD_SAVEENV | |
addb2e16 BS |
123 | /* |
124 | * The legacy NAND code saved the environment in the first NAND device i.e., | |
125 | * nand_dev_desc + 0. This is also the behaviour using the new NAND code. | |
126 | */ | |
cc49cade SW |
127 | int writeenv(size_t offset, u_char *buf) |
128 | { | |
0e8d1586 | 129 | size_t end = offset + CONFIG_ENV_RANGE; |
cc49cade | 130 | size_t amount_saved = 0; |
c3db8c64 | 131 | size_t blocksize, len; |
cc49cade SW |
132 | u_char *char_ptr; |
133 | ||
134 | blocksize = nand_info[0].erasesize; | |
0e8d1586 | 135 | len = min(blocksize, CONFIG_ENV_SIZE); |
cc49cade | 136 | |
0e8d1586 | 137 | while (amount_saved < CONFIG_ENV_SIZE && offset < end) { |
cc49cade SW |
138 | if (nand_block_isbad(&nand_info[0], offset)) { |
139 | offset += blocksize; | |
140 | } else { | |
141 | char_ptr = &buf[amount_saved]; | |
de152b9b | 142 | if (nand_write(&nand_info[0], offset, &len, char_ptr)) |
cc49cade | 143 | return 1; |
de152b9b | 144 | |
cc49cade | 145 | offset += blocksize; |
c3db8c64 | 146 | amount_saved += len; |
cc49cade SW |
147 | } |
148 | } | |
0e8d1586 | 149 | if (amount_saved != CONFIG_ENV_SIZE) |
cc49cade SW |
150 | return 1; |
151 | ||
152 | return 0; | |
153 | } | |
eef1d719 | 154 | |
2b26201a PS |
155 | struct env_location { |
156 | const char *name; | |
157 | const nand_erase_options_t erase_opts; | |
158 | }; | |
eef1d719 | 159 | |
2b26201a PS |
160 | static int erase_and_write_env(const struct env_location *location, |
161 | u_char *env_new) | |
13a5695b | 162 | { |
2b26201a | 163 | int ret = 0; |
cc49cade | 164 | |
2b26201a PS |
165 | printf("Erasing %s...\n", location->name); |
166 | if (nand_erase_opts(&nand_info[0], &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 PS |
175 | |
176 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
177 | static unsigned char env_flags; | |
178 | #endif | |
179 | ||
e443c944 MK |
180 | int saveenv(void) |
181 | { | |
de152b9b | 182 | int ret = 0; |
cd0f4fa1 | 183 | ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); |
2b26201a PS |
184 | int env_idx = 0; |
185 | static const struct env_location location[] = { | |
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 PS |
212 | #ifdef CONFIG_ENV_OFFSET_REDUND |
213 | env_new->flags = ++env_flags; /* increase the serial */ | |
214 | env_idx = (gd->env_valid == 1); | |
215 | #endif | |
13a5695b | 216 | |
2b26201a PS |
217 | ret = erase_and_write_env(&location[env_idx], (u_char *)env_new); |
218 | #ifdef CONFIG_ENV_OFFSET_REDUND | |
219 | if (!ret) { | |
220 | /* preset other copy for next write */ | |
221 | gd->env_valid = gd->env_valid == 2 ? 1 : 2; | |
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 | ||
de152b9b | 236 | int readenv(size_t offset, u_char *buf) |
cc49cade | 237 | { |
0e8d1586 | 238 | size_t end = offset + CONFIG_ENV_RANGE; |
cc49cade | 239 | size_t amount_loaded = 0; |
c3db8c64 | 240 | size_t blocksize, len; |
cc49cade SW |
241 | u_char *char_ptr; |
242 | ||
243 | blocksize = nand_info[0].erasesize; | |
962ad59e MF |
244 | if (!blocksize) |
245 | return 1; | |
de152b9b | 246 | |
0e8d1586 | 247 | len = min(blocksize, CONFIG_ENV_SIZE); |
cc49cade | 248 | |
0e8d1586 | 249 | while (amount_loaded < CONFIG_ENV_SIZE && offset < end) { |
cc49cade SW |
250 | if (nand_block_isbad(&nand_info[0], offset)) { |
251 | offset += blocksize; | |
252 | } else { | |
253 | char_ptr = &buf[amount_loaded]; | |
de152b9b | 254 | if (nand_read_skip_bad(&nand_info[0], offset, |
c39d6a0e TR |
255 | &len, NULL, |
256 | nand_info[0].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 | } | |
269 | ||
c9f7351b BG |
270 | #ifdef CONFIG_ENV_OFFSET_OOB |
271 | int get_nand_env_oob(nand_info_t *nand, unsigned long *result) | |
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 BG |
282 | |
283 | ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops); | |
53504a27 SW |
284 | if (ret) { |
285 | printf("error reading OOB block 0\n"); | |
286 | return ret; | |
287 | } | |
c9f7351b | 288 | |
53504a27 SW |
289 | if (oob_buf[0] == ENV_OOB_MARKER) { |
290 | *result = oob_buf[1] * nand->erasesize; | |
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 |
ea882baf | 303 | void env_relocate_spec(void) |
e443c944 MK |
304 | { |
305 | #if !defined(ENV_IS_EMBEDDED) | |
b76a147b | 306 | int read1_fail = 0, read2_fail = 0; |
2770bcb2 | 307 | int crc1_ok = 0, crc2_ok = 0; |
ea882baf | 308 | env_t *ep, *tmp_env1, *tmp_env2; |
e443c944 | 309 | |
ea882baf WD |
310 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); |
311 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); | |
de152b9b | 312 | if (tmp_env1 == NULL || tmp_env2 == NULL) { |
15b86c3d | 313 | puts("Can't allocate buffers for environment\n"); |
ea882baf | 314 | set_default_env("!malloc() failed"); |
de152b9b | 315 | goto done; |
15b86c3d WD |
316 | } |
317 | ||
b76a147b PS |
318 | read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1); |
319 | read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2); | |
ea882baf | 320 | |
b76a147b PS |
321 | if (read1_fail && read2_fail) |
322 | puts("*** Error - No Valid Environment Area found\n"); | |
323 | else if (read1_fail || read2_fail) | |
324 | puts("*** Warning - some problems detected " | |
325 | "reading environment; recovered successfully\n"); | |
e443c944 | 326 | |
a1eac57a PS |
327 | crc1_ok = !read1_fail && |
328 | (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc); | |
329 | crc2_ok = !read2_fail && | |
330 | (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc); | |
e443c944 | 331 | |
ea882baf | 332 | if (!crc1_ok && !crc2_ok) { |
ea882baf | 333 | set_default_env("!bad CRC"); |
de152b9b | 334 | goto done; |
ea882baf | 335 | } else if (crc1_ok && !crc2_ok) { |
e443c944 | 336 | gd->env_valid = 1; |
ea882baf | 337 | } else if (!crc1_ok && crc2_ok) { |
e443c944 | 338 | gd->env_valid = 2; |
ea882baf | 339 | } else { |
e443c944 | 340 | /* both ok - check serial */ |
ea882baf | 341 | if (tmp_env1->flags == 255 && tmp_env2->flags == 0) |
e443c944 | 342 | gd->env_valid = 2; |
ea882baf | 343 | else if (tmp_env2->flags == 255 && tmp_env1->flags == 0) |
e443c944 | 344 | gd->env_valid = 1; |
ea882baf | 345 | else if (tmp_env1->flags > tmp_env2->flags) |
e443c944 | 346 | gd->env_valid = 1; |
ea882baf | 347 | else if (tmp_env2->flags > tmp_env1->flags) |
e443c944 MK |
348 | gd->env_valid = 2; |
349 | else /* flags are equal - almost impossible */ | |
350 | gd->env_valid = 1; | |
e443c944 MK |
351 | } |
352 | ||
353 | free(env_ptr); | |
ea882baf WD |
354 | |
355 | if (gd->env_valid == 1) | |
356 | ep = tmp_env1; | |
357 | else | |
358 | ep = tmp_env2; | |
359 | ||
eef1d719 | 360 | env_flags = ep->flags; |
ea882baf WD |
361 | env_import((char *)ep, 0); |
362 | ||
de152b9b | 363 | done: |
ea882baf WD |
364 | free(tmp_env1); |
365 | free(tmp_env2); | |
e443c944 MK |
366 | |
367 | #endif /* ! ENV_IS_EMBEDDED */ | |
368 | } | |
0e8d1586 | 369 | #else /* ! CONFIG_ENV_OFFSET_REDUND */ |
addb2e16 | 370 | /* |
ea882baf WD |
371 | * The legacy NAND code saved the environment in the first NAND |
372 | * device i.e., nand_dev_desc + 0. This is also the behaviour using | |
373 | * the new NAND code. | |
addb2e16 | 374 | */ |
de152b9b | 375 | void env_relocate_spec(void) |
13a5695b WD |
376 | { |
377 | #if !defined(ENV_IS_EMBEDDED) | |
d52fb7e3 | 378 | int ret; |
cd0f4fa1 | 379 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
13a5695b | 380 | |
c9f7351b BG |
381 | #if defined(CONFIG_ENV_OFFSET_OOB) |
382 | ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset); | |
ea882baf WD |
383 | /* |
384 | * If unable to read environment offset from NAND OOB then fall through | |
c9f7351b BG |
385 | * to the normal environment reading code below |
386 | */ | |
ea882baf | 387 | if (!ret) { |
c9f7351b | 388 | printf("Found Environment offset in OOB..\n"); |
ea882baf WD |
389 | } else { |
390 | set_default_env("!no env offset in OOB"); | |
391 | return; | |
392 | } | |
c9f7351b BG |
393 | #endif |
394 | ||
cd0f4fa1 | 395 | ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf); |
ea882baf WD |
396 | if (ret) { |
397 | set_default_env("!readenv() failed"); | |
398 | return; | |
399 | } | |
13a5695b | 400 | |
cd0f4fa1 | 401 | env_import(buf, 1); |
13a5695b | 402 | #endif /* ! ENV_IS_EMBEDDED */ |
13a5695b | 403 | } |
0e8d1586 | 404 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |