callback.c 3.3 KB

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