blob: e9b72e60a94cb57091fed15d6aeeada78f16abd2 [file] [log] [blame]
Joe Hershberger71497d02012-12-11 22:16:31 -06001/*
2 * (C) Copyright 2012
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Joe Hershberger71497d02012-12-11 22:16:31 -06006 */
7
8#include <linux/string.h>
9#include <linux/ctype.h>
10
Joe Hershbergerdd7750a2012-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 Hershberger71497d02012-12-11 22:16:31 -060019#include <common.h>
20#include <environment.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060021#endif
Joe Hershberger71497d02012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-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 Hershberger6fe26c92012-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 Hershbergera2d62b72012-12-11 22:16:33 -0600102#endif /* CONFIG_CMD_ENV_FLAGS */
Joe Hershberger71497d02012-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 Hershberger6fe26c92012-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 Hershberger71497d02012-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 Hershbergerdd7750a2012-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 Hershberger6fe26c92012-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 Hershbergerdd7750a2012-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 Hershberger6fe26c92012-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 Hershbergerdd7750a2012-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 Hershberger71497d02012-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 Hershberger6fe26c92012-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 Hershberger71497d02012-12-11 22:16:31 -0600396}
397
398/*
399 * Look for possible flags for a newly added variable
400 * This is called specifically when the variable did not exist in the hash
401 * previously, so the blanket update did not find this variable.
402 */
403void env_flags_init(ENTRY *var_entry)
404{
405 const char *var_name = var_entry->key;
406 const char *flags_list = getenv(ENV_FLAGS_VAR);
407 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
408 int ret = 1;
409
410 /* look in the ".flags" and static for a reference to this variable */
411 ret = env_flags_lookup(flags_list, var_name, flags);
412
413 /* if any flags were found, set the binary form to the entry */
414 if (!ret && strlen(flags))
415 var_entry->flags = env_parse_flags_to_bin(flags);
416}
417
418/*
419 * Called on each existing env var prior to the blanket update since removing
420 * a flag in the flag list should remove its flags.
421 */
422static int clear_flags(ENTRY *entry)
423{
424 entry->flags = 0;
425
426 return 0;
427}
428
429/*
430 * Call for each element in the list that defines flags for a variable
431 */
432static int set_flags(const char *name, const char *value)
433{
434 ENTRY e, *ep;
435
436 e.key = name;
437 e.data = NULL;
438 hsearch_r(e, FIND, &ep, &env_htab, 0);
439
440 /* does the env variable actually exist? */
441 if (ep != NULL) {
442 /* the flag list is empty, so clear the flags */
443 if (value == NULL || strlen(value) == 0)
444 ep->flags = 0;
445 else
446 /* assign the requested flags */
447 ep->flags = env_parse_flags_to_bin(value);
448 }
449
450 return 0;
451}
452
453static int on_flags(const char *name, const char *value, enum env_op op,
454 int flags)
455{
456 /* remove all flags */
457 hwalk_r(&env_htab, clear_flags);
458
459 /* configure any static flags */
460 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
461 /* configure any dynamic flags */
462 env_attr_walk(value, set_flags);
463
464 return 0;
465}
466U_BOOT_ENV_CALLBACK(flags, on_flags);
467
468/*
469 * Perform consistency checking before creating, overwriting, or deleting an
470 * environment variable. Called as a callback function by hsearch_r() and
471 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
472 * When (flag & H_FORCE) is set, do not print out any error message and force
473 * overwriting of write-once variables.
474 */
475
476int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
477 int flag)
478{
479 const char *name;
Joe Hershberger71497d02012-12-11 22:16:31 -0600480 const char *oldval = NULL;
481
482 if (op != env_op_create)
483 oldval = item->data;
Joe Hershberger71497d02012-12-11 22:16:31 -0600484
485 name = item->key;
486
487 /* Default value for NULL to protect string-manipulating functions */
488 newval = newval ? : "";
489
Joe Hershberger71497d02012-12-11 22:16:31 -0600490 /* validate the value to match the variable type */
491 if (op != env_op_delete) {
492 enum env_flags_vartype type = (enum env_flags_vartype)
493 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
494
495 if (_env_flags_validate_type(newval, type) < 0) {
496 printf("## Error: flags type check failure for "
497 "\"%s\" <= \"%s\" (type: %c)\n",
498 name, newval, env_flags_vartype_rep[type]);
499 return -1;
500 }
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600501 }
502
503 /* check for access permission */
504#ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
505 if (flag & H_FORCE)
506 return 0;
507#endif
508 switch (op) {
509 case env_op_delete:
510 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
511 printf("## Error: Can't delete \"%s\"\n", name);
512 return 1;
513 }
514 break;
515 case env_op_overwrite:
516 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
517 printf("## Error: Can't overwrite \"%s\"\n", name);
518 return 1;
519 } else if (item->flags &
520 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
521 const char *defval = getenv_default(name);
522
523 if (defval == NULL)
524 defval = "";
525 printf("oldval: %s defval: %s\n", oldval, defval);
526 if (strcmp(oldval, defval) != 0) {
527 printf("## Error: Can't overwrite \"%s\"\n",
528 name);
529 return 1;
530 }
531 }
532 break;
533 case env_op_create:
534 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
535 printf("## Error: Can't create \"%s\"\n", name);
536 return 1;
537 }
538 break;
Joe Hershberger71497d02012-12-11 22:16:31 -0600539 }
540
541 return 0;
542}
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600543
544#endif