1 // SPDX-License-Identifier: GPL-2.0+
8 #include <env_internal.h>
9 #include <asm/global_data.h>
12 * Look up a callback function pointer by name
14 static struct env_clbk_tbl *find_env_callback(const char *name)
16 struct env_clbk_tbl *clbkp;
18 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
23 /* look up the callback in the linker-list */
24 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
27 if (strcmp(name, clbkp->name) == 0)
34 static int first_call = 1;
35 static const char *callback_list;
38 * Look for a possible callback for a newly added variable
39 * This is called specifically when the variable did not exist in the hash
40 * previously, so the blanket update did not find this variable.
42 void env_callback_init(struct env_entry *var_entry)
44 const char *var_name = var_entry->key;
45 char callback_name[256] = "";
46 struct env_clbk_tbl *clbkp;
50 callback_list = env_get(ENV_CALLBACK_VAR);
54 var_entry->callback = NULL;
56 /* look in the ".callbacks" var for a reference to this variable */
57 if (callback_list != NULL)
58 ret = env_attr_lookup(callback_list, var_name, callback_name);
60 /* only if not found there, look in the static list */
62 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
65 /* if an association was found, set the callback pointer */
66 if (!ret && strlen(callback_name)) {
67 clbkp = find_env_callback(callback_name);
69 var_entry->callback = clbkp->callback;
74 * Called on each existing env var prior to the blanket update since removing
75 * a callback association should remove its callback.
77 static int clear_callback(struct env_entry *entry)
79 entry->callback = NULL;
85 * Call for each element in the list that associates variables to callbacks
87 static int set_callback(const char *name, const char *value, void *priv)
89 struct env_entry e, *ep;
90 struct env_clbk_tbl *clbkp;
95 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
97 /* does the env variable actually exist? */
99 /* the assocaition delares no callback, so remove the pointer */
100 if (value == NULL || strlen(value) == 0)
103 /* assign the requested callback */
104 clbkp = find_env_callback(value);
106 ep->callback = clbkp->callback;
113 static int on_callbacks(const char *name, const char *value, enum env_op op,
116 /* remove all callbacks */
117 hwalk_r(&env_htab, clear_callback);
119 /* configure any static callback bindings */
120 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
121 /* configure any dynamic callback bindings */
122 env_attr_walk(value, set_callback, NULL);
126 U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);