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))
191 * Based on the declared type enum, validate that the value string complies
194 static int _env_flags_validate_type(const char *value,
195 enum env_flags_vartype type)
198 #ifdef CONFIG_CMD_NET
204 case env_flags_vartype_string:
206 case env_flags_vartype_decimal:
207 skip_num(0, value, &end, -1);
211 case env_flags_vartype_hex:
212 skip_num(1, value, &end, -1);
215 if (value + 2 == end && is_hex_prefix(value))
218 case env_flags_vartype_bool:
219 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
220 value[0] != 'Y' && value[0] != 'T' &&
221 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
222 value[0] != 'N' && value[0] != 'F')
224 if (value[1] != '\0')
227 #ifdef CONFIG_CMD_NET
228 case env_flags_vartype_ipaddr:
230 for (i = 0; i < 4; i++) {
231 skip_num(0, cur, &end, 3);
234 if (i != 3 && *end != '.')
236 if (i == 3 && *end != '\0')
241 case env_flags_vartype_macaddr:
243 for (i = 0; i < 6; i++) {
244 skip_num(1, cur, &end, 2);
247 if (cur + 2 == end && is_hex_prefix(cur))
249 if (i != 5 && *end != ':')
251 if (i == 5 && *end != '\0')
257 case env_flags_vartype_end:
266 * Look for flags in a provided list and failing that the static list
268 static inline int env_flags_lookup(const char *flags_list, const char *name,
277 /* try the env first */
279 ret = env_attr_lookup(flags_list, name, flags);
282 /* if not found in the env, look in the static list */
283 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
288 #ifdef USE_HOSTCC /* Functions only used from tools/env */
290 * Look up any flags directly from the .flags variable and the static list
291 * and convert them to the vartype enum.
293 enum env_flags_vartype env_flags_get_type(const char *name)
295 const char *flags_list = getenv(ENV_FLAGS_VAR);
296 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
298 if (env_flags_lookup(flags_list, name, flags))
299 return env_flags_vartype_string;
301 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
302 return env_flags_vartype_string;
304 return env_flags_parse_vartype(flags);
308 * Look up the access of a variable directly from the .flags var.
310 enum env_flags_varaccess env_flags_get_varaccess(const char *name)
312 const char *flags_list = getenv(ENV_FLAGS_VAR);
313 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
315 if (env_flags_lookup(flags_list, name, flags))
316 return env_flags_varaccess_any;
318 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
319 return env_flags_varaccess_any;
321 return env_flags_parse_varaccess(flags);
325 * Validate that the proposed new value for "name" is valid according to the
326 * defined flags for that variable, if any.
328 int env_flags_validate_type(const char *name, const char *value)
330 enum env_flags_vartype type;
334 type = env_flags_get_type(name);
335 if (_env_flags_validate_type(value, type) < 0) {
336 printf("## Error: flags type check failure for "
337 "\"%s\" <= \"%s\" (type: %c)\n",
338 name, value, env_flags_vartype_rep[type]);
345 * Validate that the proposed access to variable "name" is valid according to
346 * the defined flags for that variable, if any.
348 int env_flags_validate_varaccess(const char *name, int check_mask)
350 enum env_flags_varaccess access;
353 access = env_flags_get_varaccess(name);
354 access_mask = env_flags_varaccess_mask[access];
356 return (check_mask & access_mask) != 0;
360 * Validate the parameters to "env set" directly
362 int env_flags_validate_env_set_params(int argc, char * const argv[])
364 if ((argc >= 3) && argv[2] != NULL) {
365 enum env_flags_vartype type = env_flags_get_type(argv[1]);
368 * we don't currently check types that need more than
371 if (type != env_flags_vartype_string && argc > 3) {
372 printf("## Error: too many parameters for setting "
373 "\"%s\"\n", argv[1]);
376 return env_flags_validate_type(argv[1], argv[2]);
382 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
385 * Parse the flag charachters from the .flags attribute list into the binary
386 * form to be stored in the environment entry->flags field.
388 static int env_parse_flags_to_bin(const char *flags)
392 binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
393 binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
398 static int first_call = 1;
399 static const char *flags_list;
402 * Look for possible flags for a newly added variable
403 * This is called specifically when the variable did not exist in the hash
404 * previously, so the blanket update did not find this variable.
406 void env_flags_init(ENTRY *var_entry)
408 const char *var_name = var_entry->key;
409 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
413 flags_list = getenv(ENV_FLAGS_VAR);
416 /* look in the ".flags" and static for a reference to this variable */
417 ret = env_flags_lookup(flags_list, var_name, flags);
419 /* if any flags were found, set the binary form to the entry */
420 if (!ret && strlen(flags))
421 var_entry->flags = env_parse_flags_to_bin(flags);
425 * Called on each existing env var prior to the blanket update since removing
426 * a flag in the flag list should remove its flags.
428 static int clear_flags(ENTRY *entry)
436 * Call for each element in the list that defines flags for a variable
438 static int set_flags(const char *name, const char *value, void *priv)
444 hsearch_r(e, FIND, &ep, &env_htab, 0);
446 /* does the env variable actually exist? */
448 /* the flag list is empty, so clear the flags */
449 if (value == NULL || strlen(value) == 0)
452 /* assign the requested flags */
453 ep->flags = env_parse_flags_to_bin(value);
459 static int on_flags(const char *name, const char *value, enum env_op op,
462 /* remove all flags */
463 hwalk_r(&env_htab, clear_flags);
465 /* configure any static flags */
466 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL);
467 /* configure any dynamic flags */
468 env_attr_walk(value, set_flags, NULL);
472 U_BOOT_ENV_CALLBACK(flags, on_flags);
475 * Perform consistency checking before creating, overwriting, or deleting an
476 * environment variable. Called as a callback function by hsearch_r() and
477 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
478 * When (flag & H_FORCE) is set, do not print out any error message and force
479 * overwriting of write-once variables.
482 int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
486 const char *oldval = NULL;
488 if (op != env_op_create)
493 /* Default value for NULL to protect string-manipulating functions */
494 newval = newval ? : "";
496 /* validate the value to match the variable type */
497 if (op != env_op_delete) {
498 enum env_flags_vartype type = (enum env_flags_vartype)
499 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
501 if (_env_flags_validate_type(newval, type) < 0) {
502 printf("## Error: flags type check failure for "
503 "\"%s\" <= \"%s\" (type: %c)\n",
504 name, newval, env_flags_vartype_rep[type]);
509 /* check for access permission */
510 #ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
516 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
517 printf("## Error: Can't delete \"%s\"\n", name);
521 case env_op_overwrite:
522 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
523 printf("## Error: Can't overwrite \"%s\"\n", name);
525 } else if (item->flags &
526 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
527 const char *defval = getenv_default(name);
531 printf("oldval: %s defval: %s\n", oldval, defval);
532 if (strcmp(oldval, defval) != 0) {
533 printf("## Error: Can't overwrite \"%s\"\n",
540 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
541 printf("## Error: Can't create \"%s\"\n", name);