env_callback.c 3.1 KB

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