| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- // SPDX-License-Identifier: GPL-2.0+
- /*
- * (C) Copyright 2000-2010
- * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * (C) Copyright 2008
- * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
- *
- * (C) Copyright 2004
- * Jian Zhang, Texas Instruments, jzhang@ti.com.
- *
- * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
- * Andreas Heppel <aheppel@sysgo.de>
- */
- #include <common.h>
- #include <command.h>
- #include <env.h>
- #include <env_internal.h>
- #include <asm/global_data.h>
- #include <linux/stddef.h>
- #include <malloc.h>
- #include <memalign.h>
- #include <nand.h>
- #include <search.h>
- #include <errno.h>
- #include <u-boot/crc.h>
- #include <mtd.h>
- #if defined(ENV_IS_EMBEDDED)
- static env_t *env_ptr = &environment;
- #endif /* ENV_IS_EMBEDDED */
- DECLARE_GLOBAL_DATA_PTR;
- static int setup_mtd_device(struct mtd_info **mtd_env)
- {
- struct mtd_info *mtd;
- mtd_probe_devices();
- mtd = get_mtd_device_nm(CONFIG_SYS_SNAND_ENV_DEV);
- if (IS_ERR_OR_NULL(mtd)) {
- env_set_default("get_mtd_device_nm() failed", 0);
- return mtd ? PTR_ERR(mtd) : -EINVAL;
- }
- *mtd_env = mtd;
- return 0;
- }
- /*
- * This is called before nand_init() so we can't read NAND to
- * validate env data.
- *
- * Mark it OK for now. env_relocate() in env_common.c will call our
- * relocate function which does the real validation.
- *
- * When using a NAND boot image (like sequoia_nand), the environment
- * can be embedded or attached to the U-Boot image in NAND flash.
- * This way the SPL loads not only the U-Boot image from NAND but
- * also the environment.
- */
- static int env_spinand_init(void)
- {
- #if defined(ENV_IS_EMBEDDED)
- int crc1_ok = 0, crc2_ok = 0;
- env_t *tmp_env1;
- #ifdef CONFIG_ENV_OFFSET_REDUND
- env_t *tmp_env2;
- tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
- crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
- #endif
- tmp_env1 = env_ptr;
- crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
- if (!crc1_ok && !crc2_ok) {
- gd->env_addr = 0;
- gd->env_valid = ENV_INVALID;
- return 0;
- } else if (crc1_ok && !crc2_ok) {
- gd->env_valid = ENV_VALID;
- }
- #ifdef CONFIG_ENV_OFFSET_REDUND
- else if (!crc1_ok && crc2_ok) {
- gd->env_valid = ENV_REDUND;
- } else {
- /* both ok - check serial */
- if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
- gd->env_valid = ENV_REDUND;
- else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
- gd->env_valid = ENV_VALID;
- else if (tmp_env1->flags > tmp_env2->flags)
- gd->env_valid = ENV_VALID;
- else if (tmp_env2->flags > tmp_env1->flags)
- gd->env_valid = ENV_REDUND;
- else /* flags are equal - almost impossible */
- gd->env_valid = ENV_VALID;
- }
- if (gd->env_valid == ENV_REDUND)
- env_ptr = tmp_env2;
- else
- #endif
- if (gd->env_valid == ENV_VALID)
- env_ptr = tmp_env1;
- gd->env_addr = (ulong)env_ptr->data;
- #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
- gd->env_valid = ENV_INVALID;
- #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
- return 0;
- }
- struct spinand_erase_options {
- loff_t length; /* number of bytes to erase */
- loff_t offset; /* first address in SPINAND to erase */
- };
- struct spinand_env_location {
- const char *name;
- const struct spinand_erase_options erase_opts;
- };
- static int spinandwriteenv(size_t offset, u_char *buf)
- {
- size_t end = offset + CONFIG_ENV_RANGE;
- size_t amount_saved = 0;
- size_t blocksize, len;
- struct mtd_info *mtd;
- u_char *char_ptr;
- int ret;
- ret = setup_mtd_device(&mtd);
- if (ret)
- return ret;
- blocksize = mtd->erasesize;
- len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
- while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
- if (mtd_block_isbad(mtd, offset)) {
- offset += blocksize;
- } else {
- char_ptr = &buf[amount_saved];
- if (mtd_write(mtd, offset, mtd->writesize, &len,
- char_ptr))
- return 1;
- offset += len;
- amount_saved += len;
- }
- }
- if (amount_saved != CONFIG_ENV_SIZE)
- return 1;
- return 0;
- }
- int spinand_erase_opts(struct mtd_info *mtd_env,
- const struct spinand_erase_options *opts)
- {
- u32 sect_size, sect_num;
- struct erase_info ei;
- int ret;
- sect_size = mtd_env->erasesize;
- sect_num = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
- ei.mtd = mtd_env;
- ei.addr = opts->offset;
- ei.len = sect_num * sect_size;
- ret = mtd_erase(mtd_env, &ei);
- if (ret)
- goto done;
- return 0;
- done:
- return ret;
- }
- static int env_spinand_erase_save(const struct spinand_env_location *location, u_char *env_new)
- {
- struct mtd_info *mtd_env;
- int ret = 0;
- ret = setup_mtd_device(&mtd_env);
- if (ret)
- return ret;
- printf("Erasing %s...\n", location->name);
- if (spinand_erase_opts(mtd_env, &location->erase_opts))
- return 1;
- printf("Writing to %s... ", location->name);
- ret = spinandwriteenv(location->erase_opts.offset, env_new);
- puts(ret ? "FAILED!\n" : "OK\n");
- return ret;
- }
- static int env_spinand_save(void)
- {
- int ret = 0;
- ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
- int env_idx = 0;
- static const struct spinand_env_location location[] = {
- {
- .name = "SPINAND",
- .erase_opts = {
- .length = CONFIG_ENV_RANGE,
- .offset = CONFIG_ENV_OFFSET,
- },
- },
- #ifdef CONFIG_ENV_OFFSET_REDUND
- {
- .name = "redundant SPINAND",
- .erase_opts = {
- .length = CONFIG_ENV_RANGE,
- .offset = CONFIG_ENV_OFFSET_REDUND,
- },
- },
- #endif
- };
- ret = env_export(env_new);
- if (ret)
- return ret;
- #ifdef CONFIG_ENV_OFFSET_REDUND
- env_idx = (gd->env_valid == ENV_VALID);
- #endif
- ret = env_spinand_erase_save(&location[env_idx], (u_char *)env_new);
- #ifdef CONFIG_ENV_OFFSET_REDUND
- if (!ret) {
- /* preset other copy for next write */
- gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
- ENV_REDUND;
- return ret;
- }
- env_idx = (env_idx + 1) & 1;
- ret = env_spinand_erase_save(&location[env_idx], (u_char *)env_new);
- if (!ret)
- printf("Warning: primary env write failed, redundancy is lost!\n");
- #endif
- return ret;
- }
- static int spinandreadenv(size_t offset, u_char *buf)
- {
- size_t end = offset + CONFIG_ENV_RANGE;
- size_t amount_loaded = 0;
- size_t blocksize, len;
- struct mtd_info *mtd_env;
- u_char *char_ptr;
- size_t ret_len;
- int ret;
- ret = setup_mtd_device(&mtd_env);
- if (ret)
- return 1;
- blocksize = mtd_env->erasesize;
- len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
- while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
- if (mtd_block_isbad(mtd_env, offset)) {
- offset += blocksize;
- } else {
- char_ptr = &buf[amount_loaded];
- if (mtd_read(mtd_env, offset, mtd_env->writesize,
- &ret_len, char_ptr))
- return 1;
- offset += ret_len;
- amount_loaded += ret_len;
- }
- }
- if (amount_loaded != CONFIG_ENV_SIZE)
- return 1;
- return 0;
- }
- static int env_spinand_load(void)
- {
- int read1_fail, read2_fail;
- env_t *tmp_env1, *tmp_env2;
- int ret = 0;
- tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
- if (!tmp_env1) {
- env_set_default("tmp_env1 malloc() failed", 0);
- return -EIO;
- }
- tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
- if (!tmp_env2) {
- env_set_default("tmp_env2 malloc() failed", 0);
- return -EIO;
- }
- if (tmp_env1 == NULL || tmp_env2 == NULL) {
- puts("Can't allocate buffers for environment\n");
- env_set_default("malloc() failed", 0);
- ret = -EIO;
- goto done;
- }
- read1_fail = spinandreadenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
- read2_fail = spinandreadenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
- ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
- read2_fail, H_EXTERNAL);
- done:
- free(tmp_env1);
- free(tmp_env2);
- return ret;
- }
- U_BOOT_ENV_LOCATION(snand) = {
- .location = ENVL_SPI_NAND_FLASH,
- ENV_NAME("SPINAND")
- .load = env_spinand_load,
- .save = env_save_ptr(env_spinand_save),
- .init = env_spinand_init,
- };
|