blob: b7cbccd1175f7df8f2da78dc2186d1a5a7e88aa3 [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
Simon Glass7b51b572019-08-01 09:46:52 -06007#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -06008#include <env_internal.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Joe Hershberger170ab112012-12-11 22:16:24 -060010
Joe Hershberger170ab112012-12-11 22:16:24 -060011/*
12 * Look up a callback function pointer by name
13 */
Tom Rini268d9662013-03-12 06:16:50 +000014static struct env_clbk_tbl *find_env_callback(const char *name)
Joe Hershberger170ab112012-12-11 22:16:24 -060015{
16 struct env_clbk_tbl *clbkp;
17 int i;
18 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
19
20 if (name == NULL)
21 return NULL;
22
23 /* look up the callback in the linker-list */
24 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
25 i < num_callbacks;
26 i++, clbkp++) {
27 if (strcmp(name, clbkp->name) == 0)
28 return clbkp;
29 }
30
31 return NULL;
32}
33
Heiko Schocher1b610272013-12-19 13:45:04 +010034static int first_call = 1;
35static const char *callback_list;
36
Joe Hershberger170ab112012-12-11 22:16:24 -060037/*
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.
41 */
Simon Glassdd2408c2019-08-02 09:44:18 -060042void env_callback_init(struct env_entry *var_entry)
Joe Hershberger170ab112012-12-11 22:16:24 -060043{
44 const char *var_name = var_entry->key;
Joe Hershberger170ab112012-12-11 22:16:24 -060045 char callback_name[256] = "";
46 struct env_clbk_tbl *clbkp;
47 int ret = 1;
48
Heiko Schocher1b610272013-12-19 13:45:04 +010049 if (first_call) {
Simon Glass00caae62017-08-03 12:22:12 -060050 callback_list = env_get(ENV_CALLBACK_VAR);
Heiko Schocher1b610272013-12-19 13:45:04 +010051 first_call = 0;
52 }
53
Rasmus Villemoes080019b2020-02-27 13:56:12 +000054 var_entry->callback = NULL;
55
Joe Hershberger170ab112012-12-11 22:16:24 -060056 /* 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);
59
60 /* only if not found there, look in the static list */
61 if (ret)
62 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
63 callback_name);
64
65 /* if an association was found, set the callback pointer */
66 if (!ret && strlen(callback_name)) {
67 clbkp = find_env_callback(callback_name);
68 if (clbkp != NULL)
Joe Hershberger170ab112012-12-11 22:16:24 -060069 var_entry->callback = clbkp->callback;
Joe Hershberger170ab112012-12-11 22:16:24 -060070 }
71}
72
73/*
74 * Called on each existing env var prior to the blanket update since removing
75 * a callback association should remove its callback.
76 */
Simon Glassdd2408c2019-08-02 09:44:18 -060077static int clear_callback(struct env_entry *entry)
Joe Hershberger170ab112012-12-11 22:16:24 -060078{
79 entry->callback = NULL;
80
81 return 0;
82}
83
84/*
85 * Call for each element in the list that associates variables to callbacks
86 */
Joe Hershbergercca98fd2015-05-20 14:27:19 -050087static int set_callback(const char *name, const char *value, void *priv)
Joe Hershberger170ab112012-12-11 22:16:24 -060088{
Simon Glassdd2408c2019-08-02 09:44:18 -060089 struct env_entry e, *ep;
Joe Hershberger170ab112012-12-11 22:16:24 -060090 struct env_clbk_tbl *clbkp;
91
92 e.key = name;
93 e.data = NULL;
Peng Fan5a689432015-12-23 12:07:24 +080094 e.callback = NULL;
Simon Glass3f0d6802019-08-01 09:47:09 -060095 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Joe Hershberger170ab112012-12-11 22:16:24 -060096
97 /* does the env variable actually exist? */
98 if (ep != NULL) {
99 /* the assocaition delares no callback, so remove the pointer */
100 if (value == NULL || strlen(value) == 0)
101 ep->callback = NULL;
102 else {
103 /* assign the requested callback */
104 clbkp = find_env_callback(value);
105 if (clbkp != NULL)
Joe Hershberger170ab112012-12-11 22:16:24 -0600106 ep->callback = clbkp->callback;
Joe Hershberger170ab112012-12-11 22:16:24 -0600107 }
108 }
109
110 return 0;
111}
112
113static int on_callbacks(const char *name, const char *value, enum env_op op,
114 int flags)
115{
116 /* remove all callbacks */
117 hwalk_r(&env_htab, clear_callback);
118
119 /* configure any static callback bindings */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500120 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
Joe Hershberger170ab112012-12-11 22:16:24 -0600121 /* configure any dynamic callback bindings */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500122 env_attr_walk(value, set_callback, NULL);
Joe Hershberger170ab112012-12-11 22:16:24 -0600123
124 return 0;
125}
126U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);