blob: 336cae4e9ff0fd1b810fc228adca9f4ee80c2886 [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <linux/string.h>
25#include <linux/ctype.h>
26
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060027#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
28#include <stdint.h>
29#include <stdio.h>
30#include "fw_env.h"
31#include <env_attr.h>
32#include <env_flags.h>
33#define getenv fw_getenv
34#else
Joe Hershberger71497d02012-12-11 22:16:31 -060035#include <common.h>
36#include <environment.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060037#endif
Joe Hershberger71497d02012-12-11 22:16:31 -060038
39#ifdef CONFIG_CMD_NET
40#define ENV_FLAGS_NET_VARTYPE_REPS "im"
41#else
42#define ENV_FLAGS_NET_VARTYPE_REPS ""
43#endif
44
45static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
Joe Hershberger6fe26c92012-12-11 22:16:34 -060046static const char env_flags_varaccess_rep[] = "aroc";
47static const int env_flags_varaccess_mask[] = {
48 0,
49 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
50 ENV_FLAGS_VARACCESS_PREVENT_CREATE |
51 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
52 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
53 ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
54 ENV_FLAGS_VARACCESS_PREVENT_DELETE |
55 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR};
56
Joe Hershbergera2d62b72012-12-11 22:16:33 -060057#ifdef CONFIG_CMD_ENV_FLAGS
58static const char * const env_flags_vartype_names[] = {
59 "string",
60 "decimal",
61 "hexadecimal",
62 "boolean",
63#ifdef CONFIG_CMD_NET
64 "IP address",
65 "MAC address",
66#endif
67};
Joe Hershberger6fe26c92012-12-11 22:16:34 -060068static const char * const env_flags_varaccess_names[] = {
69 "any",
70 "read-only",
71 "write-once",
72 "change-default",
73};
Joe Hershbergera2d62b72012-12-11 22:16:33 -060074
75/*
76 * Print the whole list of available type flags.
77 */
78void env_flags_print_vartypes(void)
79{
80 enum env_flags_vartype curtype = (enum env_flags_vartype)0;
81
82 while (curtype != env_flags_vartype_end) {
83 printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype],
84 env_flags_vartype_names[curtype]);
85 curtype++;
86 }
87}
88
89/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -060090 * Print the whole list of available access flags.
91 */
92void env_flags_print_varaccess(void)
93{
94 enum env_flags_varaccess curaccess = (enum env_flags_varaccess)0;
95
96 while (curaccess != env_flags_varaccess_end) {
97 printf("\t%c -\t%s\n", env_flags_varaccess_rep[curaccess],
98 env_flags_varaccess_names[curaccess]);
99 curaccess++;
100 }
101}
102
103/*
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600104 * Return the name of the type.
105 */
106const char *env_flags_get_vartype_name(enum env_flags_vartype type)
107{
108 return env_flags_vartype_names[type];
109}
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600110
111/*
112 * Return the name of the access.
113 */
114const char *env_flags_get_varaccess_name(enum env_flags_varaccess access)
115{
116 return env_flags_varaccess_names[access];
117}
Joe Hershbergera2d62b72012-12-11 22:16:33 -0600118#endif /* CONFIG_CMD_ENV_FLAGS */
Joe Hershberger71497d02012-12-11 22:16:31 -0600119
120/*
121 * Parse the flags string from a .flags attribute list into the vartype enum.
122 */
123enum env_flags_vartype env_flags_parse_vartype(const char *flags)
124{
125 char *type;
126
127 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
128 return env_flags_vartype_string;
129
130 type = strchr(env_flags_vartype_rep,
131 flags[ENV_FLAGS_VARTYPE_LOC]);
132
133 if (type != NULL)
134 return (enum env_flags_vartype)
135 (type - &env_flags_vartype_rep[0]);
136
137 printf("## Warning: Unknown environment variable type '%c'\n",
138 flags[ENV_FLAGS_VARTYPE_LOC]);
139 return env_flags_vartype_string;
140}
141
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600142/*
143 * Parse the flags string from a .flags attribute list into the varaccess enum.
144 */
145enum env_flags_varaccess env_flags_parse_varaccess(const char *flags)
146{
147 char *access;
148
149 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
150 return env_flags_varaccess_any;
151
152 access = strchr(env_flags_varaccess_rep,
153 flags[ENV_FLAGS_VARACCESS_LOC]);
154
155 if (access != NULL)
156 return (enum env_flags_varaccess)
157 (access - &env_flags_varaccess_rep[0]);
158
159 printf("## Warning: Unknown environment variable access method '%c'\n",
160 flags[ENV_FLAGS_VARACCESS_LOC]);
161 return env_flags_varaccess_any;
162}
163
164/*
165 * Parse the binary flags from a hash table entry into the varaccess enum.
166 */
167enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags)
168{
169 int i;
170
171 for (i = 0; i < sizeof(env_flags_varaccess_mask); i++)
172 if (env_flags_varaccess_mask[i] ==
173 (binflags & ENV_FLAGS_VARACCESS_BIN_MASK))
174 return (enum env_flags_varaccess)i;
175
176 printf("Warning: Non-standard access flags. (0x%x)\n",
177 binflags & ENV_FLAGS_VARACCESS_BIN_MASK);
178
179 return env_flags_varaccess_any;
180}
181
Joe Hershberger71497d02012-12-11 22:16:31 -0600182static inline int is_hex_prefix(const char *value)
183{
184 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
185}
186
187static void skip_num(int hex, const char *value, const char **end,
188 int max_digits)
189{
190 int i;
191
192 if (hex && is_hex_prefix(value))
193 value += 2;
194
195 for (i = max_digits; i != 0; i--) {
196 if (hex && !isxdigit(*value))
197 break;
198 if (!hex && !isdigit(*value))
199 break;
200 value++;
201 }
202 if (end != NULL)
203 *end = value;
204}
205
206/*
207 * Based on the declared type enum, validate that the value string complies
208 * with that format
209 */
210static int _env_flags_validate_type(const char *value,
211 enum env_flags_vartype type)
212{
213 const char *end;
214#ifdef CONFIG_CMD_NET
215 const char *cur;
216 int i;
217#endif
218
219 switch (type) {
220 case env_flags_vartype_string:
221 break;
222 case env_flags_vartype_decimal:
223 skip_num(0, value, &end, -1);
224 if (*end != '\0')
225 return -1;
226 break;
227 case env_flags_vartype_hex:
228 skip_num(1, value, &end, -1);
229 if (*end != '\0')
230 return -1;
231 if (value + 2 == end && is_hex_prefix(value))
232 return -1;
233 break;
234 case env_flags_vartype_bool:
235 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
236 value[0] != 'Y' && value[0] != 'T' &&
237 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
238 value[0] != 'N' && value[0] != 'F')
239 return -1;
240 if (value[1] != '\0')
241 return -1;
242 break;
243#ifdef CONFIG_CMD_NET
244 case env_flags_vartype_ipaddr:
245 cur = value;
246 for (i = 0; i < 4; i++) {
247 skip_num(0, cur, &end, 3);
248 if (cur == end)
249 return -1;
250 if (i != 3 && *end != '.')
251 return -1;
252 if (i == 3 && *end != '\0')
253 return -1;
254 cur = end + 1;
255 }
256 break;
257 case env_flags_vartype_macaddr:
258 cur = value;
259 for (i = 0; i < 6; i++) {
260 skip_num(1, cur, &end, 2);
261 if (cur == end)
262 return -1;
263 if (cur + 2 == end && is_hex_prefix(cur))
264 return -1;
265 if (i != 5 && *end != ':')
266 return -1;
267 if (i == 5 && *end != '\0')
268 return -1;
269 cur = end + 1;
270 }
271 break;
272#endif
273 case env_flags_vartype_end:
274 return -1;
275 }
276
277 /* OK */
278 return 0;
279}
280
281/*
282 * Look for flags in a provided list and failing that the static list
283 */
284static inline int env_flags_lookup(const char *flags_list, const char *name,
285 char *flags)
286{
287 int ret = 1;
288
289 if (!flags)
290 /* bad parameter */
291 return -1;
292
293 /* try the env first */
294 if (flags_list)
295 ret = env_attr_lookup(flags_list, name, flags);
296
297 if (ret != 0)
298 /* if not found in the env, look in the static list */
299 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
300
301 return ret;
302}
303
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600304#ifdef USE_HOSTCC /* Functions only used from tools/env */
305/*
306 * Look up any flags directly from the .flags variable and the static list
307 * and convert them to the vartype enum.
308 */
309enum env_flags_vartype env_flags_get_type(const char *name)
310{
311 const char *flags_list = getenv(ENV_FLAGS_VAR);
312 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
313
314 if (env_flags_lookup(flags_list, name, flags))
315 return env_flags_vartype_string;
316
317 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
318 return env_flags_vartype_string;
319
320 return env_flags_parse_vartype(flags);
321}
322
323/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600324 * Look up the access of a variable directly from the .flags var.
325 */
326enum env_flags_varaccess env_flags_get_varaccess(const char *name)
327{
328 const char *flags_list = getenv(ENV_FLAGS_VAR);
329 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
330
331 if (env_flags_lookup(flags_list, name, flags))
332 return env_flags_varaccess_any;
333
334 if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
335 return env_flags_varaccess_any;
336
337 return env_flags_parse_varaccess(flags);
338}
339
340/*
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600341 * Validate that the proposed new value for "name" is valid according to the
342 * defined flags for that variable, if any.
343 */
344int env_flags_validate_type(const char *name, const char *value)
345{
346 enum env_flags_vartype type;
347
348 if (value == NULL)
349 return 0;
350 type = env_flags_get_type(name);
351 if (_env_flags_validate_type(value, type) < 0) {
352 printf("## Error: flags type check failure for "
353 "\"%s\" <= \"%s\" (type: %c)\n",
354 name, value, env_flags_vartype_rep[type]);
355 return -1;
356 }
357 return 0;
358}
359
360/*
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600361 * Validate that the proposed access to variable "name" is valid according to
362 * the defined flags for that variable, if any.
363 */
364int env_flags_validate_varaccess(const char *name, int check_mask)
365{
366 enum env_flags_varaccess access;
367 int access_mask;
368
369 access = env_flags_get_varaccess(name);
370 access_mask = env_flags_varaccess_mask[access];
371
372 return (check_mask & access_mask) != 0;
373}
374
375/*
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600376 * Validate the parameters to "env set" directly
377 */
378int env_flags_validate_env_set_params(int argc, char * const argv[])
379{
380 if ((argc >= 3) && argv[2] != NULL) {
381 enum env_flags_vartype type = env_flags_get_type(argv[1]);
382
383 /*
384 * we don't currently check types that need more than
385 * one argument
386 */
387 if (type != env_flags_vartype_string && argc > 3) {
388 printf("## Error: too many parameters for setting "
389 "\"%s\"\n", argv[1]);
390 return -1;
391 }
392 return env_flags_validate_type(argv[1], argv[2]);
393 }
394 /* ok */
395 return 0;
396}
397
398#else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
399
Joe Hershberger71497d02012-12-11 22:16:31 -0600400/*
401 * Parse the flag charachters from the .flags attribute list into the binary
402 * form to be stored in the environment entry->flags field.
403 */
404static int env_parse_flags_to_bin(const char *flags)
405{
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600406 int binflags;
407
408 binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
409 binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
410
411 return binflags;
Joe Hershberger71497d02012-12-11 22:16:31 -0600412}
413
414/*
415 * Look for possible flags for a newly added variable
416 * This is called specifically when the variable did not exist in the hash
417 * previously, so the blanket update did not find this variable.
418 */
419void env_flags_init(ENTRY *var_entry)
420{
421 const char *var_name = var_entry->key;
422 const char *flags_list = getenv(ENV_FLAGS_VAR);
423 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
424 int ret = 1;
425
426 /* look in the ".flags" and static for a reference to this variable */
427 ret = env_flags_lookup(flags_list, var_name, flags);
428
429 /* if any flags were found, set the binary form to the entry */
430 if (!ret && strlen(flags))
431 var_entry->flags = env_parse_flags_to_bin(flags);
432}
433
434/*
435 * Called on each existing env var prior to the blanket update since removing
436 * a flag in the flag list should remove its flags.
437 */
438static int clear_flags(ENTRY *entry)
439{
440 entry->flags = 0;
441
442 return 0;
443}
444
445/*
446 * Call for each element in the list that defines flags for a variable
447 */
448static int set_flags(const char *name, const char *value)
449{
450 ENTRY e, *ep;
451
452 e.key = name;
453 e.data = NULL;
454 hsearch_r(e, FIND, &ep, &env_htab, 0);
455
456 /* does the env variable actually exist? */
457 if (ep != NULL) {
458 /* the flag list is empty, so clear the flags */
459 if (value == NULL || strlen(value) == 0)
460 ep->flags = 0;
461 else
462 /* assign the requested flags */
463 ep->flags = env_parse_flags_to_bin(value);
464 }
465
466 return 0;
467}
468
469static int on_flags(const char *name, const char *value, enum env_op op,
470 int flags)
471{
472 /* remove all flags */
473 hwalk_r(&env_htab, clear_flags);
474
475 /* configure any static flags */
476 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
477 /* configure any dynamic flags */
478 env_attr_walk(value, set_flags);
479
480 return 0;
481}
482U_BOOT_ENV_CALLBACK(flags, on_flags);
483
484/*
485 * Perform consistency checking before creating, overwriting, or deleting an
486 * environment variable. Called as a callback function by hsearch_r() and
487 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
488 * When (flag & H_FORCE) is set, do not print out any error message and force
489 * overwriting of write-once variables.
490 */
491
492int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
493 int flag)
494{
495 const char *name;
Joe Hershberger71497d02012-12-11 22:16:31 -0600496 const char *oldval = NULL;
497
498 if (op != env_op_create)
499 oldval = item->data;
Joe Hershberger71497d02012-12-11 22:16:31 -0600500
501 name = item->key;
502
503 /* Default value for NULL to protect string-manipulating functions */
504 newval = newval ? : "";
505
Joe Hershberger71497d02012-12-11 22:16:31 -0600506 /* validate the value to match the variable type */
507 if (op != env_op_delete) {
508 enum env_flags_vartype type = (enum env_flags_vartype)
509 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
510
511 if (_env_flags_validate_type(newval, type) < 0) {
512 printf("## Error: flags type check failure for "
513 "\"%s\" <= \"%s\" (type: %c)\n",
514 name, newval, env_flags_vartype_rep[type]);
515 return -1;
516 }
Joe Hershberger6fe26c92012-12-11 22:16:34 -0600517 }
518
519 /* check for access permission */
520#ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE
521 if (flag & H_FORCE)
522 return 0;
523#endif
524 switch (op) {
525 case env_op_delete:
526 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) {
527 printf("## Error: Can't delete \"%s\"\n", name);
528 return 1;
529 }
530 break;
531 case env_op_overwrite:
532 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) {
533 printf("## Error: Can't overwrite \"%s\"\n", name);
534 return 1;
535 } else if (item->flags &
536 ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) {
537 const char *defval = getenv_default(name);
538
539 if (defval == NULL)
540 defval = "";
541 printf("oldval: %s defval: %s\n", oldval, defval);
542 if (strcmp(oldval, defval) != 0) {
543 printf("## Error: Can't overwrite \"%s\"\n",
544 name);
545 return 1;
546 }
547 }
548 break;
549 case env_op_create:
550 if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) {
551 printf("## Error: Can't create \"%s\"\n", name);
552 return 1;
553 }
554 break;
Joe Hershberger71497d02012-12-11 22:16:31 -0600555 }
556
557 return 0;
558}
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600559
560#endif