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;
46 #ifdef CONFIG_CMD_ENV_FLAGS
47 static const char * const env_flags_vartype_names[] = {
59 * Print the whole list of available type flags.
61 void env_flags_print_vartypes(void)
63 enum env_flags_vartype curtype = (enum env_flags_vartype)0;
65 while (curtype != env_flags_vartype_end) {
66 printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype],
67 env_flags_vartype_names[curtype]);
73 * Return the name of the type.
75 const char *env_flags_get_vartype_name(enum env_flags_vartype type)
77 return env_flags_vartype_names[type];
79 #endif /* CONFIG_CMD_ENV_FLAGS */
82 * Parse the flags string from a .flags attribute list into the vartype enum.
84 enum env_flags_vartype env_flags_parse_vartype(const char *flags)
88 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
89 return env_flags_vartype_string;
91 type = strchr(env_flags_vartype_rep,
92 flags[ENV_FLAGS_VARTYPE_LOC]);
95 return (enum env_flags_vartype)
96 (type - &env_flags_vartype_rep[0]);
98 printf("## Warning: Unknown environment variable type '%c'\n",
99 flags[ENV_FLAGS_VARTYPE_LOC]);
100 return env_flags_vartype_string;
103 static inline int is_hex_prefix(const char *value)
105 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
108 static void skip_num(int hex, const char *value, const char **end,
113 if (hex && is_hex_prefix(value))
116 for (i = max_digits; i != 0; i--) {
117 if (hex && !isxdigit(*value))
119 if (!hex && !isdigit(*value))
128 * Based on the declared type enum, validate that the value string complies
131 static int _env_flags_validate_type(const char *value,
132 enum env_flags_vartype type)
135 #ifdef CONFIG_CMD_NET
141 case env_flags_vartype_string:
143 case env_flags_vartype_decimal:
144 skip_num(0, value, &end, -1);
148 case env_flags_vartype_hex:
149 skip_num(1, value, &end, -1);
152 if (value + 2 == end && is_hex_prefix(value))
155 case env_flags_vartype_bool:
156 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
157 value[0] != 'Y' && value[0] != 'T' &&
158 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
159 value[0] != 'N' && value[0] != 'F')
161 if (value[1] != '\0')
164 #ifdef CONFIG_CMD_NET
165 case env_flags_vartype_ipaddr:
167 for (i = 0; i < 4; i++) {
168 skip_num(0, cur, &end, 3);
171 if (i != 3 && *end != '.')
173 if (i == 3 && *end != '\0')
178 case env_flags_vartype_macaddr:
180 for (i = 0; i < 6; i++) {
181 skip_num(1, cur, &end, 2);
184 if (cur + 2 == end && is_hex_prefix(cur))
186 if (i != 5 && *end != ':')
188 if (i == 5 && *end != '\0')
194 case env_flags_vartype_end:
203 * Look for flags in a provided list and failing that the static list
205 static inline int env_flags_lookup(const char *flags_list, const char *name,
214 /* try the env first */
216 ret = env_attr_lookup(flags_list, name, flags);
219 /* if not found in the env, look in the static list */
220 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
225 #ifdef USE_HOSTCC /* Functions only used from tools/env */
227 * Look up any flags directly from the .flags variable and the static list
228 * and convert them to the vartype enum.
230 enum env_flags_vartype env_flags_get_type(const char *name)
232 const char *flags_list = getenv(ENV_FLAGS_VAR);
233 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
235 if (env_flags_lookup(flags_list, name, flags))
236 return env_flags_vartype_string;
238 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
239 return env_flags_vartype_string;
241 return env_flags_parse_vartype(flags);
245 * Validate that the proposed new value for "name" is valid according to the
246 * defined flags for that variable, if any.
248 int env_flags_validate_type(const char *name, const char *value)
250 enum env_flags_vartype type;
254 type = env_flags_get_type(name);
255 if (_env_flags_validate_type(value, type) < 0) {
256 printf("## Error: flags type check failure for "
257 "\"%s\" <= \"%s\" (type: %c)\n",
258 name, value, env_flags_vartype_rep[type]);
265 * Validate the parameters to "env set" directly
267 int env_flags_validate_env_set_params(int argc, char * const argv[])
269 if ((argc >= 3) && argv[2] != NULL) {
270 enum env_flags_vartype type = env_flags_get_type(argv[1]);
273 * we don't currently check types that need more than
276 if (type != env_flags_vartype_string && argc > 3) {
277 printf("## Error: too many parameters for setting "
278 "\"%s\"\n", argv[1]);
281 return env_flags_validate_type(argv[1], argv[2]);
287 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
290 * Parse the flag charachters from the .flags attribute list into the binary
291 * form to be stored in the environment entry->flags field.
293 static int env_parse_flags_to_bin(const char *flags)
295 return env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
299 * Look for possible flags for a newly added variable
300 * This is called specifically when the variable did not exist in the hash
301 * previously, so the blanket update did not find this variable.
303 void env_flags_init(ENTRY *var_entry)
305 const char *var_name = var_entry->key;
306 const char *flags_list = getenv(ENV_FLAGS_VAR);
307 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
310 /* look in the ".flags" and static for a reference to this variable */
311 ret = env_flags_lookup(flags_list, var_name, flags);
313 /* if any flags were found, set the binary form to the entry */
314 if (!ret && strlen(flags))
315 var_entry->flags = env_parse_flags_to_bin(flags);
319 * Called on each existing env var prior to the blanket update since removing
320 * a flag in the flag list should remove its flags.
322 static int clear_flags(ENTRY *entry)
330 * Call for each element in the list that defines flags for a variable
332 static int set_flags(const char *name, const char *value)
338 hsearch_r(e, FIND, &ep, &env_htab, 0);
340 /* does the env variable actually exist? */
342 /* the flag list is empty, so clear the flags */
343 if (value == NULL || strlen(value) == 0)
346 /* assign the requested flags */
347 ep->flags = env_parse_flags_to_bin(value);
353 static int on_flags(const char *name, const char *value, enum env_op op,
356 /* remove all flags */
357 hwalk_r(&env_htab, clear_flags);
359 /* configure any static flags */
360 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
361 /* configure any dynamic flags */
362 env_attr_walk(value, set_flags);
366 U_BOOT_ENV_CALLBACK(flags, on_flags);
369 * Perform consistency checking before creating, overwriting, or deleting an
370 * environment variable. Called as a callback function by hsearch_r() and
371 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
372 * When (flag & H_FORCE) is set, do not print out any error message and force
373 * overwriting of write-once variables.
376 int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
380 #if !defined(CONFIG_ENV_OVERWRITE) && defined(CONFIG_OVERWRITE_ETHADDR_ONCE) \
381 && defined(CONFIG_ETHADDR)
382 const char *oldval = NULL;
384 if (op != env_op_create)
390 /* Default value for NULL to protect string-manipulating functions */
391 newval = newval ? : "";
393 #ifndef CONFIG_ENV_OVERWRITE
395 * Some variables like "ethaddr" and "serial#" can be set only once and
396 * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
398 if (op != env_op_create && /* variable exists */
399 (flag & H_FORCE) == 0) { /* and we are not forced */
400 if (strcmp(name, "serial#") == 0 ||
401 (strcmp(name, "ethaddr") == 0
402 #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
403 && strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0
404 #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
406 printf("Can't overwrite \"%s\"\n", name);
412 /* validate the value to match the variable type */
413 if (op != env_op_delete) {
414 enum env_flags_vartype type = (enum env_flags_vartype)
415 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
417 if (_env_flags_validate_type(newval, type) < 0) {
418 printf("## Error: flags type check failure for "
419 "\"%s\" <= \"%s\" (type: %c)\n",
420 name, newval, env_flags_vartype_rep[type]);