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 static const char env_flags_varaccess_rep
[] = "aroc";
47 static const int env_flags_varaccess_mask
[] = {
49 ENV_FLAGS_VARACCESS_PREVENT_DELETE
|
50 ENV_FLAGS_VARACCESS_PREVENT_CREATE
|
51 ENV_FLAGS_VARACCESS_PREVENT_OVERWR
,
52 ENV_FLAGS_VARACCESS_PREVENT_DELETE
|
53 ENV_FLAGS_VARACCESS_PREVENT_OVERWR
,
54 ENV_FLAGS_VARACCESS_PREVENT_DELETE
|
55 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR
};
57 #ifdef CONFIG_CMD_ENV_FLAGS
58 static const char * const env_flags_vartype_names
[] = {
68 static const char * const env_flags_varaccess_names
[] = {
76 * Print the whole list of available type flags.
78 void env_flags_print_vartypes(void)
80 enum env_flags_vartype curtype
= (enum env_flags_vartype
)0;
82 while (curtype
!= env_flags_vartype_end
) {
83 printf("\t%c -\t%s\n", env_flags_vartype_rep
[curtype
],
84 env_flags_vartype_names
[curtype
]);
90 * Print the whole list of available access flags.
92 void env_flags_print_varaccess(void)
94 enum env_flags_varaccess curaccess
= (enum env_flags_varaccess
)0;
96 while (curaccess
!= env_flags_varaccess_end
) {
97 printf("\t%c -\t%s\n", env_flags_varaccess_rep
[curaccess
],
98 env_flags_varaccess_names
[curaccess
]);
104 * Return the name of the type.
106 const char *env_flags_get_vartype_name(enum env_flags_vartype type
)
108 return env_flags_vartype_names
[type
];
112 * Return the name of the access.
114 const char *env_flags_get_varaccess_name(enum env_flags_varaccess access
)
116 return env_flags_varaccess_names
[access
];
118 #endif /* CONFIG_CMD_ENV_FLAGS */
121 * Parse the flags string from a .flags attribute list into the vartype enum.
123 enum env_flags_vartype
env_flags_parse_vartype(const char *flags
)
127 if (strlen(flags
) <= ENV_FLAGS_VARTYPE_LOC
)
128 return env_flags_vartype_string
;
130 type
= strchr(env_flags_vartype_rep
,
131 flags
[ENV_FLAGS_VARTYPE_LOC
]);
134 return (enum env_flags_vartype
)
135 (type
- &env_flags_vartype_rep
[0]);
137 printf("## Warning: Unknown environment variable type '%c'\n",
138 flags
[ENV_FLAGS_VARTYPE_LOC
]);
139 return env_flags_vartype_string
;
143 * Parse the flags string from a .flags attribute list into the varaccess enum.
145 enum env_flags_varaccess
env_flags_parse_varaccess(const char *flags
)
149 if (strlen(flags
) <= ENV_FLAGS_VARACCESS_LOC
)
150 return env_flags_varaccess_any
;
152 access
= strchr(env_flags_varaccess_rep
,
153 flags
[ENV_FLAGS_VARACCESS_LOC
]);
156 return (enum env_flags_varaccess
)
157 (access
- &env_flags_varaccess_rep
[0]);
159 printf("## Warning: Unknown environment variable access method '%c'\n",
160 flags
[ENV_FLAGS_VARACCESS_LOC
]);
161 return env_flags_varaccess_any
;
165 * Parse the binary flags from a hash table entry into the varaccess enum.
167 enum env_flags_varaccess
env_flags_parse_varaccess_from_binflags(int binflags
)
171 for (i
= 0; i
< sizeof(env_flags_varaccess_mask
); i
++)
172 if (env_flags_varaccess_mask
[i
] ==
173 (binflags
& ENV_FLAGS_VARACCESS_BIN_MASK
))
174 return (enum env_flags_varaccess
)i
;
176 printf("Warning: Non-standard access flags. (0x%x)\n",
177 binflags
& ENV_FLAGS_VARACCESS_BIN_MASK
);
179 return env_flags_varaccess_any
;
182 static inline int is_hex_prefix(const char *value
)
184 return value
[0] == '0' && (value
[1] == 'x' || value
[1] == 'X');
187 static void skip_num(int hex
, const char *value
, const char **end
,
192 if (hex
&& is_hex_prefix(value
))
195 for (i
= max_digits
; i
!= 0; i
--) {
196 if (hex
&& !isxdigit(*value
))
198 if (!hex
&& !isdigit(*value
))
207 * Based on the declared type enum, validate that the value string complies
210 static int _env_flags_validate_type(const char *value
,
211 enum env_flags_vartype type
)
214 #ifdef CONFIG_CMD_NET
220 case env_flags_vartype_string
:
222 case env_flags_vartype_decimal
:
223 skip_num(0, value
, &end
, -1);
227 case env_flags_vartype_hex
:
228 skip_num(1, value
, &end
, -1);
231 if (value
+ 2 == end
&& is_hex_prefix(value
))
234 case env_flags_vartype_bool
:
235 if (value
[0] != '1' && value
[0] != 'y' && value
[0] != 't' &&
236 value
[0] != 'Y' && value
[0] != 'T' &&
237 value
[0] != '0' && value
[0] != 'n' && value
[0] != 'f' &&
238 value
[0] != 'N' && value
[0] != 'F')
240 if (value
[1] != '\0')
243 #ifdef CONFIG_CMD_NET
244 case env_flags_vartype_ipaddr
:
246 for (i
= 0; i
< 4; i
++) {
247 skip_num(0, cur
, &end
, 3);
250 if (i
!= 3 && *end
!= '.')
252 if (i
== 3 && *end
!= '\0')
257 case env_flags_vartype_macaddr
:
259 for (i
= 0; i
< 6; i
++) {
260 skip_num(1, cur
, &end
, 2);
263 if (cur
+ 2 == end
&& is_hex_prefix(cur
))
265 if (i
!= 5 && *end
!= ':')
267 if (i
== 5 && *end
!= '\0')
273 case env_flags_vartype_end
:
282 * Look for flags in a provided list and failing that the static list
284 static inline int env_flags_lookup(const char *flags_list
, const char *name
,
293 /* try the env first */
295 ret
= env_attr_lookup(flags_list
, name
, flags
);
298 /* if not found in the env, look in the static list */
299 ret
= env_attr_lookup(ENV_FLAGS_LIST_STATIC
, name
, flags
);
304 #ifdef USE_HOSTCC /* Functions only used from tools/env */
306 * Look up any flags directly from the .flags variable and the static list
307 * and convert them to the vartype enum.
309 enum env_flags_vartype
env_flags_get_type(const char *name
)
311 const char *flags_list
= getenv(ENV_FLAGS_VAR
);
312 char flags
[ENV_FLAGS_ATTR_MAX_LEN
+ 1];
314 if (env_flags_lookup(flags_list
, name
, flags
))
315 return env_flags_vartype_string
;
317 if (strlen(flags
) <= ENV_FLAGS_VARTYPE_LOC
)
318 return env_flags_vartype_string
;
320 return env_flags_parse_vartype(flags
);
324 * Look up the access of a variable directly from the .flags var.
326 enum env_flags_varaccess
env_flags_get_varaccess(const char *name
)
328 const char *flags_list
= getenv(ENV_FLAGS_VAR
);
329 char flags
[ENV_FLAGS_ATTR_MAX_LEN
+ 1];
331 if (env_flags_lookup(flags_list
, name
, flags
))
332 return env_flags_varaccess_any
;
334 if (strlen(flags
) <= ENV_FLAGS_VARACCESS_LOC
)
335 return env_flags_varaccess_any
;
337 return env_flags_parse_varaccess(flags
);
341 * Validate that the proposed new value for "name" is valid according to the
342 * defined flags for that variable, if any.
344 int env_flags_validate_type(const char *name
, const char *value
)
346 enum env_flags_vartype type
;
350 type
= env_flags_get_type(name
);
351 if (_env_flags_validate_type(value
, type
) < 0) {
352 printf("## Error: flags type check failure for "
353 "\"%s\" <= \"%s\" (type: %c)\n",
354 name
, value
, env_flags_vartype_rep
[type
]);
361 * Validate that the proposed access to variable "name" is valid according to
362 * the defined flags for that variable, if any.
364 int env_flags_validate_varaccess(const char *name
, int check_mask
)
366 enum env_flags_varaccess access
;
369 access
= env_flags_get_varaccess(name
);
370 access_mask
= env_flags_varaccess_mask
[access
];
372 return (check_mask
& access_mask
) != 0;
376 * Validate the parameters to "env set" directly
378 int env_flags_validate_env_set_params(int argc
, char * const argv
[])
380 if ((argc
>= 3) && argv
[2] != NULL
) {
381 enum env_flags_vartype type
= env_flags_get_type(argv
[1]);
384 * we don't currently check types that need more than
387 if (type
!= env_flags_vartype_string
&& argc
> 3) {
388 printf("## Error: too many parameters for setting "
389 "\"%s\"\n", argv
[1]);
392 return env_flags_validate_type(argv
[1], argv
[2]);
398 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
401 * Parse the flag charachters from the .flags attribute list into the binary
402 * form to be stored in the environment entry->flags field.
404 static int env_parse_flags_to_bin(const char *flags
)
408 binflags
= env_flags_parse_vartype(flags
) & ENV_FLAGS_VARTYPE_BIN_MASK
;
409 binflags
|= env_flags_varaccess_mask
[env_flags_parse_varaccess(flags
)];
415 * Look for possible flags for a newly added variable
416 * This is called specifically when the variable did not exist in the hash
417 * previously, so the blanket update did not find this variable.
419 void env_flags_init(ENTRY
*var_entry
)
421 const char *var_name
= var_entry
->key
;
422 const char *flags_list
= getenv(ENV_FLAGS_VAR
);
423 char flags
[ENV_FLAGS_ATTR_MAX_LEN
+ 1] = "";
426 /* look in the ".flags" and static for a reference to this variable */
427 ret
= env_flags_lookup(flags_list
, var_name
, flags
);
429 /* if any flags were found, set the binary form to the entry */
430 if (!ret
&& strlen(flags
))
431 var_entry
->flags
= env_parse_flags_to_bin(flags
);
435 * Called on each existing env var prior to the blanket update since removing
436 * a flag in the flag list should remove its flags.
438 static int clear_flags(ENTRY
*entry
)
446 * Call for each element in the list that defines flags for a variable
448 static int set_flags(const char *name
, const char *value
)
454 hsearch_r(e
, FIND
, &ep
, &env_htab
, 0);
456 /* does the env variable actually exist? */
458 /* the flag list is empty, so clear the flags */
459 if (value
== NULL
|| strlen(value
) == 0)
462 /* assign the requested flags */
463 ep
->flags
= env_parse_flags_to_bin(value
);
469 static int on_flags(const char *name
, const char *value
, enum env_op op
,
472 /* remove all flags */
473 hwalk_r(&env_htab
, clear_flags
);
475 /* configure any static flags */
476 env_attr_walk(ENV_FLAGS_LIST_STATIC
, set_flags
);
477 /* configure any dynamic flags */
478 env_attr_walk(value
, set_flags
);
482 U_BOOT_ENV_CALLBACK(flags
, on_flags
);
485 * Perform consistency checking before creating, overwriting, or deleting an
486 * environment variable. Called as a callback function by hsearch_r() and
487 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
488 * When (flag & H_FORCE) is set, do not print out any error message and force
489 * overwriting of write-once variables.
492 int env_flags_validate(const ENTRY
*item
, const char *newval
, enum env_op op
,
496 const char *oldval
= NULL
;
498 if (op
!= env_op_create
)
503 /* Default value for NULL to protect string-manipulating functions */
504 newval
= newval
? : "";
506 /* validate the value to match the variable type */
507 if (op
!= env_op_delete
) {
508 enum env_flags_vartype type
= (enum env_flags_vartype
)
509 (ENV_FLAGS_VARTYPE_BIN_MASK
& item
->flags
);
511 if (_env_flags_validate_type(newval
, type
) < 0) {
512 printf("## Error: flags type check failure for "
513 "\"%s\" <= \"%s\" (type: %c)\n",
514 name
, newval
, env_flags_vartype_rep
[type
]);
519 /* check for access permission */
520 #ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
526 if (item
->flags
& ENV_FLAGS_VARACCESS_PREVENT_DELETE
) {
527 printf("## Error: Can't delete \"%s\"\n", name
);
531 case env_op_overwrite
:
532 if (item
->flags
& ENV_FLAGS_VARACCESS_PREVENT_OVERWR
) {
533 printf("## Error: Can't overwrite \"%s\"\n", name
);
535 } else if (item
->flags
&
536 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR
) {
537 const char *defval
= getenv_default(name
);
541 printf("oldval: %s defval: %s\n", oldval
, defval
);
542 if (strcmp(oldval
, defval
) != 0) {
543 printf("## Error: Can't overwrite \"%s\"\n",
550 if (item
->flags
& ENV_FLAGS_VARACCESS_PREVENT_CREATE
) {
551 printf("## Error: Can't create \"%s\"\n", name
);