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