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 static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
32 static const char env_flags_varaccess_rep[] = "aroc";
33 static const int env_flags_varaccess_mask[] = {
35 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
36 ENV_FLAGS_VARACCESS_PREVENT_CREATE |
37 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
38 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
39 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
40 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
41 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR};
43 #ifdef CONFIG_CMD_ENV_FLAGS
44 static const char * const env_flags_vartype_names[] = {
54 static const char * const env_flags_varaccess_names[] = {
62 * Print the whole list of available type flags.
64 void env_flags_print_vartypes(void)
66 enum env_flags_vartype curtype = (enum env_flags_vartype)0;
68 while (curtype != env_flags_vartype_end) {
69 printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype],
70 env_flags_vartype_names[curtype]);
76 * Print the whole list of available access flags.
78 void env_flags_print_varaccess(void)
80 enum env_flags_varaccess curaccess = (enum env_flags_varaccess)0;
82 while (curaccess != env_flags_varaccess_end) {
83 printf("\t%c -\t%s\n", env_flags_varaccess_rep[curaccess],
84 env_flags_varaccess_names[curaccess]);
90 * Return the name of the type.
92 const char *env_flags_get_vartype_name(enum env_flags_vartype type)
94 return env_flags_vartype_names[type];
98 * Return the name of the access.
100 const char *env_flags_get_varaccess_name(enum env_flags_varaccess access)
102 return env_flags_varaccess_names[access];
104 #endif /* CONFIG_CMD_ENV_FLAGS */
107 * Parse the flags string from a .flags attribute list into the vartype enum.
109 enum env_flags_vartype env_flags_parse_vartype(const char *flags)
113 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
114 return env_flags_vartype_string;
116 type = strchr(env_flags_vartype_rep,
117 flags[ENV_FLAGS_VARTYPE_LOC]);
120 return (enum env_flags_vartype)
121 (type - &env_flags_vartype_rep[0]);
123 printf("## Warning: Unknown environment variable type '%c'\n",
124 flags[ENV_FLAGS_VARTYPE_LOC]);
125 return env_flags_vartype_string;
129 * Parse the flags string from a .flags attribute list into the varaccess enum.
131 enum env_flags_varaccess env_flags_parse_varaccess(const char *flags)
135 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
136 return env_flags_varaccess_any;
138 access = strchr(env_flags_varaccess_rep,
139 flags[ENV_FLAGS_VARACCESS_LOC]);
142 return (enum env_flags_varaccess)
143 (access - &env_flags_varaccess_rep[0]);
145 printf("## Warning: Unknown environment variable access method '%c'\n",
146 flags[ENV_FLAGS_VARACCESS_LOC]);
147 return env_flags_varaccess_any;
151 * Parse the binary flags from a hash table entry into the varaccess enum.
153 enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags)
157 for (i = 0; i < ARRAY_SIZE(env_flags_varaccess_mask); i++)
158 if (env_flags_varaccess_mask[i] ==
159 (binflags & ENV_FLAGS_VARACCESS_BIN_MASK))
160 return (enum env_flags_varaccess)i;
162 printf("Warning: Non-standard access flags. (0x%x)\n",
163 binflags & ENV_FLAGS_VARACCESS_BIN_MASK);
165 return env_flags_varaccess_any;
168 static inline int is_hex_prefix(const char *value)
170 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
173 static void skip_num(int hex, const char *value, const char **end,
178 if (hex && is_hex_prefix(value))
181 for (i = max_digits; i != 0; i--) {
182 if (hex && !isxdigit(*value))
184 if (!hex && !isdigit(*value))
192 #ifdef CONFIG_CMD_NET
193 int eth_validate_ethaddr_str(const char *addr)
200 for (i = 0; i < 6; i++) {
201 skip_num(1, cur, &end, 2);
204 if (cur + 2 == end && is_hex_prefix(cur))
206 if (i != 5 && *end != ':')
208 if (i == 5 && *end != '\0')
218 * Based on the declared type enum, validate that the value string complies
221 static int _env_flags_validate_type(const char *value,
222 enum env_flags_vartype type)
225 #ifdef CONFIG_CMD_NET
231 case env_flags_vartype_string:
233 case env_flags_vartype_decimal:
234 skip_num(0, value, &end, -1);
238 case env_flags_vartype_hex:
239 skip_num(1, value, &end, -1);
242 if (value + 2 == end && is_hex_prefix(value))
245 case env_flags_vartype_bool:
246 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
247 value[0] != 'Y' && value[0] != 'T' &&
248 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
249 value[0] != 'N' && value[0] != 'F')
251 if (value[1] != '\0')
254 #ifdef CONFIG_CMD_NET
255 case env_flags_vartype_ipaddr:
257 for (i = 0; i < 4; i++) {
258 skip_num(0, cur, &end, 3);
261 if (i != 3 && *end != '.')
263 if (i == 3 && *end != '\0')
268 case env_flags_vartype_macaddr:
269 if (eth_validate_ethaddr_str(value))
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 = env_get(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 = env_get(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(char *name, char * const val[], int count)
380 if ((count >= 1) && val[0] != NULL) {
381 enum env_flags_vartype type = env_flags_get_type(name);
384 * we don't currently check types that need more than
387 if (type != env_flags_vartype_string && count > 1) {
388 printf("## Error: too many parameters for setting \"%s\"\n",
392 return env_flags_validate_type(name, val[0]);
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)];
414 static int first_call = 1;
415 static const char *flags_list;
418 * Look for possible flags for a newly added variable
419 * This is called specifically when the variable did not exist in the hash
420 * previously, so the blanket update did not find this variable.
422 void env_flags_init(struct env_entry *var_entry)
424 const char *var_name = var_entry->key;
425 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
429 flags_list = env_get(ENV_FLAGS_VAR);
432 /* look in the ".flags" and static for a reference to this variable */
433 ret = env_flags_lookup(flags_list, var_name, flags);
435 /* if any flags were found, set the binary form to the entry */
436 if (!ret && strlen(flags))
437 var_entry->flags = env_parse_flags_to_bin(flags);
441 * Called on each existing env var prior to the blanket update since removing
442 * a flag in the flag list should remove its flags.
444 static int clear_flags(struct env_entry *entry)
452 * Call for each element in the list that defines flags for a variable
454 static int set_flags(const char *name, const char *value, void *priv)
456 struct env_entry e, *ep;
461 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
463 /* does the env variable actually exist? */
465 /* the flag list is empty, so clear the flags */
466 if (value == NULL || strlen(value) == 0)
469 /* assign the requested flags */
470 ep->flags = env_parse_flags_to_bin(value);
476 static int on_flags(const char *name, const char *value, enum env_op op,
479 /* remove all flags */
480 hwalk_r(&env_htab, clear_flags);
482 /* configure any static flags */
483 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL);
484 /* configure any dynamic flags */
485 env_attr_walk(value, set_flags, NULL);
489 U_BOOT_ENV_CALLBACK(flags, on_flags);
492 * Perform consistency checking before creating, overwriting, or deleting an
493 * environment variable. Called as a callback function by hsearch_r() and
494 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
495 * When (flag & H_FORCE) is set, do not print out any error message and force
496 * overwriting of write-once variables.
499 int env_flags_validate(const struct env_entry *item, const char *newval,
500 enum env_op op, int flag)
503 const char *oldval = NULL;
505 if (op != env_op_create)
510 /* Default value for NULL to protect string-manipulating functions */
511 newval = newval ? : "";
513 /* validate the value to match the variable type */
514 if (op != env_op_delete) {
515 enum env_flags_vartype type = (enum env_flags_vartype)
516 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
518 if (_env_flags_validate_type(newval, type) < 0) {
519 printf("## Error: flags type check failure for "
520 "\"%s\" <= \"%s\" (type: %c)\n",
521 name, newval, env_flags_vartype_rep[type]);
526 /* check for access permission */
527 #ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
533 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
534 printf("## Error: Can't delete \"%s\"\n", name);
538 case env_op_overwrite:
539 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
540 printf("## Error: Can't overwrite \"%s\"\n", name);
542 } else if (item->flags &
543 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
544 const char *defval = env_get_default(name);
548 printf("oldval: %s defval: %s\n", oldval, defval);
549 if (strcmp(oldval, defval) != 0) {
550 printf("## Error: Can't overwrite \"%s\"\n",
557 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
558 printf("## Error: Can't create \"%s\"\n", name);