env_flags.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2012
  4. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  5. */
  6. #ifndef __ENV_FLAGS_H__
  7. #define __ENV_FLAGS_H__
  8. enum env_flags_vartype {
  9. env_flags_vartype_string,
  10. env_flags_vartype_decimal,
  11. env_flags_vartype_hex,
  12. env_flags_vartype_bool,
  13. #ifdef CONFIG_CMD_NET
  14. env_flags_vartype_ipaddr,
  15. env_flags_vartype_macaddr,
  16. #endif
  17. env_flags_vartype_end
  18. };
  19. enum env_flags_varaccess {
  20. env_flags_varaccess_any,
  21. env_flags_varaccess_readonly,
  22. env_flags_varaccess_writeonce,
  23. env_flags_varaccess_changedefault,
  24. #ifdef CONFIG_ENV_WRITEABLE_LIST
  25. env_flags_varaccess_writeable,
  26. #endif
  27. env_flags_varaccess_end
  28. };
  29. #define ENV_FLAGS_VAR ".flags"
  30. #define ENV_FLAGS_ATTR_MAX_LEN 2
  31. #define ENV_FLAGS_VARTYPE_LOC 0
  32. #define ENV_FLAGS_VARACCESS_LOC 1
  33. #ifndef CONFIG_ENV_FLAGS_LIST_STATIC
  34. #define CONFIG_ENV_FLAGS_LIST_STATIC ""
  35. #endif
  36. #ifdef CONFIG_NET
  37. #ifdef CONFIG_REGEX
  38. #define ETHADDR_WILDCARD "\\d*"
  39. #else
  40. #define ETHADDR_WILDCARD
  41. #endif
  42. #ifdef CONFIG_ENV_OVERWRITE
  43. #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:ma,"
  44. #else
  45. #ifdef CONFIG_OVERWRITE_ETHADDR_ONCE
  46. #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mc,"
  47. #else
  48. #define ETHADDR_FLAGS "eth" ETHADDR_WILDCARD "addr:mo,"
  49. #endif
  50. #endif
  51. #define NET_FLAGS \
  52. "ipaddr:i," \
  53. "gatewayip:i," \
  54. "netmask:i," \
  55. "serverip:i," \
  56. "nvlan:d," \
  57. "vlan:d," \
  58. "dnsip:i,"
  59. #else
  60. #define ETHADDR_FLAGS
  61. #define NET_FLAGS
  62. #endif
  63. #ifndef CONFIG_ENV_OVERWRITE
  64. #define SERIAL_FLAGS "serial#:so,"
  65. #else
  66. #define SERIAL_FLAGS ""
  67. #endif
  68. #define ENV_FLAGS_LIST_STATIC \
  69. ETHADDR_FLAGS \
  70. NET_FLAGS \
  71. SERIAL_FLAGS \
  72. CONFIG_ENV_FLAGS_LIST_STATIC
  73. #ifdef CONFIG_CMD_ENV_FLAGS
  74. /*
  75. * Print the whole list of available type flags.
  76. */
  77. void env_flags_print_vartypes(void);
  78. /*
  79. * Print the whole list of available access flags.
  80. */
  81. void env_flags_print_varaccess(void);
  82. /*
  83. * Return the name of the type.
  84. */
  85. const char *env_flags_get_vartype_name(enum env_flags_vartype type);
  86. /*
  87. * Return the name of the access.
  88. */
  89. const char *env_flags_get_varaccess_name(enum env_flags_varaccess access);
  90. #endif
  91. /*
  92. * Parse the flags string from a .flags attribute list into the vartype enum.
  93. */
  94. enum env_flags_vartype env_flags_parse_vartype(const char *flags);
  95. /*
  96. * Parse the flags string from a .flags attribute list into the varaccess enum.
  97. */
  98. enum env_flags_varaccess env_flags_parse_varaccess(const char *flags);
  99. /*
  100. * Parse the binary flags from a hash table entry into the varaccess enum.
  101. */
  102. enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags);
  103. #ifdef CONFIG_CMD_NET
  104. /*
  105. * Check if a string has the format of an Ethernet MAC address
  106. */
  107. int eth_validate_ethaddr_str(const char *addr);
  108. #endif
  109. #ifdef USE_HOSTCC
  110. /*
  111. * Look up the type of a variable directly from the .flags var.
  112. */
  113. enum env_flags_vartype env_flags_get_type(const char *name);
  114. /*
  115. * Look up the access of a variable directly from the .flags var.
  116. */
  117. enum env_flags_varaccess env_flags_get_access(const char *name);
  118. /*
  119. * Validate the newval for its type to conform with the requirements defined by
  120. * its flags (directly looked at the .flags var).
  121. */
  122. int env_flags_validate_type(const char *name, const char *newval);
  123. /*
  124. * Validate the newval for its access to conform with the requirements defined
  125. * by its flags (directly looked at the .flags var).
  126. */
  127. int env_flags_validate_access(const char *name, int check_mask);
  128. /*
  129. * Validate that the proposed access to variable "name" is valid according to
  130. * the defined flags for that variable, if any.
  131. */
  132. int env_flags_validate_varaccess(const char *name, int check_mask);
  133. /*
  134. * Validate the parameters passed to "env set" for type compliance
  135. */
  136. int env_flags_validate_env_set_params(char *name, char *const val[], int count);
  137. #else /* !USE_HOSTCC */
  138. #include <env.h>
  139. #include <search.h>
  140. /*
  141. * When adding a variable to the environment, initialize the flags for that
  142. * variable.
  143. */
  144. void env_flags_init(struct env_entry *var_entry);
  145. /*
  146. * Validate the newval for to conform with the requirements defined by its flags
  147. */
  148. int env_flags_validate(const struct env_entry *item, const char *newval,
  149. enum env_op op, int flag);
  150. #endif /* USE_HOSTCC */
  151. /*
  152. * These are the binary flags used in the environment entry->flags variable to
  153. * decribe properties of veriables in the table
  154. */
  155. #define ENV_FLAGS_VARTYPE_BIN_MASK 0x00000007
  156. /* The actual variable type values use the enum value (within the mask) */
  157. #define ENV_FLAGS_VARACCESS_PREVENT_DELETE 0x00000008
  158. #define ENV_FLAGS_VARACCESS_PREVENT_CREATE 0x00000010
  159. #define ENV_FLAGS_VARACCESS_PREVENT_OVERWR 0x00000020
  160. #define ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR 0x00000040
  161. #define ENV_FLAGS_VARACCESS_WRITEABLE 0x00000080
  162. #define ENV_FLAGS_VARACCESS_BIN_MASK 0x000000f8
  163. #endif /* __ENV_FLAGS_H__ */