flags.c 14 KB

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