Skip to contentSkip to conversation

Add environment call-back capability

RFC PATCH Superseded Joe Hershberger 3 participants 14 messages Sep 21, 2012 – Sep 28, 2012

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Add environment call-back capability

When a variable with a registered callback is inserted, deleted, or overwritten the callback is called and gives the system an opportunity to do something in response to the change. It also has the opportunuty to reject the change by returning non-zero.

Before I go much further, I want to get a little feedback if this is a good implementation. It certainly cleans up cmd_nvedit.c significantly!

Joe Hershberger (5):
env: Add support for callbacks to environment vars
env: Add a loadaddr env handler
env: Add a bootfile env handler
env: Add a baudrate env handler
env: Add a console env handler

 arch/arm/cpu/u-boot.lds |   7 +++
 common/Makefile         |   4 +-
 common/cmd_load.c       |  24 +++++++-
 common/cmd_nvedit.c     |  87 --------------------------
 common/console.c        |  46 ++++++++++++++
 common/env_attr.c       | 159 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/env_callback.c   | 114 ++++++++++++++++++++++++++++++++++
 common/serial.c         |  58 ++++++++++++++++++
 include/env_attr.h      |  35 +++++++++++
 include/env_callback.h  |  64 +++++++++++++++++++
 include/environment.h   |   2 +
 include/search.h        |   5 ++
 lib/hashtable.c         |  65 +++++++++++++++++++-
 net/net.c               |  16 +++++
 14 files changed, 595 insertions(+), 91 deletions(-)
 create mode 100644 common/env_attr.c
 create mode 100644 common/env_callback.c
 create mode 100644 include/env_attr.h
 create mode 100644 include/env_callback.h
Signature · 1 line
1.7.11.5
#1 lore raw

RFC PATCH 1/5 env: Add support for callbacks to environment vars

Add support for callbacks to the "hashtable" functions.

One check-patch warning to inter-op with existing hashtable code:
WARNING: do not add new typedefs
+typedef struct entry ENTRY;

---
 arch/arm/cpu/u-boot.lds |   7 +++
 common/Makefile         |   2 +
 common/env_attr.c       | 159 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/env_callback.c   | 114 ++++++++++++++++++++++++++++++++++
 include/env_attr.h      |  35 +++++++++++
 include/env_callback.h  |  62 +++++++++++++++++++
 include/environment.h   |   2 +
 include/search.h        |   5 ++
 lib/hashtable.c         |  65 +++++++++++++++++++-
 9 files changed, 450 insertions(+), 1 deletion(-)
 create mode 100644 common/env_attr.c
 create mode 100644 common/env_callback.c
 create mode 100644 include/env_attr.h
 create mode 100644 include/env_callback.h
arch/arm/cpu/u-boot.lds +7 -0
index e49ca0c..5af8c36 100644
@@ -55,6 +55,13 @@ SECTIONS
 
 	. = ALIGN(4);
 
