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