blob: 638a02b28f78a73a675994aa37fa7942ad127bb0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershberger170ab112012-12-11 22:16:24 -06002/*
3 * (C) Copyright 2012
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
Joe Hershberger170ab112012-12-11 22:16:24 -06005 */
6
7#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -06008#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -06009#include <env_internal.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Joe Hershberger170ab112012-12-11 22:16:24 -060011
12#if defined(CONFIG_NEEDS_MANUAL_RELOC)
13DECLARE_GLOBAL_DATA_PTR;
14#endif
15
16/*
17 * Look up a callback function pointer by name
18 */
Tom Rini268d9662013-03-12 06:16:50 +000019static struct env_clbk_tbl *find_env_callback(const char *name)
Joe Hershberger170ab112012-12-11 22:16:24 -060020{
21 struct env_clbk_tbl *clbkp;
22 int i;
23 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
24
25 if (name == NULL)
26 return NULL;
27
28 /* look up the callback in the linker-list */
29 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
30 i < num_callbacks;
31 i++, clbkp++) {
32 if (strcmp(name, clbkp->name) == 0)
33 return clbkp;
34 }
35
36 return NULL;
37}
38
Heiko Schocher1b610272013-12-19 13:45:04 +010039static int first_call = 1;
40static const char *callback_list;
41
Joe Hershberger170ab112012-12-11 22:16:24 -060042/*
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.
46 */
Simon Glassdd2408c2019-08-02 09:44:18 -060047void env_callback_init(struct env_entry *var_entry)
Joe Hershberger170ab112012-12-11 22:16:24 -060048{
49 const char *var_name = var_entry->key;
Joe Hershberger170ab112012-12-11 22:16:24 -060050 char callback_name[256] = "";
51 struct env_clbk_tbl *clbkp;
52 int ret = 1;
53
Heiko Schocher1b610272013-12-19 13:45:04 +010054 if (first_call) {
Simon Glass00caae62017-08-03 12:22:12 -060055 callback_list = env_get(ENV_CALLBACK_VAR);
Heiko Schocher1b610272013-12-19 13:45:04 +010056 first_call = 0;
57 }
58
Rasmus Villemoes080019b2020-02-27 13:56:12 +000059 var_entry->callback = NULL;
60
Joe Hershberger170ab112012-12-11 22:16:24 -060061 /* 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);
64
65 /* only if not found there, look in the static list */
66 if (ret)
67 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
68 callback_name);
69
70 /* if an association was found, set the callback pointer */
71 if (!ret && strlen(callback_name)) {
72 clbkp = find_env_callback(callback_name);
73 if (clbkp != NULL)
74#if defined(CONFIG_NEEDS_MANUAL_RELOC)
75 var_entry->callback = clbkp->callback + gd->reloc_off;
76#else
77 var_entry->callback = clbkp->callback;
78#endif
79 }
80}
81
82/*
83 * Called on each existing env var prior to the blanket update since removing
84 * a callback association should remove its callback.
85 */
Simon Glassdd2408c2019-08-02 09:44:18 -060086static int clear_callback(struct env_entry *entry)
Joe Hershberger170ab112012-12-11 22:16:24 -060087{
88 entry->callback = NULL;
89
90 return 0;
91}
92
93/*
94 * Call for each element in the list that associates variables to callbacks
95 */
Joe Hershbergercca98fd2015-05-20 14:27:19 -050096static int set_callback(const char *name, const char *value, void *priv)
Joe Hershberger170ab112012-12-11 22:16:24 -060097{
Simon Glassdd2408c2019-08-02 09:44:18 -060098 struct env_entry e, *ep;
Joe Hershberger170ab112012-12-11 22:16:24 -060099 struct env_clbk_tbl *clbkp;
100
101 e.key = name;
102 e.data = NULL;
Peng Fan5a689432015-12-23 12:07:24 +0800103 e.callback = NULL;
Simon Glass3f0d6802019-08-01 09:47:09 -0600104 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Joe Hershberger170ab112012-12-11 22:16:24 -0600105
106 /* does the env variable actually exist? */
107 if (ep != NULL) {
108 /* the assocaition delares no callback, so remove the pointer */
109 if (value == NULL || strlen(value) == 0)
110 ep->callback = NULL;
111 else {
112 /* assign the requested callback */
113 clbkp = find_env_callback(value);
114 if (clbkp != NULL)
115#if defined(CONFIG_NEEDS_MANUAL_RELOC)
116 ep->callback = clbkp->callback + gd->reloc_off;
117#else
118 ep->callback = clbkp->callback;
119#endif
120 }
121 }
122
123 return 0;
124}
125
126static int on_callbacks(const char *name, const char *value, enum env_op op,
127 int flags)
128{
129 /* remove all callbacks */
130 hwalk_r(&env_htab, clear_callback);
131
132 /* configure any static callback bindings */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500133 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
Joe Hershberger170ab112012-12-11 22:16:24 -0600134 /* configure any dynamic callback bindings */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500135 env_attr_walk(value, set_callback, NULL);
Joe Hershberger170ab112012-12-11 22:16:24 -0600136
137 return 0;
138}
139U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);