Skip to contentSkip to conversation

tools/env: minor refactorings

PATCH New Andreas Fenkart 2 participants 9 messages Jul 16, 2016 – Jul 23, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover tools/env: minor refactorings

various patches, see individual patch descriptions

Andreas Fenkart (4):
tools/env: return with error if redundant environments have unequal
size
tools/env: kernel-doc for fw_printenv, fw_getenv and fw_parse_script tools/env: move envmatch further up in file to avoid forward
declarations
tools/env: reuse fw_getenv in fw_printenv function

 tools/env/fw_env.c | 107 ++++++++++++++++++++++---------------------------
 tools/env/fw_env.h | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 162 insertions(+), 60 deletions(-)
Signature · 1 line
2.8.1
#1 lore raw

PATCH v2 1/4 tools/env: return with error if redundant environments have unequal size

For double buffering to work, the target buffer must always be big enough to hold all data. This can only be ensured if buffers are of equal size, otherwise one must be smaller and we risk data loss when copying from the bigger to the smaller buffer.

---
 tools/env/fw_env.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
tools/env/fw_env.c +12 -11
index 692abda..b1c8217 100644
@@ -1382,18 +1382,19 @@ static int parse_config(struct env_opts *opts)
 		return -1;
 	}
 
-	if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
-		fprintf (stderr,
-			"Cannot access MTD device %s: %s\n",
-			DEVNAME (1), strerror (errno));
-		return -1;
-	}
+	if (HaveRedundEnv) {
+		if (stat(DEVNAME(1), &st)) {
+			fprintf(stderr,
+				"Cannot access MTD device %s: %s\n",
+				DEVNAME(1), strerror(errno));
+			return -1;
+		}
 
-	if (HaveRedundEnv && ENVSIZE(0) != ENVSIZE(1)) {
-		ENVSIZE(0) = ENVSIZE(1) = min(ENVSIZE(0), ENVSIZE(1));
-		fprintf(stderr,
-			"Redundant environments have inequal size, set to 0x%08lx\n",
-			ENVSIZE(1));
+		if (ENVSIZE(0) != ENVSIZE(1)) {
+			fprintf(stderr,
+				"Redundant environments have unequal size");
+			return -1;
+		}
 	}
 
 	usable_envsize = CUR_ENVSIZE - sizeof(uint32_t);
Signature · 1 line
2.8.1
#2 lore raw

PATCH v2 1/4 tools/env: return with error if redundant environments have unequal size

Quoted 7 lines · Andreas Fenkart

For double buffering to work, the target buffer must always be big enough to hold all data. This can only be ensured if buffers are of equal size, otherwise one must be smaller and we risk data loss when copying from the bigger to the smaller buffer.

With https://patchwork.ozlabs.org/patch/648142/ applied this needs to be reworked, and may not be applicable now, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160722/a5375252/attachment.sig>
#3 lore raw

PATCH v2 2/4 tools/env: kernel-doc for fw_printenv, fw_getenv and fw_parse_script

In reply to Andreas Fenkart

there are two groups of functions:
- application ready tools: fw_setenv/fw_getenv/fw_parse_script these are used, when creating a single binary containing multiple tools (busybox like)
- file access like: open/read/write/close
above functions are implemented on top of these. applications can use those to modify several variables without creating a temporary batch script file
tested with "./scripts/kernel-doc -html -v tools/env/fw_env.h"

---
 tools/env/fw_env.c |   2 +-
 tools/env/fw_env.h | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 115 insertions(+), 2 deletions(-)
tools/env/fw_env.c +1 -1
index b1c8217..fe479e9 100644
@@ -483,7 +483,7 @@ int fw_setenv(int argc, char *argv[], struct env_opts *opts)
 	valc = argc - 1;
 
 	if (env_flags_validate_env_set_params(name, valv, valc) < 0)
-		return 1;
+		return -1;
 
 	len = 0;
 	for (i = 0; i < valc; ++i) {
tools/env/fw_env.h +114 -1

126 lines not on this page · raw message

Signature · 1 line
2.8.1
#4 lore raw

PATCH v2 2/4 tools/env: kernel-doc for fw_printenv, fw_getenv and fw_parse_script

Quoted 10 lines · Andreas Fenkart

there are two groups of functions:
- application ready tools: fw_setenv/fw_getenv/fw_parse_script these are used, when creating a single binary containing multiple tools (busybox like)
- file access like: open/read/write/close
above functions are implemented on top of these. applications can use those to modify several variables without creating a temporary batch script file
tested with "./scripts/kernel-doc -html -v tools/env/fw_env.h"

Quoted 2 lines

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160722/38c674ac/attachment.sig>
#5 lore raw

PATCH v2 3/4 tools/env: move envmatch further up in file to avoid forward declarations

In reply to Andreas Fenkart

forward declaration not needed when re-ordered

---
 tools/env/fw_env.c | 38 ++++++++++++++++++--------------------
 1 file changed, 18 insertions(+), 20 deletions(-)
tools/env/fw_env.c +18 -20
index fe479e9..f155c12 100644
@@ -121,7 +121,6 @@ static unsigned char obsolete_flag = 0;
 #include <env_default.h>
 
 static int flash_io (int mode);
-static char *envmatch (char * s1, char * s2);
 static int parse_config(struct env_opts *opts);
 
 #if defined(CONFIG_FILE)
@@ -147,6 +146,24 @@ static char *skip_blanks(char *s)
 }
 
 /*
+ * s1 is either a simple 'name', or a 'name=value' pair.
+ * s2 is a 'name=value' pair.
+ * If the names match, return the value of s2, else NULL.
+ */
+static char *envmatch(char *s1, char *s2)
+{
+	if (s1 == NULL || s2 == NULL)
+		return NULL;
+
+	while (*s1 == *s2++)
+		if (*s1++ == '=')
+			return s2;
+	if (*s1 == '\0' && *(s2 - 1) == '=')
+		return s2;
+	return NULL;
+}
+
+/**
  * Search the environment for a variable.
  * Return the value, if found, or NULL, if not found.
  */
@@ -1121,25 +1138,6 @@ exit:
 }
 
 /*
- * s1 is either a simple 'name', or a 'name=value' pair.
- * s2 is a 'name=value' pair.
- * If the names match, return the value of s2, else NULL.
- */
-
-static char *envmatch (char * s1, char * s2)
-{
-	if (s1 == NULL || s2 == NULL)
-		return NULL;
-
-	while (*s1 == *s2++)
-		if (*s1++ == '=')
-			return s2;
-	if (*s1 == '\0' && *(s2 - 1) == '=')
-		return s2;
-	return NULL;
-}
-
-/*
  * Prevent confusion if running from erased flash memory
  */
 int fw_env_open(struct env_opts *opts)
