Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Joe Hershberger | 2598090 | 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 | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __ENV_FLAGS_H__ |
| 8 | #define __ENV_FLAGS_H__ |
| 9 | |
| 10 | enum 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_CMD_NET |
| 16 | env_flags_vartype_ipaddr, |
| 17 | env_flags_vartype_macaddr, |
| 18 | #endif |
| 19 | env_flags_vartype_end |
| 20 | }; |
| 21 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 22 | enum env_flags_varaccess { |
| 23 | env_flags_varaccess_any, |
| 24 | env_flags_varaccess_readonly, |
| 25 | env_flags_varaccess_writeonce, |
| 26 | env_flags_varaccess_changedefault, |
Marek Vasut | d045cba | 2020-07-07 20:51:39 +0200 | [diff] [blame] | 27 | #ifdef CONFIG_ENV_WRITEABLE_LIST |
| 28 | env_flags_varaccess_writeable, |
| 29 | #endif |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 30 | env_flags_varaccess_end |
| 31 | }; |
| 32 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 33 | #define ENV_FLAGS_VAR ".flags" |
| 34 | #define ENV_FLAGS_ATTR_MAX_LEN 2 |
| 35 | #define ENV_FLAGS_VARTYPE_LOC 0 |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 36 | #define ENV_FLAGS_VARACCESS_LOC 1 |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 37 | |
| 38 | #ifndef CONFIG_ENV_FLAGS_LIST_STATIC |
| 39 | #define CONFIG_ENV_FLAGS_LIST_STATIC "" |
| 40 | #endif |
| 41 | |
Heinrich Schuchardt | cccc05e | 2019-09-02 10:10:34 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_NET |
Joe Hershberger | 73c2bbe | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 43 | #ifdef CONFIG_REGEX |
Simon Goldschmidt | be09f5b | 2018-11-22 17:06:39 +0100 | [diff] [blame] | 44 | #define ETHADDR_WILDCARD "\\d*" |
Joe Hershberger | 73c2bbe | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 45 | #else |
| 46 | #define ETHADDR_WILDCARD |
| 47 | #endif |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 48 | #ifdef CONFIG_ENV_OVERWRITE |
Joe Hershberger | 73c2bbe | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 49 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:ma," |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 50 | #else |
| 51 | #ifdef CONFIG_OVERWRITE_ETHADDR_ONCE |
Joe Hershberger | 73c2bbe | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 52 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mc," |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 53 | #else |
Joe Hershberger | 73c2bbe | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 54 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mo," |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 55 | #endif |
| 56 | #endif |
Joe Hershberger | c0a9344 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 57 | #define NET_FLAGS \ |
| 58 | "ipaddr:i," \ |
| 59 | "gatewayip:i," \ |
| 60 | "netmask:i," \ |
| 61 | "serverip:i," \ |
Stefan Agner | 0299cee | 2016-04-13 16:38:01 -0700 | [diff] [blame] | 62 | "nvlan:d," \ |
| 63 | "vlan:d," \ |
Joe Hershberger | c0a9344 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 64 | "dnsip:i," |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 65 | #else |
Joe Hershberger | c0a9344 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 66 | #define ETHADDR_FLAGS |
| 67 | #define NET_FLAGS |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | #ifndef CONFIG_ENV_OVERWRITE |
| 71 | #define SERIAL_FLAGS "serial#:so," |
| 72 | #else |
| 73 | #define SERIAL_FLAGS "" |
| 74 | #endif |
| 75 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 76 | #define ENV_FLAGS_LIST_STATIC \ |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 77 | ETHADDR_FLAGS \ |
Joe Hershberger | c0a9344 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 78 | NET_FLAGS \ |
Joe Hershberger | 1d6cd0a | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 79 | SERIAL_FLAGS \ |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 80 | CONFIG_ENV_FLAGS_LIST_STATIC |
| 81 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 82 | #ifdef CONFIG_CMD_ENV_FLAGS |
| 83 | /* |
| 84 | * Print the whole list of available type flags. |
| 85 | */ |
| 86 | void env_flags_print_vartypes(void); |
| 87 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 88 | * Print the whole list of available access flags. |
| 89 | */ |
| 90 | void env_flags_print_varaccess(void); |
| 91 | /* |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 92 | * Return the name of the type. |
| 93 | */ |
| 94 | const char *env_flags_get_vartype_name(enum env_flags_vartype type); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 95 | /* |
| 96 | * Return the name of the access. |
| 97 | */ |
| 98 | const char *env_flags_get_varaccess_name(enum env_flags_varaccess access); |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 99 | #endif |
| 100 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 101 | /* |
| 102 | * Parse the flags string from a .flags attribute list into the vartype enum. |
| 103 | */ |
| 104 | enum env_flags_vartype env_flags_parse_vartype(const char *flags); |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 105 | /* |
| 106 | * Parse the flags string from a .flags attribute list into the varaccess enum. |
| 107 | */ |
| 108 | enum env_flags_varaccess env_flags_parse_varaccess(const char *flags); |
| 109 | /* |
| 110 | * Parse the binary flags from a hash table entry into the varaccess enum. |
| 111 | */ |
| 112 | enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags); |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 113 | |
Codrin Ciubotariu | 0118e83 | 2015-09-09 18:00:51 +0300 | [diff] [blame] | 114 | #ifdef CONFIG_CMD_NET |
| 115 | /* |
| 116 | * Check if a string has the format of an Ethernet MAC address |
| 117 | */ |
| 118 | int eth_validate_ethaddr_str(const char *addr); |
| 119 | #endif |
| 120 | |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 121 | #ifdef USE_HOSTCC |
| 122 | /* |
| 123 | * Look up the type of a variable directly from the .flags var. |
| 124 | */ |
| 125 | enum env_flags_vartype env_flags_get_type(const char *name); |
| 126 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 127 | * Look up the access of a variable directly from the .flags var. |
| 128 | */ |
| 129 | enum env_flags_varaccess env_flags_get_access(const char *name); |
| 130 | /* |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 131 | * Validate the newval for its type to conform with the requirements defined by |
| 132 | * its flags (directly looked at the .flags var). |
| 133 | */ |
| 134 | int env_flags_validate_type(const char *name, const char *newval); |
| 135 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 136 | * Validate the newval for its access to conform with the requirements defined |
| 137 | * by its flags (directly looked at the .flags var). |
| 138 | */ |
| 139 | int env_flags_validate_access(const char *name, int check_mask); |
| 140 | /* |
| 141 | * Validate that the proposed access to variable "name" is valid according to |
| 142 | * the defined flags for that variable, if any. |
| 143 | */ |
| 144 | int env_flags_validate_varaccess(const char *name, int check_mask); |
| 145 | /* |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 146 | * Validate the parameters passed to "env set" for type compliance |
| 147 | */ |
Andreas Fenkart | 167f525 | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 148 | int env_flags_validate_env_set_params(char *name, char *const val[], int count); |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 149 | |
| 150 | #else /* !USE_HOSTCC */ |
| 151 | |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 152 | #include <env.h> |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 153 | #include <search.h> |
| 154 | |
| 155 | /* |
| 156 | * When adding a variable to the environment, initialize the flags for that |
| 157 | * variable. |
| 158 | */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 159 | void env_flags_init(struct env_entry *var_entry); |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Validate the newval for to conform with the requirements defined by its flags |
| 163 | */ |
Simon Glass | dd2408c | 2019-08-02 09:44:18 -0600 | [diff] [blame] | 164 | int env_flags_validate(const struct env_entry *item, const char *newval, |
| 165 | enum env_op op, int flag); |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 166 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 167 | #endif /* USE_HOSTCC */ |
| 168 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 169 | /* |
| 170 | * These are the binary flags used in the environment entry->flags variable to |
| 171 | * decribe properties of veriables in the table |
| 172 | */ |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 173 | #define ENV_FLAGS_VARTYPE_BIN_MASK 0x00000007 |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 174 | /* The actual variable type values use the enum value (within the mask) */ |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 175 | #define ENV_FLAGS_VARACCESS_PREVENT_DELETE 0x00000008 |
| 176 | #define ENV_FLAGS_VARACCESS_PREVENT_CREATE 0x00000010 |
| 177 | #define ENV_FLAGS_VARACCESS_PREVENT_OVERWR 0x00000020 |
| 178 | #define ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR 0x00000040 |
Marek Vasut | d045cba | 2020-07-07 20:51:39 +0200 | [diff] [blame] | 179 | #define ENV_FLAGS_VARACCESS_WRITEABLE 0x00000080 |
| 180 | #define ENV_FLAGS_VARACCESS_BIN_MASK 0x000000f8 |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 181 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 182 | #endif /* __ENV_FLAGS_H__ */ |