blob: 3ef631105f13ce40112bc90c1386043b15533120 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Joe Hershberger25980902012-12-11 22:16:31 -06006 */
7
8#ifndef __ENV_FLAGS_H__
9#define __ENV_FLAGS_H__
10
11enum env_flags_vartype {
12 env_flags_vartype_string,
13 env_flags_vartype_decimal,
14 env_flags_vartype_hex,
15 env_flags_vartype_bool,
16#ifdef CONFIG_CMD_NET
17 env_flags_vartype_ipaddr,
18 env_flags_vartype_macaddr,
19#endif
20 env_flags_vartype_end
21};
22
Joe Hershberger267541f2012-12-11 22:16:34 -060023enum env_flags_varaccess {
24 env_flags_varaccess_any,
25 env_flags_varaccess_readonly,
26 env_flags_varaccess_writeonce,
27 env_flags_varaccess_changedefault,
28 env_flags_varaccess_end
29};
30
Joe Hershberger25980902012-12-11 22:16:31 -060031#define ENV_FLAGS_VAR ".flags"
32#define ENV_FLAGS_ATTR_MAX_LEN 2
33#define ENV_FLAGS_VARTYPE_LOC 0
Joe Hershberger267541f2012-12-11 22:16:34 -060034#define ENV_FLAGS_VARACCESS_LOC 1
Joe Hershberger25980902012-12-11 22:16:31 -060035
36#ifndef CONFIG_ENV_FLAGS_LIST_STATIC
37#define CONFIG_ENV_FLAGS_LIST_STATIC ""
38#endif
39
Joe Hershberger1d6cd0a2012-12-11 22:16:37 -060040#ifdef CONFIG_CMD_NET
41#ifdef CONFIG_ENV_OVERWRITE
42#define ETHADDR_FLAGS "ethaddr:ma,"
43#else
44#ifdef CONFIG_OVERWRITE_ETHADDR_ONCE
45#define ETHADDR_FLAGS "ethaddr:mc,"
46#else
47#define ETHADDR_FLAGS "ethaddr:mo,"
48#endif
49#endif
50#else
51#define ETHADDR_FLAGS ""
52#endif
53
54#ifndef CONFIG_ENV_OVERWRITE
55#define SERIAL_FLAGS "serial#:so,"
56#else
57#define SERIAL_FLAGS ""
58#endif
59
Joe Hershberger25980902012-12-11 22:16:31 -060060#define ENV_FLAGS_LIST_STATIC \
Joe Hershberger1d6cd0a2012-12-11 22:16:37 -060061 ETHADDR_FLAGS \
62 SERIAL_FLAGS \
Joe Hershberger25980902012-12-11 22:16:31 -060063 CONFIG_ENV_FLAGS_LIST_STATIC
64
Joe Hershbergerfffad712012-12-11 22:16:33 -060065#ifdef CONFIG_CMD_ENV_FLAGS
66/*
67 * Print the whole list of available type flags.
68 */
69void env_flags_print_vartypes(void);
70/*
Joe Hershberger267541f2012-12-11 22:16:34 -060071 * Print the whole list of available access flags.
72 */
73void env_flags_print_varaccess(void);
74/*
Joe Hershbergerfffad712012-12-11 22:16:33 -060075 * Return the name of the type.
76 */
77const char *env_flags_get_vartype_name(enum env_flags_vartype type);
Joe Hershberger267541f2012-12-11 22:16:34 -060078/*
79 * Return the name of the access.
80 */
81const char *env_flags_get_varaccess_name(enum env_flags_varaccess access);
Joe Hershbergerfffad712012-12-11 22:16:33 -060082#endif
83
Joe Hershberger25980902012-12-11 22:16:31 -060084/*
85 * Parse the flags string from a .flags attribute list into the vartype enum.
86 */
87enum env_flags_vartype env_flags_parse_vartype(const char *flags);
Joe Hershberger267541f2012-12-11 22:16:34 -060088/*
89 * Parse the flags string from a .flags attribute list into the varaccess enum.
90 */
91enum env_flags_varaccess env_flags_parse_varaccess(const char *flags);
92/*
93 * Parse the binary flags from a hash table entry into the varaccess enum.
94 */
95enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags);
Joe Hershberger25980902012-12-11 22:16:31 -060096
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060097#ifdef USE_HOSTCC
98/*
99 * Look up the type of a variable directly from the .flags var.
100 */
101enum env_flags_vartype env_flags_get_type(const char *name);
102/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600103 * Look up the access of a variable directly from the .flags var.
104 */
105enum env_flags_varaccess env_flags_get_access(const char *name);
106/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600107 * Validate the newval for its type to conform with the requirements defined by
108 * its flags (directly looked at the .flags var).
109 */
110int env_flags_validate_type(const char *name, const char *newval);
111/*
Joe Hershberger267541f2012-12-11 22:16:34 -0600112 * Validate the newval for its access to conform with the requirements defined
113 * by its flags (directly looked at the .flags var).
114 */
115int env_flags_validate_access(const char *name, int check_mask);
116/*
117 * Validate that the proposed access to variable "name" is valid according to
118 * the defined flags for that variable, if any.
119 */
120int env_flags_validate_varaccess(const char *name, int check_mask);
121/*
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600122 * Validate the parameters passed to "env set" for type compliance
123 */
124int env_flags_validate_env_set_params(int argc, char * const argv[]);
125
126#else /* !USE_HOSTCC */
127
Joe Hershberger25980902012-12-11 22:16:31 -0600128#include <search.h>
129
130/*
131 * When adding a variable to the environment, initialize the flags for that
132 * variable.
133 */
134void env_flags_init(ENTRY *var_entry);
135
136/*
137 * Validate the newval for to conform with the requirements defined by its flags
138 */
139int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
140 int flag);
141
Joe Hershberger267541f2012-12-11 22:16:34 -0600142#endif /* USE_HOSTCC */
143
Joe Hershberger25980902012-12-11 22:16:31 -0600144/*
145 * These are the binary flags used in the environment entry->flags variable to
146 * decribe properties of veriables in the table
147 */
Joe Hershberger267541f2012-12-11 22:16:34 -0600148#define ENV_FLAGS_VARTYPE_BIN_MASK 0x00000007
Joe Hershberger25980902012-12-11 22:16:31 -0600149/* The actual variable type values use the enum value (within the mask) */
Joe Hershberger267541f2012-12-11 22:16:34 -0600150#define ENV_FLAGS_VARACCESS_PREVENT_DELETE 0x00000008
151#define ENV_FLAGS_VARACCESS_PREVENT_CREATE 0x00000010
152#define ENV_FLAGS_VARACCESS_PREVENT_OVERWR 0x00000020
153#define ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR 0x00000040
154#define ENV_FLAGS_VARACCESS_BIN_MASK 0x00000078
Joe Hershberger30fd4fad2012-12-11 22:16:32 -0600155
Joe Hershberger25980902012-12-11 22:16:31 -0600156#endif /* __ENV_FLAGS_H__ */