env: Add env_export() wrapper

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>
master
Marek Vasut 10 years ago committed by Tom Rini
parent b401b73d02
commit 7ce1526ed2
  1. 17
      common/env_common.c
  2. 17
      common/env_dataflash.c
  3. 12
      common/env_eeprom.c
  4. 12
      common/env_fat.c
  5. 21
      common/env_flash.c
  6. 12
      common/env_mmc.c
  7. 13
      common/env_nand.c
  8. 12
      common/env_nvram.c
  9. 13
      common/env_onenand.c
  10. 24
      common/env_sf.c
  11. 27
      common/env_ubi.c
  12. 3
      include/environment.h

@ -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) void env_relocate(void)
{ {
#if defined(CONFIG_NEEDS_MANUAL_RELOC) #if defined(CONFIG_NEEDS_MANUAL_RELOC)

@ -56,17 +56,12 @@ void env_relocate_spec(void)
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len; int ret;
char *res;
ret = env_export(&env_new);
res = (char *)&env_new.data; if (ret)
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); return ret;
if (len < 0) {
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
return write_dataflash(CONFIG_ENV_ADDR, return write_dataflash(CONFIG_ENV_ADDR,
(unsigned long)&env_new, (unsigned long)&env_new,

@ -98,8 +98,6 @@ void env_relocate_spec(void)
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len;
char *res;
int rc; int rc;
unsigned int off = CONFIG_ENV_OFFSET; unsigned int off = CONFIG_ENV_OFFSET;
#ifdef CONFIG_ENV_OFFSET_REDUND #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; rc = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (rc)
if (len < 0) { return rc;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
#ifdef CONFIG_ENV_OFFSET_REDUND #ifdef CONFIG_ENV_OFFSET_REDUND
if (gd->env_valid == 1) { if (gd->env_valid == 1) {

@ -37,19 +37,14 @@ int env_init(void)
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len;
char *res;
block_dev_desc_t *dev_desc = NULL; block_dev_desc_t *dev_desc = NULL;
int dev = FAT_ENV_DEVICE; int dev = FAT_ENV_DEVICE;
int part = FAT_ENV_PART; int part = FAT_ENV_PART;
int err; int err;
res = (char *)&env_new.data; err = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (err)
if (len < 0) { return err;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
#ifdef CONFIG_MMC #ifdef CONFIG_MMC
if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) { 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)); err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
if (err == -1) { if (err == -1) {
printf("\n** Unable to write \"%s\" from %s%d:%d **\n", printf("\n** Unable to write \"%s\" from %s%d:%d **\n",

@ -106,8 +106,7 @@ int env_init(void)
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len; char *saved_data = NULL;
char *res, *saved_data = NULL;
char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
int rc = 1; int rc = 1;
#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE #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)) if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
goto done; goto done;
res = (char *)&env_new.data; rc = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (rc)
if (len < 0) { return rc;
error("Cannot export environment: errno = %d\n", errno);
goto done;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
env_new.flags = new_flag; env_new.flags = new_flag;
#if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE #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)) if (flash_sect_protect(0, (long)flash_addr, end_addr))
goto done; goto done;
res = (char *)&env_new.data; rc = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (rc)
if (len < 0) {
error("Cannot export environment: errno = %d\n", errno);
goto done; goto done;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
puts("Erasing Flash..."); puts("Erasing Flash...");
if (flash_sect_erase((long)flash_addr, end_addr)) if (flash_sect_erase((long)flash_addr, end_addr))

@ -118,8 +118,6 @@ static unsigned char env_flags;
int saveenv(void) int saveenv(void)
{ {
ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); 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); struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
u32 offset; u32 offset;
int ret, copy = 0; int ret, copy = 0;
@ -127,15 +125,9 @@ int saveenv(void)
if (init_mmc_for_env(mmc)) if (init_mmc_for_env(mmc))
return 1; return 1;
res = (char *)&env_new->data; ret = env_export(env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) {
error("Cannot export environment: errno = %d\n", errno);
ret = 1;
goto fini; goto fini;
}
env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
#ifdef CONFIG_ENV_OFFSET_REDUND #ifdef CONFIG_ENV_OFFSET_REDUND
env_new->flags = ++env_flags; /* increase the serial */ env_new->flags = ++env_flags; /* increase the serial */

@ -181,8 +181,6 @@ int saveenv(void)
{ {
int ret = 0; int ret = 0;
ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
ssize_t len;
char *res;
int env_idx = 0; int env_idx = 0;
static const struct env_location location[] = { static const struct env_location location[] = {
{ {
@ -207,13 +205,10 @@ int saveenv(void)
if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE) if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
return 1; return 1;
res = (char *)&env_new->data; ret = env_export(env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) { return ret;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
env_new->crc = crc32(0, env_new->data, ENV_SIZE);
#ifdef CONFIG_ENV_OFFSET_REDUND #ifdef CONFIG_ENV_OFFSET_REDUND
env_new->flags = ++env_flags; /* increase the serial */ env_new->flags = ++env_flags; /* increase the serial */
env_idx = (gd->env_valid == 1); env_idx = (gd->env_valid == 1);

@ -69,17 +69,11 @@ void env_relocate_spec(void)
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len;
char *res;
int rcode = 0; int rcode = 0;
res = (char *)&env_new.data; rcode = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (rcode)
if (len < 0) { return rcode;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);

@ -66,8 +66,7 @@ void env_relocate_spec(void)
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len; int ret;
char *res;
struct mtd_info *mtd = &onenand_mtd; struct mtd_info *mtd = &onenand_mtd;
#ifdef CONFIG_ENV_ADDR_FLEX #ifdef CONFIG_ENV_ADDR_FLEX
struct onenand_chip *this = &onenand_chip; struct onenand_chip *this = &onenand_chip;
@ -78,13 +77,9 @@ int saveenv(void)
.callback = NULL, .callback = NULL,
}; };
res = (char *)&env_new.data; ret = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) { return ret;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
instr.len = CONFIG_ENV_SIZE; instr.len = CONFIG_ENV_SIZE;
#ifdef CONFIG_ENV_ADDR_FLEX #ifdef CONFIG_ENV_ADDR_FLEX

@ -47,8 +47,7 @@ static struct spi_flash *env_flash;
int saveenv(void) int saveenv(void)
{ {
env_t env_new; env_t env_new;
ssize_t len; char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
u32 saved_size, saved_offset, sector = 1; u32 saved_size, saved_offset, sector = 1;
int ret; int ret;
@ -62,13 +61,9 @@ int saveenv(void)
} }
} }
res = (char *)&env_new.data; ret = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) { return ret;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
env_new.flags = ACTIVE_FLAG; env_new.flags = ACTIVE_FLAG;
if (gd->env_valid == 1) { if (gd->env_valid == 1) {
@ -225,10 +220,9 @@ out:
int saveenv(void) int saveenv(void)
{ {
u32 saved_size, saved_offset, sector = 1; u32 saved_size, saved_offset, sector = 1;
char *res, *saved_buffer = NULL; char *saved_buffer = NULL;
int ret = 1; int ret = 1;
env_t env_new; env_t env_new;
ssize_t len;
if (!env_flash) { if (!env_flash) {
env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
@ -260,13 +254,9 @@ int saveenv(void)
sector++; sector++;
} }
res = (char *)&env_new.data; ret = env_export(&env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) {
error("Cannot export environment: errno = %d\n", errno);
goto done; goto done;
}
env_new.crc = crc32(0, env_new.data, ENV_SIZE);
puts("Erasing SPI flash..."); puts("Erasing SPI flash...");
ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,

@ -37,15 +37,11 @@ static unsigned char env_flags;
int saveenv(void) int saveenv(void)
{ {
ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
ssize_t len; int ret;
char *res;
res = (char *)&env_new->data; ret = env_export(env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) { return ret;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
printf("\n** Cannot find mtd partition \"%s\"\n", 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 */ env_new->flags = ++env_flags; /* increase the serial */
if (gd->env_valid == 1) { if (gd->env_valid == 1) {
@ -86,15 +81,11 @@ int saveenv(void)
int saveenv(void) int saveenv(void)
{ {
ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1); ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
ssize_t len; int ret;
char *res;
res = (char *)&env_new->data; ret = env_export(env_new);
len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); if (ret)
if (len < 0) { return ret;
error("Cannot export environment: errno = %d\n", errno);
return 1;
}
if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) { if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
printf("\n** Cannot find mtd partition \"%s\"\n", 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, if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
CONFIG_ENV_SIZE)) { CONFIG_ENV_SIZE)) {
printf("\n** Unable to write env to %s:%s **\n", printf("\n** Unable to write env to %s:%s **\n",

@ -201,6 +201,9 @@ int set_default_vars(int nvars, char * const vars[]);
/* Import from binary representation into hash table */ /* Import from binary representation into hash table */
int env_import(const char *buf, int check); 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 /* DO_DEPS_ONLY */
#endif /* _ENVIRONMENT_H_ */ #endif /* _ENVIRONMENT_H_ */

Loading…
Cancel
Save