mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] filetype: Add detection for barebox environment
@ 2014-02-20 13:49 Sascha Hauer
  2014-02-20 13:49 ` [PATCH 2/4] environment: constify arguments Sascha Hauer
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Sascha Hauer @ 2014-02-20 13:49 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/filetype.c  | 4 ++++
 include/filetype.h | 1 +
 2 files changed, 5 insertions(+)

diff --git a/common/filetype.c b/common/filetype.c
index 8cdf827..0b5da30 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -23,6 +23,7 @@
 #include <fs.h>
 #include <malloc.h>
 #include <errno.h>
+#include <envfs.h>
 
 struct filetype_str {
 	const char *name;	/* human readable filetype */
@@ -51,6 +52,7 @@ static const struct filetype_str filetype_str[] = {
 	[filetype_ext] = { "ext filesystem", "ext" },
 	[filetype_gpt] = { "GUID Partition Table", "gpt" },
 	[filetype_bpk] = { "Binary PacKage", "bpk" },
+	[filetype_barebox_env] = { "barebox environment file", "bbenv" },
 };
 
 const char *file_type_to_string(enum filetype f)
@@ -188,6 +190,8 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
 
 	if (strncmp(buf8, "#!/bin/sh", 9) == 0)
 		return filetype_sh;
+	if (buf[0] == ENVFS_32(ENVFS_MAGIC))
+		return filetype_barebox_env;
 
 	if (bufsize < 32)
 		return filetype_unknown;
diff --git a/include/filetype.h b/include/filetype.h
index ffefe1c..c20a4f9 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -29,6 +29,7 @@ enum filetype {
 	filetype_gpt,
 	filetype_ubifs,
 	filetype_bpk,
+	filetype_barebox_env,
 	filetype_max,
 };
 
-- 
1.8.5.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/4] environment: constify arguments
  2014-02-20 13:49 [PATCH 1/4] filetype: Add detection for barebox environment Sascha Hauer
@ 2014-02-20 13:49 ` Sascha Hauer
  2014-02-20 13:49 ` [PATCH 3/4] environment: Add function to load envfs from buffer Sascha Hauer
  2014-02-20 13:49 ` [PATCH 4/4] defaultenv: Align defaultenv array Sascha Hauer
  2 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2014-02-20 13:49 UTC (permalink / raw)
  To: barebox

The directory arguments to envfs_load and envfs_save can
be const.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/environment.c | 4 ++--
 include/envfs.h      | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/common/environment.c b/common/environment.c
index 6f06bfc..695baf7 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -162,7 +162,7 @@ out:
  * Note: This function will also be used on the host! See note in the header
  * of this file.
  */
