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_valid = ENV_INVALID;
111 #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
118 * The legacy NAND code saved the environment in the first NAND device i.e.,
119 * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
121 static int writeenv(size_t offset, u_char *buf)
123 size_t end = offset + CONFIG_ENV_RANGE;
124 size_t amount_saved = 0;
125 size_t blocksize, len;
126 struct mtd_info *mtd;
129 mtd = get_nand_dev_by_index(0);
133 blocksize = mtd->erasesize;
134 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
136 while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
137 if (nand_block_isbad(mtd, offset)) {
140 char_ptr = &buf[amount_saved];
141 if (nand_write(mtd, offset, &len, char_ptr))
148 if (amount_saved != CONFIG_ENV_SIZE)
154 struct nand_env_location {
156 const nand_erase_options_t erase_opts;
159 static int erase_and_write_env(const struct nand_env_location *location,
162 struct mtd_info *mtd;
165 mtd = get_nand_dev_by_index(0);
169 printf("Erasing %s...\n", location->name);
170 if (nand_erase_opts(mtd, &location->erase_opts))
173 printf("Writing to %s... ", location->name);
174 ret = writeenv(location->erase_opts.offset, env_new);
175 puts(ret ? "FAILED!\n" : "OK\n");
180 static int env_nand_save(void)
183 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
185 static const struct nand_env_location location[] = {
189 .length = CONFIG_ENV_RANGE,
190 .offset = CONFIG_ENV_OFFSET,
193 #ifdef CONFIG_ENV_OFFSET_REDUND
195 .name = "redundant NAND",
197 .length = CONFIG_ENV_RANGE,
198 .offset = CONFIG_ENV_OFFSET_REDUND,
205 if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
208 ret = env_export(env_new);
212 #ifdef CONFIG_ENV_OFFSET_REDUND
213 env_idx = (gd->env_valid == ENV_VALID);
216 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
217 #ifdef CONFIG_ENV_OFFSET_REDUND
219 /* preset other copy for next write */
220 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
225 env_idx = (env_idx + 1) & 1;
226 ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
228 printf("Warning: primary env write failed,"
229 " redundancy is lost!\n");
234 #endif /* CMD_SAVEENV */
236 #if defined(CONFIG_SPL_BUILD)
237 static int readenv(size_t offset, u_char *buf)
239 return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
242 static int readenv(size_t offset, u_char *buf)
244 size_t end = offset + CONFIG_ENV_RANGE;
245 size_t amount_loaded = 0;
246 size_t blocksize, len;
247 struct mtd_info *mtd;
250 mtd = get_nand_dev_by_index(0);
254 blocksize = mtd->erasesize;
255 len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
257 while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
258 if (nand_block_isbad(mtd, offset)) {
261 char_ptr = &buf[amount_loaded];
262 if (nand_read_skip_bad(mtd, offset,
264 mtd->size, char_ptr))
268 amount_loaded += len;
272 if (amount_loaded != CONFIG_ENV_SIZE)
277 #endif /* #if defined(CONFIG_SPL_BUILD) */
279 #ifdef CONFIG_ENV_OFFSET_OOB
280 int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
282 struct mtd_oob_ops ops;
283 uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
287 ops.mode = MTD_OOB_AUTO;
289 ops.ooblen = ENV_OFFSET_SIZE;
290 ops.oobbuf = (void *)oob_buf;
292 ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
294 printf("error reading OOB block 0\n");
298 if (oob_buf[0] == ENV_OOB_MARKER) {
299 *result = ovoid ob_buf[1] * mtd->erasesize;
300 } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
301 *result = oob_buf[1];
303 printf("No dynamic environment marker in OOB block 0\n");
311 #ifdef CONFIG_ENV_OFFSET_REDUND
312 static int env_nand_load(void)
314 #if defined(ENV_IS_EMBEDDED)
317 int read1_fail, read2_fail;
318 env_t *tmp_env1, *tmp_env2;
321 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
322 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
323 if (tmp_env1 == NULL || tmp_env2 == NULL) {
324 puts("Can't allocate buffers for environment\n");
325 env_set_default("malloc() failed", 0);
330 read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
331 read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
333 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
334 read2_fail, H_EXTERNAL);
341 #endif /* ! ENV_IS_EMBEDDED */
343 #else /* ! CONFIG_ENV_OFFSET_REDUND */
345 * The legacy NAND code saved the environment in the first NAND
346 * device i.e., nand_dev_desc + 0. This is also the behaviour using
349 static int env_nand_load(void)
351 #if !defined(ENV_IS_EMBEDDED)
353 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
355 #if defined(CONFIG_ENV_OFFSET_OOB)
356 struct mtd_info *mtd = get_nand_dev_by_index(0);
358 * If unable to read environment offset from NAND OOB then fall through
359 * to the normal environment reading code below
361 if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
362 printf("Found Environment offset in OOB..\n");
364 env_set_default("no env offset in OOB", 0);
369 ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
371 env_set_default("readenv() failed", 0);
375 return env_import(buf, 1, H_EXTERNAL);
376 #endif /* ! ENV_IS_EMBEDDED */
380 #endif /* CONFIG_ENV_OFFSET_REDUND */
382 U_BOOT_ENV_LOCATION(nand) = {
383 .location = ENVL_NAND,
385 .load = env_nand_load,
386 #if defined(CMD_SAVEENV)
387 .save = env_save_ptr(env_nand_save),
389 .init = env_nand_init,