1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2012
4  * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5  */
6 
7 #ifndef __ENV_FLAGS_H__
8 #define __ENV_FLAGS_H__
9 
10 #include <config.h>
11 
12 enum env_flags_vartype {
13 	env_flags_vartype_string,
14 	env_flags_vartype_decimal,
15 	env_flags_vartype_hex,
16 	env_flags_vartype_bool,
17 #if CONFIG_IS_ENABLED(NET) || CONFIG_IS_ENABLED(NET_LWIP)
18 	env_flags_vartype_ipaddr,
19 	env_flags_vartype_macaddr,
20 #endif
21 	env_flags_vartype_end
22 };
23 
24 enum env_flags_varaccess {
25 	env_flags_varaccess_any,
26 	env_flags_varaccess_readonly,
27 	env_flags_varaccess_writeonce,
28 	env_flags_varaccess_changedefault,
29 #ifdef CONFIG_ENV_WRITEABLE_LIST
30 	env_flags_varaccess_writeable,
31 #endif
32 	env_flags_varaccess_end
33 };
34 
35 #define ENV_FLAGS_VAR ".flags"
36 #define ENV_FLAGS_ATTR_MAX_LEN 2
37 #define ENV_FLAGS_VARTYPE_LOC 0
38 #define ENV_FLAGS_VARACCESS_LOC 1
39 
40 #ifndef CFG_ENV_FLAGS_LIST_STATIC
41 #define CFG_ENV_FLAGS_LIST_STATIC ""
42 #endif
43 
44 #if CONFIG_IS_ENABLED(NET) || CONFIG_IS_ENABLED(NET_LWIP)
45 #ifdef CONFIG_REGEX
46 #define ETHADDR_WILDCARD "\\d*"
47 #else
48 #define ETHADDR_WILDCARD
49 #endif
50 #ifdef CONFIG_ENV_OVERWRITE
51 #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:ma,"
52 #else
53 #ifdef CONFIG_ENV_OVERWRITE_ETHADDR_ONCE
54 #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mc,"
55 #else
56 #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mo,"
57 #endif
58 #endif
59 #define NET_FLAGS \
60 	"ipaddr:i," \
61 	"gatewayip:i," \
62 	"netmask:i," \
63 	"serverip:i," \
64 	"nvlan:d," \
65 	"vlan:d," \
66 	"dnsip:i,"
67 #else
68 #define ETHADDR_FLAGS
69 #define NET_FLAGS
70 #endif
71 
72 #ifdef CONFIG_IPV6
73 #define NET6_FLAGS \
74 	"ip6addr:s," \
75 	"serverip6:s," \
76 	"gatewayip6:s,"
77 #else
78 #define NET6_FLAGS
79 #endif
80 
81 #ifndef CONFIG_ENV_OVERWRITE
82 #define SERIAL_FLAGS "serial#:so,"
83 #else
84 #define SERIAL_FLAGS ""
85 #endif
86 
87 #define ENV_FLAGS_LIST_STATIC \
88 	ETHADDR_FLAGS \
89 	NET_FLAGS \
90 	NET6_FLAGS \
91 	SERIAL_FLAGS \
92 	CFG_ENV_FLAGS_LIST_STATIC
93 
94 #ifdef CONFIG_CMD_ENV_FLAGS
95 /*
96  * Print the whole list of available type flags.
97  */
98 void env_flags_print_vartypes(void);
99 /*
100  * Print the whole list of available access flags.
101  */
102 void env_flags_print_varaccess(void);
103 /*
104  * Return the name of the type.
105  */
106 const char *env_flags_get_vartype_name(enum env_flags_vartype type);
107 /*
108  * Return the name of the access.
109  */
110 const char *env_flags_get_varaccess_name(enum env_flags_varaccess access);
111 #endif
112 
113 /*
114  * Parse the flags string from a .flags attribute list into the vartype enum.
115  */
116 enum env_flags_vartype env_flags_parse_vartype(const char *flags);
117 /*
118  * Parse the flags string from a .flags attribute list into the varaccess enum.
119  */
120 enum env_flags_varaccess env_flags_parse_varaccess(const char *flags);
121 /*
122  * Parse the binary flags from a hash table entry into the varaccess enum.
123  */
124 enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags);
125 
126 #if CONFIG_IS_ENABLED(NET) || CONFIG_IS_ENABLED(NET_LWIP)
127 /*
128  * Check if a string has the format of an Ethernet MAC address
129  */
130 int eth_validate_ethaddr_str(const char *addr);
131 #endif
132 
133 #ifdef USE_HOSTCC
134 /*
135  * Look up the type of a variable directly from the .flags var.
136  */
137 enum env_flags_vartype env_flags_get_type(const char *name);
138 /*
139  * Look up the access of a variable directly from the .flags var.
140  */
141 enum env_flags_varaccess env_flags_get_access(const char *name);
142 /*
143  * Validate the newval for its type to conform with the requirements defined by
144  * its flags (directly looked at the .flags var).
145  */
146 int env_flags_validate_type(const char *name, const char *newval);
147 /*
148  * Validate the newval for its access to conform with the requirements defined
149  * by its flags (directly looked at the .flags var).
150  */
151 int env_flags_validate_access(const char *name, int check_mask);
152 /*
153  * Validate that the proposed access to variable "name" is valid according to
154  * the defined flags for that variable, if any.
155  */
156 int env_flags_validate_varaccess(const char *name, int check_mask);
157 /*
158  * Validate the parameters passed to "env set" for type compliance
159  */
160 int env_flags_validate_env_set_params(char *name, char *const val[], int count);
161 
162 #else /* !USE_HOSTCC */
163 
164 #include <env.h>
165 #include <search.h>
166 
167 /*
168  * When adding a variable to the environment, initialize the flags for that
169  * variable.
170  */
171 void env_flags_init(struct env_entry *var_entry);
172 
173 /*
174  * Validate the newval for to conform with the requirements defined by its flags
175  */
176 int env_flags_validate(const struct env_entry *item, const char *newval,
177 		       enum env_op op, int flag);
178 
179 #endif /* USE_HOSTCC */
180 
181 /*
182  * These are the binary flags used in the environment entry->flags variable to
183  * decribe properties of veriables in the table
184  */
185 #define ENV_FLAGS_VARTYPE_BIN_MASK			0x00000007
186 /* The actual variable type values use the enum value (within the mask) */
187 #define ENV_FLAGS_VARACCESS_PREVENT_DELETE		0x00000008
188 #define ENV_FLAGS_VARACCESS_PREVENT_CREATE		0x00000010
189 #define ENV_FLAGS_VARACCESS_PREVENT_OVERWR		0x00000020
190 #define ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR	0x00000040
191 #define ENV_FLAGS_VARACCESS_WRITEABLE			0x00000080
192 #define ENV_FLAGS_VARACCESS_BIN_MASK			0x000000f8
193 
194 #endif /* __ENV_FLAGS_H__ */
195