+	. = .;
+	__u_boot_env_clbk_start = .;
+	.u_boot_env_clbk : { *(.u_boot_env_clbk) }
+	__u_boot_env_clbk_end = .;
+
+	. = ALIGN(4);
+
 	__image_copy_end = .;
 
 	.rel.dyn : {
common/Makefile +2 -0
index 3d62775..f61c9a1 100644
@@ -43,7 +43,9 @@ COBJS-y += cmd_nvedit.o
 COBJS-y += cmd_version.o
 
 # environment
+COBJS-y += env_attr.o
 COBJS-y += env_common.o
+COBJS-y += env_callback.o
 COBJS-$(CONFIG_ENV_IS_IN_DATAFLASH) += env_dataflash.o
 COBJS-$(CONFIG_ENV_IS_IN_EEPROM) += env_eeprom.o
 XCOBJS-$(CONFIG_ENV_IS_EMBEDDED) += env_embedded.o
common/env_attr.c new file +159 -0

159 lines not on this page · raw message

common/env_callback.c new file +114 -0

114 lines not on this page · raw message

include/env_attr.h new file +35 -0
new file mode 100644
index 0000000..62a3667
@@ -0,0 +1,35 @@
+/*
+ * (C) Copyright 2012
+ * Joe Hershberger, National Instruments, joe.hershberger at ni.com
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef __ENV_ATTR_H__
+#define __ENV_ATTR_H__
+
+#define ENV_ATTR_LIST_DELIM	','
+#define ENV_ATTR_SEP		':'
+
+extern int env_attr_walk(const char *attr_list,
+	int (*callback)(const char *name, const char *value));
+extern int env_attr_lookup(const char *attr_list, const char *name,
+	char *attributes);
+
+#endif /* __ENV_ATTR_H__ */
include/env_callback.h new file +62 -0

62 lines not on this page · raw message

include/environment.h +2 -0
index ae3f7b6..040352f 100644
@@ -164,6 +164,8 @@ extern void env_reloc(void);
 
 #ifndef DO_DEPS_ONLY
 
+#include <env_attr.h>
+#include <env_callback.h>
 #include <search.h>
 
 extern struct hsearch_data env_htab;
include/search.h +5 -0
index ef53edb..9d0d498 100644
@@ -29,6 +29,7 @@
 #define	_SEARCH_H 1
 
 #include <stddef.h>
+#include <env_callback.h>
 
 #define __set_errno(val) do { errno = val; } while (0)
 
@@ -41,6 +42,7 @@ typedef enum {
 typedef struct entry {
 	const char *key;
 	char *data;
+	int (*callback)(const char *name, const char *value, enum env_op op);
 } ENTRY;
 
 /* Opaque type for internal use.  */
@@ -98,6 +100,9 @@ extern int himport_r(struct hsearch_data *__htab,
 		     const char *__env, size_t __size, const char __sep,
 		     int __flag);
 
+/* Walk the whole table calling the callback on each element */
+extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
+
 /* Flags for himport_r() */
 #define	H_NOCLEAR	1	/* do not clear hash table before importing */
 
lib/hashtable.c +64 -1

98 lines not on this page · raw message

Signature · 1 line
1.7.11.5
#2 lore raw

RFC PATCH 1/5 env: Add support for callbacks to environment vars

Dear Joe Hershberger,

Quoted 1 line · In message <[email protected]> you wrote:

Add support for callbacks to the "hashtable" functions.

...

Quoted 13 lines
common/env_attr.c new file +13 -0
+/*
+ * Iterate through the whole list calling the callback for each found element.
+ */
+int env_attr_walk(const char *attr_list,
+	int (*callback)(const char *name, const char *value))
+{
+	const char *entry, *entry_end;
+	char *name, *value;
+
+	if (!attr_list)
+		/* list not found */
+		return 1;
+

common/env_attr.c line 51

Quoted 10 lines
common/env_attr.c new file +10 -0
+	entry = attr_list;
+	do {
+		char *entry_cpy = NULL;
+
+		entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
+		if (entry_end == NULL) {
+			int entry_len = strlen(entry);
+
+			if (entry_len) {
+				entry_cpy = malloc(entry_len + 1);

--------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

common/env_attr.c line 61

Quoted 10 lines
common/env_attr.c new file +10 -0
+				if (entry_cpy)
+					strcpy(entry_cpy, entry);
+				else
+					return -ENOMEM;
+			}
+		} else {
+			int entry_len = entry_end - entry;
+
+			if (entry_len) {
+				entry_cpy = malloc(entry_len + 1);

--------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

common/env_attr.c line 68

Quoted 7 lines
common/env_attr.c new file +7 -0
+				if (entry_cpy) {
+					strncpy(entry_cpy, entry, entry_len);
+					entry_cpy[entry_len] = '\0';
+				} else
+					return -ENOMEM;
+			}
+		}

I find it a bit strange that a function which walks an existing list of data structures would malloc() anything?

Quoted 15 lines
common/env_attr.c new file +15 -0
+		if (entry_cpy != NULL) {
+			value = strchr(entry_cpy, ENV_ATTR_SEP);
+			if (value != NULL) {
+				*value++ = '\0';
+				value = strim(value);
+			}
+			name = strim(entry_cpy);
+
+			if (strlen(name) != 0) {
+				int retval = 0;
+
+				retval = callback(name, value);
+				if (retval) {
+					free(entry_cpy);
+					return retval;

common/env_attr.c line 94

Quoted 10 lines
common/env_attr.c new file +10 -0
+				}
+			}
+		}
+
+		free(entry_cpy);
+		entry = entry_end + 1;
+	} while (entry_end != NULL);
+
+	return 0;
+}

Actually, this function is unreadable to me, especially because there is zero documentation about what exactly it is supposed to do, which data formats are being used, etc.

common/env_attr.c line 100

Quoted 5 lines
common/env_attr.c new file +5 -0
+/*
+ * Retrieve the attributes string associated with a single name in the list
+ * There is no protection on attributes being too small for the value
+ */
+int env_attr_lookup(const char *attr_list, const char *name, char *attributes)

Atttributes in a list?

What exactly has this to do with implementing callbacks?

[Yes, I think I do know what you have in mind, but this is based on a lot of speculation, and I may be wrong. And people who didn't follow the previous discussion and the old patch seris probably have no clue at all.]

common/env_attr.c line 119

Quoted 9 lines
common/env_attr.c new file +9 -0
+	entry = strstr(attr_list, name);
+	while (entry != NULL) {
+		if ((entry == attr_list ||
+		    *(entry - 1) == ENV_ATTR_LIST_DELIM ||
+		    *(entry - 1) == ' ') &&
+		    (*(entry + strlen(name)) == ENV_ATTR_SEP ||
+		     *(entry + strlen(name)) == ENV_ATTR_LIST_DELIM ||
+		     *(entry + strlen(name)) == '\0' ||
+		     *(entry + strlen(name)) == ' '))

Factor out strlen() ?

...

Quoted 5 lines
include/env_attr.h new file +0 -0
new file mode 100644
index 0000000..62a3667

...

include/env_attr.h line 33

Quoted 7 lines
include/env_attr.h new file +7 -0
+#define ENV_ATTR_LIST_DELIM	','
+#define ENV_ATTR_SEP		':'
+
+extern int env_attr_walk(const char *attr_list,
+	int (*callback)(const char *name, const char *value));
+extern int env_attr_lookup(const char *attr_list, const char *name,
+	char *attributes);

This does need documentation...

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
You're dead, Jim.
	-- McCoy, "The Tholian Web", stardate unknown
#3 lore raw

v2 cover Add environment call-back capability

When a variable with a registered callback is inserted, deleted, or overwritten the callback is called and gives the system an opportunity to do something in response to the change. It also has the opportunuty to reject the change by returning non-zero.

Before I go much further, I want to get a little feedback if this is a good implementation. It certainly cleans up cmd_nvedit.c significantly!

Changes in v2:
- Added much-needed documentation
- Factored out prevch and nextch in env_attr_lookup()

Joe Hershberger (5):
env: Add support for callbacks to environment vars
env: Add a loadaddr env handler
env: Add a bootfile env handler
env: Add a baudrate env handler
env: Add a console env handler

 README                  |  28 ++++++++
 arch/arm/cpu/u-boot.lds |   7 ++
 common/Makefile         |   4 +-
 common/cmd_load.c       |  24 ++++++-
 common/cmd_nvedit.c     |  87 ----------------------
 common/console.c        |  46 ++++++++++++
 common/env_attr.c       | 187 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/env_callback.c   | 130 +++++++++++++++++++++++++++++++++
 common/serial.c         |  58 +++++++++++++++
 include/env_attr.h      |  55 ++++++++++++++
 include/env_callback.h  |  74 +++++++++++++++++++
 include/environment.h   |   2 +
 include/search.h        |   5 ++
 lib/hashtable.c         |  65 ++++++++++++++++-
 net/net.c               |  16 +++++
 15 files changed, 697 insertions(+), 91 deletions(-)
 create mode 100644 common/env_attr.c
 create mode 100644 common/env_callback.c
 create mode 100644 include/env_attr.h
 create mode 100644 include/env_callback.h
Signature · 1 line
1.7.11.5
#4 lore raw

RFC PATCH v2 1/5 env: Add support for callbacks to environment vars

Add support for callbacks to the "hashtable" functions.

One check-patch warning to inter-op with existing hashtable code:
WARNING: do not add new typedefs
+typedef struct entry ENTRY;

---
Changes in v2:
- Added much-needed documentation
- Factored out prevch and nextch in env_attr_lookup()

 README                  |  28 ++++++++
 arch/arm/cpu/u-boot.lds |   7 ++
 common/Makefile         |   2 +
 common/env_attr.c       | 187 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/env_callback.c   | 130 +++++++++++++++++++++++++++++++++
 include/env_attr.h      |  55 ++++++++++++++
 include/env_callback.h  |  72 +++++++++++++++++++
 include/environment.h   |   2 +
 include/search.h        |   5 ++
 lib/hashtable.c         |  65 ++++++++++++++++-
 10 files changed, 552 insertions(+), 1 deletion(-)
 create mode 100644 common/env_attr.c
 create mode 100644 common/env_callback.c
 create mode 100644 include/env_attr.h
 create mode 100644 include/env_callback.h
README +28 -0
index fb9d904..47bed09 100644
@@ -3991,6 +3991,34 @@ Please note that changes to some configuration parameters may take
 only effect after the next boot (yes, that's just like Windoze :-).
 
 
+Callback functions for environment variables:
+---------------------------------------------
+
+For some environment variables, the behavior of u-boot needs to change
+when their values are changed.  This functionailty allows functions to
+be associated with arbitrary variables.  On creation, overwrite, or
+deletion, the callback will provide the opportunity for some side
+effect to happen or for the change to be rejected.
+
+The callbacks are named and associated with a function using the
+U_BOOT_ENV_CALLBACK macro in your board or driver code.
+
+These callbacks are associated with variables in one of two ways.  The
+static list can be added to by defining CONFIG_ENV_CALLBACK_LIST_STATIC
+in the board configuration to a string that defines a list of
+associations.  The list must be in the following format:
+
+	entry = variable_name[:callback_name]
+	list = entry[,list]
+
+If the callback name is not specified, then the callback is deleted.
+Spaces are also allowed anywhere in the list.
+
+Callbacks can also be associated by defining the ".callbacks" variable
+with the same list format above.  Any association in ".callbacks" will
+override any association in the static list.
+
+
 Command Line Parsing:
 =====================
 
arch/arm/cpu/u-boot.lds +7 -0
index e49ca0c..5af8c36 100644
@@ -55,6 +55,13 @@ SECTIONS
 
 	. = ALIGN(4);
 
+	. = .;
+	__u_boot_env_clbk_start = .;
+	.u_boot_env_clbk : { *(.u_boot_env_clbk) }
+	__u_boot_env_clbk_end = .;
+
+	. = ALIGN(4);
+
 	__image_copy_end = .;
 
 	.rel.dyn : {
common/Makefile +2 -0
index 3d62775..f61c9a1 100644
@@ -43,7 +43,9 @@ COBJS-y += cmd_nvedit.o
 COBJS-y += cmd_version.o
 
 # environment
+COBJS-y += env_attr.o
 COBJS-y += env_common.o
+COBJS-y += env_callback.o
 COBJS-$(CONFIG_ENV_IS_IN_DATAFLASH) += env_dataflash.o
 COBJS-$(CONFIG_ENV_IS_IN_EEPROM) += env_eeprom.o
 XCOBJS-$(CONFIG_ENV_IS_EMBEDDED) += env_embedded.o
common/env_attr.c new file +187 -0

187 lines not on this page · raw message

common/env_callback.c new file +130 -0

130 lines not on this page · raw message

include/env_attr.h new file +55 -0

55 lines not on this page · raw message

include/env_callback.h new file +72 -0

72 lines not on this page · raw message

include/environment.h +2 -0
index ae3f7b6..040352f 100644
@@ -164,6 +164,8 @@ extern void env_reloc(void);
 
 #ifndef DO_DEPS_ONLY
 
+#include <env_attr.h>
+#include <env_callback.h>
 #include <search.h>
 
 extern struct hsearch_data env_htab;
include/search.h +5 -0
index ef53edb..9d0d498 100644
@@ -29,6 +29,7 @@
 #define	_SEARCH_H 1
 
 #include <stddef.h>
+#include <env_callback.h>
 
 #define __set_errno(val) do { errno = val; } while (0)
 
@@ -41,6 +42,7 @@ typedef enum {
 typedef struct entry {
 	const char *key;
 	char *data;
+	int (*callback)(const char *name, const char *value, enum env_op op);
 } ENTRY;
 
 /* Opaque type for internal use.  */
@@ -98,6 +100,9 @@ extern int himport_r(struct hsearch_data *__htab,
 		     const char *__env, size_t __size, const char __sep,
 		     int __flag);
 
+/* Walk the whole table calling the callback on each element */
+extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *));
+
 /* Flags for himport_r() */
 #define	H_NOCLEAR	1	/* do not clear hash table before importing */
 
lib/hashtable.c +64 -1

98 lines not on this page · raw message

Signature · 1 line
1.7.11.5
#5 lore raw

RFC PATCH v2 1/5 env: Add support for callbacks to environment vars

Dear Joe Hershberger,

[...]

arch/arm/cpu/u-boot.lds SECTIONS · line 64

Quoted 7 lines
arch/arm/cpu/u-boot.lds +7 -0
+	. = .;
+	__u_boot_env_clbk_start = .;
+	.u_boot_env_clbk : { *(.u_boot_env_clbk) }
+	__u_boot_env_clbk_end = .;
+
+	. = ALIGN(4);
+

[...]

Can you make some use of the LG-array patches ?

Best regards,
Marek Vasut

#6 lore raw

RFC PATCH v2 2/5 env: Add a loadaddr env handler

In reply to Joe Hershberger

Remove the hard-coded loadaddr handler and use a callback instead

---

 common/cmd_load.c      | 24 ++++++++++++++++++++++--
 common/cmd_nvedit.c    | 10 +---------
 include/env_callback.h |  1 +
 3 files changed, 24 insertions(+), 11 deletions(-)
common/cmd_load.c +22 -2
index f4d66de..8849596 100644
@@ -30,9 +30,14 @@
 #include <net.h>
 #include <exports.h>
 #include <xyzModem.h>
+#include <environment.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
+ulong save_addr;			/* Default Save Address */
+ulong save_size;			/* Default Save Size (in bytes) */
+
 #if defined(CONFIG_CMD_LOADB)
 static ulong load_serial_ymodem (ulong offset);
 #endif
@@ -50,6 +55,21 @@ static int do_echo = 1;
 
 /* -------------------------------------------------------------------- */
 
+static int on_loadaddr(const char *name, const char *value, enum env_op op)
+{
+	switch (op) {
+	case env_op_create:
+	case env_op_overwrite:
+		load_addr = simple_strtoul(value, NULL, 16);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
+
 #if defined(CONFIG_CMD_LOADS)
 int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
@@ -257,8 +277,8 @@ read_record (char *buf, ulong len)
 
 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	ulong offset = 0;
-	ulong size   = 0;
+	ulong offset = save_addr;
+	ulong size   = save_size;
 #ifdef	CONFIG_SYS_LOADS_BAUD_CHANGE
 	int save_baudrate, current_baudrate;
 
common/cmd_nvedit.c +1 -9
index fd05e72..9e8ea12 100644
@@ -79,10 +79,6 @@ SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
  */
 #define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
 
-ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
-ulong save_addr;			/* Default Save Address */
-ulong save_size;			/* Default Save Size (in bytes) */
-
 /*
  * Table with supported baudrates (defined in config_xyz.h)
  */
@@ -341,12 +337,8 @@ int _do_env_set(int flag, int argc, char * const argv[])
 	 * Some variables should be updated when the corresponding
 	 * entry in the environment is changed
 	 */
-	if (strcmp(argv[1], "loadaddr") == 0) {
-		load_addr = simple_strtoul(argv[2], NULL, 16);
-		return 0;
-	}
 #if defined(CONFIG_CMD_NET)
-	else if (strcmp(argv[1], "bootfile") == 0) {
+	if (strcmp(argv[1], "bootfile") == 0) {
 		copy_filename(BootFile, argv[2], sizeof(BootFile));
 		return 0;
 	}
include/env_callback.h +1 -0
index 76ac865..9c1cbe9 100644
@@ -36,6 +36,7 @@
  * a new assogiation in the ".callbacks" environment variable.
  */
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
+	"loadaddr:loadaddr," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
Signature · 1 line
1.7.11.5
#7 lore raw

RFC PATCH v2 3/5 env: Add a bootfile env handler

In reply to Joe Hershberger

Remove the hard-coded bootfile handler and use a callback instead

---

 common/cmd_nvedit.c    | 13 -------------
 include/env_callback.h |  2 +-
 net/net.c              | 16 ++++++++++++++++
 3 files changed, 17 insertions(+), 14 deletions(-)
common/cmd_nvedit.c +0 -13
index 9e8ea12..ac15a3c 100644
@@ -50,9 +50,6 @@
 #include <serial.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
-#if defined(CONFIG_CMD_NET)
-#include <net.h>
-#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -333,16 +330,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
 		return 1;
 	}
 
-	/*
-	 * Some variables should be updated when the corresponding
-	 * entry in the environment is changed
-	 */
-#if defined(CONFIG_CMD_NET)
-	if (strcmp(argv[1], "bootfile") == 0) {
-		copy_filename(BootFile, argv[2], sizeof(BootFile));
-		return 0;
-	}
-#endif
 	return 0;
 }
 
include/env_callback.h +1 -1
index 9c1cbe9..b450077 100644
@@ -36,7 +36,7 @@
  * a new assogiation in the ".callbacks" environment variable.
  */
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
-	"loadaddr:loadaddr," \
+	"loadaddr:loadaddr,bootfile:bootfile," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
net/net.c +16 -0
index e8ff066..7d7196f 100644
@@ -82,6 +82,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <environment.h>
 #include <net.h>
 #if defined(CONFIG_STATUS_LED)
 #include <miiphy.h>
@@ -208,6 +209,21 @@ static int NetTryCount;
 
 /**********************************************************************/
 
+static int on_bootfile(const char *name, const char *value, enum env_op op)
+{
+	switch (op) {
+	case env_op_create:
+	case env_op_overwrite:
+		copy_filename(BootFile, value, sizeof(BootFile));
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
+
 /*
  * Check if autoload is enabled. If so, use either NFS or TFTP to download
  * the boot file.
Signature · 1 line
1.7.11.5
#8 lore raw

RFC PATCH v2 4/5 env: Add a baudrate env handler

In reply to Joe Hershberger

Remove the hard-coded baudrate handler and use a callback instead

---

 common/Makefile        |  2 +-
 common/cmd_nvedit.c    | 35 ------------------------------
 common/serial.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/env_callback.h |  2 +-
 4 files changed, 60 insertions(+), 37 deletions(-)
common/Makefile +1 -1
index f61c9a1..1187960 100644
@@ -32,7 +32,7 @@ COBJS-y += command.o
 COBJS-y += exports.o
 COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
 COBJS-y += s_record.o
-COBJS-$(CONFIG_SERIAL_MULTI) += serial.o
+COBJS-y += serial.o
 COBJS-y += xyzModem.o
 
 # core command
common/cmd_nvedit.c +0 -35
index ac15a3c..652e8ce 100644
@@ -47,7 +47,6 @@
 #include <errno.h>
 #include <malloc.h>
 #include <watchdog.h>
-#include <serial.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
 
@@ -77,12 +76,6 @@ SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
 #define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
 
 /*
- * Table with supported baudrates (defined in config_xyz.h)
- */
-static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
-#define	N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
-
-/*
  * This variable is incremented on each do_env_set(), so it can
  * be used via get_env_id() as an indication, if the environment
  * has changed or not. So it is possible to reread an environment
@@ -263,34 +256,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
 			return 1;
 		}
 #endif
-		/*
-		 * Switch to new baudrate if new baudrate is supported
-		 */
-		if (strcmp(name, "baudrate") == 0) {
-			int baudrate = simple_strtoul(argv[2], NULL, 10);
-			int i;
-			for (i = 0; i < N_BAUDRATES; ++i) {
-				if (baudrate == baudrate_table[i])
-					break;
-			}
-			if (i == N_BAUDRATES) {
-				printf("## Baudrate %d bps not supported\n",
-					baudrate);
-				return 1;
-			}
-			printf("## Switch baudrate to %d bps and"
-			       "press ENTER ...\n", baudrate);
-			udelay(50000);
-			gd->baudrate = baudrate;
-#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
-			gd->bd->bi_baudrate = baudrate;
-#endif
-
-			serial_setbrg();
-			udelay(50000);
-			while (getc() != '\r')
-				;
-		}
 	}
 
 	/* Delete only ? */
common/serial.c +58 -0

73 lines not on this page · raw message

include/env_callback.h +1 -1
index b450077..d399b39 100644
@@ -36,7 +36,7 @@
  * a new assogiation in the ".callbacks" environment variable.
  */
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
-	"loadaddr:loadaddr,bootfile:bootfile," \
+	"loadaddr:loadaddr,bootfile:bootfile,baudrate:baudrate," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
Signature · 1 line
1.7.11.5
#9 lore raw

RFC PATCH v2 5/5 env: Add a console env handler

In reply to Joe Hershberger

Remove the hard-coded console handler and use a callback instead

---

 common/cmd_nvedit.c    | 31 -------------------------------
 common/console.c       | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 include/env_callback.h |  1 +
 3 files changed, 47 insertions(+), 31 deletions(-)
common/cmd_nvedit.c +0 -31
index 652e8ce..6a6421a 100644
@@ -190,7 +190,6 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
 int _do_env_set(int flag, int argc, char * const argv[])
 {
 	int   i, len;
-	int   console = -1;
 	char  *name, *value, *s;
 	ENTRY e, *ep;
 
@@ -210,36 +209,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
 	e.data = NULL;
 	hsearch_r(e, FIND, &ep, &env_htab);
 
-	/* Check for console redirection */
-	if (strcmp(name, "stdin") == 0)
-		console = stdin;
-	else if (strcmp(name, "stdout") == 0)
-		console = stdout;
-	else if (strcmp(name, "stderr") == 0)
-		console = stderr;
-
-	if (console != -1) {
-		if (argc < 3) {		/* Cannot delete it! */
-			printf("Can't delete \"%s\"\n", name);
-			return 1;
-		}
-
-#ifdef CONFIG_CONSOLE_MUX
-		i = iomux_doenv(console, argv[2]);
-		if (i)
-			return i;
-#else
-		/* Try assigning specified device */
-		if (console_assign(console, argv[2]) < 0)
-			return 1;
-
-#ifdef CONFIG_SERIAL_MULTI
-		if (serial_assign(argv[2]) < 0)
-			return 1;
-#endif
-#endif /* CONFIG_CONSOLE_MUX */
-	}
-
 	/*
 	 * Some variables like "ethaddr" and "serial#" can be set only
 	 * once and cannot be deleted; also, "ver" is readonly.
common/console.c +46 -0

55 lines not on this page · raw message

include/env_callback.h +1 -0
index d399b39..0a50268 100644
@@ -37,6 +37,7 @@
  */
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
 	"loadaddr:loadaddr,bootfile:bootfile,baudrate:baudrate," \
+	"stdin:console,stdout:console,stderr:console," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
Signature · 1 line
1.7.11.5
#10 lore raw

RFC PATCH 2/5 env: Add a loadaddr env handler

In reply to Joe Hershberger

Remove the hard-coded loadaddr handler and use a callback instead

---
 common/cmd_load.c      | 24 ++++++++++++++++++++++--
 common/cmd_nvedit.c    | 10 +---------
 include/env_callback.h |  1 +
 3 files changed, 24 insertions(+), 11 deletions(-)
common/cmd_load.c +22 -2
index f4d66de..8849596 100644
@@ -30,9 +30,14 @@
 #include <net.h>
 #include <exports.h>
 #include <xyzModem.h>
+#include <environment.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
+ulong save_addr;			/* Default Save Address */
+ulong save_size;			/* Default Save Size (in bytes) */
+
 #if defined(CONFIG_CMD_LOADB)
 static ulong load_serial_ymodem (ulong offset);
 #endif
@@ -50,6 +55,21 @@ static int do_echo = 1;
 
 /* -------------------------------------------------------------------- */
 
+static int on_loadaddr(const char *name, const char *value, enum env_op op)
+{
+	switch (op) {
+	case env_op_create:
+	case env_op_overwrite:
+		load_addr = simple_strtoul(value, NULL, 16);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
+
 #if defined(CONFIG_CMD_LOADS)
 int do_load_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
@@ -257,8 +277,8 @@ read_record (char *buf, ulong len)
 
 int do_save_serial (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	ulong offset = 0;
-	ulong size   = 0;
+	ulong offset = save_addr;
+	ulong size   = save_size;
 #ifdef	CONFIG_SYS_LOADS_BAUD_CHANGE
 	int save_baudrate, current_baudrate;
 
common/cmd_nvedit.c +1 -9
index fd05e72..9e8ea12 100644
@@ -79,10 +79,6 @@ SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
  */
 #define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
 
-ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
-ulong save_addr;			/* Default Save Address */
-ulong save_size;			/* Default Save Size (in bytes) */
-
 /*
  * Table with supported baudrates (defined in config_xyz.h)
  */
@@ -341,12 +337,8 @@ int _do_env_set(int flag, int argc, char * const argv[])
 	 * Some variables should be updated when the corresponding
 	 * entry in the environment is changed
 	 */
-	if (strcmp(argv[1], "loadaddr") == 0) {
-		load_addr = simple_strtoul(argv[2], NULL, 16);
-		return 0;
-	}
 #if defined(CONFIG_CMD_NET)
-	else if (strcmp(argv[1], "bootfile") == 0) {
+	if (strcmp(argv[1], "bootfile") == 0) {
 		copy_filename(BootFile, argv[2], sizeof(BootFile));
 		return 0;
 	}
include/env_callback.h +1 -0
index 4a92c24..31155e7 100644
@@ -31,6 +31,7 @@
 #endif
 
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
+	"loadaddr:loadaddr," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
Signature · 1 line
1.7.11.5
#11 lore raw

RFC PATCH 3/5 env: Add a bootfile env handler

In reply to Joe Hershberger

Remove the hard-coded bootfile handler and use a callback instead

---
 common/cmd_nvedit.c    | 13 -------------
 include/env_callback.h |  2 +-
 net/net.c              | 16 ++++++++++++++++
 3 files changed, 17 insertions(+), 14 deletions(-)
common/cmd_nvedit.c +0 -13
index 9e8ea12..ac15a3c 100644
@@ -50,9 +50,6 @@
 #include <serial.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
-#if defined(CONFIG_CMD_NET)
-#include <net.h>
-#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -333,16 +330,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
 		return 1;
 	}
 
-	/*
-	 * Some variables should be updated when the corresponding
-	 * entry in the environment is changed
-	 */
-#if defined(CONFIG_CMD_NET)
-	if (strcmp(argv[1], "bootfile") == 0) {
-		copy_filename(BootFile, argv[2], sizeof(BootFile));
-		return 0;
-	}
-#endif
 	return 0;
 }
 
include/env_callback.h +1 -1
index 31155e7..42c3d91 100644
@@ -31,7 +31,7 @@
 #endif
 
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
-	"loadaddr:loadaddr," \
+	"loadaddr:loadaddr,bootfile:bootfile," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
net/net.c +16 -0
index e8ff066..7d7196f 100644
@@ -82,6 +82,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <environment.h>
 #include <net.h>
 #if defined(CONFIG_STATUS_LED)
 #include <miiphy.h>
@@ -208,6 +209,21 @@ static int NetTryCount;
 
 /**********************************************************************/
 
+static int on_bootfile(const char *name, const char *value, enum env_op op)
+{
+	switch (op) {
+	case env_op_create:
+	case env_op_overwrite:
+		copy_filename(BootFile, value, sizeof(BootFile));
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+U_BOOT_ENV_CALLBACK(bootfile, on_bootfile);
+
 /*
  * Check if autoload is enabled. If so, use either NFS or TFTP to download
  * the boot file.
Signature · 1 line
1.7.11.5
#12 lore raw

RFC PATCH 4/5 env: Add a baudrate env handler

In reply to Joe Hershberger

Remove the hard-coded baudrate handler and use a callback instead

---
 common/Makefile        |  2 +-
 common/cmd_nvedit.c    | 35 ------------------------------
 common/serial.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/env_callback.h |  2 +-
 4 files changed, 60 insertions(+), 37 deletions(-)
common/Makefile +1 -1
index f61c9a1..1187960 100644
@@ -32,7 +32,7 @@ COBJS-y += command.o
 COBJS-y += exports.o
 COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
 COBJS-y += s_record.o
-COBJS-$(CONFIG_SERIAL_MULTI) += serial.o
+COBJS-y += serial.o
 COBJS-y += xyzModem.o
 
 # core command
common/cmd_nvedit.c +0 -35
index ac15a3c..652e8ce 100644
@@ -47,7 +47,6 @@
 #include <errno.h>
 #include <malloc.h>
 #include <watchdog.h>
-#include <serial.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
 
@@ -77,12 +76,6 @@ SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
 #define	MAX_ENV_SIZE	(1 << 20)	/* 1 MiB */
 
 /*
- * Table with supported baudrates (defined in config_xyz.h)
- */
-static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
-#define	N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
-
-/*
  * This variable is incremented on each do_env_set(), so it can
  * be used via get_env_id() as an indication, if the environment
  * has changed or not. So it is possible to reread an environment
@@ -263,34 +256,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
 			return 1;
 		}
 #endif
-		/*
-		 * Switch to new baudrate if new baudrate is supported
-		 */
-		if (strcmp(name, "baudrate") == 0) {
-			int baudrate = simple_strtoul(argv[2], NULL, 10);
-			int i;
-			for (i = 0; i < N_BAUDRATES; ++i) {
-				if (baudrate == baudrate_table[i])
-					break;
-			}
-			if (i == N_BAUDRATES) {
-				printf("## Baudrate %d bps not supported\n",
-					baudrate);
-				return 1;
-			}
-			printf("## Switch baudrate to %d bps and"
-			       "press ENTER ...\n", baudrate);
-			udelay(50000);
-			gd->baudrate = baudrate;
-#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
-			gd->bd->bi_baudrate = baudrate;
-#endif
-
-			serial_setbrg();
-			udelay(50000);
-			while (getc() != '\r')
-				;
-		}
 	}
 
 	/* Delete only ? */
common/serial.c +58 -0

73 lines not on this page · raw message

include/env_callback.h +1 -1
index 42c3d91..3f39996 100644
@@ -31,7 +31,7 @@
 #endif
 
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
-	"loadaddr:loadaddr,bootfile:bootfile," \
+	"loadaddr:loadaddr,bootfile:bootfile,baudrate:baudrate," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
Signature · 1 line
1.7.11.5
#13 lore raw

RFC PATCH 5/5 env: Add a console env handler

In reply to Joe Hershberger

Remove the hard-coded console handler and use a callback instead

---
 common/cmd_nvedit.c    | 31 -------------------------------
 common/console.c       | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 include/env_callback.h |  1 +
 3 files changed, 47 insertions(+), 31 deletions(-)
common/cmd_nvedit.c +0 -31
index 652e8ce..6a6421a 100644
@@ -190,7 +190,6 @@ static int do_env_grep(cmd_tbl_t *cmdtp, int flag,
 int _do_env_set(int flag, int argc, char * const argv[])
 {
 	int   i, len;
-	int   console = -1;
 	char  *name, *value, *s;
 	ENTRY e, *ep;
 
@@ -210,36 +209,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
 	e.data = NULL;
 	hsearch_r(e, FIND, &ep, &env_htab);
 
-	/* Check for console redirection */
-	if (strcmp(name, "stdin") == 0)
-		console = stdin;
-	else if (strcmp(name, "stdout") == 0)
-		console = stdout;
-	else if (strcmp(name, "stderr") == 0)
-		console = stderr;
-
-	if (console != -1) {
-		if (argc < 3) {		/* Cannot delete it! */
-			printf("Can't delete \"%s\"\n", name);
-			return 1;
-		}
-
-#ifdef CONFIG_CONSOLE_MUX
-		i = iomux_doenv(console, argv[2]);
-		if (i)
-			return i;
-#else
-		/* Try assigning specified device */
-		if (console_assign(console, argv[2]) < 0)
-			return 1;
-
-#ifdef CONFIG_SERIAL_MULTI
-		if (serial_assign(argv[2]) < 0)
-			return 1;
-#endif
-#endif /* CONFIG_CONSOLE_MUX */
-	}
-
 	/*
 	 * Some variables like "ethaddr" and "serial#" can be set only
 	 * once and cannot be deleted; also, "ver" is readonly.
common/console.c +46 -0

55 lines not on this page · raw message

include/env_callback.h +1 -0
index 3f39996..94ffdad 100644
@@ -32,6 +32,7 @@
 
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
 	"loadaddr:loadaddr,bootfile:bootfile,baudrate:baudrate," \
+	"stdin:console,stdout:console,stderr:console," \
 	CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
Signature · 1 line
1.7.11.5