]> git.ipfire.org Git - people/ms/u-boot.git/commitdiff
env: Adjust the load() method to return an error
authorSimon Glass <sjg@chromium.org>
Thu, 3 Aug 2017 18:22:17 +0000 (12:22 -0600)
committerTom Rini <trini@konsulko.com>
Wed, 16 Aug 2017 12:31:24 +0000 (08:31 -0400)
The load() methods have inconsistent behaviour on error. Some of them load
an empty default environment. Some load an environment containing an error
message. Others do nothing.

As a step in the right direction, have the method return an error code.
Then the caller could handle this itself in a consistent way.

Signed-off-by: Simon Glass <sjg@chromium.org>
14 files changed:
env/dataflash.c
env/eeprom.c
env/ext4.c
env/fat.c
env/flash.c
env/mmc.c
env/nand.c
env/nvram.c
env/onenand.c
env/remote.c
env/sata.c
env/sf.c
env/ubi.c
include/environment.h

index afa08f8fd50914b875f53b9cc1138edf7c23e125..77bc595e0debc4c65a4518900237d80022166731 100644 (file)
@@ -23,7 +23,7 @@ static int env_dataflash_get_char(int index)
        return c;
 }
 
-static void env_dataflash_load(void)
+static int env_dataflash_load(void)
 {
        ulong crc, new = 0;
        unsigned off;
@@ -44,6 +44,8 @@ static void env_dataflash_load(void)
                env_import(buf, 1);
        else
                set_default_env("!bad CRC");
+
+       return 0;
 }
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
index fbe4fd4efc5d7ea2d7090a384745b375282d3a5d..08ef6307fc5c672ed7a814757cd7bef090b97361 100644 (file)
@@ -76,7 +76,7 @@ static int env_eeprom_get_char(int index)
        return c;
 }
 
-static void env_eeprom_load(void)
+static int env_eeprom_load(void)
 {
        char buf_env[CONFIG_ENV_SIZE];
        unsigned int off = CONFIG_ENV_OFFSET;
@@ -182,6 +182,8 @@ static void env_eeprom_load(void)
                off, (uchar *)buf_env, CONFIG_ENV_SIZE);
 
        env_import(buf_env, 1);
+
+       return 0;
 }
 
 static int env_eeprom_save(void)
index ee073a8b7ccd99c72c65e9b3f072e493da2a055b..65202213d3a80328f1117e5153c6178cc957ca8e 100644 (file)
@@ -75,7 +75,7 @@ static int env_ext4_save(void)
 }
 #endif /* CONFIG_CMD_SAVEENV */
 
-static void env_ext4_load(void)
+static int env_ext4_load(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
        struct blk_desc *dev_desc = NULL;
@@ -109,10 +109,12 @@ static void env_ext4_load(void)
        }
 
        env_import(buf, 1);
-       return;
+       return 0;
 
 err_env_relocate:
        set_default_env(NULL);
+
+       return -EIO;
 }
 
 U_BOOT_ENV_LOCATION(ext4) = {
index a5a156c88b5b78c17f133e40397634784d0e93c3..ec49c39053691a5d6638fc64af2e9caf910ca5ea 100644 (file)
--- a/env/fat.c
+++ b/env/fat.c
@@ -74,7 +74,7 @@ static int env_fat_save(void)
 #endif /* CMD_SAVEENV */
 
 #ifdef LOADENV
-static void env_fat_load(void)
+static int env_fat_load(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
        struct blk_desc *dev_desc = NULL;
@@ -103,10 +103,12 @@ static void env_fat_load(void)
        }
 
        env_import(buf, 1);
-       return;
+       return 0;
 
 err_env_relocate:
        set_default_env(NULL);
+
+       return -EIO;
 }
 #endif /* LOADENV */
 
index 2d72c51622ff5d7198e28cf476604b39588267d4..b60be57a8dd1585bcc9edb895e9d00dce1f4d789 100644 (file)
@@ -308,7 +308,7 @@ done:
 #endif /* CONFIG_ENV_ADDR_REDUND */
 
 #ifdef LOADENV
