flags.c 13 KB

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