callback.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. var_entry->callback = NULL;
  49. /* look in the ".callbacks" var for a reference to this variable */
  50. if (callback_list != NULL)
  51. ret = env_attr_lookup(callback_list, var_name, callback_name);
  52. /* only if not found there, look in the static list */
  53. if (ret)
  54. ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
  55. callback_name);
  56. /* if an association was found, set the callback pointer */
  57. if (!ret && strlen(callback_name)) {
  58. clbkp = find_env_callback(callback_name);
  59. if (clbkp != NULL)
  60. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  61. var_entry->callback = clbkp->callback + gd->reloc_off;
  62. #else
  63. var_entry->callback = clbkp->callback;
  64. #endif
  65. }
  66. }
  67. /*
  68. * Called on each existing env var prior to the blanket update since removing
  69. * a callback association should remove its callback.
  70. */
  71. static int clear_callback(struct env_entry *entry)
  72. {
  73. entry->callback = NULL;
  74. return 0;
  75. }
  76. /*
  77. * Call for each element in the list that associates variables to callbacks
  78. */
  79. static int set_callback(const char *name, const char *value, void *priv)
  80. {
  81. struct env_entry e, *ep;
  82. struct env_clbk_tbl *clbkp;
  83. e.key = name;
  84. e.data = NULL;
  85. e.callback = NULL;
  86. hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
  87. /* does the env variable actually exist? */
  88. if (ep != NULL) {
  89. /* the assocaition delares no callback, so remove the pointer */
  90. if (value == NULL || strlen(value) == 0)
  91. ep->callback = NULL;
  92. else {
  93. /* assign the requested callback */
  94. clbkp = find_env_callback(value);
  95. if (clbkp != NULL)
  96. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  97. ep->callback = clbkp->callback + gd->reloc_off;
  98. #else
  99. ep->callback = clbkp->callback;
  100. #endif
  101. }
  102. }
  103. return 0;
  104. }
  105. static int on_callbacks(const char *name, const char *value, enum env_op op,
  106. int flags)
  107. {
  108. /* remove all callbacks */
  109. hwalk_r(&env_htab, clear_callback);
  110. /* configure any static callback bindings */
  111. env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
  112. /* configure any dynamic callback bindings */
  113. env_attr_walk(value, set_callback, NULL);
  114. return 0;
  115. }
  116. U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);