env_flags.c 14 KB

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