mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* getenv helpers
@ 2013-09-27  6:30 Sascha Hauer
  2013-09-27  6:30 ` [PATCH 1/4] environment variables: introduce new helpers Sascha Hauer
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sascha Hauer @ 2013-09-27  6:30 UTC (permalink / raw)
  To: barebox

This introduces some helpers for getting int values from enironment
variables and uses them where appropriate.

Sascha

----------------------------------------------------------------
Sascha Hauer (4):
      environment variables: introduce new helpers
      loads: use getenv_bool for 'loads_echo'
      bootm: Replace getenv_loadaddr with getenv_ul
      dhcp: replace dhcp_getenv_int with getenv_uint

 arch/arm/lib/armlinux.c | 29 +++++---------------
 commands/bootm.c        |  4 +--
 commands/loads.c        |  7 +----
 common/env.c            | 71 +++++++++++++++++++++++++++++++++++++++++++++----
 include/environment.h   | 31 ++++++++++++++++++---
 net/dhcp.c              | 12 +--------
 6 files changed, 104 insertions(+), 50 deletions(-)


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

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

* [PATCH 1/4] environment variables: introduce new helpers
  2013-09-27  6:30 getenv helpers Sascha Hauer
@ 2013-09-27  6:30 ` Sascha Hauer
  2013-09-27  6:30 ` [PATCH 2/4] loads: use getenv_bool for 'loads_echo' Sascha Hauer
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2013-09-27  6:30 UTC (permalink / raw)
  To: barebox

This introduces some new environment variable helpers and updates
the existing ones. Newly introduced are:

getenv_bool: read a bool variable
getenv_ul: read an unsigned long variable
getenev_uint: read an unsigned int variable
getenv_nonempty: like normal getenv, but does return NULL instead of an
                 empty string

All new helpers take a pointer to the value. This value is only modified
when the variable exists. This allows the following programming scheme:

	unsigned int myvalue = sanedefault;

	getenv_uint("myvalue", &myvalue);

So without checking the return value myvalue contains the best possible
value.

getenv_ull is updated to this scheme.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 arch/arm/lib/armlinux.c | 29 +++++---------------
 common/env.c            | 71 +++++++++++++++++++++++++++++++++++++++++++++----
 include/environment.h   | 31 ++++++++++++++++++---
 3 files changed, 100 insertions(+), 31 deletions(-)

diff --git a/arch/arm/lib/armlinux.c b/arch/arm/lib/armlinux.c
index 40a63ea..75d751b 100644
--- a/arch/arm/lib/armlinux.c
+++ b/arch/arm/lib/armlinux.c
@@ -42,11 +42,9 @@
 static struct tag *params;
 static void *armlinux_bootparams = NULL;
 
-#ifndef CONFIG_ENVIRONMENT_VARIABLES
 static int armlinux_architecture;
 static u32 armlinux_system_rev;
 static u64 armlinux_system_serial;
-#endif
 
 BAREBOX_MAGICVAR(armlinux_architecture, "ARM machine ID");
 BAREBOX_MAGICVAR(armlinux_system_rev, "ARM system revision");
@@ -54,56 +52,41 @@ BAREBOX_MAGICVAR(armlinux_system_serial, "ARM system serial");
 
 void armlinux_set_architecture(int architecture)
 {
-#ifdef CONFIG_ENVIRONMENT_VARIABLES
 	export_env_ull("armlinux_architecture", architecture);
-#else
 	armlinux_architecture = architecture;
-#endif
 }
 
 int armlinux_get_architecture(void)
 {
-#ifdef CONFIG_ENVIRONMENT_VARIABLES
-	return getenv_ull("armlinux_architecture");
-#else
+	getenv_uint("armlinux_architecture", &armlinux_architecture);
+
 	return armlinux_architecture;
-#endif
 }
 
 void armlinux_set_revision(unsigned int rev)
 {
-#ifdef CONFIG_ENVIRONMENT_VARIABLES
 	export_env_ull("armlinux_system_rev", rev);
-#else
 	armlinux_system_rev = rev;
-#endif
 }
 
 unsigned int armlinux_get_revision(void)
 {
-#ifdef CONFIG_ENVIRONMENT_VARIABLES
-	return getenv_ull("armlinux_system_rev");
-#else
+	getenv_uint("armlinux_system_rev", &armlinux_system_rev);
+
 	return armlinux_system_rev;
-#endif
 }
 
 void armlinux_set_serial(u64 serial)
 {
-#ifdef CONFIG_ENVIRONMENT_VARIABLES
 	export_env_ull("armlinux_system_serial", serial);
-#else
 	armlinux_system_serial = serial;
-#endif
 }
 
 u64 armlinux_get_serial(void)
 {
-#ifdef CONFIG_ENVIRONMENT_VARIABLES
-	return getenv_ull("armlinux_system_serial");
-#else
+	getenv_ull("armlinux_system_serial", &armlinux_system_serial);
+
 	return armlinux_system_serial;
-#endif
 }
 
 #ifdef CONFIG_ARM_BOARD_APPEND_ATAG
diff --git a/common/env.c b/common/env.c
index 33a871f..748b655 100644
--- a/common/env.c
+++ b/common/env.c
@@ -267,13 +267,74 @@ void export_env_ull(const char *name, unsigned long long val)
 }
 EXPORT_SYMBOL(export_env_ull);
 
-unsigned long long getenv_ull(const char *name)
+/*
+ * Like regular getenv, but never returns an empty string.
+ * If the string is empty, NULL is returned instead
+ */
+const char *getenv_nonempty(const char *var)
 {
-	const char *valstr = getenv(name);
+	const char *val = getenv(var);
 
-	if (!valstr)
-		return 0;
+	if (val && *val)
+		return val;
 
-	return simple_strtoull(valstr, NULL, 0);
+	return NULL;
+}
+EXPORT_SYMBOL(getenv_nonempty);
+
+int getenv_ull(const char *var , unsigned long long *val)
+{
+	const char *valstr = getenv(var);
+
+	if (!valstr || !*valstr)
+		return -EINVAL;
+
+	*val = simple_strtoull(valstr, NULL, 0);
+
+	return 0;
 }
 EXPORT_SYMBOL(getenv_ull);
+
+int getenv_ul(const char *var , unsigned long *val)
+{
+	const char *valstr = getenv(var);
+
+	if (!valstr || !*valstr)
+		return -EINVAL;
+
+	*val = simple_strtoul(valstr, NULL, 0);
+
+	return 0;
+}
+EXPORT_SYMBOL(getenv_ul);
+
+int getenv_uint(const char *var , unsigned int *val)
+{
+	const char *valstr = getenv(var);
+
+	if (!valstr || !*valstr)
+		return -EINVAL;
+
+	*val = simple_strtoul(valstr, NULL, 0);
+
+	return 0;
+}
+EXPORT_SYMBOL(getenv_uint);
+
+int getenv_bool(const char *var, int *val)
+{
+	const char *valstr = getenv(var);
+
+	if (!valstr || !*valstr)
+		return -EINVAL;
+
+	if (!*valstr)
+		*val = false;
+	else if (*valstr == '0')
+		*val = false;
+	else
+		*val = true;
+
+	return 0;
+}
+EXPORT_SYMBOL(getenv_bool);
diff --git a/include/environment.h b/include/environment.h
index ae1ecf5..a08f597 100644
--- a/include/environment.h
+++ b/include/environment.h
@@ -45,7 +45,11 @@ char *var_name(struct variable_d *);
 const char *getenv(const char *);
 int setenv(const char *, const char *);
 void export_env_ull(const char *name, unsigned long long val);
-unsigned long long getenv_ull(const char *name);
+int getenv_ull(const char *name, unsigned long long *val);
+int getenv_ul(const char *name, unsigned long *val);
+int getenv_uint(const char *name, unsigned int *val);
+int getenv_bool(const char *var, int *val);
+const char *getenv_nonempty(const char *var);
 #else
 static inline char *getenv(const char *var)
 {
@@ -56,10 +60,22 @@ static inline int setenv(const char *var, const char *val)
 {
 	return 0;
 }
+
 static inline void export_env_ull(const char *name, unsigned long long val) {}
-static inline unsigned long long getenv_ull(const char *name)
+
+static inline int getenv_ull(const char *name, unsigned long long *val)
 {
-	return 0;
+	return -EINVAL;
+}
+
+static inline int getenv_ul(const char *name, unsigned long *val)
+{
+	return -EINVAL;
+}
+
+static inline int getenv_uint(const char *name, unsigned int *val)
+{
+	return -EINVAL;
 }
 
 static inline int export(const char *var)
@@ -67,6 +83,15 @@ static inline int export(const char *var)
 	return -EINVAL;
 }
 
+static inline int getenv_bool(const char *var, int *val)
+{
+	return -EINVAL;
+}
+
+static inline const char *getenv_nonempty(const char *var)
+{
+	return NULL;
+}
 #endif
 
 int env_pop_context(void);
-- 
1.8.4.rc3


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

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

* [PATCH 2/4] loads: use getenv_bool for 'loads_echo'
  2013-09-27  6:30 getenv helpers Sascha Hauer
  2013-09-27  6:30 ` [PATCH 1/4] environment variables: introduce new helpers Sascha Hauer
