Skip to contentSkip to conversation

Read-only env variables

U-Boot Superseded Joe Hershberger 9 participants 124 messages Apr 28, 2010 – Jan 15, 2013 Page 2 of 2

Subsystems (8)
lore
Share & tools
lore
#111 lore raw

v5 cover Add environment call-back and flags capability

In reply to Joe Hershberger
Quoted 135 lines · Joe Hershberger

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.

The flags on variables can control their type as well as their allowed access.

The format of the list is:
type_attribute = [s|d|x|b|i|m] attributes = type_attribute entry = variable_name[:attributes] list = entry[,list]

The type attributes are:
s - String (default)
d - Decimal
x - Hexadecimal
b - Boolean ([1yYtT|0nNfF])
i - IP address
m - MAC address

The access attributes are:
a - Any (default)
r - Read-only
o - Write-once
c - Change-default

Changes in v5:
- Manually relocate change_ok() pointer
- Add support for CONFIG_NEEDS_MANUAL_RELOC boards
- Fixed comment typo
- Fixed callbacks command help
- Compare current callback against pre-relocation address manually
- Fixed out-of-bounds array access in env_flags_parse_vartype()
- Fixed out-of-bounds array access in env_flags_parse_varaccess()

Changes in v4:
- Prevent crash on relocation import
- Fixed help text
- Add help text for env flags command
- Add force support to setenv
- Implement delete

Changes in v3:
- Rebase onto Gerlando Falauto's env patches
- Refactor himport_r() and hsearch_r()'s parameters
- Split hdelete_r() into the core delete and the validation before delete
- Delete vars on failed insertion
- Use Marek's linker lists instead of implementing it directly
- Rebase onto latest master
- Add flags parameter to callbacks
- Implement reverse search in env_attr_lookup()
- Fix space skipping in env_attr_lookup()
- All errors coming back from hsearch_r() are no longer fatal. Don't abort import on failed ENTER action.
- Removed checkpatch.pl warning

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

Joe Hershberger (20):
Make linux kernel string funcs available to tools
env: Refactor do_apply to a flag
env: Consolidate common code in hsearch_r()
env: Refactor apply into change_ok
env: Use getenv_yesno() more generally
env: Hide '.' variables in env print by default
env: Add support for callbacks to environment vars
env: Add a command to view callbacks
env: Add a bootfile env handler
env: Add a baudrate env handler
env: Add a loadaddr env handler
env: Add a console env handler
env: Add a silent env handler
env: Add environment variable flags tools/env: Add environment variable flags support
env: Add a command to display details about env flags
env: Add support for access control to .flags
env: Add setenv force support
env: Implement the env delete command
env: Handle write-once ethaddr and serial# generically

 README                          |  80 ++++++
 arch/arm/lib/board.c            |   4 +-
 arch/m68k/lib/board.c           |   3 +-
 arch/microblaze/lib/board.c     |   4 +-
 arch/powerpc/cpu/mpc85xx/mp.c   |   4 +-
 arch/powerpc/lib/board.c        |   9 +-
 arch/sparc/lib/board.c          |   3 +-
 board/Marvell/db64360/db64360.c |  10 +-
 board/Marvell/db64460/db64460.c |  10 +-
 board/esd/cpci750/cpci750.c     |  10 +-
 board/esd/pmc440/cmd_pmc440.c   |   2 +-
 board/gw8260/gw8260.c           |  10 +-
 board/prodrive/p3mx/p3mx.c      |  10 +-
 common/Makefile                 |   6 +
 common/cmd_nvedit.c             | 406 +++++++++++++++++------------
 common/console.c                |  75 +++++-
 common/env_attr.c               | 229 ++++++++++++++++
 common/env_callback.c           | 144 +++++++++++
 common/env_common.c             |  61 +++--
 common/env_dataflash.c          |   2 +-
 common/env_eeprom.c             |   2 +-
 common/env_fat.c                |   2 +-
 common/env_flags.c              | 560 ++++++++++++++++++++++++++++++++++++++++
 common/env_flash.c              |   4 +-
 common/env_mmc.c                |   2 +-
 common/env_nand.c               |   4 +-
 common/env_nvram.c              |   2 +-
 common/env_onenand.c            |   2 +-
 common/env_sf.c                 |   4 +-
 common/image.c                  |  21 +-
 doc/README.silent               |  14 +-
 drivers/serial/serial.c         |  70 +++++
 include/common.h                |   5 +
 include/env_attr.h              |  55 ++++
 include/env_callback.h          |  75 ++++++
 include/env_default.h           |   8 +
 include/env_flags.h             | 172 ++++++++++++
 include/environment.h           |  15 +-
 include/image.h                 |   1 -
 include/linux/linux_string.h    |   8 +
 include/linux/string.h          |   5 +-
 include/search.h                |  37 ++-
 lib/Makefile                    |   1 +
 lib/hashtable.c                 | 244 +++++++++++------
 lib/linux_string.c              |  51 ++++
 lib/string.c                    |  39 ---
 net/net.c                       |  49 ++--
 tools/env/Makefile              |   3 +
 tools/env/fw_env.c              |  92 +++++--
 49 files changed, 2182 insertions(+), 447 deletions(-)
