]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
env: Add env_export() wrapper
authorMarek Vasut <marex@denx.de>
Wed, 5 Mar 2014 18:59:50 +0000 (19:59 +0100)
committerTom Rini <trini@ti.com>
Fri, 21 Mar 2014 20:43:59 +0000 (16:43 -0400)
Implement env_export() wrapper, so that all implementers of saveenv() don't
have to call hexport_r(), crc32() etc. sequence . This trims down a bit of
code duplication.

Signed-off-by: Marek Vasut <marex@denx.de>
12 files changed:
common/env_common.c
common/env_dataflash.c
common/env_eeprom.c
common/env_fat.c
common/env_flash.c
common/env_mmc.c
common/env_nand.c
common/env_nvram.c
common/env_onenand.c
common/env_sf.c
common/env_ubi.c
include/environment.h

index c0bfc2f5db3b3efebb7c039747b54abc186c1acc..fe35ff4360c78ba9dfe45d1fcad366850c0a51ad 100644 (file)
@@ -172,6 +172,23 @@ int env_import(const char *buf, int check)
        return 0;
 }
 
        return 0;
 }
 
+/* Emport the environment and generate CRC for it. */
+int env_export(env_t *env_out)
+{
+       char *res;
+       ssize_t len;
+
+       res = (char *)env_out->data;
+       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
+       if (len < 0) {
+               error("Cannot export environment: errno = %d\n", errno);
+               return 1;
+       }
+       env_out->crc = crc32(0, env_out->data, ENV_SIZE);
+
+       return 0;
+}
+
 void env_relocate(void)
 {
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
 void env_relocate(void)
 {
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
index b53b87e95874bb0c9299db2eb2d0bfab6b044aac..034e3231693e3b751dc41f6e181a9672f03d6540 100644 (file)
@@ -56,17 +56,12 @@ void env_relocate_spec(void)
 
 int saveenv(void)
 {
 
 int saveenv(void)
 {
-       env_t   env_new;
-       ssize_t len;
-       char    *res;
-
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
+       env_t env_new;
+       int ret;
+
+       ret = env_export(&env_new);
+       if (ret)
+               return ret;
 
        return write_dataflash(CONFIG_ENV_ADDR,
                                (unsigned long)&env_new,
 
        return write_dataflash(CONFIG_ENV_ADDR,
                                (unsigned long)&env_new,
index 0db2bb63fe1d9671b7e586a88e9b4f08063fae37..490ac731b31da018a4c3e12d0941e71a35011ec9 100644 (file)
@@ -98,8 +98,6 @@ void env_relocate_spec(void)
 int saveenv(void)
 {
        env_t   env_new;
 int saveenv(void)
 {
        env_t   env_new;
-       ssize_t len;
-       char    *res;
        int     rc;
        unsigned int off        = CONFIG_ENV_OFFSET;
 #ifdef CONFIG_ENV_OFFSET_REDUND
        int     rc;
        unsigned int off        = CONFIG_ENV_OFFSET;
 #ifdef CONFIG_ENV_OFFSET_REDUND
@@ -109,13 +107,9 @@ int saveenv(void)
 
        BUG_ON(env_ptr != NULL);
 
 
        BUG_ON(env_ptr != NULL);
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
+       rc = env_export(&env_new);
+       if (rc)
+               return rc;
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
        if (gd->env_valid == 1) {
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
        if (gd->env_valid == 1) {
index 708fd13dc72b25eba1bb3e2a86c0fe7a4706f37d..aad0487c32805aea4ec94e488f498c842e18cef6 100644 (file)
@@ -37,19 +37,14 @@ int env_init(void)
 int saveenv(void)
 {
        env_t   env_new;
 int saveenv(void)
 {
        env_t   env_new;
-       ssize_t len;
-       char    *res;
        block_dev_desc_t *dev_desc = NULL;
        int dev = FAT_ENV_DEVICE;
        int part = FAT_ENV_PART;
        int err;
 
        block_dev_desc_t *dev_desc = NULL;
        int dev = FAT_ENV_DEVICE;
        int part = FAT_ENV_PART;
        int err;
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
+       err = env_export(&env_new);
+       if (err)
+               return err;
 
 #ifdef CONFIG_MMC
        if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
 
 #ifdef CONFIG_MMC
        if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
@@ -79,7 +74,6 @@ int saveenv(void)
                return 1;
        }
 
                return 1;
        }
 
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
        err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
        if (err == -1) {
                printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
        err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
        if (err == -1) {
                printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
index 7d5a4cfc8aa31d58e0ddf8d79061b87e27dc5b21..b3ad908894551bce70601cb0e7d3cfc9bc679ddb 100644 (file)
@@ -106,8 +106,7 @@ int env_init(void)
 int saveenv(void)
 {
        env_t   env_new;
 int saveenv(void)
 {
        env_t   env_new;
-       ssize_t len;
-       char    *res, *saved_data = NULL;
+       char    *saved_data = NULL;
        char    flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
        int     rc = 1;
 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
        char    flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
        int     rc = 1;
 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
@@ -125,13 +124,9 @@ int saveenv(void)
        if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
                goto done;
 
        if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
                goto done;
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               goto done;
-       }
-       env_new.crc     = crc32(0, env_new.data, ENV_SIZE);
+       rc = env_export(&env_new);
+       if (rc)
+               return rc;
        env_new.flags   = new_flag;
 
 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
        env_new.flags   = new_flag;
 
 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
@@ -258,13 +253,9 @@ int saveenv(void)
        if (flash_sect_protect(0, (long)flash_addr, end_addr))
                goto done;
 
        if (flash_sect_protect(0, (long)flash_addr, end_addr))
                goto done;
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
+       rc = env_export(&env_new);
+       if (rc)
                goto done;
                goto done;
-       }
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
 
        puts("Erasing Flash...");
        if (flash_sect_erase((long)flash_addr, end_addr))
 
        puts("Erasing Flash...");
        if (flash_sect_erase((long)flash_addr, end_addr))
index 045428c6ec96038e8aec0ee9d15b3561219e0233..c99fc750fa0a0b95db379b673423866d71b77a82 100644 (file)
@@ -118,8 +118,6 @@ static unsigned char env_flags;
 int saveenv(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
 int saveenv(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
-       ssize_t len;
-       char    *res;
        struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
        u32     offset;
        int     ret, copy = 0;
        struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
        u32     offset;
        int     ret, copy = 0;
@@ -127,15 +125,9 @@ int saveenv(void)
        if (init_mmc_for_env(mmc))
                return 1;
 
        if (init_mmc_for_env(mmc))
                return 1;
 
-       res = (char *)&env_new->data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               ret = 1;
+       ret = env_export(env_new);
+       if (ret)
                goto fini;
                goto fini;
-       }
-
-       env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
        env_new->flags  = ++env_flags; /* increase the serial */
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
        env_new->flags  = ++env_flags; /* increase the serial */
index 695a9eebb01e68e3f11a7a545d96ac68507eebbc..5a734a9321a9400a73ee8024f29bcdb17d546ee6 100644 (file)
@@ -181,8 +181,6 @@ int saveenv(void)
 {
        int     ret = 0;
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
 {
        int     ret = 0;
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
-       ssize_t len;
-       char    *res;
        int     env_idx = 0;
        static const struct env_location location[] = {
                {
        int     env_idx = 0;
        static const struct env_location location[] = {
                {
@@ -207,13 +205,10 @@ int saveenv(void)
        if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
                return 1;
 
        if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
                return 1;
 
-       res = (char *)&env_new->data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
-       env_new->crc   = crc32(0, env_new->data, ENV_SIZE);
+       ret = env_export(env_new);
+       if (ret)
+               return ret;
+
 #ifdef CONFIG_ENV_OFFSET_REDUND
        env_new->flags = ++env_flags; /* increase the serial */
        env_idx = (gd->env_valid == 1);
 #ifdef CONFIG_ENV_OFFSET_REDUND
        env_new->flags = ++env_flags; /* increase the serial */
        env_idx = (gd->env_valid == 1);
index 0866cde924b658f37419e42df61624afefb9e297..524f07d5f8967460ac37dd7eb453f385f54cafc8 100644 (file)
@@ -69,17 +69,11 @@ void env_relocate_spec(void)
 int saveenv(void)
 {
        env_t   env_new;
 int saveenv(void)
 {
        env_t   env_new;
-       ssize_t len;
-       char    *res;
        int     rcode = 0;
 
        int     rcode = 0;
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
+       rcode = env_export(&env_new);
+       if (rcode)
+               return rcode;
 
 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
        nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
 
 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
        nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
index 4b4463218092fc48e29c5dce2dd67fb1c90d216a..cc3d670de83d4314834695c86ef8ab654f56a8fd 100644 (file)
@@ -66,8 +66,7 @@ void env_relocate_spec(void)
 int saveenv(void)
 {
        env_t   env_new;
 int saveenv(void)
 {
        env_t   env_new;
-       ssize_t len;
-       char    *res;
+       int ret;
        struct mtd_info *mtd = &onenand_mtd;
 #ifdef CONFIG_ENV_ADDR_FLEX
        struct onenand_chip *this = &onenand_chip;
        struct mtd_info *mtd = &onenand_mtd;
 #ifdef CONFIG_ENV_ADDR_FLEX
        struct onenand_chip *this = &onenand_chip;
@@ -78,13 +77,9 @@ int saveenv(void)
                .callback       = NULL,
        };
 
                .callback       = NULL,
        };
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
+       ret = env_export(&env_new);
+       if (ret)
+               return ret;
 
        instr.len = CONFIG_ENV_SIZE;
 #ifdef CONFIG_ENV_ADDR_FLEX
 
        instr.len = CONFIG_ENV_SIZE;
 #ifdef CONFIG_ENV_ADDR_FLEX
index be270f21bcf6577ae44bd1d394817c5eb609fb01..37ab13ae17842309339fa9a70a42e49b8669207e 100644 (file)
@@ -47,8 +47,7 @@ static struct spi_flash *env_flash;
 int saveenv(void)
 {
        env_t   env_new;
 int saveenv(void)
 {
        env_t   env_new;
-       ssize_t len;
-       char    *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
+       char    *saved_buffer = NULL, flag = OBSOLETE_FLAG;
        u32     saved_size, saved_offset, sector = 1;
        int     ret;
 
        u32     saved_size, saved_offset, sector = 1;
        int     ret;
 
@@ -62,13 +61,9 @@ int saveenv(void)
                }
        }
 
                }
        }
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
-       env_new.crc     = crc32(0, env_new.data, ENV_SIZE);
+       ret = env_export(&env_new);
+       if (ret)
+               return ret;
        env_new.flags   = ACTIVE_FLAG;
 
        if (gd->env_valid == 1) {
        env_new.flags   = ACTIVE_FLAG;
 
        if (gd->env_valid == 1) {
@@ -225,10 +220,9 @@ out:
 int saveenv(void)
 {
        u32     saved_size, saved_offset, sector = 1;
 int saveenv(void)
 {
        u32     saved_size, saved_offset, sector = 1;
-       char    *res, *saved_buffer = NULL;
+       char    *saved_buffer = NULL;
        int     ret = 1;
        env_t   env_new;
        int     ret = 1;
        env_t   env_new;
-       ssize_t len;
 
        if (!env_flash) {
                env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
 
        if (!env_flash) {
                env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
@@ -260,13 +254,9 @@ int saveenv(void)
                        sector++;
        }
 
                        sector++;
        }
 
-       res = (char *)&env_new.data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
+       ret = env_export(&env_new);
+       if (ret)
                goto done;
                goto done;
-       }
-       env_new.crc = crc32(0, env_new.data, ENV_SIZE);
 
        puts("Erasing SPI flash...");
        ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
 
        puts("Erasing SPI flash...");
        ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
index c0828a47d3214131c9156157fe7c53aaf5f2e6cd..77bbfa6ef43caf051ba5166dd8045944e6e7a821 100644 (file)
@@ -37,15 +37,11 @@ static unsigned char env_flags;
 int saveenv(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
 int saveenv(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
-       ssize_t len;
-       char *res;
+       int ret;
 
 
-       res = (char *)&env_new->data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
+       ret = env_export(env_new);
+       if (ret)
+               return ret;
 
        if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
                printf("\n** Cannot find mtd partition \"%s\"\n",
 
        if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
                printf("\n** Cannot find mtd partition \"%s\"\n",
@@ -53,7 +49,6 @@ int saveenv(void)
                return 1;
        }
 
                return 1;
        }
 
-       env_new->crc = crc32(0, env_new->data, ENV_SIZE);
        env_new->flags = ++env_flags; /* increase the serial */
 
        if (gd->env_valid == 1) {
        env_new->flags = ++env_flags; /* increase the serial */
 
        if (gd->env_valid == 1) {
@@ -86,15 +81,11 @@ int saveenv(void)
 int saveenv(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
 int saveenv(void)
 {
        ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
-       ssize_t len;
-       char *res;
+       int ret;
 
 
-       res = (char *)&env_new->data;
-       len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
-       if (len < 0) {
-               error("Cannot export environment: errno = %d\n", errno);
-               return 1;
-       }
+       ret = env_export(env_new);
+       if (ret)
+               return ret;
 
        if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
                printf("\n** Cannot find mtd partition \"%s\"\n",
 
        if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
                printf("\n** Cannot find mtd partition \"%s\"\n",
@@ -102,8 +93,6 @@ int saveenv(void)
                return 1;
        }
 
                return 1;
        }
 
-       env_new->crc = crc32(0, env_new->data, ENV_SIZE);
-
        if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
                             CONFIG_ENV_SIZE)) {
                printf("\n** Unable to write env to %s:%s **\n",
        if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
                             CONFIG_ENV_SIZE)) {
                printf("\n** Unable to write env to %s:%s **\n",
index 46a3554ff9235607e38fc83e8c1b6321d7bb1113..f797595c7607cdf59537117f374d65e805e49529 100644 (file)
@@ -201,6 +201,9 @@ int set_default_vars(int nvars, char * const vars[]);
 /* Import from binary representation into hash table */
 int env_import(const char *buf, int check);
 
 /* Import from binary representation into hash table */
 int env_import(const char *buf, int check);
 
+/* Export from hash table into binary representation */
+int env_export(env_t *env_out);
+
 #endif /* DO_DEPS_ONLY */
 
 #endif /* _ENVIRONMENT_H_ */
 #endif /* DO_DEPS_ONLY */
 
 #endif /* _ENVIRONMENT_H_ */