]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
env: move more common code to env_import_redund
authorSimon Goldschmidt <sgoldschmidt@de.pepperl-fuchs.com>
Wed, 31 Jan 2018 13:47:11 +0000 (14:47 +0100)
committerTom Rini <trini@konsulko.com>
Thu, 1 Feb 2018 13:05:49 +0000 (08:05 -0500)
There is more common code in mmc, nand and ubi env drivers that
can be shared by moving to env_import_redund.

For this, a status/error value whether the buffers were loaded
are passed as additional parameters to env_import_redund.
Ideally, these are already returned to the env driver by the
storage driver. This is the case for mmc, nand and ubi, so for
this change, code deduplicated.

Signed-off-by: Simon Goldschmidt <sgoldschmidt@de.pepperl-fuchs.com>
Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
env/common.c
env/mmc.c
env/nand.c
env/ubi.c
include/environment.h

index 363ba6feadfed905e34ddfe2a444a4da966c2d1b..f21ff700969561d022e63b89dad931aa29beada6 100644 (file)
@@ -138,7 +138,8 @@ int env_import(const char *buf, int check)
 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
 static unsigned char env_flags;
 
-int env_import_redund(const char *buf1, const char *buf2)
+int env_import_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;
@@ -146,6 +147,24 @@ int env_import_redund(const char *buf1, const char *buf2)
        tmp_env1 = (env_t *)buf1;
        tmp_env2 = (env_t *)buf2;
 
+       if (buf1_read_fail && buf2_read_fail) {
+               puts("*** Error - No Valid Environment Area found\n");
+       } else if (buf1_read_fail || buf2_read_fail) {
+               puts("*** Warning - some problems detected ");
+               puts("reading environment; recovered successfully\n");
+       }
+
+       if (buf1_read_fail && buf2_read_fail) {
+               set_default_env("!bad env area");
+               return -EIO;
+       } else if (!buf1_read_fail && buf2_read_fail) {
+               gd->env_valid = ENV_VALID;
+               return env_import((char *)tmp_env1, 1);
+       } else if (buf1_read_fail && !buf2_read_fail) {
+               gd->env_valid = ENV_REDUND;
+               return env_import((char *)tmp_env2, 1);
+       }
+
        crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
                        tmp_env1->crc;
        crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
index 528fbf978162ddb40ea1c09d36a71f3e8c4023af..8847fdc7e2d6b351cce41baf6fa4f581be80abcb 100644 (file)
--- a/env/mmc.c
+++ b/env/mmc.c
@@ -290,27 +290,8 @@ static int env_mmc_load(void)
        read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
        read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
 
-       if (read1_fail && read2_fail)
-               puts("*** Error - No Valid Environment Area found\n");
-       else if (read1_fail || read2_fail)
-               puts("*** Warning - some problems detected "
-                    "reading environment; recovered successfully\n");
-
-       if (read1_fail && read2_fail) {
-               errmsg = "!bad CRC";
-               ret = -EIO;
-               goto fini;
-       } else if (!read1_fail && read2_fail) {
-               gd->env_valid = ENV_VALID;
-               env_import((char *)tmp_env1, 1);
-       } else if (read1_fail && !read2_fail) {
-               gd->env_valid = ENV_REDUND;
-               env_import((char *)tmp_env2, 1);
-       } else {
-               env_import_redund((char *)tmp_env1, (char *)tmp_env2);
-       }
-
-       ret = 0;
+       ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
+                               read2_fail);
 
 fini:
        fini_mmc_for_env(mmc);
index 8058b55c502992540b44b236f625c452fb045b61..3e8df39c26805f4686aac78767652a21ef4eceaf 100644 (file)
@@ -320,7 +320,7 @@ static int env_nand_load(void)
 #if defined(ENV_IS_EMBEDDED)
        return 0;
 #else
-       int read1_fail = 0, read2_fail = 0;
+       int read1_fail, read2_fail;
        env_t *tmp_env1, *tmp_env2;
        int ret = 0;
 
@@ -336,24 +336,8 @@ static int env_nand_load(void)
        read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
        read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
 
-       if (read1_fail && read2_fail)
-               puts("*** Error - No Valid Environment Area found\n");
-       else if (read1_fail || read2_fail)
-               puts("*** Warning - some problems detected "
-                    "reading environment; recovered successfully\n");
-
-       if (read1_fail && read2_fail) {
-               set_default_env("!bad env area");
-               goto done;
-       } else if (!read1_fail && read2_fail) {
-               gd->env_valid = ENV_VALID;
-               env_import((char *)tmp_env1, 1);
-       } else if (read1_fail && !read2_fail) {
-               gd->env_valid = ENV_REDUND;
-               env_import((char *)tmp_env2, 1);
-       } else {
-               env_import_redund((char *)tmp_env1, (char *)tmp_env2);
-       }
+       ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
+                               read2_fail);
 
 done:
        free(tmp_env1);
index 1c4653d4f6ac440e3b2e7b89c9815c26a77e6cd1..72c7a7829e556fcc7d732b0acc6863d7bbdad409 100644 (file)
--- a/env/ubi.c
+++ b/env/ubi.c
@@ -95,6 +95,7 @@ static int env_ubi_load(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
        ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
+       int read1_fail, read2_fail;
        env_t *tmp_env1, *tmp_env2;
 
        /*
@@ -118,21 +119,20 @@ static int env_ubi_load(void)
                return -EIO;
        }
 
-       if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
-                           CONFIG_ENV_SIZE)) {
+       read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
+                                    CONFIG_ENV_SIZE);
+       if (read1_fail)
                printf("\n** Unable to read env from %s:%s **\n",
                       CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
-       }
 
-       if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
-                           CONFIG_ENV_SIZE)) {
+       read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
+                                    (void *)tmp_env2, CONFIG_ENV_SIZE);
+       if (read2_fail)
                printf("\n** Unable to read redundant env from %s:%s **\n",
                       CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
-       }
 
-       env_import_redund((char *)tmp_env1, (char *)tmp_env2);
-
-       return 0;
+       return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
+                                                        read2_fail);
 }
 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
 static int env_ubi_load(void)
index a4060506fabb543ab291feb394c8059c8c1454aa..6044b9e1b463e84c9bc20e7f1e8eac59fbc46168 100644 (file)
@@ -297,7 +297,8 @@ int env_export(env_t *env_out);
 
 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
 /* Select and import one of two redundant environments */
-int env_import_redund(const char *buf1, const char *buf2);
+int env_import_redund(const char *buf1, int buf1_status,
+                     const char *buf2, int buf2_status);
 #endif
 
 /**