Quoted 8 lines
 create mode 100644 common/env_attr.c
 create mode 100644 common/env_callback.c
 create mode 100644 common/env_flags.c
 create mode 100644 include/env_attr.h
 create mode 100644 include/env_callback.h
 create mode 100644 include/env_flags.h
 create mode 100644 include/linux/linux_string.h
 create mode 100644 lib/linux_string.c

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20121214/620a9b9d/attachment.pgp>
#112 lore raw

PATCH 11/12 env: acl: Add support for access control to env ACL

In reply to Joe Hershberger

Add support for read-only and write-once.

---
 README              | 13 +++++++++++-
 common/cmd_nvedit.c | 51 ++++++++++++++++++++++++++++++++++++++++++++
 common/env_acl.c    | 38 +++++++++++++++++++++++++++++++++
 include/env_acl.h   | 14 ++++++++++++
 tools/env/fw_env.c  | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 176 insertions(+), 1 deletion(-)
README +12 -1
index 2e34bcd..fa665f0 100644
@@ -2094,6 +2094,11 @@ The following options need to be configured:
 		serial# is unaffected by this, i. e. it remains
 		read-only.]
 
+		The same can be accomplished in a more flexible way
+		for any variable by defining CONFIG_ENV_ACL and
+		specifying the type of access to allow to each
+		variable.
+
 - Protected RAM:
 		CONFIG_PRAM
 
@@ -2916,7 +2921,8 @@ Configuration Settings:
 
 	The format of the list is:
 		type_attribute = [s|d|x|b|i|m]
-		attributes = type_attribute
+		access_atribute = [a|r|o]
+		attributes = type_attribute[access_atribute]
 		entry = variable_name[:attributes]
 		list = entry[,list]
 
@@ -2928,6 +2934,11 @@ Configuration Settings:
 		i - IP address
 		m - MAC address
 
+	The access attributes are:
+		a - Any (default)
+		r - Read-only
+		o - Write-once (change default)
+
 	- CONFIG_ENV_ACL_DEFAULT
 		Define this to a list (string) to define the "acl" envirnoment
 		variable in the default or embedded environment.
common/cmd_nvedit.c +51 -0

63 lines not on this page · raw message

common/env_acl.c +38 -0
index 7c86243..1a75e09 100644
@@ -47,6 +47,12 @@
 
 static const char env_acl_static[] = CONFIG_ENV_ACL_STATIC "\0";
 static const char env_acl_type_rep[] = "sdxb" ENV_ACL_NET_TYPE_REPS;
+static const char env_acl_access_rep[] = "aro";
+static const char env_acl_access_mask[] = {
+	0,
+	ENV_ACL_PREVENT_DELETE | ENV_ACL_PREVENT_CREATE |
+		ENV_ACL_PREVENT_OVERWR,
+	ENV_ACL_PREVENT_DELETE | ENV_ACL_PREVENT_NONDEF_OVERWR};
 
 static int _env_acl_lookup_r(const char *name, char *attributes, int static_acl)
 {
@@ -147,6 +153,27 @@ enum env_acl_var_type env_acl_get_type(const char *name)
 	return ENV_ACL_VAR_TYPE_STRING;
 }
 