Signature · 1 line
2.8.1
#6 lore raw

PATCH v2 3/4 tools/env: move envmatch further up in file to avoid forward declarations

Quoted 4 lines · Andreas Fenkart

forward declaration not needed when re-ordered

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160722/5818ad51/attachment.sig>
#7 lore raw

PATCH v2 4/4 tools/env: reuse fw_getenv in fw_printenv function

In reply to Andreas Fenkart

Try to avoid adhoc iteration of the environment. Reuse fw_getenv to find the variables that should be printed. Only use open-coded iteration when printing all variables.
For backwards compatibility, keep emitting a newline when printing with value_only.

---
 tools/env/fw_env.c | 44 +++++++++++++++++---------------------------
 1 file changed, 17 insertions(+), 27 deletions(-)
tools/env/fw_env.c +17 -27
index f155c12..8b3a132 100644
@@ -249,9 +249,14 @@ int parse_aes_key(char *key, uint8_t *bin_key)
  */
 int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 {
-	char *env, *nxt;
 	int i, rc = 0;
 
+	if (value_only && argc != 1) {
+		fprintf(stderr,
+			"## Error: `-n' option requires exactly one argument\n");
+		return -1;
+	}
+
 	if (!opts)
 		opts = &default_opts;
 
@@ -259,6 +264,7 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 		return -1;
 
 	if (argc == 0) {		/* Print all env variables  */
+		char *env, *nxt;
 		for (env = environment.data; *env; env = nxt + 1) {
 			for (nxt = env; *nxt; ++nxt) {
 				if (nxt >= &environment.data[ENV_SIZE]) {
@@ -273,39 +279,23 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 		return 0;
 	}
 
-	if (value_only && argc != 1) {
-		fprintf(stderr,
-			"## Error: `-n' option requires exactly one argument\n");
-		return -1;
-	}
-
-	for (i = 0; i < argc; ++i) {	/* print single env variables   */
+	for (i = 0; i < argc; ++i) {	/* print a subset of env variables */
 		char *name = argv[i];
 		char *val = NULL;
 
-		for (env = environment.data; *env; env = nxt + 1) {
-
-			for (nxt = env; *nxt; ++nxt) {
-				if (nxt >= &environment.data[ENV_SIZE]) {
-					fprintf (stderr, "## Error: "
-						"environment not terminated\n");
-					return -1;
-				}
-			}
-			val = envmatch (name, env);
-			if (val) {
-				if (!value_only) {
-					fputs (name, stdout);
-					putc ('=', stdout);
-				}
-				puts (val);
-				break;
-			}
-		}
+		val = fw_getenv(name);
 		if (!val) {
 			fprintf (stderr, "## Error: \"%s\" not defined\n", name);
 			rc = -1;
+			continue;
 		}
+
+		if (value_only) {
+			puts(val);
+			break;
+		}
+
+		printf("%s=%s\n", name, val);
 	}
 
 	return rc;
Signature · 1 line
2.8.1
#8 lore raw

PATCH v2 4/4 tools/env: reuse fw_getenv in fw_printenv function

Quoted 7 lines · Andreas Fenkart

Try to avoid adhoc iteration of the environment. Reuse fw_getenv to find the variables that should be printed. Only use open-coded iteration when printing all variables.
For backwards compatibility, keep emitting a newline when printing with value_only.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160722/704df3dc/attachment.sig>