env_flags.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include "fw_env.h"
  29. #include <env_attr.h>
  30. #include <env_flags.h>
  31. #define getenv fw_getenv
  32. #else
  33. #include <common.h>
  34. #include <environment.h>
  35. #endif
  36. #ifdef CONFIG_CMD_NET
  37. #define ENV_FLAGS_NET_VARTYPE_REPS "im"
  38. #else
  39. #define ENV_FLAGS_NET_VARTYPE_REPS ""
  40. #endif
  41. static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
  42. /*
  43. * Parse the flags string from a .flags attribute list into the vartype enum.
  44. */
  45. enum env_flags_vartype env_flags_parse_vartype(const char *flags)
  46. {
  47. char *type;
  48. if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
  49. return env_flags_vartype_string;
  50. type = strchr(env_flags_vartype_rep,
  51. flags[ENV_FLAGS_VARTYPE_LOC]);
  52. if (type != NULL)
  53. return (enum env_flags_vartype)
  54. (type - &env_flags_vartype_rep[0]);
  55. printf("## Warning: Unknown environment variable type '%c'\n",
  56. flags[ENV_FLAGS_VARTYPE_LOC]);
  57. return env_flags_vartype_string;
  58. }
  59. static inline int is_hex_prefix(const char *value)
  60. {
  61. return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
  62. }
  63. static void skip_num(int hex, const char *value, const char **end,
  64. int max_digits)
  65. {
  66. int i;
  67. if (hex && is_hex_prefix(value))
  68. value += 2;
  69. for (i = max_digits; i != 0; i--) {
  70. if (hex && !isxdigit(*value))
  71. break;
  72. if (!hex && !isdigit(*value))
  73. break;
  74. value++;
  75. }
  76. if (end != NULL)
  77. *end = value;
  78. }
  79. /*
  80. * Based on the declared type enum, validate that the value string complies
  81. * with that format
  82. */
  83. static int _env_flags_validate_type(const char *value,
  84. enum env_flags_vartype type)
  85. {
  86. const char *end;
  87. #ifdef CONFIG_CMD_NET
  88. const char *cur;
  89. int i;
  90. #endif
  91. switch (type) {
  92. case env_flags_vartype_string:
  93. break;
  94. case env_flags_vartype_decimal:
  95. skip_num(0, value, &end, -1);
  96. if (*end != '\0')
  97. return -1;
  98. break;
  99. case env_flags_vartype_hex:
  100. skip_num(1, value, &end, -1);
  101. if (*end != '\0')
  102. return -1;
  103. if (value + 2 == end && is_hex_prefix(value))
  104. return -1;
  105. break;
  106. case env_flags_vartype_bool:
  107. if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
  108. value[0] != 'Y' && value[0] != 'T' &&
  109. value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
  110. value[0] != 'N' && value[0] != 'F')
  111. return -1;
  112. if (value[1] != '\0')
  113. return -1;
  114. break;
  115. #ifdef CONFIG_CMD_NET
  116. case env_flags_vartype_ipaddr:
  117. cur = value;
  118. for (i = 0; i < 4; i++) {
  119. skip_num(0, cur, &end, 3);
  120. if (cur == end)
  121. return -1;
  122. if (i != 3 && *end != '.')
  123. return -1;
  124. if (i == 3 && *end != '\0')
  125. return -1;
  126. cur = end + 1;
  127. }
  128. break;
  129. case env_flags_vartype_macaddr:
  130. cur = value;
  131. for (i = 0; i < 6; i++) {
  132. skip_num(1, cur, &end, 2);
  133. if (cur == end)
  134. return -1;
  135. if (cur + 2 == end && is_hex_prefix(cur))
  136. return -1;
  137. if (i != 5 && *end != ':')
  138. return -1;
  139. if (i == 5 && *end != '\0')
  140. return -1;
  141. cur = end + 1;
  142. }
  143. break;
  144. #endif
  145. case env_flags_vartype_end:
  146. return -1;
  147. }
  148. /* OK */
  149. return 0;
  150. }
  151. /*
  152. * Look for flags in a provided list and failing that the static list
  153. */
  154. static inline int env_flags_lookup(const char *flags_list, const char *name,
  155. char *flags)
  156. {
  157. int ret = 1;
  158. if (!flags)
  159. /* bad parameter */
  160. return -1;
  161. /* try the env first */
  162. if (flags_list)
  163. ret = env_attr_lookup(flags_list, name, flags);
  164. if (ret != 0)
  165. /* if not found in the env, look in the static list */
  166. ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
  167. return ret;
  168. }
  169. #ifdef USE_HOSTCC /* Functions only used from tools/env */
  170. /*
  171. * Look up any flags directly from the .flags variable and the static list
  172. * and convert them to the vartype enum.
  173. */
  174. enum env_flags_vartype env_flags_get_type(const char *name)
  175. {
  176. const char *flags_list = getenv(ENV_FLAGS_VAR);
  177. char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
  178. if (env_flags_lookup(flags_list, name, flags))
  179. return env_flags_vartype_string;
  180. if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
  181. return env_flags_vartype_string;
  182. return env_flags_parse_vartype(flags);
  183. }
  184. /*
  185. * Validate that the proposed new value for "name" is valid according to the
  186. * defined flags for that variable, if any.
  187. */
  188. int env_flags_validate_type(const char *name, const char *value)
  189. {
  190. enum env_flags_vartype type;
  191. if (value == NULL)
  192. return 0;
  193. type = env_flags_get_type(name);
  194. if (_env_flags_validate_type(value, type) < 0) {
  195. printf("## Error: flags type check failure for "
  196. "\"%s\" <= \"%s\" (type: %c)\n",
  197. name, value, env_flags_vartype_rep[type]);
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. /*
  203. * Validate the parameters to "env set" directly
  204. */
  205. int env_flags_validate_env_set_params(int argc, char * const argv[])
  206. {
  207. if ((argc >= 3) && argv[2] != NULL) {
  208. enum env_flags_vartype type = env_flags_get_type(argv[1]);
  209. /*
  210. * we don't currently check types that need more than
  211. * one argument
  212. */
  213. if (type != env_flags_vartype_string && argc > 3) {
  214. printf("## Error: too many parameters for setting "
  215. "\"%s\"\n", argv[1]);
  216. return -1;
  217. }
  218. return env_flags_validate_type(argv[1], argv[2]);
  219. }
  220. /* ok */
  221. return 0;
  222. }
  223. #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
  224. /*
  225. * Parse the flag charachters from the .flags attribute list into the binary
  226. * form to be stored in the environment entry->flags field.
  227. */
  228. static int env_parse_flags_to_bin(const char *flags)
  229. {
  230. return env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
  231. }
  232. /*
  233. * Look for possible flags for a newly added variable
  234. * This is called specifically when the variable did not exist in the hash
  235. * previously, so the blanket update did not find this variable.
  236. */
  237. void env_flags_init(ENTRY *var_entry)
  238. {
  239. const char *var_name = var_entry->key;
  240. const char *flags_list = getenv(ENV_FLAGS_VAR);
  241. char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
  242. int ret = 1;
  243. /* look in the ".flags" and static for a reference to this variable */
  244. ret = env_flags_lookup(flags_list, var_name, flags);
  245. /* if any flags were found, set the binary form to the entry */
  246. if (!ret && strlen(flags))
  247. var_entry->flags = env_parse_flags_to_bin(flags);
  248. }
  249. /*
  250. * Called on each existing env var prior to the blanket update since removing
  251. * a flag in the flag list should remove its flags.
  252. */
  253. static int clear_flags(ENTRY *entry)
  254. {
  255. entry->flags = 0;
  256. return 0;
  257. }
  258. /*
  259. * Call for each element in the list that defines flags for a variable
  260. */
  261. static int set_flags(const char *name, const char *value)
  262. {
  263. ENTRY e, *ep;
  264. e.key = name;
  265. e.data = NULL;
  266. hsearch_r(e, FIND, &ep, &env_htab, 0);
  267. /* does the env variable actually exist? */
  268. if (ep != NULL) {
  269. /* the flag list is empty, so clear the flags */
  270. if (value == NULL || strlen(value) == 0)
  271. ep->flags = 0;
  272. else
  273. /* assign the requested flags */
  274. ep->flags = env_parse_flags_to_bin(value);
  275. }
  276. return 0;
  277. }
  278. static int on_flags(const char *name, const char *value, enum env_op op,
  279. int flags)
  280. {
  281. /* remove all flags */
  282. hwalk_r(&env_htab, clear_flags);
  283. /* configure any static flags */
  284. env_attr_walk(ENV_FLAGS_LIST_STATIC, set_flags);
  285. /* configure any dynamic flags */
  286. env_attr_walk(value, set_flags);
  287. return 0;
  288. }
  289. U_BOOT_ENV_CALLBACK(flags, on_flags);
  290. /*
  291. * Perform consistency checking before creating, overwriting, or deleting an
  292. * environment variable. Called as a callback function by hsearch_r() and
  293. * hdelete_r(). Returns 0 in case of success, 1 in case of failure.
  294. * When (flag & H_FORCE) is set, do not print out any error message and force
  295. * overwriting of write-once variables.
  296. */
  297. int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op,
  298. int flag)
  299. {
  300. const char *name;
  301. #if !defined(CONFIG_ENV_OVERWRITE) && defined(CONFIG_OVERWRITE_ETHADDR_ONCE) \
  302. && defined(CONFIG_ETHADDR)
  303. const char *oldval = NULL;
  304. if (op != env_op_create)
  305. oldval = item->data;
  306. #endif
  307. name = item->key;
  308. /* Default value for NULL to protect string-manipulating functions */
  309. newval = newval ? : "";
  310. #ifndef CONFIG_ENV_OVERWRITE
  311. /*
  312. * Some variables like "ethaddr" and "serial#" can be set only once and
  313. * cannot be deleted, unless CONFIG_ENV_OVERWRITE is defined.
  314. */
  315. if (op != env_op_create && /* variable exists */
  316. (flag & H_FORCE) == 0) { /* and we are not forced */
  317. if (strcmp(name, "serial#") == 0 ||
  318. (strcmp(name, "ethaddr") == 0
  319. #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
  320. && strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0
  321. #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
  322. )) {
  323. printf("Can't overwrite \"%s\"\n", name);
  324. return 1;
  325. }
  326. }
  327. #endif
  328. /* validate the value to match the variable type */
  329. if (op != env_op_delete) {
  330. enum env_flags_vartype type = (enum env_flags_vartype)
  331. (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags);
  332. if (_env_flags_validate_type(newval, type) < 0) {
  333. printf("## Error: flags type check failure for "
  334. "\"%s\" <= \"%s\" (type: %c)\n",
  335. name, newval, env_flags_vartype_rep[type]);
  336. return -1;
  337. }
  338. }
  339. return 0;
  340. }
  341. #endif