@ 2013-09-27  6:30 ` Sascha Hauer
  2013-09-27  6:30 ` [PATCH 3/4] bootm: Replace getenv_loadaddr with getenv_ul Sascha Hauer
  2013-09-27  6:30 ` [PATCH 4/4] dhcp: replace dhcp_getenv_int with getenv_uint Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2013-09-27  6:30 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/loads.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/commands/loads.c b/commands/loads.c
index bfc465b..58cd24b 100644
--- a/commands/loads.c
+++ b/commands/loads.c
@@ -40,14 +40,9 @@ static int do_load_serial(int argc, char *argv[])
 	ulong offset = 0;
 	ulong addr;
 	int i;
-	const char *env_echo;
 	int rcode = 0;
 
-	if (((env_echo = getenv("loads_echo")) != NULL) && (*env_echo == '1')) {
-		do_echo = 1;
-	} else {
-		do_echo = 0;
-	}
+	getenv_bool("loads_echo", &do_echo);
 
 	if (argc == 2) {
 		offset = simple_strtoul(argv[1], NULL, 16);
-- 
1.8.4.rc3


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

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

* [PATCH 3/4] bootm: Replace getenv_loadaddr with getenv_ul
  2013-09-27  6:30 getenv helpers Sascha Hauer
  2013-09-27  6:30 ` [PATCH 1/4] environment variables: introduce new helpers Sascha Hauer
  2013-09-27  6:30 ` [PATCH 2/4] loads: use getenv_bool for 'loads_echo' Sascha Hauer
@ 2013-09-27  6:30 ` Sascha Hauer
  2013-09-27  6:30 ` [PATCH 4/4] dhcp: replace dhcp_getenv_int with getenv_uint Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2013-09-27  6:30 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/bootm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commands/bootm.c b/commands/bootm.c
index a4004df..ae6b5f3 100644
--- a/commands/bootm.c
+++ b/commands/bootm.c
@@ -109,8 +109,8 @@ static int do_bootm(int argc, char *argv[])
 
 	oftree = getenv("global.bootm.oftree");
 	os_file = getenv("global.bootm.image");
-	data.os_address = getenv_loadaddr("global.bootm.image.loadaddr");
-	data.initrd_address = getenv_loadaddr("global.bootm.initrd.loadaddr");
+	getenv_ul("global.bootm.image.loadaddr", &data.os_address);
+	getenv_ul("global.bootm.initrd.loadaddr", &data.initrd_address);
 	if (IS_ENABLED(CONFIG_CMD_BOOTM_INITRD))
 		initrd_file = getenv("global.bootm.initrd");
 
-- 
1.8.4.rc3


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

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

* [PATCH 4/4] dhcp: replace dhcp_getenv_int with getenv_uint
  2013-09-27  6:30 getenv helpers Sascha Hauer
                   ` (2 preceding siblings ...)
  2013-09-27  6:30 ` [PATCH 3/4] bootm: Replace getenv_loadaddr with getenv_ul Sascha Hauer
@ 2013-09-27  6:30 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2013-09-27  6:30 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/dhcp.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/net/dhcp.c b/net/dhcp.c
index ff54924..e0c231f 100644
--- a/net/dhcp.c
+++ b/net/dhcp.c
@@ -699,16 +699,6 @@ static int dhcp_global_init(void)
 }
 late_initcall(dhcp_global_init);
 
-static void dhcp_getenv_int(const char *name, int *i)
-{
-	const char* str = getenv(name);
-
-	if (!str)
-		return;
-
-	*i = simple_strtoul(str, NULL, 10);
-}
-
 static int do_dhcp(int argc, char *argv[])
 {
 	int ret, opt;
@@ -716,7 +706,7 @@ static int do_dhcp(int argc, char *argv[])
 
 	dhcp_reset_env();
 
-	dhcp_getenv_int("global.dhcp.retries", &retries);
+	getenv_uint("global.dhcp.retries", &retries);
 
 	while((opt = getopt(argc, argv, "H:v:c:u:U:r:")) > 0) {
 		switch(opt) {
-- 
1.8.4.rc3


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

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

end of thread, other threads:[~2013-09-27  6:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-27  6:30 getenv helpers Sascha Hauer
2013-09-27  6:30 ` [PATCH 1/4] environment variables: introduce new helpers Sascha Hauer
2013-09-27  6:30 ` [PATCH 2/4] loads: use getenv_bool for 'loads_echo' Sascha Hauer
2013-09-27  6:30 ` [PATCH 3/4] bootm: Replace getenv_loadaddr with getenv_ul Sascha Hauer
2013-09-27  6:30 ` [PATCH 4/4] dhcp: replace dhcp_getenv_int with getenv_uint Sascha Hauer

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