1 // SPDX-License-Identifier: GPL-2.0+
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
9 #include <env_internal.h>
11 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
12 DECLARE_GLOBAL_DATA_PTR;
16 * Look up a callback function pointer by name
18 static struct env_clbk_tbl *find_env_callback(const char *name)
20 struct env_clbk_tbl *clbkp;
22 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
27 /* look up the callback in the linker-list */
28 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
31 if (strcmp(name, clbkp->name) == 0)
38 static int first_call = 1;
39 static const char *callback_list;
42 * Look for a possible callback for a newly added variable
43 * This is called specifically when the variable did not exist in the hash
44 * previously, so the blanket update did not find this variable.
46 void env_callback_init(struct env_entry *var_entry)
48 const char *var_name = var_entry->key;
49 char callback_name[256] = "";
50 struct env_clbk_tbl *clbkp;
54 callback_list = env_get(ENV_CALLBACK_VAR);
58 var_entry->callback = NULL;
60 /* look in the ".callbacks" var for a reference to this variable */
61 if (callback_list != NULL)
62 ret = env_attr_lookup(callback_list, var_name, callback_name);
64 /* only if not found there, look in the static list */
66 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
69 /* if an association was found, set the callback pointer */
70 if (!ret && strlen(callback_name)) {
71 clbkp = find_env_callback(callback_name);
73 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
74 var_entry->callback = clbkp->callback + gd->reloc_off;
76 var_entry->callback = clbkp->callback;
82 * Called on each existing env var prior to the blanket update since removing
83 * a callback association should remove its callback.
85 static int clear_callback(struct env_entry *entry)
87 entry->callback = NULL;
93 * Call for each element in the list that associates variables to callbacks
95 static int set_callback(const char *name, const char *value, void *priv)
97 struct env_entry e, *ep;
98 struct env_clbk_tbl *clbkp;
103 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
105 /* does the env variable actually exist? */
107 /* the assocaition delares no callback, so remove the pointer */
108 if (value == NULL || strlen(value) == 0)
111 /* assign the requested callback */
112 clbkp = find_env_callback(value);
114 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
115 ep->callback = clbkp->callback + gd->reloc_off;
117 ep->callback = clbkp->callback;
125 static int on_callbacks(const char *name, const char *value, enum env_op op,
128 /* remove all callbacks */
129 hwalk_r(&env_htab, clear_callback);
131 /* configure any static callback bindings */
132 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
133 /* configure any dynamic callback bindings */
134 env_attr_walk(value, set_callback, NULL);
138 U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);