-static void env_flash_load(void)
+static int env_flash_load(void)
 {
 #ifdef CONFIG_ENV_ADDR_REDUND
        if (gd->env_addr != (ulong)&(flash_addr->data)) {
@@ -352,6 +352,8 @@ static void env_flash_load(void)
 #endif /* CONFIG_ENV_ADDR_REDUND */
 
        env_import((char *)flash_addr, 1);
+
+       return 0;
 }
 #endif /* LOADENV */
 
index 88ffc91f0b58838106e560e7648e47fbc5f5880c..3f3092d975607a48161e3e74819d98a67a16ad64 100644 (file)
--- a/env/mmc.c
+++ b/env/mmc.c
@@ -207,7 +207,7 @@ static inline int read_env(struct mmc *mmc, unsigned long size,
 }
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
-static void env_mmc_load(void)
+static int env_mmc_load(void)
 {
 #if !defined(ENV_IS_EMBEDDED)
        struct mmc *mmc;
@@ -224,13 +224,13 @@ static void env_mmc_load(void)
 
        errmsg = init_mmc_for_env(mmc);
        if (errmsg) {
-               ret = 1;
+               ret = -EIO;
                goto err;
        }
 
        if (mmc_get_env_addr(mmc, 0, &offset1) ||
            mmc_get_env_addr(mmc, 1, &offset2)) {
-               ret = 1;
+               ret = -EIO;
                goto fini;
        }
 
@@ -245,7 +245,7 @@ static void env_mmc_load(void)
 
        if (read1_fail && read2_fail) {
                errmsg = "!bad CRC";
-               ret = 1;
+               ret = -EIO;
                goto fini;
        } else if (!read1_fail && read2_fail) {
                gd->env_valid = ENV_VALID;
@@ -264,10 +264,12 @@ fini:
 err:
        if (ret)
                set_default_env(errmsg);
+
 #endif
+       return ret;
 }
 #else /* ! CONFIG_ENV_OFFSET_REDUND */
-static void env_mmc_load(void)
+static int env_mmc_load(void)
 {
 #if !defined(ENV_IS_EMBEDDED)
        ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
@@ -281,18 +283,18 @@ static void env_mmc_load(void)
 
        errmsg = init_mmc_for_env(mmc);
        if (errmsg) {
-               ret = 1;
+               ret = -EIO;
                goto err;
        }
 
        if (mmc_get_env_addr(mmc, 0, &offset)) {
-               ret = 1;
+               ret = -EIO;
                goto fini;
        }
 
        if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
                errmsg = "!read failed";
-               ret = 1;
+               ret = -EIO;
                goto fini;
        }
 
@@ -305,6 +307,7 @@ err:
        if (ret)
                set_default_env(errmsg);
 #endif
+       return ret;
 }
 #endif /* CONFIG_ENV_OFFSET_REDUND */
 
index e74a8c674e2cc4958109830459051904bed9f4d6..dea7b00720ef9fbb11309abfda326f11d8568aa5 100644 (file)
@@ -302,7 +302,7 @@ int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
        }
 
        if (oob_buf[0] == ENV_OOB_MARKER) {
-               *result = oob_buf[1] * mtd->erasesize;
+               *result = ovoid ob_buf[1] * mtd->erasesize;
        } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
                *result = oob_buf[1];
        } else {
@@ -315,17 +315,21 @@ int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
 #endif
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
-static void env_nand_load(void)
+static int env_nand_load(void)
 {
-#if !defined(ENV_IS_EMBEDDED)
+#if defined(ENV_IS_EMBEDDED)
+       return 0;
+#else
        int read1_fail = 0, read2_fail = 0;
        env_t *tmp_env1, *tmp_env2;
+       int ret = 0;
 
        tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
        tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
        if (tmp_env1 == NULL || tmp_env2 == NULL) {
                puts("Can't allocate buffers for environment\n");
                set_default_env("!malloc() failed");
+               ret = -EIO;
                goto done;
        }
 
@@ -355,6 +359,7 @@ done:
        free(tmp_env1);
        free(tmp_env2);
 
+       return ret;
 #endif /* ! ENV_IS_EMBEDDED */
 }
 #else /* ! CONFIG_ENV_OFFSET_REDUND */
@@ -363,7 +368,7 @@ done:
  * device i.e., nand_dev_desc + 0. This is also the behaviour using
  * the new NAND code.
  */
-static void env_nand_load(void)
+static int env_nand_load(void)
 {
 #if !defined(ENV_IS_EMBEDDED)
        int ret;
@@ -386,11 +391,13 @@ static void env_nand_load(void)
        ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
        if (ret) {
                set_default_env("!readenv() failed");
-               return;
+               return -EIO;
        }
 
        env_import(buf, 1);
 #endif /* ! ENV_IS_EMBEDDED */
+
+       return 0;
 }
 #endif /* CONFIG_ENV_OFFSET_REDUND */
 
index 85af37d4a0540fd51dfce4c099c4bc297b751a64..5fb3115ce65af3db82429e8311c3d507adc6fa47 100644 (file)
@@ -51,7 +51,7 @@ static int env_nvram_get_char(int index)
 }
 #endif
 
