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
18 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
21 #include <environment.h>
25 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
27 #define ENV_FLAGS_NET_VARTYPE_REPS ""
30 static const char env_flags_vartype_rep
[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS
;
31 static const char env_flags_varaccess_rep
[] = "aroc";
32 static const int env_flags_varaccess_mask
[] = {
34 ENV_FLAGS_VARACCESS_PREVENT_DELETE
|
35 ENV_FLAGS_VARACCESS_PREVENT_CREATE
|
36 ENV_FLAGS_VARACCESS_PREVENT_OVERWR
,
37 ENV_FLAGS_VARACCESS_PREVENT_DELETE
|
38 ENV_FLAGS_VARACCESS_PREVENT_OVERWR
,
39 ENV_FLAGS_VARACCESS_PREVENT_DELETE
|
40 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR
};
42 #ifdef CONFIG_CMD_ENV_FLAGS
43 static const char * const env_flags_vartype_names
[] = {
53 static const char * const env_flags_varaccess_names
[] = {
61 * Print the whole list of available type flags.
63 void env_flags_print_vartypes(void)
65 enum env_flags_vartype curtype
= (enum env_flags_vartype
)0;
67 while (curtype
!= env_flags_vartype_end
) {
68 printf("\t%c -\t%s\n", env_flags_vartype_rep
[curtype
],
69 env_flags_vartype_names
[curtype
]);
75 * Print the whole list of available access flags.
77 void env_flags_print_varaccess(void)
79 enum env_flags_varaccess curaccess
= (enum env_flags_varaccess
)0;
81 while (curaccess
!= env_flags_varaccess_end
) {
82 printf("\t%c -\t%s\n", env_flags_varaccess_rep
[curaccess
],
83 env_flags_varaccess_names
[curaccess
]);
89 * Return the name of the type.
91 const char *env_flags_get_vartype_name(enum env_flags_vartype type
)
93 return env_flags_vartype_names
[type
];
97 * Return the name of the access.
99 const char *env_flags_get_varaccess_name(enum env_flags_varaccess access
)
101 return env_flags_varaccess_names
[access
];
103 #endif /* CONFIG_CMD_ENV_FLAGS */
106 * Parse the flags string from a .flags attribute list into the vartype enum.
108 enum env_flags_vartype
env_flags_parse_vartype(const char *flags
)
112 if (strlen(flags
) <= ENV_FLAGS_VARTYPE_LOC
)
113 return env_flags_vartype_string
;
115 type
= strchr(env_flags_vartype_rep
,
116 flags
[ENV_FLAGS_VARTYPE_LOC
]);
119 return (enum env_flags_vartype
)
120 (type
- &env_flags_vartype_rep
[0]);
122 printf("## Warning: Unknown environment variable type '%c'\n",
123 flags
[ENV_FLAGS_VARTYPE_LOC
]);
124 return env_flags_vartype_string
;
128 * Parse the flags string from a .flags attribute list into the varaccess enum.
130 enum env_flags_varaccess
env_flags_parse_varaccess(const char *flags
)
134 if (strlen(flags
) <= ENV_FLAGS_VARACCESS_LOC
)
135 return env_flags_varaccess_any
;
137 access
= strchr(env_flags_varaccess_rep
,
138 flags
[ENV_FLAGS_VARACCESS_LOC
]);
141 return (enum env_flags_varaccess
)
142 (access
- &env_flags_varaccess_rep
[0]);
144 printf("## Warning: Unknown environment variable access method '%c'\n",
145 flags
[ENV_FLAGS_VARACCESS_LOC
]);
146 return env_flags_varaccess_any
;
150 * Parse the binary flags from a hash table entry into the varaccess enum.
152 enum env_flags_varaccess
env_flags_parse_varaccess_from_binflags(int binflags
)
156 for (i
= 0; i
< ARRAY_SIZE(env_flags_varaccess_mask
); i
++)
157 if (env_flags_varaccess_mask
[i
] ==
158 (binflags
& ENV_FLAGS_VARACCESS_BIN_MASK
))
159 return (enum env_flags_varaccess
)i
;
161 printf("Warning: Non-standard access flags. (0x%x)\n",
162 binflags
& ENV_FLAGS_VARACCESS_BIN_MASK
);
164 return env_flags_varaccess_any
;
167 static inline int is_hex_prefix(const char *value
)
169 return value
[0] == '0' && (value
[1] == 'x' || value
[1] == 'X');
172 static void skip_num(int hex
, const char *value
, const char **end
,
177 if (hex
&& is_hex_prefix(value
))
180 for (i
= max_digits
; i
!= 0; i
--) {
181 if (hex
&& !isxdigit(*value
))
183 if (!hex
&& !isdigit(*value
))
191 #ifdef CONFIG_CMD_NET
192 int eth_validate_ethaddr_str(const char *addr
)
199 for (i
= 0; i
< 6; i
++) {
200 skip_num(1, cur
, &end
, 2);
203 if (cur
+ 2 == end
&& is_hex_prefix(cur
))
205 if (i
!= 5 && *end
!= ':')
207 if (i
== 5 && *end
!= '\0')
217 * Based on the declared type enum, validate that the value string complies
220 static int _env_flags_validate_type(const char *value
,
221 enum env_flags_vartype type
)
224 #ifdef CONFIG_CMD_NET
230 case env_flags_vartype_string
:
232 case env_flags_vartype_decimal
:
233 skip_num(0, value
, &end
, -1);
237 case env_flags_vartype_hex
:
238 skip_num(1, value
, &end
, -1);
241 if (value
+ 2 == end
&& is_hex_prefix(value
))
244 case env_flags_vartype_bool
:
245 if (value
[0] != '1' && value
[0] != 'y' && value
[0] != 't' &&
246 value
[0] != 'Y' && value
[0] != 'T' &&
247 value
[0] != '0' && value
[0] != 'n' && value
[0] != 'f' &&
248 value
[0] != 'N' && value
[0] != 'F')
250 if (value
[1] != '\0')
253 #ifdef CONFIG_CMD_NET
254 case env_flags_vartype_ipaddr
:
256 for (i
= 0; i
< 4; i
++) {
257 skip_num(0, cur
, &end
, 3);
260 if (i
!= 3 && *end
!= '.')
262 if (i
== 3 && *end
!= '\0')
267 case env_flags_vartype_macaddr
:
268 if (eth_validate_ethaddr_str(value
))
272 case env_flags_vartype_end
:
281 * Look for flags in a provided list and failing that the static list
283 static inline int env_flags_lookup(const char *flags_list
, const char *name
,
292 /* try the env first */
294 ret
= env_attr_lookup(flags_list
, name
, flags
);
297 /* if not found in the env, look in the static list */
298 ret
= env_attr_lookup(ENV_FLAGS_LIST_STATIC
, name
, flags
);
303 #ifdef USE_HOSTCC /* Functions only used from tools/env */
305 * Look up any flags directly from the .flags variable and the static list
306 * and convert them to the vartype enum.
308 enum env_flags_vartype
env_flags_get_type(const char *name
)
310 const char *flags_list
= getenv(ENV_FLAGS_VAR
);
311 char flags
[ENV_FLAGS_ATTR_MAX_LEN
+ 1];
313 if (env_flags_lookup(flags_list
, name
, flags
))
314 return env_flags_vartype_string
;
316 if (strlen(flags
) <= ENV_FLAGS_VARTYPE_LOC
)
317 return env_flags_vartype_string
;
319 return env_flags_parse_vartype(flags
);
323 * Look up the access of a variable directly from the .flags var.
325 enum env_flags_varaccess
env_flags_get_varaccess(const char *name
)
327 const char *flags_list
= getenv(ENV_FLAGS_VAR
);
328 char flags
[ENV_FLAGS_ATTR_MAX_LEN
+ 1];
330 if (env_flags_lookup(flags_list
, name
, flags
))
331 return env_flags_varaccess_any
;
333 if (strlen(flags
) <= ENV_FLAGS_VARACCESS_LOC
)
334 return env_flags_varaccess_any
;
336 return env_flags_parse_varaccess(flags
);
340 * Validate that the proposed new value for "name" is valid according to the
341 * defined flags for that variable, if any.
343 int env_flags_validate_type(const char *name
, const char *value
)
345 enum env_flags_vartype type
;
349 type
= env_flags_get_type(name
);
350 if (_env_flags_validate_type(value
, type
) < 0) {
351 printf("## Error: flags type check failure for "
352 "\"%s\" <= \"%s\" (type: %c)\n",
353 name
, value
, env_flags_vartype_rep
[type
]);
360 * Validate that the proposed access to variable "name" is valid according to
361 * the defined flags for that variable, if any.
363 int env_flags_validate_varaccess(const char *name
, int check_mask
)
365 enum env_flags_varaccess access
;
368 access
= env_flags_get_varaccess(name
);
369 access_mask
= env_flags_varaccess_mask
[access
];
371 return (check_mask
& access_mask
) != 0;
375 * Validate the parameters to "env set" directly
377 int env_flags_validate_env_set_params(char *name
, char * const val
[], int count
)
379 if ((count
>= 1) && val
[0] != NULL
) {
380 enum env_flags_vartype type
= env_flags_get_type(name
);
383 * we don't currently check types that need more than
386 if (type
!= env_flags_vartype_string
&& count
> 1) {
387 printf("## Error: too many parameters for setting \"%s\"\n",
391 return env_flags_validate_type(name
, val
[0]);
397 #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
400 * Parse the flag charachters from the .flags attribute list into the binary
401 * form to be stored in the environment entry->flags field.
403 static int env_parse_flags_to_bin(const char *flags
)
407 binflags
= env_flags_parse_vartype(flags
) & ENV_FLAGS_VARTYPE_BIN_MASK
;
408 binflags
|= env_flags_varaccess_mask
[env_flags_parse_varaccess(flags
)];
413 static int first_call
= 1;
414 static const char *flags_list
;
417 * Look for possible flags for a newly added variable
418 * This is called specifically when the variable did not exist in the hash
419 * previously, so the blanket update did not find this variable.
421 void env_flags_init(ENTRY
*var_entry
)
423 const char *var_name
= var_entry
->key
;
424 char flags
[ENV_FLAGS_ATTR_MAX_LEN
+ 1] = "";
428 flags_list
= getenv(ENV_FLAGS_VAR
);
431 /* look in the ".flags" and static for a reference to this variable */
432 ret
= env_flags_lookup(flags_list
, var_name
, flags
);
434 /* if any flags were found, set the binary form to the entry */
435 if (!ret
&& strlen(flags
))
436 var_entry
->flags
= env_parse_flags_to_bin(flags
);
440 * Called on each existing env var prior to the blanket update since removing
441 * a flag in the flag list should remove its flags.
443 static int clear_flags(ENTRY
*entry
)
451 * Call for each element in the list that defines flags for a variable
453 static int set_flags(const char *name
, const char *value
, void *priv
)
460 hsearch_r(e
, FIND
, &ep
, &env_htab
, 0);
462 /* does the env variable actually exist? */
464 /* the flag list is empty, so clear the flags */
465 if (value
== NULL
|| strlen(value
) == 0)
468 /* assign the requested flags */
469 ep
->flags
= env_parse_flags_to_bin(value
);
475 static int on_flags(const char *name
, const char *value
, enum env_op op
,
478 /* remove all flags */
479 hwalk_r(&env_htab
, clear_flags
);
481 /* configure any static flags */
482 env_attr_walk(ENV_FLAGS_LIST_STATIC
, set_flags
, NULL
);
483 /* configure any dynamic flags */
484 env_attr_walk(value
, set_flags
, NULL
);
488 U_BOOT_ENV_CALLBACK(flags
, on_flags
);
491 * Perform consistency checking before creating, overwriting, or deleting an
492 * environment variable. Called as a callback function by hsearch_r() and
493 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
494 * When (flag & H_FORCE) is set, do not print out any error message and force
495 * overwriting of write-once variables.
498 int env_flags_validate(const ENTRY
*item
, const char *newval
, enum env_op op
,
502 const char *oldval
= NULL
;
504 if (op
!= env_op_create
)
509 /* Default value for NULL to protect string-manipulating functions */
510 newval
= newval
? : "";
512 /* validate the value to match the variable type */
513 if (op
!= env_op_delete
) {
514 enum env_flags_vartype type
= (enum env_flags_vartype
)
515 (ENV_FLAGS_VARTYPE_BIN_MASK
& item
->flags
);
517 if (_env_flags_validate_type(newval
, type
) < 0) {
518 printf("## Error: flags type check failure for "
519 "\"%s\" <= \"%s\" (type: %c)\n",
520 name
, newval
, env_flags_vartype_rep
[type
]);
525 /* check for access permission */
526 #ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
532 if (item
->flags
& ENV_FLAGS_VARACCESS_PREVENT_DELETE
) {
533 printf("## Error: Can't delete \"%s\"\n", name
);
537 case env_op_overwrite
:
538 if (item
->flags
& ENV_FLAGS_VARACCESS_PREVENT_OVERWR
) {
539 printf("## Error: Can't overwrite \"%s\"\n", name
);
541 } else if (item
->flags
&
542 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR
) {
543 const char *defval
= getenv_default(name
);
547 printf("oldval: %s defval: %s\n", oldval
, defval
);
548 if (strcmp(oldval
, defval
) != 0) {
549 printf("## Error: Can't overwrite \"%s\"\n",
556 if (item
->flags
& ENV_FLAGS_VARACCESS_PREVENT_CREATE
) {
557 printf("## Error: Can't create \"%s\"\n", name
);