env_flags.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * (C) Copyright 2012
  3. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <linux/string.h>
  24. #include <linux/ctype.h>
  25. #include <common.h>
  26. #include <environment.h>
  27. #ifdef CONFIG_CMD_NET
  28. #define ENV_FLAGS_NET_VARTYPE_REPS "im"
  29. #else
  30. #define ENV_FLAGS_NET_VARTYPE_REPS ""
  31. #endif
  32. static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
  33. /*
  34. * Parse the flags string from a .flags attribute list into the vartype enum.
  35. */
  36. enum env_flags_vartype env_flags_parse_vartype(const char *flags)
  37. {
  38. char *type;
  39. if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
  40. return env_flags_vartype_string;
  41. type = strchr(env_flags_vartype_rep,
  42. flags[ENV_FLAGS_VARTYPE_LOC]);
  43. if (type != NULL)
  44. return (enum env_flags_vartype)
  45. (type - &env_flags_vartype_rep[0]);
  46. printf("## Warning: Unknown environment variable type '%c'\n",
  47. flags[ENV_FLAGS_VARTYPE_LOC]);
  48. return env_flags_vartype_string;
  49. }
  50. static inline int is_hex_prefix(const char *value)
  51. {
  52. return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
  53. }
  54. static void skip_num(int hex, const char *value, const char **end,
  55. int max_digits)
  56. {
  57. int i;
  58. if (hex && is_hex_prefix(value))
  59. value += 2;
  60. for (i = max_digits; i != 0; i--) {
  61. if (hex && !isxdigit(*value))
  62. break;
  63. if (!hex && !isdigit(*value))
  64. break;
  65. value++;
  66. }
  67. if (end != NULL)
  68. *end = value;
  69. }
  70. /*
  71. * Based on the declared type enum, validate that the value string complies
  72. * with that format
  73. */
  74. static int _env_flags_validate_type(const char *value,
  75. enum env_flags_vartype type)
  76. {
  77. const char *end;
  78. #ifdef CONFIG_CMD_NET
  79. const char *cur;
  80. int i;
  81. #endif
  82. switch (type) {
  83. case env_flags_vartype_string:
  84. break;
  85. case env_flags_vartype_decimal:
  86. skip_num(0, value, &end, -1);
  87. if (*end != '\0')
  88. return -1;
  89. break;
  90. case env_flags_vartype_hex:
  91. skip_num(1, value, &end, -1);
  92. if (*end != '\0')
  93. return -1;
  94. if (value + 2 == end && is_hex_prefix(value))
  95. return -1;
  96. break;
  97. case env_flags_vartype_bool:
  98. if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
  99. value[0] != 'Y' && value[0] != 'T' &&
  100. value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
  101. value[0] != 'N' && value[0] != 'F')
  102. return -1;
  103. if (value[1] != '\0')
  104. return -1;
  105. break;
  106. #ifdef CONFIG_CMD_NET
  107. case env_flags_vartype_ipaddr:
  108. cur = value;
  109. for (i = 0; i < 4; i++) {
  110. skip_num(0, cur, &end, 3);
  111. if (cur == end)
  112. return -1;
  113. if (i != 3 && *end != '.')
  114. return -1;
  115. if (i == 3 && *end != '\0')
  116. return -1;
  117. cur = end + 1;
  118. }
  119. break;
  120. case env_flags_vartype_macaddr:
  121. cur = value;
  122. for (i = 0; i < 6; i++) {
  123. skip_num(1, cur, &end, 2);
  124. if (cur == end)
  125. return -1;
  126. if (cur + 2 == end && is_hex_prefix(cur))
  127. return -1;
  128. if (i != 5 && *end != ':')
  129. return -1;
  130. if (i == 5 && *end != '\0')
  131. return -1;
  132. cur = end + 1;
  133. }
  134. break;
  135. #endif
  136. case env_flags_vartype_end:
  137. return -1;
  138. }
  139. /* OK */
  140. return 0;
  141. }
  142. /*
  143. * Look for flags in a provided list and failing that the static list
  144. */
  145. static inline int env_flags_lookup(const char *flags_list, const char *name,
  146. char *flags)
  147. {
  148. int ret = 1;
  149. if (!flags)
  150. /* bad parameter */
  151. return -1;
  152. /* try the env first */
  153. if (flags_list)
  154. ret = env_attr_lookup(flags_list, name, flags);
  155. if (ret != 0)
  156. /* if not found in the env, look in the static list */
  157. ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
  158. return ret;
  159. }
  160. /*
  161. * Parse the flag charachters from the .flags attribute list into the binary
  162. * form to be stored in the environment entry->flags field.
  163. */
  164. static int env_parse_flags_to_bin(const char *flags)
  165. {
  166. return env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
  167. }
  168. /*
  169. * Look for possible flags for a newly added variable
  170. * This is called specifically when the variable did not exist in the hash
  171. * previously, so the blanket update did not find this variable.
  172. */
  173. void env_flags_init(ENTRY *var_entry)
  174. {
  175. const char *var_name = var_entry->key;
  176. const char *flags_list = getenv(ENV_FLAGS_VAR);
  177. char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
  178. int ret = 1;
  179. /* look in the ".flags" and static for a reference to this variable */
  180. ret = env_flags_lookup(flags_list, var_name, flags);
  181. /* if any flags were found, set the binary form to the entry */
  182. if (!ret && strlen(flags))
  183. var_entry->flags = env_parse_flags_to_bin(flags);
  184. }
  185. /*
  186. * Called on each existing env var prior to the blanket update since removing
  187. * a flag in the flag list should remove its flags.
  188. */
  189. static int clear_flags(ENTRY *entry)
  190. {
  191. entry->flags = 0;
  192. return 0;
  193. }
  194. /*
  195. * Call for each element in the list that defines flags for a variable
  196. */
  197. static int set_flags(const char *name, const char *value)
  198. {
  199. ENTRY e, *ep;
  200. e.key = name;
  201. e.data = NULL;
  202. hsearch_r(e, FIND, &ep, &env_htab, 0);
  203. /* does the env variable actually exist? */
  204. if (ep != NULL) {
  205. /* the flag list is empty, so clear the flags */
  206. if (value == NULL || strlen(value) == 0)
  207. ep->flags = 0;
  208. else
  209. /* assign the requested flags */
  210. ep->flags = env_parse_flags_to_bin(value);
  211. }
  212. return 0;
  213. }
  214. static int on_flags(const char *name, const char *value, enum env_op op,
  215. int flags)
  216. {
  217. /* remove all flags */
  218. hwalk_r(&env_htab, clear_flags);
  219. /* configure any static flags */
  220. env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
  221. /* configure any dynamic flags */
  222. env_attr_walk(value, set_flags);
  223. return 0;
  224. }
  225. U_BOOT_ENV_CALLBACK(flags, on_flags);
  226. /*
  227. * Perform consistency checking before creating, overwriting, or deleting an
  228. * environment variable. Called as a callback function by hsearch_r() and
  229. * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
  230. * When (flag & H_FORCE) is set, do not print out any error message and force
  231. * overwriting of write-once variables.
  232. */
  233. int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
  234. int flag)
  235. {
  236. const char *name;
  237. #if !defined(CONFIG_ENV_OVERWRITE) && defined(CONFIG_OVERWRITE_ETHADDR_ONCE) \
  238. && defined(CONFIG_ETHADDR)
  239. const char *oldval = NULL;
  240. if (op != env_op_create)
  241. oldval = item->data;
  242. #endif
  243. name = item->key;
  244. /* Default value for NULL to protect string-manipulating functions */
  245. newval = newval ? : "";
  246. #ifndef CONFIG_ENV_OVERWRITE
  247. /*
  248. * Some variables like "ethaddr" and "serial#" can be set only once and
  249. * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
  250. */
  251. if (op != env_op_create && /* variable exists */
  252. (flag & H_FORCE) == 0) { /* and we are not forced */
  253. if (strcmp(name, "serial#") == 0 ||
  254. (strcmp(name, "ethaddr") == 0
  255. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  256. && strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0
  257. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  258. )) {
  259. printf("Can't overwrite \"%s\"\n", name);
  260. return 1;
  261. }
  262. }
  263. #endif
  264. /* validate the value to match the variable type */
  265. if (op != env_op_delete) {
  266. enum env_flags_vartype type = (enum env_flags_vartype)
  267. (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
  268. if (_env_flags_validate_type(newval, type) < 0) {
  269. printf("## Error: flags type check failure for "
  270. "\"%s\" <= \"%s\" (type: %c)\n",
  271. name, newval, env_flags_vartype_rep[type]);
  272. return -1;
  273. }
  274. }
  275. return 0;
  276. }