1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
10 * Jian Zhang, Texas Instruments, jzhang@ti.com.
12 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Andreas Heppel <aheppel@sysgo.de>
18 #include <environment.h>
19 #include <linux/stddef.h>
26 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) && \
27 !defined(CONFIG_SPL_BUILD)
29 #elif defined(CONFIG_ENV_OFFSET_REDUND)
30 #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
33 #if defined(CONFIG_ENV_SIZE_REDUND) && \
34 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
35 #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
38 #ifndef CONFIG_ENV_RANGE
39 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
42 #if defined(ENV_IS_EMBEDDED)
43 env_t *env_ptr = &environment;
44 #elif defined(CONFIG_NAND_ENV_DST)
45 env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
46 #else /* ! ENV_IS_EMBEDDED */
48 #endif /* ENV_IS_EMBEDDED */
50 DECLARE_GLOBAL_DATA_PTR;
53 * This is called before nand_init() so we can't read NAND to
56 * Mark it OK for now. env_relocate() in env_common.c will call our
57 * relocate function which does the real validation.
59 * When using a NAND boot image (like sequoia_nand), the environment
60 * can be embedded or attached to the U-Boot image in NAND flash.
61 * This way the SPL loads not only the U-Boot image from NAND but
62 * also the environment.
64 static int env_nand_init(void)
66 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
67 int crc1_ok = 0, crc2_ok = 0;
70 #ifdef CONFIG_ENV_OFFSET_REDUND
73 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
74 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
77 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
79 if (!crc1_ok && !crc2_ok) {
81 gd->env_valid = ENV_INVALID;
84 } else if (crc1_ok && !crc2_ok) {
85 gd->env_valid = ENV_VALID;
87 #ifdef CONFIG_ENV_OFFSET_REDUND
88 else if (!crc1_ok && crc2_ok) {
89 gd->env_valid = ENV_REDUND;
91 /* both ok - check serial */
92 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
93 gd->env_valid = ENV_REDUND;
94 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
95 gd->env_valid = ENV_VALID;
96 else if (tmp_env1->flags > tmp_env2->flags)
97 gd->env_valid = ENV_VALID;
98 else if (tmp_env2->flags > tmp_env1->flags)
99 gd->env_valid = ENV_REDUND;
100 else /* flags are equal - almost impossible */
101 gd->env_valid = ENV_VALID;
104 if (gd->env_valid == ENV_REDUND)
108 if (gd->env_valid == ENV_VALID)
111 gd->env_addr = (ulong)env_ptr->data;
113 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
114 gd->env_addr = (ulong)&default_environment[0];
115 gd->env_valid = ENV_VALID;
116 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
123 * The legacy NAND code saved the environment in the first NAND device i.e.,
124 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
126 static int writeenv(size_t offset, u_char *buf)
128 size_t end = offset + CONFIG_ENV_RANGE;
129 size_t amount_saved = 0;
130 size_t blocksize, len;
131 struct mtd_info *mtd;
134 mtd = get_nand_dev_by_index(0);
138 blocksize = mtd->erasesize;
139 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
141 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
142 if (nand_block_isbad(mtd, offset)) {
145 char_ptr = &buf[amount_saved];
146 if (nand_write(mtd, offset, &len, char_ptr))
153 if (amount_saved != CONFIG_ENV_SIZE)
159 struct nand_env_location {
161 const nand_erase_options_t erase_opts;
164 static int erase_and_write_env(const struct nand_env_location *location,
167 struct mtd_info *mtd;
170 mtd = get_nand_dev_by_index(0);
174 printf("Erasing %s...\n", location->name);
175 if (nand_erase_opts(mtd, &location->erase_opts))
178 printf("Writing to %s... ", location->name);
179 ret = writeenv(location->erase_opts.offset, env_new);
180 puts(ret ? "FAILED!\n" : "OK\n");
185 static int env_nand_save(void)
188 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
190 static const struct nand_env_location location[] = {
194 .length = CONFIG_ENV_RANGE,
195 .offset = CONFIG_ENV_OFFSET,
198 #ifdef CONFIG_ENV_OFFSET_REDUND
200 .name = "redundant NAND",
202 .length = CONFIG_ENV_RANGE,
203 .offset = CONFIG_ENV_OFFSET_REDUND,
210 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
213 ret = env_export(env_new);
217 #ifdef CONFIG_ENV_OFFSET_REDUND
218 env_idx = (gd->env_valid == ENV_VALID);
221 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
222 #ifdef CONFIG_ENV_OFFSET_REDUND
224 /* preset other copy for next write */
225 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
230 env_idx = (env_idx + 1) & 1;
231 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
233 printf("Warning: primary env write failed,"
234 " redundancy is lost!\n");
239 #endif /* CMD_SAVEENV */
241 #if defined(CONFIG_SPL_BUILD)
242 static int readenv(size_t offset, u_char *buf)
244 return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
247 static int readenv(size_t offset, u_char *buf)
249 size_t end = offset + CONFIG_ENV_RANGE;
250 size_t amount_loaded = 0;
251 size_t blocksize, len;
252 struct mtd_info *mtd;
255 mtd = get_nand_dev_by_index(0);
259 blocksize = mtd->erasesize;
260 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
262 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
263 if (nand_block_isbad(mtd, offset)) {
266 char_ptr = &buf[amount_loaded];
267 if (nand_read_skip_bad(mtd, offset,
269 mtd->size, char_ptr))
273 amount_loaded += len;
277 if (amount_loaded != CONFIG_ENV_SIZE)
282 #endif /* #if defined(CONFIG_SPL_BUILD) */
284 #ifdef CONFIG_ENV_OFFSET_OOB
285 int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
287 struct mtd_oob_ops ops;
288 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
292 ops.mode = MTD_OOB_AUTO;
294 ops.ooblen = ENV_OFFSET_SIZE;
295 ops.oobbuf = (void *)oob_buf;
297 ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
299 printf("error reading OOB block 0\n");
303 if (oob_buf[0] == ENV_OOB_MARKER) {
304 *result = ovoid ob_buf[1] * mtd->erasesize;
305 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
306 *result = oob_buf[1];
308 printf("No dynamic environment marker in OOB block 0\n");
316 #ifdef CONFIG_ENV_OFFSET_REDUND
317 static int env_nand_load(void)
319 #if defined(ENV_IS_EMBEDDED)
322 int read1_fail, read2_fail;
323 env_t *tmp_env1, *tmp_env2;
326 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
327 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
328 if (tmp_env1 == NULL || tmp_env2 == NULL) {
329 puts("Can't allocate buffers for environment\n");
330 set_default_env("!malloc() failed");
335 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
336 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
338 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
346 #endif /* ! ENV_IS_EMBEDDED */
348 #else /* ! CONFIG_ENV_OFFSET_REDUND */
350 * The legacy NAND code saved the environment in the first NAND
351 * device i.e., nand_dev_desc + 0. This is also the behaviour using
354 static int env_nand_load(void)
356 #if !defined(ENV_IS_EMBEDDED)
358 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
360 #if defined(CONFIG_ENV_OFFSET_OOB)
361 struct mtd_info *mtd = get_nand_dev_by_index(0);
363 * If unable to read environment offset from NAND OOB then fall through
364 * to the normal environment reading code below
366 if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
367 printf("Found Environment offset in OOB..\n");
369 set_default_env("!no env offset in OOB");
374 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
376 set_default_env("!readenv() failed");
380 return env_import(buf, 1);
381 #endif /* ! ENV_IS_EMBEDDED */
385 #endif /* CONFIG_ENV_OFFSET_REDUND */
387 U_BOOT_ENV_LOCATION(nand) = {
388 .location = ENVL_NAND,
390 .load = env_nand_load,
391 #if defined(CMD_SAVEENV)
392 .save = env_save_ptr(env_nand_save),
394 .init = env_nand_init,