Joe Hershberger | 2598090 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/string.h> |
| 9 | #include <linux/ctype.h> |
| 10 | |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 11 | #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */ |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include "fw_env.h" |
| 15 | #include <env_attr.h> |
| 16 | #include <env_flags.h> |
| 17 | #define getenv fw_getenv |
| 18 | #else |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 19 | #include <common.h> |
| 20 | #include <environment.h> |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 21 | #endif |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_CMD_NET |
| 24 | #define ENV_FLAGS_NET_VARTYPE_REPS "im" |
| 25 | #else |
| 26 | #define ENV_FLAGS_NET_VARTYPE_REPS "" |
| 27 | #endif |
| 28 | |
| 29 | static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 30 | static const char env_flags_varaccess_rep[] = "aroc"; |
| 31 | static const int env_flags_varaccess_mask[] = { |
| 32 | 0, |
| 33 | ENV_FLAGS_VARACCESS_PREVENT_DELETE | |
| 34 | ENV_FLAGS_VARACCESS_PREVENT_CREATE | |
| 35 | ENV_FLAGS_VARACCESS_PREVENT_OVERWR, |
| 36 | ENV_FLAGS_VARACCESS_PREVENT_DELETE | |
| 37 | ENV_FLAGS_VARACCESS_PREVENT_OVERWR, |
| 38 | ENV_FLAGS_VARACCESS_PREVENT_DELETE | |
| 39 | ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR}; |
| 40 | |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 41 | #ifdef CONFIG_CMD_ENV_FLAGS |
| 42 | static const char * const env_flags_vartype_names[] = { |
| 43 | "string", |
| 44 | "decimal", |
| 45 | "hexadecimal", |
| 46 | "boolean", |
| 47 | #ifdef CONFIG_CMD_NET |
| 48 | "IP address", |
| 49 | "MAC address", |
| 50 | #endif |
| 51 | }; |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 52 | static const char * const env_flags_varaccess_names[] = { |
| 53 | "any", |
| 54 | "read-only", |
| 55 | "write-once", |
| 56 | "change-default", |
| 57 | }; |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Print the whole list of available type flags. |
| 61 | */ |
| 62 | void env_flags_print_vartypes(void) |
| 63 | { |
| 64 | enum env_flags_vartype curtype = (enum env_flags_vartype)0; |
| 65 | |
| 66 | while (curtype != env_flags_vartype_end) { |
| 67 | printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype], |
| 68 | env_flags_vartype_names[curtype]); |
| 69 | curtype++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 74 | * Print the whole list of available access flags. |
| 75 | */ |
| 76 | void env_flags_print_varaccess(void) |
| 77 | { |
| 78 | enum env_flags_varaccess curaccess = (enum env_flags_varaccess)0; |
| 79 | |
| 80 | while (curaccess != env_flags_varaccess_end) { |
| 81 | printf("\t%c -\t%s\n", env_flags_varaccess_rep[curaccess], |
| 82 | env_flags_varaccess_names[curaccess]); |
| 83 | curaccess++; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 88 | * Return the name of the type. |
| 89 | */ |
| 90 | const char *env_flags_get_vartype_name(enum env_flags_vartype type) |
| 91 | { |
| 92 | return env_flags_vartype_names[type]; |
| 93 | } |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Return the name of the access. |
| 97 | */ |
| 98 | const char *env_flags_get_varaccess_name(enum env_flags_varaccess access) |
| 99 | { |
| 100 | return env_flags_varaccess_names[access]; |
| 101 | } |
Joe Hershberger | fffad71 | 2012-12-11 22:16:33 -0600 | [diff] [blame] | 102 | #endif /* CONFIG_CMD_ENV_FLAGS */ |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Parse the flags string from a .flags attribute list into the vartype enum. |
| 106 | */ |
| 107 | enum env_flags_vartype env_flags_parse_vartype(const char *flags) |
| 108 | { |
| 109 | char *type; |
| 110 | |
| 111 | if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC) |
| 112 | return env_flags_vartype_string; |
| 113 | |
| 114 | type = strchr(env_flags_vartype_rep, |
| 115 | flags[ENV_FLAGS_VARTYPE_LOC]); |
| 116 | |
| 117 | if (type != NULL) |
| 118 | return (enum env_flags_vartype) |
| 119 | (type - &env_flags_vartype_rep[0]); |
| 120 | |
| 121 | printf("## Warning: Unknown environment variable type '%c'\n", |
| 122 | flags[ENV_FLAGS_VARTYPE_LOC]); |
| 123 | return env_flags_vartype_string; |
| 124 | } |
| 125 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 126 | /* |
| 127 | * Parse the flags string from a .flags attribute list into the varaccess enum. |
| 128 | */ |
| 129 | enum env_flags_varaccess env_flags_parse_varaccess(const char *flags) |
| 130 | { |
| 131 | char *access; |
| 132 | |
| 133 | if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC) |
| 134 | return env_flags_varaccess_any; |
| 135 | |
| 136 | access = strchr(env_flags_varaccess_rep, |
| 137 | flags[ENV_FLAGS_VARACCESS_LOC]); |
| 138 | |
| 139 | if (access != NULL) |
| 140 | return (enum env_flags_varaccess) |
| 141 | (access - &env_flags_varaccess_rep[0]); |
| 142 | |
| 143 | printf("## Warning: Unknown environment variable access method '%c'\n", |
| 144 | flags[ENV_FLAGS_VARACCESS_LOC]); |
| 145 | return env_flags_varaccess_any; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Parse the binary flags from a hash table entry into the varaccess enum. |
| 150 | */ |
| 151 | enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | for (i = 0; i < sizeof(env_flags_varaccess_mask); i++) |
| 156 | if (env_flags_varaccess_mask[i] == |
| 157 | (binflags & ENV_FLAGS_VARACCESS_BIN_MASK)) |
| 158 | return (enum env_flags_varaccess)i; |
| 159 | |
| 160 | printf("Warning: Non-standard access flags. (0x%x)\n", |
| 161 | binflags & ENV_FLAGS_VARACCESS_BIN_MASK); |
| 162 | |
| 163 | return env_flags_varaccess_any; |
| 164 | } |
| 165 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 166 | static inline int is_hex_prefix(const char *value) |
| 167 | { |
| 168 | return value[0] == '0' && (value[1] == 'x' || value[1] == 'X'); |
| 169 | } |
| 170 | |
| 171 | static void skip_num(int hex, const char *value, const char **end, |
| 172 | int max_digits) |
| 173 | { |
| 174 | int i; |
| 175 | |
| 176 | if (hex && is_hex_prefix(value)) |
| 177 | value += 2; |
| 178 | |
| 179 | for (i = max_digits; i != 0; i--) { |
| 180 | if (hex && !isxdigit(*value)) |
| 181 | break; |
| 182 | if (!hex && !isdigit(*value)) |
| 183 | break; |
| 184 | value++; |
| 185 | } |
| 186 | if (end != NULL) |
| 187 | *end = value; |
| 188 | } |
| 189 | |
Codrin Ciubotariu | 0118e83 | 2015-09-09 18:00:51 +0300 | [diff] [blame] | 190 | #ifdef CONFIG_CMD_NET |
| 191 | int eth_validate_ethaddr_str(const char *addr) |
| 192 | { |
| 193 | const char *end; |
| 194 | const char *cur; |
| 195 | int i; |
| 196 | |
| 197 | cur = addr; |
| 198 | for (i = 0; i < 6; i++) { |
| 199 | skip_num(1, cur, &end, 2); |
| 200 | if (cur == end) |
| 201 | return -1; |
| 202 | if (cur + 2 == end && is_hex_prefix(cur)) |
| 203 | return -1; |
| 204 | if (i != 5 && *end != ':') |
| 205 | return -1; |
| 206 | if (i == 5 && *end != '\0') |
| 207 | return -1; |
| 208 | cur = end + 1; |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | #endif |
| 214 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 215 | /* |
| 216 | * Based on the declared type enum, validate that the value string complies |
| 217 | * with that format |
| 218 | */ |
| 219 | static int _env_flags_validate_type(const char *value, |
| 220 | enum env_flags_vartype type) |
| 221 | { |
| 222 | const char *end; |
| 223 | #ifdef CONFIG_CMD_NET |
| 224 | const char *cur; |
| 225 | int i; |
| 226 | #endif |
| 227 | |
| 228 | switch (type) { |
| 229 | case env_flags_vartype_string: |
| 230 | break; |
| 231 | case env_flags_vartype_decimal: |
| 232 | skip_num(0, value, &end, -1); |
| 233 | if (*end != '\0') |
| 234 | return -1; |
| 235 | break; |
| 236 | case env_flags_vartype_hex: |
| 237 | skip_num(1, value, &end, -1); |
| 238 | if (*end != '\0') |
| 239 | return -1; |
| 240 | if (value + 2 == end && is_hex_prefix(value)) |
| 241 | return -1; |
| 242 | break; |
| 243 | case env_flags_vartype_bool: |
| 244 | if (value[0] != '1' && value[0] != 'y' && value[0] != 't' && |
| 245 | value[0] != 'Y' && value[0] != 'T' && |
| 246 | value[0] != '0' && value[0] != 'n' && value[0] != 'f' && |
| 247 | value[0] != 'N' && value[0] != 'F') |
| 248 | return -1; |
| 249 | if (value[1] != '\0') |
| 250 | return -1; |
| 251 | break; |
| 252 | #ifdef CONFIG_CMD_NET |
| 253 | case env_flags_vartype_ipaddr: |
| 254 | cur = value; |
| 255 | for (i = 0; i < 4; i++) { |
| 256 | skip_num(0, cur, &end, 3); |
| 257 | if (cur == end) |
| 258 | return -1; |
| 259 | if (i != 3 && *end != '.') |
| 260 | return -1; |
| 261 | if (i == 3 && *end != '\0') |
| 262 | return -1; |
| 263 | cur = end + 1; |
| 264 | } |
| 265 | break; |
| 266 | case env_flags_vartype_macaddr: |
Codrin Ciubotariu | 0118e83 | 2015-09-09 18:00:51 +0300 | [diff] [blame] | 267 | if (eth_validate_ethaddr_str(value)) |
| 268 | return -1; |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 269 | break; |
| 270 | #endif |
| 271 | case env_flags_vartype_end: |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | /* OK */ |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Look for flags in a provided list and failing that the static list |
| 281 | */ |
| 282 | static inline int env_flags_lookup(const char *flags_list, const char *name, |
| 283 | char *flags) |
| 284 | { |
| 285 | int ret = 1; |
| 286 | |
| 287 | if (!flags) |
| 288 | /* bad parameter */ |
| 289 | return -1; |
| 290 | |
| 291 | /* try the env first */ |
| 292 | if (flags_list) |
| 293 | ret = env_attr_lookup(flags_list, name, flags); |
| 294 | |
| 295 | if (ret != 0) |
| 296 | /* if not found in the env, look in the static list */ |
| 297 | ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags); |
| 298 | |
| 299 | return ret; |
| 300 | } |
| 301 | |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 302 | #ifdef USE_HOSTCC /* Functions only used from tools/env */ |
| 303 | /* |
| 304 | * Look up any flags directly from the .flags variable and the static list |
| 305 | * and convert them to the vartype enum. |
| 306 | */ |
| 307 | enum env_flags_vartype env_flags_get_type(const char *name) |
| 308 | { |
| 309 | const char *flags_list = getenv(ENV_FLAGS_VAR); |
| 310 | char flags[ENV_FLAGS_ATTR_MAX_LEN + 1]; |
| 311 | |
| 312 | if (env_flags_lookup(flags_list, name, flags)) |
| 313 | return env_flags_vartype_string; |
| 314 | |
| 315 | if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC) |
| 316 | return env_flags_vartype_string; |
| 317 | |
| 318 | return env_flags_parse_vartype(flags); |
| 319 | } |
| 320 | |
| 321 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 322 | * Look up the access of a variable directly from the .flags var. |
| 323 | */ |
| 324 | enum env_flags_varaccess env_flags_get_varaccess(const char *name) |
| 325 | { |
| 326 | const char *flags_list = getenv(ENV_FLAGS_VAR); |
| 327 | char flags[ENV_FLAGS_ATTR_MAX_LEN + 1]; |
| 328 | |
| 329 | if (env_flags_lookup(flags_list, name, flags)) |
| 330 | return env_flags_varaccess_any; |
| 331 | |
| 332 | if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC) |
| 333 | return env_flags_varaccess_any; |
| 334 | |
| 335 | return env_flags_parse_varaccess(flags); |
| 336 | } |
| 337 | |
| 338 | /* |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 339 | * Validate that the proposed new value for "name" is valid according to the |
| 340 | * defined flags for that variable, if any. |
| 341 | */ |
| 342 | int env_flags_validate_type(const char *name, const char *value) |
| 343 | { |
| 344 | enum env_flags_vartype type; |
| 345 | |
| 346 | if (value == NULL) |
| 347 | return 0; |
| 348 | type = env_flags_get_type(name); |
| 349 | if (_env_flags_validate_type(value, type) < 0) { |
| 350 | printf("## Error: flags type check failure for " |
| 351 | "\"%s\" <= \"%s\" (type: %c)\n", |
| 352 | name, value, env_flags_vartype_rep[type]); |
| 353 | return -1; |
| 354 | } |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 359 | * Validate that the proposed access to variable "name" is valid according to |
| 360 | * the defined flags for that variable, if any. |
| 361 | */ |
| 362 | int env_flags_validate_varaccess(const char *name, int check_mask) |
| 363 | { |
| 364 | enum env_flags_varaccess access; |
| 365 | int access_mask; |
| 366 | |
| 367 | access = env_flags_get_varaccess(name); |
| 368 | access_mask = env_flags_varaccess_mask[access]; |
| 369 | |
| 370 | return (check_mask & access_mask) != 0; |
| 371 | } |
| 372 | |
| 373 | /* |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 374 | * Validate the parameters to "env set" directly |
| 375 | */ |
| 376 | int env_flags_validate_env_set_params(int argc, char * const argv[]) |
| 377 | { |
| 378 | if ((argc >= 3) && argv[2] != NULL) { |
| 379 | enum env_flags_vartype type = env_flags_get_type(argv[1]); |
| 380 | |
| 381 | /* |
| 382 | * we don't currently check types that need more than |
| 383 | * one argument |
| 384 | */ |
| 385 | if (type != env_flags_vartype_string && argc > 3) { |
| 386 | printf("## Error: too many parameters for setting " |
| 387 | "\"%s\"\n", argv[1]); |
| 388 | return -1; |
| 389 | } |
| 390 | return env_flags_validate_type(argv[1], argv[2]); |
| 391 | } |
| 392 | /* ok */ |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */ |
| 397 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 398 | /* |
| 399 | * Parse the flag charachters from the .flags attribute list into the binary |
| 400 | * form to be stored in the environment entry->flags field. |
| 401 | */ |
| 402 | static int env_parse_flags_to_bin(const char *flags) |
| 403 | { |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 404 | int binflags; |
| 405 | |
| 406 | binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK; |
| 407 | binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)]; |
| 408 | |
| 409 | return binflags; |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 410 | } |
| 411 | |
Heiko Schocher | 1b61027 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 412 | static int first_call = 1; |
| 413 | static const char *flags_list; |
| 414 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 415 | /* |
| 416 | * Look for possible flags for a newly added variable |
| 417 | * This is called specifically when the variable did not exist in the hash |
| 418 | * previously, so the blanket update did not find this variable. |
| 419 | */ |
| 420 | void env_flags_init(ENTRY *var_entry) |
| 421 | { |
| 422 | const char *var_name = var_entry->key; |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 423 | char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = ""; |
| 424 | int ret = 1; |
| 425 | |
Heiko Schocher | 1b61027 | 2013-12-19 13:45:04 +0100 | [diff] [blame] | 426 | if (first_call) { |
| 427 | flags_list = getenv(ENV_FLAGS_VAR); |
| 428 | first_call = 0; |
| 429 | } |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 430 | /* look in the ".flags" and static for a reference to this variable */ |
| 431 | ret = env_flags_lookup(flags_list, var_name, flags); |
| 432 | |
| 433 | /* if any flags were found, set the binary form to the entry */ |
| 434 | if (!ret && strlen(flags)) |
| 435 | var_entry->flags = env_parse_flags_to_bin(flags); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Called on each existing env var prior to the blanket update since removing |
| 440 | * a flag in the flag list should remove its flags. |
| 441 | */ |
| 442 | static int clear_flags(ENTRY *entry) |
| 443 | { |
| 444 | entry->flags = 0; |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * Call for each element in the list that defines flags for a variable |
| 451 | */ |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 452 | static int set_flags(const char *name, const char *value, void *priv) |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 453 | { |
| 454 | ENTRY e, *ep; |
| 455 | |
| 456 | e.key = name; |
| 457 | e.data = NULL; |
Peng Fan | 5a68943 | 2015-12-23 12:07:24 +0800 | [diff] [blame] | 458 | e.callback = NULL; |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 459 | hsearch_r(e, FIND, &ep, &env_htab, 0); |
| 460 | |
| 461 | /* does the env variable actually exist? */ |
| 462 | if (ep != NULL) { |
| 463 | /* the flag list is empty, so clear the flags */ |
| 464 | if (value == NULL || strlen(value) == 0) |
| 465 | ep->flags = 0; |
| 466 | else |
| 467 | /* assign the requested flags */ |
| 468 | ep->flags = env_parse_flags_to_bin(value); |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int on_flags(const char *name, const char *value, enum env_op op, |
| 475 | int flags) |
| 476 | { |
| 477 | /* remove all flags */ |
| 478 | hwalk_r(&env_htab, clear_flags); |
| 479 | |
| 480 | /* configure any static flags */ |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 481 | env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags, NULL); |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 482 | /* configure any dynamic flags */ |
Joe Hershberger | cca98fd | 2015-05-20 14:27:19 -0500 | [diff] [blame] | 483 | env_attr_walk(value, set_flags, NULL); |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | U_BOOT_ENV_CALLBACK(flags, on_flags); |
| 488 | |
| 489 | /* |
| 490 | * Perform consistency checking before creating, overwriting, or deleting an |
| 491 | * environment variable. Called as a callback function by hsearch_r() and |
| 492 | * hdelete_r(). Returns 0 in case of success, 1 in case of failure. |
| 493 | * When (flag & H_FORCE) is set, do not print out any error message and force |
| 494 | * overwriting of write-once variables. |
| 495 | */ |
| 496 | |
| 497 | int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op, |
| 498 | int flag) |
| 499 | { |
| 500 | const char *name; |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 501 | const char *oldval = NULL; |
| 502 | |
| 503 | if (op != env_op_create) |
| 504 | oldval = item->data; |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 505 | |
| 506 | name = item->key; |
| 507 | |
| 508 | /* Default value for NULL to protect string-manipulating functions */ |
| 509 | newval = newval ? : ""; |
| 510 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 511 | /* validate the value to match the variable type */ |
| 512 | if (op != env_op_delete) { |
| 513 | enum env_flags_vartype type = (enum env_flags_vartype) |
| 514 | (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags); |
| 515 | |
| 516 | if (_env_flags_validate_type(newval, type) < 0) { |
| 517 | printf("## Error: flags type check failure for " |
| 518 | "\"%s\" <= \"%s\" (type: %c)\n", |
| 519 | name, newval, env_flags_vartype_rep[type]); |
| 520 | return -1; |
| 521 | } |
| 522 | } |
| 523 | |
Joe Hershberger | 267541f | 2012-12-11 22:16:34 -0600 | [diff] [blame] | 524 | /* check for access permission */ |
| 525 | #ifndef CONFIG_ENV_ACCESS_IGNORE_FORCE |
| 526 | if (flag & H_FORCE) |
| 527 | return 0; |
| 528 | #endif |
| 529 | switch (op) { |
| 530 | case env_op_delete: |
| 531 | if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) { |
| 532 | printf("## Error: Can't delete \"%s\"\n", name); |
| 533 | return 1; |
| 534 | } |
| 535 | break; |
| 536 | case env_op_overwrite: |
| 537 | if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) { |
| 538 | printf("## Error: Can't overwrite \"%s\"\n", name); |
| 539 | return 1; |
| 540 | } else if (item->flags & |
| 541 | ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) { |
| 542 | const char *defval = getenv_default(name); |
| 543 | |
| 544 | if (defval == NULL) |
| 545 | defval = ""; |
| 546 | printf("oldval: %s defval: %s\n", oldval, defval); |
| 547 | if (strcmp(oldval, defval) != 0) { |
| 548 | printf("## Error: Can't overwrite \"%s\"\n", |
| 549 | name); |
| 550 | return 1; |
| 551 | } |
| 552 | } |
| 553 | break; |
| 554 | case env_op_create: |
| 555 | if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) { |
| 556 | printf("## Error: Can't create \"%s\"\n", name); |
| 557 | return 1; |
| 558 | } |
| 559 | break; |
| 560 | } |
| 561 | |
Joe Hershberger | 2598090 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 562 | return 0; |
| 563 | } |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 564 | |
| 565 | #endif |