+enum env_acl_var_access env_acl_get_access(const char *name)
+{
+	char *access;
+	char attr[ENV_ACL_ATTR_MAX_LEN + 1];
+	if (env_acl_lookup_r(name, attr))
+		return ENV_ACL_VAR_ACCESS_ANY;
+
+	if (strlen(attr) <= ENV_ACL_ACCESS_LOC)
+		return ENV_ACL_VAR_ACCESS_ANY;
+
+	access = strchr(env_acl_access_rep, attr[ENV_ACL_ACCESS_LOC]);
+
+	if (access != NULL)
+		return (enum env_acl_var_access)
+			(access - &env_acl_access_rep[0]);
+
+	printf("## Warning: Unknown environment variable access method '%c'\n",
+		attr[ENV_ACL_ACCESS_LOC]);
+	return ENV_ACL_VAR_ACCESS_ANY;
+}
+
 static inline int is_hex_prefix(const char *value)
 {
 	return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
@@ -249,6 +276,17 @@ int env_acl_validate_type(const char *name, const char *value)
 	return 0;
 }
 
+int env_acl_validate_access(const char *name, int check_mask)
+{
+	enum env_acl_var_access access;
+	int access_mask;
+
+	access = env_acl_get_access(name);
+	access_mask = env_acl_access_mask[access];
+
+	return (check_mask & access_mask) != 0;
+}
+
 int env_acl_validate_env_set_params(int argc, char * const argv[])
 {
 	if ((argc >= 3) && argv[2] != NULL) {
include/env_acl.h +14 -0
index 9b0a199..09618f9 100644
@@ -35,14 +35,28 @@ enum env_acl_var_type {
 #endif
 };
 
+enum env_acl_var_access {
+	ENV_ACL_VAR_ACCESS_ANY,
+	ENV_ACL_VAR_ACCESS_READ,
+	ENV_ACL_VAR_ACCESS_SET_ONCE,
+};
+
+#define ENV_ACL_PREVENT_DELETE		0x01
+#define ENV_ACL_PREVENT_CREATE		0x02
+#define ENV_ACL_PREVENT_OVERWR		0x04
+#define ENV_ACL_PREVENT_NONDEF_OVERWR	0x08
+
 #define ENV_ACL_LIST_DELIM	','
 #define ENV_ACL_ATTR_SEP	':'
 #define ENV_ACL_LIST_VAR_NAME	"acl"
 #define ENV_ACL_ATTR_MAX_LEN	2
 #define ENV_ACL_TYPE_LOC	0
+#define ENV_ACL_ACCESS_LOC	1
 
 enum env_acl_var_type env_acl_get_type(const char *name);
+enum env_acl_var_access env_acl_get_access(const char *name);
 int env_acl_validate_type(const char *name, const char *value);
+int env_acl_validate_access(const char *name, int check_mask);
 int env_acl_validate_env_set_params(int argc, char * const argv[]);
 
 
tools/env/fw_env.c +61 -0

73 lines not on this page · raw message

Signature · 1 line
1.7.11.5
#113 lore raw

PATCH 12/12 env: cosmetic: Consilidate the default env definition

In reply to Joe Hershberger

There used to be a huge structure duplicated 3 times in the source.

---
 common/env_common.c   |  99 +----------------------------------
 common/env_embedded.c | 114 ++--------------------------------------
 include/env_default.h | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/env/fw_env.c    |  93 +--------------------------------
 4 files changed, 148 insertions(+), 300 deletions(-)
 create mode 100644 include/env_default.h
common/env_common.c +1 -98
index c32c846..f6f2a77 100644
@@ -37,104 +37,7 @@ DECLARE_GLOBAL_DATA_PTR;
 /************************************************************************
  * Default settings to be used when no valid environment is found
  */
-#define XMK_STR(x)	#x
-#define MK_STR(x)	XMK_STR(x)
-
-const uchar default_environment[] = {
-#ifdef	CONFIG_BOOTARGS
-	"bootargs="	CONFIG_BOOTARGS			"\0"
-#endif
-#ifdef	CONFIG_BOOTCOMMAND
-	"bootcmd="	CONFIG_BOOTCOMMAND		"\0"
-#endif
-#ifdef	CONFIG_RAMBOOTCOMMAND
-	"ramboot="	CONFIG_RAMBOOTCOMMAND		"\0"
-#endif
-#ifdef	CONFIG_NFSBOOTCOMMAND
-	"nfsboot="	CONFIG_NFSBOOTCOMMAND		"\0"
-#endif
-#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
-	"bootdelay="	MK_STR(CONFIG_BOOTDELAY)	"\0"
-#endif
-#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
-	"baudrate="	MK_STR(CONFIG_BAUDRATE)		"\0"
-#endif
-#ifdef	CONFIG_LOADS_ECHO
-	"loads_echo="	MK_STR(CONFIG_LOADS_ECHO)	"\0"
-#endif
-#ifdef	CONFIG_ETHADDR
-	"ethaddr="	MK_STR(CONFIG_ETHADDR)		"\0"
-#endif
-#ifdef	CONFIG_ETH1ADDR
-	"eth1addr="	MK_STR(CONFIG_ETH1ADDR)		"\0"
-#endif
-#ifdef	CONFIG_ETH2ADDR
-	"eth2addr="	MK_STR(CONFIG_ETH2ADDR)		"\0"
-#endif
-#ifdef	CONFIG_ETH3ADDR
-	"eth3addr="	MK_STR(CONFIG_ETH3ADDR)		"\0"
-#endif
-#ifdef	CONFIG_ETH4ADDR
-	"eth4addr="	MK_STR(CONFIG_ETH4ADDR)		"\0"
-#endif
-#ifdef	CONFIG_ETH5ADDR
-	"eth5addr="	MK_STR(CONFIG_ETH5ADDR)		"\0"
-#endif
-#ifdef	CONFIG_IPADDR
-	"ipaddr="	MK_STR(CONFIG_IPADDR)		"\0"
-#endif
-#ifdef	CONFIG_SERVERIP
-	"serverip="	MK_STR(CONFIG_SERVERIP)		"\0"
-#endif
-#ifdef	CONFIG_SYS_AUTOLOAD
-	"autoload="	CONFIG_SYS_AUTOLOAD		"\0"
-#endif
-#ifdef	CONFIG_PREBOOT
-	"preboot="	CONFIG_PREBOOT			"\0"
-#endif
-#ifdef	CONFIG_ROOTPATH
-	"rootpath="	CONFIG_ROOTPATH			"\0"
-#endif
-#ifdef	CONFIG_GATEWAYIP
-	"gatewayip="	MK_STR(CONFIG_GATEWAYIP)	"\0"
-#endif
-#ifdef	CONFIG_NETMASK
-	"netmask="	MK_STR(CONFIG_NETMASK)		"\0"
-#endif
-#ifdef	CONFIG_HOSTNAME
-	"hostname="	MK_STR(CONFIG_HOSTNAME)		"\0"
-#endif
-#ifdef	CONFIG_BOOTFILE
-	"bootfile="	CONFIG_BOOTFILE			"\0"
-#endif
-#ifdef	CONFIG_LOADADDR
-	"loadaddr="	MK_STR(CONFIG_LOADADDR)		"\0"
-#endif
-#ifdef	CONFIG_CLOCKS_IN_MHZ
-	"clocks_in_mhz=1\0"
-#endif
-#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
-	"pcidelay="	MK_STR(CONFIG_PCI_BOOTDELAY)	"\0"
-#endif
-#ifdef	CONFIG_ENV_VARS_UBOOT_CONFIG
-	"arch="		CONFIG_SYS_ARCH			"\0"
-	"cpu="		CONFIG_SYS_CPU			"\0"
-	"board="	CONFIG_SYS_BOARD		"\0"
-#ifdef CONFIG_SYS_VENDOR
-	"vendor="	CONFIG_SYS_VENDOR		"\0"
-#endif
-#ifdef CONFIG_SYS_SOC
-	"soc="		CONFIG_SYS_SOC			"\0"
-#endif
-#endif
-#ifdef	CONFIG_ENV_ACL_DEFAULT
-	"acl="		CONFIG_ENV_ACL_DEFAULT		"\0"
-#endif
-#ifdef	CONFIG_EXTRA_ENV_SETTINGS
-	CONFIG_EXTRA_ENV_SETTINGS
-#endif
-	"\0"
-};
+#include <env_default.h>
 
 struct hsearch_data env_htab;
 
common/env_embedded.c +3 -111

126 lines not on this page · raw message

include/env_default.h new file +142 -0

142 lines not on this page · raw message

tools/env/fw_env.c +2 -91

99 lines not on this page · raw message

Signature · 1 line
1.7.11.5
#114 lore raw

PATCH 12/12 env: cosmetic: Consilidate the default env definition

Quoted 1 line · On Friday 17 August 2012 16:49:46 Joe Hershberger wrote:

There used to be a huge structure duplicated 3 times in the source.

if this actually works:

-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120822/a1677d38/attachment.pgp>

#115 lore raw

v2 cover Cleanup fw_*env and a few common env

In reply to Joe Hershberger

This is a gutted version of my previous env series in an effort to reduce the risk to the release that is approaching shortly.

Changes in v2:
- Further simplified based Mike's comments

Joe Hershberger (5):
tools/env: Use a board-specific default env
tools/env: Remove unneeded complexity
tools/env: Don't call env_init() in fw_getenv()
tools/env: Serialize calls to fw_*env
env: Check for NULL pointer in envmatch()

 common/cmd_nvedit.c     |  3 +++
 tools/env/Makefile      |  5 ++--
 tools/env/fw_env.c      | 66 +++++++++++++++++++++----------------------------
 tools/env/fw_env.h      | 19 ++++++++++++++
 tools/env/fw_env_main.c | 59 ++++++++++++++++++++++++++-----------------
 5 files changed, 90 insertions(+), 62 deletions(-)
Signature · 1 line
1.7.11.5
#116 lore raw

PATCH v2 1/5 tools/env: Use a board-specific default env

Originally added in aa701b94336b358798d676eef12a7b90bdac23f5

Before this patch, there was a hard-coded env that was used as default if the env in flash is detected as invalid. Now this tool (compiled for a given board) will share the default env with the u-boot for the board.

Fix include of config.h

Need to define "TEXT_BASE" when building the fw_env tool so that the default env will be correct for environments which use it.

Define __ASSEMBLY__ when calling #include <config.h> so that we only get #defines (all we're interested in).

---

 tools/env/Makefile |  5 +++--
 tools/env/fw_env.c | 28 +++++++++++++---------------
 tools/env/fw_env.h | 19 +++++++++++++++++++
 3 files changed, 35 insertions(+), 17 deletions(-)
tools/env/Makefile +3 -2
index 07634bc..ab73c8c 100644
@@ -24,13 +24,14 @@
 include $(TOPDIR)/config.mk
 
 HOSTSRCS := $(SRCTREE)/lib/crc32.c  fw_env.c  fw_env_main.c
-HEADERS	:= fw_env.h
+HEADERS	:= fw_env.h $(OBJTREE)/include/config.h
 
 # Compile for a hosted environment on the target
 HOSTCPPFLAGS  = -idirafter $(SRCTREE)/include \
 		-idirafter $(OBJTREE)/include2 \
 		-idirafter $(OBJTREE)/include \
-		-DUSE_HOSTCC
+		-DUSE_HOSTCC \
+		-DTEXT_BASE=$(TEXT_BASE)
 
 ifeq ($(MTD_VERSION),old)
 HOSTCPPFLAGS += -DMTD_OLD
tools/env/fw_env.c +13 -15
index 1a2c227..16073ea 100644
@@ -45,8 +45,6 @@
 
 #include "fw_env.h"
 
-#include <config.h>
-
 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
 
 #define min(x, y) ({				\
@@ -81,7 +79,7 @@ static int dev_current;
 #define ENVSECTORS(i) envdevices[(i)].env_sectors
 #define DEVTYPE(i)    envdevices[(i)].mtd_type
 
-#define CONFIG_ENV_SIZE ENVSIZE(dev_current)
+#define CUR_ENVSIZE ENVSIZE(dev_current)
 
 #define ENV_SIZE      getenvsize()
 
@@ -229,7 +227,7 @@ static int get_config (char *);
 #endif
 static inline ulong getenvsize (void)
 {
-	ulong rc = CONFIG_ENV_SIZE - sizeof (long);
+	ulong rc = CUR_ENVSIZE - sizeof(long);
 
 	if (HaveRedundEnv)
 		rc -= sizeof (char);
@@ -445,7 +443,7 @@ int fw_env_write(char *name, char *value)
 		++env;
 	/*
 	 * Overflow when:
-	 * "name" + "=" + "val" +"\0\0"  > CONFIG_ENV_SIZE - (env-environment)
+	 * "name" + "=" + "val" +"\0\0"  > CUR_ENVSIZE - (env-environment)
 	 */
 	len = strlen (name) + 2;
 	/* add '=' for first arg, ' ' for all others */
@@ -960,8 +958,8 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
 	printf ("Writing new environment at 0x%lx on %s\n",
 		DEVOFFSET (dev_target), DEVNAME (dev_target));
 #endif
-	rc = flash_write_buf (dev_target, fd_target, environment.image,
-			      CONFIG_ENV_SIZE, DEVOFFSET (dev_target),
+	rc = flash_write_buf(dev_target, fd_target, environment.image,
+			      CUR_ENVSIZE, DEVOFFSET(dev_target),
 			      DEVTYPE(dev_target));
 	if (rc < 0)
 		return rc;
@@ -1000,10 +998,10 @@ static int flash_read (int fd)
 
 	DEVTYPE(dev_current) = mtdinfo.type;
 
-	rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE,
+	rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
 			     DEVOFFSET (dev_current), mtdinfo.type);
 
-	return (rc != CONFIG_ENV_SIZE) ? -1 : 0;
+	return (rc != CUR_ENVSIZE) ? -1 : 0;
 }
 
 static int flash_io (int mode)
@@ -1100,11 +1098,11 @@ int fw_env_open(void)
 	if (parse_config ())		/* should fill envdevices */
 		return -1;
 
-	addr0 = calloc (1, CONFIG_ENV_SIZE);
+	addr0 = calloc(1, CUR_ENVSIZE);
 	if (addr0 == NULL) {
-		fprintf (stderr,
+		fprintf(stderr,
 			"Not enough memory for environment (%ld bytes)\n",
-			CONFIG_ENV_SIZE);
+			CUR_ENVSIZE);
 		return -1;
 	}
 
@@ -1139,11 +1137,11 @@ int fw_env_open(void)
 		flag0 = *environment.flags;
 
 		dev_current = 1;
-		addr1 = calloc (1, CONFIG_ENV_SIZE);
+		addr1 = calloc(1, CUR_ENVSIZE);
 		if (addr1 == NULL) {
-			fprintf (stderr,
+			fprintf(stderr,
 				"Not enough memory for environment (%ld bytes)\n",
-				CONFIG_ENV_SIZE);
+				CUR_ENVSIZE);
 			return -1;
 		}
 		redundant = addr1;
tools/env/fw_env.h +19 -0
index ad32446..a1a6807 100644
@@ -21,6 +21,15 @@
  * MA 02111-1307 USA
  */
 
+/* Pull in the current config to define the default environment */
+#ifndef __ASSEMBLY__
+#define __ASSEMBLY__ /* get only #defines from config.h */
+#include <config.h>
+#undef	__ASSEMBLY__
+#else
+#include <config.h>
+#endif
+
 /*
  * To build the utility with the static configuration
  * comment out the next line.
@@ -29,6 +38,7 @@
  */
 #define CONFIG_FILE     "/etc/fw_env.config"
 
+#ifndef CONFIG_FILE
 #define HAVE_REDUND /* For systems with 2 env sectors */
 #define DEVICE1_NAME      "/dev/mtd1"
 #define DEVICE2_NAME      "/dev/mtd2"
@@ -40,14 +50,23 @@
 #define ENV2_SIZE         0x4000
 #define DEVICE2_ESIZE     0x4000
 #define DEVICE2_ENVSECTORS     2
+#endif
 
+#ifndef CONFIG_BAUDRATE
 #define CONFIG_BAUDRATE		115200
+#endif
+
+#ifndef CONFIG_BOOTDELAY
 #define CONFIG_BOOTDELAY	5	/* autoboot after 5 seconds	*/
+#endif
+
+#ifndef CONFIG_BOOTCOMMAND
 #define CONFIG_BOOTCOMMAND							\
 	"bootp; "								\
 	"setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} "	\
 	"ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; "	\
 	"bootm"
+#endif
 
 extern int   fw_printenv(int argc, char *argv[]);
 extern char *fw_getenv  (char *name);
Signature · 1 line
1.7.11.5
#117 lore raw

PATCH v2 2/5 tools/env: Remove unneeded complexity

In reply to Joe Hershberger

The length included the name length, and then it was subtracted back out on each use. Now we don't include it in the first place. Also realloc as we process arguments and eliminate memset. Use memcpy instead of manually copying each byte.

---
Changes in v2:
- Further simplified based Mike's comments

 tools/env/fw_env.c | 33 +++++++++++++--------------------
 1 file changed, 13 insertions(+), 20 deletions(-)
tools/env/fw_env.c +13 -20
index 16073ea..af879f1 100644
@@ -481,7 +481,6 @@ int fw_setenv(int argc, char *argv[])
 	int i, len;
 	char *name;
 	char *value = NULL;
-	char *tmpval = NULL;
 
 	if (argc < 2) {
 		errno = EINVAL;
@@ -495,34 +494,28 @@ int fw_setenv(int argc, char *argv[])
 
 	name = argv[1];
 
-	len = strlen(name) + 2;
-	for (i = 2; i < argc; ++i)
-		len += strlen(argv[i]) + 1;
-
-	/* Allocate enough place to the data string */
+	len = 0;
 	for (i = 2; i < argc; ++i) {
 		char *val = argv[i];
+		size_t val_len = strlen(val);
+
+		value = realloc(value, len + val_len + 1);
 		if (!value) {
-			value = (char *)malloc(len - strlen(name));
-			if (!value) {
-				fprintf(stderr,
+			fprintf(stderr,
 				"Cannot malloc %zu bytes: %s\n",
-				len - strlen(name), strerror(errno));
-				return -1;
-			}
-			memset(value, 0, len - strlen(name));
-			tmpval = value;
+				len, strerror(errno));
+			return -1;
 		}
-		if (i != 2)
-			*tmpval++ = ' ';
-		while (*val != '\0')
-			*tmpval++ = *val++;
+
+		memcpy(value + len, val, val_len);
+		len += val_len;
+		value[len++] = ' ';
 	}
+	value[len - 1] = '\0';
 
 	fw_env_write(name, value);
 
-	if (value)
-		free(value);
+	free(value);
 
 	return fw_env_close();
 }
Signature · 1 line
1.7.11.5
#118 lore raw

PATCH v2 3/5 tools/env: Don't call env_init() in fw_getenv()

In reply to Joe Hershberger

We will only call fw_getenv when the env has already been initialized.

---

 tools/env/fw_env.c | 3 ---
 1 file changed, 3 deletions(-)
tools/env/fw_env.c +0 -3
index af879f1..f883804 100644
@@ -258,9 +258,6 @@ char *fw_getenv (char *name)
 {
 	char *env, *nxt;
 
-	if (fw_env_open())
-		return NULL;
-
 	for (env = environment.data; *env; env = nxt + 1) {
 		char *val;
 
Signature · 1 line
1.7.11.5
#119 lore raw

PATCH v2 4/5 tools/env: Serialize calls to fw_*env

In reply to Joe Hershberger

Use a lock file at /var/lock/fw_printenv.lock.
Avoids seriously confusing the MTD driver.

---

 tools/env/fw_env_main.c | 59 +++++++++++++++++++++++++++++++------------------
 1 file changed, 37 insertions(+), 22 deletions(-)
tools/env/fw_env_main.c +37 -22
index c654057..c855f4c 100644
@@ -39,10 +39,13 @@
  *		  variable "name"
  */
 
+#include <fcntl.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <getopt.h>
+#include <sys/file.h>
+#include <unistd.h>
 #include "fw_env.h"
 
 #define	CMD_PRINTENV	"fw_printenv"
@@ -81,13 +84,27 @@ void usage(void)
 	);
 }
 
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 	char *p;
 	char *cmdname = *argv;
 	char *script_file = NULL;
 	int c;
+	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
+	int lockfd = -1;
+	int retval = EXIT_SUCCESS;
+
+	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC);
+	if (-1 == lockfd) {
+		fprintf(stderr, "Error opening lock file %s\n", lockname);
+		return EXIT_FAILURE;
+	}
+
+	if (-1 == flock(lockfd, LOCK_EX)) {
+		fprintf(stderr, "Error locking file %s\n", lockname);
+		close(lockfd);
+		return EXIT_FAILURE;
+	}
 
 	if ((p = strrchr (cmdname, '/')) != NULL) {
 		cmdname = p + 1;
@@ -104,38 +121,36 @@ main(int argc, char *argv[])
 			break;
 		case 'h':
 			usage();
-			return EXIT_SUCCESS;
+			goto exit;
 		default: /* '?' */
 			fprintf(stderr, "Try `%s --help' for more information."
 				"\n", cmdname);
-			return EXIT_FAILURE;
+			retval = EXIT_FAILURE;
+			goto exit;
 		}
 	}
 
-
 	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
-
-		if (fw_printenv (argc, argv) != 0)
-			return EXIT_FAILURE;
-
-		return EXIT_SUCCESS;
-
+		if (fw_printenv(argc, argv) != 0)
+			retval = EXIT_FAILURE;
 	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
 		if (!script_file) {
 			if (fw_setenv(argc, argv) != 0)
-				return EXIT_FAILURE;
+				retval = EXIT_FAILURE;
 		} else {
 			if (fw_parse_script(script_file) != 0)
-				return EXIT_FAILURE;
+				retval = EXIT_FAILURE;
 		}
-
-		return EXIT_SUCCESS;
-
+	} else {
+		fprintf(stderr,
+			"Identity crisis - may be called as `" CMD_PRINTENV
+			"' or as `" CMD_SETENV "' but not as `%s'\n",
+			cmdname);
+		retval = EXIT_FAILURE;
 	}
 
-	fprintf (stderr,
-		"Identity crisis - may be called as `" CMD_PRINTENV
-		"' or as `" CMD_SETENV "' but not as `%s'\n",
-		cmdname);
-	return EXIT_FAILURE;
+exit:
+	flock(lockfd, LOCK_UN);
+	close(lockfd);
+	return retval;
 }
Signature · 1 line
1.7.11.5
#120 lore raw

