env_flags.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * (C) Copyright 2012
  3. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <linux/string.h>
  8. #include <linux/ctype.h>
  9. #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include "fw_env.h"
  13. #include <env_attr.h>
  14. #include <env_flags.h>
  15. #define getenv fw_getenv
  16. #else
  17. #include <common.h>
  18. #include <environment.h>
  19. #endif
  20. #ifdef CONFIG_CMD_NET
  21. #define ENV_FLAGS_NET_VARTYPE_REPS "im"
  22. #else
  23. #define ENV_FLAGS_NET_VARTYPE_REPS ""
  24. #endif
  25. static const char env_flags_vartype_rep[] = "sdxb" ENV_FLAGS_NET_VARTYPE_REPS;
  26. static const char env_flags_varaccess_rep[] = "aroc";
  27. static const int env_flags_varaccess_mask[] = {
  28. 0,
  29. ENV_FLAGS_VARACCESS_PREVENT_DELETE |
  30. ENV_FLAGS_VARACCESS_PREVENT_CREATE |
  31. ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
  32. ENV_FLAGS_VARACCESS_PREVENT_DELETE |
  33. ENV_FLAGS_VARACCESS_PREVENT_OVERWR,
  34. ENV_FLAGS_VARACCESS_PREVENT_DELETE |
  35. ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR};
  36. #ifdef CONFIG_CMD_ENV_FLAGS
  37. static const char * const env_flags_vartype_names[] = {
  38. "string",
  39. "decimal",
  40. "hexadecimal",
  41. "boolean",
  42. #ifdef CONFIG_CMD_NET
  43. "IP address",
  44. "MAC address",
  45. #endif
  46. };
  47. static const char * const env_flags_varaccess_names[] = {
  48. "any",
  49. "read-only",
  50. "write-once",
  51. "change-default",
  52. };
  53. /*
  54. * Print the whole list of available type flags.
  55. */
  56. void env_flags_print_vartypes(void)
  57. {
  58. enum env_flags_vartype curtype = (enum env_flags_vartype)0;
  59. while (curtype != env_flags_vartype_end) {
  60. printf("\t%c -\t%s\n", env_flags_vartype_rep[curtype],
  61. env_flags_vartype_names[curtype]);
  62. curtype++;
  63. }
  64. }
  65. /*
  66. * Print the whole list of available access flags.
  67. */
  68. void env_flags_print_varaccess(void)
  69. {
  70. enum env_flags_varaccess curaccess = (enum env_flags_varaccess)0;
  71. while (curaccess != env_flags_varaccess_end) {
  72. printf("\t%c -\t%s\n", env_flags_varaccess_rep[curaccess],
  73. env_flags_varaccess_names[curaccess]);
  74. curaccess++;
  75. }
  76. }
  77. /*
  78. * Return the name of the type.
  79. */
  80. const char *env_flags_get_vartype_name(enum env_flags_vartype type)
  81. {
  82. return env_flags_vartype_names[type];
  83. }
  84. /*
  85. * Return the name of the access.
  86. */
  87. const char *env_flags_get_varaccess_name(enum env_flags_varaccess access)
  88. {
  89. return env_flags_varaccess_names[access];
  90. }
  91. #endif /* CONFIG_CMD_ENV_FLAGS */
  92. /*
  93. * Parse the flags string from a .flags attribute list into the vartype enum.
  94. */
  95. enum env_flags_vartype env_flags_parse_vartype(const char *flags)
  96. {
  97. char *type;
  98. if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
  99. return env_flags_vartype_string;
  100. type = strchr(env_flags_vartype_rep,
  101. flags[ENV_FLAGS_VARTYPE_LOC]);
  102. if (type != NULL)
  103. return (enum env_flags_vartype)
  104. (type - &env_flags_vartype_rep[0]);
  105. printf("## Warning: Unknown environment variable type '%c'\n",
  106. flags[ENV_FLAGS_VARTYPE_LOC]);
  107. return env_flags_vartype_string;
  108. }
  109. /*
  110. * Parse the flags string from a .flags attribute list into the varaccess enum.
  111. */
  112. enum env_flags_varaccess env_flags_parse_varaccess(const char *flags)
  113. {
  114. char *access;
  115. if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
  116. return env_flags_varaccess_any;
  117. access = strchr(env_flags_varaccess_rep,
  118. flags[ENV_FLAGS_VARACCESS_LOC]);
  119. if (access != NULL)
  120. return (enum env_flags_varaccess)
  121. (access - &env_flags_varaccess_rep[0]);
  122. printf("## Warning: Unknown environment variable access method '%c'\n",
  123. flags[ENV_FLAGS_VARACCESS_LOC]);
  124. return env_flags_varaccess_any;
  125. }
  126. /*
  127. * Parse the binary flags from a hash table entry into the varaccess enum.
  128. */
  129. enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags)
  130. {
  131. int i;
  132. for (i = 0; i < sizeof(env_flags_varaccess_mask); i++)
  133. if (env_flags_varaccess_mask[i] ==
  134. (binflags & ENV_FLAGS_VARACCESS_BIN_MASK))
  135. return (enum env_flags_varaccess)i;
  136. printf("Warning: Non-standard access flags. (0x%x)\n",
  137. binflags & ENV_FLAGS_VARACCESS_BIN_MASK);
  138. return env_flags_varaccess_any;
  139. }
  140. static inline int is_hex_prefix(const char *value)
  141. {
  142. return value[0] == '0' && (value[1] == 'x' || value[1] == 'X');
  143. }
  144. static void skip_num(int hex, const char *value, const char **end,
  145. int max_digits)
  146. {
  147. int i;
  148. if (hex && is_hex_prefix(value))
  149. value += 2;
  150. for (i = max_digits; i != 0; i--) {
  151. if (hex && !isxdigit(*value))
  152. break;
  153. if (!hex && !isdigit(*value))
  154. break;
  155. value++;
  156. }
  157. if (end != NULL)
  158. *end = value;
  159. }
  160. #ifdef CONFIG_CMD_NET
  161. int eth_validate_ethaddr_str(const char *addr)
  162. {
  163. const char *end;
  164. const char *cur;
  165. int i;
  166. cur = addr;
  167. for (i = 0; i < 6; i++) {
  168. skip_num(1, cur, &end, 2);
  169. if (cur == end)
  170. return -1;
  171. if (cur + 2 == end && is_hex_prefix(cur))
  172. return -1;
  173. if (i != 5 && *end != ':')
  174. return -1;
  175. if (i == 5 && *end != '\0')
  176. return -1;
  177. cur = end + 1;
  178. }
  179. return 0;
  180. }
  181. #endif
  182. /*
  183. * Based on the declared type enum, validate that the value string complies
  184. * with that format
  185. */
  186. static int _env_flags_validate_type(const char *value,
  187. enum env_flags_vartype type)
  188. {
  189. const char *end;
  190. #ifdef CONFIG_CMD_NET
  191. const char *cur;
  192. int i;
  193. #endif
  194. switch (type) {
  195. case env_flags_vartype_string:
  196. break;
  197. case env_flags_vartype_decimal:
  198. skip_num(0, value, &end, -1);
  199. if (*end != '\0')
  200. return -1;
  201. break;
  202. case env_flags_vartype_hex:
  203. skip_num(1, value, &end, -1);
  204. if (*end != '\0')
  205. return -1;
  206. if (value + 2 == end && is_hex_prefix(value))
  207. return -1;
  208. break;
  209. case env_flags_vartype_bool:
  210. if (value[0] != '1' && value[0] != 'y' && value[0] != 't' &&
  211. value[0] != 'Y' && value[0] != 'T' &&
  212. value[0] != '0' && value[0] != 'n' && value[0] != 'f' &&
  213. value[0] != 'N' && value[0] != 'F')
  214. return -1;
  215. if (value[1] != '\0')
  216. return -1;
  217. break;
  218. #ifdef CONFIG_CMD_NET
  219. case env_flags_vartype_ipaddr:
  220. cur = value;
  221. for (i = 0; i < 4; i++) {
  222. skip_num(0, cur, &end, 3);
  223. if (cur == end)
  224. return -1;
  225. if (i != 3 && *end != '.')
  226. return -1;
  227. if (i == 3 && *end != '\0')
  228. return -1;
  229. cur = end + 1;
  230. }
  231. break;
  232. case env_flags_vartype_macaddr:
  233. if (eth_validate_ethaddr_str(value))
  234. return -1;
  235. break;
  236. #endif
  237. case env_flags_vartype_end:
  238. return -1;
  239. }
  240. /* OK */
  241. return 0;
  242. }
  243. /*
  244. * Look for flags in a provided list and failing that the static list
  245. */
  246. static inline int env_flags_lookup(const char *flags_list, const char *name,
  247. char *flags)
  248. {
  249. int ret = 1;
  250. if (!flags)
  251. /* bad parameter */
  252. return -1;
  253. /* try the env first */
  254. if (flags_list)
  255. ret = env_attr_lookup(flags_list, name, flags);
  256. if (ret != 0)
  257. /* if not found in the env, look in the static list */
  258. ret = env_attr_lookup(ENV_FLAGS_LIST_STATIC, name, flags);
  259. return ret;
  260. }
  261. #ifdef USE_HOSTCC /* Functions only used from tools/env */
  262. /*
  263. * Look up any flags directly from the .flags variable and the static list
  264. * and convert them to the vartype enum.
  265. */
  266. enum env_flags_vartype env_flags_get_type(const char *name)
  267. {
  268. const char *flags_list = getenv(ENV_FLAGS_VAR);
  269. char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
  270. if (env_flags_lookup(flags_list, name, flags))
  271. return env_flags_vartype_string;
  272. if (strlen(flags) <= ENV_FLAGS_VARTYPE_LOC)
  273. return env_flags_vartype_string;
  274. return env_flags_parse_vartype(flags);
  275. }
  276. /*
  277. * Look up the access of a variable directly from the .flags var.
  278. */
  279. enum env_flags_varaccess env_flags_get_varaccess(const char *name)
  280. {
  281. const char *flags_list = getenv(ENV_FLAGS_VAR);
  282. char flags[ENV_FLAGS_ATTR_MAX_LEN + 1];
  283. if (env_flags_lookup(flags_list, name, flags))
  284. return env_flags_varaccess_any;
  285. if (strlen(flags) <= ENV_FLAGS_VARACCESS_LOC)
  286. return env_flags_varaccess_any;
  287. return env_flags_parse_varaccess(flags);
  288. }
  289. /*
  290. * Validate that the proposed new value for "name" is valid according to the
  291. * defined flags for that variable, if any.
  292. */
  293. int env_flags_validate_type(const char *name, const char *value)
  294. {
  295. enum env_flags_vartype type;
  296. if (value == NULL)
  297. return 0;
  298. type = env_flags_get_type(name);
  299. if (_env_flags_validate_type(value, type) < 0) {
  300. printf("## Error: flags type check failure for "
  301. "\"%s\" <= \"%s\" (type: %c)\n",
  302. name, value, env_flags_vartype_rep[type]);
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. /*
  308. * Validate that the proposed access to variable "name" is valid according to
  309. * the defined flags for that variable, if any.
  310. */
  311. int env_flags_validate_varaccess(const char *name, int check_mask)
  312. {
  313. enum env_flags_varaccess access;
  314. int access_mask;
  315. access = env_flags_get_varaccess(name);
  316. access_mask = env_flags_varaccess_mask[access];
  317. return (check_mask & access_mask) != 0;
  318. }
  319. /*
  320. * Validate the parameters to "env set" directly
  321. */
  322. int env_flags_validate_env_set_params(int argc, char * const argv[])
  323. {
  324. if ((argc >= 3) && argv[2] != NULL) {
  325. enum env_flags_vartype type = env_flags_get_type(argv[1]);
  326. /*
  327. * we don't currently check types that need more than
  328. * one argument
  329. */
  330. if (type != env_flags_vartype_string && argc > 3) {
  331. printf("## Error: too many parameters for setting "
  332. "\"%s\"\n", argv[1]);
  333. return -1;
  334. }
  335. return env_flags_validate_type(argv[1], argv[2]);
  336. }
  337. /* ok */
  338. return 0;
  339. }
  340. #else /* !USE_HOSTCC - Functions only used from lib/hashtable.c */
  341. /*
  342. * Parse the flag charachters from the .flags attribute list into the binary
  343. * form to be stored in the environment entry->flags field.
  344. */
  345. static int env_parse_flags_to_bin(const char *flags)
  346. {
  347. int binflags;
  348. binflags = env_flags_parse_vartype(flags) & ENV_FLAGS_VARTYPE_BIN_MASK;
  349. binflags |= env_flags_varaccess_mask[env_flags_parse_varaccess(flags)];
  350. return binflags;
  351. }
  352. static int first_call = 1;
  353. static const char *flags_list;
  354. /*
  355. * Look for possible flags for a newly added variable
  356. * This is called specifically when the variable did not exist in the hash
  357. * previously, so the blanket update did not find this variable.
  358. */
  359. void env_flags_init(ENTRY *var_entry)
  360. {
  361. const char *var_name = var_entry->key;
  362. char flags[ENV_FLAGS_ATTR_MAX_LEN + 1] = "";
  363. int ret = 1;
  364. if (first_call) {
  365. flags_list = getenv(ENV_FLAGS_VAR);
  366. first_call = 0;
  367. }
  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, void *priv)
  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, NULL);
  410. /* configure any dynamic flags */
  411. env_attr_walk(value, set_flags, NULL);
  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