3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <linux/string.h>
25 #include <linux/ctype.h>
27 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
32 #include <env_flags.h>
33 #define getenv fw_getenv
36 #include <environment.h>
40 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
42 #define ENV_FLAGS_NET_VARTYPE_REPS ""
45 static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
48 * Parse the flags string from a .flags attribute list into the vartype enum.
50 enum env_flags_vartype env_flags_parse_vartype(const char *flags)
54 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
55 return env_flags_vartype_string;
57 type = strchr(env_flags_vartype_rep,
58 flags[ENV_FLAGS_VARTYPE_LOC]);
61 return (enum env_flags_vartype)
62 (type - &env_flags_vartype_rep[0]);
64 printf("## Warning: Unknown environment variable type '%c'\n",
65 flags[ENV_FLAGS_VARTYPE_LOC]);
66 return env_flags_vartype_string;
69 static inline int is_hex_prefix(const char *value)
71 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
74 static void skip_num(int hex, const char *value, const char **end,
79 if (hex && is_hex_prefix(value))
82 for (i = max_digits; i != 0; i--) {
83 if (hex && !isxdigit(*value))
85 if (!hex && !isdigit(*value))
94 * Based on the declared type enum, validate that the value string complies
97 static int _env_flags_validate_type(const char *value,
98 enum env_flags_vartype type)
101 #ifdef CONFIG_CMD_NET
107 case env_flags_vartype_string:
109 case env_flags_vartype_decimal:
110 skip_num(0, value, &end, -1);
114 case env_flags_vartype_hex:
115 skip_num(1, value, &end, -1);
118 if (value + 2 == end && is_hex_prefix(value))
121 case env_flags_vartype_bool:
122 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
123 value[0] != 'Y' && value[0] != 'T' &&
124 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
125 value[0] != 'N' && value[0] != 'F')
127 if (value[1] != '\0')
130 #ifdef CONFIG_CMD_NET
131 case env_flags_vartype_ipaddr:
133 for (i = 0; i < 4; i++) {
134 skip_num(0, cur, &end, 3);
137 if (i != 3 && *end != '.')
139 if (i == 3 && *end != '\0')
144 case env_flags_vartype_macaddr:
146 for (i = 0; i < 6; i++) {
147 skip_num(1, cur, &end, 2);
150 if (cur + 2 == end && is_hex_prefix(cur))
152 if (i != 5 && *end != ':')
154 if (i == 5 && *end != '\0')
160 case env_flags_vartype_end:
169 * Look for flags in a provided list and failing that the static list
171 static inline int env_flags_lookup(const char *flags_list, const char *name,
180 /* try the env first */
182 ret = env_attr_lookup(flags_list, name, flags);
185 /* if not found in the env, look in the static list */
186 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
191 #ifdef USE_HOSTCC /* Functions only used from tools/env */
193 * Look up any flags directly from the .flags variable and the static list
194 * and convert them to the vartype enum.
196 enum env_flags_vartype env_flags_get_type(const char *name)
198 const char *flags_list = getenv(ENV_FLAGS_VAR);
199 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
201 if (env_flags_lookup(flags_list, name, flags))
202 return env_flags_vartype_string;
204 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
205 return env_flags_vartype_string;
207 return env_flags_parse_vartype(flags);
211 * Validate that the proposed new value for "name" is valid according to the
212 * defined flags for that variable, if any.
214 int env_flags_validate_type(const char *name, const char *value)
216 enum env_flags_vartype type;
220 type = env_flags_get_type(name);
221 if (_env_flags_validate_type(value, type) < 0) {
222 printf("## Error: flags type check failure for "
223 "\"%s\" <= \"%s\" (type: %c)\n",
224 name, value, env_flags_vartype_rep[type]);
231 * Validate the parameters to "env set" directly
233 int env_flags_validate_env_set_params(int argc, char * const argv[])
235 if ((argc >= 3) && argv[2] != NULL) {
236 enum env_flags_vartype type = env_flags_get_type(argv[1]);
239 * we don't currently check types that need more than
242 if (type != env_flags_vartype_string && argc > 3) {
243 printf("## Error: too many parameters for setting "
244 "\"%s\"\n", argv[1]);
247 return env_flags_validate_type(argv[1], argv[2]);
253 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
256 * Parse the flag charachters from the .flags attribute list into the binary
257 * form to be stored in the environment entry->flags field.
259 static int env_parse_flags_to_bin(const char *flags)
261 return env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
265 * Look for possible flags for a newly added variable
266 * This is called specifically when the variable did not exist in the hash
267 * previously, so the blanket update did not find this variable.
269 void env_flags_init(ENTRY *var_entry)
271 const char *var_name = var_entry->key;
272 const char *flags_list = getenv(ENV_FLAGS_VAR);
273 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
276 /* look in the ".flags" and static for a reference to this variable */
277 ret = env_flags_lookup(flags_list, var_name, flags);
279 /* if any flags were found, set the binary form to the entry */
280 if (!ret && strlen(flags))
281 var_entry->flags = env_parse_flags_to_bin(flags);
285 * Called on each existing env var prior to the blanket update since removing
286 * a flag in the flag list should remove its flags.
288 static int clear_flags(ENTRY *entry)
296 * Call for each element in the list that defines flags for a variable
298 static int set_flags(const char *name, const char *value)
304 hsearch_r(e, FIND, &ep, &env_htab, 0);
306 /* does the env variable actually exist? */
308 /* the flag list is empty, so clear the flags */
309 if (value == NULL || strlen(value) == 0)
312 /* assign the requested flags */
313 ep->flags = env_parse_flags_to_bin(value);
319 static int on_flags(const char *name, const char *value, enum env_op op,
322 /* remove all flags */
323 hwalk_r(&env_htab, clear_flags);
325 /* configure any static flags */
326 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
327 /* configure any dynamic flags */
328 env_attr_walk(value, set_flags);
332 U_BOOT_ENV_CALLBACK(flags, on_flags);
335 * Perform consistency checking before creating, overwriting, or deleting an
336 * environment variable. Called as a callback function by hsearch_r() and
337 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
338 * When (flag & H_FORCE) is set, do not print out any error message and force
339 * overwriting of write-once variables.
342 int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
346 #if !defined(CONFIG_ENV_OVERWRITE) && defined(CONFIG_OVERWRITE_ETHADDR_ONCE) \
347 && defined(CONFIG_ETHADDR)
348 const char *oldval = NULL;
350 if (op != env_op_create)
356 /* Default value for NULL to protect string-manipulating functions */
357 newval = newval ? : "";
359 #ifndef CONFIG_ENV_OVERWRITE
361 * Some variables like "ethaddr" and "serial#" can be set only once and
362 * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
364 if (op != env_op_create && /* variable exists */
365 (flag & H_FORCE) == 0) { /* and we are not forced */
366 if (strcmp(name, "serial#") == 0 ||
367 (strcmp(name, "ethaddr") == 0
368 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
369 && strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0
370 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
372 printf("Can't overwrite \"%s\"\n", name);
378 /* validate the value to match the variable type */
379 if (op != env_op_delete) {
380 enum env_flags_vartype type = (enum env_flags_vartype)
381 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
383 if (_env_flags_validate_type(newval, type) < 0) {
384 printf("## Error: flags type check failure for "
385 "\"%s\" <= \"%s\" (type: %c)\n",
386 name, newval, env_flags_vartype_rep[type]);