callback.c 3.2 KB

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