-int envfs_save(const char *filename, char *dirname)
+int envfs_save(const char *filename, const char *dirname)
 {
 	struct envfs_super *super;
 	int envfd, size, ret;
@@ -227,7 +227,7 @@ EXPORT_SYMBOL(envfs_save);
  * Note: This function will also be used on the host! See note in the header
  * of this file.
  */
-int envfs_load(const char *filename, char *dir, unsigned flags)
+int envfs_load(const char *filename, const char *dir, unsigned flags)
 {
 	struct envfs_super super;
 	void *buf = NULL, *buf_free = NULL;
diff --git a/include/envfs.h b/include/envfs.h
index f8b24ed..94d1353 100644
--- a/include/envfs.h
+++ b/include/envfs.h
@@ -90,8 +90,8 @@ struct envfs_super {
 #endif
 
 #define ENV_FLAG_NO_OVERWRITE	(1 << 0)
-int envfs_load(const char *filename, char *dirname, unsigned flags);
-int envfs_save(const char *filename, char *dirname);
+int envfs_load(const char *filename, const char *dirname, unsigned flags);
+int envfs_save(const char *filename, const char *dirname);
 
 /* defaults to /dev/env0 */
 #ifdef CONFIG_ENV_HANDLING
-- 
1.8.5.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/4] environment: Add function to load envfs from buffer
  2014-02-20 13:49 [PATCH 1/4] filetype: Add detection for barebox environment Sascha Hauer
  2014-02-20 13:49 ` [PATCH 2/4] environment: constify arguments Sascha Hauer
@ 2014-02-20 13:49 ` Sascha Hauer
  2014-02-20 15:16   ` Alexander Aring
  2014-02-20 13:49 ` [PATCH 4/4] defaultenv: Align defaultenv array Sascha Hauer
  2 siblings, 1 reply; 7+ messages in thread
From: Sascha Hauer @ 2014-02-20 13:49 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/environment.c | 181 +++++++++++++++++++++++++++++++++------------------
 include/envfs.h      |   1 +
 2 files changed, 117 insertions(+), 65 deletions(-)

diff --git a/common/environment.c b/common/environment.c
index 695baf7..19fb027 100644
--- a/common/environment.c
+++ b/common/environment.c
@@ -218,78 +218,52 @@ out1:
 }
 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
- * @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(const char *filename, const char *dir, unsigned flags)
+static int envfs_check_super(struct envfs_super *super, size_t *size)
 {
-	struct envfs_super super;
-	void *buf = NULL, *buf_free = NULL;
-	int envfd;
-	int fd, ret = 0;
-	char *str, *tmp;
-	int headerlen_full;
-	unsigned long size;
-	/* for envfs < 1.0 */
-	struct envfs_inode_end inode_end_dummy;
-
-	inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO);
-	inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC);
-
-	envfd = open(filename, O_RDONLY);
-	if (envfd < 0) {
-		printf("Open %s %s\n", filename, errno_str());
-		return -1;
+	if (ENVFS_32(super->magic) != ENVFS_MAGIC) {
+		printf("envfs: wrong magic\n");
+		return -EIO;
 	}
 
-	/* read superblock */
-	ret = read(envfd, &super, sizeof(struct envfs_super));
-	if ( ret < sizeof(struct envfs_super)) {
-		perror("read");
-		ret = -errno;
-		goto out;
+	if (crc32(0, super, sizeof(*super) - 4) != ENVFS_32(super->sb_crc)) {
+		printf("wrong crc on env superblock\n");
+		return -EIO;
 	}
 
-	if ( ENVFS_32(super.magic) != ENVFS_MAGIC) {
-		printf("envfs: wrong magic on %s\n", filename);
-		ret = -EIO;
-		goto out;
-	}
+	if (super->major < ENVFS_MAJOR)
+		printf("envfs version %d.%d loaded into %d.%d\n",
+			super->major, super->minor,
+			ENVFS_MAJOR, ENVFS_MINOR);
 
-	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;
-	}
+	*size = ENVFS_32(super->size);
 
-	size = ENVFS_32(super.size);
-	buf = xmalloc(size);
-	buf_free = buf;
-	ret = read(envfd, buf, size);
-	if (ret < size) {
-		perror("read");
-		ret = -errno;
-		goto out;
-	}
+	return 0;
+}
 
-	if (crc32(0, (unsigned char *)buf, size)
-		     != ENVFS_32(super.crc)) {
+static int envfs_check_data(struct envfs_super *super, void *buf, size_t size)
+{
+	uint32_t crc;
+
+	crc = crc32(0, buf, size);
+	if (crc != ENVFS_32(super->crc)) {
 		printf("wrong crc on env\n");
-		ret = -EIO;
-		goto out;
+		return -EIO;
 	}
 
-	if (super.major < ENVFS_MAJOR)
-		printf("envfs version %d.%d loaded into %d.%d\n",
-			super.major, super.minor,
-			ENVFS_MAJOR, ENVFS_MINOR);
+	return 0;
+}
+
+static int envfs_load_data(void *buf, size_t size, const char *dir, unsigned flags)
+{
+	struct envfs_super super;
+	int fd, ret = 0;
+	char *str, *tmp;
+	int headerlen_full;
+	/* for envfs < 1.0 */
+	struct envfs_inode_end inode_end_dummy;
+
+	inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO);
+	inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC);
 
 	while (size) {
 		struct envfs_inode *inode;
@@ -300,7 +274,7 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)
 		buf += sizeof(struct envfs_inode);
 
 		if (ENVFS_32(inode->magic) != ENVFS_INODE_MAGIC) {
-			printf("envfs: wrong magic on %s\n", filename);
+			printf("envfs: wrong magic\n");
 			ret = -EIO;
 			goto out;
 		}
