* [PATCH 1/5] fix error return value while loading environment
2010-03-26 12:52 Environment handling fixes and support for multiple environment backends Luotao Fu
@ 2010-03-26 12:52 ` Luotao Fu
2010-03-26 12:52 ` [PATCH 2/5] crc value handling and dry run mode in envfs_load Luotao Fu
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Luotao Fu @ 2010-03-26 12:52 UTC (permalink / raw)
To: sha; +Cc: barebox, Luotao Fu
Signed-off-by: Luotao Fu <l.fu@pengutronix.de>
---
common/environment.c | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/common/environment.c b/common/environment.c
index 0c7de84..0eb7e6b 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -198,6 +198,7 @@ int envfs_load(char *filename, char *dir)
if (crc32(0, (unsigned char *)&super, sizeof(struct envfs_super) - 4)
!= ENVFS_32(super.sb_crc)) {
printf("wrong crc on env superblock\n");
+ ret = -EIO;
goto out;
}
@@ -207,18 +208,20 @@ int envfs_load(char *filename, char *dir)
ret = read(envfd, buf, size);
if (ret < size) {
perror("read");
+ ret = errno;
goto out;
}
if (crc32(0, (unsigned char *)buf, size)
!= ENVFS_32(super.crc)) {
printf("wrong crc on env\n");
+ ret = -EIO;
goto out;
}
while (size) {
struct envfs_inode *inode;
- uint32_t inode_size,inode_namelen;
+ uint32_t inode_size, inode_namelen;
inode = (struct envfs_inode *)buf;
@@ -251,6 +254,7 @@ int envfs_load(char *filename, char *dir)
inode_size);
if (ret < inode_size) {
perror("write");
+ ret = errno;
close(fd);
goto out;
}
--
1.7.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/5] crc value handling and dry run mode in envfs_load
2010-03-26 12:52 Environment handling fixes and support for multiple environment backends Luotao Fu
2010-03-26 12:52 ` [PATCH 1/5] fix error return value while loading environment Luotao Fu
@ 2010-03-26 12:52 ` Luotao Fu
2010-03-26 12:52 ` [PATCH 3/5] add multi environment support Luotao Fu
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Luotao Fu @ 2010-03-26 12:52 UTC (permalink / raw)
To: sha; +Cc: barebox, Luotao Fu
We add some stuffs to envfs_load to enable handling of multiple environment
partitions:
* envfs_load now accepts a parameter, where the crc value of the environment
partition can be stored.
* Also added "dry run" mode to envfs_load. If the dirname is not set.
envfs_load will now only check the crc values of the partition and returns
without writing to the file system.
Signed-off-by: Luotao Fu <l.fu@pengutronix.de>
---
commands/loadenv.c | 4 +++-
common/environment.c | 13 +++++++++++--
include/environment.h | 2 +-
scripts/bareboxenv.c | 2 +-
4 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/commands/loadenv.c b/commands/loadenv.c
index 14fbf1e..3da67d9 100644
--- a/commands/loadenv.c
+++ b/commands/loadenv.c
@@ -35,12 +35,14 @@ static int do_loadenv(struct command *cmdtp, int argc, char *argv[])
dirname = "/env";
else
dirname = argv[2];
+
if (argc < 2)
filename = "/dev/env0";
else
filename = argv[1];
printf("loading environment from %s\n", filename);
- return envfs_load(filename, dirname);
+
+ return envfs_load(filename, dirname, NULL);
}
static const __maybe_unused char cmd_loadenv_help[] =
diff --git a/common/environment.c b/common/environment.c
index 0eb7e6b..e79ec99 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -159,13 +159,15 @@ EXPORT_SYMBOL(envfs_save);
/**
* Restore the last environment into the current one
* @param[in] filename from where to restore
- * @param[in] dir where to store the last content
+ * @param[in] dir where to store the last content. Function will return crc
+ * value only if dir==NULL.
+ * @param[in] crc pointer to variale, where the crc can be stored.
* @return 0 on success, anything else in case of failure
*
* Note: This function will also be used on the host! See note in the header
* of this file.
*/
-int envfs_load(char *filename, char *dir)
+int envfs_load(char *filename, char *dir, int *crc)
{
struct envfs_super super;
void *buf = NULL, *buf_free = NULL;
@@ -219,6 +221,13 @@ int envfs_load(char *filename, char *dir)
goto out;
}
+ if (crc != NULL)
+ *crc = ENVFS_32(super.crc);
+
+ /* return crc only if dir is not set */
+ if (!dir)
+ goto out;
+
while (size) {
struct envfs_inode *inode;
uint32_t inode_size, inode_namelen;
diff --git a/include/environment.h b/include/environment.h
index 21a7ffa..2b1f5d9 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -50,7 +50,7 @@ int setenv(const char *, const char *);
int env_pop_context(void);
int env_push_context(void);
-int envfs_load(char *filename, char *dirname);
+int envfs_load(char *filename, char *dirname, int *crc);
int envfs_save(char *filename, char *dirname);
int export(const char *);
diff --git a/scripts/bareboxenv.c b/scripts/bareboxenv.c
index 5c7f10e..af012e8 100644
--- a/scripts/bareboxenv.c
+++ b/scripts/bareboxenv.c
@@ -186,7 +186,7 @@ int main(int argc, char *argv[])
if (load) {
printf("loading env from file %s to %s\n", filename, dirname);
- envfs_load(filename, dirname);
+ envfs_load(filename, dirname, NULL);
}
if (save) {
printf("saving contents of %s to file %s\n", dirname, filename);
--
1.7.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 3/5] add multi environment support
2010-03-26 12:52 Environment handling fixes and support for multiple environment backends Luotao Fu
2010-03-26 12:52 ` [PATCH 1/5] fix error return value while loading environment Luotao Fu
2010-03-26 12:52 ` [PATCH 2/5] crc value handling and dry run mode in envfs_load Luotao Fu
@ 2010-03-26 12:52 ` Luotao Fu
2010-03-26 12:52 ` [PATCH 4/5] Add multi env support to saveenv and loadenv commands Luotao Fu
2010-03-26 12:52 ` [PATCH 5/5] add general check_and_erase callback to environment handling Luotao Fu
4 siblings, 0 replies; 6+ messages in thread
From: Luotao Fu @ 2010-03-26 12:52 UTC (permalink / raw)
To: sha; +Cc: barebox, Luotao Fu
We can deal with multiple environments now in our barebox. With this option
enabled barebox will scan all environment partions on start up and load the
environment from the first loadable environment partition it finds. Also it will
check the content of all environment parttions and autmotically synchronize
content of any outdated partition.
Signed-off-by: Luotao Fu <l.fu@pengutronix.de>
---
common/Kconfig | 11 ++++++
common/startup.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 92 insertions(+), 9 deletions(-)
diff --git a/common/Kconfig b/common/Kconfig
index 4c4a627..ffb0257 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -40,6 +40,17 @@ config LOCALVERSION_AUTO
which is done within the script "scripts/setlocalversion".)
+config MULTI_ENV_HANDLING
+ select ENV_HANDLING
+ bool "Enable handling multiple environment partitions"
+ help
+ We can have multiple environments in our barebox. With this option
+ enabled barebox will scan all environment partions on start up and
+ load the environment from the first loadable environment partition
+ it finds. Also it will check if the content of all environment
+ parttions are synchronized and autmotically synchronize content of
+ the outdated partition.
+
config BOARDINFO
string
diff --git a/common/startup.c b/common/startup.c
index 6dca270..af929f8 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -110,6 +110,87 @@ static int mount_root(void)
}
fs_initcall(mount_root);
+#ifdef CONFIG_ENV_HANDLING
+#ifdef CONFIG_MULTI_ENV_HANDLING
+static int init_envfs_load(void)
+{
+ char *dirname = "/env";
+ char file[9 + 5]; /* '/dev/env.....' */
+ int i = 0, j = 0, no_more_parts = 0;
+ int crc = 0, crc_ref = 0;
+ int rc;
+ struct stat file_info;
+
+ while (1) {
+ sprintf(file, "/dev/env%d", i);
+
+ if (stat(file, &file_info) != 0) {
+ no_more_parts = 1;
+ break;
+ }
+
+ /* first loadable environment is considered to be
+ * reference */
+ if (envfs_load(file, dirname, &crc_ref) == 0)
+ break;
+
+ i++;
+ }
+
+ /* no loadable environment partition found */
+ if (no_more_parts == 1) {
+#ifdef CONFIG_DEFAULT_ENVIRONMENT
+ printf("no valid environment found. "
+ "Using default environment\n");
+ envfs_load("/dev/defaultenv", "/env", &crc_ref);
+#endif
+ goto out;
+ }
+
+ /* Now try to restore, if any, the previous failed partitions */
+ for (j = 0; j < i; j++) {
+ sprintf(file, "/dev/env%d", j);
+ if (envfs_save(file, dirname))
+ printf("failed to sync environment on %s\n", file);
+ }
+
+ /* proceed to scan further env partitions */
+ while (1) {
+ i++;
+ sprintf(file, "/dev/env%d", i);
+
+ if (stat(file, &file_info) != 0)
+ break;
+
+ /* sync partition if loading failed or crc mismatch with
+ * the reference*/
+ rc = envfs_load(file, NULL, &crc);
+ if (rc != 0 || crc != crc_ref) {
+ if (envfs_save(file, dirname))
+ printf("failed to sync environment on %s\n",
+ file);
+ }
+ }
+out:
+ return 0;
+}
+#else /* MULTI_ENV_HANDLING */
+static int init_envfs_load(void)
+{
+ if (envfs_load("/dev/env0", "/env")) {
+#ifdef CONFIG_DEFAULT_ENVIRONMENT
+ printf("no valid environment found. "
+ "Using default environment\n");
+ envfs_load("/dev/defaultenv", "/env", NULL);
+#endif
+ }
+
+ return 0;
+}
+#endif /* MULTI_ENV_HANDLING */
+late_initcall(init_envfs_load);
+#endif /* CONFIG_ENV_HANDLING */
+
void start_barebox (void)
{
initcall_t *initcall;
@@ -137,15 +218,6 @@ void start_barebox (void)
display_meminfo();
-#ifdef CONFIG_ENV_HANDLING
- if (envfs_load("/dev/env0", "/env")) {
-#ifdef CONFIG_DEFAULT_ENVIRONMENT
- printf("no valid environment found on /dev/env0. "
- "Using default environment\n");
- envfs_load("/dev/defaultenv", "/env");
-#endif
- }
-#endif
printf("running /env/bin/init...\n");
if (!stat("/env/bin/init", &s)) {
--
1.7.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/5] Add multi env support to saveenv and loadenv commands.
2010-03-26 12:52 Environment handling fixes and support for multiple environment backends Luotao Fu
` (2 preceding siblings ...)
2010-03-26 12:52 ` [PATCH 3/5] add multi environment support Luotao Fu
@ 2010-03-26 12:52 ` Luotao Fu
2010-03-26 12:52 ` [PATCH 5/5] add general check_and_erase callback to environment handling Luotao Fu
4 siblings, 0 replies; 6+ messages in thread
From: Luotao Fu @ 2010-03-26 12:52 UTC (permalink / raw)
To: sha; +Cc: barebox, Luotao Fu
saveenv and loadenv commands now support multiple environments. If
CONFIG_MULTI_ENV_HANDLING is turned on. saveenv will try to save the environment
into all available environment backends, while loadenv will load the environment
from the first loadable backend it will find. Also updated the help text.
Signed-off-by: Luotao Fu <l.fu@pengutronix.de>
---
commands/loadenv.c | 49 ++++++++++++++++++++++++++++++++++++++--------
commands/saveenv.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 90 insertions(+), 13 deletions(-)
diff --git a/commands/loadenv.c b/commands/loadenv.c
index 3da67d9..cb66bb4 100644
--- a/commands/loadenv.c
+++ b/commands/loadenv.c
@@ -26,31 +26,62 @@
#include <common.h>
#include <command.h>
#include <environment.h>
+#include <fs.h>
+#include <linux/stat.h>
static int do_loadenv(struct command *cmdtp, int argc, char *argv[])
{
- char *filename, *dirname;
+ char *dirname;
+ int rc = 0;
+ int i = 0;
+ char file[9 + 5]; /* '/dev/env.....' */
+#ifdef CONFIG_MULTI_ENV_HANDLING
+ struct stat file_info;
+#endif
if (argc < 3)
dirname = "/env";
else
dirname = argv[2];
- if (argc < 2)
- filename = "/dev/env0";
- else
- filename = argv[1];
- printf("loading environment from %s\n", filename);
+ if (argc > 1) {
+ rc = envfs_load(argv[1], dirname, NULL);
+ return rc ? 1 : 0;
+ }
+
+#ifdef CONFIG_MULTI_ENV_HANDLING
+ /* default filename, loop over all /dev/env[number] till one succeeds */
+ while (1) {
+ sprintf(file, "/dev/env%d", i);
+
+ if (stat(file, &file_info) != 0)
+ break;
+
+ rc = envfs_load(file, dirname, NULL);
+ if (!rc)
+ break;
+ i++;
+ }
+#else
+ sprintf(file, "/dev/env0");
+ rc = envfs_load(file, dirname);
+#endif
- return envfs_load(filename, dirname, NULL);
+ return rc ? 1 : 0;
}
static const __maybe_unused char cmd_loadenv_help[] =
"Usage: loadenv [ENVFS] [DIRECTORY]\n"
"Load the persistent storage contained in <envfs> to the directory\n"
"<directory>.\n"
-"If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n"
-"Note that envfs can only handle files. Directories are skipped silently.\n";
+#ifdef CONFIG_MULTI_ENV_HANDLING
+"If ommitted <directory> defaults the command will scan for available"
+" environment backend storages, if more than one of such storages are"
+" available, the environment will try to load the environment from the"
+" loadable backend in ascending order (env0 -> env1).\n";
+#else
+"If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n";
+#endif
BAREBOX_CMD_START(loadenv)
.cmd = do_loadenv,
diff --git a/commands/saveenv.c b/commands/saveenv.c
index 42ea58f..668e6ae 100644
--- a/commands/saveenv.c
+++ b/commands/saveenv.c
@@ -29,11 +29,13 @@
#include <fs.h>
#include <fcntl.h>
#include <environment.h>
+#include <linux/stat.h>
-static int do_saveenv(struct command *cmdtp, int argc, char *argv[])
+static int saveenv(char *filename, char *dirname)
{
int ret, fd;
- char *filename, *dirname;
+ char filename[9 + 5];
+ char *dirname;
printf("saving environment\n");
if (argc < 3)
@@ -91,6 +93,44 @@ static int do_saveenv(struct command *cmdtp, int argc, char *argv[])
ret = 0;
out:
close(fd);
+
+ return 0;
+}
+
+static int do_saveenv(struct command *cmdtp, int argc, char *argv[])
+{
+ int ret = 0;
+ char *dirname;
+ int i = 0;
+ char file[9 + 5];
+#ifdef CONFIG_MULTI_ENV_HANDLING
+ struct stat file_info;
+#endif
+
+ printf("saving environment\n");
+ if (argc < 3)
+ dirname = "/env";
+ else
+ dirname = argv[2];
+
+ if (argc > 1)
+ return saveenv(argv[1], dirname);
+#ifdef CONFIG_MULTI_ENV_HANDLING
+ /* default filename, save environment to all /dev/env[number] */
+ while (1) {
+ sprintf(file, "/dev/env%d", i);
+ if (stat(file, &file_info))
+ break;
+
+ ret |= saveenv(file, dirname);
+
+ i++;
+ }
+#else
+ sprintf(file, "/dev/env%d", 0);
+ ret = saveenv(file, dirname);
+#endif
+
return ret;
}
@@ -98,8 +138,14 @@ static const __maybe_unused char cmd_saveenv_help[] =
"Usage: saveenv [<envfs>] [<directory>]\n"
"Save the files in <directory> to the persistent storage device <envfs>.\n"
"<envfs> is normally a block in flash, but could be any other file.\n"
-"If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n"
-"Note that envfs can only handle files. Directories are skipped silently.\n";
+#ifdef CONFIG_MULTI_ENV_HANDLING
+"If ommitted <directory> defaults the command will scan for available"
+" environment backend storages, if more than one of such storages are"
+" available, the environment will be stored into all of them ascending"
+" order (env0 -> env1).\n";
+#else
+"If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n";
+#endif
BAREBOX_CMD_START(saveenv)
.cmd = do_saveenv,
--
1.7.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 5/5] add general check_and_erase callback to environment handling
2010-03-26 12:52 Environment handling fixes and support for multiple environment backends Luotao Fu
` (3 preceding siblings ...)
2010-03-26 12:52 ` [PATCH 4/5] Add multi env support to saveenv and loadenv commands Luotao Fu
@ 2010-03-26 12:52 ` Luotao Fu
4 siblings, 0 replies; 6+ messages in thread
From: Luotao Fu @ 2010-03-26 12:52 UTC (permalink / raw)
To: sha; +Cc: barebox, Luotao Fu
Checking and erasing the envfs partition prior to saving the environment is
required both in start initial environment loading and saveenv commands. Hence
we add a general callback to do this task. This callback is barebox only and
cannot be used in the script utils.
Signed-off-by: Luotao Fu <l.fu@pengutronix.de>
---
commands/saveenv.c | 46 +++++-----------------------------------------
common/environment.c | 34 ++++++++++++++++++++++++++++++++++
common/startup.c | 7 +++++++
include/environment.h | 1 +
4 files changed, 47 insertions(+), 41 deletions(-)
diff --git a/commands/saveenv.c b/commands/saveenv.c
index 668e6ae..efc89bd 100644
--- a/commands/saveenv.c
+++ b/commands/saveenv.c
@@ -33,50 +33,17 @@
static int saveenv(char *filename, char *dirname)
{
- int ret, fd;
- char filename[9 + 5];
- char *dirname;
-
- printf("saving environment\n");
- if (argc < 3)
- dirname = "/env";
- else
- dirname = argv[2];
- if (argc < 2)
- filename = "/dev/env0";
- else
- filename = argv[1];
-
- fd = open(filename, O_WRONLY | O_CREAT);
- if (fd < 0) {
- printf("could not open %s: %s\n", filename, errno_str());
- return 1;
- }
-
- ret = protect(fd, ~0, 0, 0);
-
- /* ENOSYS is no error here, many devices do not need it */
- if (ret && errno != -ENOSYS) {
- printf("could not unprotect %s: %s\n", filename, errno_str());
- close(fd);
- return 1;
- }
-
- ret = erase(fd, ~0, 0);
+ int ret = 0;
+ int fd;
- /* ENOSYS is no error here, many devices do not need it */
- if (ret && errno != -ENOSYS) {
- printf("could not erase %s: %s\n", filename, errno_str());
- close(fd);
+ ret = file_check_and_erase(filename);
+ if (ret)
return 1;
- }
-
- close(fd);
ret = envfs_save(filename, dirname);
if (ret) {
printf("saveenv failed\n");
- goto out;
+ return 1;
}
fd = open(filename, O_WRONLY | O_CREAT);
@@ -90,10 +57,7 @@ static int saveenv(char *filename, char *dirname)
return 1;
}
- ret = 0;
-out:
close(fd);
-
return 0;
}
diff --git a/common/environment.c b/common/environment.c
index e79ec99..da34069 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -93,6 +93,40 @@ out:
return 1;
}
+#ifdef __BAREBOX__
+int file_check_and_erase(char *filename)
+{
+ int fd;
+ int ret;
+
+ fd = open(filename, O_WRONLY | O_CREAT);
+ if (fd < 0) {
+ printf("could not open %s: %s\n", filename, errno_str());
+ return errno;
+ }
+
+ ret = protect(fd, ~0, 0, 0);
+
+ /* ENOSYS is no error here, many devices do not need it */
+ if (ret && errno != -ENOSYS) {
+ printf("could not unprotect %s: %s\n", filename, errno_str());
+ return errno;
+ }
+
+ ret = erase(fd, ~0, 0);
+
+ /* ENOSYS is no error here, many devices do not need it */
+ if (ret && errno != -ENOSYS) {
+ printf("could not erase %s: %s\n", filename, errno_str());
+ close(fd);
+ return errno;
+ }
+
+ close(fd);
+ return 0;
+}
+#endif /* __BAREBOX__ */
+
/**
* Make the current environment persistent
* @param[in] filename where to store
diff --git a/common/startup.c b/common/startup.c
index af929f8..c589646 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -150,6 +150,10 @@ static int init_envfs_load(void)
/* Now try to restore, if any, the previous failed partitions */
for (j = 0; j < i; j++) {
sprintf(file, "/dev/env%d", j);
+
+ if (file_check_and_erase(file))
+ continue;
+
if (envfs_save(file, dirname))
printf("failed to sync environment on %s\n", file);
}
@@ -166,6 +170,9 @@ static int init_envfs_load(void)
* the reference*/
rc = envfs_load(file, NULL, &crc);
if (rc != 0 || crc != crc_ref) {
+ if (file_check_and_erase(file))
+ continue;
+
if (envfs_save(file, dirname))
printf("failed to sync environment on %s\n",
file);
diff --git a/include/environment.h b/include/environment.h
index 2b1f5d9..b2b95e0 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -58,6 +58,7 @@ int export(const char *);
struct stat;
int file_size_action(const char *, struct stat *, void *, int);
int file_save_action(const char *, struct stat *, void *, int);
+int file_check_and_erase(char *filename);
#endif /* __BAREBOX__ */
--
1.7.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 6+ messages in thread