3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/string.h>
9 #include <linux/ctype.h>
11 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
16 #include <env_flags.h>
17 #define getenv fw_getenv
20 #include <environment.h>
24 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
26 #define ENV_FLAGS_NET_VARTYPE_REPS ""
29 static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
30 static const char env_flags_varaccess_rep[] = "aroc";
31 static const int env_flags_varaccess_mask[] = {
33 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
34 ENV_FLAGS_VARACCESS_PREVENT_CREATE |
35 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
36 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
37 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
38 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
39 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR};
41 #ifdef CONFIG_CMD_ENV_FLAGS
42 static const char * const env_flags_vartype_names[] = {
52 static const char * const env_flags_varaccess_names[] = {
60 * Print the whole list of available type flags.
62 void env_flags_print_vartypes(void)
64 enum env_flags_vartype curtype = (enum env_flags_vartype)0;
66 while (curtype != env_flags_vartype_end) {
67 printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype],
68 env_flags_vartype_names[curtype]);
74 * Print the whole list of available access flags.
76 void env_flags_print_varaccess(void)
78 enum env_flags_varaccess curaccess = (enum env_flags_varaccess)0;
80 while (curaccess != env_flags_varaccess_end) {
81 printf("\t%c -\t%s\n", env_flags_varaccess_rep[curaccess],
82 env_flags_varaccess_names[curaccess]);
88 * Return the name of the type.
90 const char *env_flags_get_vartype_name(enum env_flags_vartype type)
92 return env_flags_vartype_names[type];
96 * Return the name of the access.
98 const char *env_flags_get_varaccess_name(enum env_flags_varaccess access)
100 return env_flags_varaccess_names[access];
102 #endif /* CONFIG_CMD_ENV_FLAGS */
105 * Parse the flags string from a .flags attribute list into the vartype enum.
107 enum env_flags_vartype env_flags_parse_vartype(const char *flags)
111 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
112 return env_flags_vartype_string;
114 type = strchr(env_flags_vartype_rep,
115 flags[ENV_FLAGS_VARTYPE_LOC]);
118 return (enum env_flags_vartype)
119 (type - &env_flags_vartype_rep[0]);
121 printf("## Warning: Unknown environment variable type '%c'\n",
122 flags[ENV_FLAGS_VARTYPE_LOC]);
123 return env_flags_vartype_string;
127 * Parse the flags string from a .flags attribute list into the varaccess enum.
129 enum env_flags_varaccess env_flags_parse_varaccess(const char *flags)
133 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
134 return env_flags_varaccess_any;
136 access = strchr(env_flags_varaccess_rep,
137 flags[ENV_FLAGS_VARACCESS_LOC]);
140 return (enum env_flags_varaccess)
141 (access - &env_flags_varaccess_rep[0]);
143 printf("## Warning: Unknown environment variable access method '%c'\n",
144 flags[ENV_FLAGS_VARACCESS_LOC]);
145 return env_flags_varaccess_any;
149 * Parse the binary flags from a hash table entry into the varaccess enum.
151 enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags)
155 for (i = 0; i < sizeof(env_flags_varaccess_mask); i++)
156 if (env_flags_varaccess_mask[i] ==
157 (binflags & ENV_FLAGS_VARACCESS_BIN_MASK))
158 return (enum env_flags_varaccess)i;
160 printf("Warning: Non-standard access flags. (0x%x)\n",
161 binflags & ENV_FLAGS_VARACCESS_BIN_MASK);
163 return env_flags_varaccess_any;
166 static inline int is_hex_prefix(const char *value)
168 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
171 static void skip_num(int hex, const char *value, const char **end,
176 if (hex && is_hex_prefix(value))
179 for (i = max_digits; i != 0; i--) {
180 if (hex && !isxdigit(*value))
182 if (!hex && !isdigit(*value))
190 #ifdef CONFIG_CMD_NET
191 int eth_validate_ethaddr_str(const char *addr)
198 for (i = 0; i < 6; i++) {
199 skip_num(1, cur, &end, 2);
202 if (cur + 2 == end && is_hex_prefix(cur))
204 if (i != 5 && *end != ':')
206 if (i == 5 && *end != '\0')
216 * Based on the declared type enum, validate that the value string complies
219 static int _env_flags_validate_type(const char *value,
220 enum env_flags_vartype type)
223 #ifdef CONFIG_CMD_NET
229 case env_flags_vartype_string:
231 case env_flags_vartype_decimal:
232 skip_num(0, value, &end, -1);
236 case env_flags_vartype_hex:
237 skip_num(1, value, &end, -1);
240 if (value + 2 == end && is_hex_prefix(value))
243 case env_flags_vartype_bool:
244 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
245 value[0] != 'Y' && value[0] != 'T' &&
246 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
247 value[0] != 'N' && value[0] != 'F')
249 if (value[1] != '\0')
252 #ifdef CONFIG_CMD_NET
253 case env_flags_vartype_ipaddr:
255 for (i = 0; i < 4; i++) {
256 skip_num(0, cur, &end, 3);
259 if (i != 3 && *end != '.')
261 if (i == 3 && *end != '\0')
266 case env_flags_vartype_macaddr:
267 if (eth_validate_ethaddr_str(value))
271 case env_flags_vartype_end:
280 * Look for flags in a provided list and failing that the static list
282 static inline int env_flags_lookup(const char *flags_list, const char *name,
291 /* try the env first */
293 ret = env_attr_lookup(flags_list, name, flags);
296 /* if not found in the env, look in the static list */
297 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
302 #ifdef USE_HOSTCC /* Functions only used from tools/env */
304 * Look up any flags directly from the .flags variable and the static list
305 * and convert them to the vartype enum.
307 enum env_flags_vartype env_flags_get_type(const char *name)
309 const char *flags_list = getenv(ENV_FLAGS_VAR);
310 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
312 if (env_flags_lookup(flags_list, name, flags))
313 return env_flags_vartype_string;
315 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
316 return env_flags_vartype_string;
318 return env_flags_parse_vartype(flags);
322 * Look up the access of a variable directly from the .flags var.
324 enum env_flags_varaccess env_flags_get_varaccess(const char *name)
326 const char *flags_list = getenv(ENV_FLAGS_VAR);
327 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
329 if (env_flags_lookup(flags_list, name, flags))
330 return env_flags_varaccess_any;
332 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
333 return env_flags_varaccess_any;
335 return env_flags_parse_varaccess(flags);
339 * Validate that the proposed new value for "name" is valid according to the
340 * defined flags for that variable, if any.
342 int env_flags_validate_type(const char *name, const char *value)
344 enum env_flags_vartype type;
348 type = env_flags_get_type(name);
349 if (_env_flags_validate_type(value, type) < 0) {
350 printf("## Error: flags type check failure for "
351 "\"%s\" <= \"%s\" (type: %c)\n",
352 name, value, env_flags_vartype_rep[type]);
359 * Validate that the proposed access to variable "name" is valid according to
360 * the defined flags for that variable, if any.
362 int env_flags_validate_varaccess(const char *name, int check_mask)
364 enum env_flags_varaccess access;
367 access = env_flags_get_varaccess(name);
368 access_mask = env_flags_varaccess_mask[access];
370 return (check_mask & access_mask) != 0;
374 * Validate the parameters to "env set" directly
376 int env_flags_validate_env_set_params(int argc, char * const argv[])
378 if ((argc >= 3) && argv[2] != NULL) {
379 enum env_flags_vartype type = env_flags_get_type(argv[1]);
382 * we don't currently check types that need more than
385 if (type != env_flags_vartype_string && argc > 3) {
386 printf("## Error: too many parameters for setting "
387 "\"%s\"\n", argv[1]);
390 return env_flags_validate_type(argv[1], argv[2]);
396 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
399 * Parse the flag charachters from the .flags attribute list into the binary
400 * form to be stored in the environment entry->flags field.
402 static int env_parse_flags_to_bin(const char *flags)
406 binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
407 binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
412 static int first_call = 1;
413 static const char *flags_list;
416 * Look for possible flags for a newly added variable
417 * This is called specifically when the variable did not exist in the hash
418 * previously, so the blanket update did not find this variable.
420 void env_flags_init(ENTRY *var_entry)
422 const char *var_name = var_entry->key;
423 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
427 flags_list = getenv(ENV_FLAGS_VAR);
430 /* look in the ".flags" and static for a reference to this variable */
431 ret = env_flags_lookup(flags_list, var_name, flags);
433 /* if any flags were found, set the binary form to the entry */
434 if (!ret && strlen(flags))
435 var_entry->flags = env_parse_flags_to_bin(flags);
439 * Called on each existing env var prior to the blanket update since removing
440 * a flag in the flag list should remove its flags.
442 static int clear_flags(ENTRY *entry)
450 * Call for each element in the list that defines flags for a variable
452 static int set_flags(const char *name, const char *value, void *priv)
458 hsearch_r(e, FIND, &ep, &env_htab, 0);
460 /* does the env variable actually exist? */
462 /* the flag list is empty, so clear the flags */
463 if (value == NULL || strlen(value) == 0)
466 /* assign the requested flags */
467 ep->flags = env_parse_flags_to_bin(value);
473 static int on_flags(const char *name, const char *value, enum env_op op,
476 /* remove all flags */
477 hwalk_r(&env_htab, clear_flags);
479 /* configure any static flags */
480 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL);
481 /* configure any dynamic flags */
482 env_attr_walk(value, set_flags, NULL);
486 U_BOOT_ENV_CALLBACK(flags, on_flags);
489 * Perform consistency checking before creating, overwriting, or deleting an
490 * environment variable. Called as a callback function by hsearch_r() and
491 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
492 * When (flag & H_FORCE) is set, do not print out any error message and force
493 * overwriting of write-once variables.
496 int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
500 const char *oldval = NULL;
502 if (op != env_op_create)
507 /* Default value for NULL to protect string-manipulating functions */
508 newval = newval ? : "";
510 /* validate the value to match the variable type */
511 if (op != env_op_delete) {
512 enum env_flags_vartype type = (enum env_flags_vartype)
513 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
515 if (_env_flags_validate_type(newval, type) < 0) {
516 printf("## Error: flags type check failure for "
517 "\"%s\" <= \"%s\" (type: %c)\n",
518 name, newval, env_flags_vartype_rep[type]);
523 /* check for access permission */
524 #ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
530 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
531 printf("## Error: Can't delete \"%s\"\n", name);
535 case env_op_overwrite:
536 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
537 printf("## Error: Can't overwrite \"%s\"\n", name);
539 } else if (item->flags &
540 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
541 const char *defval = getenv_default(name);
545 printf("oldval: %s defval: %s\n", oldval, defval);
546 if (strcmp(oldval, defval) != 0) {
547 printf("## Error: Can't overwrite \"%s\"\n",
554 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
555 printf("## Error: Can't create \"%s\"\n", name);