Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __ENV_FLAGS_H__ |
| 8 | #define __ENV_FLAGS_H__ |
| 9 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 10 | #include <config.h> |
| 11 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 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, |
Jan Kiszka | 59b328d | 2023-02-03 13:22:52 +0100 | [diff] [blame] | 17 | #ifdef CONFIG_NET |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 18 | env_flags_vartype_ipaddr, |
| 19 | env_flags_vartype_macaddr, |
| 20 | #endif |
| 21 | env_flags_vartype_end |
| 22 | }; |
| 23 | |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 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, |
Marek Vasut | 803549f | 2020-07-07 20:51:39 +0200 | [diff] [blame] | 29 | #ifdef CONFIG_ENV_WRITEABLE_LIST |
| 30 | env_flags_varaccess_writeable, |
| 31 | #endif |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 32 | env_flags_varaccess_end |
| 33 | }; |
| 34 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 35 | #define ENV_FLAGS_VAR ".flags" |
| 36 | #define ENV_FLAGS_ATTR_MAX_LEN 2 |
| 37 | #define ENV_FLAGS_VARTYPE_LOC 0 |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 38 | #define ENV_FLAGS_VARACCESS_LOC 1 |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 39 | |
Tom Rini | 0297e5f | 2022-12-04 10:03:40 -0500 | [diff] [blame] | 40 | #ifndef CFG_ENV_FLAGS_LIST_STATIC |
| 41 | #define CFG_ENV_FLAGS_LIST_STATIC "" |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 42 | #endif |
| 43 | |
Heinrich Schuchardt | fe04d06 | 2019-09-02 10:10:34 +0200 | [diff] [blame] | 44 | #ifdef CONFIG_NET |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 45 | #ifdef CONFIG_REGEX |
Simon Goldschmidt | 7da0229 | 2018-11-22 17:06:39 +0100 | [diff] [blame] | 46 | #define ETHADDR_WILDCARD "\\d*" |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 47 | #else |
| 48 | #define ETHADDR_WILDCARD |
| 49 | #endif |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 50 | #ifdef CONFIG_ENV_OVERWRITE |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 51 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:ma," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 52 | #else |
| 53 | #ifdef CONFIG_OVERWRITE_ETHADDR_ONCE |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 54 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mc," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 55 | #else |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 56 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mo," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 57 | #endif |
| 58 | #endif |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 59 | #define NET_FLAGS \ |
| 60 | "ipaddr:i," \ |
| 61 | "gatewayip:i," \ |
| 62 | "netmask:i," \ |
| 63 | "serverip:i," \ |
Stefan Agner | 18c067f | 2016-04-13 16:38:01 -0700 | [diff] [blame] | 64 | "nvlan:d," \ |
| 65 | "vlan:d," \ |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 66 | "dnsip:i," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 67 | #else |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 68 | #define ETHADDR_FLAGS |
| 69 | #define NET_FLAGS |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 70 | #endif |
| 71 | |
Viacheslav Mitrofanov | df264cf | 2022-12-02 12:18:00 +0300 | [diff] [blame] | 72 | #ifdef CONFIG_IPV6 |
| 73 | #define NET6_FLAGS \ |
| 74 | "ip6addr:s," \ |
| 75 | "serverip6:s," \ |
Sean Edmond | 65815e7 | 2023-02-15 20:38:36 -0800 | [diff] [blame] | 76 | "gatewayip6:s," |
Viacheslav Mitrofanov | df264cf | 2022-12-02 12:18:00 +0300 | [diff] [blame] | 77 | #else |
| 78 | #define NET6_FLAGS |
| 79 | #endif |
| 80 | |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 81 | #ifndef CONFIG_ENV_OVERWRITE |
| 82 | #define SERIAL_FLAGS "serial#:so," |
| 83 | #else |
| 84 | #define SERIAL_FLAGS "" |
| 85 | #endif |
| 86 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 87 | #define ENV_FLAGS_LIST_STATIC \ |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 88 | ETHADDR_FLAGS \ |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 89 | NET_FLAGS \ |
Viacheslav Mitrofanov | df264cf | 2022-12-02 12:18:00 +0300 | [diff] [blame] | 90 | NET6_FLAGS \ |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 91 | SERIAL_FLAGS \ |
Tom Rini | 0297e5f | 2022-12-04 10:03:40 -0500 | [diff] [blame] | 92 | CFG_ENV_FLAGS_LIST_STATIC |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 93 | |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 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 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 100 | * Print the whole list of available access flags. |
| 101 | */ |
| 102 | void env_flags_print_varaccess(void); |
| 103 | /* |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 104 | * Return the name of the type. |
| 105 | */ |
| 106 | const char *env_flags_get_vartype_name(enum env_flags_vartype type); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 107 | /* |
| 108 | * Return the name of the access. |
| 109 | */ |
| 110 | const char *env_flags_get_varaccess_name(enum env_flags_varaccess access); |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 111 | #endif |
| 112 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 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); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 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); |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 125 | |
Jan Kiszka | 59b328d | 2023-02-03 13:22:52 +0100 | [diff] [blame] | 126 | #ifdef CONFIG_NET |
Codrin Ciubotariu | 265c0d3 | 2015-09-09 18:00:51 +0300 | [diff] [blame] | 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 | |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 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 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 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 | /* |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 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 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 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 | /* |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 158 | * Validate the parameters passed to "env set" for type compliance |
| 159 | */ |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 160 | int env_flags_validate_env_set_params(char *name, char *const val[], int count); |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 161 | |
| 162 | #else /* !USE_HOSTCC */ |
| 163 | |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 164 | #include <env.h> |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 165 | #include <search.h> |
| 166 | |
| 167 | /* |
| 168 | * When adding a variable to the environment, initialize the flags for that |
| 169 | * variable. |
| 170 | */ |
Simon Glass | 1a23686 | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 171 | void env_flags_init(struct env_entry *var_entry); |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Validate the newval for to conform with the requirements defined by its flags |
| 175 | */ |
Simon Glass | 1a23686 | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 176 | int env_flags_validate(const struct env_entry *item, const char *newval, |
| 177 | enum env_op op, int flag); |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 178 | |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 179 | #endif /* USE_HOSTCC */ |
| 180 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 181 | /* |
| 182 | * These are the binary flags used in the environment entry->flags variable to |
| 183 | * decribe properties of veriables in the table |
| 184 | */ |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 185 | #define ENV_FLAGS_VARTYPE_BIN_MASK 0x00000007 |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 186 | /* The actual variable type values use the enum value (within the mask) */ |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 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 |
Marek Vasut | 803549f | 2020-07-07 20:51:39 +0200 | [diff] [blame] | 191 | #define ENV_FLAGS_VARACCESS_WRITEABLE 0x00000080 |
| 192 | #define ENV_FLAGS_VARACCESS_BIN_MASK 0x000000f8 |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 193 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 194 | #endif /* __ENV_FLAGS_H__ */ |