env: Add an enum for environment state

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>
This commit is contained in:
Simon Glass 2017-08-03 12:21:56 -06:00 committed by Tom Rini
parent 75de597d2d
commit 203e94f6c9
28 changed files with 104 additions and 83 deletions

View File

@ -10,6 +10,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
@ -99,7 +100,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
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

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <asm/spl.h>
#include <malloc.h>
#include <ns16550.h>
@ -101,7 +102,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
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();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
@ -63,7 +64,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
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();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
@ -87,7 +88,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
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

View File

@ -6,6 +6,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
@ -100,7 +101,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
#else
env_relocate();
#endif

View File

@ -6,6 +6,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <ns16550.h>
#include <malloc.h>
#include <mmc.h>
@ -92,7 +93,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
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

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
@ -138,7 +139,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
@ -125,7 +126,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
@ -119,7 +120,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
(uchar *)CONFIG_ENV_ADDR);
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
@ -124,7 +125,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <malloc.h>
#include <ns16550.h>
#include <nand.h>
@ -94,7 +95,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

View File

@ -5,6 +5,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <asm/spl.h>
#include <malloc.h>
#include <ns16550.h>
@ -129,7 +130,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
#endif
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

View File

@ -8,6 +8,7 @@
#include <common.h>
#include <console.h>
#include <environment.h>
#include <asm/spl.h>
#include <malloc.h>
#include <ns16550.h>
@ -87,7 +88,7 @@ void board_init_r(gd_t *gd, ulong dest_addr)
(uchar *)CONFIG_ENV_ADDR);
gd->env_addr = (ulong)(CONFIG_ENV_ADDR);
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
i2c_init_all();

2
env/dataflash.c vendored
View File

@ -78,7 +78,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}

30
env/eeprom.c vendored
View File

@ -71,7 +71,7 @@ uchar env_get_char_spec(int index)
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,
@ -128,21 +128,21 @@ void env_relocate_spec(void)
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 */
@ -170,7 +170,7 @@ void env_relocate_spec(void)
}
if (crc == new) {
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
} else {
gd->env_valid = 0;
}
@ -178,7 +178,7 @@ void env_relocate_spec(void)
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
@ -205,7 +205,7 @@ int saveenv(void)
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;
}
@ -222,10 +222,10 @@ int saveenv(void)
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;
@ -240,6 +240,6 @@ int saveenv(void)
int env_init(void)
{
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}

2
env/ext4.c vendored
View File

@ -41,7 +41,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}

2
env/fat.c vendored
View File

@ -29,7 +29,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}

22
env/flash.c vendored
View File

@ -75,28 +75,28 @@ int env_init(void)
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;
@ -211,7 +211,7 @@ int env_init(void)
{
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;
}
@ -309,7 +309,7 @@ void env_relocate_spec(void)
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),
@ -321,7 +321,7 @@ void env_relocate_spec(void)
(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),
@ -329,7 +329,7 @@ void env_relocate_spec(void)
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 */

10
env/mmc.c vendored
View File

@ -86,7 +86,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}
@ -180,7 +180,7 @@ int saveenv(void)
goto fini;
#ifdef CONFIG_ENV_OFFSET_REDUND
if (gd->env_valid == 1)
if (gd->env_valid == ENV_VALID)
copy = 1;
#endif
@ -200,7 +200,7 @@ int saveenv(void)
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:
@ -265,10 +265,10 @@ void env_relocate_spec(void)
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);

29
env/nand.c vendored
View File

@ -84,37 +84,37 @@ int env_init(void)
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;
@ -217,14 +217,15 @@ int saveenv(void)
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;
}
@ -342,10 +343,10 @@ void env_relocate_spec(void)
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);

2
env/nvram.c vendored
View File

@ -104,7 +104,7 @@ int env_init(void)
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;

4
env/onenand.c vendored
View File

@ -60,7 +60,7 @@ void env_relocate_spec(void)
rc = env_import(buf, 1);
if (rc)
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
}
int saveenv(void)
@ -110,7 +110,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}

2
env/remote.c vendored
View File

@ -29,7 +29,7 @@ int env_init(void)
{
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;
}

2
env/sata.c vendored
View File

@ -37,7 +37,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}

26
env/sf.c vendored
View File

@ -93,7 +93,7 @@ int saveenv(void)
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 {
@ -145,7 +145,7 @@ int saveenv(void)
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);
@ -198,30 +198,30 @@ void env_relocate_spec(void)
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;
@ -324,7 +324,7 @@ void env_relocate_spec(void)
ret = env_import(buf, 1);
if (ret)
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
err_read:
spi_flash_free(env_flash);
@ -338,7 +338,7 @@ int env_init(void)
{
/* 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;
}

6
env/ubi.c vendored
View File

@ -26,7 +26,7 @@ int env_init(void)
{
/* use default */
gd->env_addr = (ulong)&default_environment[0];
gd->env_valid = 1;
gd->env_valid = ENV_VALID;
return 0;
}
@ -48,7 +48,7 @@ int saveenv(void)
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)) {
@ -70,7 +70,7 @@ int saveenv(void)
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;
}

View File

@ -49,7 +49,7 @@ typedef struct global_data {
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 */

View File

@ -197,6 +197,13 @@ extern uint mmc_get_env_part(struct mmc *mmc);
#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 */