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