blob: 5189f5b2ddd54e0245a4455035a220383c1e3052 [file] [log] [blame]
Joe Hershberger25980902012-12-11 22:16:31 -06001/*
2 * (C) Copyright 2012
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Joe Hershberger25980902012-12-11 22:16:31 -06006 */
7
8#include <linux/string.h>
9#include <linux/ctype.h>
10
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060011#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
12#include <stdint.h>
13#include <stdio.h>
14#include "fw_env.h"
15#include <env_attr.h>
16#include <env_flags.h>
17#define getenv fw_getenv
18#else
Joe Hershberger25980902012-12-11 22:16:31 -060019#include <common.h>
20#include <environment.h>
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060021#endif
Joe Hershberger25980902012-12-11 22:16:31 -060022
23#ifdef CONFIG_CMD_NET
24#define ENV_FLAGS_NET_VARTYPE_REPS "im"
25#else
26#define ENV_FLAGS_NET_VARTYPE_REPS ""
27#endif
28
29static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
Joe Hershberger267541f2012-12-11 22:16:34 -060030static const char env_flags_varaccess_rep[] = "aroc";
31static const int env_flags_varaccess_mask[] = {
32 0,
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};
40
Joe Hershbergerfffad712012-12-11 22:16:33 -060041#ifdef CONFIG_CMD_ENV_FLAGS
42static const char * const env_flags_vartype_names[] = {
43 "string",
44 "decimal",
45 "hexadecimal",
46 "boolean",
47#ifdef CONFIG_CMD_NET
48 "IP address",
49 "MAC address",
50#endif
51};
Joe Hershberger267541f2012-12-11 22:16:34 -060052static const char * const env_flags_varaccess_names[] = {
53 "any",
54 "read-only",
55 "write-once",
56 "change-default",
57};
Joe Hershbergerfffad712012-12-11 22:16:33 -060058
59/*
60 * Print the whole list of available type flags.
61 */
62void env_flags_print_vartypes(void)
63{
64 enum env_flags_vartype curtype = (enum env_flags_vartype)0;
65
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]);
69 curtype++;
70 }
71}
72
73/*
Joe Hershberger267541f2012-12-11 22:16:34 -060074 * Print the whole list of available access flags.
75 */
76void env_flags_print_varaccess(void)
77{
78 enum env_flags_varaccess curaccess = (enum env_flags_varaccess)0;
79
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]);
83 curaccess++;
84 }
85}
86
87/*
Joe Hershbergerfffad712012-12-11 22:16:33 -060088 * Return the name of the type.
89 */
90const char *env_flags_get_vartype_name(enum env_flags_vartype type)
91{
92 return env_flags_vartype_names[type];
93}
Joe Hershberger267541f2012-12-11 22:16:34 -060094
95/*
96 * Return the name of the access.
97 */
98const char *env_flags_get_varaccess_name(enum env_flags_varaccess access)
99{
100 return env_flags_varaccess_names[access];
101}
Joe Hershbergerfffad712012-12-11 22:16:33 -0600102#endif /* CONFIG_CMD_ENV_FLAGS */
Joe Hershberger25980902012-12-11 22:16:31 -0600103
104/*
105 * Parse the flags string from a .flags attribute list into the vartype enum.
106 */
107enum env_flags_vartype env_flags_parse_vartype(const char *flags)
108{
109 char *type;
110
111 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
112 return env_flags_vartype_string;
113
114 type = strchr(env_flags_vartype_rep,
115 flags[ENV_FLAGS_VARTYPE_LOC]);
116
117 if (type != NULL)
118 return (enum env_flags_vartype)
119 (type - &env_flags_vartype_rep[0]);
120
121 printf("## Warning: Unknown environment variable type '%c'\n",
122 flags[ENV_FLAGS_VARTYPE_LOC]);
123 return env_flags_vartype_string;
124}
125
Joe Hershberger267541f2012-12-11 22:16:34 -0600126/*
127 * Parse the flags string from a .flags attribute list into the varaccess enum.
128 */
129enum env_flags_varaccess env_flags_parse_varaccess(const char *flags)
130{
131 char *access;
132
133 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
134 return env_flags_varaccess_any;
135
136 access = strchr(env_flags_varaccess_rep,
137 flags[ENV_FLAGS_VARACCESS_LOC]);
138
139 if (access != NULL)
140 return (enum env_flags_varaccess)
141 (access - &env_flags_varaccess_rep[0]);
142
143 printf("## Warning: Unknown environment variable access method '%c'\n",
144 flags[ENV_FLAGS_VARACCESS_LOC]);
145 return env_flags_varaccess_any;
146}
147
148/*
149 * Parse the binary flags from a hash table entry into the varaccess enum.
150 */
151enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags)
152{
153 int i;
154
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;
159
160 printf("Warning: Non-standard access flags. (0x%x)\n",
161 binflags & ENV_FLAGS_VARACCESS_BIN_MASK);
162
163 return env_flags_varaccess_any;
164}
165
Joe Hershberger25980902012-12-11 22:16:31 -0600166static inline int is_hex_prefix(const char *value)
167{
168 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
169}
170
171static void skip_num(int hex, const char *value, const char **end,
172 int max_digits)
173{
174 int i;
175
176 if (hex && is_hex_prefix(value))
177 value += 2;
178
179 for (i = max_digits; i != 0; i--) {
180 if (hex && !isxdigit(*value))
181 break;
182 if (!hex && !isdigit(*value))
183 break;
184 value++;
185 }
186 if (end != NULL)
187 *end = value;
188}
189
190/*
191 * Based on the declared type enum, validate that the value string complies
192 * with that format
193 */
194static int _env_flags_validate_type(const char *value,
195 enum env_flags_vartype type)
196{
197 const char *end;
198#ifdef CONFIG_CMD_NET
199 const char *cur;
200 int i;
201#endif
202
203 switch (type) {
204 case env_flags_vartype_string:
205 break;
206 case env_flags_vartype_decimal:
207 skip_num(0, value, &end, -1);
208 if (*end != '\0')
209 return -1;
210 break;
211 case env_flags_vartype_hex:
212 skip_num(1, value, &end, -1);
213 if (*end != '\0')
214 return -1;
215 if (value + 2 == end && is_hex_prefix(value))
216 return -1;
217 break;
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')
223 return -1;
224 if (value[1] != '\0')
225 return -1;
226 break;
227#ifdef CONFIG_CMD_NET
228 case env_flags_vartype_ipaddr:
229 cur = value;
230 for (i = 0; i < 4; i++) {
231 skip_num(0, cur, &end, 3);
232 if (cur == end)
233 return -1;
234 if (i != 3 && *end != '.')
235 return -1;
236 if (i == 3 && *end != '\0')
237 return -1;
238 cur = end + 1;
239 }
240 break;
241 case env_flags_vartype_macaddr:
242 cur = value;
243 for (i = 0; i < 6; i++) {
244 skip_num(1, cur, &end, 2);
245 if (cur == end)
246 return -1;
247 if (cur + 2 == end && is_hex_prefix(cur))
248 return -1;
249 if (i != 5 && *end != ':')
250 return -1;
251 if (i == 5 && *end != '\0')
252 return -1;
253 cur = end + 1;
254 }
255 break;
256#endif
257 case env_flags_vartype_end:
258 return -1;
259 }
260
261 /* OK */
262 return 0;
263}
264
265/*
266 * Look for flags in a provided list and failing that the static list
267 */
268static inline int env_flags_lookup(const char *flags_list, const char *name,
269 char *flags)
270{
271 int ret = 1;
272
273 if (!flags)
274 /* bad parameter */
275 return -1;
276
277 /* try the env first */
278 if (flags_list)
279 ret = env_attr_lookup(flags_list, name, flags);
280
281 if (ret != 0)
282 /* if not found in the env, look in the static list */
283 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
284
285 return ret;
286}
287
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600288#ifdef USE_HOSTCC /* Functions only used from tools/env */
289/*
290 * Look up any flags directly from the .flags variable and the static list
291 * and convert them to the vartype enum.
292 */
293enum env_flags_vartype env_flags_get_type(const char *name)
294{
295 const char *flags_list = getenv(ENV_FLAGS_VAR);
296 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
297
298 if (env_flags_lookup(flags_list, name, flags))
299 return env_flags_vartype_string;
300
301 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
302 return env_flags_vartype_string;
303
304 return env_flags_parse_vartype(flags);
305}
306
307/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600308 * Look up the access of a variable directly from the .flags var.
309 */
310enum env_flags_varaccess env_flags_get_varaccess(const char *name)
311{
312 const char *flags_list = getenv(ENV_FLAGS_VAR);
313 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
314
315 if (env_flags_lookup(flags_list, name, flags))
316 return env_flags_varaccess_any;
317
318 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
319 return env_flags_varaccess_any;
320
321 return env_flags_parse_varaccess(flags);
322}
323
324/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600325 * Validate that the proposed new value for "name" is valid according to the
326 * defined flags for that variable, if any.
327 */
328int env_flags_validate_type(const char *name, const char *value)
329{
330 enum env_flags_vartype type;
331
332 if (value == NULL)
333 return 0;
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]);
339 return -1;
340 }
341 return 0;
342}
343
344/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600345 * Validate that the proposed access to variable "name" is valid according to
346 * the defined flags for that variable, if any.
347 */
348int env_flags_validate_varaccess(const char *name, int check_mask)
349{
350 enum env_flags_varaccess access;
351 int access_mask;
352
353 access = env_flags_get_varaccess(name);
354 access_mask = env_flags_varaccess_mask[access];
355
356 return (check_mask & access_mask) != 0;
357}
358
359/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600360 * Validate the parameters to "env set" directly
361 */
362int env_flags_validate_env_set_params(int argc, char * const argv[])
363{
364 if ((argc >= 3) && argv[2] != NULL) {
365 enum env_flags_vartype type = env_flags_get_type(argv[1]);
366
367 /*
368 * we don't currently check types that need more than
369 * one argument
370 */
371 if (type != env_flags_vartype_string && argc > 3) {
372 printf("## Error: too many parameters for setting "
373 "\"%s\"\n", argv[1]);
374 return -1;
375 }
376 return env_flags_validate_type(argv[1], argv[2]);
377 }
378 /* ok */
379 return 0;
380}
381
382#else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
383
Joe Hershberger25980902012-12-11 22:16:31 -0600384/*
385 * Parse the flag charachters from the .flags attribute list into the binary
386 * form to be stored in the environment entry->flags field.
387 */
388static int env_parse_flags_to_bin(const char *flags)
389{
Joe Hershberger267541f2012-12-11 22:16:34 -0600390 int binflags;
391
392 binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
393 binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
394
395 return binflags;
Joe Hershberger25980902012-12-11 22:16:31 -0600396}
397
Heiko Schocher1b610272013-12-19 13:45:04 +0100398static int first_call = 1;
399static const char *flags_list;
400
Joe Hershberger25980902012-12-11 22:16:31 -0600401/*
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.
405 */
406void env_flags_init(ENTRY *var_entry)
407{
408 const char *var_name = var_entry->key;
Joe Hershberger25980902012-12-11 22:16:31 -0600409 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
410 int ret = 1;
411
Heiko Schocher1b610272013-12-19 13:45:04 +0100412 if (first_call) {
413 flags_list = getenv(ENV_FLAGS_VAR);
414 first_call = 0;
415 }
Joe Hershberger25980902012-12-11 22:16:31 -0600416 /* look in the ".flags" and static for a reference to this variable */
417 ret = env_flags_lookup(flags_list, var_name, flags);
418
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);
422}
423
424/*
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.
427 */
428static int clear_flags(ENTRY *entry)
429{
430 entry->flags = 0;
431
432 return 0;
433}
434
435/*
436 * Call for each element in the list that defines flags for a variable
437 */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500438static int set_flags(const char *name, const char *value, void *priv)
Joe Hershberger25980902012-12-11 22:16:31 -0600439{
440 ENTRY e, *ep;
441
442 e.key = name;
443 e.data = NULL;
444 hsearch_r(e, FIND, &ep, &env_htab, 0);
445
446 /* does the env variable actually exist? */
447 if (ep != NULL) {
448 /* the flag list is empty, so clear the flags */
449 if (value == NULL || strlen(value) == 0)
450 ep->flags = 0;
451 else
452 /* assign the requested flags */
453 ep->flags = env_parse_flags_to_bin(value);
454 }
455
456 return 0;
457}
458
459static int on_flags(const char *name, const char *value, enum env_op op,
460 int flags)
461{
462 /* remove all flags */
463 hwalk_r(&env_htab, clear_flags);
464
465 /* configure any static flags */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500466 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL);
Joe Hershberger25980902012-12-11 22:16:31 -0600467 /* configure any dynamic flags */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500468 env_attr_walk(value, set_flags, NULL);
Joe Hershberger25980902012-12-11 22:16:31 -0600469
470 return 0;
471}
472U_BOOT_ENV_CALLBACK(flags, on_flags);
473
474/*
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.
480 */
481
482int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
483 int flag)
484{
485 const char *name;
Joe Hershberger25980902012-12-11 22:16:31 -0600486 const char *oldval = NULL;
487
488 if (op != env_op_create)
489 oldval = item->data;
Joe Hershberger25980902012-12-11 22:16:31 -0600490
491 name = item->key;
492
493 /* Default value for NULL to protect string-manipulating functions */
494 newval = newval ? : "";
495
Joe Hershberger25980902012-12-11 22:16:31 -0600496 /* 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);
500
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]);
505 return -1;
506 }
507 }
508
Joe Hershberger267541f2012-12-11 22:16:34 -0600509 /* check for access permission */
510#ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
511 if (flag & H_FORCE)
512 return 0;
513#endif
514 switch (op) {
515 case env_op_delete:
516 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
517 printf("## Error: Can't delete \"%s\"\n", name);
518 return 1;
519 }
520 break;
521 case env_op_overwrite:
522 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
523 printf("## Error: Can't overwrite \"%s\"\n", name);
524 return 1;
525 } else if (item->flags &
526 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
527 const char *defval = getenv_default(name);
528
529 if (defval == NULL)
530 defval = "";
531 printf("oldval: %s defval: %s\n", oldval, defval);
532 if (strcmp(oldval, defval) != 0) {
533 printf("## Error: Can't overwrite \"%s\"\n",
534 name);
535 return 1;
536 }
537 }
538 break;
539 case env_op_create:
540 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
541 printf("## Error: Can't create \"%s\"\n", name);
542 return 1;
543 }
544 break;
545 }
546
Joe Hershberger25980902012-12-11 22:16:31 -0600547 return 0;
548}
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600549
550#endif