env_callback.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <common.h>
  24. #include <environment.h>
  25. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #endif
  28. /*
  29. * Look up a callback function pointer by name
  30. */
  31. static struct env_clbk_tbl *find_env_callback(const char *name)
  32. {
  33. struct env_clbk_tbl *clbkp;
  34. int i;
  35. int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
  36. if (name == NULL)
  37. return NULL;
  38. /* look up the callback in the linker-list */
  39. for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
  40. i < num_callbacks;
  41. i++, clbkp++) {
  42. if (strcmp(name, clbkp->name) == 0)
  43. return clbkp;
  44. }
  45. return NULL;
  46. }
  47. /*
  48. * Look for a possible callback for a newly added variable
  49. * This is called specifically when the variable did not exist in the hash
  50. * previously, so the blanket update did not find this variable.
  51. */
  52. void env_callback_init(ENTRY *var_entry)
  53. {
  54. const char *var_name = var_entry->key;
  55. const char *callback_list = getenv(ENV_CALLBACK_VAR);
  56. char callback_name[256] = "";
  57. struct env_clbk_tbl *clbkp;
  58. int ret = 1;
  59. /* look in the ".callbacks" var for a reference to this variable */
  60. if (callback_list != NULL)
  61. ret = env_attr_lookup(callback_list, var_name, callback_name);
  62. /* only if not found there, look in the static list */
  63. if (ret)
  64. ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
  65. callback_name);
  66. /* if an association was found, set the callback pointer */
  67. if (!ret && strlen(callback_name)) {
  68. clbkp = find_env_callback(callback_name);
  69. if (clbkp != NULL)
  70. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  71. var_entry->callback = clbkp->callback + gd->reloc_off;
  72. #else
  73. var_entry->callback = clbkp->callback;
  74. #endif
  75. }
  76. }
  77. /*
  78. * Called on each existing env var prior to the blanket update since removing
  79. * a callback association should remove its callback.
  80. */
  81. static int clear_callback(ENTRY *entry)
  82. {
  83. entry->callback = NULL;
  84. return 0;
  85. }
  86. /*
  87. * Call for each element in the list that associates variables to callbacks
  88. */
  89. static int set_callback(const char *name, const char *value)
  90. {
  91. ENTRY e, *ep;
  92. struct env_clbk_tbl *clbkp;
  93. e.key = name;
  94. e.data = NULL;
  95. hsearch_r(e, FIND, &ep, &env_htab, 0);
  96. /* does the env variable actually exist? */
  97. if (ep != NULL) {
  98. /* the assocaition delares no callback, so remove the pointer */
  99. if (value == NULL || strlen(value) == 0)
  100. ep->callback = NULL;
  101. else {
  102. /* assign the requested callback */
  103. clbkp = find_env_callback(value);
  104. if (clbkp != NULL)
  105. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  106. ep->callback = clbkp->callback + gd->reloc_off;
  107. #else
  108. ep->callback = clbkp->callback;
  109. #endif
  110. }
  111. }
  112. return 0;
  113. }
  114. static int on_callbacks(const char *name, const char *value, enum env_op op,
  115. int flags)
  116. {
  117. /* remove all callbacks */
  118. hwalk_r(&env_htab, clear_callback);
  119. /* configure any static callback bindings */
  120. env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback);
  121. /* configure any dynamic callback bindings */
  122. env_attr_walk(value, set_callback);
  123. return 0;
  124. }
  125. U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);