1 // SPDX-License-Identifier: GPL-2.0+
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
8 #include <linux/string.h>
9 #include <linux/ctype.h>
11 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
14 #include "fw_env_private.h"
17 #include <env_flags.h>
18 #define env_get fw_getenv
19 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
22 #include <env_internal.h>
26 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
28 #define ENV_FLAGS_NET_VARTYPE_REPS ""
31 #ifdef CONFIG_ENV_WRITEABLE_LIST
32 #define ENV_FLAGS_WRITEABLE_VARACCESS_REPS "w"
34 #define ENV_FLAGS_WRITEABLE_VARACCESS_REPS ""
37 static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
38 static const char env_flags_varaccess_rep[] =
39 "aroc" ENV_FLAGS_WRITEABLE_VARACCESS_REPS;
40 static const int env_flags_varaccess_mask[] = {
42 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
43 ENV_FLAGS_VARACCESS_PREVENT_CREATE |
44 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
45 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
46 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
47 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
48 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR,
49 #ifdef CONFIG_ENV_WRITEABLE_LIST
50 ENV_FLAGS_VARACCESS_WRITEABLE,
54 #ifdef CONFIG_CMD_ENV_FLAGS
55 static const char * const env_flags_vartype_names[] = {
65 static const char * const env_flags_varaccess_names[] = {
70 #ifdef CONFIG_ENV_WRITEABLE_LIST
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)
147 enum env_flags_varaccess va_default = env_flags_varaccess_any;
148 enum env_flags_varaccess va;
151 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
154 access = strchr(env_flags_varaccess_rep,
155 flags[ENV_FLAGS_VARACCESS_LOC]);
157 if (access != NULL) {
158 va = (enum env_flags_varaccess)
159 (access - &env_flags_varaccess_rep[0]);
163 printf("## Warning: Unknown environment variable access method '%c'\n",
164 flags[ENV_FLAGS_VARACCESS_LOC]);
169 * Parse the binary flags from a hash table entry into the varaccess enum.
171 enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags)
173 enum env_flags_varaccess va_default = env_flags_varaccess_any;
174 enum env_flags_varaccess va;
177 for (i = 0; i < ARRAY_SIZE(env_flags_varaccess_mask); i++)
178 if (env_flags_varaccess_mask[i] ==
179 (binflags & ENV_FLAGS_VARACCESS_BIN_MASK)) {
180 va = (enum env_flags_varaccess)i;
184 printf("Warning: Non-standard access flags. (0x%x)\n",
185 binflags & ENV_FLAGS_VARACCESS_BIN_MASK);
190 static inline int is_hex_prefix(const char *value)
192 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
195 static void skip_num(int hex, const char *value, const char **end,
200 if (hex && is_hex_prefix(value))
203 for (i = max_digits; i != 0; i--) {
204 if (hex && !isxdigit(*value))
206 if (!hex && !isdigit(*value))
214 #ifdef CONFIG_CMD_NET
215 int eth_validate_ethaddr_str(const char *addr)
222 for (i = 0; i < 6; i++) {
223 skip_num(1, cur, &end, 2);
226 if (cur + 2 == end && is_hex_prefix(cur))
228 if (i != 5 && *end != ':')
230 if (i == 5 && *end != '\0')
240 * Based on the declared type enum, validate that the value string complies
243 static int _env_flags_validate_type(const char *value,
244 enum env_flags_vartype type)
247 #ifdef CONFIG_CMD_NET
253 case env_flags_vartype_string:
255 case env_flags_vartype_decimal:
256 skip_num(0, value, &end, -1);
260 case env_flags_vartype_hex:
261 skip_num(1, value, &end, -1);
264 if (value + 2 == end && is_hex_prefix(value))
267 case env_flags_vartype_bool:
268 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
269 value[0] != 'Y' && value[0] != 'T' &&
270 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
271 value[0] != 'N' && value[0] != 'F')
273 if (value[1] != '\0')
276 #ifdef CONFIG_CMD_NET
277 case env_flags_vartype_ipaddr:
279 for (i = 0; i < 4; i++) {
280 skip_num(0, cur, &end, 3);
283 if (i != 3 && *end != '.')
285 if (i == 3 && *end != '\0')
290 case env_flags_vartype_macaddr:
291 if (eth_validate_ethaddr_str(value))
295 case env_flags_vartype_end:
304 * Look for flags in a provided list and failing that the static list
306 static inline int env_flags_lookup(const char *flags_list, const char *name,
315 /* try the env first */
317 ret = env_attr_lookup(flags_list, name, flags);
320 /* if not found in the env, look in the static list */
321 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
326 #ifdef USE_HOSTCC /* Functions only used from tools/env */
328 * Look up any flags directly from the .flags variable and the static list
329 * and convert them to the vartype enum.
331 enum env_flags_vartype env_flags_get_type(const char *name)
333 const char *flags_list = env_get(ENV_FLAGS_VAR);
334 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
336 if (env_flags_lookup(flags_list, name, flags))
337 return env_flags_vartype_string;
339 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
340 return env_flags_vartype_string;
342 return env_flags_parse_vartype(flags);
346 * Look up the access of a variable directly from the .flags var.
348 enum env_flags_varaccess env_flags_get_varaccess(const char *name)
350 const char *flags_list = env_get(ENV_FLAGS_VAR);
351 enum env_flags_varaccess va_default = env_flags_varaccess_any;
352 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
354 if (env_flags_lookup(flags_list, name, flags))
357 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
360 return env_flags_parse_varaccess(flags);
364 * Validate that the proposed new value for "name" is valid according to the
365 * defined flags for that variable, if any.
367 int env_flags_validate_type(const char *name, const char *value)
369 enum env_flags_vartype type;
373 type = env_flags_get_type(name);
374 if (_env_flags_validate_type(value, type) < 0) {
375 printf("## Error: flags type check failure for "
376 "\"%s\" <= \"%s\" (type: %c)\n",
377 name, value, env_flags_vartype_rep[type]);
384 * Validate that the proposed access to variable "name" is valid according to
385 * the defined flags for that variable, if any.
387 int env_flags_validate_varaccess(const char *name, int check_mask)
389 enum env_flags_varaccess access;
392 access = env_flags_get_varaccess(name);
393 access_mask = env_flags_varaccess_mask[access];
395 return (check_mask & access_mask) != 0;
399 * Validate the parameters to "env set" directly
401 int env_flags_validate_env_set_params(char *name, char * const val[], int count)
403 if ((count >= 1) && val[0] != NULL) {
404 enum env_flags_vartype type = env_flags_get_type(name);
407 * we don't currently check types that need more than
410 if (type != env_flags_vartype_string && count > 1) {
411 printf("## Error: too many parameters for setting \"%s\"\n",
415 return env_flags_validate_type(name, val[0]);
421 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
424 * Parse the flag charachters from the .flags attribute list into the binary
425 * form to be stored in the environment entry->flags field.
427 static int env_parse_flags_to_bin(const char *flags)
431 binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
432 binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
437 static int first_call = 1;
438 static const char *flags_list;
441 * Look for possible flags for a newly added variable
442 * This is called specifically when the variable did not exist in the hash
443 * previously, so the blanket update did not find this variable.
445 void env_flags_init(struct env_entry *var_entry)
447 const char *var_name = var_entry->key;
448 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
452 #ifdef CONFIG_ENV_WRITEABLE_LIST
453 flags_list = ENV_FLAGS_LIST_STATIC;
455 flags_list = env_get(ENV_FLAGS_VAR);
459 /* look in the ".flags" and static for a reference to this variable */
460 ret = env_flags_lookup(flags_list, var_name, flags);
462 /* if any flags were found, set the binary form to the entry */
463 if (!ret && strlen(flags))
464 var_entry->flags = env_parse_flags_to_bin(flags);
468 * Called on each existing env var prior to the blanket update since removing
469 * a flag in the flag list should remove its flags.
471 static int clear_flags(struct env_entry *entry)
479 * Call for each element in the list that defines flags for a variable
481 static int set_flags(const char *name, const char *value, void *priv)
483 struct env_entry e, *ep;
487 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
489 /* does the env variable actually exist? */
491 /* the flag list is empty, so clear the flags */
492 if (value == NULL || strlen(value) == 0)
495 /* assign the requested flags */
496 ep->flags = env_parse_flags_to_bin(value);
502 static int on_flags(const char *name, const char *value, enum env_op op,
505 /* remove all flags */
506 hwalk_r(&env_htab, clear_flags);
508 /* configure any static flags */
509 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL);
510 /* configure any dynamic flags */
511 env_attr_walk(value, set_flags, NULL);
515 U_BOOT_ENV_CALLBACK(flags, on_flags);
518 * Perform consistency checking before creating, overwriting, or deleting an
519 * environment variable. Called as a callback function by hsearch_r() and
520 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
521 * When (flag & H_FORCE) is set, do not print out any error message and force
522 * overwriting of write-once variables.
525 int env_flags_validate(const struct env_entry *item, const char *newval,
526 enum env_op op, int flag)
529 const char *oldval = NULL;
531 if (op != env_op_create)
536 /* Default value for NULL to protect string-manipulating functions */
537 newval = newval ? : "";
539 /* validate the value to match the variable type */
540 if (op != env_op_delete) {
541 enum env_flags_vartype type = (enum env_flags_vartype)
542 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
544 if (_env_flags_validate_type(newval, type) < 0) {
545 printf("## Error: flags type check failure for "
546 "\"%s\" <= \"%s\" (type: %c)\n",
547 name, newval, env_flags_vartype_rep[type]);
552 /* check for access permission */
553 #ifdef CONFIG_ENV_WRITEABLE_LIST
554 if (flag & H_DEFAULT)
555 return 0; /* Default env is always OK */
558 * External writeable variables can be overwritten by external env,
559 * anything else can not be overwritten by external env.
561 if ((flag & H_EXTERNAL) &&
562 !(item->flags & ENV_FLAGS_VARACCESS_WRITEABLE))
566 if (flag & H_FORCE) {
567 #ifdef CONFIG_ENV_ACCESS_IGNORE_FORCE
568 printf("## Error: Can't force access to \"%s\"\n", name);
575 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
576 printf("## Error: Can't delete \"%s\"\n", name);
580 case env_op_overwrite:
581 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
582 printf("## Error: Can't overwrite \"%s\"\n", name);
584 } else if (item->flags &
585 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
586 const char *defval = env_get_default(name);
590 printf("oldval: %s defval: %s\n", oldval, defval);
591 if (strcmp(oldval, defval) != 0) {
592 printf("## Error: Can't overwrite \"%s\"\n",
599 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
600 printf("## Error: Can't create \"%s\"\n", name);