U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Heiko Schocher <hs@denx.de>
To: u-boot@lists.denx.de
Subject: [PATCH v4 1/3] env: split env_import_redund() into 2 functions
Date: Sat, 10 Oct 2020 10:28:04 +0200	[thread overview]
Message-ID: <20201010082806.2260318-2-hs@denx.de> (raw)
In-Reply-To: <20201010082806.2260318-1-hs@denx.de>

split from env_import_redund() the part which checks
which Environment is valid into a separate function
called env_check_redund() and call it from env_import_redund().

So env_check_redund() can be used from places which also
need to do this checks.

Signed-off-by: Heiko Schocher <hs@denx.de>

---
The return values from env_check_redund() for the cases
only one read buffer is OK are not perfect, may we should
find better return codes?

(no changes since v2)

Changes in v2:
- patch "env: split env_import_redund into 2 functions"
  is new in version 2. Idea is to not duplicate too much
  code as Simon suggested.

 env/common.c  | 42 ++++++++++++++++++++++++++++++++----------
 include/env.h | 18 ++++++++++++++++++
 2 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/env/common.c b/env/common.c
index ed18378000..6c32a9b479 100644
--- a/env/common.c
+++ b/env/common.c
@@ -141,12 +141,11 @@ int env_import(const char *buf, int check, int flags)
 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
 static unsigned char env_flags;
 
-int env_import_redund(const char *buf1, int buf1_read_fail,
-		      const char *buf2, int buf2_read_fail,
-		      int flags)
+int env_check_redund(const char *buf1, int buf1_read_fail,
+		     const char *buf2, int buf2_read_fail)
 {
 	int crc1_ok, crc2_ok;
-	env_t *ep, *tmp_env1, *tmp_env2;
+	env_t *tmp_env1, *tmp_env2;
 
 	tmp_env1 = (env_t *)buf1;
 	tmp_env2 = (env_t *)buf2;
@@ -159,14 +158,13 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 	}
 
 	if (buf1_read_fail && buf2_read_fail) {
-		env_set_default("bad env area", 0);
 		return -EIO;
 	} else if (!buf1_read_fail && buf2_read_fail) {
 		gd->env_valid = ENV_VALID;
-		return env_import((char *)tmp_env1, 1, flags);
+		return -EINVAL;
 	} else if (buf1_read_fail && !buf2_read_fail) {
 		gd->env_valid = ENV_REDUND;
-		return env_import((char *)tmp_env2, 1, flags);
+		return -ENOENT;
 	}
 
 	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
@@ -175,7 +173,6 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 			tmp_env2->crc;
 
 	if (!crc1_ok && !crc2_ok) {
-		env_set_default("bad CRC", 0);
 		return -ENOMSG; /* needed for env_load() */
 	} else if (crc1_ok && !crc2_ok) {
 		gd->env_valid = ENV_VALID;
@@ -195,12 +192,37 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 			gd->env_valid = ENV_VALID;
 	}
 
+	return 0;
+}
+
+int env_import_redund(const char *buf1, int buf1_read_fail,
+		      const char *buf2, int buf2_read_fail,
+		      int flags)
+{
+	env_t *ep;
+	int ret;
+
+	ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
+
+	if (ret == -EIO) {
+		env_set_default("bad env area", 0);
+		return -EIO;
+	} else if (ret == -EINVAL) {
+		return env_import((char *)buf1, 1, flags);
+	} else if (ret == -ENOENT) {
+		return env_import((char *)buf2, 1, flags);
+	} else if (ret == -ENOMSG) {
+		env_set_default("bad CRC", 0);
+		return -ENOMSG;
+	}
+
 	if (gd->env_valid == ENV_VALID)
-		ep = tmp_env1;
+		ep = (env_t *)buf1;
 	else
-		ep = tmp_env2;
+		ep = (env_t *)buf2;
 
 	env_flags = ep->flags;
+
 	return env_import((char *)ep, 0, flags);
 }
 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
diff --git a/include/env.h b/include/env.h
index af405955b0..c15339a93f 100644
--- a/include/env.h
+++ b/include/env.h
@@ -318,6 +318,24 @@ int env_import(const char *buf, int check, int flags);
  */
 int env_export(struct environment_s *env_out);
 
+/**
+ * env_check_redund() - check the two redundant environments
+ *   and find out, which is the valid one.
+ *
+ * @buf1: First environment (struct environemnt_s *)
+ * @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
+ * @return 0 if OK,
+ *	-EIO if no environment is valid,
+ *	-EINVAL if read of second entry is good
+ *	-ENOENT if read of first entry is good
+ *	-ENOMSG if the CRC was bad
+ */
+
+int env_check_redund(const char *buf1, int buf1_read_fail,
+		     const char *buf2, int buf2_read_fail);
+
 /**
  * env_import_redund() - Select and import one of two redundant environments
  *
-- 
2.25.4

  reply	other threads:[~2020-10-10  8:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-10  8:28 [PATCH v4 0/3] env: Access Environment in SPI flashes before relocation Heiko Schocher
2020-10-10  8:28 ` Heiko Schocher [this message]
2020-10-12  3:35   ` [PATCH v4 1/3] env: split env_import_redund() into 2 functions Simon Glass
2020-10-30 18:46   ` Tom Rini
2020-10-10  8:28 ` [PATCH v4 2/3] env: Access Environment in SPI flashes before relocation Heiko Schocher
2020-10-12  3:35   ` Simon Glass
2020-10-30 18:46   ` Tom Rini
2020-10-30 23:51   ` Michael Walle
2020-10-31  0:07     ` Tom Rini
2020-10-31  0:25       ` Michael Walle
2020-11-02  5:20       ` Heiko Schocher
2020-10-10  8:28 ` [PATCH v4 3/3] imx6: enable early spi environment access on aristainetos boards Heiko Schocher
2020-10-30 18:46   ` Tom Rini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201010082806.2260318-2-hs@denx.de \
    --to=hs@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox