]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
tools/env: Add environment variable flags support
authorJoe Hershberger <joe.hershberger@ni.com>
Wed, 12 Dec 2012 04:16:32 +0000 (22:16 -0600)
committerTom Rini <trini@ti.com>
Thu, 13 Dec 2012 18:46:56 +0000 (11:46 -0700)
Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.  Call
env_acl_validate_setenv_params() from setenv() in fw_env.c.

If the entry is not found in the env .flags, then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Need to build in _ctype for isdigit for Linux.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
common/env_attr.c
common/env_flags.c
include/env_flags.h
tools/env/Makefile
tools/env/fw_env.c

index 7d330a58bba8770af7cfc725ebd11758f11223f4..210c98dcfdd338f84fa3a7ab2d776b0790db15eb 100644 (file)
  * MA 02111-1307 USA
  */
 
  * MA 02111-1307 USA
  */
 
+#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
+#include <stdint.h>
+#include <stdio.h>
+#include <linux/linux_string.h>
+#else
 #include <common.h>
 #include <common.h>
+#endif
+
 #include <env_attr.h>
 #include <errno.h>
 #include <linux/string.h>
 #include <env_attr.h>
 #include <errno.h>
 #include <linux/string.h>
index a58d614bb3f63e871a63500057b484dbd9305f1d..ed0857cf9e7a42cec3364f5d9a425b562a188a2d 100644 (file)
 #include <linux/string.h>
 #include <linux/ctype.h>
 
 #include <linux/string.h>
 #include <linux/ctype.h>
 
+#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
+#include <stdint.h>
+#include <stdio.h>
+#include "fw_env.h"
+#include <env_attr.h>
+#include <env_flags.h>
+#define getenv fw_getenv
+#else
 #include <common.h>
 #include <environment.h>
 #include <common.h>
 #include <environment.h>
+#endif
 
 #ifdef CONFIG_CMD_NET
 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
 
 #ifdef CONFIG_CMD_NET
 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
@@ -179,6 +188,70 @@ static inline int env_flags_lookup(const char *flags_list, const char *name,
        return ret;
 }
 
        return ret;
 }
 
+#ifdef USE_HOSTCC /* Functions only used from tools/env */
+/*
+ * Look up any flags directly from the .flags variable and the static list
+ * and convert them to the vartype enum.
+ */
+enum env_flags_vartype env_flags_get_type(const char *name)
+{
+       const char *flags_list = getenv(ENV_FLAGS_VAR);
+       char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
+
+       if (env_flags_lookup(flags_list, name, flags))
+               return env_flags_vartype_string;
+
+       if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
+               return env_flags_vartype_string;
+
+       return env_flags_parse_vartype(flags);
+}
+
+/*
+ * Validate that the proposed new value for "name" is valid according to the
+ * defined flags for that variable, if any.
+ */
+int env_flags_validate_type(const char *name, const char *value)
+{
+       enum env_flags_vartype type;
+
+       if (value == NULL)
+               return 0;
+       type = env_flags_get_type(name);
+       if (_env_flags_validate_type(value, type) < 0) {
+               printf("## Error: flags type check failure for "
+                       "\"%s\" <= \"%s\" (type: %c)\n",
+                       name, value, env_flags_vartype_rep[type]);
+               return -1;
+       }
+       return 0;
+}
+
+/*
+ * Validate the parameters to "env set" directly
+ */
+int env_flags_validate_env_set_params(int argc, char * const argv[])
+{
+       if ((argc >= 3) && argv[2] != NULL) {
+               enum env_flags_vartype type = env_flags_get_type(argv[1]);
+
+               /*
+                * we don't currently check types that need more than
+                * one argument
+                */
+               if (type != env_flags_vartype_string && argc > 3) {
+                       printf("## Error: too many parameters for setting "
+                               "\"%s\"\n", argv[1]);
+                       return -1;
+               }
+               return env_flags_validate_type(argv[1], argv[2]);
+       }
+       /* ok */
+       return 0;
+}
+
+#else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
+
 /*
  * Parse the flag charachters from the .flags attribute list into the binary
  * form to be stored in the environment entry->flags field.
 /*
  * Parse the flag charachters from the .flags attribute list into the binary
  * form to be stored in the environment entry->flags field.
@@ -317,3 +390,5 @@ int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
 
        return 0;
 }
 
        return 0;
 }
+
+#endif
index bf25f2746bc1157a70139675e08e1ad12b6631d5..33334464d430ae77c7e1eb130ff815a1667da3b4 100644 (file)
@@ -52,6 +52,23 @@ enum env_flags_vartype {
  */
 enum env_flags_vartype env_flags_parse_vartype(const char *flags);
 
  */
 enum env_flags_vartype env_flags_parse_vartype(const char *flags);
 
