At present we have three states for the environment, numbered 0, 1 and 2.
Add an enum to record this to avoid open-coded values.
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
#else
env_relocate();
#endif
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <asm/spl.h>
#include <malloc.h>
#include <ns16550.h>
nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
#endif
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
#else
env_relocate();
#endif
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
#else
env_relocate();
#endif
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
nand_spl_load_image(CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
#else
env_relocate();
#endif
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
(uchar *)CONFIG_ENV_ADDR);
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <asm/spl.h>
#include <malloc.h>
#include <ns16550.h>
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
#include <common.h>
#include <console.h>
+#include <environment.h>
#include <asm/spl.h>
#include <malloc.h>
#include <ns16550.h>
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
i2c_init_all();
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
unsigned int off = CONFIG_ENV_OFFSET;
#ifdef CONFIG_ENV_OFFSET_REDUND
- if (gd->env_valid == 2)
+ if (gd->env_valid == ENV_REDUND)
off = CONFIG_ENV_OFFSET_REDUND;
#endif
eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
gd->env_addr = 0;
gd->env_valid = 0;
} else if (crc_ok[0] && !crc_ok[1]) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (!crc_ok[0] && crc_ok[1]) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else {
/* both ok - check serial */
if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
else if (flags[0] == 0xFF && flags[1] == 0)
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
else if (flags[1] == 0xFF && flags[0] == 0)
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
else /* flags are equal - almost impossible */
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
}
#else /* CONFIG_ENV_OFFSET_REDUND */
}
if (crc == new) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else {
gd->env_valid = 0;
}
off = CONFIG_ENV_OFFSET;
#ifdef CONFIG_ENV_OFFSET_REDUND
- if (gd->env_valid == 2)
+ if (gd->env_valid == ENV_REDUND)
off = CONFIG_ENV_OFFSET_REDUND;
#endif
return rc;
#ifdef CONFIG_ENV_OFFSET_REDUND
- if (gd->env_valid == 1) {
+ if (gd->env_valid == ENV_VALID) {
off = CONFIG_ENV_OFFSET_REDUND;
off_red = CONFIG_ENV_OFFSET;
}
off_red + offsetof(env_t, flags),
(uchar *)&flag_obsolete, 1);
- if (gd->env_valid == 1)
- gd->env_valid = 2;
+ if (gd->env_valid == ENV_VALID)
+ gd->env_valid = ENV_REDUND;
else
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
}
#endif
return rc;
int env_init(void)
{
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
if (crc1_ok && !crc2_ok) {
gd->env_addr = addr1;
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (!crc1_ok && crc2_ok) {
gd->env_addr = addr2;
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (!crc1_ok && !crc2_ok) {
gd->env_addr = addr_default;
gd->env_valid = 0;
} else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
gd->env_addr = addr1;
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
gd->env_addr = addr2;
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (flag1 == flag2) {
gd->env_addr = addr1;
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else if (flag1 == 0xFF) {
gd->env_addr = addr1;
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else if (flag2 == 0xFF) {
gd->env_addr = addr2;
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
}
return 0;
{
if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
gd->env_addr = (ulong)&(env_ptr->data);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
char flag = OBSOLETE_FLAG;
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
flash_write(&flag,
(ulong)&(flash_addr_new->flags),
(flash_addr->flags & ACTIVE_FLAG) == ACTIVE_FLAG) {
char flag = ACTIVE_FLAG;
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
flash_sect_protect(0, (ulong)flash_addr, end_addr);
flash_write(&flag,
(ulong)&(flash_addr->flags),
flash_sect_protect(1, (ulong)flash_addr, end_addr);
}
- if (gd->env_valid == 2)
+ if (gd->env_valid == ENV_REDUND)
puts("*** Warning - some problems detected "
"reading environment; recovered successfully\n\n");
#endif /* CONFIG_ENV_ADDR_REDUND */
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
goto fini;
#ifdef CONFIG_ENV_OFFSET_REDUND
- if (gd->env_valid == 1)
+ if (gd->env_valid == ENV_VALID)
copy = 1;
#endif
ret = 0;
#ifdef CONFIG_ENV_OFFSET_REDUND
- gd->env_valid = gd->env_valid == 2 ? 1 : 2;
+ gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
#endif
fini:
ret = 1;
goto fini;
} else if (!read1_fail && read2_fail) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
env_import((char *)tmp_env1, 1);
} else if (read1_fail && !read2_fail) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
env_import((char *)tmp_env2, 1);
} else {
env_import_redund((char *)tmp_env1, (char *)tmp_env2);
return 0;
} else if (crc1_ok && !crc2_ok) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
}
#ifdef CONFIG_ENV_OFFSET_REDUND
else if (!crc1_ok && crc2_ok) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else {
/* both ok - check serial */
if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
else if (tmp_env1->flags > tmp_env2->flags)
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
else if (tmp_env2->flags > tmp_env1->flags)
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
else /* flags are equal - almost impossible */
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
}
- if (gd->env_valid == 2)
+ if (gd->env_valid == ENV_REDUND)
env_ptr = tmp_env2;
else
#endif
- if (gd->env_valid == 1)
+ if (gd->env_valid == ENV_VALID)
env_ptr = tmp_env1;
gd->env_addr = (ulong)env_ptr->data;
#else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
#endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
return 0;
return ret;
#ifdef CONFIG_ENV_OFFSET_REDUND
- env_idx = (gd->env_valid == 1);
+ env_idx = (gd->env_valid == ENV_VALID);
#endif
ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
#ifdef CONFIG_ENV_OFFSET_REDUND
if (!ret) {
/* preset other copy for next write */
- gd->env_valid = gd->env_valid == 2 ? 1 : 2;
+ gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
+ ENV_REDUND;
return ret;
}
set_default_env("!bad env area");
goto done;
} else if (!read1_fail && read2_fail) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
env_import((char *)tmp_env1, 1);
} else if (read1_fail && !read2_fail) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
env_import((char *)tmp_env2, 1);
} else {
env_import_redund((char *)tmp_env1, (char *)tmp_env2);
if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
gd->env_addr = (ulong)&env_ptr->data;
#endif
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else {
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 0;
rc = env_import(buf, 1);
if (rc)
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
}
int saveenv(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
{
if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
gd->env_addr = (ulong)&(env_ptr->data);
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
return ret;
env_new.flags = ACTIVE_FLAG;
- if (gd->env_valid == 1) {
+ if (gd->env_valid == ENV_VALID) {
env_new_offset = CONFIG_ENV_OFFSET_REDUND;
env_offset = CONFIG_ENV_OFFSET;
} else {
puts("done\n");
- gd->env_valid = gd->env_valid == 2 ? 1 : 2;
+ gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
printf("Valid environment: %d\n", (int)gd->env_valid);
set_default_env("!bad CRC");
goto err_read;
} else if (crc1_ok && !crc2_ok) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (!crc1_ok && crc2_ok) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else if (tmp_env1->flags == ACTIVE_FLAG &&
tmp_env2->flags == OBSOLETE_FLAG) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (tmp_env1->flags == OBSOLETE_FLAG &&
tmp_env2->flags == ACTIVE_FLAG) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else if (tmp_env1->flags == tmp_env2->flags) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (tmp_env1->flags == 0xFF) {
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
} else if (tmp_env2->flags == 0xFF) {
- gd->env_valid = 2;
+ gd->env_valid = ENV_REDUND;
} else {
/*
* this differs from code in env_flash.c, but I think a sane
* default path is desirable.
*/
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
}
- if (gd->env_valid == 1)
+ if (gd->env_valid == ENV_VALID)
ep = tmp_env1;
else
ep = tmp_env2;
ret = env_import(buf, 1);
if (ret)
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
err_read:
spi_flash_free(env_flash);
{
/* SPI flash isn't usable before relocation */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
- gd->env_valid = 1;
+ gd->env_valid = ENV_VALID;
return 0;
}
return 1;
}
- if (gd->env_valid == 1) {
+ if (gd->env_valid == ENV_VALID) {
puts("Writing to redundant UBI... ");
if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
(void *)env_new, CONFIG_ENV_SIZE)) {
puts("done\n");
- gd->env_valid = gd->env_valid == 2 ? 1 : 2;
+ gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
return 0;
}
unsigned long precon_buf_idx; /* Pre-Console buffer index */
#endif
unsigned long env_addr; /* Address of Environment struct */
- unsigned long env_valid; /* Checksum of Environment valid? */
+ unsigned long env_valid; /* Environment valid? enum env_valid */
unsigned long ram_top; /* Top address of RAM used by U-Boot */
unsigned long relocaddr; /* Start address of U-Boot in RAM */
#include <env_flags.h>
#include <search.h>
+/* Value for environment validity */
+enum env_valid {
+ ENV_INVALID, /* No valid environment */
+ ENV_VALID, /* First or only environment is valid */
+ ENV_REDUND, /* Redundant environment is valid */
+};
+
extern struct hsearch_data env_htab;
/* Function that returns a character from the environment */