-static void env_nvram_load(void)
+static int env_nvram_load(void)
 {
        char buf[CONFIG_ENV_SIZE];
 
@@ -61,6 +61,8 @@ static void env_nvram_load(void)
        memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
 #endif
        env_import(buf, 1);
+
+       return 0;
 }
 
 static int env_nvram_save(void)
index 319f553262db4945ef19ccc7642b0a82bbdbd75f..2e3045c5f5de69b9de5acaa60eb58df1c8f435af 100644 (file)
@@ -26,7 +26,7 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static void env_onenand_load(void)
+static int env_onenand_load(void)
 {
        struct mtd_info *mtd = &onenand_mtd;
 #ifdef CONFIG_ENV_ADDR_FLEX
@@ -59,6 +59,8 @@ static void env_onenand_load(void)
        rc = env_import(buf, 1);
        if (rc)
                gd->env_valid = ENV_VALID;
+
+       return rc ? 0 : -EIO;
 }
 
 static int env_onenand_save(void)
index 0d8865bd67777bf8b5b7fe9df08a7f7cf6a45c26..c013fdd4b0194181d50905a54b92e4b169fee6a7 100644 (file)
@@ -46,11 +46,13 @@ static int env_remote_save(void)
 }
 #endif /* CONFIG_CMD_SAVEENV */
 
-static void env_remote_load(void)
+static int env_remote_load(void)
 {
 #ifndef ENV_IS_EMBEDDED
        env_import((char *)env_ptr, 1);
 #endif
+
+       return 0;
 }
 
 U_BOOT_ENV_LOCATION(remote) = {
index 16d8f939db6ccd3cdbaef5711d2812ac226dd4ce..a77029774e1f5f46baa09ce09abc18d13f1bde77 100644 (file)
@@ -98,21 +98,24 @@ static void env_sata_load(void)
        int env_sata;
 
        if (sata_initialize())
-               return;
+               return -EIO;
 
        env_sata = sata_get_env_dev();
 
        sata = sata_get_dev(env_sata);
        if (sata == NULL) {
-               printf("Unknown SATA(%d) device for environment!\n",
-                      env_sata);
-               return;
+               printf("Unknown SATA(%d) device for environment!\n", env_sata);
+               return -EIO;
        }
 
-       if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf))
-               return set_default_env(NULL);
+       if (read_env(sata, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, buf)) {
+               set_default_env(NULL);
+               return -EIO;
+       }
 
        env_import(buf, 1);
+
+       return 0;
 }
 
 U_BOOT_ENV_LOCATION(sata) = {
index 07386c629ae22261dd32adf75911868f41374397..6f74371c098203e2ba9c805227da7a9a492bae7a 100644 (file)
--- a/env/sf.c
+++ b/env/sf.c
@@ -61,7 +61,7 @@ static int setup_flash_device(void)
                                     0, 0, &new);
        if (ret) {
                set_default_env("!spi_flash_probe_bus_cs() failed");
-               return 1;
+               return ret;
        }
 
        env_flash = dev_get_uclass_priv(new);
@@ -73,7 +73,7 @@ static int setup_flash_device(void)
                        CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
                if (!env_flash) {
                        set_default_env("!spi_flash_probe() failed");
-                       return 1;
+                       return -EIO;
                }
        }
 #endif
