1 // SPDX-License-Identifier: GPL-2.0+
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
9 #include <env_internal.h>
10 #include <asm/global_data.h>
12 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
13 DECLARE_GLOBAL_DATA_PTR;
17 * Look up a callback function pointer by name
19 static struct env_clbk_tbl *find_env_callback(const char *name)
21 struct env_clbk_tbl *clbkp;
23 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
28 /* look up the callback in the linker-list */
29 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
32 if (strcmp(name, clbkp->name) == 0)
39 static int first_call = 1;
40 static const char *callback_list;
43 * Look for a possible callback for a newly added variable
44 * This is called specifically when the variable did not exist in the hash
45 * previously, so the blanket update did not find this variable.
47 void env_callback_init(struct env_entry *var_entry)
49 const char *var_name = var_entry->key;
50 char callback_name[256] = "";
51 struct env_clbk_tbl *clbkp;
55 callback_list = env_get(ENV_CALLBACK_VAR);
59 var_entry->callback = NULL;
61 /* look in the ".callbacks" var for a reference to this variable */
62 if (callback_list != NULL)
63 ret = env_attr_lookup(callback_list, var_name, callback_name);
65 /* only if not found there, look in the static list */
67 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
70 /* if an association was found, set the callback pointer */
71 if (!ret && strlen(callback_name)) {
72 clbkp = find_env_callback(callback_name);
74 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
75 var_entry->callback = clbkp->callback + gd->reloc_off;
77 var_entry->callback = clbkp->callback;
83 * Called on each existing env var prior to the blanket update since removing
84 * a callback association should remove its callback.
86 static int clear_callback(struct env_entry *entry)
88 entry->callback = NULL;
94 * Call for each element in the list that associates variables to callbacks
96 static int set_callback(const char *name, const char *value, void *priv)
98 struct env_entry e, *ep;
99 struct env_clbk_tbl *clbkp;
104 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
106 /* does the env variable actually exist? */
108 /* the assocaition delares no callback, so remove the pointer */
109 if (value == NULL || strlen(value) == 0)
112 /* assign the requested callback */
113 clbkp = find_env_callback(value);
115 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
116 ep->callback = clbkp->callback + gd->reloc_off;
118 ep->callback = clbkp->callback;
126 static int on_callbacks(const char *name, const char *value, enum env_op op,
129 /* remove all callbacks */
130 hwalk_r(&env_htab, clear_callback);
132 /* configure any static callback bindings */
133 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
134 /* configure any dynamic callback bindings */
135 env_attr_walk(value, set_callback, NULL);
139 U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);