Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 |
| 3 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef __ENV_FLAGS_H__ |
| 9 | #define __ENV_FLAGS_H__ |
| 10 | |
| 11 | enum 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 Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 23 | enum 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 Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 31 | #define ENV_FLAGS_VAR ".flags" |
| 32 | #define ENV_FLAGS_ATTR_MAX_LEN 2 |
| 33 | #define ENV_FLAGS_VARTYPE_LOC 0 |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 34 | #define ENV_FLAGS_VARACCESS_LOC 1 |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 35 | |
| 36 | #ifndef CONFIG_ENV_FLAGS_LIST_STATIC |
| 37 | #define CONFIG_ENV_FLAGS_LIST_STATIC "" |
| 38 | #endif |
| 39 | |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 40 | #ifdef CONFIG_CMD_NET |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 41 | #ifdef CONFIG_REGEX |
| 42 | #define ETHADDR_WILDCARD "\\d?" |
| 43 | #else |
| 44 | #define ETHADDR_WILDCARD |
| 45 | #endif |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 46 | #ifdef CONFIG_ENV_OVERWRITE |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 47 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:ma," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 48 | #else |
| 49 | #ifdef CONFIG_OVERWRITE_ETHADDR_ONCE |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 50 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mc," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 51 | #else |
Joe Hershberger | e84ca29 | 2015-05-20 14:27:22 -0500 | [diff] [blame] | 52 | #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mo," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 53 | #endif |
| 54 | #endif |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 55 | #define NET_FLAGS \ |
| 56 | "ipaddr:i," \ |
| 57 | "gatewayip:i," \ |
| 58 | "netmask:i," \ |
| 59 | "serverip:i," \ |
| 60 | "nvlan:i," \ |
| 61 | "vlan:i," \ |
| 62 | "dnsip:i," |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 63 | #else |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 64 | #define ETHADDR_FLAGS |
| 65 | #define NET_FLAGS |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #ifndef CONFIG_ENV_OVERWRITE |
| 69 | #define SERIAL_FLAGS "serial#:so," |
| 70 | #else |
| 71 | #define SERIAL_FLAGS "" |
| 72 | #endif |
| 73 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 74 | #define ENV_FLAGS_LIST_STATIC \ |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 75 | ETHADDR_FLAGS \ |
Joe Hershberger | d26195e7 | 2015-05-20 14:27:24 -0500 | [diff] [blame] | 76 | NET_FLAGS \ |
Joe Hershberger | 2289ec1 | 2012-12-11 22:16:37 -0600 | [diff] [blame] | 77 | SERIAL_FLAGS \ |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 78 | CONFIG_ENV_FLAGS_LIST_STATIC |
| 79 | |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 80 | #ifdef CONFIG_CMD_ENV_FLAGS |
| 81 | /* |
| 82 | * Print the whole list of available type flags. |
| 83 | */ |
| 84 | void env_flags_print_vartypes(void); |
| 85 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 86 | * Print the whole list of available access flags. |
| 87 | */ |
| 88 | void env_flags_print_varaccess(void); |
| 89 | /* |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 90 | * Return the name of the type. |
| 91 | */ |
| 92 | const char *env_flags_get_vartype_name(enum env_flags_vartype type); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 93 | /* |
| 94 | * Return the name of the access. |
| 95 | */ |
| 96 | const char *env_flags_get_varaccess_name(enum env_flags_varaccess access); |
Joe Hershberger | a2d62b7 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 97 | #endif |
| 98 | |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 99 | /* |
| 100 | * Parse the flags string from a .flags attribute list into the vartype enum. |
| 101 | */ |
| 102 | enum env_flags_vartype env_flags_parse_vartype(const char *flags); |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 103 | /* |
| 104 | * Parse the flags string from a .flags attribute list into the varaccess enum. |
| 105 | */ |
| 106 | enum env_flags_varaccess env_flags_parse_varaccess(const char *flags); |
| 107 | /* |
| 108 | * Parse the binary flags from a hash table entry into the varaccess enum. |
| 109 | */ |
| 110 | enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags); |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 111 | |
Codrin Ciubotariu | 265c0d3 | 2015-09-09 18:00:51 +0300 | [diff] [blame] | 112 | #ifdef CONFIG_CMD_NET |
| 113 | /* |
| 114 | * Check if a string has the format of an Ethernet MAC address |
| 115 | */ |
| 116 | int eth_validate_ethaddr_str(const char *addr); |
| 117 | #endif |
| 118 | |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 119 | #ifdef USE_HOSTCC |
| 120 | /* |
| 121 | * Look up the type of a variable directly from the .flags var. |
| 122 | */ |
| 123 | enum env_flags_vartype env_flags_get_type(const char *name); |
| 124 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 125 | * Look up the access of a variable directly from the .flags var. |
| 126 | */ |
| 127 | enum env_flags_varaccess env_flags_get_access(const char *name); |
| 128 | /* |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 129 | * Validate the newval for its type to conform with the requirements defined by |
| 130 | * its flags (directly looked at the .flags var). |
| 131 | */ |
| 132 | int env_flags_validate_type(const char *name, const char *newval); |
| 133 | /* |
Joe Hershberger | 6fe26c9 | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 134 | * Validate the newval for its access to conform with the requirements defined |
| 135 | * by its flags (directly looked at the .flags var). |
| 136 | */ |
| 137 | int env_flags_validate_access(const char *name, int check_mask); |
| 138 | /* |
| 139 | * Validate that the proposed access to variable "name" is valid according to |
| 140 | * the defined flags for that variable, if any. |
| 141 | */ |
| 142 | int env_flags_validate_varaccess(const char *name, int check_mask); |
| 143 | /* |
Joe Hershberger | dd7750a | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 144 | * Validate the parameters passed to "env set" for type compliance |
| 145 | */ |
Andreas Fenkart | 4e336ee | 2015-12-09 13:13:21 +0100 | [diff] [blame] | 146 | 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] | 147 | |
| 148 | #else /* !USE_HOSTCC */ |
| 149 | |
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 | */ |
| 156 | void env_flags_init(ENTRY *var_entry); |
| 157 | |
| 158 | /* |
| 159 | * Validate the newval for to conform with the requirements defined by its flags |
| 160 | */ |
| 161 | int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op, |
| 162 | int flag); |
| 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__ */ |