@@ -324,7 +298,7 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)
 		buf += headerlen_full;
 
 		if (ENVFS_32(inode_end->magic) != ENVFS_INODE_END_MAGIC) {
-			printf("envfs: wrong inode_end_magic on %s\n", filename);
+			printf("envfs: wrong inode_end_magic\n");
 			ret = -EIO;
 			goto out;
 		}
@@ -370,9 +344,86 @@ skip:
 
 	ret = 0;
 out:
+	return ret;
+}
+
+int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags)
+{
+	int ret;
+	size_t size;
+	struct envfs_super *super = buf;
+
+	buf = super + 1;
+
+	ret = envfs_check_super(super, &size);
+	if (ret)
+		return ret;
+
+	ret = envfs_check_data(super, buf, size);
+	if (ret)
+		return ret;
+
+	ret = envfs_load_data(buf, size, dir, flags);
+
+	return ret;
+}
+
+/**
+ * Restore the last environment into the current one
+ * @param[in] filename from where to restore
+ * @param[in] dir where to store the last content
+ * @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(const char *filename, const char *dir, unsigned flags)
+{
+	struct envfs_super super;
+	void *buf = NULL;
+	int envfd;
+	int ret = 0;
+	size_t size;
+
+	envfd = open(filename, O_RDONLY);
+	if (envfd < 0) {
+		printf("Open %s %s\n", filename, errno_str());
+		return -1;
+	}
+
+	/* read superblock */
+	ret = read(envfd, &super, sizeof(struct envfs_super));
+	if ( ret < sizeof(struct envfs_super)) {
+		perror("read");
+		ret = -errno;
+		goto out;
+	}
+
+	ret = envfs_check_super(&super, &size);
+	if (ret)
+		goto out;
+
+	buf = xmalloc(size);
+	ret = read(envfd, buf, size);
+	if (ret < size) {
+		perror("read");
+		ret = -errno;
+		goto out;
+	}
+
+	ret = envfs_check_data(&super, buf, size);
+	if (ret)
+		goto out;
+
+	ret = envfs_load_data(buf, size, dir, flags);
+	if (ret)
+		goto out;
+
+	ret = 0;
+out:
 	close(envfd);
-	if (buf_free)
-		free(buf_free);
+	free(buf);
+
 	return ret;
 }
 
diff --git a/include/envfs.h b/include/envfs.h
index 94d1353..b63683c 100644
--- a/include/envfs.h
+++ b/include/envfs.h
@@ -92,6 +92,7 @@ struct envfs_super {
 #define ENV_FLAG_NO_OVERWRITE	(1 << 0)
 int envfs_load(const char *filename, const char *dirname, unsigned flags);
 int envfs_save(const char *filename, const char *dirname);
+int envfs_load_from_buf(void *buf, int len, const char *dir, unsigned flags);
 
 /* defaults to /dev/env0 */
 #ifdef CONFIG_ENV_HANDLING
-- 
1.8.5.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/4] defaultenv: Align defaultenv array
  2014-02-20 13:49 [PATCH 1/4] filetype: Add detection for barebox environment Sascha Hauer
  2014-02-20 13:49 ` [PATCH 2/4] environment: constify arguments Sascha Hauer
  2014-02-20 13:49 ` [PATCH 3/4] environment: Add function to load envfs from buffer Sascha Hauer
