]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_nand.c
nand_util: delete a useless variable
[people/ms/u-boot.git] / common / env_nand.c
CommitLineData
13a5695b 1/*
ea882baf
WD
2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
cc49cade
SW
5 * (C) Copyright 2008
6 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
7 *
13a5695b
WD
8 * (C) Copyright 2004
9 * Jian Zhang, Texas Instruments, jzhang@ti.com.
13a5695b
WD
10 *
11 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Andreas Heppel <aheppel@sysgo.de>
ea882baf 13 *
1a459660 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 41char *env_name_spec = "NAND";
13a5695b 42
b74ab737 43#if defined(ENV_IS_EMBEDDED)
994bc671 44env_t *env_ptr = &environment;
b74ab737
GL
45#elif defined(CONFIG_NAND_ENV_DST)
46env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
13a5695b 47#else /* ! ENV_IS_EMBEDDED */
de152b9b 48env_t *env_ptr;
13a5695b
WD
49#endif /* ENV_IS_EMBEDDED */
50
d87080b7 51DECLARE_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 */
65int 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
127int 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
0e8d1586 155#ifdef CONFIG_ENV_OFFSET_REDUND
eef1d719
SW
156static unsigned char env_flags;
157
13a5695b
WD
158int saveenv(void)
159{
cd0f4fa1 160 env_t env_new;
ea882baf
WD
161 ssize_t len;
162 char *res;
163 int ret = 0;
cc49cade 164 nand_erase_options_t nand_erase_options;
e443c944 165
3b250ffb 166 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
0e8d1586 167 nand_erase_options.length = CONFIG_ENV_RANGE;
cc49cade 168
0e8d1586 169 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
cc49cade 170 return 1;
ea882baf 171
cd0f4fa1 172 res = (char *)&env_new.data;
be11235a 173 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
ea882baf
WD
174 if (len < 0) {
175 error("Cannot export environment: errno = %d\n", errno);
176 return 1;
177 }
cd0f4fa1
TR
178 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
179 env_new.flags = ++env_flags; /* increase the serial */
ea882baf 180
de152b9b 181 if (gd->env_valid == 1) {
ea882baf 182 puts("Erasing redundant NAND...\n");
0e8d1586 183 nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
cc49cade 184 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
e443c944 185 return 1;
cc49cade 186
ea882baf 187 puts("Writing to redundant NAND... ");
cd0f4fa1 188 ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
e443c944 189 } else {
ea882baf 190 puts("Erasing NAND...\n");
0e8d1586 191 nand_erase_options.offset = CONFIG_ENV_OFFSET;
cc49cade 192 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
e443c944
MK
193 return 1;
194
ea882baf 195 puts("Writing to NAND... ");
cd0f4fa1 196 ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
e443c944 197 }
cc49cade
SW
198 if (ret) {
199 puts("FAILED!\n");
e443c944 200 return 1;
cc49cade 201 }
e443c944 202
ea882baf
WD
203 puts("done\n");
204
de152b9b 205 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
ea882baf 206
e443c944
MK
207 return ret;
208}
0e8d1586 209#else /* ! CONFIG_ENV_OFFSET_REDUND */
e443c944
MK
210int saveenv(void)
211{
de152b9b 212 int ret = 0;
cd0f4fa1 213 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
ea882baf
WD
214 ssize_t len;
215 char *res;
9e4006bc 216 nand_erase_options_t nand_erase_options;
e093a247 217
3b250ffb 218 memset(&nand_erase_options, 0, sizeof(nand_erase_options));
0e8d1586 219 nand_erase_options.length = CONFIG_ENV_RANGE;
0e8d1586 220 nand_erase_options.offset = CONFIG_ENV_OFFSET;
cc49cade 221
0e8d1586 222 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
cc49cade 223 return 1;
ea882baf 224
cd0f4fa1 225 res = (char *)&env_new->data;
be11235a 226 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
ea882baf
WD
227 if (len < 0) {
228 error("Cannot export environment: errno = %d\n", errno);
229 return 1;
230 }
3801a15f 231 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
ea882baf
WD
232
233 puts("Erasing Nand...\n");
cc49cade 234 if (nand_erase_opts(&nand_info[0], &nand_erase_options))
addb2e16 235 return 1;
13a5695b 236
ea882baf 237 puts("Writing to Nand... ");
3801a15f 238 if (writeenv(CONFIG_ENV_OFFSET, (u_char *)env_new)) {
cc49cade 239 puts("FAILED!\n");
13a5695b 240 return 1;
cc49cade 241 }
13a5695b 242
ea882baf 243 puts("done\n");
addb2e16 244 return ret;
13a5695b 245}
0e8d1586 246#endif /* CONFIG_ENV_OFFSET_REDUND */
13a5695b
WD
247#endif /* CMD_SAVEENV */
248
de152b9b 249int readenv(size_t offset, u_char *buf)
cc49cade 250{
0e8d1586 251 size_t end = offset + CONFIG_ENV_RANGE;
cc49cade 252 size_t amount_loaded = 0;
c3db8c64 253 size_t blocksize, len;
cc49cade
SW
254 u_char *char_ptr;
255
256 blocksize = nand_info[0].erasesize;
962ad59e
MF
257 if (!blocksize)
258 return 1;
de152b9b 259
0e8d1586 260 len = min(blocksize, CONFIG_ENV_SIZE);
cc49cade 261
0e8d1586 262 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
cc49cade
SW
263 if (nand_block_isbad(&nand_info[0], offset)) {
264 offset += blocksize;
265 } else {
266 char_ptr = &buf[amount_loaded];
de152b9b 267 if (nand_read_skip_bad(&nand_info[0], offset,
c39d6a0e
TR
268 &len, NULL,
269 nand_info[0].size, char_ptr))
cc49cade 270 return 1;
de152b9b 271
cc49cade 272 offset += blocksize;
c3db8c64 273 amount_loaded += len;
cc49cade
SW
274 }
275 }
de152b9b 276
0e8d1586 277 if (amount_loaded != CONFIG_ENV_SIZE)
cc49cade
SW
278 return 1;
279
280 return 0;
281}
282
c9f7351b
BG
283#ifdef CONFIG_ENV_OFFSET_OOB
284int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
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
BG
295
296 ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
53504a27
SW
297 if (ret) {
298 printf("error reading OOB block 0\n");
299 return ret;
300 }
c9f7351b 301
53504a27
SW
302 if (oob_buf[0] == ENV_OOB_MARKER) {
303 *result = oob_buf[1] * nand->erasesize;
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
ea882baf 316void env_relocate_spec(void)
e443c944
MK
317{
318#if !defined(ENV_IS_EMBEDDED)
b76a147b 319 int read1_fail = 0, read2_fail = 0;
2770bcb2 320 int crc1_ok = 0, crc2_ok = 0;
ea882baf 321 env_t *ep, *tmp_env1, *tmp_env2;
e443c944 322
ea882baf
WD
323 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
324 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
de152b9b 325 if (tmp_env1 == NULL || tmp_env2 == NULL) {
15b86c3d 326 puts("Can't allocate buffers for environment\n");
ea882baf 327 set_default_env("!malloc() failed");
de152b9b 328 goto done;
15b86c3d
WD
329 }
330
b76a147b
PS
331 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
332 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
ea882baf 333
b76a147b
PS
334 if (read1_fail && read2_fail)
335 puts("*** Error - No Valid Environment Area found\n");
336 else if (read1_fail || read2_fail)
337 puts("*** Warning - some problems detected "
338 "reading environment; recovered successfully\n");
e443c944 339
a1eac57a
PS
340 crc1_ok = !read1_fail &&
341 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
342 crc2_ok = !read2_fail &&
343 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
e443c944 344
ea882baf 345 if (!crc1_ok && !crc2_ok) {
ea882baf 346 set_default_env("!bad CRC");
de152b9b 347 goto done;
ea882baf 348 } else if (crc1_ok && !crc2_ok) {
e443c944 349 gd->env_valid = 1;
ea882baf 350 } else if (!crc1_ok && crc2_ok) {
e443c944 351 gd->env_valid = 2;
ea882baf 352 } else {
e443c944 353 /* both ok - check serial */
ea882baf 354 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
e443c944 355 gd->env_valid = 2;
ea882baf 356 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
e443c944 357 gd->env_valid = 1;
ea882baf 358 else if (tmp_env1->flags > tmp_env2->flags)
e443c944 359 gd->env_valid = 1;
ea882baf 360 else if (tmp_env2->flags > tmp_env1->flags)
e443c944
MK
361 gd->env_valid = 2;
362 else /* flags are equal - almost impossible */
363 gd->env_valid = 1;
e443c944
MK
364 }
365
366 free(env_ptr);
ea882baf
WD
367
368 if (gd->env_valid == 1)
369 ep = tmp_env1;
370 else
371 ep = tmp_env2;
372
eef1d719 373 env_flags = ep->flags;
ea882baf
WD
374 env_import((char *)ep, 0);
375
de152b9b 376done:
ea882baf
WD
377 free(tmp_env1);
378 free(tmp_env2);
e443c944
MK
379
380#endif /* ! ENV_IS_EMBEDDED */
381}
0e8d1586 382#else /* ! CONFIG_ENV_OFFSET_REDUND */
addb2e16 383/*
ea882baf
WD
384 * The legacy NAND code saved the environment in the first NAND
385 * device i.e., nand_dev_desc + 0. This is also the behaviour using
386 * the new NAND code.
addb2e16 387 */
de152b9b 388void env_relocate_spec(void)
13a5695b
WD
389{
390#if !defined(ENV_IS_EMBEDDED)
d52fb7e3 391 int ret;
cd0f4fa1 392 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
13a5695b 393
c9f7351b
BG
394#if defined(CONFIG_ENV_OFFSET_OOB)
395 ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
ea882baf
WD
396 /*
397 * If unable to read environment offset from NAND OOB then fall through
c9f7351b
BG
398 * to the normal environment reading code below
399 */
ea882baf 400 if (!ret) {
c9f7351b 401 printf("Found Environment offset in OOB..\n");
ea882baf
WD
402 } else {
403 set_default_env("!no env offset in OOB");
404 return;
405 }
c9f7351b
BG
406#endif
407
cd0f4fa1 408 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
ea882baf
WD
409 if (ret) {
410 set_default_env("!readenv() failed");
411 return;
412 }
13a5695b 413
cd0f4fa1 414 env_import(buf, 1);
13a5695b 415#endif /* ! ENV_IS_EMBEDDED */
13a5695b 416}
0e8d1586 417#endif /* CONFIG_ENV_OFFSET_REDUND */