flags.c 13 KB

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