mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Luotao Fu <l.fu@pengutronix.de>
To: sha@pengutronix.de
Cc: barebox@lists.infradead.org, Luotao Fu <l.fu@pengutronix.de>
Subject: [PATCH 5/5] add general check_and_erase callback to environment handling
Date: Fri, 26 Mar 2010 13:52:29 +0100	[thread overview]
Message-ID: <1269607949-5336-6-git-send-email-l.fu@pengutronix.de> (raw)
In-Reply-To: <1269607949-5336-1-git-send-email-l.fu@pengutronix.de>

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

      parent reply	other threads:[~2010-03-26 12:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [PATCH 3/5] add multi environment support 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 ` Luotao Fu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1269607949-5336-6-git-send-email-l.fu@pengutronix.de \
    --to=l.fu@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=sha@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox