blob: 98ddba035ea74cf47ab68c31d02c366fb6418784 [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
Joe Hershberger170ab112012-12-11 22:16:24 -060012/*
13 * Look up a callback function pointer by name
14 */
Tom Rini268d9662013-03-12 06:16:50 +000015static struct env_clbk_tbl *find_env_callback(const char *name)
Joe Hershberger170ab112012-12-11 22:16:24 -060016{
17 struct env_clbk_tbl *clbkp;
18 int i;
19 int num_callbacks = ll_entry_count(struct env_clbk_tbl, env_clbk);
20
21 if (name == NULL)
22 return NULL;
23
24 /* look up the callback in the linker-list */
25 for (i = 0, clbkp = ll_entry_start(struct env_clbk_tbl, env_clbk);
26 i < num_callbacks;
27 i++, clbkp++) {
28 if (strcmp(name, clbkp->name) == 0)
29 return clbkp;
30 }
31
32 return NULL;
33}
34
Heiko Schocher1b610272013-12-19 13:45:04 +010035static int first_call = 1;
36static const char *callback_list;
37
Joe Hershberger170ab112012-12-11 22:16:24 -060038/*
39 * Look for a possible callback for a newly added variable
40 * This is called specifically when the variable did not exist in the hash
41 * previously, so the blanket update did not find this variable.
42 */
Simon Glassdd2408c2019-08-02 09:44:18 -060043void env_callback_init(struct env_entry *var_entry)
Joe Hershberger170ab112012-12-11 22:16:24 -060044{
45 const char *var_name = var_entry->key;
Joe Hershberger170ab112012-12-11 22:16:24 -060046 char callback_name[256] = "";
47 struct env_clbk_tbl *clbkp;
48 int ret = 1;
49
Heiko Schocher1b610272013-12-19 13:45:04 +010050 if (first_call) {
Simon Glass00caae62017-08-03 12:22:12 -060051 callback_list = env_get(ENV_CALLBACK_VAR);
Heiko Schocher1b610272013-12-19 13:45:04 +010052 first_call = 0;
53 }
54
Rasmus Villemoes080019b2020-02-27 13:56:12 +000055 var_entry->callback = NULL;
56
Joe Hershberger170ab112012-12-11 22:16:24 -060057 /* look in the ".callbacks" var for a reference to this variable */
58 if (callback_list != NULL)
59 ret = env_attr_lookup(callback_list, var_name, callback_name);
60
61 /* only if not found there, look in the static list */
62 if (ret)
63 ret = env_attr_lookup(ENV_CALLBACK_LIST_STATIC, var_name,
64 callback_name);
65
66 /* if an association was found, set the callback pointer */
67 if (!ret && strlen(callback_name)) {
68 clbkp = find_env_callback(callback_name);
69 if (clbkp != NULL)
Joe Hershberger170ab112012-12-11 22:16:24 -060070 var_entry->callback = clbkp->callback;
Joe Hershberger170ab112012-12-11 22:16:24 -060071 }
72}
73
74/*
75 * Called on each existing env var prior to the blanket update since removing
76 * a callback association should remove its callback.
77 */
Simon Glassdd2408c2019-08-02 09:44:18 -060078static int clear_callback(struct env_entry *entry)
Joe Hershberger170ab112012-12-11 22:16:24 -060079{
80 entry->callback = NULL;
81
82 return 0;
83}
84
85/*
86 * Call for each element in the list that associates variables to callbacks
87 */
Joe Hershbergercca98fd2015-05-20 14:27:19 -050088static int set_callback(const char *name, const char *value, void *priv)
Joe Hershberger170ab112012-12-11 22:16:24 -060089{
Simon Glassdd2408c2019-08-02 09:44:18 -060090 struct env_entry e, *ep;
Joe Hershberger170ab112012-12-11 22:16:24 -060091 struct env_clbk_tbl *clbkp;
92
93 e.key = name;
94 e.data = NULL;
Peng Fan5a689432015-12-23 12:07:24 +080095 e.callback = NULL;
Simon Glass3f0d6802019-08-01 09:47:09 -060096 hsearch_r(e, ENV_FIND, &ep, &env_htab, 0);
Joe Hershberger170ab112012-12-11 22:16:24 -060097
98 /* does the env variable actually exist? */
99 if (ep != NULL) {
100 /* the assocaition delares no callback, so remove the pointer */
101 if (value == NULL || strlen(value) == 0)
102 ep->callback = NULL;
103 else {
104 /* assign the requested callback */
105 clbkp = find_env_callback(value);
106 if (clbkp != NULL)
Joe Hershberger170ab112012-12-11 22:16:24 -0600107 ep->callback = clbkp->callback;
Joe Hershberger170ab112012-12-11 22:16:24 -0600108 }
109 }
110
111 return 0;
112}
113
114static int on_callbacks(const char *name, const char *value, enum env_op op,
115 int flags)
116{
117 /* remove all callbacks */
118 hwalk_r(&env_htab, clear_callback);
119
120 /* configure any static callback bindings */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500121 env_attr_walk(ENV_CALLBACK_LIST_STATIC, set_callback, NULL);
Joe Hershberger170ab112012-12-11 22:16:24 -0600122 /* configure any dynamic callback bindings */
Joe Hershbergercca98fd2015-05-20 14:27:19 -0500123 env_attr_walk(value, set_callback, NULL);
Joe Hershberger170ab112012-12-11 22:16:24 -0600124
125 return 0;
126}
127U_BOOT_ENV_CALLBACK(callbacks, on_callbacks);