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