PATCH v2 4/5 tools/env: Serialize calls to fw_*env

Quoted 8 lines · Joe Hershberger

Use a lock file at /var/lock/fw_printenv.lock.
Avoids seriously confusing the MTD driver.

---

 tools/env/fw_env_main.c | 59 +++++++++++++++++++++++++++++++------------------
 1 file changed, 37 insertions(+), 22 deletions(-)

I thing we should document usage and location of lock file in tools/env/README.

Luka

#121 lore raw

PATCH 1/1 tools: Add a README note about fw_printenv lock file

Add a mention of the lock file to the README for the fw_printenv tool.

---
This patch is independent of the series, but should be applied afterward

 tools/env/README | 4 ++++
 1 file changed, 4 insertions(+)
tools/env/README +4 -0
index 3f0d77e..df020e4 100644
@@ -55,3 +55,7 @@ partition where the environment resides.
 DEVICEx_ENVSECTORS defines the number of sectors that may be used for
 this environment instance. On NAND this is used to limit the range
 within which bad blocks are skipped, on NOR it is not used.
+
+To prevent losing changes to the environment and to prevent confusing the MTD
+drivers, a lock file at /var/lock/fw_printenv.lock is used to serialize access
+to the environment.
Signature · 1 line
1.7.11.5
#122 lore raw

