Blame view

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