@@ -95,7 +95,7 @@ static int env_sf_save(void)
 
        ret = env_export(&env_new);
        if (ret)
-               return ret;
+               return -EIO;
        env_new.flags   = ACTIVE_FLAG;
 
        if (gd->env_valid == ENV_VALID) {
@@ -112,7 +112,7 @@ static int env_sf_save(void)
                saved_offset = env_new_offset + CONFIG_ENV_SIZE;
                saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
                if (!saved_buffer) {
-                       ret = 1;
+                       ret = -ENOMEM;
                        goto done;
                }
                ret = spi_flash_read(env_flash, saved_offset,
@@ -162,7 +162,7 @@ static int env_sf_save(void)
 }
 #endif /* CMD_SAVEENV */
 
-static void env_sf_load(void)
+static int env_sf_load(void)
 {
        int ret;
        int crc1_ok = 0, crc2_ok = 0;
@@ -176,6 +176,7 @@ static void env_sf_load(void)
                        CONFIG_ENV_SIZE);
        if (!tmp_env1 || !tmp_env2) {
                set_default_env("!malloc() failed");
+               ret = -EIO;
                goto out;
        }
 
@@ -202,6 +203,7 @@ static void env_sf_load(void)
 
        if (!crc1_ok && !crc2_ok) {
                set_default_env("!bad CRC");
+               ret = -EIO;
                goto err_read;
        } else if (crc1_ok && !crc2_ok) {
                gd->env_valid = ENV_VALID;
@@ -244,6 +246,8 @@ err_read:
 out:
        free(tmp_env1);
        free(tmp_env2);
+
+       return ret;
 }
 #else
 #ifdef CMD_SAVEENV
@@ -308,7 +312,7 @@ static int env_sf_save(void)
 }
 #endif /* CMD_SAVEENV */
 
-static void env_sf_load(void)
+static int env_sf_load(void)
 {
        int ret;
        char *buf = NULL;
@@ -316,7 +320,7 @@ static void env_sf_load(void)
        buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
        if (!buf) {
                set_default_env("!malloc() failed");
-               return;
+               return -EIO;
        }
 
        ret = setup_flash_device();
@@ -339,6 +343,8 @@ err_read:
        env_flash = NULL;
 out:
        free(buf);
+
+       return ret;
 }
 #endif
 
index 9399f943dc88b918d33ba3f8f824ea27f00662bf..1c4653d4f6ac440e3b2e7b89c9815c26a77e6cd1 100644 (file)
--- a/env/ubi.c
+++ b/env/ubi.c
@@ -91,7 +91,7 @@ static int env_ubi_save(void)
 #endif /* CONFIG_CMD_SAVEENV */
 
 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
-static void env_ubi_load(void)
+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);
@@ -115,7 +115,7 @@ static void env_ubi_load(void)
                printf("\n** Cannot find mtd partition \"%s\"\n",
                       CONFIG_ENV_UBI_PART);
                set_default_env(NULL);
-               return;
+               return -EIO;
        }
 
        if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
@@ -131,9 +131,11 @@ static void env_ubi_load(void)
        }
 
        env_import_redund((char *)tmp_env1, (char *)tmp_env2);
+
+       return 0;
 }
 #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
-static void env_ubi_load(void)
+static int env_ubi_load(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
 
@@ -151,17 +153,19 @@ static void env_ubi_load(void)
                printf("\n** Cannot find mtd partition \"%s\"\n",
                       CONFIG_ENV_UBI_PART);
                set_default_env(NULL);
-               return;
+               return -EIO;
        }
 
        if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
                printf("\n** Unable to read env from %s:%s **\n",
                       CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
                set_default_env(NULL);
-               return;
+               return -EIO;
        }
 
        env_import(buf, 1);
+
+       return 0;
 }
 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
 
index ba8af28414be611d437b1b41117237f4ac0230ab..03b41e0c51160d295b17f4764abfb4cb6dff312e 100644 (file)
@@ -236,8 +236,10 @@ struct env_driver {
         *
         * This method is optional. If not provided, no environment will be
         * loaded.
+        *
+        * @return 0 if OK, -ve on error
         */
-       void (*load)(void);
+       int (*load)(void);
 
        /**
         * save() - Save the environment to storage