PATCH v2 5/5 env: Check for NULL pointer in envmatch()

In reply to Joe Hershberger

If the pointer passed into envmatch() is NULL, return -1 instead of crashing.

---

 common/cmd_nvedit.c | 3 +++
 tools/env/fw_env.c  | 2 ++
 2 files changed, 5 insertions(+)
common/cmd_nvedit.c +3 -0
index 3474bc6..5a28dee 100644
@@ -646,6 +646,9 @@ U_BOOT_CMD(
  */
 int envmatch(uchar *s1, int i2)
 {
+	if (s1 == NULL)
+		return -1;
+
 	while (*s1 == env_get_char(i2++))
 		if (*s1++ == '=')
 			return i2;
tools/env/fw_env.c +2 -0
index f883804..ed56ba0 100644
@@ -1060,6 +1060,8 @@ exit:
 
 static char *envmatch (char * s1, char * s2)
 {
+	if (s1 == NULL || s2 == NULL)
+		return NULL;
 
 	while (*s1 == *s2++)
 		if (*s1++ == '=')
Signature · 1 line
1.7.11.5
#123 lore raw

v2 cover Cleanup fw_*env and a few common env

In reply to Joe Hershberger
Quoted 13 lines · Joe Hershberger

This is a gutted version of my previous env series in an effort to reduce the risk to the release that is approaching shortly.

Changes in v2:
- Further simplified based Mike's comments

Joe Hershberger (5):
tools/env: Use a board-specific default env
tools/env: Remove unneeded complexity
tools/env: Don't call env_init() in fw_getenv()
tools/env: Serialize calls to fw_*env
env: Check for NULL pointer in envmatch()

Quoted 6 lines
 common/cmd_nvedit.c     |  3 +++
 tools/env/Makefile      |  5 ++--
 tools/env/fw_env.c      | 66 +++++++++++++++++++++----------------------------
 tools/env/fw_env.h      | 19 ++++++++++++++
 tools/env/fw_env_main.c | 59 ++++++++++++++++++++++++++-----------------
 5 files changed, 90 insertions(+), 62 deletions(-)

This, and the follow up README have been applied to u-boot/next, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20121009/e92c473a/attachment.pgp>