@ 2014-02-20 13:49 ` Sascha Hauer
  2 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2014-02-20 13:49 UTC (permalink / raw)
  To: barebox

The default environment buffer is an unsigned char array and thus
may be unaligned. Some decompression algorithms expect the buffer
to be sufficiently aligned for u32 accesses. We make this sure by
copying the default env to a temporary buffer. Instead of doing this
just add a __aligned(4) to the default environment.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/Makefile  |  2 +-
 common/startup.c | 11 +----------
 2 files changed, 2 insertions(+), 11 deletions(-)

diff --git a/common/Makefile b/common/Makefile
index 3cfaae2..a52e96b 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -77,7 +77,7 @@ $(obj)/barebox_default_env: FORCE
 	$(call cmd,envs)
 
 quiet_cmd_env_h = ENVH    $@
-cmd_env_h = cat $< | (cd $(obj) && $(objtree)/scripts/bin2c default_environment) > $@; \
+cmd_env_h = cat $< | (cd $(obj) && $(objtree)/scripts/bin2c "__aligned(4) default_environment") > $@; \
 	echo "static const int default_environment_uncompress_size=`stat -c%s $(obj)/barebox_default_env`;" >> $@
 
 $(obj)/barebox_default_env.h: $(obj)/barebox_default_env$(DEFAULT_COMPRESSION_SUFFIX) FORCE
diff --git a/common/startup.c b/common/startup.c
index 4bc5628..6847b61 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -53,21 +53,12 @@ static int register_default_env(void)
 	void *defaultenv;
 
 	if (!IS_ENABLED(CONFIG_DEFAULT_COMPRESSION_NONE)) {
-		void *tmp = malloc(default_environment_size);
-
-		if (!tmp)
-			return -ENOMEM;
-
-		memcpy(tmp, default_environment, default_environment_size);
 
 		defaultenv = xzalloc(default_environment_uncompress_size);
 
-		ret = uncompress(tmp, default_environment_size,
+		ret = uncompress(default_environment, default_environment_size,
 				NULL, NULL,
 				defaultenv, NULL, uncompress_err_stdout);
-
-		free(tmp);
-
 		if (ret) {
 			free(defaultenv);
 			return ret;
-- 
1.8.5.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/4] environment: Add function to load envfs from buffer
  2014-02-20 13:49 ` [PATCH 3/4] environment: Add function to load envfs from buffer Sascha Hauer
@ 2014-02-20 15:16   ` Alexander Aring
  2014-02-22 18:28     ` Sascha Hauer
  0 siblings, 1 reply; 7+ messages in thread
From: Alexander Aring @ 2014-02-20 15:16 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

Hi Sascha,

some little (maybe interesting) note, but all patches in this series
looks fine for me. :-)

