From patchwork Tue Jul 7 18:51:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 240888 List-Id: U-Boot discussion From: marex at denx.de (Marek Vasut) Date: Tue, 7 Jul 2020 20:51:35 +0200 Subject: [PATCH V2 3/7] env: Discern environment coming from external storage In-Reply-To: <20200707185139.2225-1-marex@denx.de> References: <20200707185139.2225-1-marex@denx.de> Message-ID: <20200707185139.2225-3-marex@denx.de> Add another custom environment flag which discerns environment coming from external storage from environment set by U-Boot itself. Signed-off-by: Marek Vasut Reviewed-by: Tom Rini --- V2: New patch --- env/common.c | 13 +++++++------ env/eeprom.c | 2 +- env/ext4.c | 2 +- env/fat.c | 2 +- env/flash.c | 2 +- env/mmc.c | 4 ++-- env/nand.c | 4 ++-- env/nvram.c | 2 +- env/onenand.c | 2 +- env/remote.c | 2 +- env/sata.c | 2 +- env/sf.c | 4 ++-- env/ubi.c | 4 ++-- include/env.h | 7 +++++-- include/search.h | 1 + 15 files changed, 29 insertions(+), 24 deletions(-) diff --git a/env/common.c b/env/common.c index 0db56e610a..ed18378000 100644 --- a/env/common.c +++ b/env/common.c @@ -110,7 +110,7 @@ int env_set_default_vars(int nvars, char * const vars[], int flags) * Check if CRC is valid and (if yes) import the environment. * Note that "buf" may or may not be aligned. */ -int env_import(const char *buf, int check) +int env_import(const char *buf, int check, int flags) { env_t *ep = (env_t *)buf; @@ -125,7 +125,7 @@ int env_import(const char *buf, int check) } } - if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, + if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', flags, 0, 0, NULL)) { gd->flags |= GD_FLG_ENV_READY; return 0; @@ -142,7 +142,8 @@ int env_import(const char *buf, int check) static unsigned char env_flags; int env_import_redund(const char *buf1, int buf1_read_fail, - const char *buf2, int buf2_read_fail) + const char *buf2, int buf2_read_fail, + int flags) { int crc1_ok, crc2_ok; env_t *ep, *tmp_env1, *tmp_env2; @@ -162,10 +163,10 @@ int env_import_redund(const char *buf1, int buf1_read_fail, return -EIO; } else if (!buf1_read_fail && buf2_read_fail) { gd->env_valid = ENV_VALID; - return env_import((char *)tmp_env1, 1); + return env_import((char *)tmp_env1, 1, flags); } else if (buf1_read_fail && !buf2_read_fail) { gd->env_valid = ENV_REDUND; - return env_import((char *)tmp_env2, 1); + return env_import((char *)tmp_env2, 1, flags); } crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == @@ -200,7 +201,7 @@ int env_import_redund(const char *buf1, int buf1_read_fail, ep = tmp_env2; env_flags = ep->flags; - return env_import((char *)ep, 0); + return env_import((char *)ep, 0, flags); } #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ diff --git a/env/eeprom.c b/env/eeprom.c index e8126cfe39..e300470ad0 100644 --- a/env/eeprom.c +++ b/env/eeprom.c @@ -188,7 +188,7 @@ static int env_eeprom_load(void) eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off, (uchar *)buf_env, CONFIG_ENV_SIZE); - return env_import(buf_env, 1); + return env_import(buf_env, 1, H_EXTERNAL); } static int env_eeprom_save(void) diff --git a/env/ext4.c b/env/ext4.c index 8e90bb71b7..b6d38324d1 100644 --- a/env/ext4.c +++ b/env/ext4.c @@ -124,7 +124,7 @@ static int env_ext4_load(void) goto err_env_relocate; } - return env_import(buf, 1); + return env_import(buf, 1, H_EXTERNAL); err_env_relocate: env_set_default(NULL, 0); diff --git a/env/fat.c b/env/fat.c index 35a1955e63..9f66a6d642 100644 --- a/env/fat.c +++ b/env/fat.c @@ -116,7 +116,7 @@ static int env_fat_load(void) goto err_env_relocate; } - return env_import(buf, 1); + return env_import(buf, 1, H_EXTERNAL); err_env_relocate: env_set_default(NULL, 0); diff --git a/env/flash.c b/env/flash.c index 3198147c38..722d5adf8b 100644 --- a/env/flash.c +++ b/env/flash.c @@ -351,7 +351,7 @@ static int env_flash_load(void) "reading environment; recovered successfully\n\n"); #endif /* CONFIG_ENV_ADDR_REDUND */ - return env_import((char *)flash_addr, 1); + return env_import((char *)flash_addr, 1, H_EXTERNAL); } #endif /* LOADENV */ diff --git a/env/mmc.c b/env/mmc.c index a8b661db80..d8fb51ffe6 100644 --- a/env/mmc.c +++ b/env/mmc.c @@ -332,7 +332,7 @@ static int env_mmc_load(void) read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2); ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, - read2_fail); + read2_fail, H_EXTERNAL); fini: fini_mmc_for_env(mmc); @@ -374,7 +374,7 @@ static int env_mmc_load(void) goto fini; } - ret = env_import(buf, 1); + ret = env_import(buf, 1, H_EXTERNAL); if (!ret) { ep = (env_t *)buf; gd->env_addr = (ulong)&ep->data; diff --git a/env/nand.c b/env/nand.c index 8b0027d304..0d7ee19bc2 100644 --- a/env/nand.c +++ b/env/nand.c @@ -331,7 +331,7 @@ static int env_nand_load(void) read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2); ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, - read2_fail); + read2_fail, H_EXTERNAL); done: free(tmp_env1); @@ -372,7 +372,7 @@ static int env_nand_load(void) return -EIO; } - return env_import(buf, 1); + return env_import(buf, 1, H_EXTERNAL); #endif /* ! ENV_IS_EMBEDDED */ return 0; diff --git a/env/nvram.c b/env/nvram.c index 1a9fcf1c06..7c8ea26f96 100644 --- a/env/nvram.c +++ b/env/nvram.c @@ -64,7 +64,7 @@ static int env_nvram_load(void) #else memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); #endif - return env_import(buf, 1); + return env_import(buf, 1, H_EXTERNAL); } static int env_nvram_save(void) diff --git a/env/onenand.c b/env/onenand.c index dfd4e939f8..a2477cef9b 100644 --- a/env/onenand.c +++ b/env/onenand.c @@ -55,7 +55,7 @@ static int env_onenand_load(void) mtd->writesize = MAX_ONENAND_PAGESIZE; #endif /* !ENV_IS_EMBEDDED */ - rc = env_import(buf, 1); + rc = env_import(buf, 1, H_EXTERNAL); if (!rc) gd->env_valid = ENV_VALID; diff --git a/env/remote.c b/env/remote.c index e3f0608b16..d93a137376 100644 --- a/env/remote.c +++ b/env/remote.c @@ -45,7 +45,7 @@ static int env_remote_save(void) static int env_remote_load(void) { #ifndef ENV_IS_EMBEDDED - return env_import((char *)env_ptr, 1); + return env_import((char *)env_ptr, 1, H_EXTERNAL); #endif return 0; diff --git a/env/sata.c b/env/sata.c index 8bfcc94306..9442cfcaf3 100644 --- a/env/sata.c +++ b/env/sata.c @@ -111,7 +111,7 @@ static void env_sata_load(void) return -EIO; } - return env_import(buf, 1); + return env_import(buf, 1, H_EXTERNAL); } U_BOOT_ENV_LOCATION(sata) = { diff --git a/env/sf.c b/env/sf.c index 3e524f2947..81d6e74127 100644 --- a/env/sf.c +++ b/env/sf.c @@ -172,7 +172,7 @@ static int env_sf_load(void) CONFIG_ENV_SIZE, tmp_env2); ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, - read2_fail); + read2_fail, H_EXTERNAL); spi_flash_free(env_flash); env_flash = NULL; @@ -265,7 +265,7 @@ static int env_sf_load(void) goto err_read; } - ret = env_import(buf, 1); + ret = env_import(buf, 1, H_EXTERNAL); if (!ret) gd->env_valid = ENV_VALID; diff --git a/env/ubi.c b/env/ubi.c index 08aac47df2..5502efe28b 100644 --- a/env/ubi.c +++ b/env/ubi.c @@ -141,7 +141,7 @@ static int env_ubi_load(void) CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND); return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, - read2_fail); + read2_fail, H_EXTERNAL); } #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */ static int env_ubi_load(void) @@ -172,7 +172,7 @@ static int env_ubi_load(void) return -EIO; } - return env_import(buf, 1); + return env_import(buf, 1, H_EXTERNAL); } #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */ diff --git a/include/env.h b/include/env.h index d6c2d751d6..8cc70fd752 100644 --- a/include/env.h +++ b/include/env.h @@ -288,10 +288,11 @@ int env_erase(void); * @buf: Buffer containing the environment (struct environemnt_s *) * @check: non-zero to check the CRC at the start of the environment, 0 to * ignore it + * @flags: Flags controlling matching (H_... - see search.h) * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if * something else went wrong */ -int env_import(const char *buf, int check); +int env_import(const char *buf, int check, int flags); /** * env_export() - Export the environment to a buffer @@ -310,10 +311,12 @@ int env_export(struct environment_s *env_out); * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid * @buf2: Second environment (struct environemnt_s *) * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid + * @flags: Flags controlling matching (H_... - see search.h) * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad */ int env_import_redund(const char *buf1, int buf1_read_fail, - const char *buf2, int buf2_read_fail); + const char *buf2, int buf2_read_fail, + int flags); /** * env_get_default() - Look up a variable from the default environment diff --git a/include/search.h b/include/search.h index c4b50c9630..e56843c26f 100644 --- a/include/search.h +++ b/include/search.h @@ -113,5 +113,6 @@ int hwalk_r(struct hsearch_data *htab, #define H_PROGRAMMATIC (1 << 9) /* indicate that an import is from env_set() */ #define H_ORIGIN_FLAGS (H_INTERACTIVE | H_PROGRAMMATIC) #define H_DEFAULT (1 << 10) /* indicate that an import is default env */ +#define H_EXTERNAL (1 << 11) /* indicate that an import is external env */ #endif /* _SEARCH_H_ */