blob: d1aa1440f704dd2a58aef381c8427d02d746edad [file] [log] [blame]
Joe Hershberger25980902012-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#ifndef __ENV_FLAGS_H__
25#define __ENV_FLAGS_H__
26
27enum env_flags_vartype {
28 env_flags_vartype_string,
29 env_flags_vartype_decimal,
30 env_flags_vartype_hex,
31 env_flags_vartype_bool,
32#ifdef CONFIG_CMD_NET
33 env_flags_vartype_ipaddr,
34 env_flags_vartype_macaddr,
35#endif
36 env_flags_vartype_end
37};
38
Joe Hershberger267541f2012-12-11 22:16:34 -060039enum env_flags_varaccess {
40 env_flags_varaccess_any,
41 env_flags_varaccess_readonly,
42 env_flags_varaccess_writeonce,
43 env_flags_varaccess_changedefault,
44 env_flags_varaccess_end
45};
46
Joe Hershberger25980902012-12-11 22:16:31 -060047#define ENV_FLAGS_VAR ".flags"
48#define ENV_FLAGS_ATTR_MAX_LEN 2
49#define ENV_FLAGS_VARTYPE_LOC 0
Joe Hershberger267541f2012-12-11 22:16:34 -060050#define ENV_FLAGS_VARACCESS_LOC 1
Joe Hershberger25980902012-12-11 22:16:31 -060051
52#ifndef CONFIG_ENV_FLAGS_LIST_STATIC
53#define CONFIG_ENV_FLAGS_LIST_STATIC ""
54#endif
55
Joe Hershberger1d6cd0a2012-12-11 22:16:37 -060056#ifdef CONFIG_CMD_NET
57#ifdef CONFIG_ENV_OVERWRITE
58#define ETHADDR_FLAGS "ethaddr:ma,"
59#else
60#ifdef CONFIG_OVERWRITE_ETHADDR_ONCE
61#define ETHADDR_FLAGS "ethaddr:mc,"
62#else
63#define ETHADDR_FLAGS "ethaddr:mo,"
64#endif
65#endif
66#else
67#define ETHADDR_FLAGS ""
68#endif
69
70#ifndef CONFIG_ENV_OVERWRITE
71#define SERIAL_FLAGS "serial#:so,"
72#else
73#define SERIAL_FLAGS ""
74#endif
75
Joe Hershberger25980902012-12-11 22:16:31 -060076#define ENV_FLAGS_LIST_STATIC \
Joe Hershberger1d6cd0a2012-12-11 22:16:37 -060077 ETHADDR_FLAGS \
78 SERIAL_FLAGS \
Joe Hershberger25980902012-12-11 22:16:31 -060079 CONFIG_ENV_FLAGS_LIST_STATIC
80
Joe Hershbergerfffad712012-12-11 22:16:33 -060081#ifdef CONFIG_CMD_ENV_FLAGS
82/*
83 * Print the whole list of available type flags.
84 */
85void env_flags_print_vartypes(void);
86/*
Joe Hershberger267541f2012-12-11 22:16:34 -060087 * Print the whole list of available access flags.
88 */
89void env_flags_print_varaccess(void);
90/*
Joe Hershbergerfffad712012-12-11 22:16:33 -060091 * Return the name of the type.
92 */
93const char *env_flags_get_vartype_name(enum env_flags_vartype type);
Joe Hershberger267541f2012-12-11 22:16:34 -060094/*
95 * Return the name of the access.
96 */
97const char *env_flags_get_varaccess_name(enum env_flags_varaccess access);
Joe Hershbergerfffad712012-12-11 22:16:33 -060098#endif
99
Joe Hershberger25980902012-12-11 22:16:31 -0600100/*
101 * Parse the flags string from a .flags attribute list into the vartype enum.
102 */
103enum env_flags_vartype env_flags_parse_vartype(const char *flags);
Joe Hershberger267541f2012-12-11 22:16:34 -0600104/*
105 * Parse the flags string from a .flags attribute list into the varaccess enum.
106 */
107enum env_flags_varaccess env_flags_parse_varaccess(const char *flags);
108/*
109 * Parse the binary flags from a hash table entry into the varaccess enum.
110 */
111enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags);
Joe Hershberger25980902012-12-11 22:16:31 -0600112
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600113#ifdef USE_HOSTCC
114/*
115 * Look up the type of a variable directly from the .flags var.
116 */
117enum env_flags_vartype env_flags_get_type(const char *name);
118/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600119 * Look up the access of a variable directly from the .flags var.
120 */
121enum env_flags_varaccess env_flags_get_access(const char *name);
122/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600123 * Validate the newval for its type to conform with the requirements defined by
124 * its flags (directly looked at the .flags var).
125 */
126int env_flags_validate_type(const char *name, const char *newval);
127/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600128 * Validate the newval for its access to conform with the requirements defined
129 * by its flags (directly looked at the .flags var).
130 */
131int env_flags_validate_access(const char *name, int check_mask);
132/*
133 * Validate that the proposed access to variable "name" is valid according to
134 * the defined flags for that variable, if any.
135 */
136int env_flags_validate_varaccess(const char *name, int check_mask);
137/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600138 * Validate the parameters passed to "env set" for type compliance
139 */
140int env_flags_validate_env_set_params(int argc, char * const argv[]);
141
142#else /* !USE_HOSTCC */
143
Joe Hershberger25980902012-12-11 22:16:31 -0600144#include <search.h>
145
146/*
147 * When adding a variable to the environment, initialize the flags for that
148 * variable.
149 */
150void env_flags_init(ENTRY *var_entry);
151
152/*
153 * Validate the newval for to conform with the requirements defined by its flags
154 */
155int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
156 int flag);
157
Joe Hershberger267541f2012-12-11 22:16:34 -0600158#endif /* USE_HOSTCC */
159
Joe Hershberger25980902012-12-11 22:16:31 -0600160/*
161 * These are the binary flags used in the environment entry->flags variable to
162 * decribe properties of veriables in the table
163 */
Joe Hershberger267541f2012-12-11 22:16:34 -0600164#define ENV_FLAGS_VARTYPE_BIN_MASK 0x00000007
Joe Hershberger25980902012-12-11 22:16:31 -0600165/* The actual variable type values use the enum value (within the mask) */
Joe Hershberger267541f2012-12-11 22:16:34 -0600166#define ENV_FLAGS_VARACCESS_PREVENT_DELETE 0x00000008
167#define ENV_FLAGS_VARACCESS_PREVENT_CREATE 0x00000010
168#define ENV_FLAGS_VARACCESS_PREVENT_OVERWR 0x00000020
169#define ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR 0x00000040
170#define ENV_FLAGS_VARACCESS_BIN_MASK 0x00000078
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600171
Joe Hershberger25980902012-12-11 22:16:31 -0600172#endif /* __ENV_FLAGS_H__ */