blob: 09f93d59d63367580f436dd82c16167ed6f86e92 [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 Hershbergera2d62b72012-12-11 22:16:33 -060046#ifdef CONFIG_CMD_ENV_FLAGS
47static const char * const env_flags_vartype_names[] = {
48 "string",
49 "decimal",
50 "hexadecimal",
51 "boolean",
52#ifdef CONFIG_CMD_NET
53 "IP address",
54 "MAC address",
55#endif
56};
57
58/*
59 * Print the whole list of available type flags.
60 */
61void env_flags_print_vartypes(void)
62{
63 enum env_flags_vartype curtype = (enum env_flags_vartype)0;
64
65 while (curtype != env_flags_vartype_end) {
66 printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype],
67 env_flags_vartype_names[curtype]);
68 curtype++;
69 }
70}
71
72/*
73 * Return the name of the type.
74 */
75const char *env_flags_get_vartype_name(enum env_flags_vartype type)
76{
77 return env_flags_vartype_names[type];
78}
79#endif /* CONFIG_CMD_ENV_FLAGS */
Joe Hershberger71497d02012-12-11 22:16:31 -060080
81/*
82 * Parse the flags string from a .flags attribute list into the vartype enum.
83 */
84enum env_flags_vartype env_flags_parse_vartype(const char *flags)
85{
86 char *type;
87
88 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
89 return env_flags_vartype_string;
90
91 type = strchr(env_flags_vartype_rep,
92 flags[ENV_FLAGS_VARTYPE_LOC]);
93
94 if (type != NULL)
95 return (enum env_flags_vartype)
96 (type - &env_flags_vartype_rep[0]);
97
98 printf("## Warning: Unknown environment variable type '%c'\n",
99 flags[ENV_FLAGS_VARTYPE_LOC]);
100 return env_flags_vartype_string;
101}
102
103static inline int is_hex_prefix(const char *value)
104{
105 return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
106}
107
108static void skip_num(int hex, const char *value, const char **end,
109 int max_digits)
110{
111 int i;
112
113 if (hex && is_hex_prefix(value))
114 value += 2;
115
116 for (i = max_digits; i != 0; i--) {
117 if (hex && !isxdigit(*value))
118 break;
119 if (!hex && !isdigit(*value))
120 break;
121 value++;
122 }
123 if (end != NULL)
124 *end = value;
125}
126
127/*
128 * Based on the declared type enum, validate that the value string complies
129 * with that format
130 */
131static int _env_flags_validate_type(const char *value,
132 enum env_flags_vartype type)
133{
134 const char *end;
135#ifdef CONFIG_CMD_NET
136 const char *cur;
137 int i;
138#endif
139
140 switch (type) {
141 case env_flags_vartype_string:
142 break;
143 case env_flags_vartype_decimal:
144 skip_num(0, value, &end, -1);
145 if (*end != '\0')
146 return -1;
147 break;
148 case env_flags_vartype_hex:
149 skip_num(1, value, &end, -1);
150 if (*end != '\0')
151 return -1;
152 if (value + 2 == end && is_hex_prefix(value))
153 return -1;
154 break;
155 case env_flags_vartype_bool:
156 if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
157 value[0] != 'Y' && value[0] != 'T' &&
158 value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
159 value[0] != 'N' && value[0] != 'F')
160 return -1;
161 if (value[1] != '\0')
162 return -1;
163 break;
164#ifdef CONFIG_CMD_NET
165 case env_flags_vartype_ipaddr:
166 cur = value;
167 for (i = 0; i < 4; i++) {
168 skip_num(0, cur, &end, 3);
169 if (cur == end)
170 return -1;
171 if (i != 3 && *end != '.')
172 return -1;
173 if (i == 3 && *end != '\0')
174 return -1;
175 cur = end + 1;
176 }
177 break;
178 case env_flags_vartype_macaddr:
179 cur = value;
180 for (i = 0; i < 6; i++) {
181 skip_num(1, cur, &end, 2);
182 if (cur == end)
183 return -1;
184 if (cur + 2 == end && is_hex_prefix(cur))
185 return -1;
186 if (i != 5 && *end != ':')
187 return -1;
188 if (i == 5 && *end != '\0')
189 return -1;
190 cur = end + 1;
191 }
192 break;
193#endif
194 case env_flags_vartype_end:
195 return -1;
196 }
197
198 /* OK */
199 return 0;
200}
201
202/*
203 * Look for flags in a provided list and failing that the static list
204 */
205static inline int env_flags_lookup(const char *flags_list, const char *name,
206 char *flags)
207{
208 int ret = 1;
209
210 if (!flags)
211 /* bad parameter */
212 return -1;
213
214 /* try the env first */
215 if (flags_list)
216 ret = env_attr_lookup(flags_list, name, flags);
217
218 if (ret != 0)
219 /* if not found in the env, look in the static list */
220 ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
221
222 return ret;
223}
224
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600225#ifdef USE_HOSTCC /* Functions only used from tools/env */
226/*
227 * Look up any flags directly from the .flags variable and the static list
228 * and convert them to the vartype enum.
229 */
230enum env_flags_vartype env_flags_get_type(const char *name)
231{
232 const char *flags_list = getenv(ENV_FLAGS_VAR);
233 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
234
235 if (env_flags_lookup(flags_list, name, flags))
236 return env_flags_vartype_string;
237
238 if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
239 return env_flags_vartype_string;
240
241 return env_flags_parse_vartype(flags);
242}
243
244/*
245 * Validate that the proposed new value for "name" is valid according to the
246 * defined flags for that variable, if any.
247 */
248int env_flags_validate_type(const char *name, const char *value)
249{
250 enum env_flags_vartype type;
251
252 if (value == NULL)
253 return 0;
254 type = env_flags_get_type(name);
255 if (_env_flags_validate_type(value, type) < 0) {
256 printf("## Error: flags type check failure for "
257 "\"%s\" <= \"%s\" (type: %c)\n",
258 name, value, env_flags_vartype_rep[type]);
259 return -1;
260 }
261 return 0;
262}
263
264/*
265 * Validate the parameters to "env set" directly
266 */
267int env_flags_validate_env_set_params(int argc, char * const argv[])
268{
269 if ((argc >= 3) && argv[2] != NULL) {
270 enum env_flags_vartype type = env_flags_get_type(argv[1]);
271
272 /*
273 * we don't currently check types that need more than
274 * one argument
275 */
276 if (type != env_flags_vartype_string && argc > 3) {
277 printf("## Error: too many parameters for setting "
278 "\"%s\"\n", argv[1]);
279 return -1;
280 }
281 return env_flags_validate_type(argv[1], argv[2]);
282 }
283 /* ok */
284 return 0;
285}
286
287#else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
288
Joe Hershberger71497d02012-12-11 22:16:31 -0600289/*
290 * Parse the flag charachters from the .flags attribute list into the binary
291 * form to be stored in the environment entry->flags field.
292 */
293static int env_parse_flags_to_bin(const char *flags)
294{
295 return env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
296}
297
298/*
299 * Look for possible flags for a newly added variable
300 * This is called specifically when the variable did not exist in the hash
301 * previously, so the blanket update did not find this variable.
302 */
303void env_flags_init(ENTRY *var_entry)
304{
305 const char *var_name = var_entry->key;
306 const char *flags_list = getenv(ENV_FLAGS_VAR);
307 char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
308 int ret = 1;
309
310 /* look in the ".flags" and static for a reference to this variable */
311 ret = env_flags_lookup(flags_list, var_name, flags);
312
313 /* if any flags were found, set the binary form to the entry */
314 if (!ret && strlen(flags))
315 var_entry->flags = env_parse_flags_to_bin(flags);
316}
317
318/*
319 * Called on each existing env var prior to the blanket update since removing
320 * a flag in the flag list should remove its flags.
321 */
322static int clear_flags(ENTRY *entry)
323{
324 entry->flags = 0;
325
326 return 0;
327}
328
329/*
330 * Call for each element in the list that defines flags for a variable
331 */
332static int set_flags(const char *name, const char *value)
333{
334 ENTRY e, *ep;
335
336 e.key = name;
337 e.data = NULL;
338 hsearch_r(e, FIND, &ep, &env_htab, 0);
339
340 /* does the env variable actually exist? */
341 if (ep != NULL) {
342 /* the flag list is empty, so clear the flags */
343 if (value == NULL || strlen(value) == 0)
344 ep->flags = 0;
345 else
346 /* assign the requested flags */
347 ep->flags = env_parse_flags_to_bin(value);
348 }
349
350 return 0;
351}
352
353static int on_flags(const char *name, const char *value, enum env_op op,
354 int flags)
355{
356 /* remove all flags */
357 hwalk_r(&env_htab, clear_flags);
358
359 /* configure any static flags */
360 env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
361 /* configure any dynamic flags */
362 env_attr_walk(value, set_flags);
363
364 return 0;
365}
366U_BOOT_ENV_CALLBACK(flags, on_flags);
367
368/*
369 * Perform consistency checking before creating, overwriting, or deleting an
370 * environment variable. Called as a callback function by hsearch_r() and
371 * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
372 * When (flag & H_FORCE) is set, do not print out any error message and force
373 * overwriting of write-once variables.
374 */
375
376int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
377 int flag)
378{
379 const char *name;
380#if !defined(CONFIG_ENV_OVERWRITE) && defined(CONFIG_OVERWRITE_ETHADDR_ONCE) \
381&& defined(CONFIG_ETHADDR)
382 const char *oldval = NULL;
383
384 if (op != env_op_create)
385 oldval = item->data;
386#endif
387
388 name = item->key;
389
390 /* Default value for NULL to protect string-manipulating functions */
391 newval = newval ? : "";
392
393#ifndef CONFIG_ENV_OVERWRITE
394 /*
395 * Some variables like "ethaddr" and "serial#" can be set only once and
396 * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
397 */
398 if (op != env_op_create && /* variable exists */
399 (flag & H_FORCE) == 0) { /* and we are not forced */
400 if (strcmp(name, "serial#") == 0 ||
401 (strcmp(name, "ethaddr") == 0
402#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
403 && strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0
404#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
405 )) {
406 printf("Can't overwrite \"%s\"\n", name);
407 return 1;
408 }
409 }
410#endif
411
412 /* validate the value to match the variable type */
413 if (op != env_op_delete) {
414 enum env_flags_vartype type = (enum env_flags_vartype)
415 (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
416
417 if (_env_flags_validate_type(newval, type) < 0) {
418 printf("## Error: flags type check failure for "
419 "\"%s\" <= \"%s\" (type: %c)\n",
420 name, newval, env_flags_vartype_rep[type]);
421 return -1;
422 }
423 }
424
425 return 0;
426}
Joe Hershbergerdd7750a2012-12-11 22:16:32 -0600427
428#endif