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>
19 #include <env_internal.h>
20 #include <asm/global_data.h>
21 #include <linux/stddef.h>
27 #include <u-boot/crc.h>
29 #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) && \
30 !defined(CONFIG_SPL_BUILD)
32 #elif defined(CONFIG_ENV_OFFSET_REDUND) && !defined(CONFIG_SPL_BUILD)
33 #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
36 #ifndef CONFIG_ENV_RANGE
37 #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
40 #if defined(ENV_IS_EMBEDDED)
41 static env_t *env_ptr = &environment;
42 #elif defined(CONFIG_NAND_ENV_DST)
43 static env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
44 #endif /* ENV_IS_EMBEDDED */
46 DECLARE_GLOBAL_DATA_PTR;
49 * This is called before nand_init() so we can't read NAND to
52 * Mark it OK for now. env_relocate() in env_common.c will call our
53 * relocate function which does the real validation.
55 * When using a NAND boot image (like sequoia_nand), the environment
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.
60 static int env_nand_init(void)
62 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
63 int crc1_ok = 0, crc2_ok = 0;
66 #ifdef CONFIG_ENV_OFFSET_REDUND
69 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
70 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
73 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
75 if (!crc1_ok && !crc2_ok) {
77 gd->env_valid = ENV_INVALID;
80 } else if (crc1_ok && !crc2_ok) {
81 gd->env_valid = ENV_VALID;
83 #ifdef CONFIG_ENV_OFFSET_REDUND
84 else if (!crc1_ok && crc2_ok) {
85 gd->env_valid = ENV_REDUND;
87 /* both ok - check serial */
88 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
89 gd->env_valid = ENV_REDUND;
90 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
91 gd->env_valid = ENV_VALID;
92 else if (tmp_env1->flags > tmp_env2->flags)
93 gd->env_valid = ENV_VALID;
94 else if (tmp_env2->flags > tmp_env1->flags)
95 gd->env_valid = ENV_REDUND;
96 else /* flags are equal - almost impossible */
97 gd->env_valid = ENV_VALID;
100 if (gd->env_valid == ENV_REDUND)
104 if (gd->env_valid == ENV_VALID)
107 gd->env_addr = (ulong)env_ptr->data;
109 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
110 gd->env_addr = (ulong)&default_environment[0];
111 gd->env_valid = ENV_VALID;
112 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
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.
122 static int writeenv(size_t offset, u_char *buf)
124 size_t end = offset + CONFIG_ENV_RANGE;
125 size_t amount_saved = 0;
126 size_t blocksize, len;
127 struct mtd_info *mtd;
130 mtd = get_nand_dev_by_index(0);
134 blocksize = mtd->erasesize;
135 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
137 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
138 if (nand_block_isbad(mtd, offset)) {
141 char_ptr = &buf[amount_saved];
142 if (nand_write(mtd, offset, &len, char_ptr))
149 if (amount_saved != CONFIG_ENV_SIZE)
155 struct nand_env_location {
157 const nand_erase_options_t erase_opts;
160 static int erase_and_write_env(const struct nand_env_location *location,
163 struct mtd_info *mtd;
166 mtd = get_nand_dev_by_index(0);
170 printf("Erasing %s...\n", location->name);
171 if (nand_erase_opts(mtd, &location->erase_opts))
174 printf("Writing to %s... ", location->name);
175 ret = writeenv(location->erase_opts.offset, env_new);
176 puts(ret ? "FAILED!\n" : "OK\n");
181 static int env_nand_save(void)
184 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
186 static const struct nand_env_location location[] = {
190 .length = CONFIG_ENV_RANGE,
191 .offset = CONFIG_ENV_OFFSET,
194 #ifdef CONFIG_ENV_OFFSET_REDUND
196 .name = "redundant NAND",
198 .length = CONFIG_ENV_RANGE,
199 .offset = CONFIG_ENV_OFFSET_REDUND,
206 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
209 ret = env_export(env_new);
213 #ifdef CONFIG_ENV_OFFSET_REDUND
214 env_idx = (gd->env_valid == ENV_VALID);
217 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
218 #ifdef CONFIG_ENV_OFFSET_REDUND
220 /* preset other copy for next write */
221 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
226 env_idx = (env_idx + 1) & 1;
227 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
229 printf("Warning: primary env write failed,"
230 " redundancy is lost!\n");
235 #endif /* CMD_SAVEENV */
237 #if defined(CONFIG_SPL_BUILD)
238 static int readenv(size_t offset, u_char *buf)
240 return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
243 static int readenv(size_t offset, u_char *buf)
245 size_t end = offset + CONFIG_ENV_RANGE;
246 size_t amount_loaded = 0;
247 size_t blocksize, len;
248 struct mtd_info *mtd;
251 mtd = get_nand_dev_by_index(0);
255 blocksize = mtd->erasesize;
256 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
258 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
259 if (nand_block_isbad(mtd, offset)) {
262 char_ptr = &buf[amount_loaded];
263 if (nand_read_skip_bad(mtd, offset,
265 mtd->size, char_ptr))
269 amount_loaded += len;
273 if (amount_loaded != CONFIG_ENV_SIZE)
278 #endif /* #if defined(CONFIG_SPL_BUILD) */
280 #ifdef CONFIG_ENV_OFFSET_OOB
281 int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
283 struct mtd_oob_ops ops;
284 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
288 ops.mode = MTD_OOB_AUTO;
290 ops.ooblen = ENV_OFFSET_SIZE;
291 ops.oobbuf = (void *)oob_buf;
293 ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
295 printf("error reading OOB block 0\n");
299 if (oob_buf[0] == ENV_OOB_MARKER) {
300 *result = ovoid ob_buf[1] * mtd->erasesize;
301 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
302 *result = oob_buf[1];
304 printf("No dynamic environment marker in OOB block 0\n");
312 #ifdef CONFIG_ENV_OFFSET_REDUND
313 static int env_nand_load(void)
315 #if defined(ENV_IS_EMBEDDED)
318 int read1_fail, read2_fail;
319 env_t *tmp_env1, *tmp_env2;
322 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
323 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
324 if (tmp_env1 == NULL || tmp_env2 == NULL) {
325 puts("Can't allocate buffers for environment\n");
326 env_set_default("malloc() failed", 0);
331 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
332 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
334 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
335 read2_fail, H_EXTERNAL);
342 #endif /* ! ENV_IS_EMBEDDED */
344 #else /* ! CONFIG_ENV_OFFSET_REDUND */
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
350 static int env_nand_load(void)
352 #if !defined(ENV_IS_EMBEDDED)
354 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
356 #if defined(CONFIG_ENV_OFFSET_OOB)
357 struct mtd_info *mtd = get_nand_dev_by_index(0);
359 * If unable to read environment offset from NAND OOB then fall through
360 * to the normal environment reading code below
362 if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
363 printf("Found Environment offset in OOB..\n");
365 env_set_default("no env offset in OOB", 0);
370 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
372 env_set_default("readenv() failed", 0);
376 return env_import(buf, 1, H_EXTERNAL);
377 #endif /* ! ENV_IS_EMBEDDED */
381 #endif /* CONFIG_ENV_OFFSET_REDUND */
383 U_BOOT_ENV_LOCATION(nand) = {
384 .location = ENVL_NAND,
386 .load = env_nand_load,
387 #if defined(CMD_SAVEENV)
388 .save = env_save_ptr(env_nand_save),
390 .init = env_nand_init,