On Thu, Feb 20, 2014 at 02:49:28PM +0100, Sascha Hauer wrote:
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  common/environment.c | 181 +++++++++++++++++++++++++++++++++------------------
>  include/envfs.h      |   1 +
>  2 files changed, 117 insertions(+), 65 deletions(-)
> 
> diff --git a/common/environment.c b/common/environment.c
> index 695baf7..19fb027 100644
> --- a/common/environment.c
> +++ b/common/environment.c
> @@ -218,78 +218,52 @@ out1:
>  }
>  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
> - * @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(const char *filename, const char *dir, unsigned flags)
> +static int envfs_check_super(struct envfs_super *super, size_t *size)
>  {
> -	struct envfs_super super;
> -	void *buf = NULL, *buf_free = NULL;
> -	int envfd;
> -	int fd, ret = 0;
> -	char *str, *tmp;
> -	int headerlen_full;
> -	unsigned long size;
> -	/* for envfs < 1.0 */
> -	struct envfs_inode_end inode_end_dummy;
> -
> -	inode_end_dummy.mode = ENVFS_32(S_IRWXU | S_IRWXG | S_IRWXO);
> -	inode_end_dummy.magic = ENVFS_32(ENVFS_INODE_END_MAGIC);
> -
> -	envfd = open(filename, O_RDONLY);
> -	if (envfd < 0) {
> -		printf("Open %s %s\n", filename, errno_str());
> -		return -1;
> +	if (ENVFS_32(super->magic) != ENVFS_MAGIC) {
> +		printf("envfs: wrong magic\n");
> +		return -EIO;

In this case only on big endians machines:
if (super->magic != ENVFS_32(ENVFS_MAGIC))

is faster than:
if (ENVFS_32(super->magic) != ENVFS_MAGIC)

It's save (I suppose) ca. 4 instruktions... so it doesn't matter
to change it.

Regards
Alex

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/4] environment: Add function to load envfs from buffer
  2014-02-20 15:16   ` Alexander Aring
@ 2014-02-22 18:28     ` Sascha Hauer
  2014-02-22 19:40       ` Alexander Aring
  0 siblings, 1 reply; 7+ messages in thread
From: Sascha Hauer @ 2014-02-22 18:28 UTC (permalink / raw)
  To: Alexander Aring; +Cc: barebox

Hi Alex,

On Thu, Feb 20, 2014 at 04:16:05PM +0100, Alexander Aring wrote:
> Hi Sascha,
> 
> > -		printf("Open %s %s\n", filename, errno_str());
> > -		return -1;
> > +	if (ENVFS_32(super->magic) != ENVFS_MAGIC) {
> > +		printf("envfs: wrong magic\n");
> > +		return -EIO;
> 
> In this case only on big endians machines:
> if (super->magic != ENVFS_32(ENVFS_MAGIC))
> 
> is faster than:
> if (ENVFS_32(super->magic) != ENVFS_MAGIC)
> 
> It's save (I suppose) ca. 4 instruktions... so it doesn't matter
> to change it.

You're right that it's slightly more efficient. I think that it's more
obviously correct converting the 'foreign' value to host order, so I
think I'll keep my version.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/4] environment: Add function to load envfs from buffer
  2014-02-22 18:28     ` Sascha Hauer
@ 2014-02-22 19:40       ` Alexander Aring
  0 siblings, 0 replies; 7+ messages in thread
From: Alexander Aring @ 2014-02-22 19:40 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On Sat, Feb 22, 2014 at 07:28:51PM +0100, Sascha Hauer wrote:
> Hi Alex,
> 
> On Thu, Feb 20, 2014 at 04:16:05PM +0100, Alexander Aring wrote:
> > Hi Sascha,
> > 
> > > -		printf("Open %s %s\n", filename, errno_str());
> > > -		return -1;
> > > +	if (ENVFS_32(super->magic) != ENVFS_MAGIC) {
> > > +		printf("envfs: wrong magic\n");
> > > +		return -EIO;
> > 
> > In this case only on big endians machines:
> > if (super->magic != ENVFS_32(ENVFS_MAGIC))
> > 
> > is faster than:
> > if (ENVFS_32(super->magic) != ENVFS_MAGIC)
> > 
> > It's save (I suppose) ca. 4 instruktions... so it doesn't matter
> > to change it.
> 
> You're right that it's slightly more efficient. I think that it's more
> obviously correct converting the 'foreign' value to host order, so I
> think I'll keep my version.

thanks for your answer. Yeah, maybe I am hanging a little bit too much
on netdev mailinglist where all netdevs checks on something like this
because it's mostly in some atomic context. This need to be fast and
not beautiful in a kind of human thinking. :-)

- Alex

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-02-22 19:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-20 13:49 [PATCH 1/4] filetype: Add detection for barebox environment Sascha Hauer
2014-02-20 13:49 ` [PATCH 2/4] environment: constify arguments Sascha Hauer
2014-02-20 13:49 ` [PATCH 3/4] environment: Add function to load envfs from buffer Sascha Hauer
2014-02-20 15:16   ` Alexander Aring
2014-02-22 18:28     ` Sascha Hauer
2014-02-22 19:40       ` Alexander Aring
2014-02-20 13:49 ` [PATCH 4/4] defaultenv: Align defaultenv array Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox