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 #if defined(ENV_IS_EMBEDDED)
37 static env_t *env_ptr = &environment;
38 #elif defined(CONFIG_NAND_ENV_DST)
39 static env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
40 #endif /* ENV_IS_EMBEDDED */
42 DECLARE_GLOBAL_DATA_PTR;
45 * This is called before nand_init() so we can't read NAND to
48 * Mark it OK for now. env_relocate() in env_common.c will call our
49 * relocate function which does the real validation.
51 * When using a NAND boot image (like sequoia_nand), the environment
52 * can be embedded or attached to the U-Boot image in NAND flash.
53 * This way the SPL loads not only the U-Boot image from NAND but
54 * also the environment.
56 static int env_nand_init(void)
58 #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
59 int crc1_ok = 0, crc2_ok = 0;
62 #ifdef CONFIG_ENV_OFFSET_REDUND
65 tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
66 crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
69 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
71 if (!crc1_ok && !crc2_ok) {
73 gd->env_valid = ENV_INVALID;
76 } else if (crc1_ok && !crc2_ok) {
77 gd->env_valid = ENV_VALID;
79 #ifdef CONFIG_ENV_OFFSET_REDUND
80 else if (!crc1_ok && crc2_ok) {
81 gd->env_valid = ENV_REDUND;
83 /* both ok - check serial */
84 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
85 gd->env_valid = ENV_REDUND;
86 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
87 gd->env_valid = ENV_VALID;
88 else if (tmp_env1->flags > tmp_env2->flags)
89 gd->env_valid = ENV_VALID;
90 else if (tmp_env2->flags > tmp_env1->flags)
91 gd->env_valid = ENV_REDUND;
92 else /* flags are equal - almost impossible */
93 gd->env_valid = ENV_VALID;
96 if (gd->env_valid == ENV_REDUND)
100 if (gd->env_valid == ENV_VALID)
103 gd->env_addr = (ulong)env_ptr->data;
105 #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
106 gd->env_valid = ENV_INVALID;
107 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
114 * The legacy NAND code saved the environment in the first NAND device i.e.,
115 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
117 static int writeenv(size_t offset, u_char *buf)
119 size_t end = offset + CONFIG_ENV_RANGE;
120 size_t amount_saved = 0;
121 size_t blocksize, len;
122 struct mtd_info *mtd;
125 mtd = get_nand_dev_by_index(0);
129 blocksize = mtd->erasesize;
130 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
132 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
133 if (nand_block_isbad(mtd, offset)) {
136 char_ptr = &buf[amount_saved];
137 if (nand_write(mtd, offset, &len, char_ptr))
144 if (amount_saved != CONFIG_ENV_SIZE)
150 struct nand_env_location {
152 const nand_erase_options_t erase_opts;
155 static int erase_and_write_env(const struct nand_env_location *location,
158 struct mtd_info *mtd;
161 mtd = get_nand_dev_by_index(0);
165 printf("Erasing %s...\n", location->name);
166 if (nand_erase_opts(mtd, &location->erase_opts))
169 printf("Writing to %s... ", location->name);
170 ret = writeenv(location->erase_opts.offset, env_new);
171 puts(ret ? "FAILED!\n" : "OK\n");
176 static int env_nand_save(void)
179 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
181 static const struct nand_env_location location[] = {
185 .length = CONFIG_ENV_RANGE,
186 .offset = CONFIG_ENV_OFFSET,
189 #ifdef CONFIG_ENV_OFFSET_REDUND
191 .name = "redundant NAND",
193 .length = CONFIG_ENV_RANGE,
194 .offset = CONFIG_ENV_OFFSET_REDUND,
200 ret = env_export(env_new);
204 #ifdef CONFIG_ENV_OFFSET_REDUND
205 env_idx = (gd->env_valid == ENV_VALID);
208 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
209 #ifdef CONFIG_ENV_OFFSET_REDUND
211 /* preset other copy for next write */
212 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
217 env_idx = (env_idx + 1) & 1;
218 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
220 printf("Warning: primary env write failed,"
221 " redundancy is lost!\n");
226 #endif /* CMD_SAVEENV */
228 #if defined(CONFIG_SPL_BUILD)
229 static int readenv(size_t offset, u_char *buf)
231 return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
234 static int readenv(size_t offset, u_char *buf)
236 size_t end = offset + CONFIG_ENV_RANGE;
237 size_t amount_loaded = 0;
238 size_t blocksize, len;
239 struct mtd_info *mtd;
242 mtd = get_nand_dev_by_index(0);
246 blocksize = mtd->erasesize;
247 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
249 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
250 if (nand_block_isbad(mtd, offset)) {
253 char_ptr = &buf[amount_loaded];
254 if (nand_read_skip_bad(mtd, offset,
256 mtd->size, char_ptr))
260 amount_loaded += len;
264 if (amount_loaded != CONFIG_ENV_SIZE)
269 #endif /* #if defined(CONFIG_SPL_BUILD) */
271 #ifdef CONFIG_ENV_OFFSET_OOB
272 int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
274 struct mtd_oob_ops ops;
275 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
279 ops.mode = MTD_OOB_AUTO;
281 ops.ooblen = ENV_OFFSET_SIZE;
282 ops.oobbuf = (void *)oob_buf;
284 ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
286 printf("error reading OOB block 0\n");
290 if (oob_buf[0] == ENV_OOB_MARKER) {
291 *result = ovoid ob_buf[1] * mtd->erasesize;
292 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
293 *result = oob_buf[1];
295 printf("No dynamic environment marker in OOB block 0\n");
303 #ifdef CONFIG_ENV_OFFSET_REDUND
304 static int env_nand_load(void)
306 #if defined(ENV_IS_EMBEDDED)
309 int read1_fail, read2_fail;
310 env_t *tmp_env1, *tmp_env2;
313 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
314 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
315 if (tmp_env1 == NULL || tmp_env2 == NULL) {
316 puts("Can't allocate buffers for environment\n");
317 env_set_default("malloc() failed", 0);
322 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
323 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
325 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
326 read2_fail, H_EXTERNAL);
333 #endif /* ! ENV_IS_EMBEDDED */
335 #else /* ! CONFIG_ENV_OFFSET_REDUND */
337 * The legacy NAND code saved the environment in the first NAND
338 * device i.e., nand_dev_desc + 0. This is also the behaviour using
341 static int env_nand_load(void)
343 #if !defined(ENV_IS_EMBEDDED)
345 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
347 #if defined(CONFIG_ENV_OFFSET_OOB)
348 struct mtd_info *mtd = get_nand_dev_by_index(0);
350 * If unable to read environment offset from NAND OOB then fall through
351 * to the normal environment reading code below
353 if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
354 printf("Found Environment offset in OOB..\n");
356 env_set_default("no env offset in OOB", 0);
361 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
363 env_set_default("readenv() failed", 0);
367 return env_import(buf, 1, H_EXTERNAL);
368 #endif /* ! ENV_IS_EMBEDDED */
372 #endif /* CONFIG_ENV_OFFSET_REDUND */
374 U_BOOT_ENV_LOCATION(nand) = {
375 .location = ENVL_NAND,
377 .load = env_nand_load,
378 #if defined(CMD_SAVEENV)
379 .save = env_save_ptr(env_nand_save),
381 .init = env_nand_init,