blob: e682d8517890cca27d87d812244e16330071e783 [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
Codrin Ciubotariu0118e832015-09-09 18:00:51 +0300190#ifdef CONFIG_CMD_NET
191int eth_validate_ethaddr_str(const char *addr)
192{
193 const char *end;
194 const char *cur;
195 int i;
196
197 cur = addr;
198 for (i = 0; i < 6; i++) {
199 skip_num(1, cur, &end, 2);
200 if (cur == end)
201 return -1;
202 if (cur + 2 == end && is_hex_prefix(cur))
203 return -1;
204 if (i != 5 && *end != ':')
205 return -1;
206 if (i == 5 && *end != '\0')
207 return -1;
208 cur = end + 1;
209 }
210
211 return 0;
212}
213#endif
214
Joe Hershberger25980902012-12-11 22:16:31 -0600215/*
216 * Based on the declared type enum, validate that the value string complies
217 * with that format
218 */
219static int _env_flags_validate_type(const char *value,
220 enum env_flags_vartype type)
221{
222 const char *end;
223#ifdef CONFIG_CMD_NET
224 const char *cur;
225 int i;
226#endif
227
228 switch (type) {
229 case env_flags_vartype_string:
230 break;
231 case env_flags_vartype_decimal:
232 skip_num(0, value, &end, -1);
233 if (*end != '\0')
234 return -1;
235 break;
236 case env_flags_vartype_hex:
237 skip_num(1, value, &end, -1);
238 if (*end != '\0')
239 return -1;
240 if (value + 2 == end && is_hex_prefix(value))
241 return -1;
242 break;
243 case env_flags_vartype_bool:
244 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
245 value[0] != 'Y' && value[0] != 'T' &&
246 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
247 value[0] != 'N' && value[0] != 'F')
248 return -1;
249 if (value[1] != '\0')
250 return -1;
251 break;
252#ifdef CONFIG_CMD_NET
253 case env_flags_vartype_ipaddr:
254 cur = value;
255 for (i = 0; i < 4; i++) {
256 skip_num(0, cur, &end, 3);
257 if (cur == end)
258 return -1;
259 if (i != 3 && *end != '.')
260 return -1;
261 if (i == 3 && *end != '\0')
262 return -1;
263 cur = end + 1;
264 }
265 break;
266 case env_flags_vartype_macaddr:
Codrin Ciubotariu0118e832015-09-09 18:00:51 +0300267 if (eth_validate_ethaddr_str(value))
268 return -1;
Joe Hershberger25980902012-12-11 22:16:31 -0600269 break;
270#endif
271 case env_flags_vartype_end:
272 return -1;
273 }
274
275 /* OK */
276 return 0;
277}
278
279/*
280 * Look for flags in a provided list and failing that the static list
281 */
282static inline int env_flags_lookup(const char *flags_list, const char *name,
283 char *flags)
284{
285 int ret = 1;
286
287 if (!flags)
288 /* bad parameter */
289 return -1;
290
291 /* try the env first */
292 if (flags_list)
293 ret = env_attr_lookup(flags_list, name, flags);
294
295 if (ret != 0)
296 /* if not found in the env, look in the static list */
297 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
298
299 return ret;
300}
301
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600302#ifdef USE_HOSTCC /* Functions only used from tools/env */
303/*
304 * Look up any flags directly from the .flags variable and the static list
305 * and convert them to the vartype enum.
306 */
307enum env_flags_vartype env_flags_get_type(const char *name)
308{
309 const char *flags_list = getenv(ENV_FLAGS_VAR);
310 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
311
312 if (env_flags_lookup(flags_list, name, flags))
313 return env_flags_vartype_string;
314
315 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
316 return env_flags_vartype_string;
317
318 return env_flags_parse_vartype(flags);
319}
320
321/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600322 * Look up the access of a variable directly from the .flags var.
323 */
324enum env_flags_varaccess env_flags_get_varaccess(const char *name)
325{
326 const char *flags_list = getenv(ENV_FLAGS_VAR);
327 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
328
329 if (env_flags_lookup(flags_list, name, flags))
330 return env_flags_varaccess_any;
331
332 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
333 return env_flags_varaccess_any;
334
335 return env_flags_parse_varaccess(flags);
336}
337
338/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600339 * Validate that the proposed new value for "name" is valid according to the
340 * defined flags for that variable, if any.
341 */
342int env_flags_validate_type(const char *name, const char *value)
343{
344 enum env_flags_vartype type;
345
346 if (value == NULL)
347 return 0;
348 type = env_flags_get_type(name);
349 if (_env_flags_validate_type(value, type) < 0) {
350 printf("## Error: flags type check failure for "
351 "\"%s\" <= \"%s\" (type: %c)\n",
352 name, value, env_flags_vartype_rep[type]);
353 return -1;
354 }
355 return 0;
356}
357
358/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600359 * Validate that the proposed access to variable "name" is valid according to
360 * the defined flags for that variable, if any.
361 */
362int env_flags_validate_varaccess(const char *name, int check_mask)
363{
364 enum env_flags_varaccess access;
365 int access_mask;
366
367 access = env_flags_get_varaccess(name);
368 access_mask = env_flags_varaccess_mask[access];
369
370 return (check_mask & access_mask) != 0;
371}
372
373/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600374 * Validate the parameters to "env set" directly
375 */
376int env_flags_validate_env_set_params(int argc, char * const argv[])
377{
378 if ((argc >= 3) && argv[2] != NULL) {
379 enum env_flags_vartype type = env_flags_get_type(argv[1]);
380
381 /*
382 * we don't currently check types that need more than
383 * one argument
384 */
385 if (type != env_flags_vartype_string && argc > 3) {
386 printf("## Error: too many parameters for setting "
387 "\"%s\"\n", argv[1]);
388 return -1;
389 }
390 return env_flags_validate_type(argv[1], argv[2]);
391 }
392 /* ok */
393 return 0;
394}
395
396#else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
397
Joe Hershberger25980902012-12-11 22:16:31 -0600398/*
399 * Parse the flag charachters from the .flags attribute list into the binary
400 * form to be stored in the environment entry->flags field.
401 */
402static int env_parse_flags_to_bin(const char *flags)
403{
Joe Hershberger267541f2012-12-11 22:16:34 -0600404 int binflags;
405
406 binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
407 binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
408
409 return binflags;
Joe Hershberger25980902012-12-11 22:16:31 -0600410}
411
Heiko Schocher1b610272013-12-19 13:45:04 +0100412static int first_call = 1;
413static const char *flags_list;
414
Joe Hershberger25980902012-12-11 22:16:31 -0600415/*
416 * Look for possible flags for a newly added variable
417 * This is called specifically when the variable did not exist in the hash
418 * previously, so the blanket update did not find this variable.
419 */
420void env_flags_init(ENTRY *var_entry)
421{
422 const char *var_name = var_entry->key;
Joe Hershberger25980902012-12-11 22:16:31 -0600423 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
424 int ret = 1;
425
Heiko Schocher1b610272013-12-19 13:45:04 +0100426 if (first_call) {
427 flags_list = getenv(ENV_FLAGS_VAR);
428 first_call = 0;
429 }
Joe Hershberger25980902012-12-11 22:16:31 -0600430 /* look in the ".flags" and static for a reference to this variable */
431 ret = env_flags_lookup(flags_list, var_name, flags);
432
433 /* if any flags were found, set the binary form to the entry */
434 if (!ret && strlen(flags))
435 var_entry->flags = env_parse_flags_to_bin(flags);
436}
437
438/*
439 * Called on each existing env var prior to the blanket update since removing
440 * a flag in the flag list should remove its flags.
441 */
442static int clear_flags(ENTRY *entry)
443{
444 entry->flags = 0;
445
446 return 0;
447}
448
449/*
450 * Call for each element in the list that defines flags for a variable
451 */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500452static int set_flags(const char *name, const char *value, void *priv)
Joe Hershberger25980902012-12-11 22:16:31 -0600453{
454 ENTRY e, *ep;
455
456 e.key = name;
457 e.data = NULL;
458 hsearch_r(e, FIND, &ep, &env_htab, 0);
459
460 /* does the env variable actually exist? */
461 if (ep != NULL) {
462 /* the flag list is empty, so clear the flags */
463 if (value == NULL || strlen(value) == 0)
464 ep->flags = 0;
465 else
466 /* assign the requested flags */
467 ep->flags = env_parse_flags_to_bin(value);
468 }
469
470 return 0;
471}
472
473static int on_flags(const char *name, const char *value, enum env_op op,
474 int flags)
475{
476 /* remove all flags */
477 hwalk_r(&env_htab, clear_flags);
478
479 /* configure any static flags */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500480 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL);
Joe Hershberger25980902012-12-11 22:16:31 -0600481 /* configure any dynamic flags */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500482 env_attr_walk(value, set_flags, NULL);
Joe Hershberger25980902012-12-11 22:16:31 -0600483
484 return 0;
485}
486U_BOOT_ENV_CALLBACK(flags, on_flags);
487
488/*
489 * Perform consistency checking before creating, overwriting, or deleting an
490 * environment variable. Called as a callback function by hsearch_r() and
491 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
492 * When (flag & H_FORCE) is set, do not print out any error message and force
493 * overwriting of write-once variables.
494 */
495
496int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
497 int flag)
498{
499 const char *name;
Joe Hershberger25980902012-12-11 22:16:31 -0600500 const char *oldval = NULL;
501
502 if (op != env_op_create)
503 oldval = item->data;
Joe Hershberger25980902012-12-11 22:16:31 -0600504
505 name = item->key;
506
507 /* Default value for NULL to protect string-manipulating functions */
508 newval = newval ? : "";
509
Joe Hershberger25980902012-12-11 22:16:31 -0600510 /* validate the value to match the variable type */
511 if (op != env_op_delete) {
512 enum env_flags_vartype type = (enum env_flags_vartype)
513 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
514
515 if (_env_flags_validate_type(newval, type) < 0) {
516 printf("## Error: flags type check failure for "
517 "\"%s\" <= \"%s\" (type: %c)\n",
518 name, newval, env_flags_vartype_rep[type]);
519 return -1;
520 }
521 }
522
Joe Hershberger267541f2012-12-11 22:16:34 -0600523 /* check for access permission */
524#ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
525 if (flag & H_FORCE)
526 return 0;
527#endif
528 switch (op) {
529 case env_op_delete:
530 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
531 printf("## Error: Can't delete \"%s\"\n", name);
532 return 1;
533 }
534 break;
535 case env_op_overwrite:
536 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
537 printf("## Error: Can't overwrite \"%s\"\n", name);
538 return 1;
539 } else if (item->flags &
540 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
541 const char *defval = getenv_default(name);
542
543 if (defval == NULL)
544 defval = "";
545 printf("oldval: %s defval: %s\n", oldval, defval);
546 if (strcmp(oldval, defval) != 0) {
547 printf("## Error: Can't overwrite \"%s\"\n",
548 name);
549 return 1;
550 }
551 }
552 break;
553 case env_op_create:
554 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
555 printf("## Error: Can't create \"%s\"\n", name);
556 return 1;
557 }
558 break;
559 }
560
Joe Hershberger25980902012-12-11 22:16:31 -0600561 return 0;
562}
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600563
564#endif