callback.c 3.3 KB

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