+#ifdef USE_HOSTCC
+/*
+ * Look up the type of a variable directly from the .flags var.
+ */
+enum env_flags_vartype env_flags_get_type(const char *name);
+/*
+ * Validate the newval for its type to conform with the requirements defined by
+ * its flags (directly looked at the .flags var).
+ */
+int env_flags_validate_type(const char *name, const char *newval);
+/*
+ * Validate the parameters passed to "env set" for type compliance
+ */
+int env_flags_validate_env_set_params(int argc, char * const argv[]);
+
+#else /* !USE_HOSTCC */
+
 #include <search.h>
 
 /*
 #include <search.h>
 
 /*
@@ -73,4 +90,6 @@ int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
 #define ENV_FLAGS_VARTYPE_BIN_MASK     0x00000007
 /* The actual variable type values use the enum value (within the mask) */
 
 #define ENV_FLAGS_VARTYPE_BIN_MASK     0x00000007
 /* The actual variable type values use the enum value (within the mask) */
 
+#endif /* USE_HOSTCC */
+
 #endif /* __ENV_FLAGS_H__ */
 #endif /* __ENV_FLAGS_H__ */
index ab73c8c744b6401c4eb88b1e5083fbfc8794ce35..0e798e09403168d67abc327432f39fcecd5c7b26 100644 (file)
 include $(TOPDIR)/config.mk
 
 HOSTSRCS := $(SRCTREE)/lib/crc32.c  fw_env.c  fw_env_main.c
 include $(TOPDIR)/config.mk
 
 HOSTSRCS := $(SRCTREE)/lib/crc32.c  fw_env.c  fw_env_main.c
+HOSTSRCS += $(SRCTREE)/lib/ctype.c $(SRCTREE)/lib/linux_string.c
+HOSTSRCS += $(SRCTREE)/common/env_attr.c $(SRCTREE)/common/env_flags.c
 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 \
 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 \
+               -idirafter $(SRCTREE)/tools/env \
                -DUSE_HOSTCC \
                -DTEXT_BASE=$(TEXT_BASE)
 
                -DUSE_HOSTCC \
                -DTEXT_BASE=$(TEXT_BASE)
 
index 9b023e807bc446bebf10caf2864d92dbed8469b3..5be36fc35956b69d36f131fe3d99558f68b6d058 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include <errno.h>
  */
 
 #include <errno.h>
+#include <env_flags.h>
 #include <fcntl.h>
 #include <linux/stringify.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <linux/stringify.h>
 #include <stdio.h>
@@ -395,6 +396,9 @@ int fw_setenv(int argc, char *argv[])
 
        name = argv[1];
 
 
        name = argv[1];
 
+       if (env_flags_validate_env_set_params(argc, argv) < 0)
+               return 1;
+
        len = 0;
        for (i = 2; i < argc; ++i) {
                char *val = argv[i];
        len = 0;
        for (i = 2; i < argc; ++i) {
                char *val = argv[i];
@@ -516,6 +520,11 @@ int fw_parse_script(char *fname)
                        name, val ? val : " removed");
 #endif
 
                        name, val ? val : " removed");
 #endif
 
+               if (env_flags_validate_type(name, val) < 0) {
+                       ret = -1;
+                       break;
+               }
+
                /*
                 * If there is an error setting a variable,
                 * try to save the environment and returns an error
                /*
                 * If there is an error setting a variable,
                 * try to save the environment and returns an error